From 3603c773b6e998d9c13cce8f826e6dc9e989deae Mon Sep 17 00:00:00 2001 From: onil Date: Fri, 11 Dec 2009 14:39:55 +0000 Subject: [PATCH] git-svn-id: file:///svnroot/family-shop-mgr@11 26eb2498-383b-47a6-be48-5d6f36779e85 --- code/family-shop-mgr/GoShopping.ui | 51 ++++++++++---------- code/family-shop-mgr/ListManager.ui | 52 ++++++++++++++++---- code/family-shop-mgr/ShoppingTreeItem.cpp | 65 ++++++++++++++++++++++++- code/family-shop-mgr/ShoppingTreeItem.h | 27 ++++++++++- code/family-shop-mgr/ShoppingTreeModel.cpp | 72 ++++++++++++++++++++++++++++ code/family-shop-mgr/ShoppingTreeModel.h | 9 ++++ 6 files changed, 238 insertions(+), 38 deletions(-) diff --git a/code/family-shop-mgr/GoShopping.ui b/code/family-shop-mgr/GoShopping.ui index 1bfdd43..92e0978 100644 --- a/code/family-shop-mgr/GoShopping.ui +++ b/code/family-shop-mgr/GoShopping.ui @@ -13,32 +13,31 @@ Form - - - - 10 - 40 - 221 - 251 - - - - - - - 60 - 10 - 121 - 21 - - - - PushButton - - - true - - + + + + + + + + + + + + + Show checked + + + true + + + + + + + + + diff --git a/code/family-shop-mgr/ListManager.ui b/code/family-shop-mgr/ListManager.ui index c7bf143..205ddf7 100644 --- a/code/family-shop-mgr/ListManager.ui +++ b/code/family-shop-mgr/ListManager.ui @@ -1,21 +1,55 @@ - - - - + + Form - - + + 0 0 - 400 + 233 300 - + Form + + + + + + + + + + Edit structure + + + true + + + + + + + Add + + + + + + + Remove + + + + + + + + + - + diff --git a/code/family-shop-mgr/ShoppingTreeItem.cpp b/code/family-shop-mgr/ShoppingTreeItem.cpp index 69daf33..5f8faa1 100644 --- a/code/family-shop-mgr/ShoppingTreeItem.cpp +++ b/code/family-shop-mgr/ShoppingTreeItem.cpp @@ -21,7 +21,8 @@ #include "ShoppingTreeItem.h" -ShoppingTreeItem::ShoppingTreeItem(const QVector &data, ShoppingTreeItem *parent) +ShoppingTreeItem::ShoppingTreeItem(const QVector &data, + ShoppingTreeItem *parent) : m_itemType(NotDefined) { parentItem = parent; itemData = data; @@ -69,11 +70,26 @@ bool ShoppingTreeItem::insertChildren(int position, int count, int columns) QVector data(columns); ShoppingTreeItem *item = new ShoppingTreeItem(data, this); childItems.insert(position, item); + emit childInserted(item); } return true; } +bool ShoppingTreeItem::insertColumns(int position, int columns) +{ + if (position < 0 || position > itemData.size()) + return false; + + for (int column = 0; column < columns; ++column) + itemData.insert(position, QVariant()); + + foreach (ShoppingTreeItem *child, childItems) + child->insertColumns(position, columns); + + return true; +} + ShoppingTreeItem *ShoppingTreeItem::parent() { return parentItem; @@ -85,7 +101,25 @@ bool ShoppingTreeItem::removeChildren(int position, int count) return false; for (int row = 0; row < count; ++row) - delete childItems.takeAt(position); + { + ShoppingTreeItem *item = childItems.takeAt(position); + emit childRemoved(item); + delete item; + } + + return true; +} + +bool ShoppingTreeItem::removeColumns(int position, int columns) +{ + if (position < 0 || position + columns > itemData.size()) + return false; + + for (int column = 0; column < columns; ++column) + itemData.remove(position); + + foreach (ShoppingTreeItem *child, childItems) + child->removeColumns(position, columns); return true; } @@ -95,6 +129,33 @@ bool ShoppingTreeItem::setData(int column, const QVariant &value) if (column < 0 || column >= itemData.size()) return false; + if(m_itemType == Category && column != 0) + return false; + itemData[column] = value; + emit dataChanged(column); return true; } + +bool ShoppingTreeItem::setItemType(ItemType type) +{ + if(type == NotDefined) + return false; + + m_itemType = type; + + // release enough resources in case we call wait more than once + m_typeSemaphore.release(1000); + + return (m_itemType == type) ? true : false; +} + +ShoppingTreeItem::ItemType ShoppingTreeItem::getItemType() const +{ + return m_itemType; +} + +void ShoppingTreeItem::waitItemTypeDefinition() +{ + m_typeSemaphore.acquire(); +} diff --git a/code/family-shop-mgr/ShoppingTreeItem.h b/code/family-shop-mgr/ShoppingTreeItem.h index 71b71fe..2ff73b0 100644 --- a/code/family-shop-mgr/ShoppingTreeItem.h +++ b/code/family-shop-mgr/ShoppingTreeItem.h @@ -25,10 +25,22 @@ #include #include #include +#include -class ShoppingTreeItem +class ShoppingTreeItem : public QObject { +Q_OBJECT + public: + + enum ItemType + { + Category = 0, + Item = 1, + + NotDefined = -1 + }; + ShoppingTreeItem(const QVector &data, ShoppingTreeItem *parent = 0); ~ShoppingTreeItem(); @@ -37,15 +49,28 @@ public: int columnCount() const; QVariant data(int column) const; bool insertChildren(int position, int count, int columns); + bool insertColumns(int position, int columns); ShoppingTreeItem *parent(); bool removeChildren(int position, int count); + bool removeColumns(int position, int columns); int childNumber() const; bool setData(int column, const QVariant &value); + bool setItemType(const ItemType type); + ItemType getItemType() const; + void waitItemTypeDefinition(); + +signals: + void childInserted(ShoppingTreeItem *item); + void dataChanged(int column); + void childRemoved(ShoppingTreeItem *item); private: QList childItems; QVector itemData; ShoppingTreeItem *parentItem; + + ItemType m_itemType; + QSemaphore m_typeSemaphore; }; #endif // SHOPPINGTREEITEM_H diff --git a/code/family-shop-mgr/ShoppingTreeModel.cpp b/code/family-shop-mgr/ShoppingTreeModel.cpp index 4690694..1d0120d 100644 --- a/code/family-shop-mgr/ShoppingTreeModel.cpp +++ b/code/family-shop-mgr/ShoppingTreeModel.cpp @@ -84,6 +84,8 @@ QAbstractItemModel(parent), m_document("ShoppingList") rootItem->columnCount()); QVector columnData = getColumnsFromItemElement(child); + rootItem->child(rootItem->childCount() - 1)-> + setItemType(ShoppingTreeItem::Item); for(int column = 0; column < columnData.size(); column++) { rootItem->child(rootItem->childCount() - 1)->setData(column, columnData[column]); @@ -91,6 +93,16 @@ QAbstractItemModel(parent), m_document("ShoppingList") m_domElementForItem.insert(rootItem->child(rootItem->childCount() - 1), child); } + + QHashIterator i(m_domElementForItem); + while(i.hasNext()) + { + i.next(); + connect(i.key(), SIGNAL(childInserted(ShoppingTreeItem*)), this, + SLOT(registerInsertedChild(ShoppingTreeItem*))); + connect(i.key(), SIGNAL(childRemoved(ShoppingTreeItem*)), this, + SLOT(deleteRemovedChild(ShoppingTreeItem*))); + } } ShoppingTreeModel::~ShoppingTreeModel() @@ -151,6 +163,17 @@ QModelIndex ShoppingTreeModel::index(int row, int column, const QModelIndex &par return QModelIndex(); } +bool ShoppingTreeModel::insertColumns(int position, int columns, const QModelIndex &parent) +{ + bool success; + + beginInsertColumns(parent, position, position + columns - 1); + success = rootItem->insertColumns(position, columns); + endInsertColumns(); + + return success; +} + bool ShoppingTreeModel::insertRows(int position, int rows, const QModelIndex &parent) { ShoppingTreeItem *parentItem = getItem(parent); @@ -177,6 +200,20 @@ QModelIndex ShoppingTreeModel::parent(const QModelIndex &index) const return createIndex(parentItem->childNumber(), 0, parentItem); } +bool ShoppingTreeModel::removeColumns(int position, int columns, const QModelIndex &parent) +{ + bool success; + + beginRemoveColumns(parent, position, position + columns - 1); + success = rootItem->removeColumns(position, columns); + endRemoveColumns(); + + if (rootItem->columnCount() == 0) + removeRows(0, rowCount()); + + return success; +} + bool ShoppingTreeModel::removeRows(int position, int rows, const QModelIndex &parent) { ShoppingTreeItem *parentItem = getItem(parent); @@ -235,6 +272,38 @@ bool ShoppingTreeModel::setHeaderData(int section, Qt::Orientation orientation, return result; } +void ShoppingTreeModel::registerInsertedChild(ShoppingTreeItem *item) +{ + // wait until item type is defined + item->waitItemTypeDefinition(); + + QDomElement parentElement = m_domElementForItem.value(item->parent()); + QDomElement element; + if(item->getItemType() == ShoppingTreeItem::Category) + element = m_document.createElement("category"); + else if(item->getItemType() == ShoppingTreeItem::Item) + element = m_document.createElement("item"); + else + return; + + parentElement.appendChild(element); + updateXmlFile(); + m_domElementForItem.insert(item, element); + connect(item, SIGNAL(childInserted(ShoppingTreeItem*)), this, + SLOT(registerInsertedChild(ShoppingTreeItem*))); + connect(item, SIGNAL(childRemoved(ShoppingTreeItem*)), this, + SLOT(deleteRemovedChild(ShoppingTreeItem*))); +} + +void ShoppingTreeModel::deleteRemovedChild(ShoppingTreeItem *item) +{ + QDomElement element = m_domElementForItem.value(item); + QDomNode parentNode = element.parentNode(); + parentNode.removeChild(element); + updateXmlFile(); + m_domElementForItem.remove(item); +} + void ShoppingTreeModel::parseCategoryElement(const QDomElement &element, ShoppingTreeItem *parentItem) { @@ -249,6 +318,8 @@ void ShoppingTreeModel::parseCategoryElement(const QDomElement &element, parentItem->insertChildren(parentItem->childCount(), 1, rootItem->columnCount()); + parentItem->child(parentItem->childCount() - 1)-> + setItemType(ShoppingTreeItem::Category); parentItem->child(parentItem->childCount() - 1)->setData(0, title); m_domElementForItem.insert(parentItem->child(parentItem->childCount() - 1), element); @@ -275,6 +346,7 @@ void ShoppingTreeModel::parseCategoryElement(const QDomElement &element, rootItem->columnCount()); QVector columnData = getColumnsFromItemElement(child); + item->child(item->childCount() - 1)->setItemType(ShoppingTreeItem::Item); for(int column = 0; column < columnData.size(); column++) { item->child(item->childCount() - 1)->setData(column, columnData[column]); diff --git a/code/family-shop-mgr/ShoppingTreeModel.h b/code/family-shop-mgr/ShoppingTreeModel.h index 5270732..31301d6 100644 --- a/code/family-shop-mgr/ShoppingTreeModel.h +++ b/code/family-shop-mgr/ShoppingTreeModel.h @@ -27,6 +27,7 @@ #include #include #include +#include class ShoppingTreeItem; @@ -54,6 +55,10 @@ public: bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole); + bool insertColumns(int position, int columns, + const QModelIndex &parent = QModelIndex()); + bool removeColumns(int position, int columns, + const QModelIndex &parent = QModelIndex()); bool insertRows(int position, int rows, const QModelIndex &parent = QModelIndex()); bool removeRows(int position, int rows, @@ -63,6 +68,10 @@ signals: void xmlParseError(QString error, int line, int column); void invalidDocument(); +public slots: + void registerInsertedChild(ShoppingTreeItem *item); + void deleteRemovedChild(ShoppingTreeItem *item); + protected: void parseCategoryElement(const QDomElement &element, ShoppingTreeItem *parentItem = 0); -- 1.7.9.5