From aeeba6a91619d6254fd5dac645a8f57e8978e397 Mon Sep 17 00:00:00 2001 From: tariq Date: Wed, 25 Jan 2006 09:35:21 +0000 Subject: [PATCH] finished drag and drop implementation for groups git-svn-id: https://svn.code.sf.net/p/keepassx/code/trunk@23 b624d157-de02-0410-bad0-e51aec6abb33 --- src/PwManager.cpp | 29 +++++++++++++++++++++++++++-- src/PwManager.h | 1 + src/lib/GroupView.cpp | 32 ++++++++++++++++++++++++-------- 3 files changed, 52 insertions(+), 10 deletions(-) diff --git a/src/PwManager.cpp b/src/PwManager.cpp index 5e6c117..d600182 100755 --- a/src/PwManager.cpp +++ b/src/PwManager.cpp @@ -997,16 +997,41 @@ else{ int n=0; //Counter for direct (first-level) childs if(n==pos)break; if(Groups[DstIndex+1+i].Level==DstGroup->Level+1)n++; } - DstIndex+=n; + DstIndex+=(n+1); } - for(i=NumSubGroups; i>=0; i--) Groups.insert(DstIndex,tmp[i]); +} + +void PwDatabase::moveGroupDirectly(CGroup* group, CGroup* DstGroup){ +int NumSubGroups=getNumberOfChilds(group); +int GroupIndex=Groups.indexOf(*group); +int DstIndex, LevelDiff, i; +QList tmp; + +if(DstGroup) + LevelDiff=DstGroup->Level - group->Level; +else + LevelDiff=-group->Level; + +for(i=GroupIndex; i<=GroupIndex+NumSubGroups; i++){ + tmp << Groups[i]; + tmp.back().Level+=LevelDiff; +} +for(i=0; i<=NumSubGroups; i++) + Groups.removeAt(GroupIndex); +if(DstGroup) + DstIndex=Groups.indexOf(*DstGroup)+1; +else + DstIndex=0; +for(i=NumSubGroups; i>=0; i--) + Groups.insert(DstIndex,tmp[i]); } + int PwDatabase::getNumberOfChilds(CGroup* group){ if(!group)return Groups.size(); int GroupIndex=Groups.indexOf(*group); diff --git a/src/PwManager.h b/src/PwManager.h index 1137187..5b7fcd6 100755 --- a/src/PwManager.h +++ b/src/PwManager.h @@ -58,6 +58,7 @@ public: void deleteGroup(CGroup* pGroup); void deleteGroup(unsigned long ID); void moveGroup(CGroup* group, CGroup* DstGroup, int pos=-1); + void moveGroupDirectly(CGroup* group, CGroup* DstGroup); //inserts group directly behind DstGroup on the same level int getGroupIndex(CGroup* group); int getGroupIndex(unsigned long ID); int getNumberOfChilds(CGroup* pGroup); diff --git a/src/lib/GroupView.cpp b/src/lib/GroupView.cpp index a24b3aa..13531ff 100644 --- a/src/lib/GroupView.cpp +++ b/src/lib/GroupView.cpp @@ -34,7 +34,7 @@ #include #include "main.h" #include "GroupView.h" - +#define INSERT_AREA_WIDTH 4 KeepassGroupView::KeepassGroupView(QWidget* parent):QTreeWidget(parent){ InsertionMarker=QLine(); @@ -64,7 +64,7 @@ InsertionMarker=QLine(); if(item){ QRect ItemRect=visualItemRect(item); if(!db->isParentGroup(item->pGroup,DragItem->pGroup) && DragItem!=item){ - if((ItemRect.height()+ItemRect.y())-event->pos().y() > 4){ + if((ItemRect.height()+ItemRect.y())-event->pos().y() > INSERT_AREA_WIDTH && event->pos().y() > INSERT_AREA_WIDTH){ QFont f=item->font(0); f.setBold(true); item->setFont(0,f); @@ -73,8 +73,12 @@ if(item){ } else{ LastHoverItem=NULL; - InsertionMarker=QLine(ItemRect.x(),ItemRect.y()+ItemRect.height(), - ItemRect.x()+ItemRect.width(),ItemRect.y()+ItemRect.height()); + if(event->pos().y() > INSERT_AREA_WIDTH) + InsertionMarker=QLine(ItemRect.x(),ItemRect.y()+ItemRect.height(), + ItemRect.x()+ItemRect.width(),ItemRect.y()+ItemRect.height()); + else + InsertionMarker=QLine(ItemRect.x(),0, + ItemRect.x()+ItemRect.width(),0); } } else @@ -109,10 +113,22 @@ if(LastHoverItem){ } GroupViewItem* item=(GroupViewItem*)itemAt(event->pos()); -if(item) - db->moveGroup(DragItem->pGroup,item->pGroup); -else - db->moveGroup(DragItem->pGroup,NULL); +if(item){ + QRect ItemRect=visualItemRect(item); + if((ItemRect.height()+ItemRect.y())-event->pos().y() > INSERT_AREA_WIDTH && event->pos().y() > INSERT_AREA_WIDTH){ + db->moveGroup(DragItem->pGroup,item->pGroup);} + else{ + if(event->pos().y() > INSERT_AREA_WIDTH){ + if(db->getNumberOfChilds(item->pGroup) > 0) + db->moveGroup(DragItem->pGroup,item->pGroup,0); + else + db->moveGroupDirectly(DragItem->pGroup,item->pGroup); + } + else db->moveGroupDirectly(DragItem->pGroup,NULL); + } +} +else db->moveGroup(DragItem->pGroup,NULL); + updateItems(); }