From f3b64470c91540f9ad00fdd0dcbc31a900427576 Mon Sep 17 00:00:00 2001 From: onil Date: Sun, 21 Mar 2010 23:49:18 +0000 Subject: [PATCH] git-svn-id: file:///svnroot/family-shop-mgr@27 26eb2498-383b-47a6-be48-5d6f36779e85 --- code/family-shop-mgr/ShoppingTreeItem.cpp | 27 ++++++++++++++------------ code/family-shop-mgr/ShoppingTreeItem.h | 10 +++++----- code/family-shop-mgr/ShoppingTreeModel.cpp | 29 ++++++++++++++-------------- 3 files changed, 34 insertions(+), 32 deletions(-) diff --git a/code/family-shop-mgr/ShoppingTreeItem.cpp b/code/family-shop-mgr/ShoppingTreeItem.cpp index 6407767..c5a74e7 100644 --- a/code/family-shop-mgr/ShoppingTreeItem.cpp +++ b/code/family-shop-mgr/ShoppingTreeItem.cpp @@ -69,7 +69,8 @@ QVariant ShoppingTreeItem::data(int column) const } /*******************************************************************/ -bool ShoppingTreeItem::insertChildren(int position, int count, int columns) +bool ShoppingTreeItem::insertChildren(int position, int count, int columns, + ShoppingTreeModel* model) { if (position < 0 || position > childItems.size()) return false; @@ -78,7 +79,13 @@ 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); + if(model) + { + connect(item, SIGNAL(childItemSet(ShoppingTreeItem*)), model, + SLOT(registerInsertedChild(ShoppingTreeItem*))); + connect(item, SIGNAL(childRemoved(ShoppingTreeItem*)), model, + SLOT(deleteRemovedChild(ShoppingTreeItem*))); + } } return true; @@ -158,10 +165,12 @@ bool ShoppingTreeItem::setItemType(ItemType type) m_itemType = type; - // release enough resources in case we call wait more than once - m_typeSemaphore.release(1000); - - return (m_itemType == type) ? true : false; + if(m_itemType == type) + { + emit childItemSet(this); + return true; + } + return false; } /*******************************************************************/ @@ -169,9 +178,3 @@ 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 2ff73b0..214db12 100644 --- a/code/family-shop-mgr/ShoppingTreeItem.h +++ b/code/family-shop-mgr/ShoppingTreeItem.h @@ -25,7 +25,8 @@ #include #include #include -#include + +#include "ShoppingTreeModel.h" class ShoppingTreeItem : public QObject { @@ -48,7 +49,8 @@ public: int childCount() const; int columnCount() const; QVariant data(int column) const; - bool insertChildren(int position, int count, int columns); + bool insertChildren(int position, int count, int columns, + ShoppingTreeModel* model = 0); bool insertColumns(int position, int columns); ShoppingTreeItem *parent(); bool removeChildren(int position, int count); @@ -57,10 +59,9 @@ public: bool setData(int column, const QVariant &value); bool setItemType(const ItemType type); ItemType getItemType() const; - void waitItemTypeDefinition(); signals: - void childInserted(ShoppingTreeItem *item); + void childItemSet(ShoppingTreeItem *item); void dataChanged(int column); void childRemoved(ShoppingTreeItem *item); @@ -70,7 +71,6 @@ private: 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 2bdde16..2ee4df1 100644 --- a/code/family-shop-mgr/ShoppingTreeModel.cpp +++ b/code/family-shop-mgr/ShoppingTreeModel.cpp @@ -90,7 +90,8 @@ QAbstractItemModel(parent), m_document("ShoppingList") setItemType(ShoppingTreeItem::Item); for(int column = 0; column < columnData.size(); column++) { - rootItem->child(rootItem->childCount() - 1)->setData(column, columnData[column]); + rootItem->child(rootItem->childCount() - 1)-> + setData(column, columnData[column]); } m_domElementForItem.insert(rootItem->child(rootItem->childCount() - 1), child); @@ -98,7 +99,7 @@ QAbstractItemModel(parent), m_document("ShoppingList") } - connect(rootItem, SIGNAL(childInserted(ShoppingTreeItem*)), this, + connect(rootItem, SIGNAL(childItemSet(ShoppingTreeItem*)), this, SLOT(registerInsertedChild(ShoppingTreeItem*))); connect(rootItem, SIGNAL(childRemoved(ShoppingTreeItem*)), this, SLOT(deleteRemovedChild(ShoppingTreeItem*))); @@ -107,7 +108,7 @@ QAbstractItemModel(parent), m_document("ShoppingList") while(i.hasNext()) { i.next(); - connect(i.key(), SIGNAL(childInserted(ShoppingTreeItem*)), this, + connect(i.key(), SIGNAL(childItemSet(ShoppingTreeItem*)), this, SLOT(registerInsertedChild(ShoppingTreeItem*))); connect(i.key(), SIGNAL(childRemoved(ShoppingTreeItem*)), this, SLOT(deleteRemovedChild(ShoppingTreeItem*))); @@ -210,7 +211,8 @@ ShoppingTreeItem *ShoppingTreeModel::getItem(const QModelIndex &index) const } /*******************************************************************/ -QVariant ShoppingTreeModel::headerData(int section, Qt::Orientation orientation, +QVariant ShoppingTreeModel::headerData(int section, + Qt::Orientation orientation, int role) const { if(orientation == Qt::Horizontal && role == Qt::DisplayRole) @@ -220,7 +222,8 @@ QVariant ShoppingTreeModel::headerData(int section, Qt::Orientation orientation, } /*******************************************************************/ -QModelIndex ShoppingTreeModel::index(int row, int column, const QModelIndex &parent) const +QModelIndex ShoppingTreeModel::index(int row, int column, + const QModelIndex &parent) const { if(parent.isValid() && parent.column() != 0) return QModelIndex(); @@ -235,7 +238,8 @@ QModelIndex ShoppingTreeModel::index(int row, int column, const QModelIndex &par } /*******************************************************************/ -bool ShoppingTreeModel::insertColumns(int position, int columns, const QModelIndex &parent) +bool ShoppingTreeModel::insertColumns(int position, int columns, + const QModelIndex &parent) { bool success; @@ -247,13 +251,15 @@ bool ShoppingTreeModel::insertColumns(int position, int columns, const QModelInd } /*******************************************************************/ -bool ShoppingTreeModel::insertRows(int position, int rows, const QModelIndex &parent) +bool ShoppingTreeModel::insertRows(int position, int rows, + const QModelIndex &parent) { ShoppingTreeItem *parentItem = getItem(parent); bool success; beginInsertRows(parent, position, position + rows - 1); - success = parentItem->insertChildren(position, rows, rootItem->columnCount()); + success = parentItem->insertChildren(position, rows, + rootItem->columnCount(), this); endInsertRows(); return success; @@ -359,9 +365,6 @@ bool ShoppingTreeModel::setHeaderData(int section, Qt::Orientation orientation, /*******************************************************************/ 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) @@ -374,10 +377,6 @@ void ShoppingTreeModel::registerInsertedChild(ShoppingTreeItem *item) 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*))); } /*******************************************************************/ -- 1.7.9.5