QmlDesigner: Change ItemLibrarySectionModel to list

Change-Id: I04932ff50e0e4f8a1eda72df35e9ba675ae2709e
Reviewed-by: Tim Jenssen <tim.jenssen@digia.com>
This commit is contained in:
Marco Bubke
2014-06-23 14:50:33 +02:00
parent c4a47e091d
commit bfe474b4ba
4 changed files with 31 additions and 60 deletions

View File

@@ -162,24 +162,17 @@ void ItemLibraryModel::update(ItemLibraryInfo *itemLibraryInfo, Model *model)
&& (entry.requiredImport().isEmpty() && (entry.requiredImport().isEmpty()
|| model->hasImport(entryToImport(entry), true, true))) { || model->hasImport(entryToImport(entry), true, true))) {
QString itemSectionName = entry.category(); QString itemSectionName = entry.category();
ItemLibrarySection *sectionModel; ItemLibrarySection *sectionModel = sectionByName(itemSectionName);
ItemLibraryItem *itemModel; ItemLibraryItem *itemModel;
int itemId = m_nextLibId++, sectionId;
if (sections.contains(itemSectionName)) { if (sectionModel == 0) {
sectionId = sections.value(itemSectionName); sectionModel = new ItemLibrarySection(itemSectionName, this);
sectionModel = section(sectionId); m_sectionModels.append(sectionModel);
} else {
sectionId = m_nextLibId++;
sectionModel = new ItemLibrarySection(sectionId, itemSectionName, this);
addSection(sectionModel, sectionId);
sections.insert(itemSectionName, sectionId);
} }
itemModel = new ItemLibraryItem(sectionModel); itemModel = new ItemLibraryItem(sectionModel);
itemModel->setItemLibraryEntry(entry); itemModel->setItemLibraryEntry(entry);
sectionModel->addSectionEntry(itemModel); sectionModel->addSectionEntry(itemModel);
m_sections.insert(itemId, sectionId);
} }
} }
@@ -200,20 +193,9 @@ QMimeData *ItemLibraryModel::getMimeData(const ItemLibraryEntry &itemLibraryEntr
return mimeData; return mimeData;
} }
ItemLibrarySection *ItemLibraryModel::section(int libraryId)
{
return m_sectionModels.value(libraryId);
}
QList<ItemLibrarySection *> ItemLibraryModel::sections() const QList<ItemLibrarySection *> ItemLibraryModel::sections() const
{ {
return m_sectionModels.values(); return m_sectionModels;
}
void ItemLibraryModel::addSection(ItemLibrarySection *sectionModel, int sectionId)
{
m_sectionModels.insert(sectionId, sectionModel);
sectionModel->setVisible(true);
} }
void ItemLibraryModel::clearSections() void ItemLibraryModel::clearSections()
@@ -234,12 +216,9 @@ int ItemLibraryModel::visibleSectionCount() const
{ {
int visibleCount = 0; int visibleCount = 0;
auto sectionIterator = m_sectionModels.constBegin(); foreach (ItemLibrarySection *section, m_sectionModels) {
while (sectionIterator != m_sectionModels.constEnd()) { if (section->isVisible())
ItemLibrarySection *sectionModel = sectionIterator.value();
if (sectionModel->isVisible())
++visibleCount; ++visibleCount;
++sectionIterator;
} }
return visibleCount; return visibleCount;
@@ -249,43 +228,43 @@ QList<ItemLibrarySection *> ItemLibraryModel::visibleSections() const
{ {
QList<ItemLibrarySection *> visibleSectionList; QList<ItemLibrarySection *> visibleSectionList;
auto sectionIterator = m_sectionModels.constBegin(); foreach (ItemLibrarySection *section, m_sectionModels) {
while (sectionIterator != m_sectionModels.constEnd()) { if (section->isVisible())
ItemLibrarySection *sectionModel = sectionIterator.value(); visibleSectionList.append(section);
if (sectionModel->isVisible())
visibleSectionList.append(sectionModel);
++sectionIterator;
} }
return visibleSectionList; return visibleSectionList;
} }
ItemLibrarySection *ItemLibraryModel::sectionByName(const QString &sectionName)
{
foreach (ItemLibrarySection *itemLibrarySection, m_sectionModels) {
if (itemLibrarySection->sectionName() == sectionName)
return itemLibrarySection;
}
return 0;
}
void ItemLibraryModel::updateVisibility() void ItemLibraryModel::updateVisibility()
{ {
beginResetModel(); beginResetModel();
endResetModel(); endResetModel();
bool changed = false; bool changed = false;
auto sectionIterator = m_sectionModels.constBegin(); foreach (ItemLibrarySection *itemLibrarySection, m_sectionModels) {
while (sectionIterator != m_sectionModels.constEnd()) {
ItemLibrarySection *sectionModel = sectionIterator.value();
QString sectionSearchText = m_searchText; QString sectionSearchText = m_searchText;
if (sectionModel->sectionName().toLower().contains(m_searchText)) if (itemLibrarySection->sectionName().toLower().contains(m_searchText))
sectionSearchText.clear(); sectionSearchText.clear();
bool sectionChanged = false, bool sectionChanged = false,
sectionVisibility = sectionModel->updateSectionVisibility(sectionSearchText, sectionVisibility = itemLibrarySection->updateSectionVisibility(sectionSearchText,
&sectionChanged); &sectionChanged);
if (sectionChanged) { if (sectionChanged)
changed = true; changed = true;
if (sectionVisibility)
emit sectionVisibilityChanged(sectionIterator.key());
}
changed |= sectionModel->setVisible(sectionVisibility); changed |= itemLibrarySection->setVisible(sectionVisibility);
++sectionIterator;
} }
if (changed) if (changed)

View File

@@ -62,9 +62,7 @@ public:
QMimeData *getMimeData(const ItemLibraryEntry &itemLibraryEntry); QMimeData *getMimeData(const ItemLibraryEntry &itemLibraryEntry);
ItemLibrarySection* section(int libraryId);
QList<ItemLibrarySection*> sections() const; QList<ItemLibrarySection*> sections() const;
void addSection(ItemLibrarySection *sectionModel, int sectionId);
void clearSections(); void clearSections();
@@ -73,6 +71,8 @@ public:
int visibleSectionCount() const; int visibleSectionCount() const;
QList<ItemLibrarySection*> visibleSections() const; QList<ItemLibrarySection*> visibleSections() const;
ItemLibrarySection *sectionByName(const QString &sectionName);
public slots: public slots:
void setSearchText(const QString &searchText); void setSearchText(const QString &searchText);
@@ -93,7 +93,7 @@ private: // functions
int getHeight(const ItemLibraryEntry &entry); int getHeight(const ItemLibraryEntry &entry);
private: // variables private: // variables
QMap<int, ItemLibrarySection*> m_sectionModels; QList<ItemLibrarySection*> m_sectionModels;
QMap<int, int> m_sections; QMap<int, int> m_sections;
QHash<int, QByteArray> m_roleNames; QHash<int, QByteArray> m_roleNames;

View File

@@ -33,13 +33,12 @@
namespace QmlDesigner { namespace QmlDesigner {
ItemLibrarySection::ItemLibrarySection(int sectionLibId, const QString &sectionName, QObject *parent) ItemLibrarySection::ItemLibrarySection(const QString &sectionName, QObject *parent)
: QObject(parent), : QObject(parent),
m_sectionEntries(parent), m_sectionEntries(parent),
m_name(sectionName), m_name(sectionName),
m_sectionLibraryId(sectionLibId),
m_sectionExpanded(true), m_sectionExpanded(true),
m_isVisible(false) m_isVisible(true)
{ {
// if (collapsedStateHash.contains(sectionName)) // if (collapsedStateHash.contains(sectionName))
// m_sectionExpanded= collapsedStateHash.value(sectionName); // m_sectionExpanded= collapsedStateHash.value(sectionName);
@@ -51,11 +50,6 @@ QString ItemLibrarySection::sectionName() const
return m_name; return m_name;
} }
int ItemLibrarySection::sectionLibraryId() const
{
return m_sectionLibraryId;
}
bool ItemLibrarySection::sectionExpanded() const bool ItemLibrarySection::sectionExpanded() const
{ {
return m_sectionExpanded; return m_sectionExpanded;

View File

@@ -44,10 +44,9 @@ class ItemLibrarySection: public QObject {
Q_PROPERTY(QVariant sortingRole READ sortingRole FINAL) Q_PROPERTY(QVariant sortingRole READ sortingRole FINAL)
public: public:
ItemLibrarySection(int sectionLibraryId, const QString &sectionName, QObject *parent = 0); ItemLibrarySection(const QString &sectionName, QObject *parent = 0);
QString sectionName() const; QString sectionName() const;
int sectionLibraryId() const;
bool sectionExpanded() const; bool sectionExpanded() const;
QVariant sortingRole() const; QVariant sortingRole() const;
@@ -65,7 +64,6 @@ signals:
private: private:
ItemLibrarySectionModel m_sectionEntries; ItemLibrarySectionModel m_sectionEntries;
QString m_name; QString m_name;
int m_sectionLibraryId;
bool m_sectionExpanded; bool m_sectionExpanded;
bool m_isVisible; bool m_isVisible;
}; };