Initial commit
[keepassx] / src / dialogs / ManageBookmarksDlg.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  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18  ***************************************************************************/
19
20
21 #include "dialogs/ManageBookmarksDlg.h"
22 #include "dialogs/AddBookmarkDlg.h"
23
24 ManageBookmarksDlg::ManageBookmarksDlg(QWidget* parent):QDialog(parent)
25 {
26         setupUi(this);
27
28         for(int i=0;i<KpxBookmarks::count();i++){
29                 QListWidgetItem* item=new QListWidgetItem(ListWidget);
30                 item->setData(Qt::UserRole,i);
31                 item->setText(KpxBookmarks::title(i));
32         }
33         connect(Button_Add,SIGNAL(clicked()),this,SLOT(OnButtonAdd()));
34         connect(Button_Edit,SIGNAL(clicked()),this,SLOT(OnButtonEdit()));
35         connect(Button_Delete,SIGNAL(clicked()),this,SLOT(OnButtonDelete()));
36         connect(Button_Up,SIGNAL(clicked()),this,SLOT(OnButtonUp()));
37         connect(Button_Down,SIGNAL(clicked()),this,SLOT(OnButtonDown()));
38         connect(ListWidget,SIGNAL(itemDoubleClicked(QListWidgetItem*)),this,SLOT(edit(QListWidgetItem*)));
39         connect(buttonBox->button(QDialogButtonBox::Close),SIGNAL(clicked()),this,SLOT(close()));
40
41         Button_Add->setIcon(getIcon("bookmark_add"));
42         Button_Edit->setIcon(getIcon("bookmark_edit"));
43         Button_Delete->setIcon(getIcon("bookmark_del"));
44         Button_Up->setIcon(getIcon("up"));
45         Button_Down->setIcon(getIcon("down"));
46 }
47
48 void ManageBookmarksDlg::paintEvent(QPaintEvent *event){
49         QDialog::paintEvent(event);
50         QPainter painter(this);
51         painter.setClipRegion(event->region());
52         painter.drawPixmap(QPoint(0,0),BannerPixmap);
53 }
54
55 void ManageBookmarksDlg::resizeEvent(QResizeEvent* event){
56         createBanner(&BannerPixmap,getPixmap("bookmark"),tr("Manage Bookmarks"),width());
57         QDialog::resizeEvent(event);
58 }
59
60 void ManageBookmarksDlg::OnButtonAdd(){
61         AddBookmarkDlg dlg(this);
62         if(dlg.exec()){
63                 int i=dlg.ItemID;
64                 QListWidgetItem* item=new QListWidgetItem(ListWidget);
65                 item->setData(Qt::UserRole,i);
66                 item->setText(KpxBookmarks::title(i));
67         }
68         return;
69 }
70
71 void ManageBookmarksDlg::OnButtonEdit(){
72         QListWidgetItem* item=ListWidget->currentItem();
73         if(!item)return;
74         edit(item);
75 }
76
77 void ManageBookmarksDlg::OnButtonDelete(){
78         QListWidgetItem* item=ListWidget->currentItem();
79         if(!item)return;
80         int index=item->data(Qt::UserRole).toInt();
81         KpxBookmarks::remove(index);
82         delete item;
83         for(int i=0;i<ListWidget->count();i++){
84                 int itemindex=ListWidget->item(i)->data(Qt::UserRole).toInt();
85                 if(itemindex>index)
86                 ListWidget->item(i)->setData(Qt::UserRole,itemindex-1);
87         }
88 }
89
90
91 void ManageBookmarksDlg::OnButtonUp(){
92         int row=ListWidget->currentRow();
93         QListWidgetItem* item=ListWidget->currentItem();
94         if(row==-1 || !item || row==0)return;
95         ListWidget->takeItem(row);
96         row--;
97         ListWidget->insertItem(row,item);
98         ListWidget->setCurrentRow(row);
99 }
100
101 void ManageBookmarksDlg::OnButtonDown(){
102         int row=ListWidget->currentRow();
103         QListWidgetItem* item=ListWidget->currentItem();
104         if(row==-1 || !item || row==ListWidget->count()-1)return;
105         ListWidget->takeItem(row);
106         row++;
107         ListWidget->insertItem(row,item);
108         ListWidget->setCurrentRow(row);
109 }
110
111
112
113 void ManageBookmarksDlg::edit(QListWidgetItem* item){
114         int i=item->data(Qt::UserRole).toInt();
115         AddBookmarkDlg dlg(this,QString(),i);
116         dlg.exec();
117         item->setText(KpxBookmarks::title(i));
118 }
119
120
121
122 void ManageBookmarksDlg::closeEvent(QCloseEvent * event){
123         QList<int> Order;
124         // Creating a list with the new indices
125         // Order[OldIndex]==NewIndex
126         for(int i=0;i<KpxBookmarks::count();i++){
127                 Order<<ListWidget->item(i)->data(Qt::UserRole).toInt();
128         }
129         KpxBookmarks::resort(Order);
130         event->accept();
131 }