Initial commit
[keepassx] / src / dialogs / CustomizeDetailViewDlg.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-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 <QColorDialog>
22 #include "CustomizeDetailViewDlg.h"
23
24 bool DisableButtonSlots=false;
25
26 CustomizeDetailViewDialog::CustomizeDetailViewDialog(QWidget* parent):QDialog(parent){
27         setupUi(this);
28         BtnBold->setIcon(getIcon("text_bold"));
29         BtnItalic->setIcon(getIcon("text_italic"));
30         BtnUnderline->setIcon(getIcon("text_under"));
31         BtnAlignLeft->setIcon(getIcon("text_left"));
32         BtnAlignRight->setIcon(getIcon("text_right"));
33         BtnAlignCenter->setIcon(getIcon("text_center"));
34         BtnAlignBlock->setIcon(getIcon("text_block"));
35         BtnTemplates->setIcon(getIcon("templates"));    
36         
37         QMenu* tmplmenu=new QMenu();
38         tmplmenu->addAction(tr("Group"))->setData("%group%");
39         tmplmenu->addAction(tr("Title"))->setData("%title%");
40         tmplmenu->addAction(tr("Username"))->setData("%username%");
41         tmplmenu->addAction(tr("Password"))->setData("%password%");
42         tmplmenu->addAction(tr("Url"))->setData("%url%");
43         tmplmenu->addAction(tr("Comment"))->setData("%comment%");
44         tmplmenu->addAction(tr("Attachment Name"))->setData("%attachment%");
45         tmplmenu->addAction(tr("Creation Date"))->setData("%creation%");
46         tmplmenu->addAction(tr("Last Access Date"))->setData("%lastaccess%");
47         tmplmenu->addAction(tr("Last Modification Date"))->setData("%lastmod%");
48         tmplmenu->addAction(tr("Expiration Date"))->setData("%expire%");
49         tmplmenu->addAction(tr("Time till Expiration"))->setData("%expire-timeleft%");
50         BtnTemplates->setMenu(tmplmenu);        
51         
52         connect(BtnBold,SIGNAL(toggled(bool)),this,SLOT(OnBtnBold(bool)));
53         connect(BtnItalic,SIGNAL(toggled(bool)),this,SLOT(OnBtnItalic(bool)));
54         connect(BtnUnderline,SIGNAL(toggled(bool)),this,SLOT(OnBtnUnderline(bool)));
55         connect(BtnAlignLeft,SIGNAL(toggled(bool)),this,SLOT(OnBtnAlignLeft()));
56         connect(BtnAlignRight,SIGNAL(toggled(bool)),this,SLOT(OnBtnAlignRight()));
57         connect(BtnAlignCenter,SIGNAL(toggled(bool)),this,SLOT(OnBtnAlignCenter()));
58         connect(BtnAlignBlock,SIGNAL(toggled(bool)),this,SLOT(OnBtnAlignBlock()));
59         connect(BtnColor,SIGNAL(clicked()),this,SLOT(OnBtnColor()));
60         connect(ButtonBox,SIGNAL(accepted()),this,SLOT(OnSave()));
61         connect(ButtonBox,SIGNAL(rejected()),this,SLOT(OnCancel()));
62         connect(ButtonBox,SIGNAL(clicked(QAbstractButton*)),this,SLOT(OnRestoreDefault(QAbstractButton*)));
63         connect(tmplmenu,SIGNAL(triggered(QAction*)),this,SLOT(OnInsertTemplate(QAction*)));
64         connect(RichEdit,SIGNAL(cursorPositionChanged()),this,SLOT(OnCursorPositionChanged()));
65         connect(TabWidget,SIGNAL(currentChanged(int)),this,SLOT(OnTabChanged(int)));
66         connect(FontSize,SIGNAL(activated(const QString&)),this,SLOT(OnFontSizeChanged(const QString&)));
67         connect(FontSize->lineEdit(),SIGNAL(returnPressed()),this,SLOT(OnFontSizeChanged()));
68         
69         RichEdit->setHtml(DetailViewTemplate);
70         
71         OnCursorPositionChanged();
72 }
73
74 void CustomizeDetailViewDialog::OnTabChanged(int index){
75         
76         if(index==0){
77                 RichEdit->setHtml(HtmlEdit->toPlainText());
78         }       
79         if(index==1){
80                 HtmlEdit->setPlainText(RichEdit->toHtml());
81         }       
82 }
83
84 void CustomizeDetailViewDialog::OnFontSizeChanged(const QString& text){
85         bool ok=false;
86         int size=text.toInt(&ok);
87         if(ok && size > 0){
88                 RichEdit->setFontPointSize(size);
89         }
90 }
91
92
93 void CustomizeDetailViewDialog::OnCursorPositionChanged(){
94         DisableButtonSlots=true;
95         if(RichEdit->fontWeight()==QFont::Bold)BtnBold->setChecked(true);
96         else BtnBold->setChecked(false);
97         BtnItalic->setChecked(RichEdit->fontItalic());
98         BtnUnderline->setChecked(RichEdit->fontUnderline());
99         switch(RichEdit->alignment()){
100                 case Qt::AlignLeft:
101                         BtnAlignLeft->setChecked(true);
102                         BtnAlignCenter->setChecked(false);
103                         BtnAlignRight->setChecked(false);
104                         BtnAlignBlock->setChecked(false);
105                         break;
106                 case Qt::AlignHCenter:
107                         BtnAlignLeft->setChecked(false);
108                         BtnAlignCenter->setChecked(true);
109                         BtnAlignRight->setChecked(false);
110                         BtnAlignBlock->setChecked(false);
111                         break;
112                 case Qt::AlignRight:
113                         BtnAlignLeft->setChecked(false);
114                         BtnAlignCenter->setChecked(false);
115                         BtnAlignRight->setChecked(true);
116                         BtnAlignBlock->setChecked(false);
117                         break;
118                 case Qt::AlignJustify:
119                         BtnAlignLeft->setChecked(false);
120                         BtnAlignCenter->setChecked(false);
121                         BtnAlignRight->setChecked(false);
122                         BtnAlignBlock->setChecked(true);
123                         break;                  
124         }
125         CurrentColor=RichEdit->textColor();
126         QPixmap pixmap=QPixmap(16,16);
127         pixmap.fill(CurrentColor);
128         BtnColor->setIcon(QIcon(pixmap));
129         if(RichEdit->fontPointSize()>0)
130                 FontSize->lineEdit()->setText(QString::number((int)RichEdit->fontPointSize()));
131         else
132                 FontSize->lineEdit()->setText("9");
133                                                                  
134         DisableButtonSlots=false;       
135 }
136
137 void CustomizeDetailViewDialog::OnBtnBold(bool toggled){
138         if(DisableButtonSlots)return;
139         if(toggled)
140                         RichEdit->setFontWeight(QFont::Bold);           
141         else
142                         RichEdit->setFontWeight(QFont::Normal);
143 }
144
145 void CustomizeDetailViewDialog::OnBtnItalic(bool toggled){
146         if(DisableButtonSlots)return;
147         RichEdit->setFontItalic(toggled);               
148 }
149
150 void CustomizeDetailViewDialog::OnBtnUnderline(bool toggled){
151         if(DisableButtonSlots)return;   
152         RichEdit->setFontUnderline(toggled);            
153 }
154
155
156 void CustomizeDetailViewDialog::OnBtnAlignLeft(){
157         if(DisableButtonSlots)return;
158         RichEdit->setAlignment(Qt::AlignLeft);
159         OnCursorPositionChanged();
160 }
161
162 void CustomizeDetailViewDialog::OnBtnAlignRight(){
163         if(DisableButtonSlots)return;
164         RichEdit->setAlignment(Qt::AlignRight); 
165         OnCursorPositionChanged();
166 }
167
168 void CustomizeDetailViewDialog::OnBtnAlignCenter(){
169         if(DisableButtonSlots)return;   
170         RichEdit->setAlignment(Qt::AlignHCenter);       
171         OnCursorPositionChanged();
172 }
173
174 void CustomizeDetailViewDialog::OnBtnAlignBlock(){
175         if(DisableButtonSlots)return;   
176         RichEdit->setAlignment(Qt::AlignJustify);       
177         OnCursorPositionChanged();
178 }
179
180 void CustomizeDetailViewDialog::OnBtnColor(){
181         CurrentColor=QColorDialog::getColor(CurrentColor,this);
182         QPixmap pixmap=QPixmap(16,16);
183         pixmap.fill(CurrentColor);
184         BtnColor->setIcon(QIcon(pixmap));
185         RichEdit->setTextColor(CurrentColor);
186 }
187
188 void CustomizeDetailViewDialog::OnInsertTemplate(QAction* action){
189         RichEdit->insertPlainText(action->data().toString());   
190 }
191
192
193 void CustomizeDetailViewDialog::OnSave(){
194         if(TabWidget->currentIndex()==0)
195                 DetailViewTemplate=RichEdit->toHtml();
196         else if(TabWidget->currentIndex()==1)
197                 DetailViewTemplate=HtmlEdit->toPlainText();
198         
199         config->setDetailViewTemplate(DetailViewTemplate);
200         
201         done(1);
202 }
203
204 void CustomizeDetailViewDialog::OnCancel(){
205         done(0);
206 }
207
208 void CustomizeDetailViewDialog::OnRestoreDefault(QAbstractButton* button){
209         if (button==ButtonBox->button(QDialogButtonBox::RestoreDefaults)){
210                 DetailViewTemplate = config->defaultDetailViewTemplate();
211                 HtmlEdit->setPlainText(DetailViewTemplate);
212                 RichEdit->setHtml(DetailViewTemplate);
213         }
214 }