From 0275da0491e101d2c55c109f3753c7b2da2e5364 Mon Sep 17 00:00:00 2001 From: onil Date: Wed, 10 Mar 2010 10:40:09 +0000 Subject: [PATCH] git-svn-id: file:///svnroot/family-shop-mgr@23 26eb2498-383b-47a6-be48-5d6f36779e85 --- .../FamilyShoppingManagerMainWindow.cpp | 97 ++++++++------------ .../FamilyShoppingManagerMainWindow.h | 11 ++- code/family-shop-mgr/ShoppingTreeModel.cpp | 61 +++++++++++- code/family-shop-mgr/ShoppingTreeModel.h | 8 ++ code/family-shop-mgr/family-shop-mgr.pro.user | 24 ++--- 5 files changed, 125 insertions(+), 76 deletions(-) diff --git a/code/family-shop-mgr/FamilyShoppingManagerMainWindow.cpp b/code/family-shop-mgr/FamilyShoppingManagerMainWindow.cpp index bb8329e..4b821f7 100644 --- a/code/family-shop-mgr/FamilyShoppingManagerMainWindow.cpp +++ b/code/family-shop-mgr/FamilyShoppingManagerMainWindow.cpp @@ -25,6 +25,7 @@ #include "ListManagerView.h" #include "GoShoppingView.h" +#include "ShoppingTreeModel.h" /*******************************************************************/ FamilyShoppingManagerMainWindow::FamilyShoppingManagerMainWindow(QWidget *parent) @@ -60,20 +61,20 @@ void FamilyShoppingManagerMainWindow::showListManager() editMenu = new QMenu(tr("&Edit"), this); addCategoryAction = new QAction(tr("Add category"), this); connect(addCategoryAction, SIGNAL(triggered()), - this, SLOT(insertRow())); + this, SLOT(addCategory())); editMenu->addAction(addCategoryAction); - removeCategoryAction = new QAction(tr("Remove category"), this); - connect(removeCategoryAction, SIGNAL(triggered()), - this, SLOT(removeRow())); - editMenu->addAction(removeCategoryAction); - addItemAction = new QAction(tr("Add item"), this); - connect(addItemAction, SIGNAL(triggered()), - this, SLOT(insertRow())); - editMenu->addAction(addItemAction); - removeItemAction = new QAction(tr("Remove item"), this); - connect(removeItemAction, SIGNAL(triggered()), - this, SLOT(removeRow())); - editMenu->addAction(removeItemAction); +// removeCategoryAction = new QAction(tr("Remove category"), this); +// connect(removeCategoryAction, SIGNAL(triggered()), +// this, SLOT(removeRow())); +// editMenu->addAction(removeCategoryAction); +// addItemAction = new QAction(tr("Add item"), this); +// connect(addItemAction, SIGNAL(triggered()), +// this, SLOT(insertRow())); +// editMenu->addAction(addItemAction); +// removeItemAction = new QAction(tr("Remove item"), this); +// connect(removeItemAction, SIGNAL(triggered()), +// this, SLOT(removeRow())); +// editMenu->addAction(removeItemAction); menuBar()->addMenu(editMenu); goShoppingAction = new QAction(tr("Go shopping!"), this); @@ -122,6 +123,7 @@ void FamilyShoppingManagerMainWindow::showAbout() } /*******************************************************************/ +/* void FamilyShoppingManagerMainWindow::insertChild() { QModelIndex index = ((ListManagerView*) activityView)-> @@ -150,70 +152,49 @@ void FamilyShoppingManagerMainWindow::insertChild() QItemSelectionModel::ClearAndSelect); ((ListManagerView*) activityView)->updateActions(); } + */ /*******************************************************************/ -/* -bool FamilyShoppingManagerMainWindow::insertColumn(const QModelIndex &parent) +void FamilyShoppingManagerMainWindow::addCategory() { - QAbstractItemModel *model = ((ListManagerView*) activityView)->model(); - int column = ((ListManagerView*) activityView)->selectionModel()-> - currentIndex().column(); + ShoppingTreeModel *model = (ShoppingTreeModel*) + ((ListManagerView*) activityView)->model(); - // Insert a column in the parent item. - bool changed = model->insertColumn(column + 1, parent); - if (changed) - model->setHeaderData(column + 1, Qt::Horizontal, - QVariant("[No header]"), Qt::EditRole); - - ((ListManagerView*) activityView)->updateActions(); - - return changed; + if(model->addCategory("New category")) + ((ListManagerView*) activityView)->updateActions(); } -*/ /*******************************************************************/ -void FamilyShoppingManagerMainWindow::insertRow() +void FamilyShoppingManagerMainWindow::addSubCategory() { - QModelIndex index = ((ListManagerView*) activityView)-> - selectionModel()->currentIndex(); - QAbstractItemModel *model = ((ListManagerView*) activityView)->model(); + QModelIndex index = ((ListManagerView*) activityView)-> + selectionModel()->currentIndex().parent(); + ShoppingTreeModel *model = (ShoppingTreeModel*) + ((ListManagerView*) activityView)->model(); - if (!model->insertRow(index.row()+1, index.parent())) - return; - - ((ListManagerView*) activityView)->updateActions(); - - for(int column = 0; column < model->columnCount(index.parent()); ++column) - { - QModelIndex child = model->index(index.row()+1, column, index.parent()); - model->setData(child, QVariant("New Item"), Qt::EditRole); - } + if(model->addSubCategory("New sub-category", index.row()+1, index)) + ((ListManagerView*) activityView)->updateActions(); } /*******************************************************************/ -/* -bool FamilyShoppingManagerMainWindow::removeColumn(const QModelIndex &parent) +void FamilyShoppingManagerMainWindow::addItem() { - QAbstractItemModel *model = ((ListManagerView*) activityView)->model(); - int column = ((ListManagerView*) activityView)->selectionModel()-> - currentIndex().column(); + QModelIndex index = ((ListManagerView*) activityView)-> + selectionModel()->currentIndex().parent(); + ShoppingTreeModel *model = (ShoppingTreeModel*) + ((ListManagerView*) activityView)->model(); - // Insert columns in each child of the parent item. - bool changed = model->removeColumn(column, parent); - - if (!parent.isValid() && changed) - ((ListManagerView*) activityView)->updateActions(); - - return changed; + if(model->addItem("New item", index.row()+1, index)) + ((ListManagerView*) activityView)->updateActions(); } - */ /*******************************************************************/ -void FamilyShoppingManagerMainWindow::removeRow() +void FamilyShoppingManagerMainWindow::removeCategoryOrItem() { QModelIndex index = ((ListManagerView*) activityView)-> selectionModel()->currentIndex(); - QAbstractItemModel *model = ((ListManagerView*) activityView)->model(); - if (model->removeRow(index.row(), index.parent())) + ShoppingTreeModel *model = (ShoppingTreeModel*) + ((ListManagerView*) activityView)->model(); + if (model->removeCategoryOrItem(index)) ((ListManagerView*) activityView)->updateActions(); } diff --git a/code/family-shop-mgr/FamilyShoppingManagerMainWindow.h b/code/family-shop-mgr/FamilyShoppingManagerMainWindow.h index 63f8e87..701dc62 100644 --- a/code/family-shop-mgr/FamilyShoppingManagerMainWindow.h +++ b/code/family-shop-mgr/FamilyShoppingManagerMainWindow.h @@ -38,11 +38,11 @@ private slots: void showGoShopping(); void showAbout(); - void insertChild(); -// bool insertColumn(const QModelIndex &parent = QModelIndex()); - void insertRow(); -// bool removeColumn(const QModelIndex &parent = QModelIndex()); - void removeRow(); + void addCategory(); + void addSubCategory(); + void addItem(); + void removeCategoryOrItem(); + //void insertChild(); private: QWidget *activityView; @@ -54,6 +54,7 @@ private: QAction *aboutAction; QAction *addCategoryAction; + QAction *addSubCategoryAction(); QAction *removeCategoryAction; QAction *addItemAction; QAction *removeItemAction; diff --git a/code/family-shop-mgr/ShoppingTreeModel.cpp b/code/family-shop-mgr/ShoppingTreeModel.cpp index 5f2985f..d304c41 100644 --- a/code/family-shop-mgr/ShoppingTreeModel.cpp +++ b/code/family-shop-mgr/ShoppingTreeModel.cpp @@ -115,6 +115,64 @@ ShoppingTreeModel::~ShoppingTreeModel() } /*******************************************************************/ +bool ShoppingTreeModel::addCategory(QString name) +{ + QModelIndex parent = QModelIndex(); + + if (!this->insertRow(parent.row()+1, 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; +} + +/*******************************************************************/ +bool ShoppingTreeModel::addSubCategory(QString name, int position, + const QModelIndex &parent) +{ + 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; +} + +/*******************************************************************/ +bool ShoppingTreeModel::addItem(QString name, int position, + const QModelIndex &parent) +{ + 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::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()) @@ -140,7 +198,8 @@ Qt::ItemFlags ShoppingTreeModel::flags(const QModelIndex &index) const ShoppingTreeItem *ShoppingTreeModel::getItem(const QModelIndex &index) const { if(index.isValid()){ - ShoppingTreeItem *item = static_cast(index.internalPointer()); + ShoppingTreeItem *item = + static_cast(index.internalPointer()); if(item) return item; } diff --git a/code/family-shop-mgr/ShoppingTreeModel.h b/code/family-shop-mgr/ShoppingTreeModel.h index 31301d6..66829bf 100644 --- a/code/family-shop-mgr/ShoppingTreeModel.h +++ b/code/family-shop-mgr/ShoppingTreeModel.h @@ -39,6 +39,13 @@ 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()); + QVariant data(const QModelIndex &index, int role) const; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; @@ -55,6 +62,7 @@ public: bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole); +private: bool insertColumns(int position, int columns, const QModelIndex &parent = QModelIndex()); bool removeColumns(int position, int columns, diff --git a/code/family-shop-mgr/family-shop-mgr.pro.user b/code/family-shop-mgr/family-shop-mgr.pro.user index e0ed2f2..0eb46bd 100644 --- a/code/family-shop-mgr/family-shop-mgr.pro.user +++ b/code/family-shop-mgr/family-shop-mgr.pro.user @@ -97,11 +97,11 @@ Debug - DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-ftBEZLbXAE,guid=e2c8517a9ef4dda368a3470a4b96c3aa + DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-q1EKrknBIg,guid=8a525aab18f7fc1a86808b054b97539c DESKTOP_SESSION=default DISPLAY=:0.0 DM_CONTROL=/var/run/xdmctl - GPG_AGENT_INFO=/tmp/gpg-aqBEsG/S.gpg-agent:1905:1 + GPG_AGENT_INFO=/tmp/gpg-7ScX2l/S.gpg-agent:1982:1 GS_LIB=/home/onil/.fonts GTK2_RC_FILES=/etc/gtk-2.0/gtkrc:/home/onil/.gtkrc-2.0:/home/onil/.gtkrc-2.0-kde4:/home/onil/.kde/share/config/gtkrc-2.0 GTK_RC_FILES=/etc/gtk/gtkrc:/home/onil/.gtkrc::/home/onil/.kde/share/config/gtkrc @@ -117,16 +117,16 @@ PWD=/home/onil/Documents QTDIR=/usr/share/qt4 QT_PLUGIN_PATH=/home/onil/.kde/lib/kde4/plugins/:/usr/lib/kde4/plugins/ - SESSION_MANAGER=local/onil-netbook:@/tmp/.ICE-unix/1998,unix/onil-netbook:/tmp/.ICE-unix/1998 + SESSION_MANAGER=local/onil-netbook:@/tmp/.ICE-unix/2071,unix/onil-netbook:/tmp/.ICE-unix/2071 SHELL=/bin/bash SHLVL=0 - SSH_AGENT_PID=1904 - SSH_AUTH_SOCK=/tmp/ssh-gckQrI1854/agent.1854 + SSH_AGENT_PID=1981 + SSH_AUTH_SOCK=/tmp/ssh-Lrqsxa1931/agent.1931 USER=onil WINDOWPATH=7 XCURSOR_THEME=oxy-white XDG_DATA_DIRS=/usr/share:/usr/share:/usr/local/share - XDG_SESSION_COOKIE=67465ad3dd74e5003d0b02474b126985-1268171689.363286-1127686922 + XDG_SESSION_COOKIE=67465ad3dd74e5003d0b02474b126985-1268208539.486626-183142450 XDM_MANAGED=method=classic @@ -147,11 +147,11 @@ Debug - DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-ftBEZLbXAE,guid=e2c8517a9ef4dda368a3470a4b96c3aa + DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-q1EKrknBIg,guid=8a525aab18f7fc1a86808b054b97539c DESKTOP_SESSION=default DISPLAY=:0.0 DM_CONTROL=/var/run/xdmctl - GPG_AGENT_INFO=/tmp/gpg-aqBEsG/S.gpg-agent:1905:1 + GPG_AGENT_INFO=/tmp/gpg-7ScX2l/S.gpg-agent:1982:1 GS_LIB=/home/onil/.fonts GTK2_RC_FILES=/etc/gtk-2.0/gtkrc:/home/onil/.gtkrc-2.0:/home/onil/.gtkrc-2.0-kde4:/home/onil/.kde/share/config/gtkrc-2.0 GTK_RC_FILES=/etc/gtk/gtkrc:/home/onil/.gtkrc::/home/onil/.kde/share/config/gtkrc @@ -167,16 +167,16 @@ PWD=/home/onil/Documents QTDIR=/usr/share/qt4 QT_PLUGIN_PATH=/home/onil/.kde/lib/kde4/plugins/:/usr/lib/kde4/plugins/ - SESSION_MANAGER=local/onil-netbook:@/tmp/.ICE-unix/1998,unix/onil-netbook:/tmp/.ICE-unix/1998 + SESSION_MANAGER=local/onil-netbook:@/tmp/.ICE-unix/2071,unix/onil-netbook:/tmp/.ICE-unix/2071 SHELL=/bin/bash SHLVL=0 - SSH_AGENT_PID=1904 - SSH_AUTH_SOCK=/tmp/ssh-gckQrI1854/agent.1854 + SSH_AGENT_PID=1981 + SSH_AUTH_SOCK=/tmp/ssh-Lrqsxa1931/agent.1931 USER=onil WINDOWPATH=7 XCURSOR_THEME=oxy-white XDG_DATA_DIRS=/usr/share:/usr/share:/usr/local/share - XDG_SESSION_COOKIE=67465ad3dd74e5003d0b02474b126985-1268171689.363286-1127686922 + XDG_SESSION_COOKIE=67465ad3dd74e5003d0b02474b126985-1268208539.486626-183142450 XDM_MANAGED=method=classic false -- 1.7.9.5