add more spacing
[personal-kdebase.git] / apps / kdialog / widgets.cpp
blob3ad490197d2f22dc74d9faacc28decd30c51e174
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.
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.
21 // Own
22 #include "widgets.h"
23 #include <kdatepicker.h>
25 // Qt
26 #include <QtCore/QFile>
27 #include <QtGui/QDesktopWidget>
28 #include <QtCore/QTextStream>
29 #include <QtGui/QTextCursor>
30 #include <QtGui/QLabel>
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>
44 // Local
45 #include "klistboxdialog.h"
46 #include "progressdialog.h"
49 #if defined Q_WS_X11 && ! defined K_WS_QTONLY
50 #include <netwm.h>
51 #endif
53 void Widgets::handleXGeometry(QWidget * dlg)
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;
71 #endif
74 bool Widgets::inputBox(QWidget *parent, const QString& title, const QString& text, const QString& init, QString &result)
76 bool ok;
77 QString str = KInputDialog::getText( title, text, init, &ok, parent, 0, 0, QString() );
78 if ( ok )
79 result = str;
80 return ok;
83 bool Widgets::passwordBox(QWidget *parent, const QString& title, const QString& text, QString &result)
85 KPasswordDialog dlg( parent );
86 kapp->setTopWidget( &dlg );
87 dlg.setCaption(title);
88 dlg.setPrompt(text);
90 handleXGeometry(&dlg);
92 bool retcode = (dlg.exec() == QDialog::Accepted);
93 if ( retcode )
94 result = dlg.password();
95 return retcode;
98 int Widgets::textBox(QWidget *parent, int width, int height, const QString& title, const QString& file)
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 );
106 kapp->setTopWidget( &dlg );
107 KVBox* vbox = new KVBox(&dlg);
108 dlg.setMainWidget(vbox);
110 KTextEdit *edit = new KTextEdit( vbox );
111 edit->setReadOnly(true);
113 QFile f(file);
114 if (!f.open(QIODevice::ReadOnly))
116 kError() << i18n("kdialog: could not open file %1", file) << endl;
117 return -1;
119 QTextStream s(&f);
121 while (!s.atEnd())
122 edit->append(s.readLine());
124 edit->setTextCursor(QTextCursor(edit->document()));
126 f.close();
128 if ( width > 0 && height > 0 )
129 dlg.setInitialSize( QSize( width, height ) );
131 handleXGeometry(&dlg);
132 dlg.setCaption(title);
133 return (dlg.exec() == KDialog::Accepted) ? 0 : 1;
136 int Widgets::textInputBox(QWidget *parent, int width, int height, const QString& title, const QStringList& args, QString &result)
138 // KTextBox dlg(parent, 0, true, width, height, file);
139 KDialog dlg( parent );
140 dlg.setCaption( title );
141 dlg.setButtons( KDialog::Ok );
142 dlg.setModal( true );
144 kapp->setTopWidget( &dlg );
145 KVBox* vbox = new KVBox(&dlg);
147 dlg.setMainWidget(vbox);
149 if( args.count() > 0 )
151 QLabel *label = new QLabel(vbox);
152 label->setText(args[0]);
155 KTextEdit *edit = new KTextEdit( vbox );
156 edit->setReadOnly(false);
157 edit->setFocus();
159 if( args.count() > 1 )
160 edit->insertPlainText( args[1] );
162 if ( width > 0 && height > 0 )
163 dlg.setInitialSize( QSize( width, height ) );
165 handleXGeometry(&dlg);
166 dlg.setCaption(title);
167 dlg.exec();
168 result = edit->toPlainText().toLocal8Bit();
169 return 0;
172 bool Widgets::comboBox(QWidget *parent, const QString& title, const QString& text, const QStringList& args,
173 const QString& defaultEntry, QString &result)
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 );
182 KVBox* vbox = new KVBox( &dlg );
183 dlg.setMainWidget( vbox );
185 QLabel label (vbox);
186 label.setText (text);
187 KComboBox combo (vbox);
188 combo.insertItems (0, args);
189 combo.setCurrentIndex( combo.findText( defaultEntry ) );
191 handleXGeometry(&dlg);
193 bool retcode = (dlg.exec() == QDialog::Accepted);
195 if (retcode)
196 result = combo.currentText();
198 return retcode;
201 bool Widgets::listBox(QWidget *parent, const QString& title, const QString& text, const QStringList& args,
202 const QString& defaultEntry, QString &result)
204 KListBoxDialog box(text,parent);
206 kapp->setTopWidget( &box );
207 box.setCaption(title);
209 for (int i = 0; i+1<args.count(); i += 2) {
210 box.insertItem(args[i+1]);
212 box.setCurrentItem( defaultEntry );
214 handleXGeometry(&box);
216 bool retcode = (box.exec() == QDialog::Accepted);
217 if ( retcode )
218 result = args[ box.currentItem()*2 ];
219 return retcode;
223 bool Widgets::checkList(QWidget *parent, const QString& title, const QString& text, const QStringList& args, bool separateOutput, QStringList &result)
225 QStringList entries, tags;
226 QString rs;
228 result.clear();
230 KListBoxDialog box(text,parent);
232 QListWidget &table = box.getTable();
234 kapp->setTopWidget( &box );
235 box.setCaption(title);
237 for (int i=0; i+2<args.count(); i += 3) {
238 tags.append(args[i]);
239 entries.append(args[i+1]);
242 table.addItems(entries);
243 table.setSelectionMode(QListWidget::MultiSelection);
244 table.setCurrentItem(0); // This is to circumvent a Qt bug
246 for (int i=0; i+2<args.count(); i += 3) {
247 table.item( i/3 )->setSelected( args[i+2] == QLatin1String("on") );
250 handleXGeometry(&box);
252 bool retcode = (box.exec() == QDialog::Accepted);
254 if ( retcode ) {
255 if (separateOutput) {
256 for (int i=0; i<table.count(); i++)
257 if (table.item(i)->isSelected())
258 result.append(tags[i]);
259 } else {
260 for (int i=0; i<table.count(); i++)
261 if (table.item(i)->isSelected())
262 rs += QLatin1String("\"") + tags[i] + QLatin1String("\" ");
263 result.append(rs);
266 return retcode;
270 bool Widgets::radioBox(QWidget *parent, const QString& title, const QString& text, const QStringList& args, QString &result)
272 QStringList entries, tags;
274 KListBoxDialog box(text,parent);
276 QListWidget &table = box.getTable();
278 kapp->setTopWidget( &box );
279 box.setCaption(title);
281 for (int i=0; i+2<args.count(); i += 3) {
282 tags.append(args[i]);
283 entries.append(args[i+1]);
286 table.addItems(entries);
288 for (int i=0; i+2<args.count(); i += 3) {
289 table.item( i/3 )->setSelected( args[i+2] == QLatin1String("on") );
292 handleXGeometry(&box);
294 bool retcode = (box.exec() == QDialog::Accepted);
295 if ( retcode )
296 result = tags[ table.currentRow() ];
297 return retcode;
300 bool Widgets::progressBar(QWidget *parent, const QString& title, const QString& text, int totalSteps)
302 ProgressDialog dlg( parent, title, text, totalSteps );
303 kapp->setTopWidget( &dlg );
304 dlg.setCaption( title );
305 handleXGeometry(&dlg);
306 dlg.exec();
307 return dlg.wasCancelled();
311 bool Widgets::slider( QWidget *parent, const QString& title, const QString& text, int minValue, int maxValue, int step, int &result )
313 KDialog dlg( parent );
314 kapp->setTopWidget( &dlg );
315 dlg.setCaption( title );
316 dlg.setButtons( KDialog::Ok|KDialog::Cancel );
317 dlg.setModal( true );
318 dlg.setDefaultButton( KDialog::Ok );
320 KVBox* vbox = new KVBox( &dlg );
321 dlg.setMainWidget( vbox );
323 QLabel label (vbox);
324 label.setText (text);
325 QSlider slider (vbox);
326 slider.setMinimum( minValue );
327 slider.setMaximum( maxValue );
328 slider.setSingleStep( step );
329 slider.setTickPosition ( QSlider::TicksAbove );
330 slider.setOrientation( Qt::Horizontal );
331 handleXGeometry(&dlg);
333 bool retcode = (dlg.exec() == QDialog::Accepted);
335 if (retcode)
336 result = slider.value();
338 return retcode;
341 bool Widgets::calendar( QWidget *parent, const QString &title, const QString &text, QDate & result )
343 KDialog dlg( parent );
344 kapp->setTopWidget( &dlg );
345 dlg.setCaption( title );
346 dlg.setButtons( KDialog::Ok|KDialog::Cancel );
347 dlg.setModal( true );
348 dlg.setDefaultButton( KDialog::Ok );
350 KVBox* vbox = new KVBox( &dlg );
351 dlg.setMainWidget( vbox );
353 QLabel label (vbox);
354 label.setText (text);
355 KDatePicker dateWidget( vbox );
356 handleXGeometry(&dlg);
358 bool retcode = (dlg.exec() == QDialog::Accepted);
360 if (retcode)
361 result = dateWidget.date();
363 return retcode;