From dbb6e00a4a90264ab6b20ded348be456af188751 Mon Sep 17 00:00:00 2001 From: sniperbeamer Date: Fri, 20 Feb 2009 20:03:21 +0000 Subject: [PATCH] Slightly reorganized the menubar Added menu action to sort the groups (Bug #2268672) git-svn-id: https://svn.code.sf.net/p/keepassx/code/trunk@268 b624d157-de02-0410-bad0-e51aec6abb33 --- src/forms/MainWindow.ui | 42 ++++++++++++++------ src/lib/GroupView.cpp | 87 +++++++++++++++++++++++++++++++++-------- src/lib/GroupView.h | 11 ++++-- src/mainwindow.cpp | 3 ++ 4 files changed, 110 insertions(+), 33 deletions(-) diff --git a/src/forms/MainWindow.ui b/src/forms/MainWindow.ui index f0eede0..a604aa1 100644 --- a/src/forms/MainWindow.ui +++ b/src/forms/MainWindow.ui @@ -167,14 +167,15 @@ - + - &Edit + &Entries - - - - + + + + + @@ -182,12 +183,6 @@ - - - - - - @@ -240,8 +235,19 @@ + + + &Groups + + + + + + + - + + @@ -593,6 +599,16 @@ Add New Group... + + + Sort groups + + + + + Sort in descending oder + + diff --git a/src/lib/GroupView.cpp b/src/lib/GroupView.cpp index 5b5bc64..d741568 100644 --- a/src/lib/GroupView.cpp +++ b/src/lib/GroupView.cpp @@ -23,6 +23,7 @@ #include "dialogs/EditGroupDlg.h" #include +#include #define INSERT_AREA_WIDTH 4 @@ -36,7 +37,7 @@ KeepassGroupView::KeepassGroupView(QWidget* parent):QTreeWidget(parent){ } -void KeepassGroupView::createItems(){ +void KeepassGroupView::createItems(){ clear(); Items.clear(); InsLinePos=-1; @@ -177,7 +178,7 @@ void KeepassGroupView::OnCurrentGroupChanged(QTreeWidgetItem* cur){ emit groupChanged(((GroupViewItem*)cur)->GroupHandle); } else - emit groupChanged(NULL); + emit groupChanged(NULL); } @@ -187,11 +188,11 @@ void KeepassGroupView::setCurrentGroup(IGroupHandle* group){ for(i=0;iGroupHandle==group){found=true; break;} if(!found)return; - setCurrentItem(Items[i]); + setCurrentItem(Items[i]); } void KeepassGroupView::dragEnterEvent ( QDragEnterEvent * event ){ - LastHoverItem=NULL; + LastHoverItem=NULL; InsLinePos=-1; if(event->mimeData()->hasFormat("application/x-keepassx-group")){ @@ -299,15 +300,15 @@ void KeepassGroupView::dropEvent( QDropEvent * event ){ if(event->pos().y()>ItemRect.y()+2){ qDebug("Insert behind sibling '%s'",((char*)Item->text(0).toUtf8().data())); if(DragItem->parent()){ - DragItem->parent()->takeChild(DragItem->parent()->indexOfChild(DragItem)); + DragItem->parent()->takeChild(DragItem->parent()->indexOfChild(DragItem)); } else{ - takeTopLevelItem(indexOfTopLevelItem(DragItem)); + takeTopLevelItem(indexOfTopLevelItem(DragItem)); } if(Item->parent()){ int index=Item->parent()->indexOfChild(Item)+1; db->moveGroup(DragItem->GroupHandle,((GroupViewItem*)Item->parent())->GroupHandle,index); - Item->parent()->insertChild(index,DragItem); + Item->parent()->insertChild(index,DragItem); } else{ int index=indexOfTopLevelItem(Item)+1; @@ -319,15 +320,15 @@ void KeepassGroupView::dropEvent( QDropEvent * event ){ else{ qDebug("Insert before sibling '%s'",((char*)Item->text(0).toUtf8().data())); if(DragItem->parent()){ - DragItem->parent()->takeChild(DragItem->parent()->indexOfChild(DragItem)); + DragItem->parent()->takeChild(DragItem->parent()->indexOfChild(DragItem)); } else{ - takeTopLevelItem(indexOfTopLevelItem(DragItem)); + takeTopLevelItem(indexOfTopLevelItem(DragItem)); } if(Item->parent()){ int index=Item->parent()->indexOfChild(Item); db->moveGroup(DragItem->GroupHandle,((GroupViewItem*)Item->parent())->GroupHandle,index); - Item->parent()->insertChild(index,DragItem); + Item->parent()->insertChild(index,DragItem); } else{ int index=indexOfTopLevelItem(Item); @@ -343,7 +344,7 @@ void KeepassGroupView::dropEvent( QDropEvent * event ){ } -void KeepassGroupView::entryDragMoveEvent( QDragMoveEvent * event ){ +void KeepassGroupView::entryDragMoveEvent(QDragMoveEvent* event){ GroupViewItem* Item=(GroupViewItem*)itemAt(event->pos()); if(!Item){ @@ -378,7 +379,7 @@ void KeepassGroupView::entryDragMoveEvent( QDragMoveEvent * event ){ } -void KeepassGroupView::dragMoveEvent( QDragMoveEvent * event ){ +void KeepassGroupView::dragMoveEvent(QDragMoveEvent* event){ if(DragType==EntryDrag){ entryDragMoveEvent(event); return; @@ -435,7 +436,7 @@ void KeepassGroupView::dragMoveEvent( QDragMoveEvent * event ){ InsLinePos=ItemRect.y(); } InsLineStart=ItemRect.x(); - viewport()->update(QRegion(0,InsLinePos-2,viewport()->width(),4)); + viewport()->update(QRegion(0,InsLinePos-2,viewport()->width(),4)); } event->acceptProposedAction(); return; @@ -451,7 +452,7 @@ void KeepassGroupView::paintEvent(QPaintEvent* event){ if(InsLinePos != -1){ QPainter painter(viewport()); painter.setBrush(QBrush(QColor(0,0,0),Qt::Dense4Pattern)); - painter.setPen(Qt::NoPen); + painter.setPen(Qt::NoPen); painter.drawRect(InsLineStart,InsLinePos-2,viewport()->width(),4); } } @@ -494,11 +495,47 @@ void KeepassGroupView::mouseMoveEvent(QMouseEvent *event){ } void KeepassGroupView::OnItemExpanded(QTreeWidgetItem* item){ - dynamic_cast(item)->GroupHandle->setExpanded(true); + static_cast(item)->GroupHandle->setExpanded(true); } void KeepassGroupView::OnItemCollapsed(QTreeWidgetItem* item){ - dynamic_cast(item)->GroupHandle->setExpanded(false); + static_cast(item)->GroupHandle->setExpanded(false); +} + +void KeepassGroupView::OnSort() { + QHash oldIndex; + for (int i=0; iparent()) + oldIndex.insert(Items[i], Items[i]->parent()->indexOfChild(Items[i])); + else + oldIndex.insert(Items[i], invisibleRootItem()->indexOfChild(Items[i])); + } + + sortItems(0, Qt::AscendingOrder); + + bool modified = false; + QMutableHashIterator i(oldIndex); + while (i.hasNext()) { + i.next(); + int newIndex; + IGroupHandle* parent; + if (i.key()->parent()) { + newIndex = i.key()->parent()->indexOfChild(i.key()); + parent = static_cast(i.key()->parent())->GroupHandle; + } + else { + newIndex = invisibleRootItem()->indexOfChild(i.key()); + parent = NULL; + } + + if (newIndex != i.value()) { + db->moveGroup(static_cast(i.key())->GroupHandle, parent, newIndex); + modified = true; + } + } + + if (modified) + emit fileModified(); } @@ -518,3 +555,21 @@ GroupViewItem::GroupViewItem(QTreeWidgetItem *parent):QTreeWidgetItem(parent){ GroupViewItem::GroupViewItem(QTreeWidgetItem *parent, QTreeWidgetItem *preceding):QTreeWidgetItem(parent,preceding){ } +bool GroupViewItem::operator<(const QTreeWidgetItem& other) const { + const GroupViewItem* otherItem = static_cast(&other); + KeepassGroupView* groupView = static_cast(treeWidget()); + + // Search result is always at the bottom + if (this == groupView->SearchResultItem) + return false; + if (otherItem == groupView->SearchResultItem) + return true; + + // Backup group is always at the bottom but above search results + if (!parent() && text(0).compare("Backup", Qt::CaseInsensitive) == 0) + return false; + if (!otherItem->parent() && otherItem->text(0).compare("Backup", Qt::CaseInsensitive) == 0) + return true; + + return QTreeWidgetItem::operator<(other); +} diff --git a/src/lib/GroupView.h b/src/lib/GroupView.h index dc0a658..10827fd 100644 --- a/src/lib/GroupView.h +++ b/src/lib/GroupView.h @@ -17,6 +17,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifndef _GROUP_VIEW_H_ #define _GROUP_VIEW_H_ @@ -44,13 +45,13 @@ class KeepassGroupView:public QTreeWidget{ virtual void dragEnterEvent(QDragEnterEvent* event); virtual void dragMoveEvent(QDragMoveEvent* event); void entryDragMoveEvent(QDragMoveEvent* event); - virtual void dragLeaveEvent ( QDragLeaveEvent * event ); - virtual void dropEvent ( QDropEvent * event ); + virtual void dragLeaveEvent(QDragLeaveEvent* event); + virtual void dropEvent(QDropEvent* event); void entryDropEvent(QDropEvent* event); virtual void mousePressEvent(QMouseEvent *event); virtual void mouseMoveEvent(QMouseEvent *event); - virtual void paintEvent ( QPaintEvent * event ); - virtual void contextMenuEvent(QContextMenuEvent *event); + virtual void paintEvent (QPaintEvent* event); + virtual void contextMenuEvent(QContextMenuEvent* event); void addChildren(GroupViewItem* item); QPoint DragStartPos; GroupViewItem* DragItem; @@ -71,6 +72,7 @@ class KeepassGroupView:public QTreeWidget{ void OnHideSearchResults(); void OnItemExpanded(QTreeWidgetItem*); void OnItemCollapsed(QTreeWidgetItem*); + void OnSort(); signals: void groupChanged(IGroupHandle* NewGroup); @@ -87,6 +89,7 @@ class GroupViewItem:public QTreeWidgetItem{ GroupViewItem(QTreeWidget *parent, QTreeWidgetItem * preceding); GroupViewItem(QTreeWidgetItem *parent); GroupViewItem(QTreeWidgetItem *parent, QTreeWidgetItem * preceding); + bool operator<(const QTreeWidgetItem& other) const; IGroupHandle* GroupHandle; }; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 0f31538..9671357 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -152,6 +152,7 @@ void KeepassMainWindow::setupConnections(){ connect(EditNewSubgroupAction, SIGNAL(triggered()), GroupView, SLOT(OnNewSubgroup())); connect(EditEditGroupAction, SIGNAL(triggered()), GroupView, SLOT(OnEditGroup())); connect(EditDeleteGroupAction, SIGNAL(triggered()), GroupView, SLOT(OnDeleteGroup())); + connect(EditGroupSortAction, SIGNAL(triggered()), GroupView, SLOT(OnSort())); connect(EditNewEntryAction, SIGNAL(triggered()), EntryView, SLOT(OnNewEntry())); connect(EditEditEntryAction, SIGNAL(triggered()), EntryView, SLOT(OnEditEntry())); connect(EntryView, SIGNAL(requestCreateGroup(QString,quint32,GroupViewItem*)), GroupView, SLOT(createGroup(QString,quint32,GroupViewItem*))); @@ -243,6 +244,7 @@ void KeepassMainWindow::setupIcons(){ EditNewEntryAction->setIcon(getIcon("newentry")); EditEditEntryAction->setIcon(getIcon("editentry")); EditDeleteEntryAction->setIcon(getIcon("deleteentry")); + EditGroupSortAction->setIcon(getIcon("swap")); EditUsernameToClipboardAction->setIcon(getIcon("copyusername")); EditPasswordToClipboardAction->setIcon(getIcon("copypwd")); EditCloneEntryAction->setIcon(getIcon("cloneentry")); @@ -278,6 +280,7 @@ void KeepassMainWindow::setupMenus(){ GroupView->ContextMenu->addAction(EditNewSubgroupAction); GroupView->ContextMenu->addAction(EditEditGroupAction); GroupView->ContextMenu->addAction(EditDeleteGroupAction); + GroupView->ContextMenu->addAction(EditGroupSortAction); GroupView->ContextMenu->addSeparator(); GroupView->ContextMenu->addAction(EditNewEntryAction); GroupView->ContextMenu->addSeparator();