1/***************************************************************************
2 KAddStringDlg.cpp - description
3 -------------------
4 begin : Sat Oct 16 1999
5 copyright : (C) 1999 by François Dupoux
6 (C) 2004 Emiliano Gulmini <emi_barbarossa@yahoo.it>
7 email : dupoux@dupoux.com
8 ***************************************************************************/
9
10/***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 ***************************************************************************/
18// QT
19#include <qlabel.h>
20#include <qpushbutton.h>
21#include <qradiobutton.h>
22#include <q3listview.h>
23
24
25// KDE
26#include <kmessagebox.h>
27#include <kiconloader.h>
28#include <kdebug.h>
29#include <kconfig.h>
30#include <kapplication.h>
31#include <ktoolinvocation.h>
32
33// local
34#include "kaddstringdlg.h"
35#include "whatthis.h"
36
37using namespace whatthisNameSpace;
38
39KAddStringDlg::KAddStringDlg(RCOptions* info, bool wantEdit, QWidget *parent, const char *name) : KAddStringDlgS(parent,name,true)
40{
41 m_option = info;
42 m_wantEdit = wantEdit;
43 m_currentMap = m_option->m_mapStringsView;
44
45 initGUI();
46
47 connect(m_pbOK, SIGNAL(clicked()), this, SLOT(slotOK()));
48 connect(m_rbSearchOnly, SIGNAL(pressed()), this, SLOT(slotSearchOnly()));
49 connect(m_rbSearchReplace, SIGNAL(pressed()), this, SLOT(slotSearchReplace()));
50 connect(m_pbAdd, SIGNAL(clicked()), this, SLOT(slotAddStringToView()));
51 connect(m_pbDel, SIGNAL(clicked()), this, SLOT(slotDeleteStringFromView()));
52 connect(m_pbHelp, SIGNAL(clicked()), this ,SLOT(slotHelp()));
53
54 whatsThis();
55}
56
57//PRIVATE
58void KAddStringDlg::initGUI()
59{
60 m_pbAdd->setIconSet(SmallIconSet(QString::fromLatin1("go-next")));
61 m_pbDel->setIconSet(SmallIconSet(QString::fromLatin1("go-previous")));
62
63 m_stack->addWidget(m_stringView);
64 m_stack->addWidget(m_stringView_2);
65
66
67 if(m_option->m_searchingOnlyMode)
68 {
69 if(m_wantEdit)
70 m_rbSearchReplace->setEnabled(false);
71 m_rbSearchOnly->setChecked(true);
72 m_edSearch->setEnabled(true);
73 m_edReplace->setEnabled(false);
74 m_tlSearch->setEnabled(true);
75 m_tlReplace->setEnabled(false);
76 }
77 else
78 {
79 if(m_wantEdit)
80 m_rbSearchOnly->setEnabled(false);
81 m_rbSearchReplace->setChecked(true);
82 m_edSearch->setEnabled(true);
83 m_edReplace->setEnabled(true);
84 m_tlSearch->setEnabled(true);
85 m_tlReplace->setEnabled(true);
86 }
87
88 raiseView();
89
90 if(m_wantEdit)
91 loadMapIntoView();
92}
93
94void KAddStringDlg::eraseViewItems()
95{
96 Q3ListViewItem* item = m_sv->firstChild();
97 if(item == 0)
98 return;
99 else
100 {
101 while(item)
102 {
103 Q3ListViewItem* tempItem = item;
104 item = item->nextSibling();
105 delete tempItem;
106 }
107 }
108}
109
110void KAddStringDlg::raiseView()
111{
112 if(m_option->m_searchingOnlyMode)
113 m_sv = m_stringView_2;
114 else
115 m_sv = m_stringView;
116
117 m_stack->raiseWidget(m_sv);
118}
119
120bool KAddStringDlg::columnContains(Q3ListView* lv,const QString& s, int column)
121{
122 Q3ListViewItem* i = lv->firstChild();
123 while (i != 0)
124 {
125 if(i->text(column) == s)
126 return true;
127 i = i->nextSibling();
128 }
129 return false;
130}
131
132void KAddStringDlg::saveViewContentIntoMap()
133{
134 Q3ListViewItem* i = m_sv->firstChild();
135 while(i != 0)
136 {
137 if(m_option->m_searchingOnlyMode)
138 m_currentMap[i->text(0)] = QString();
139 else
140 m_currentMap[i->text(0)] = i->text(1);
141 i = i->nextSibling();
142 }
143}
144
145void KAddStringDlg::loadMapIntoView()
146{
147 KeyValueMap::Iterator itMap;
148
149 for (itMap = m_currentMap.begin(); itMap != m_currentMap.end(); ++itMap)
150 {
151 Q3ListViewItem* temp = new Q3ListViewItem(m_sv);
152 temp->setText(0,itMap.key());
153 if(!m_option->m_searchingOnlyMode)
154 temp->setText(1,itMap.data());
155 }
156}
157
158void KAddStringDlg::whatsThis()
159{
160 m_rbSearchOnly->setWhatsThis( rbSearchOnlyWhatthis);
161 m_rbSearchReplace->setWhatsThis( rbSearchReplaceWhatthis);
162 m_edSearch->setWhatsThis( edSearchWhatthis);
163 m_edReplace->setWhatsThis( edReplaceWhatthis);
164}
165
166//PRIVATE SLOTS
167void KAddStringDlg::slotOK()
168{
169 m_option->m_mapStringsView = m_currentMap;
170
171 accept();
172}
173
174void KAddStringDlg::slotSearchOnly()
175{
176 m_option->m_searchingOnlyMode = true;
177
178 m_rbSearchOnly->setChecked(true);
179 m_edSearch->setEnabled(true);
180 m_edReplace->setEnabled(false);
181 m_tlSearch->setEnabled(true);
182 m_tlReplace->setEnabled(false);
183
184 //sets the right view appearance
185 raiseView();
186 //empties the view content
187 eraseViewItems();
188}
189
190void KAddStringDlg::slotSearchReplace()
191{
192 m_option->m_searchingOnlyMode = false;
193
194 m_rbSearchReplace->setChecked(true);
195 m_edSearch->setEnabled(true);
196 m_edReplace->setEnabled(true);
197 m_tlSearch->setEnabled(true);
198 m_tlReplace->setEnabled(true);
199
200 //sets the right view appearance
201 raiseView();
202 //empties the view content
203 eraseViewItems();
204}
205
206void KAddStringDlg::slotAddStringToView()
207{
208 if(m_option->m_searchingOnlyMode)
209 {
210 QString text = m_edSearch->text();
211 if(!(text.isEmpty() || columnContains(m_sv, text, 0)))
212 {
213 Q3ListViewItem* lvi = new Q3ListViewItem(m_sv);
214 lvi->setMultiLinesEnabled(true);
215 lvi->setText(0,text);
216 m_currentMap[text] = QString();
217 m_edSearch->clear();
218 }
219 }
220 else
221 {
222 QString searchText = m_edSearch->text(),
223 replaceText = m_edReplace->text();
224
225 if(!(searchText.isEmpty() || replaceText.isEmpty() || columnContains(m_sv,searchText,0) || columnContains(m_sv,replaceText,1)))
226 {
227 Q3ListViewItem* lvi = new Q3ListViewItem(m_sv);
228 lvi->setMultiLinesEnabled(true);
229 lvi->setText(0,searchText);
230 m_edSearch->clear();
231 lvi->setText(1,replaceText);
232 m_currentMap[searchText] = replaceText;
233 m_edReplace->clear();
234 }
235 }
236}
237
238void KAddStringDlg::slotDeleteStringFromView()
239{
240 // Choose current item or selected item
241 Q3ListViewItem* currentItem = m_sv->currentItem();
242
243 // Do nothing if list is empty
244 if(currentItem == 0)
245 return;
246
247 m_currentMap.remove(currentItem->text(0));
248
249 if(m_option->m_searchingOnlyMode)
250 {
251 m_edSearch->setText(currentItem->text(0));
252 m_edReplace->clear();
253 //currentItem->setText(1,m_edReplace->text());
254 }
255 else
256 {
257 m_edSearch->setText(currentItem->text(0));
258 m_edReplace->setText(currentItem->text(1));
259 }
260
261 delete currentItem;
262
263 currentItem = 0;
264}
265
266void KAddStringDlg::slotHelp()
267{
268 KToolInvocation::invokeHelp(QString::null, "kfilereplace"); //krazy:exclude=nullstrassign for old broken gcc
269}
270
271#include "kaddstringdlg.moc"
272
273