From e57aab740273f11bca064efe2bc1062ac5f8f982 Mon Sep 17 00:00:00 2001 From: tkawata Date: Sat, 13 Oct 2012 15:25:52 +0900 Subject: [PATCH] [denncoCreator] implemented rename receptor functionality. --- Source/command/dceditcommands.cpp | 17 ++ Source/command/dceditcommands.h | 15 ++ Source/dccell.cpp | 20 ++ Source/dccell.h | 5 + Source/dccreator.cpp | 17 ++ Source/dccreator.h | 2 + Source/utils/dccommandutil.cpp | 13 + Source/utils/dccommandutil.h | 5 + Source/utils/dcqtitemmodel.h | 6 +- .../toolwindow/dctoolwindowcelleditor.cpp | 263 +++++++++++++++++++-- .../visualizer/toolwindow/dctoolwindowcelleditor.h | 2 + 11 files changed, 344 insertions(+), 21 deletions(-) diff --git a/Source/command/dceditcommands.cpp b/Source/command/dceditcommands.cpp index a6a904d..a4a9e9a 100644 --- a/Source/command/dceditcommands.cpp +++ b/Source/command/dceditcommands.cpp @@ -743,3 +743,20 @@ void DCRemoveDirectoryCommand::undo() } +/*--------------------------------*/ +DCRenameReceptorNameCommand::DCRenameReceptorNameCommand(const void *requester, DCCreator *creator, DCCell *cell, const QString &oldName, const QString &newName) + : DCCommand(requester, creator), d_cell(cell), d_oldName(oldName), d_newName(newName) +{ + +} + +void DCRenameReceptorNameCommand::redo() +{ + bool r = d_cell->renameReceptor(d_oldName, d_newName); + setCommandResult(r ? DCCommand::DCCOMMAND_SUCCEEDED : DCCommand::DCCOMMAND_FAILED); +} + +void DCRenameReceptorNameCommand::undo() +{ + +} diff --git a/Source/command/dceditcommands.h b/Source/command/dceditcommands.h index 75e1388..6c91521 100644 --- a/Source/command/dceditcommands.h +++ b/Source/command/dceditcommands.h @@ -323,4 +323,19 @@ public: void redo(); }; +//------------------------------------------ + +class DCRenameReceptorNameCommand : public DCCommand +{ + DCCell* d_cell; + QString d_oldName; + QString d_newName; + +public: + DCRenameReceptorNameCommand(const void *requester, DCCreator *creator, DCCell *cell, const QString& oldName, const QString& newName); + virtual ~DCRenameReceptorNameCommand() {} + void undo(); + void redo(); +}; + #endif // DCEDITCOMMANDS_H diff --git a/Source/dccell.cpp b/Source/dccell.cpp index 85102bf..3242cb6 100644 --- a/Source/dccell.cpp +++ b/Source/dccell.cpp @@ -211,6 +211,26 @@ void DCCell::setViewHeight(float _height) } } +bool DCCell::renameReceptor(const QString &oldName, const QString &newName) +{ + if (newName.length() == 0) + return false; + + TKReceptorMap::iterator newNameIt = mReceptors.find(newName.toStdString()); + if (newNameIt != mReceptors.end()) + return false; + + TKReceptorMap::iterator it = mReceptors.find(oldName.toStdString()); + if (it != mReceptors.end()) + { + TKReceptor *receptor = (*it).second; + mReceptors.erase(it); + mReceptors.insert( TKReceptorMap::value_type( newName.toStdString(), receptor ) ); + return true; + } + return false; +} + bool DCCell::removeReceptor(const QString &name) { TKReceptorMap::iterator it = mReceptors.find(name.toStdString()); diff --git a/Source/dccell.h b/Source/dccell.h index 208af75..3899bb9 100644 --- a/Source/dccell.h +++ b/Source/dccell.h @@ -100,6 +100,11 @@ public: */ /** + * Rename receptor name + */ + bool renameReceptor(const QString& oldName, const QString& newName); + + /** * Remove receptor from the list * Note that this remove receptor object but won't remove the axon terminal connected to the receptor. * This method is expected to be only called from command classes (defined in dceditcommands.h ) diff --git a/Source/dccreator.cpp b/Source/dccreator.cpp index 97be4b1..b92ff8d 100644 --- a/Source/dccreator.cpp +++ b/Source/dccreator.cpp @@ -30,6 +30,8 @@ #include "dcreceptor.h" #include "command/dccommandevent.h" +#include "command/dccommand.h" + #include "utils/dcdialogutil.h" #include "utils/dccommandutil.h" #include "dialog/dcinputreceptornamedialog.h" @@ -503,6 +505,21 @@ void DCCreator::doCommandRemoveDirectory(const void *requester, const QString &s DCCommandUtil::postRemoveDirectoryCommand(requester, this, sysFilePath); } +bool DCCreator::doCommandRenameReceptorName(const void *requester, DCCell *cell, const QString &oldName, const QString &newName, bool doImmediate) +{ + if (doImmediate) + { + DCCommand *command = DCCommandUtil::createRenameReceptorNameCommand(requester, this, cell, oldName, newName); + d_undoStack->push(command); + return command->getCommandResult() == DCCommand::DCCOMMAND_SUCCEEDED; + } + else + { + DCCommandUtil::postRenameReceptorNameCommand(requester, this, cell, oldName, newName); + return true; + } +} + DCContainer* DCCreator::getCurrentContainer() const { if (d_vcontent) diff --git a/Source/dccreator.h b/Source/dccreator.h index 658822b..e30d7f0 100644 --- a/Source/dccreator.h +++ b/Source/dccreator.h @@ -118,6 +118,8 @@ public: void doCommandRenameDirectory(const void *requester, const QString& oldSysFilePath, const QString& newSysFilePath); void doCommandRemoveDirectory(const void *requester, const QString& sysFilePath); + bool doCommandRenameReceptorName(const void *requester, DCCell *cell, const QString& oldName, const QString& newName, bool doImmediate = false); + private slots: void slotSceneSelectedPageChanged(const void *requester); void slotSceneSelectedCellObjectChanged(const void *requester); diff --git a/Source/utils/dccommandutil.cpp b/Source/utils/dccommandutil.cpp index a5467cb..53e288f 100644 --- a/Source/utils/dccommandutil.cpp +++ b/Source/utils/dccommandutil.cpp @@ -151,3 +151,16 @@ void DCCommandUtil::postRemoveDirectoryCommand(const void *requester, DCCreator { postEvent(creator, new DCRemoveDirectoryCommand(requester, creator, sysFilePath)); } + +//static +void DCCommandUtil::postRenameReceptorNameCommand(const void *requester, DCCreator *creator, DCCell *cell, const QString &oldName, const QString &newName) +{ + postEvent(creator, new DCRenameReceptorNameCommand(requester, creator, cell, oldName, newName)); +} + +//static +DCCommand* DCCommandUtil::createRenameReceptorNameCommand(const void *requester, DCCreator *creator, DCCell *cell, const QString &oldName, const QString &newName) +{ + return new DCRenameReceptorNameCommand(requester, creator, cell, oldName, newName); +} + diff --git a/Source/utils/dccommandutil.h b/Source/utils/dccommandutil.h index 863d9db..46a17c2 100644 --- a/Source/utils/dccommandutil.h +++ b/Source/utils/dccommandutil.h @@ -58,6 +58,11 @@ struct DCCommandUtil static void postAddDirectoryCommand(const void *requester, DCCreator *creator, const QString& sysFilePath); static void postRenameDirectoryCommand(const void *requester, DCCreator *creator, const QString& sysOldFilePath, const QString& sysNewFilePath); static void postRemoveDirectoryCommand(const void *requester, DCCreator *creator, const QString& sysFilePath); + + static void postRenameReceptorNameCommand(const void *requester, DCCreator *creator, DCCell *cell, const QString& oldName, const QString& newName); + + static DCCommand* createRenameReceptorNameCommand(const void *requester, DCCreator *creator, DCCell *cell, const QString& oldName, const QString& newName); + }; #endif // DCCOMMANDUTIL_H diff --git a/Source/utils/dcqtitemmodel.h b/Source/utils/dcqtitemmodel.h index 41b63c7..c19bf00 100644 --- a/Source/utils/dcqtitemmodel.h +++ b/Source/utils/dcqtitemmodel.h @@ -63,8 +63,8 @@ public: 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); + virtual Qt::ItemFlags flags(const QModelIndex &index) const; + virtual 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); bool insertColumns(int position, int columns, const QModelIndex &parent = QModelIndex()); @@ -78,7 +78,7 @@ public: bool insertString(const QString &string, const QModelIndex &parent = QModelIndex()); void setReadOnly(int column, bool isReadOnly); -private: +protected: DCQtItemModelItem *getItem(const QModelIndex &index) const; DCQtItemModelItem *mRootItem; diff --git a/Source/visualizer/toolwindow/dctoolwindowcelleditor.cpp b/Source/visualizer/toolwindow/dctoolwindowcelleditor.cpp index 348777a..388ecda 100644 --- a/Source/visualizer/toolwindow/dctoolwindowcelleditor.cpp +++ b/Source/visualizer/toolwindow/dctoolwindowcelleditor.cpp @@ -29,6 +29,7 @@ #include "dccontainer.h" #include "utils/dcresources.h" #include "utils/dcqtitemmodel.h" +#include "utils/dcutil.h" #include #include @@ -36,8 +37,205 @@ #include #include +class ReceptorModel : public DCQtItemModel +{ + DCToolWindowCellEditor *d_owner; +public: + ReceptorModel(DCToolWindowCellEditor *owner) : DCQtItemModel(QStringList("name")), d_owner(owner) + { + + } + + virtual ~ReceptorModel() + { + } + + virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) + { + if (role != Qt::EditRole) + return false; + + DCCell *cell = d_owner->getCell(); + if (!cell) + return false; + + DCQtItemModelItem *item = getItem(index); + QString oldReceptorName = item->data(index.column()).toString(); + QString newReceptorName = value.toString().trimmed(); + if (newReceptorName.length() == 0) + { + return false; + } + + if (oldReceptorName == newReceptorName) + { + //nothing actually changes + return false; + } + + if (!d_owner->getIsViewUpdating()) + { + if (cell->getReceptor(newReceptorName)) + { + //already exist + QMessageBox::warning(NULL, tr("Rename receptor failed"), tr("The receptor name already exist")); + return false; + } + + bool result = item->setData(index.column(), newReceptorName); + + if (result) + { + DCCreator *creator = d_owner->getController(); + if (creator->doCommandRenameReceptorName(d_owner, cell, oldReceptorName, newReceptorName, true)) + { + emit dataChanged(index, index); + } + else + { + item->setData(index.column(), oldReceptorName); + } + } + return result; + } + else + { + return item->setData(index.column(), newReceptorName); + } + } + + virtual Qt::ItemFlags flags(const QModelIndex &index) const + { + if (!index.isValid()) + return 0; + + Qt::ItemFlags flags = Qt::ItemIsEnabled; + + if (index.column() == 0 && index.isValid() && index.parent().isValid() && !index.parent().parent().isValid()) + { + flags |= Qt::ItemIsEditable | Qt::ItemIsSelectable; + } + + return flags; + } +}; + +class AxonTerminalModel : public DCQtItemModel +{ + DCToolWindowCellEditor *d_owner; +public: + AxonTerminalModel(DCToolWindowCellEditor *owner) : DCQtItemModel(QStringList("name")), d_owner(owner) + { + + } + + virtual ~AxonTerminalModel() + { + } + + virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) + { + if (role != Qt::EditRole) + return false; + + DCCell *cell = d_owner->getCell(); + if (!cell) + return false; + + DCQtItemModelItem *item = getItem(index); + QString oldReceptorName = item->data(index.column()).toString(); + QString newReceptorName = value.toString().trimmed(); + if (newReceptorName.length() == 0) + { + return false; + } + + if (oldReceptorName == newReceptorName) + { + //nothing actually changes + return false; + } + + DCCell *axonTargetCell = NULL; + if (!d_owner->getIsViewUpdating()) + { + bool alreadyExist = false; + DCAxon *axon = cell->getAxon(); + int num = axon->getNumberOfTerminals(); + for (int i = 0; i < num; i++) + { + DCReceptor *receptor = dynamic_cast(axon->getTerminalAt(i)->getTarget()); + if (receptor) + { + DCCell *targetCell = dynamic_cast(receptor->getOwnerCell()); + if (targetCell) + { + QString targetReceptorName = QString::fromStdString(targetCell->getReceptorName(receptor)); + if (targetReceptorName == newReceptorName) + { + alreadyExist = true; + break; + } + else if (targetReceptorName == oldReceptorName) + { + axonTargetCell = targetCell; + } + } + } + } + + if (alreadyExist) + { + QMessageBox::warning(NULL, tr("Rename receptor failed"), tr("The receptor name already exist")); + return false; + } + + bool result = false; + + if (axonTargetCell) + { + result = item->setData(index.column(), newReceptorName); + + if (result) + { + DCCreator *creator = d_owner->getController(); + if (creator->doCommandRenameReceptorName(d_owner, axonTargetCell, oldReceptorName, newReceptorName, true)) + { + emit dataChanged(index, index); + } + else + { + item->setData(index.column(), oldReceptorName); + } + } + } + return result; + } + else + { + return item->setData(index.column(), newReceptorName); + } + } + + virtual Qt::ItemFlags flags(const QModelIndex &index) const + { + if (!index.isValid()) + return 0; + + Qt::ItemFlags flags = Qt::ItemIsEnabled; + + if (index.column() == 0 && index.isValid() && index.parent().isValid() && index.parent().parent().isValid() && !index.parent().parent().parent().isValid()) + { + flags |= Qt::ItemIsEditable | Qt::ItemIsSelectable; + } + + return flags; + } + +}; + DCToolWindowCellEditor::DCToolWindowCellEditor(DCCreator *creator) : - DCToolWindowBase("CELL:", creator), d_cell(NULL) + DCToolWindowBase("CELL:", creator), d_cell(NULL), d_isViewUpdating(false) { d_layout = new QGridLayout; d_layout->setSpacing(6); @@ -72,8 +270,8 @@ DCToolWindowCellEditor::DCToolWindowCellEditor(DCCreator *creator) : d_axonTerminals->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - d_receptorItemModel = new DCQtItemModel(QStringList("name")); - d_axonTerminalItemModel = new DCQtItemModel(QStringList("name")); + d_receptorItemModel = new ReceptorModel(this); + d_axonTerminalItemModel = new AxonTerminalModel(this); d_receptors->setModel(d_receptorItemModel); d_axonTerminals->setModel(d_axonTerminalItemModel); @@ -136,6 +334,7 @@ void DCToolWindowCellEditor::setCell(DCCell *cell) void DCToolWindowCellEditor::updateView() { + d_isViewUpdating = true; d_receptorItemModel->removeAllItems(); d_axonTerminalItemModel->removeAllItems(); @@ -146,12 +345,10 @@ void DCToolWindowCellEditor::updateView() d_receptorItemModel->insertString("receptors"); d_receptorItemModel->insertColumns(1,2); - d_receptorItemModel->setReadOnly(0,false); d_receptorItemModel->setData(d_receptorItemModel->index(0,1), QVariant::fromValue(DCResources::addItemIcon())); d_axonTerminalItemModel->insertString("axonTerminals"); d_axonTerminalItemModel->insertColumns(1,2); - d_axonTerminalItemModel->setReadOnly(0,false); d_axonTerminalItemModel->setData(d_axonTerminalItemModel->index(0,1), QVariant::fromValue(DCResources::addItemIcon())); const TKReceptorMap *receptors = d_cell->getReceptors(); @@ -161,13 +358,37 @@ void DCToolWindowCellEditor::updateView() while( it != receptors->end()) { - QString receptorName = QString::fromStdString((*it).first); + QString receptorName = QString::fromStdString((*it).first); d_receptorItemModel->insertString(receptorName,rroot); d_receptorItemModel->setData(rroot.child(i,2), QVariant::fromValue(DCResources::deleteItemIcon())); + + TKReceptor *receptor = (*it).second; + TKAxonTerminal *terminal = receptor->getTarget(); + if (terminal) + { + TKCell *targetCell = terminal->getOwner()->getOwner(); + if (targetCell) + { + QString targetPath = DCUtil::getFQNPath(targetCell->getLocation(), targetCell->getName()); + d_receptorItemModel->insertString(targetPath,rroot.child(i,0)); + } + } + ++it; ++i; } + d_receptors->expand(rroot); + for (int i = 0; i < d_receptorItemModel->rowCount(rroot); i++) + { + QModelIndex index = rroot.child(i,0); + d_receptors->collapse(index); + if (d_receptorItemModel->rowCount(index) > 0) + { + d_receptors->collapse(index.child(0,0)); + } + } + QModelIndex aroot = d_axonTerminalItemModel->index(0,0); DCAxon *axon = d_cell->getAxon(); int acnt = axon->getNumberOfTerminals(); @@ -184,30 +405,35 @@ void DCToolWindowCellEditor::updateView() targetCell = dynamic_cast(targetReceptor->getOwnerCell()); } } - QString axonTerminalName; if (targetCell) { - QTextStream(&axonTerminalName) - << QString::fromStdString(targetCell->getLocation()) - << "/" - << QString::fromStdString(targetCell->getName()) - << "#" - << QString::fromStdString(targetCell->getReceptorName(targetReceptor)); + QString targetPath = DCUtil::getFQNPath(targetCell->getLocation(), targetCell->getName()); + QString targetReceptorName = QString::fromStdString(targetCell->getReceptorName(targetReceptor)); + d_axonTerminalItemModel->insertString(targetPath,aroot); + d_receptorItemModel->setData(aroot.child(i,2), QVariant::fromValue(DCResources::deleteItemIcon())); + d_axonTerminalItemModel->insertString(targetReceptorName,aroot.child(i,0)); } else { - axonTerminalName = "-"; + //TODO } + } - d_axonTerminalItemModel->insertString(axonTerminalName,aroot); - d_receptorItemModel->setData(aroot.child(i,2), QVariant::fromValue(DCResources::deleteItemIcon())); + d_axonTerminals->expand(aroot); + for (int i = 0; i < d_axonTerminalItemModel->rowCount(aroot); i++) + { + QModelIndex index = aroot.child(i,0); + d_axonTerminals->collapse(index); + if (d_axonTerminalItemModel->rowCount(index) > 0) + { + d_axonTerminals->collapse(index.child(0,0)); + } } - d_receptors->expandAll(); - d_axonTerminals->expandAll(); resizeView(); } + d_isViewUpdating = false; } void DCToolWindowCellEditor::slotReceptorTreeCollapsed(const QModelIndex &index) @@ -380,3 +606,4 @@ void DCToolWindowCellEditor::slotCellDestroyed() { setCell(NULL); } + diff --git a/Source/visualizer/toolwindow/dctoolwindowcelleditor.h b/Source/visualizer/toolwindow/dctoolwindowcelleditor.h index d5a45a7..a0bfebe 100644 --- a/Source/visualizer/toolwindow/dctoolwindowcelleditor.h +++ b/Source/visualizer/toolwindow/dctoolwindowcelleditor.h @@ -42,6 +42,7 @@ private: DCEditableTreeView *d_axonTerminals; DCQtItemModel *d_axonTerminalItemModel; QGridLayout *d_layout; + bool d_isViewUpdating; void resizeView(); @@ -51,6 +52,7 @@ public: virtual int getPosPriority() const { return 2; } DCCell* getCell() const { return d_cell; } + bool getIsViewUpdating() const { return d_isViewUpdating; } void setCell(DCCell *cell); -- 2.11.0