1 | // |
2 | // Copyright (C) 1998 Matthias Hoelzer <hoelzer@kde.org> |
3 | // Copyright (C) 2002-2005 David Faure <faure@kde.org> |
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; either version 2 of the License, or |
8 | // (at your option) any later version. |
9 | // |
10 | // This program is distributed in the hope that it will be useful, |
11 | // but WITHOUT ANY WARRANTY; without even the7 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 Free Software |
17 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
18 | // |
19 | |
20 | |
21 | // Own |
22 | #include "widgets.h" |
23 | #include <kdatepicker.h> |
24 | |
25 | // Qt |
26 | #include <QtCore/QFile> |
27 | #include <QDesktopWidget> |
28 | #include <QtCore/QTextStream> |
29 | #include <QTextCursor> |
30 | #include <QLabel> |
31 | |
32 | // KDE |
33 | #include <klocale.h> |
34 | #include <kmessagebox.h> |
35 | #include <kinputdialog.h> |
36 | #include <kpassworddialog.h> |
37 | #include <kcombobox.h> |
38 | #include <kdebug.h> |
39 | #include <kapplication.h> |
40 | #include <kcmdlineargs.h> |
41 | #include <ktextedit.h> |
42 | #include <kvbox.h> |
43 | |
44 | // Local |
45 | #include "klistboxdialog.h" |
46 | #include "progressdialog.h" |
47 | |
48 | |
49 | #if defined Q_WS_X11 && ! defined K_WS_QTONLY |
50 | #include <netwm.h> |
51 | #endif |
52 | |
53 | void Widgets::handleXGeometry(QWidget * dlg) |
54 | { |
55 | #ifdef Q_WS_X11 |
56 | QString geometry; |
57 | KCmdLineArgs *args = KCmdLineArgs::parsedArgs("kde" ); |
58 | if (args && args->isSet("geometry" )) |
59 | geometry = args->getOption("geometry" ); |
60 | if ( ! geometry.isEmpty()) { |
61 | int x, y; |
62 | int w, h; |
63 | int m = XParseGeometry( geometry.toLatin1(), &x, &y, (unsigned int*)&w, (unsigned int*)&h); |
64 | if ( (m & XNegative) ) |
65 | x = KApplication::desktop()->width() + x - w; |
66 | if ( (m & YNegative) ) |
67 | y = KApplication::desktop()->height() + y - h; |
68 | dlg->setGeometry(x, y, w, h); |
69 | // kDebug() << "x: " << x << " y: " << y << " w: " << w << " h: " << h; |
70 | } |
71 | #endif |
72 | } |
73 | |
74 | bool Widgets::inputBox(QWidget *parent, const QString& title, const QString& text, const QString& init, QString &result) |
75 | { |
76 | bool ok; |
77 | const QString str = KInputDialog::getText( title, text, init, &ok, parent, 0, 0, QString() ); |
78 | if ( ok ) |
79 | result = str; |
80 | return ok; |
81 | } |
82 | |
83 | bool Widgets::passwordBox(QWidget *parent, const QString& title, const QString& text, QString &result) |
84 | { |
85 | KPasswordDialog dlg( parent ); |
86 | kapp->setTopWidget( &dlg ); |
87 | dlg.setCaption(title); |
88 | dlg.setPrompt(text); |
89 | |
90 | handleXGeometry(&dlg); |
91 | |
92 | bool retcode = (dlg.exec() == QDialog::Accepted); |
93 | if ( retcode ) |
94 | result = dlg.password(); |
95 | return retcode; |
96 | } |
97 | |
98 | int Widgets::textBox(QWidget *parent, int width, int height, const QString& title, const QString& file) |
99 | { |
100 | // KTextBox dlg(parent, 0, true, width, height, file); |
101 | KDialog dlg( parent ); |
102 | dlg.setCaption( title ); |
103 | dlg.setButtons( KDialog::Ok ); |
104 | dlg.setModal( true ); |
105 | |
106 | kapp->setTopWidget( &dlg ); |
107 | KVBox* vbox = new KVBox(&dlg); |
108 | dlg.setMainWidget(vbox); |
109 | |
110 | KTextEdit *edit = new KTextEdit( vbox ); |
111 | edit->setReadOnly(true); |
112 | edit->setFocus(); |
113 | |
114 | QFile f(file); |
115 | if (!f.open(QIODevice::ReadOnly)) |
116 | { |
117 | kError() << i18n("kdialog: could not open file %1" , file) << endl; |
118 | return -1; |
119 | } |
120 | QTextStream s(&f); |
121 | |
122 | while (!s.atEnd()) |
123 | edit->append(s.readLine()); |
124 | |
125 | edit->setTextCursor(QTextCursor(edit->document())); |
126 | |
127 | f.close(); |
128 | |
129 | if ( width > 0 && height > 0 ) |
130 | dlg.setInitialSize( QSize( width, height ) ); |
131 | |
132 | handleXGeometry(&dlg); |
133 | dlg.setCaption(title); |
134 | return (dlg.exec() == KDialog::Accepted) ? 0 : 1; |
135 | } |
136 | |
137 | int Widgets::textInputBox(QWidget *parent, int width, int height, const QString& title, const QString& text, const QString& init, QString &result) |
138 | { |
139 | // KTextBox dlg(parent, 0, true, width, height, file); |
140 | KDialog dlg( parent ); |
141 | dlg.setCaption( title ); |
142 | dlg.setButtons( KDialog::Ok ); |
143 | dlg.setModal( true ); |
144 | |
145 | kapp->setTopWidget( &dlg ); |
146 | KVBox* vbox = new KVBox(&dlg); |
147 | |
148 | dlg.setMainWidget(vbox); |
149 | |
150 | if( !text.isEmpty() ) |
151 | { |
152 | QLabel *label = new QLabel(vbox); |
153 | label->setText(text); |
154 | } |
155 | |
156 | KTextEdit *edit = new KTextEdit( vbox ); |
157 | edit->setReadOnly(false); |
158 | edit->setFocus(); |
159 | |
160 | edit->insertPlainText( init ); |
161 | |
162 | if ( width > 0 && height > 0 ) |
163 | dlg.setInitialSize( QSize( width, height ) ); |
164 | |
165 | handleXGeometry(&dlg); |
166 | dlg.setCaption(title); |
167 | const int returnDialogCode = dlg.exec(); |
168 | result = edit->toPlainText(); |
169 | return (returnDialogCode == KDialog::Accepted ? 0 : 1); |
170 | } |
171 | |
172 | bool Widgets::comboBox(QWidget *parent, const QString& title, const QString& text, const QStringList& args, |
173 | const QString& defaultEntry, QString &result) |
174 | { |
175 | KDialog dlg( parent ); |
176 | kapp->setTopWidget( &dlg ); |
177 | dlg.setCaption( title ); |
178 | dlg.setButtons( KDialog::Ok|KDialog::Cancel ); |
179 | dlg.setModal( true ); |
180 | dlg.setDefaultButton( KDialog::Ok ); |
181 | |
182 | KVBox* vbox = new KVBox( &dlg ); |
183 | dlg.setMainWidget( vbox ); |
184 | |
185 | QLabel label (vbox); |
186 | label.setText (text); |
187 | KComboBox combo (vbox); |
188 | combo.insertItems (0, args); |
189 | combo.setCurrentIndex( combo.findText( defaultEntry ) ); |
190 | combo.setFocus(); |
191 | |
192 | handleXGeometry(&dlg); |
193 | |
194 | bool retcode = (dlg.exec() == QDialog::Accepted); |
195 | |
196 | if (retcode) |
197 | result = combo.currentText(); |
198 | |
199 | return retcode; |
200 | } |
201 | |
202 | bool Widgets::listBox(QWidget *parent, const QString& title, const QString& text, const QStringList& args, |
203 | const QString& defaultEntry, QString &result) |
204 | { |
205 | KListBoxDialog box(text,parent); |
206 | |
207 | kapp->setTopWidget( &box ); |
208 | box.setCaption(title); |
209 | |
210 | for (int i = 0; i+1<args.count(); i += 2) { |
211 | box.insertItem(args[i+1]); |
212 | } |
213 | box.setCurrentItem( defaultEntry ); |
214 | |
215 | handleXGeometry(&box); |
216 | |
217 | const bool retcode = (box.exec() == QDialog::Accepted); |
218 | if ( retcode ) |
219 | result = args[ box.currentItem()*2 ]; |
220 | return retcode; |
221 | } |
222 | |
223 | |
224 | bool Widgets::checkList(QWidget *parent, const QString& title, const QString& text, const QStringList& args, bool separateOutput, QStringList &result) |
225 | { |
226 | QStringList entries, tags; |
227 | QString rs; |
228 | |
229 | result.clear(); |
230 | |
231 | KListBoxDialog box(text,parent); |
232 | |
233 | QListWidget &table = box.getTable(); |
234 | |
235 | kapp->setTopWidget( &box ); |
236 | box.setCaption(title); |
237 | |
238 | for (int i=0; i+2<args.count(); i += 3) { |
239 | tags.append(args[i]); |
240 | entries.append(args[i+1]); |
241 | } |
242 | |
243 | table.addItems(entries); |
244 | table.setSelectionMode(QListWidget::MultiSelection); |
245 | table.setCurrentItem(0); // This is to circumvent a Qt bug |
246 | |
247 | for (int i=0; i+2<args.count(); i += 3) { |
248 | table.item( i/3 )->setSelected( args[i+2] == QLatin1String("on" ) ); |
249 | } |
250 | |
251 | handleXGeometry(&box); |
252 | |
253 | const bool retcode = (box.exec() == QDialog::Accepted); |
254 | |
255 | if ( retcode ) { |
256 | if (separateOutput) { |
257 | for (int i=0; i<table.count(); i++) |
258 | if (table.item(i)->isSelected()) |
259 | result.append(tags[i]); |
260 | } else { |
261 | for (int i=0; i<table.count(); i++) |
262 | if (table.item(i)->isSelected()) |
263 | rs += QLatin1String("\"" ) + tags[i] + QLatin1String("\" " ); |
264 | result.append(rs); |
265 | } |
266 | } |
267 | return retcode; |
268 | } |
269 | |
270 | |
271 | bool Widgets::radioBox(QWidget *parent, const QString& title, const QString& text, const QStringList& args, QString &result) |
272 | { |
273 | QStringList entries, tags; |
274 | |
275 | KListBoxDialog box(text,parent); |
276 | |
277 | QListWidget &table = box.getTable(); |
278 | |
279 | kapp->setTopWidget( &box ); |
280 | box.setCaption(title); |
281 | |
282 | for (int i=0; i+2<args.count(); i += 3) { |
283 | tags.append(args[i]); |
284 | entries.append(args[i+1]); |
285 | } |
286 | |
287 | table.addItems(entries); |
288 | |
289 | for (int i=0; i+2<args.count(); i += 3) { |
290 | if (args[i+2] == QLatin1String("on" )) { |
291 | table.setCurrentRow(i/3); |
292 | } |
293 | } |
294 | |
295 | handleXGeometry(&box); |
296 | |
297 | const bool retcode = (box.exec() == QDialog::Accepted); |
298 | if ( retcode ) |
299 | result = tags[ table.currentRow() ]; |
300 | return retcode; |
301 | } |
302 | |
303 | bool Widgets::progressBar(QWidget *parent, const QString& title, const QString& text, int totalSteps) |
304 | { |
305 | ProgressDialog dlg( parent, title, text, totalSteps ); |
306 | kapp->setTopWidget( &dlg ); |
307 | dlg.setCaption( title ); |
308 | handleXGeometry(&dlg); |
309 | dlg.exec(); |
310 | return dlg.wasCancelled(); |
311 | } |
312 | |
313 | |
314 | bool Widgets::slider( QWidget *parent, const QString& title, const QString& text, int minValue, int maxValue, int step, int &result ) |
315 | { |
316 | KDialog dlg( parent ); |
317 | kapp->setTopWidget( &dlg ); |
318 | dlg.setCaption( title ); |
319 | dlg.setButtons( KDialog::Ok|KDialog::Cancel ); |
320 | dlg.setModal( true ); |
321 | dlg.setDefaultButton( KDialog::Ok ); |
322 | |
323 | KVBox* vbox = new KVBox( &dlg ); |
324 | dlg.setMainWidget( vbox ); |
325 | |
326 | QLabel label (vbox); |
327 | label.setText (text); |
328 | QSlider slider (vbox); |
329 | slider.setMinimum( minValue ); |
330 | slider.setMaximum( maxValue ); |
331 | slider.setSingleStep( step ); |
332 | slider.setTickPosition ( QSlider::TicksAbove ); |
333 | slider.setOrientation( Qt::Horizontal ); |
334 | slider.setFocus(); |
335 | handleXGeometry(&dlg); |
336 | |
337 | const bool retcode = (dlg.exec() == QDialog::Accepted); |
338 | |
339 | if (retcode) |
340 | result = slider.value(); |
341 | |
342 | return retcode; |
343 | } |
344 | |
345 | bool Widgets::calendar( QWidget *parent, const QString &title, const QString &text, QDate & result ) |
346 | { |
347 | KDialog dlg( parent ); |
348 | kapp->setTopWidget( &dlg ); |
349 | dlg.setCaption( title ); |
350 | dlg.setButtons( KDialog::Ok|KDialog::Cancel ); |
351 | dlg.setModal( true ); |
352 | dlg.setDefaultButton( KDialog::Ok ); |
353 | |
354 | KVBox* vbox = new KVBox( &dlg ); |
355 | dlg.setMainWidget( vbox ); |
356 | |
357 | QLabel label (vbox); |
358 | label.setText (text); |
359 | KDatePicker dateWidget( vbox ); |
360 | dateWidget.setFocus(); |
361 | handleXGeometry(&dlg); |
362 | |
363 | const bool retcode = (dlg.exec() == QDialog::Accepted); |
364 | |
365 | if (retcode) |
366 | result = dateWidget.date(); |
367 | |
368 | return retcode; |
369 | } |
370 | |
371 | QString Widgets::parseString(const QString &str) |
372 | { |
373 | QString ret; |
374 | ret.reserve(str.size()); |
375 | bool escaped = false; |
376 | for (int i = 0; i < str.size(); i++) { |
377 | QChar c = str.at(i); |
378 | if (escaped) { |
379 | escaped = false; |
380 | if (c == '\\') { |
381 | ret += c; |
382 | } else if (c == 'n') { |
383 | ret += '\n'; |
384 | } else { |
385 | kWarning() << qPrintable(QString::fromLatin1("Unrecognized escape sequence \\%1" ).arg(c)); |
386 | ret += '\\'; |
387 | ret += c; |
388 | } |
389 | } else { |
390 | if (c == '\\') { |
391 | escaped = true; |
392 | } else { |
393 | ret += c; |
394 | } |
395 | } |
396 | } |
397 | if (escaped) { |
398 | kWarning() << "Unterminated escape sequence" ; |
399 | ret += '\\'; |
400 | } |
401 | return ret; |
402 | } |
403 | |