Initial commit
[keepassx] / src / dialogs / TrashCanDlg.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Tarek Saidi                                     *
3  *   tarek.saidi@arcor.de                                                  *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; version 2 of the License.               *
8
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20   
21 #include <QTreeWidget>
22 #include <QPainter>
23 #include <QPaintEvent>
24 #include <QResizeEvent>
25 #include "main.h"
26 #include "TrashCanDlg.h"
27
28 TrashCanDialog::TrashCanDialog(QWidget* parent,IDatabase* database,const QList<IEntryHandle*>& TrashItems):QDialog(parent){
29         setupUi(this);
30         Entries=TrashItems;
31         for(int i=0;i<Entries.size();i++){
32                 QTreeWidgetItem* item=new QTreeWidgetItem(treeWidget);
33                 item->setData(0,Qt::UserRole,i);
34                 item->setText(0,Entries[i]->group()->title());
35                 item->setText(1,Entries[i]->title());
36                 item->setText(2,Entries[i]->username());
37                 item->setText(3,Entries[i]->expire().dateToString(Qt::LocalDate));
38                 item->setIcon(0,database->icon(Entries[i]->group()->image()));
39                 item->setIcon(1,database->icon(Entries[i]->image()));
40                         
41         }
42         connect(treeWidget,SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),this,SLOT(OnItemDoubleClicked(QTreeWidgetItem*)));
43         connect(treeWidget,SIGNAL(customContextMenuRequested(const QPoint&)),this,SLOT(OnContextMenu(const QPoint&)));
44         ContextMenu=new QMenu(this);
45         ContextMenu->addAction(getIcon("restore"),"Restore");
46         ContextMenu->addAction(getIcon("deleteentry"),"Delete");        
47 }
48
49
50 void TrashCanDialog::paintEvent(QPaintEvent* event){
51         QDialog::paintEvent(event);
52         QPainter painter(this);
53         painter.setClipRegion(event->region());
54         painter.drawPixmap(QPoint(0,0),BannerPixmap);
55 }
56
57 void TrashCanDialog::resizeEvent(QResizeEvent* event){
58         createBanner(&BannerPixmap,getPixmap("trashcan"),tr("Recycle Bin"),width());
59         QDialog::resizeEvent(event);    
60 }
61
62 void TrashCanDialog::OnItemDoubleClicked(QTreeWidgetItem* item){
63         SelectedEntry=Entries[item->data(0,Qt::UserRole).toInt()];      
64         accept();
65 }
66
67 void TrashCanDialog::OnContextMenu(const QPoint& pos){
68         if(treeWidget->itemAt(pos)){
69                 QTreeWidgetItem* item=treeWidget->itemAt(pos);
70                 if(treeWidget->selectedItems().size()==0){
71                         treeWidget->setItemSelected(item,true);
72                 }
73                 else{
74                                 if(!treeWidget->isItemSelected(item)){
75                                         while(treeWidget->selectedItems().size()){
76                                                 treeWidget->setItemSelected(treeWidget->selectedItems()[0],false);
77                                         }
78                                         treeWidget->setItemSelected(item,true);
79                                 }
80                         }
81         }
82         else
83         {
84                 while(treeWidget->selectedItems().size())
85                         treeWidget->setItemSelected(treeWidget->selectedItems()[0],false);
86         }
87         ContextMenu->popup(treeWidget->viewport()->mapToGlobal(pos));
88 }
89
90
91
92 ///TODO 0.2.3 locale aware string/date compare for correct sorting