Initial commit
[keepassx] / src / dialogs / SelectIconDlg.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2006 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 #include <QFileDialog>
21 #include "dialogs/SelectIconDlg.h"
22
23
24 CSelectIconDlg::CSelectIconDlg(IDatabase* database,int CurrentId,QWidget* parent, Qt::WFlags fl):QDialog(parent,fl){
25         setupUi(this);
26         db=database;
27         Id=CurrentId;
28         CtxMenu=new QMenu(this);
29         ReplaceAction=CtxMenu->addAction(getIcon("swap"),tr("Replace..."));
30         DeleteAction=CtxMenu->addAction(getIcon("delete"),tr("Delete"));
31         QPushButton* Button_AddIcon = ButtonBox->addButton(tr("Add Custom Icon"), QDialogButtonBox::ActionRole);
32         Button_PickIcon = ButtonBox->addButton(tr("Pick"), QDialogButtonBox::AcceptRole);
33         connect(Button_AddIcon, SIGNAL(clicked()), this, SLOT(OnAddIcon()));
34         connect(Button_PickIcon, SIGNAL(clicked()), this, SLOT(OnPickIcon()));
35         connect(ButtonBox, SIGNAL(rejected()), this, SLOT(OnCancel()));
36         connect(DeleteAction,SIGNAL(triggered()),this,SLOT(OnDelete()));
37         connect(ReplaceAction,SIGNAL(triggered()),this,SLOT(OnReplace()));
38         connect(List,SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),this,SLOT(OnSelectionChanged(QListWidgetItem*)));
39 }
40
41 void CSelectIconDlg::updateView(){
42         List->clear();
43         for(int i=0; i<db->numIcons(); i++){
44                 QListWidgetItem* item = NULL;
45                 if(i<BUILTIN_ICONS)
46                         item = new QListWidgetItem(QIcon(db->icon(i)),QString::number(i));
47                 else
48                         if(!db->icon(i).isNull())
49                                 item = new QListWidgetItem(QIcon(db->icon(i)),"["+QString::number(i)+"]");
50                 
51                 if (item) {
52                         item->setData(32,i);
53                         List->addItem(item);
54                 }
55         }
56 }
57
58
59 void CSelectIconDlg::OnAddIcon(){
60         QStringList filenames=QFileDialog::getOpenFileNames(this,tr("Add Icons..."),QDir::homePath(),tr("Images (%1)")
61                                                                                                                 .arg("*.png *.jpeg *.jpg *.bmp *.gif *.bpm *.pgm *.ppm *.xbm *xpm"));
62         QString errors;
63         for(int i=0;i<filenames.size();i++){
64                 QPixmap icon;
65                 if(!icon.load(filenames[i])){
66                         errors+=tr("%1: File could not be loaded.").arg(filenames[i].section("/",-1)).append("\n");
67                         continue;
68                 }
69                 dynamic_cast<ICustomIcons*>(db)->addIcon(icon.scaled(16,16,Qt::KeepAspectRatio,Qt::SmoothTransformation));
70         }
71         if(errors.size())
72                 QMessageBox::warning(this,tr("Error"),tr("An error occured while loading the icon(s):").append("\n").append(errors));
73         updateView();
74         List->setCurrentItem(List->item(List->count()-1));
75 }
76
77 void CSelectIconDlg::contextMenuEvent(QContextMenuEvent *event){        
78         QListWidgetItem* item=List->itemAt(List->mapFromParent(event->pos()));  
79         if(!item)return;
80         if(item->data(32).toInt()<BUILTIN_ICONS){
81                 DeleteAction->setDisabled(true);
82                 ReplaceAction->setDisabled(true);}
83         else{
84                 DeleteAction->setDisabled(false);
85                 ReplaceAction->setDisabled(false);}     
86         event->accept();
87         CtxMenu->popup(event->globalPos());
88 }
89
90 void CSelectIconDlg::OnDelete(){
91         dynamic_cast<ICustomIcons*>(db)->removeIcon(List->currentItem()->data(32).toInt());
92         updateView();
93         List->setCurrentItem(List->item(0));
94 }
95
96 void CSelectIconDlg::OnReplace(){
97         QString filename=QFileDialog::getOpenFileName(this,tr("Add Icons..."),QDir::homePath(),tr("Images (%1)")
98                 .arg("*.png *.jpeg *.jpg *.bmp *.gif *.bpm *.pgm *.ppm *.xbm *xpm"));
99         if(filename.isEmpty())return;
100         QPixmap icon;
101         if(!icon.load(filename)){
102                 QMessageBox::warning(this,tr("Error"),tr("An error occured while loading the icon."));
103                 return;
104         }
105         dynamic_cast<ICustomIcons*>(db)->replaceIcon(List->currentItem()->data(32).toInt(),icon.scaled(16,16,Qt::KeepAspectRatio,Qt::SmoothTransformation));
106         List->currentItem()->setIcon(QIcon(db->icon(List->currentItem()->data(32).toInt())));
107 }
108
109 void CSelectIconDlg::OnPickIcon(){
110         done(List->currentItem()->data(32).toInt());
111 }
112
113 void CSelectIconDlg::OnCancel(){
114         done(-1);
115 }
116
117 void CSelectIconDlg::OnSelectionChanged(QListWidgetItem* cur){
118         Button_PickIcon->setEnabled(cur);
119 }
120
121 void CSelectIconDlg::showEvent(QShowEvent *event){
122         if(!event->spontaneous()){
123                 updateView();
124                 List->setCurrentItem(List->item(Id));
125         }
126 }