Commit 9befa036 authored by Marco Bubke's avatar Marco Bubke Committed by Tim Jenssen
Browse files

QmlDesigner: more cleanup in ItemLibrarySectionModel



Change-Id: I6aa149047bd76b65d28416bfaef2dd707587cecb
Reviewed-by: default avatarTim Jenssen <tim.jenssen@digia.com>
parent ce2f80c2
...@@ -88,10 +88,9 @@ QObject *ItemLibrarySection::sectionEntries() ...@@ -88,10 +88,9 @@ QObject *ItemLibrarySection::sectionEntries()
int ItemLibrarySection::visibleItemIndex(int itemLibId) int ItemLibrarySection::visibleItemIndex(int itemLibId)
{ {
return m_sectionEntries.visibleItemPosition(itemLibId); return m_sectionEntries.item(itemLibId)->isVisible();
} }
bool ItemLibrarySection::isItemVisible(int itemLibId) bool ItemLibrarySection::isItemVisible(int itemLibId)
{ {
return m_sectionEntries.itemVisible(itemLibId); return m_sectionEntries.itemVisible(itemLibId);
......
...@@ -36,6 +36,7 @@ namespace QmlDesigner { ...@@ -36,6 +36,7 @@ namespace QmlDesigner {
ItemLibrarySectionModel::ItemLibrarySectionModel(QObject *parent) : ItemLibrarySectionModel::ItemLibrarySectionModel(QObject *parent) :
QAbstractListModel(parent) QAbstractListModel(parent)
{ {
addRoleNames();
} }
ItemLibrarySectionModel::~ItemLibrarySectionModel() ItemLibrarySectionModel::~ItemLibrarySectionModel()
...@@ -72,8 +73,6 @@ QVariant ItemLibrarySectionModel::data(const QModelIndex &index, int role) const ...@@ -72,8 +73,6 @@ QVariant ItemLibrarySectionModel::data(const QModelIndex &index, int role) const
void ItemLibrarySectionModel::clearItems() void ItemLibrarySectionModel::clearItems()
{ {
beginResetModel(); beginResetModel();
while (m_itemOrder.count() > 0)
removeItem(m_itemOrder.at(0).libId);
endResetModel(); endResetModel();
} }
...@@ -86,65 +85,38 @@ static bool compareFunction(QObject *first, QObject *second) ...@@ -86,65 +85,38 @@ static bool compareFunction(QObject *first, QObject *second)
void ItemLibrarySectionModel::addItem(ItemLibraryItem *element, int libId) void ItemLibrarySectionModel::addItem(ItemLibraryItem *element, int libId)
{ {
struct order_struct orderEntry;
orderEntry.libId = libId;
orderEntry.visible = false;
int pos = 0;
while ((pos < m_itemOrder.count()) &&
compareFunction(m_itemModels.value(m_itemOrder.at(pos).libId), element))
++pos;
m_itemModels.insert(libId, element); m_itemModels.insert(libId, element);
m_itemOrder.insert(pos, orderEntry);
setItemVisible(libId, true);
element->setVisible(true);
} }
void ItemLibrarySectionModel::removeItem(int libId) void ItemLibrarySectionModel::removeItem(int libId)
{ {
QObject *element = m_itemModels.value(libId); ItemLibraryItem *itemLibraryItem = m_itemModels.value(libId);
int pos = findItem(libId);
setItemVisible(libId, false); itemLibraryItem->setVisible(false);
m_itemModels.remove(libId); m_itemModels.remove(libId);
m_itemOrder.removeAt(pos);
delete element; delete itemLibraryItem;
} }
bool ItemLibrarySectionModel::itemVisible(int libId) const bool ItemLibrarySectionModel::itemVisible(int libId) const
{ {
int pos = findItem(libId); ItemLibraryItem *itemLibraryItem = m_itemModels.value(libId);
return m_itemOrder.at(pos).visible; return itemLibraryItem->isVisible();
} }
bool ItemLibrarySectionModel::setItemVisible(int libId, bool visible) bool ItemLibrarySectionModel::setItemVisible(int libId, bool visible)
{ {
int pos = findItem(libId); ItemLibraryItem *itemLibraryItem = m_itemModels.value(libId);
if (m_itemOrder.at(pos).visible == visible) return itemLibraryItem->setVisible(visible);
return false;
int visiblePos = visibleItemPosition(libId);
if (visible)
privateInsert(visiblePos, (m_itemModels.value(libId)));
else
privateRemove(visiblePos);
m_itemOrder[pos].visible = visible;
return true;
} }
void ItemLibrarySectionModel::privateInsert(int pos, QObject *element) void ItemLibrarySectionModel::privateInsert(int pos, QObject *element)
{ {
QObject *object = element;
for (int i = 0; i < object->metaObject()->propertyCount(); ++i) {
QMetaProperty property = object->metaObject()->property(i);
addRoleName(property.name());
}
m_privList.insert(pos, element); m_privList.insert(pos, element);
} }
...@@ -168,49 +140,21 @@ ItemLibraryItem *ItemLibrarySectionModel::item(int libId) ...@@ -168,49 +140,21 @@ ItemLibraryItem *ItemLibrarySectionModel::item(int libId)
return m_itemModels.value(libId); return m_itemModels.value(libId);
} }
int ItemLibrarySectionModel::findItem(int libId) const
{
int i = 0;
QListIterator<struct order_struct> it(m_itemOrder);
while (it.hasNext()) {
if (it.next().libId == libId)
return i;
++i;
}
return -1;
}
int ItemLibrarySectionModel::visibleItemPosition(int libId) const
{
int i = 0;
QListIterator<struct order_struct> it(m_itemOrder);
while (it.hasNext()) {
struct order_struct order = it.next();
if (order.libId == libId)
return i;
if (order.visible)
++i;
}
return -1;
}
void ItemLibrarySectionModel::resetModel() void ItemLibrarySectionModel::resetModel()
{ {
beginResetModel(); beginResetModel();
endResetModel(); endResetModel();
} }
void ItemLibrarySectionModel::addRoleName(const QByteArray &roleName) void ItemLibrarySectionModel::addRoleNames()
{ {
if (m_roleNames.values().contains(roleName)) int role = 0;
return; for (int propertyIndex = 0; propertyIndex < ItemLibraryItem::staticMetaObject.propertyCount(); ++propertyIndex) {
QMetaProperty property = ItemLibraryItem::staticMetaObject.property(propertyIndex);
m_roleNames.insert(role, property.name());
++role;
}
int key = m_roleNames.count();
m_roleNames.insert(key, roleName);
setRoleNames(m_roleNames); setRoleNames(m_roleNames);
} }
......
...@@ -65,21 +65,13 @@ public: ...@@ -65,21 +65,13 @@ public:
ItemLibraryItem* item(int libId); ItemLibraryItem* item(int libId);
int findItem(int libId) const;
int visibleItemPosition(int libId) const;
void resetModel(); void resetModel();
private: private: // functions
void addRoleName(const QByteArray &roleName); void addRoleNames();
struct order_struct {
int libId;
bool visible;
};
private: // variables
QMap<int, ItemLibraryItem*> m_itemModels; QMap<int, ItemLibraryItem*> m_itemModels;
QList<struct order_struct> m_itemOrder;
QList<QObject *> m_privList; QList<QObject *> m_privList;
QHash<int, QByteArray> m_roleNames; QHash<int, QByteArray> m_roleNames;
......
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