Commit ed1908c0 authored by Marco Bubke's avatar Marco Bubke Committed by Tim Jenssen

QmlDesigner: Refactor ItemLibraryModel

Change-Id: Ic3768bbd3ed3f65b456b0e9e2b4c8ab9497ca1ce
Reviewed-by: default avatarTim Jenssen <tim.jenssen@digia.com>
parent c244eb18
......@@ -257,17 +257,47 @@ void ItemLibraryModel::setExpanded(bool expanded, const QString &section)
}
ItemLibraryModel::ItemLibraryModel(QObject *parent)
: ItemLibrarySortedModel(parent),
m_searchText(""),
: QAbstractListModel(parent),
m_itemIconSize(64, 64),
m_nextLibId(0)
{
addRoleNames();
}
ItemLibraryModel::~ItemLibraryModel()
{
clearSections();
}
int ItemLibraryModel::rowCount(const QModelIndex & /*parent*/) const
{
return visibleSectionCount();
}
QVariant ItemLibraryModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() +1 > visibleSectionCount())
return QVariant();
if (m_roleNames.contains(role)) {
QVariant value = visibleSections().at(index.row())->property(m_roleNames.value(role));
ItemLibrarySortedModel* model = qobject_cast<ItemLibrarySortedModel *>(value.value<QObject*>());
if (model)
return QVariant::fromValue(model);
ItemLibraryModel* model2 = qobject_cast<ItemLibraryModel *>(value.value<QObject*>());
if (model2)
return QVariant::fromValue(model2);
return value;
}
qWarning() << Q_FUNC_INFO << "invalid role requested";
return QVariant();
}
QString ItemLibraryModel::searchText() const
{
......@@ -319,7 +349,7 @@ bool ItemLibraryModel::isItemVisible(int itemLibId)
return false;
int sectionLibId = m_sections.value(itemLibId);
if (!elementVisible(sectionLibId))
if (section(sectionLibId)->isVisible())
return false;
return section(sectionLibId)->isItemVisible(itemLibId);
......@@ -342,7 +372,7 @@ void ItemLibraryModel::update(ItemLibraryInfo *itemLibraryInfo, Model *model)
QMap<QString, int> sections;
clearElements();
clearSections();
m_itemInfos.clear();
m_sections.clear();
m_nextLibId = 0;
......@@ -371,7 +401,7 @@ void ItemLibraryModel::update(ItemLibraryInfo *itemLibraryInfo, Model *model)
} else {
sectionId = m_nextLibId++;
sectionModel = new ItemLibrarySectionModel(sectionId, itemSectionName, this);
addElement(sectionModel, sectionId);
addSection(sectionModel, sectionId);
sections.insert(itemSectionName, sectionId);
}
......@@ -429,14 +459,65 @@ QIcon ItemLibraryModel::getIcon(int libId)
return m_itemInfos.value(libId).icon();
}
ItemLibrarySectionModel *ItemLibraryModel::section(int libId)
ItemLibrarySectionModel *ItemLibraryModel::section(int libraryId)
{
return elementByType<ItemLibrarySectionModel*>(libId);
return m_sectionModels.value(libraryId);
}
QList<ItemLibrarySectionModel *> ItemLibraryModel::sections() const
{
return elementsByType<ItemLibrarySectionModel*>();
return m_sectionModels.values();
}
void ItemLibraryModel::addSection(ItemLibrarySectionModel *sectionModel, int sectionId)
{
m_sectionModels.insert(sectionId, sectionModel);
sectionModel->setVisible(true);
}
void ItemLibraryModel::clearSections()
{
beginResetModel();
qDeleteAll(m_sectionModels);
m_sectionModels.clear();
endResetModel();
}
void ItemLibraryModel::registerQmlTypes()
{
qmlRegisterType<QmlDesigner::ItemLibrarySortedModel>();
qmlRegisterType<QmlDesigner::ItemLibraryModel>();
}
int ItemLibraryModel::visibleSectionCount() const
{
int visibleCount = 0;
QMap<int, ItemLibrarySectionModel*>::const_iterator sectionIterator = m_sectionModels.constBegin();
while (sectionIterator != m_sectionModels.constEnd()) {
ItemLibrarySectionModel *sectionModel = sectionIterator.value();
if (sectionModel->isVisible())
++visibleCount;
++sectionIterator;
qDebug() << __FUNCTION__ << visibleCount;
}
return visibleCount;
}
QList<ItemLibrarySectionModel *> ItemLibraryModel::visibleSections() const
{
QList<ItemLibrarySectionModel *> visibleSectionList;
QMap<int, ItemLibrarySectionModel*>::const_iterator sectionIterator = m_sectionModels.constBegin();
while (sectionIterator != m_sectionModels.constEnd()) {
ItemLibrarySectionModel *sectionModel = sectionIterator.value();
if (sectionModel->isVisible())
visibleSectionList.append(sectionModel);
++sectionIterator;
}
return visibleSectionList;
}
void ItemLibraryModel::updateVisibility()
......@@ -445,14 +526,14 @@ void ItemLibraryModel::updateVisibility()
endResetModel();
bool changed = false;
QMap<int, QObject *>::const_iterator sectionIt = elements().constBegin();
while (sectionIt != elements().constEnd()) {
ItemLibrarySectionModel *sectionModel = section(sectionIt.key());
QMap<int, ItemLibrarySectionModel*>::const_iterator sectionIterator = m_sectionModels.constBegin();
while (sectionIterator != m_sectionModels.constEnd()) {
ItemLibrarySectionModel *sectionModel = sectionIterator.value();
QString sectionSearchText = m_searchText;
if (sectionModel->sectionName().toLower().contains(m_searchText))
sectionSearchText = "";
sectionSearchText.clear();
bool sectionChanged = false,
sectionVisibility = sectionModel->updateSectionVisibility(sectionSearchText,
......@@ -460,17 +541,29 @@ void ItemLibraryModel::updateVisibility()
if (sectionChanged) {
changed = true;
if (sectionVisibility)
emit sectionVisibilityChanged(sectionIt.key());
emit sectionVisibilityChanged(sectionIterator.key());
}
changed |= setElementVisible(sectionIt.key(), sectionVisibility);
++sectionIt;
changed |= sectionModel->setVisible(sectionVisibility);
++sectionIterator;
}
if (changed)
emit visibilityChanged();
}
void ItemLibraryModel::addRoleNames()
{
int role = 0;
for (int propertyIndex = 0; propertyIndex < ItemLibrarySectionModel::staticMetaObject.propertyCount(); ++propertyIndex) {
QMetaProperty property = ItemLibrarySectionModel::staticMetaObject.property(propertyIndex);
m_roleNames.insert(role, property.name());
++role;
}
setRoleNames(m_roleNames);
}
int ItemLibraryModel::getWidth(const ItemLibraryEntry &itemLibraryEntry)
{
foreach (const ItemLibraryEntry::Property &property, itemLibraryEntry.properties())
......@@ -478,6 +571,7 @@ int ItemLibraryModel::getWidth(const ItemLibraryEntry &itemLibraryEntry)
if (property.name() == "width")
return property.value().toInt();
}
return 64;
}
......@@ -488,6 +582,7 @@ int ItemLibraryModel::getHeight(const ItemLibraryEntry &itemLibraryEntry)
if (property.name() == "height")
return property.value().toInt();
}
return 64;
}
......
......@@ -44,8 +44,6 @@ class ItemLibraryEntry;
class Model;
class ItemLibrarySectionModel;
void registerQmlTypes();
class ItemLibrarySortedModel: public QAbstractListModel {
Q_OBJECT
......@@ -98,7 +96,7 @@ private:
QHash<int, QByteArray> m_roleNames;
};
class ItemLibraryModel: public ItemLibrarySortedModel {
class ItemLibraryModel: public QAbstractListModel {
Q_OBJECT
Q_PROPERTY(QString searchText READ searchText WRITE setSearchText NOTIFY searchTextChanged)
......@@ -107,6 +105,9 @@ public:
explicit ItemLibraryModel(QObject *parent = 0);
~ItemLibraryModel();
int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
QString searchText() const;
void update(ItemLibraryInfo *itemLibraryInfo, Model *model);
......@@ -115,8 +116,16 @@ public:
QMimeData *getMimeData(int libId);
QIcon getIcon(int libId);
ItemLibrarySectionModel* section(int libId);
ItemLibrarySectionModel* section(int libraryId);
QList<ItemLibrarySectionModel*> sections() const;
void addSection(ItemLibrarySectionModel *sectionModel, int sectionId);
void clearSections();
static void registerQmlTypes();
int visibleSectionCount() const;
QList<ItemLibrarySectionModel*> visibleSections() const;
public slots:
void setSearchText(const QString &searchText);
......@@ -133,15 +142,19 @@ signals:
void visibilityChanged();
void sectionVisibilityChanged(int changedSectionLibId);
private:
private: // functions
void updateVisibility();
void addRoleNames();
int getWidth(const ItemLibraryEntry &entry);
int getHeight(const ItemLibraryEntry &entry);
QPixmap createDragPixmap(int width, int height);
private: // variables
QMap<int, ItemLibrarySectionModel*> m_sectionModels;
QMap<int, ItemLibraryEntry> m_itemInfos;
QMap<int, int> m_sections;
QHash<int, QByteArray> m_roleNames;
QString m_searchText;
QSize m_itemIconSize;
......@@ -151,6 +164,6 @@ private:
} // namespace QmlDesigner
QML_DECLARE_TYPE(QmlDesigner::ItemLibrarySortedModel)
QML_DECLARE_TYPE(QmlDesigner::ItemLibraryModel)
#endif // ITEMLIBRARYMODEL_H
......@@ -36,9 +36,10 @@ namespace QmlDesigner {
ItemLibrarySectionModel::ItemLibrarySectionModel(int sectionLibId, const QString &sectionName, QObject *parent)
: QObject(parent),
m_name(sectionName),
m_sectionLibId(sectionLibId),
m_sectionLibraryId(sectionLibId),
m_sectionExpanded(true),
m_sectionEntries(parent)
m_sectionEntries(parent),
m_isVisible(false)
{
// if (collapsedStateHash.contains(sectionName))
// m_sectionExpanded= collapsedStateHash.value(sectionName);
......@@ -50,9 +51,9 @@ QString ItemLibrarySectionModel::sectionName() const
return m_name;
}
int ItemLibrarySectionModel::sectionLibId() const
int ItemLibrarySectionModel::sectionLibraryId() const
{
return m_sectionLibId;
return m_sectionLibraryId;
}
bool ItemLibrarySectionModel::sectionExpanded() const
......@@ -132,7 +133,22 @@ void ItemLibrarySectionModel::updateItemIconSize(const QSize &itemIconSize)
{
// foreach (ItemLibraryItemModel* itemLibraryItemModel, m_sectionEntries.elementsByType<ItemLibraryItemModel*>()) {
// itemLibraryItemModel->setItemIconSize(itemIconSize);
// }
// }
}
bool ItemLibrarySectionModel::setVisible(bool isVisible)
{
if (isVisible != m_isVisible) {
m_isVisible = isVisible;
return true;
}
return false;
}
bool ItemLibrarySectionModel::isVisible() const
{
return m_isVisible;
}
} // namespace QmlDesigner
......@@ -43,16 +43,16 @@ class ItemLibrarySectionModel: public QObject {
Q_OBJECT
Q_PROPERTY(QObject* sectionEntries READ sectionEntries NOTIFY sectionEntriesChanged FINAL)
Q_PROPERTY(int sectionLibId READ sectionLibId FINAL)
Q_PROPERTY(int sectionLibraryId READ sectionLibraryId FINAL)
Q_PROPERTY(QString sectionName READ sectionName FINAL)
Q_PROPERTY(bool sectionExpanded READ sectionExpanded FINAL)
Q_PROPERTY(QVariant sortingRole READ sortingRole FINAL)
public:
ItemLibrarySectionModel(int sectionLibId, const QString &sectionName, QObject *parent = 0);
ItemLibrarySectionModel(int sectionLibraryId, const QString &sectionName, QObject *parent = 0);
QString sectionName() const;
int sectionLibId() const;
int sectionLibraryId() const;
bool sectionExpanded() const;
QVariant sortingRole() const;
......@@ -66,14 +66,18 @@ public:
bool updateSectionVisibility(const QString &searchText, bool *changed);
void updateItemIconSize(const QSize &itemIconSize);
bool setVisible(bool isVisible);
bool isVisible() const;
signals:
void sectionEntriesChanged();
private:
ItemLibrarySortedModel m_sectionEntries;
QString m_name;
int m_sectionLibId;
int m_sectionLibraryId;
bool m_sectionExpanded;
ItemLibrarySortedModel m_sectionEntries;
bool m_isVisible;
};
} // namespace QmlDesigner
......
......@@ -67,7 +67,7 @@ ItemLibraryWidget::ItemLibraryWidget(QWidget *parent) :
m_filterFlag(QtBasic),
m_itemLibraryId(-1)
{
registerQmlTypes();
ItemLibraryModel::registerQmlTypes();
setWindowTitle(tr("Library", "Title of library view"));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment