1 /***************************************************************************
2 * Copyright (C) 2005-2006 by Tarek Saidi *
3 * tarek.saidi@arcor.de *
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. *
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. *
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 ***************************************************************************/
20 #include <QFileDialog>
21 #include "dialogs/SelectIconDlg.h"
24 CSelectIconDlg::CSelectIconDlg(IDatabase* database,int CurrentId,QWidget* parent, Qt::WFlags fl):QDialog(parent,fl){
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*)));
41 void CSelectIconDlg::updateView(){
43 for(int i=0; i<db->numIcons(); i++){
44 QListWidgetItem* item = NULL;
46 item = new QListWidgetItem(QIcon(db->icon(i)),QString::number(i));
48 if(!db->icon(i).isNull())
49 item = new QListWidgetItem(QIcon(db->icon(i)),"["+QString::number(i)+"]");
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"));
63 for(int i=0;i<filenames.size();i++){
65 if(!icon.load(filenames[i])){
66 errors+=tr("%1: File could not be loaded.").arg(filenames[i].section("/",-1)).append("\n");
69 dynamic_cast<ICustomIcons*>(db)->addIcon(icon.scaled(16,16,Qt::KeepAspectRatio,Qt::SmoothTransformation));
72 QMessageBox::warning(this,tr("Error"),tr("An error occured while loading the icon(s):").append("\n").append(errors));
74 List->setCurrentItem(List->item(List->count()-1));
77 void CSelectIconDlg::contextMenuEvent(QContextMenuEvent *event){
78 QListWidgetItem* item=List->itemAt(List->mapFromParent(event->pos()));
80 if(item->data(32).toInt()<BUILTIN_ICONS){
81 DeleteAction->setDisabled(true);
82 ReplaceAction->setDisabled(true);}
84 DeleteAction->setDisabled(false);
85 ReplaceAction->setDisabled(false);}
87 CtxMenu->popup(event->globalPos());
90 void CSelectIconDlg::OnDelete(){
91 dynamic_cast<ICustomIcons*>(db)->removeIcon(List->currentItem()->data(32).toInt());
93 List->setCurrentItem(List->item(0));
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;
101 if(!icon.load(filename)){
102 QMessageBox::warning(this,tr("Error"),tr("An error occured while loading the icon."));
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())));
109 void CSelectIconDlg::OnPickIcon(){
110 done(List->currentItem()->data(32).toInt());
113 void CSelectIconDlg::OnCancel(){
117 void CSelectIconDlg::OnSelectionChanged(QListWidgetItem* cur){
118 Button_PickIcon->setEnabled(cur);
121 void CSelectIconDlg::showEvent(QShowEvent *event){
122 if(!event->spontaneous()){
124 List->setCurrentItem(List->item(Id));