From: onil Date: Fri, 28 May 2010 21:08:12 +0000 (+0000) Subject: git-svn-id: file:///svnroot/family-shop-mgr@32 26eb2498-383b-47a6-be48-5d6f36779e85 X-Git-Url: http://git.maemo.org/git/?p=family-shop-mgr;a=commitdiff_plain;h=949383586d4a19d7f5d8f537e10cdfa01d36b6fc git-svn-id: file:///svnroot/family-shop-mgr@32 26eb2498-383b-47a6-be48-5d6f36779e85 --- diff --git a/code/family-shop-mgr/ShoppingTreeItem.cpp b/code/family-shop-mgr/ShoppingTreeItem.cpp index c5a74e7..ae21ccc 100644 --- a/code/family-shop-mgr/ShoppingTreeItem.cpp +++ b/code/family-shop-mgr/ShoppingTreeItem.cpp @@ -19,162 +19,67 @@ * */ -#include "ShoppingTreeItem.h" - -/*******************************************************************/ -ShoppingTreeItem::ShoppingTreeItem(const QVector &data, - ShoppingTreeItem *parent) : m_itemType(NotDefined) -{ - parentItem = parent; - itemData = data; -} - -/*******************************************************************/ -ShoppingTreeItem::~ShoppingTreeItem() -{ - qDeleteAll(childItems); -} - -/*******************************************************************/ -ShoppingTreeItem *ShoppingTreeItem::child(int number) -{ - return childItems.value(number); -} +#include -/*******************************************************************/ -int ShoppingTreeItem::childCount() const -{ - return childItems.count(); -} - -/*******************************************************************/ -int ShoppingTreeItem::childNumber() const -{ - if (parentItem) - return parentItem->childItems.indexOf(const_cast(this)); +#include "ShoppingTreeItem.h" - return 0; -} -/*******************************************************************/ -int ShoppingTreeItem::columnCount() const +DomItem::DomItem(QDomNode &node, int row, DomItem *parent) { - return itemData.count(); + domNode = node; + rowNumber = row; + parentItem = parent; } /*******************************************************************/ -QVariant ShoppingTreeItem::data(int column) const +DomItem::~DomItem() { - return itemData.value(column); + QHash::iterator it; + for (it = childItems.begin(); it != childItems.end(); ++it) + delete it.value(); } /*******************************************************************/ -bool ShoppingTreeItem::insertChildren(int position, int count, int columns, - ShoppingTreeModel* model) +QDomNode DomItem::node() const { - if (position < 0 || position > childItems.size()) - return false; - - for (int row = 0; row < count; ++row) { - QVector data(columns); - ShoppingTreeItem *item = new ShoppingTreeItem(data, this); - childItems.insert(position, item); - if(model) - { - connect(item, SIGNAL(childItemSet(ShoppingTreeItem*)), model, - SLOT(registerInsertedChild(ShoppingTreeItem*))); - connect(item, SIGNAL(childRemoved(ShoppingTreeItem*)), model, - SLOT(deleteRemovedChild(ShoppingTreeItem*))); - } - } - - return true; + return domNode; } /*******************************************************************/ -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() +DomItem *DomItem::parent() { return parentItem; } /*******************************************************************/ -bool ShoppingTreeItem::removeChildren(int position, int count) +DomItem *DomItem::child(int i) { - if (position < 0 || position + count > childItems.size()) - return false; + if (childItems.contains(i)) + return childItems[i]; - for (int row = 0; row < count; ++row) + QDomNodeList childNodesList = domNode.childNodes(); + QList childElementsList; + QDomNode childNode; + for(int j = 0; !(childNode = childNodesList.at(j)).isNull(); j++) { - ShoppingTreeItem *item = childItems.takeAt(position); - emit childRemoved(item); - delete item; + if(childNode.isElement()) + { + if(childNode.toElement().nodeName() == "category" || + childNode.toElement().nodeName() == "item") + childElementsList.append(childNode); + } } - - 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; -} - -/*******************************************************************/ -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; - - if(m_itemType == type) - { - emit childItemSet(this); - return true; + if (i >= 0 && i < childElementsList.count()) { + childNode = childElementsList.at(i); + DomItem *childItem = new DomItem(childNode, i, this); + childItems[i] = childItem; + return childItem; } - return false; + return 0; } /*******************************************************************/ -ShoppingTreeItem::ItemType ShoppingTreeItem::getItemType() const +int DomItem::row() { - return m_itemType; + return rowNumber; } diff --git a/code/family-shop-mgr/ShoppingTreeModel.cpp b/code/family-shop-mgr/ShoppingTreeModel.cpp index 28dd5d0..f79b957 100644 --- a/code/family-shop-mgr/ShoppingTreeModel.cpp +++ b/code/family-shop-mgr/ShoppingTreeModel.cpp @@ -19,562 +19,155 @@ * */ -#include "ShoppingTreeModel.h" +#include #include "ShoppingTreeItem.h" -#include -#include -#include +#include "ShoppingTreeModel.h" -/*******************************************************************/ -ShoppingTreeModel::ShoppingTreeModel(const QString &xmlFileName, - QObject *parent) : -QAbstractItemModel(parent), m_document("ShoppingList") +DomModel::DomModel(QDomDocument document, QObject *parent) + : QAbstractItemModel(parent), domDocument(document), + rootItemNode(domDocument.firstChildElement("shoppingList")) { - QString error; - int errLine; - int errColumn; - - m_xmlFileName = QApplication::applicationDirPath() + "/" + xmlFileName; - QFile file(m_xmlFileName); - if(!file.open(QIODevice::ReadOnly)) - return; - // Parse xml file - if(!m_document.setContent(&file, true, &error, &errLine, &errColumn)) - { - emit xmlParseError(error, errLine, errColumn); - file.close(); - return; - } - file.close(); - - QDomElement root = m_document.documentElement(); - if(root.tagName() != "shoppingList" || !root.hasAttribute("version")) - { - emit invalidDocument(); - return; - } - else if(root.attribute("version") == "1.0") - { - // set column titles - QVector rootData; - rootData << "Category/Item name" - << "Quantity" << "Store"; - - rootItem = new ShoppingTreeItem(rootData); - m_domElementForItem.insert(rootItem, root); - } - else - { - // upgrade document version if possible - ; - } - - QDomElement child = root.firstChildElement("category"); - while(!child.isNull()) - { - // Parse all categories - parseCategoryElement(child); - child = child.nextSiblingElement("category"); - } - - child = root.firstChildElement("item"); - while(!child.isNull()) - { - // parse all items which don't have category - rootItem->insertChildren( - rootItem->childCount(), 1, - 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]); - } - m_domElementForItem.insert(rootItem->child(rootItem->childCount() - 1), - child); - child = child.nextSiblingElement("item"); - } - - - connect(rootItem, SIGNAL(childItemSet(ShoppingTreeItem*)), this, - SLOT(registerInsertedChild(ShoppingTreeItem*))); - connect(rootItem, SIGNAL(childRemoved(ShoppingTreeItem*)), this, - SLOT(deleteRemovedChild(ShoppingTreeItem*))); - - QHashIterator i(m_domElementForItem); - while(i.hasNext()) - { - i.next(); - connect(i.key(), SIGNAL(childItemSet(ShoppingTreeItem*)), this, - SLOT(registerInsertedChild(ShoppingTreeItem*))); - connect(i.key(), SIGNAL(childRemoved(ShoppingTreeItem*)), this, - SLOT(deleteRemovedChild(ShoppingTreeItem*))); - } + rootItem = new DomItem(rootItemNode, 0); } /*******************************************************************/ -ShoppingTreeModel::~ShoppingTreeModel() +DomModel::~DomModel() { delete rootItem; } /*******************************************************************/ -bool ShoppingTreeModel::addCategory(QString name) -{ - QModelIndex parent = QModelIndex(); - - if (!this->insertRow(parent.row()+1, parent)) - return false; - - int column = 0; - QModelIndex child = this->index(parent.row()+1, column, parent); - ShoppingTreeItem* item = this->getItem(child); - item->setItemType(ShoppingTreeItem::Category); - this->setData(child, QVariant(name), Qt::EditRole); - return true; -} - -/*******************************************************************/ -bool ShoppingTreeModel::addSubCategory(QString name, int position, - const QModelIndex &parent) +int DomModel::columnCount(const QModelIndex &/*parent*/) const { - if (!this->insertRow(position, parent)) - return false; - - for(int column = 0; column < this->columnCount(parent); ++column) - { - QModelIndex child = this->index(parent.row()+1, column, parent); - ShoppingTreeItem* item = this->getItem(child); - item->setItemType(ShoppingTreeItem::Category); - this->setData(child, QVariant(name), Qt::EditRole); - } - return true; + return 4; } /*******************************************************************/ -bool ShoppingTreeModel::addItem(QString name, int position, - const QModelIndex &parent) +QVariant DomModel::data(const QModelIndex &index, int role) const { - if (!this->insertRow(position, parent)) - return false; - - int column = 0; - QModelIndex child = this->index(parent.row()+1, column, parent); - ShoppingTreeItem* item = this->getItem(child); - item->setItemType(ShoppingTreeItem::Item); - this->setData(child, QVariant(name), Qt::EditRole); - - return true; -} - -/*******************************************************************/ -bool ShoppingTreeModel::removeCategoryOrItem(const QModelIndex &index) -{ - return this->removeRow(index.row(), index.parent()); -} - -/*******************************************************************/ -QVariant ShoppingTreeModel::data(const QModelIndex &index, int role) const -{ - if(!index.isValid()) + if (!index.isValid()) return QVariant(); - if(role != Qt::DisplayRole && role != Qt::EditRole) + if (role != Qt::DisplayRole) return QVariant(); - ShoppingTreeItem *item = getItem(index); - return item->data(index.column()); -} + DomItem *item = static_cast(index.internalPointer()); -/*******************************************************************/ -Qt::ItemFlags ShoppingTreeModel::flags(const QModelIndex &index) const -{ - if(!index.isValid()) - return 0; + QDomNode node = item->node(); - return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable; + switch (index.column()) { + case 0: + return node.firstChildElement("title").text(); + case 1: + if(node.toElement().nodeName() == "item") + { + return node.firstChildElement("quantity").text(); + } + return QVariant(); + case 2: + if(node.toElement().nodeName() == "item") + { + return node.firstChildElement("store").text(); + } + return QVariant(); + case 3: + if(node.toElement().nodeName() == "item") + { + return (bool) node.firstChildElement("checked").text().toInt(); + } + return QVariant(); + default: + return QVariant(); + } } /*******************************************************************/ -ShoppingTreeItem *ShoppingTreeModel::getItem(const QModelIndex &index) const +Qt::ItemFlags DomModel::flags(const QModelIndex &index) const { - if(index.isValid()){ - ShoppingTreeItem *item = - static_cast(index.internalPointer()); - if(item) return item; - } + if (!index.isValid()) + return 0; - return rootItem; + return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable; } /*******************************************************************/ -QVariant ShoppingTreeModel::headerData(int section, - Qt::Orientation orientation, - int role) const +QVariant DomModel::headerData(int section, Qt::Orientation orientation, + int role) const { - if(orientation == Qt::Horizontal && role == Qt::DisplayRole) - return rootItem->data(section); + if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { + switch (section) { + case 0: + return tr("Category/Item name"); + case 1: + return tr("Quantity"); + case 2: + return tr("Store"); + case 3: + return tr("Checked"); + default: + return QVariant(); + } + } return QVariant(); } /*******************************************************************/ -QModelIndex ShoppingTreeModel::index(int row, int column, - const QModelIndex &parent) const +QModelIndex DomModel::index(int row, int column, const QModelIndex &parent) + const { - if(parent.isValid() && parent.column() != 0) + if (!hasIndex(row, column, parent)) return QModelIndex(); - ShoppingTreeItem *parentItem = getItem(parent); + DomItem *parentItem; - ShoppingTreeItem *childItem = parentItem->child(row); - if(childItem) + if (!parent.isValid()) + parentItem = rootItem; + else + parentItem = static_cast(parent.internalPointer()); + + DomItem *childItem = parentItem->child(row); + if (childItem) return createIndex(row, column, childItem); else return QModelIndex(); } /*******************************************************************/ -bool ShoppingTreeModel::insertColumns(int position, int columns, - const QModelIndex &parent) +QModelIndex DomModel::parent(const QModelIndex &child) const { - 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); - bool success = false; - - beginInsertRows(parent, position, position + rows - 1); - success = parentItem->insertChildren(position, rows, - rootItem->columnCount(), this); - endInsertRows(); - - return success; -} - -/*******************************************************************/ -QModelIndex ShoppingTreeModel::parent(const QModelIndex &index) const -{ - if(!index.isValid()) + if (!child.isValid()) return QModelIndex(); - ShoppingTreeItem *childItem = getItem(index); - ShoppingTreeItem *parentItem = childItem->parent(); + DomItem *childItem = static_cast(child.internalPointer()); + DomItem *parentItem = childItem->parent(); - if(parentItem == rootItem) + if (!parentItem || parentItem == rootItem) return QModelIndex(); - return createIndex(parentItem->childNumber(), 0, parentItem); + return createIndex(parentItem->row(), 0, parentItem); } /*******************************************************************/ -bool ShoppingTreeModel::removeColumns(int position, int columns, const QModelIndex &parent) +int DomModel::rowCount(const QModelIndex &parent) const { - 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); - bool success; - - beginRemoveRows(parent, position, position + rows - 1); - success = parentItem->removeChildren(position, rows); - endRemoveRows(); - - return success; -} - -/*******************************************************************/ -int ShoppingTreeModel::rowCount(const QModelIndex &parent) const -{ - ShoppingTreeItem *parentItem = getItem(parent); - - return parentItem->childCount(); -} - -/*******************************************************************/ -int ShoppingTreeModel::columnCount(const QModelIndex &parent) const -{ - if(parent.isValid()) - return getItem(parent)->columnCount(); - else - return rootItem->columnCount(); -} - -/*******************************************************************/ -bool ShoppingTreeModel::setData(const QModelIndex &index, const QVariant &value, int role) -{ - if(role != Qt::EditRole) - return false; - - ShoppingTreeItem *item = getItem(index); - - // only "items" have more than one editable column - if(index.column() != 0 && m_domElementForItem.value(item).tagName() != "item") - return false; - - // edit item - bool result = (item->setData(index.column(),value) && - updateDomElement(item, index.column())); - - if(result) - emit dataChanged(index, index); - - return result; -} - -/*******************************************************************/ -bool ShoppingTreeModel::setHeaderData(int section, Qt::Orientation orientation, - const QVariant &value, int role) -{ - if(role != Qt::EditRole || orientation != Qt::Horizontal) - return false; - - bool result = rootItem->setData(section, value); - - if(result) - emit headerDataChanged(orientation, section, section); - - return result; -} - -/*******************************************************************/ -void ShoppingTreeModel::registerInsertedChild(ShoppingTreeItem *item) -{ - QDomElement parentElement = m_domElementForItem.value(item->parent()); - QDomElement childElement; - if(item->getItemType() == ShoppingTreeItem::Category) - { - childElement = m_document.createElement("category"); - QDomElement title = m_document.createElement("title"); - QDomText newTitleText = m_document.createTextNode("new category"); - title.appendChild(newTitleText); - childElement.appendChild(title); - } - else if(item->getItemType() == ShoppingTreeItem::Item) - { - childElement = m_document.createElement("category"); - QDomElement title = m_document.createElement("title"); - QDomText newTitleText = m_document.createTextNode("new category"); - title.appendChild(newTitleText); - childElement.appendChild(title); - QDomElement quantity = m_document.createElement("quantity"); - QDomText newQuantityText = m_document.createTextNode("0"); - quantity.appendChild(newQuantityText); - childElement.appendChild(quantity); - QDomElement store = m_document.createElement("store"); - QDomText newStoreText = m_document.createTextNode(""); - store.appendChild(newStoreText); - childElement.appendChild(store); - QDomElement lastModified = m_document.createElement("lastModified"); - QDomText newDateText = m_document.createTextNode( - QDateTime::currentDateTime().toString("dd/MM/yyyy-hh:mm:ss")); - lastModified.appendChild(newDateText); - childElement.appendChild(lastModified); - } - else - return; - - parentElement.appendChild(childElement); - m_domElementForItem.insert(item, childElement); - updateXmlFile(); -} + if (parent.column() > 0) + return 0; -/*******************************************************************/ -void ShoppingTreeModel::deleteRemovedChild(ShoppingTreeItem *item) -{ - QDomElement element = m_domElementForItem.value(item); - QDomNode parentNode = element.parentNode(); - parentNode.removeChild(element); - updateXmlFile(); - m_domElementForItem.remove(item); -} + DomItem *parentItem; -/*******************************************************************/ -void ShoppingTreeModel::parseCategoryElement(const QDomElement &element, - ShoppingTreeItem *parentItem) -{ - // if parent is null then add category to root - if(!parentItem) + if (!parent.isValid()) parentItem = rootItem; - - ShoppingTreeItem *item; - QDomElement child = element.firstChildElement("title"); - QString title = child.text(); - if(!title.isEmpty()) - { - 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); - item = parentItem->child(parentItem->childCount() - 1); - } else - { - emit invalidDocument(); - return; - } + parentItem = static_cast(parent.internalPointer()); - // add each sub category and item to the tree - child = child.nextSiblingElement(); - while(!child.isNull()) + int childCount = 0; + for(QDomElement childElement = parentItem->node().firstChildElement(); + !childElement.isNull(); childElement = childElement.nextSiblingElement()) { - if(child.tagName() == "category") - { - parseCategoryElement(child, parentItem); - } - else if(child.tagName() == "item") - { - item->insertChildren( - item->childCount(), 1, - 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]); - } - m_domElementForItem.insert(item->child(item->childCount() - 1), - child); - } - else - { - emit invalidDocument(); - return; - } - - child = child.nextSiblingElement(); - } -} - -/*******************************************************************/ -QVector ShoppingTreeModel::getColumnsFromItemElement(const QDomElement &element) -{ - QVector data; - QString title = element.firstChildElement("title").text(); - int quantity = element.firstChildElement("quantity").text().toInt(); - QString store = element.firstChildElement("store").text(); - if(title.isEmpty() || quantity < 0) - { - emit invalidDocument(); - return data; - } - - data << title << quantity << store; - return data; -} - -/*******************************************************************/ -bool ShoppingTreeModel::updateDomElement(ShoppingTreeItem *item, int column) -{ - QDomElement element = m_domElementForItem.value(item); - - if(element.isNull()) - return false; - - bool success = false; - switch(column) - { - case 0: - { - QDomElement oldTitleElement = element.firstChildElement("title"); - QDomElement newTitleElement = m_document.createElement("title"); - - QDomText newTitleText = m_document.createTextNode(item->data(0).toString()); - newTitleElement.appendChild(newTitleText); - - element.replaceChild(newTitleElement, oldTitleElement); - success = true; - break; - } - case 1: - { - QDomElement oldQuantityElement = element.firstChildElement("quantity"); - QDomElement newQuantityElement = m_document.createElement("quantity"); - - QDomText newQuantityText = m_document.createTextNode(item->data(1).toString()); - newQuantityElement.appendChild(newQuantityText); - - element.replaceChild(newQuantityElement, oldQuantityElement); - success = true; - break; - } - case 2: - { - QDomElement oldStoreElement = element.firstChildElement("store"); - QDomElement newStoreElement = m_document.createElement("store"); - - QDomText newStoreText = m_document.createTextNode(item->data(0).toString()); - newStoreElement.appendChild(newStoreText); - - element.replaceChild(newStoreElement, oldStoreElement); - success = true; - break; - } - default: - success = false; + if(childElement.nodeName() == "category" || childElement.nodeName() == "item") + childCount++; } - - QDomElement oldDateElement = element.firstChildElement("lastModified"); - if(!oldDateElement.isNull()) - { - QDomElement newDateElement = m_document.createElement("lastModified"); - - QDomText newDateText = m_document.createTextNode( - QDateTime::currentDateTime().toString("dd/MM/yyyy-hh:mm:ss")); - newDateElement.appendChild(newDateText); - - element.replaceChild(newDateElement, oldDateElement); - } - - updateXmlFile(); - - return success; -} - -/*******************************************************************/ -void ShoppingTreeModel::updateXmlFile() const -{ - QFile xmlFile(m_xmlFileName); - xmlFile.remove(); - xmlFile.open(QIODevice::WriteOnly); - xmlFile.write(m_document.toByteArray(4)); - xmlFile.close(); -} - -/*******************************************************************/ -void ShoppingTreeModel::sort(int column, Qt::SortOrder order) -{ - // TODO: implement sorting - ; + return childCount; } diff --git a/code/family-shop-mgr/ShoppingTreeModel.h b/code/family-shop-mgr/ShoppingTreeModel.h index 8535e62..92ef5c7 100644 --- a/code/family-shop-mgr/ShoppingTreeModel.h +++ b/code/family-shop-mgr/ShoppingTreeModel.h @@ -25,76 +25,32 @@ #include #include #include -#include #include -#include -class ShoppingTreeItem; +class DomItem; -class ShoppingTreeModel : public QAbstractItemModel +class DomModel : public QAbstractItemModel { -Q_OBJECT + Q_OBJECT public: - ShoppingTreeModel(const QString &xmlFileName, QObject *parent = 0); - ~ShoppingTreeModel(); - - bool addCategory(QString name); - bool addSubCategory(QString name, int position, - const QModelIndex &parent = QModelIndex()); - bool addItem(QString name, int position, - const QModelIndex &parent = QModelIndex()); - bool removeCategoryOrItem(const QModelIndex &index = QModelIndex()); + DomModel(QDomDocument document, QObject *parent = 0); + ~DomModel(); QVariant data(const QModelIndex &index, int role) const; + Qt::ItemFlags flags(const QModelIndex &index) const; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; - QModelIndex parent(const QModelIndex &index) const; - + QModelIndex parent(const QModelIndex &child) const; int rowCount(const QModelIndex &parent = QModelIndex()) const; int columnCount(const QModelIndex &parent = QModelIndex()) const; - Qt::ItemFlags flags(const QModelIndex &index) const; - bool setData(const QModelIndex &index, const QVariant &value, - int role = Qt::EditRole); - bool setHeaderData(int section, Qt::Orientation orientation, - const QVariant &value, int role = Qt::EditRole); - void sort(int column, Qt::SortOrder order); - private: - 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, - const QModelIndex &parent = QModelIndex()); - -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); - QVector getColumnsFromItemElement(const QDomElement &element); - bool updateDomElement(ShoppingTreeItem *item, int column); - void updateXmlFile() const; - -private: - ShoppingTreeItem *getItem(const QModelIndex &index) const; - - ShoppingTreeItem *rootItem; - QString m_xmlFileName; - QDomDocument m_document; - QHash m_domElementForItem; + QDomDocument domDocument; + QDomNode rootItemNode; + DomItem *rootItem; }; #endif // SHOPPINGTREEMODEL_H