forked from qt-creator/qt-creator
QmlDesigner: Refactor ItemLibraryModel
Change-Id: Ic3768bbd3ed3f65b456b0e9e2b4c8ab9497ca1ce Reviewed-by: Tim Jenssen <tim.jenssen@digia.com>
This commit is contained in:
@@ -257,17 +257,47 @@ void ItemLibraryModel::setExpanded(bool expanded, const QString §ion)
|
|||||||
}
|
}
|
||||||
|
|
||||||
ItemLibraryModel::ItemLibraryModel(QObject *parent)
|
ItemLibraryModel::ItemLibraryModel(QObject *parent)
|
||||||
: ItemLibrarySortedModel(parent),
|
: QAbstractListModel(parent),
|
||||||
m_searchText(""),
|
|
||||||
m_itemIconSize(64, 64),
|
m_itemIconSize(64, 64),
|
||||||
m_nextLibId(0)
|
m_nextLibId(0)
|
||||||
{
|
{
|
||||||
|
addRoleNames();
|
||||||
}
|
}
|
||||||
|
|
||||||
ItemLibraryModel::~ItemLibraryModel()
|
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
|
QString ItemLibraryModel::searchText() const
|
||||||
{
|
{
|
||||||
@@ -319,7 +349,7 @@ bool ItemLibraryModel::isItemVisible(int itemLibId)
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
int sectionLibId = m_sections.value(itemLibId);
|
int sectionLibId = m_sections.value(itemLibId);
|
||||||
if (!elementVisible(sectionLibId))
|
if (section(sectionLibId)->isVisible())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return section(sectionLibId)->isItemVisible(itemLibId);
|
return section(sectionLibId)->isItemVisible(itemLibId);
|
||||||
@@ -342,7 +372,7 @@ void ItemLibraryModel::update(ItemLibraryInfo *itemLibraryInfo, Model *model)
|
|||||||
|
|
||||||
QMap<QString, int> sections;
|
QMap<QString, int> sections;
|
||||||
|
|
||||||
clearElements();
|
clearSections();
|
||||||
m_itemInfos.clear();
|
m_itemInfos.clear();
|
||||||
m_sections.clear();
|
m_sections.clear();
|
||||||
m_nextLibId = 0;
|
m_nextLibId = 0;
|
||||||
@@ -371,7 +401,7 @@ void ItemLibraryModel::update(ItemLibraryInfo *itemLibraryInfo, Model *model)
|
|||||||
} else {
|
} else {
|
||||||
sectionId = m_nextLibId++;
|
sectionId = m_nextLibId++;
|
||||||
sectionModel = new ItemLibrarySectionModel(sectionId, itemSectionName, this);
|
sectionModel = new ItemLibrarySectionModel(sectionId, itemSectionName, this);
|
||||||
addElement(sectionModel, sectionId);
|
addSection(sectionModel, sectionId);
|
||||||
sections.insert(itemSectionName, sectionId);
|
sections.insert(itemSectionName, sectionId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -429,14 +459,65 @@ QIcon ItemLibraryModel::getIcon(int libId)
|
|||||||
return m_itemInfos.value(libId).icon();
|
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
|
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()
|
void ItemLibraryModel::updateVisibility()
|
||||||
@@ -445,14 +526,14 @@ void ItemLibraryModel::updateVisibility()
|
|||||||
endResetModel();
|
endResetModel();
|
||||||
bool changed = false;
|
bool changed = false;
|
||||||
|
|
||||||
QMap<int, QObject *>::const_iterator sectionIt = elements().constBegin();
|
QMap<int, ItemLibrarySectionModel*>::const_iterator sectionIterator = m_sectionModels.constBegin();
|
||||||
while (sectionIt != elements().constEnd()) {
|
while (sectionIterator != m_sectionModels.constEnd()) {
|
||||||
ItemLibrarySectionModel *sectionModel = section(sectionIt.key());
|
ItemLibrarySectionModel *sectionModel = sectionIterator.value();
|
||||||
|
|
||||||
QString sectionSearchText = m_searchText;
|
QString sectionSearchText = m_searchText;
|
||||||
|
|
||||||
if (sectionModel->sectionName().toLower().contains(m_searchText))
|
if (sectionModel->sectionName().toLower().contains(m_searchText))
|
||||||
sectionSearchText = "";
|
sectionSearchText.clear();
|
||||||
|
|
||||||
bool sectionChanged = false,
|
bool sectionChanged = false,
|
||||||
sectionVisibility = sectionModel->updateSectionVisibility(sectionSearchText,
|
sectionVisibility = sectionModel->updateSectionVisibility(sectionSearchText,
|
||||||
@@ -460,17 +541,29 @@ void ItemLibraryModel::updateVisibility()
|
|||||||
if (sectionChanged) {
|
if (sectionChanged) {
|
||||||
changed = true;
|
changed = true;
|
||||||
if (sectionVisibility)
|
if (sectionVisibility)
|
||||||
emit sectionVisibilityChanged(sectionIt.key());
|
emit sectionVisibilityChanged(sectionIterator.key());
|
||||||
}
|
}
|
||||||
|
|
||||||
changed |= setElementVisible(sectionIt.key(), sectionVisibility);
|
changed |= sectionModel->setVisible(sectionVisibility);
|
||||||
++sectionIt;
|
++sectionIterator;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (changed)
|
if (changed)
|
||||||
emit visibilityChanged();
|
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)
|
int ItemLibraryModel::getWidth(const ItemLibraryEntry &itemLibraryEntry)
|
||||||
{
|
{
|
||||||
foreach (const ItemLibraryEntry::Property &property, itemLibraryEntry.properties())
|
foreach (const ItemLibraryEntry::Property &property, itemLibraryEntry.properties())
|
||||||
@@ -478,6 +571,7 @@ int ItemLibraryModel::getWidth(const ItemLibraryEntry &itemLibraryEntry)
|
|||||||
if (property.name() == "width")
|
if (property.name() == "width")
|
||||||
return property.value().toInt();
|
return property.value().toInt();
|
||||||
}
|
}
|
||||||
|
|
||||||
return 64;
|
return 64;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -488,6 +582,7 @@ int ItemLibraryModel::getHeight(const ItemLibraryEntry &itemLibraryEntry)
|
|||||||
if (property.name() == "height")
|
if (property.name() == "height")
|
||||||
return property.value().toInt();
|
return property.value().toInt();
|
||||||
}
|
}
|
||||||
|
|
||||||
return 64;
|
return 64;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -44,8 +44,6 @@ class ItemLibraryEntry;
|
|||||||
class Model;
|
class Model;
|
||||||
class ItemLibrarySectionModel;
|
class ItemLibrarySectionModel;
|
||||||
|
|
||||||
void registerQmlTypes();
|
|
||||||
|
|
||||||
class ItemLibrarySortedModel: public QAbstractListModel {
|
class ItemLibrarySortedModel: public QAbstractListModel {
|
||||||
|
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
@@ -98,7 +96,7 @@ private:
|
|||||||
QHash<int, QByteArray> m_roleNames;
|
QHash<int, QByteArray> m_roleNames;
|
||||||
};
|
};
|
||||||
|
|
||||||
class ItemLibraryModel: public ItemLibrarySortedModel {
|
class ItemLibraryModel: public QAbstractListModel {
|
||||||
|
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
Q_PROPERTY(QString searchText READ searchText WRITE setSearchText NOTIFY searchTextChanged)
|
Q_PROPERTY(QString searchText READ searchText WRITE setSearchText NOTIFY searchTextChanged)
|
||||||
@@ -107,6 +105,9 @@ public:
|
|||||||
explicit ItemLibraryModel(QObject *parent = 0);
|
explicit ItemLibraryModel(QObject *parent = 0);
|
||||||
~ItemLibraryModel();
|
~ItemLibraryModel();
|
||||||
|
|
||||||
|
int rowCount(const QModelIndex &parent = QModelIndex()) const;
|
||||||
|
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
|
||||||
|
|
||||||
QString searchText() const;
|
QString searchText() const;
|
||||||
|
|
||||||
void update(ItemLibraryInfo *itemLibraryInfo, Model *model);
|
void update(ItemLibraryInfo *itemLibraryInfo, Model *model);
|
||||||
@@ -115,8 +116,16 @@ public:
|
|||||||
QMimeData *getMimeData(int libId);
|
QMimeData *getMimeData(int libId);
|
||||||
QIcon getIcon(int libId);
|
QIcon getIcon(int libId);
|
||||||
|
|
||||||
ItemLibrarySectionModel* section(int libId);
|
ItemLibrarySectionModel* section(int libraryId);
|
||||||
QList<ItemLibrarySectionModel*> sections() const;
|
QList<ItemLibrarySectionModel*> sections() const;
|
||||||
|
void addSection(ItemLibrarySectionModel *sectionModel, int sectionId);
|
||||||
|
|
||||||
|
void clearSections();
|
||||||
|
|
||||||
|
static void registerQmlTypes();
|
||||||
|
|
||||||
|
int visibleSectionCount() const;
|
||||||
|
QList<ItemLibrarySectionModel*> visibleSections() const;
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void setSearchText(const QString &searchText);
|
void setSearchText(const QString &searchText);
|
||||||
@@ -133,15 +142,19 @@ signals:
|
|||||||
void visibilityChanged();
|
void visibilityChanged();
|
||||||
void sectionVisibilityChanged(int changedSectionLibId);
|
void sectionVisibilityChanged(int changedSectionLibId);
|
||||||
|
|
||||||
private:
|
private: // functions
|
||||||
void updateVisibility();
|
void updateVisibility();
|
||||||
|
void addRoleNames();
|
||||||
|
|
||||||
int getWidth(const ItemLibraryEntry &entry);
|
int getWidth(const ItemLibraryEntry &entry);
|
||||||
int getHeight(const ItemLibraryEntry &entry);
|
int getHeight(const ItemLibraryEntry &entry);
|
||||||
QPixmap createDragPixmap(int width, int height);
|
QPixmap createDragPixmap(int width, int height);
|
||||||
|
|
||||||
|
private: // variables
|
||||||
|
QMap<int, ItemLibrarySectionModel*> m_sectionModels;
|
||||||
QMap<int, ItemLibraryEntry> m_itemInfos;
|
QMap<int, ItemLibraryEntry> m_itemInfos;
|
||||||
QMap<int, int> m_sections;
|
QMap<int, int> m_sections;
|
||||||
|
QHash<int, QByteArray> m_roleNames;
|
||||||
|
|
||||||
QString m_searchText;
|
QString m_searchText;
|
||||||
QSize m_itemIconSize;
|
QSize m_itemIconSize;
|
||||||
@@ -151,6 +164,6 @@ private:
|
|||||||
} // namespace QmlDesigner
|
} // namespace QmlDesigner
|
||||||
|
|
||||||
QML_DECLARE_TYPE(QmlDesigner::ItemLibrarySortedModel)
|
QML_DECLARE_TYPE(QmlDesigner::ItemLibrarySortedModel)
|
||||||
|
QML_DECLARE_TYPE(QmlDesigner::ItemLibraryModel)
|
||||||
#endif // ITEMLIBRARYMODEL_H
|
#endif // ITEMLIBRARYMODEL_H
|
||||||
|
|
||||||
|
|||||||
@@ -36,9 +36,10 @@ namespace QmlDesigner {
|
|||||||
ItemLibrarySectionModel::ItemLibrarySectionModel(int sectionLibId, const QString §ionName, QObject *parent)
|
ItemLibrarySectionModel::ItemLibrarySectionModel(int sectionLibId, const QString §ionName, QObject *parent)
|
||||||
: QObject(parent),
|
: QObject(parent),
|
||||||
m_name(sectionName),
|
m_name(sectionName),
|
||||||
m_sectionLibId(sectionLibId),
|
m_sectionLibraryId(sectionLibId),
|
||||||
m_sectionExpanded(true),
|
m_sectionExpanded(true),
|
||||||
m_sectionEntries(parent)
|
m_sectionEntries(parent),
|
||||||
|
m_isVisible(false)
|
||||||
{
|
{
|
||||||
// if (collapsedStateHash.contains(sectionName))
|
// if (collapsedStateHash.contains(sectionName))
|
||||||
// m_sectionExpanded= collapsedStateHash.value(sectionName);
|
// m_sectionExpanded= collapsedStateHash.value(sectionName);
|
||||||
@@ -50,9 +51,9 @@ QString ItemLibrarySectionModel::sectionName() const
|
|||||||
return m_name;
|
return m_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ItemLibrarySectionModel::sectionLibId() const
|
int ItemLibrarySectionModel::sectionLibraryId() const
|
||||||
{
|
{
|
||||||
return m_sectionLibId;
|
return m_sectionLibraryId;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ItemLibrarySectionModel::sectionExpanded() const
|
bool ItemLibrarySectionModel::sectionExpanded() const
|
||||||
@@ -135,4 +136,19 @@ void ItemLibrarySectionModel::updateItemIconSize(const QSize &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
|
} // namespace QmlDesigner
|
||||||
|
|||||||
@@ -43,16 +43,16 @@ class ItemLibrarySectionModel: public QObject {
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
Q_PROPERTY(QObject* sectionEntries READ sectionEntries NOTIFY sectionEntriesChanged FINAL)
|
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(QString sectionName READ sectionName FINAL)
|
||||||
Q_PROPERTY(bool sectionExpanded READ sectionExpanded FINAL)
|
Q_PROPERTY(bool sectionExpanded READ sectionExpanded FINAL)
|
||||||
Q_PROPERTY(QVariant sortingRole READ sortingRole FINAL)
|
Q_PROPERTY(QVariant sortingRole READ sortingRole FINAL)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ItemLibrarySectionModel(int sectionLibId, const QString §ionName, QObject *parent = 0);
|
ItemLibrarySectionModel(int sectionLibraryId, const QString §ionName, QObject *parent = 0);
|
||||||
|
|
||||||
QString sectionName() const;
|
QString sectionName() const;
|
||||||
int sectionLibId() const;
|
int sectionLibraryId() const;
|
||||||
bool sectionExpanded() const;
|
bool sectionExpanded() const;
|
||||||
QVariant sortingRole() const;
|
QVariant sortingRole() const;
|
||||||
|
|
||||||
@@ -66,14 +66,18 @@ public:
|
|||||||
bool updateSectionVisibility(const QString &searchText, bool *changed);
|
bool updateSectionVisibility(const QString &searchText, bool *changed);
|
||||||
void updateItemIconSize(const QSize &itemIconSize);
|
void updateItemIconSize(const QSize &itemIconSize);
|
||||||
|
|
||||||
|
bool setVisible(bool isVisible);
|
||||||
|
bool isVisible() const;
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void sectionEntriesChanged();
|
void sectionEntriesChanged();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QString m_name;
|
|
||||||
int m_sectionLibId;
|
|
||||||
bool m_sectionExpanded;
|
|
||||||
ItemLibrarySortedModel m_sectionEntries;
|
ItemLibrarySortedModel m_sectionEntries;
|
||||||
|
QString m_name;
|
||||||
|
int m_sectionLibraryId;
|
||||||
|
bool m_sectionExpanded;
|
||||||
|
bool m_isVisible;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QmlDesigner
|
} // namespace QmlDesigner
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ ItemLibraryWidget::ItemLibraryWidget(QWidget *parent) :
|
|||||||
m_filterFlag(QtBasic),
|
m_filterFlag(QtBasic),
|
||||||
m_itemLibraryId(-1)
|
m_itemLibraryId(-1)
|
||||||
{
|
{
|
||||||
registerQmlTypes();
|
ItemLibraryModel::registerQmlTypes();
|
||||||
|
|
||||||
setWindowTitle(tr("Library", "Title of library view"));
|
setWindowTitle(tr("Library", "Title of library view"));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user