Commit 9380703a authored by Marco Bubke's avatar Marco Bubke Committed by Tim Jenssen

QmlDesigner: Move ItemLibrarySortedModel

Change-Id: I99238a1c3eb11debabddf4fb10558242b14dcef6
Reviewed-by: default avatarTim Jenssen <tim.jenssen@digia.com>
parent ed1908c0
......@@ -51,201 +51,6 @@ namespace QmlDesigner {
static QHash<QString, bool> collapsedStateHash;
ItemLibrarySortedModel::ItemLibrarySortedModel(QObject *parent) :
QAbstractListModel(parent)
{
}
ItemLibrarySortedModel::~ItemLibrarySortedModel()
{
clearElements();
}
int ItemLibrarySortedModel::rowCount(const QModelIndex &) const
{
return m_privList.count();
}
QVariant ItemLibrarySortedModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row()+1 > m_privList.count()) {
qDebug() << Q_FUNC_INFO << "invalid index requested";
return QVariant();
}
if (m_roleNames.contains(role)) {
QVariant value = m_privList.at(index.row())->property(m_roleNames.value(role));
if (ItemLibrarySortedModel* model = qobject_cast<ItemLibrarySortedModel *>(value.value<QObject*>()))
return QVariant::fromValue(model);
return m_privList.at(index.row())->property(m_roleNames.value(role));
}
qWarning() << Q_FUNC_INFO << "invalid role requested";
return QVariant();
}
void ItemLibrarySortedModel::clearElements()
{
beginResetModel();
while (m_elementOrder.count() > 0)
removeElement(m_elementOrder.at(0).libId);
endResetModel();
}
static bool compareFunction(QObject *first, QObject *second)
{
static const char sortRoleName[] = "sortingRole";
return first->property(sortRoleName).toString() < second->property(sortRoleName).toString();
}
void ItemLibrarySortedModel::addElement(QObject *element, int libId)
{
struct order_struct orderEntry;
orderEntry.libId = libId;
orderEntry.visible = false;
int pos = 0;
while ((pos < m_elementOrder.count()) &&
compareFunction(m_elementModels.value(m_elementOrder.at(pos).libId), element))
++pos;
m_elementModels.insert(libId, element);
m_elementOrder.insert(pos, orderEntry);
setElementVisible(libId, true);
}
void ItemLibrarySortedModel::removeElement(int libId)
{
QObject *element = m_elementModels.value(libId);
int pos = findElement(libId);
setElementVisible(libId, false);
m_elementModels.remove(libId);
m_elementOrder.removeAt(pos);
delete element;
}
bool ItemLibrarySortedModel::elementVisible(int libId) const
{
int pos = findElement(libId);
return m_elementOrder.at(pos).visible;
}
bool ItemLibrarySortedModel::setElementVisible(int libId, bool visible)
{
int pos = findElement(libId);
if (m_elementOrder.at(pos).visible == visible)
return false;
int visiblePos = visibleElementPosition(libId);
if (visible)
privateInsert(visiblePos, (m_elementModels.value(libId)));
else
privateRemove(visiblePos);
m_elementOrder[pos].visible = visible;
return true;
}
void ItemLibrarySortedModel::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);
}
void ItemLibrarySortedModel::privateRemove(int pos)
{
m_privList.removeAt(pos);
}
const QMap<int, QObject *> &ItemLibrarySortedModel::elements() const
{
return m_elementModels;
}
template<typename T>
const QList<T> ItemLibrarySortedModel::elementsByType() const
{
QList<T> objectList;
foreach (QObject *item, elements()) {
T object = qobject_cast<T>(item);
if (object)
objectList.append(object);
}
return objectList;
}
QObject *ItemLibrarySortedModel::element(int libId)
{
return m_elementModels.value(libId);
}
template<typename T>
T ItemLibrarySortedModel::elementByType(int libId)
{
return qobject_cast<T>(element(libId));
}
int ItemLibrarySortedModel::findElement(int libId) const
{
int i = 0;
QListIterator<struct order_struct> it(m_elementOrder);
while (it.hasNext()) {
if (it.next().libId == libId)
return i;
++i;
}
return -1;
}
int ItemLibrarySortedModel::visibleElementPosition(int libId) const
{
int i = 0;
QListIterator<struct order_struct> it(m_elementOrder);
while (it.hasNext()) {
struct order_struct order = it.next();
if (order.libId == libId)
return i;
if (order.visible)
++i;
}
return -1;
}
void ItemLibrarySortedModel::resetModel()
{
beginResetModel();
endResetModel();
}
void ItemLibrarySortedModel::addRoleName(const QByteArray &roleName)
{
if (m_roleNames.values().contains(roleName))
return;
int key = m_roleNames.count();
m_roleNames.insert(key, roleName);
setRoleNames(m_roleNames);
}
void ItemLibraryModel::setExpanded(bool expanded, const QString &section)
{
......
......@@ -44,58 +44,6 @@ class ItemLibraryEntry;
class Model;
class ItemLibrarySectionModel;
class ItemLibrarySortedModel: public QAbstractListModel {
Q_OBJECT
public:
ItemLibrarySortedModel(QObject *parent = 0);
~ItemLibrarySortedModel();
int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
void clearElements();
void addElement(QObject *element, int libId);
void removeElement(int libId);
bool elementVisible(int libId) const;
bool setElementVisible(int libId, bool visible);
void privateInsert(int pos, QObject *element);
void privateRemove(int pos);
const QMap<int, QObject *> &elements() const;
template<typename T>
const QList<T> elementsByType() const;
QObject *element(int libId);
template<typename T>
T elementByType(int libId);
int findElement(int libId) const;
int visibleElementPosition(int libId) const;
void resetModel();
private:
void addRoleName(const QByteArray &roleName);
struct order_struct {
int libId;
bool visible;
};
QMap<int, QObject *> m_elementModels;
QList<struct order_struct> m_elementOrder;
QList<QObject *> m_privList;
QHash<int, QByteArray> m_roleNames;
};
class ItemLibraryModel: public QAbstractListModel {
Q_OBJECT
......@@ -163,7 +111,6 @@ private: // variables
} // namespace QmlDesigner
QML_DECLARE_TYPE(QmlDesigner::ItemLibrarySortedModel)
QML_DECLARE_TYPE(QmlDesigner::ItemLibraryModel)
#endif // ITEMLIBRARYMODEL_H
......@@ -33,6 +33,202 @@
namespace QmlDesigner {
ItemLibrarySortedModel::ItemLibrarySortedModel(QObject *parent) :
QAbstractListModel(parent)
{
}
ItemLibrarySortedModel::~ItemLibrarySortedModel()
{
clearElements();
}
int ItemLibrarySortedModel::rowCount(const QModelIndex &) const
{
return m_privList.count();
}
QVariant ItemLibrarySortedModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row()+1 > m_privList.count()) {
qDebug() << Q_FUNC_INFO << "invalid index requested";
return QVariant();
}
if (m_roleNames.contains(role)) {
QVariant value = m_privList.at(index.row())->property(m_roleNames.value(role));
if (ItemLibrarySortedModel* model = qobject_cast<ItemLibrarySortedModel *>(value.value<QObject*>()))
return QVariant::fromValue(model);
return m_privList.at(index.row())->property(m_roleNames.value(role));
}
qWarning() << Q_FUNC_INFO << "invalid role requested";
return QVariant();
}
void ItemLibrarySortedModel::clearElements()
{
beginResetModel();
while (m_elementOrder.count() > 0)
removeElement(m_elementOrder.at(0).libId);
endResetModel();
}
static bool compareFunction(QObject *first, QObject *second)
{
static const char sortRoleName[] = "sortingRole";
return first->property(sortRoleName).toString() < second->property(sortRoleName).toString();
}
void ItemLibrarySortedModel::addElement(QObject *element, int libId)
{
struct order_struct orderEntry;
orderEntry.libId = libId;
orderEntry.visible = false;
int pos = 0;
while ((pos < m_elementOrder.count()) &&
compareFunction(m_elementModels.value(m_elementOrder.at(pos).libId), element))
++pos;
m_elementModels.insert(libId, element);
m_elementOrder.insert(pos, orderEntry);
setElementVisible(libId, true);
}
void ItemLibrarySortedModel::removeElement(int libId)
{
QObject *element = m_elementModels.value(libId);
int pos = findElement(libId);
setElementVisible(libId, false);
m_elementModels.remove(libId);
m_elementOrder.removeAt(pos);
delete element;
}
bool ItemLibrarySortedModel::elementVisible(int libId) const
{
int pos = findElement(libId);
return m_elementOrder.at(pos).visible;
}
bool ItemLibrarySortedModel::setElementVisible(int libId, bool visible)
{
int pos = findElement(libId);
if (m_elementOrder.at(pos).visible == visible)
return false;
int visiblePos = visibleElementPosition(libId);
if (visible)
privateInsert(visiblePos, (m_elementModels.value(libId)));
else
privateRemove(visiblePos);
m_elementOrder[pos].visible = visible;
return true;
}
void ItemLibrarySortedModel::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);
}
void ItemLibrarySortedModel::privateRemove(int pos)
{
m_privList.removeAt(pos);
}
const QMap<int, QObject *> &ItemLibrarySortedModel::elements() const
{
return m_elementModels;
}
template<typename T>
const QList<T> ItemLibrarySortedModel::elementsByType() const
{
QList<T> objectList;
foreach (QObject *item, elements()) {
T object = qobject_cast<T>(item);
if (object)
objectList.append(object);
}
return objectList;
}
QObject *ItemLibrarySortedModel::element(int libId)
{
return m_elementModels.value(libId);
}
template<typename T>
T ItemLibrarySortedModel::elementByType(int libId)
{
return qobject_cast<T>(element(libId));
}
int ItemLibrarySortedModel::findElement(int libId) const
{
int i = 0;
QListIterator<struct order_struct> it(m_elementOrder);
while (it.hasNext()) {
if (it.next().libId == libId)
return i;
++i;
}
return -1;
}
int ItemLibrarySortedModel::visibleElementPosition(int libId) const
{
int i = 0;
QListIterator<struct order_struct> it(m_elementOrder);
while (it.hasNext()) {
struct order_struct order = it.next();
if (order.libId == libId)
return i;
if (order.visible)
++i;
}
return -1;
}
void ItemLibrarySortedModel::resetModel()
{
beginResetModel();
endResetModel();
}
void ItemLibrarySortedModel::addRoleName(const QByteArray &roleName)
{
if (m_roleNames.values().contains(roleName))
return;
int key = m_roleNames.count();
m_roleNames.insert(key, roleName);
setRoleNames(m_roleNames);
}
ItemLibrarySectionModel::ItemLibrarySectionModel(int sectionLibId, const QString &sectionName, QObject *parent)
: QObject(parent),
m_name(sectionName),
......@@ -107,16 +303,16 @@ bool ItemLibrarySectionModel::updateSectionVisibility(const QString &searchText,
QMap<int, QObject *>::const_iterator itemIt = m_sectionEntries.elements().constBegin();
while (itemIt != m_sectionEntries.elements().constEnd()) {
// bool itemVisible = m_sectionEntries.elementByType<ItemLibraryItemModel*>(
// itemIt.key())->itemName().toLower().contains(searchText);
bool itemVisible = m_sectionEntries.elementByType<ItemLibraryItemModel*>(
itemIt.key())->itemName().toLower().contains(searchText);
bool itemChanged = false;
// itemChanged = m_sectionEntries.setElementVisible(itemIt.key(), itemVisible);
itemChanged = m_sectionEntries.setElementVisible(itemIt.key(), itemVisible);
*changed |= itemChanged;
// if (itemVisible)
// haveVisibleItems = true;
if (itemVisible)
haveVisibleItems = true;
++itemIt;
}
......
......@@ -38,12 +38,63 @@ namespace QmlDesigner {
class ItemLibraryItemModel;
class ItemLibrarySortedModel: public QAbstractListModel {
Q_OBJECT
public:
ItemLibrarySortedModel(QObject *parent = 0);
~ItemLibrarySortedModel();
int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
void clearElements();
void addElement(QObject *element, int libId);
void removeElement(int libId);
bool elementVisible(int libId) const;
bool setElementVisible(int libId, bool visible);
void privateInsert(int pos, QObject *element);
void privateRemove(int pos);
const QMap<int, QObject *> &elements() const;
template<typename T>
const QList<T> elementsByType() const;
QObject *element(int libId);
template<typename T>
T elementByType(int libId);
int findElement(int libId) const;
int visibleElementPosition(int libId) const;
void resetModel();
private:
void addRoleName(const QByteArray &roleName);
struct order_struct {
int libId;
bool visible;
};
QMap<int, QObject *> m_elementModels;
QList<struct order_struct> m_elementOrder;
QList<QObject *> m_privList;
QHash<int, QByteArray> m_roleNames;
};
class ItemLibrarySectionModel: public QObject {
Q_OBJECT
Q_PROPERTY(QObject* sectionEntries READ sectionEntries NOTIFY sectionEntriesChanged 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)
......@@ -82,4 +133,6 @@ private:
} // namespace QmlDesigner
QML_DECLARE_TYPE(QmlDesigner::ItemLibrarySortedModel)
#endif // QMLDESIGNER_ITEMLIBRARYSECTIONMODEL_H
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