# # # add_file "guitone/src/view/InventoryViewDelegate.cpp" # content [0b36236e7307a1e4cf3cd129431bb1df67f8666a] # # add_file "guitone/src/view/InventoryViewDelegate.h" # content [704deafe354280795ca9bfe0e7c42ab82519643f] # # patch "guitone/guitone.pro" # from [9c37c31facd0210a4d47db3d688b0f70f807c3ce] # to [159ee4bd8f31bbd72ad80397de9a45054bc76aa5] # # patch "guitone/res/forms/main_window.ui" # from [62161c4ccc8ae3874887d716c76ea8f179aa2b4c] # to [e4f7de13795e9cb65247203bb4d62da67995ba14] # # patch "guitone/src/model/Inventory.cpp" # from [7cfde5f3e235a09824f02c67f34df394985e9b69] # to [772074d5e31ac0818d33e77f06c6c09f681a2179] # # patch "guitone/src/model/Inventory.h" # from [674d9096eb81627fde41a2dd58a98c8a83744216] # to [06b97a117b824cfa76995e6895a711a1d41e6076] # # patch "guitone/src/model/InventoryItem.cpp" # from [4b6ac761fc478cf202bc86740b946768d981593b] # to [355359d12820d5b033152183174b9d738ce601e1] # # patch "guitone/src/model/InventoryItem.h" # from [a8bbfeb525913eddc4a3c73b67cd79b6ab86db64] # to [70cef46c046d56928dbe9adc86b0d9da8a3f282c] # # patch "guitone/src/view/InventoryView.cpp" # from [c399d776f71094302d6a2840ebc7ea34018850ab] # to [7ecee38b487625d1da171e490841d6f466243c6a] # # patch "guitone/src/view/InventoryView.h" # from [2f42802112e608d07157a625250d17b31640fa42] # to [a69bbded6b9e82e8ed33f3dd090fc7c91fa8fc7d] # ============================================================ --- guitone/src/view/InventoryViewDelegate.cpp 0b36236e7307a1e4cf3cd129431bb1df67f8666a +++ guitone/src/view/InventoryViewDelegate.cpp 0b36236e7307a1e4cf3cd129431bb1df67f8666a @@ -0,0 +1,65 @@ +/*************************************************************************** + * Copyright (C) 2007 by Thomas Keller * + * address@hidden * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#include "InventoryViewDelegate.h" + +#include +#include + +InventoryViewDelegate::InventoryViewDelegate(QObject * parent) + : QItemDelegate(parent) +{} + +QWidget * InventoryViewDelegate::createEditor( + QWidget * parent, + const QStyleOptionViewItem & option, + const QModelIndex & index) const +{ + Q_UNUSED(option); + QLineEdit * lineEdit = new QLineEdit(parent); + connect( + lineEdit, SIGNAL(editingFinished()), + this, SLOT(emitCommitData()) + ); + return lineEdit; +} + +void InventoryViewDelegate::setEditorData(QWidget * editor, const QModelIndex & index) const +{ + QLineEdit * lineEdit = qobject_cast(editor); + if (!lineEdit) return; + lineEdit->setText(index.model()->data(index).toString()); +} + +void InventoryViewDelegate::setModelData( + QWidget * editor, + QAbstractItemModel * model, + const QModelIndex & index) const +{ + QLineEdit * lineEdit = qobject_cast(editor); + if (!lineEdit) return; + model->setData(index, lineEdit->text()); +} + +void InventoryViewDelegate::emitCommitData() +{ + emit commitData(qobject_cast(sender())); +} + ============================================================ --- guitone/src/view/InventoryViewDelegate.h 704deafe354280795ca9bfe0e7c42ab82519643f +++ guitone/src/view/InventoryViewDelegate.h 704deafe354280795ca9bfe0e7c42ab82519643f @@ -0,0 +1,42 @@ +/*************************************************************************** + * Copyright (C) 2007 by Thomas Keller * + * address@hidden * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#ifndef INVENTORY_VIEW_DELEGATE_H +#define INVENTORY_VIEW_DELEGATE_H + +#include + +class InventoryViewDelegate : public QItemDelegate +{ + Q_OBJECT + +public: + InventoryViewDelegate(QObject *); + + QWidget * createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const; + void setEditorData(QWidget *, const QModelIndex &) const; + void setModelData(QWidget *, QAbstractItemModel *, const QModelIndex &) const; + + private slots: + void emitCommitData(); +}; + +#endif + ============================================================ --- guitone/guitone.pro 9c37c31facd0210a4d47db3d688b0f70f807c3ce +++ guitone/guitone.pro 159ee4bd8f31bbd72ad80397de9a45054bc76aa5 @@ -17,6 +17,7 @@ HEADERS += src/view/MainWindow.h \ src/view/TreeView.h \ src/view/Splitter.h \ src/view/InventoryView.h \ + src/view/InventoryViewDelegate.h \ src/view/AttributesView.h \ src/view/DiffView.h \ src/view/DiffStatusView.h \ @@ -70,6 +71,7 @@ SOURCES += src/view/MainWindow.cpp \ src/view/TreeView.cpp \ src/view/Splitter.cpp \ src/view/InventoryView.cpp \ + src/view/InventoryViewDelegate.cpp \ src/view/AttributesView.cpp \ src/view/DiffView.cpp \ src/view/DiffStatusView.cpp \ ============================================================ --- guitone/res/forms/main_window.ui 62161c4ccc8ae3874887d716c76ea8f179aa2b4c +++ guitone/res/forms/main_window.ui e4f7de13795e9cb65247203bb4d62da67995ba14 @@ -42,6 +42,9 @@ Qt::Horizontal + + QAbstractItemView::EditKeyPressed + QAbstractItemView::ExtendedSelection @@ -51,6 +54,9 @@ Qt::Vertical + + QAbstractItemView::EditKeyPressed + QAbstractItemView::ExtendedSelection ============================================================ --- guitone/src/model/Inventory.cpp 7cfde5f3e235a09824f02c67f34df394985e9b69 +++ guitone/src/model/Inventory.cpp 772074d5e31ac0818d33e77f06c6c09f681a2179 @@ -232,11 +232,27 @@ QVariant Inventory::data(const QModelInd } } +bool Inventory::setData(const QModelIndex & idx, const QVariant & value, int role) +{ + Q_ASSERT(idx.column() == 0); + if (!idx.isValid()) return false; + + InventoryItem * item = static_cast(idx.internalPointer()); + if (!item || role != Qt::EditRole) return false; + + if (!item->rename(value.toString())) return false; + + emit layoutChanged(); + + return true; +} + Qt::ItemFlags Inventory::flags(const QModelIndex &index) const { if (index.isValid()) { - return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsSelectable | Qt::ItemIsEnabled; + return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | + Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable; } return Qt::ItemIsDropEnabled; @@ -260,8 +276,8 @@ QModelIndex Inventory::parent(const QMod return QModelIndex(); } - InventoryItem *childItem = static_cast(index.internalPointer()); - InventoryItem *parentItem = childItem->parent(); + InventoryItem * childItem = static_cast(index.internalPointer()); + InventoryItem * parentItem = childItem->parent(); if (parentItem == rootItem) { ============================================================ --- guitone/src/model/Inventory.h 674d9096eb81627fde41a2dd58a98c8a83744216 +++ guitone/src/model/Inventory.h 06b97a117b824cfa76995e6895a711a1d41e6076 @@ -44,6 +44,7 @@ class Inventory : public QAbstractItemMo // needed Qt Model methods QVariant data(const QModelIndex&, int) const; + bool setData(const QModelIndex &, const QVariant &, int role = Qt::EditRole); Qt::ItemFlags flags(const QModelIndex&) const; QVariant headerData(int, Qt::Orientation, int) const; QModelIndex index(int, int, const QModelIndex&) const; ============================================================ --- guitone/src/model/InventoryItem.cpp 4b6ac761fc478cf202bc86740b946768d981593b +++ guitone/src/model/InventoryItem.cpp 355359d12820d5b033152183174b9d738ce601e1 @@ -41,7 +41,7 @@ InventoryItem::InventoryItem() status = 0; dirFlag = false; childDirFlag = false; - rootFlag = true; + rootFlag = false; } InventoryItem::InventoryItem(InventoryItem* parent, QString p, int st, bool isDir, bool isRoot) @@ -89,47 +89,12 @@ void InventoryItem::deleteAllChildren(vo children.clear(); } -void InventoryItem::setRenamedFrom(InventoryItem *to_item) -{ - renamed_from = to_item; -} - -InventoryItem* InventoryItem::getRenamedFrom() -{ - return renamed_from; -} - -InventoryItem* InventoryItem::getRenamedTo() -{ - return renamed_to; -} - -void InventoryItem::setRenamedTo(InventoryItem *from_item) -{ - renamed_to = from_item; -} - -void InventoryItem::setParent(InventoryItem *p) -{ - parentItem = p; -} - -InventoryItem* InventoryItem::parent() -{ - return parentItem; -} - void InventoryItem::appendChild(InventoryItem* child) { child->setParent(this); children.append(child); } -InventoryItem* InventoryItem::child(int row) -{ - return children.value(row); -} - void InventoryItem::setChildren(QList items) { // set the current element as parent for each children @@ -158,13 +123,6 @@ int InventoryItem::row() const return 0; } -int InventoryItem::columnCount() const -{ - // there are currently three columns per item - // 1: item name, 2: item status - return 2; -} - QVariant InventoryItem::data(int column, int role) const { if (role == Qt::DisplayRole) @@ -216,6 +174,12 @@ QString InventoryItem::getRelativePath(c return path.right(path.length() - partLen); } +QString InventoryItem::getBaseDirectory() const +{ + int pos = path.lastIndexOf('/'); + return pos == -1 ? QString("") : path.left(pos + 1); +} + bool InventoryItem::hasStatus(int statusBits) const { return (status & statusBits) == statusBits; @@ -302,3 +266,47 @@ QString InventoryItem::getStatusString() return list.join(", "); } +bool InventoryItem::rename(const QString & n) +{ + QString newName(n.trimmed()); + if (newName.size() == 0) return false; + + // the file wasn't renamed + if (newName == getFilename()) return false; + + qDebug("InventoryItem::rename: renaming to %s", qPrintable(newName)); + + // TODO: do the actual rename here + + // check if this item is part of a uncommitted rename action already + if (hasStatus(InventoryItem::RenamedTo)) + { + setPath(getBaseDirectory() + newName); + qDebug("InventoryItem::rename: item already renamed, changing item's name"); + return true; + } + + if (hasStatus(InventoryItem::RenamedFrom)) + { + InventoryItem * renamedTo = getRenamedTo(); + renamedTo->setPath(getBaseDirectory() + newName); + qDebug("InventoryItem::rename: item already renamed, changing target item's name"); + return true; + } + + // apparently its not, lets create a new rename entry + InventoryItem * renamedItem = new InventoryItem(); + parentItem->appendChild(renamedItem); + qDebug("child count: %d", parentItem->childCount()); + renamedItem->setStatus(status | InventoryItem::RenamedTo); + setStatus(status | InventoryItem::RenamedFrom); + + renamedItem->setPath(getBaseDirectory() + newName); + + renamedItem->setRenamedFrom(this); + this->setRenamedTo(renamedItem); + + qDebug("InventoryItem::rename: created new rename item"); + + return true; +} ============================================================ --- guitone/src/model/InventoryItem.h a8bbfeb525913eddc4a3c73b67cd79b6ab86db64 +++ guitone/src/model/InventoryItem.h 70cef46c046d56928dbe9adc86b0d9da8a3f282c @@ -25,70 +25,80 @@ class InventoryItem : public QObject class InventoryItem : public QObject { - - Q_OBJECT - public: - InventoryItem(void); - InventoryItem(InventoryItem*, QString, int, bool, bool isRoot = false); - ~InventoryItem(void); - void deleteAllChildren(void); - void setRenamedFrom(InventoryItem*); - void setRenamedTo(InventoryItem*); - InventoryItem* getRenamedFrom(void); - InventoryItem* getRenamedTo(void); - void setParent(InventoryItem*); + Q_OBJECT - void setChildren(QList); - inline QList getChildren(void) const { return children; }; - inline QString getPath(void) const { return path; }; - QString getRelativePath(const QString&) const; - QString getFilename(void) const; - inline bool isCdUp() const { return getFilename() == ".."; }; - inline bool isTracked() const { return hasNotStatus(Ignored | Unknown); }; - - void setLabel(const QString &); - QString getLabel() const; - - bool hasStatus(int) const; - bool hasNotStatus(int) const; - int getStatusRecursive() const; - bool hasChangedRecursive() const; - QString getStatusString(void) const; - inline bool isDirectory(void) const { return dirFlag; }; - inline bool isRootDirectory(void) const { return rootFlag; }; - inline bool hasChildDirs(void) const { return childDirFlag; }; - inline int getStatus(void){ return status; }; - - /* needed for Qt model class */ - InventoryItem* child(int); - inline int childCount(void) const { return children.count(); }; - int row(void) const; - int columnCount(void) const; - void appendChild(InventoryItem*); - InventoryItem* parent(void); - QVariant data(int, int) const; - - static const int RenamedFrom; - static const int RenamedTo; - static const int Added; - static const int Dropped; - static const int Missing; - static const int Patched; - static const int Unchanged; - static const int Unknown; - static const int Ignored; +public: + InventoryItem(void); + InventoryItem(InventoryItem*, QString, int, bool, bool isRoot = false); + ~InventoryItem(void); + + inline void setRenamedFrom(InventoryItem * from) { renamed_from = from; } + inline void setRenamedTo(InventoryItem * to) { renamed_to = to; } + inline InventoryItem * getRenamedFrom(void) const { return renamed_from; } + inline InventoryItem * getRenamedTo(void) const { return renamed_to; } + + inline void setParent(InventoryItem * p) { parentItem = p; } + inline InventoryItem * parent() const { return parentItem; } + inline QList getChildren(void) const { return children; } + + inline QString getPath(void) const { return path; } + inline void setPath(const QString & p) { path = p; } + + inline bool isCdUp() const { return getFilename() == ".."; } + inline bool isTracked() const { return hasNotStatus(Ignored | Unknown); } + inline bool isDirectory(void) const { return dirFlag; } + inline bool isRootDirectory(void) const { return rootFlag; } + inline bool hasChildDirs(void) const { return childDirFlag; } + inline int getStatus(void) { return status; } + inline void setStatus(int st) { status = st; } + + inline InventoryItem * child(int row) const { return children.value(row); } + inline int childCount(void) const { return children.count(); }; + inline int columnCount(void) const { return 2; } + + void setChildren(QList); + void deleteAllChildren(void); + + QString getRelativePath(const QString&) const; + QString getFilename(void) const; + QString getBaseDirectory(void) const; + + void setLabel(const QString &); + QString getLabel() const; + + bool hasStatus(int) const; + bool hasNotStatus(int) const; + int getStatusRecursive() const; + bool hasChangedRecursive() const; + QString getStatusString(void) const; + + /* needed for Qt model class */ + int row(void) const; + void appendChild(InventoryItem*); + QVariant data(int, int) const; + bool rename(const QString &); + + static const int RenamedFrom; + static const int RenamedTo; + static const int Added; + static const int Dropped; + static const int Missing; + static const int Patched; + static const int Unchanged; + static const int Unknown; + static const int Ignored; - private: - InventoryItem *renamed_from; - InventoryItem *renamed_to; - InventoryItem *parentItem; - QList children; - int status; - bool dirFlag; - bool childDirFlag; - bool rootFlag; - QString path; - QString label; +private: + InventoryItem * renamed_from; + InventoryItem * renamed_to; + InventoryItem * parentItem; + QList children; + int status; + bool dirFlag; + bool childDirFlag; + bool rootFlag; + QString path; + QString label; }; #endif ============================================================ --- guitone/src/view/InventoryView.cpp c399d776f71094302d6a2840ebc7ea34018850ab +++ guitone/src/view/InventoryView.cpp 7ecee38b487625d1da171e490841d6f466243c6a @@ -35,8 +35,9 @@ InventoryView::InventoryView(QWidget* pa #include InventoryView::InventoryView(QWidget* parent) -: TreeView(parent) +: TreeView(parent), invViewDelegate(parent) { + setItemDelegate(&invViewDelegate); setSelectionMode(QAbstractItemView::ExtendedSelection); setDragEnabled(true); setDropIndicatorShown(true); @@ -417,7 +418,9 @@ void InventoryView::slotRename(void) void InventoryView::slotRename(void) { - qDebug("InventoryView::slotRename!!!"); + QModelIndex index(getSingleSelection(false)); + if (!index.isValid()) return; + edit(index); } // @@ -469,9 +472,9 @@ void InventoryView::slotRevisionDiff(voi clearSelection(); } -QModelIndex InventoryView::getSingleSelection() +QModelIndex InventoryView::getSingleSelection(bool mapToSource /* = true */) { - QItemSelectionModel *selectionModel = this->selectionModel(); + QItemSelectionModel * selectionModel = this->selectionModel(); QList list(selectionModel->selectedIndexes()); if (list.size() == 0) @@ -485,9 +488,9 @@ QModelIndex InventoryView::getSingleSele qDebug("InventoryView::getSingleSelection: Multiple items selected, only returning the first."); } - return - static_cast(list[0].model())->mapToSource(list[0]); - + if (!mapToSource) return list[0]; + + return static_cast(list[0].model())->mapToSource(list[0]); } void InventoryView::itemClicked(const QModelIndex & index) ============================================================ --- guitone/src/view/InventoryView.h 2f42802112e608d07157a625250d17b31640fa42 +++ guitone/src/view/InventoryView.h a69bbded6b9e82e8ed33f3dd090fc7c91fa8fc7d @@ -22,6 +22,8 @@ #define INVENTORY_VIEW_H #include "TreeView.h" +#include "InventoryViewDelegate.h" + #include #include @@ -47,7 +49,7 @@ private: void setModel(QAbstractItemModel *); void createAndConnectContextActions(void); void closeEvent(void); - QModelIndex getSingleSelection(); + QModelIndex getSingleSelection(bool mapToSource = true); QAction *actChdir; QAction *actOpen; @@ -63,6 +65,8 @@ private: Type type; + InventoryViewDelegate invViewDelegate; + private slots: void delegateModelReset(); void modelReset();