2 // Copyright (C) 1998 Matthias Hoelzer <hoelzer@kde.org>
3 // Copyright (C) 2002-2005 David Faure <faure@kde.org>
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.
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.
23 #include <kdatepicker.h>
26 #include <QtCore/QFile>
27 #include <QtGui/QDesktopWidget>
28 #include <QtCore/QTextStream>
29 #include <QtGui/QTextCursor>
30 #include <QtGui/QLabel>
34 #include <kmessagebox.h>
35 #include <kinputdialog.h>
36 #include <kpassworddialog.h>
37 #include <kcombobox.h>
39 #include <kapplication.h>
40 #include <kcmdlineargs.h>
41 #include <ktextedit.h>
45 #include "klistboxdialog.h"
46 #include "progressdialog.h"
49 #if defined Q_WS_X11 && ! defined K_WS_QTONLY
53 void Widgets::handleXGeometry(QWidget
* dlg
)
57 KCmdLineArgs
*args
= KCmdLineArgs::parsedArgs("kde");
58 if (args
&& args
->isSet("geometry"))
59 geometry
= args
->getOption("geometry");
60 if ( ! geometry
.isEmpty()) {
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;
74 bool Widgets::inputBox(QWidget
*parent
, const QString
& title
, const QString
& text
, const QString
& init
, QString
&result
)
77 QString str
= KInputDialog::getText( title
, text
, init
, &ok
, parent
, 0, 0, QString() );
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
);
90 handleXGeometry(&dlg
);
92 bool retcode
= (dlg
.exec() == QDialog::Accepted
);
94 result
= dlg
.password();
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);
114 if (!f
.open(QIODevice::ReadOnly
))
116 kError() << i18n("kdialog: could not open file %1", file
) << endl
;
122 edit
->append(s
.readLine());
124 edit
->setTextCursor(QTextCursor(edit
->document()));
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);
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
);
168 result
= edit
->toPlainText().toLocal8Bit();
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
);
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
);
196 result
= combo
.currentText();
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
);
218 result
= args
[ box
.currentItem()*2 ];
223 bool Widgets::checkList(QWidget
*parent
, const QString
& title
, const QString
& text
, const QStringList
& args
, bool separateOutput
, QStringList
&result
)
225 QStringList entries
, tags
;
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
);
255 if (separateOutput
) {
256 for (int i
=0; i
<table
.count(); i
++)
257 if (table
.item(i
)->isSelected())
258 result
.append(tags
[i
]);
260 for (int i
=0; i
<table
.count(); i
++)
261 if (table
.item(i
)->isSelected())
262 rs
+= QLatin1String("\"") + tags
[i
] + QLatin1String("\" ");
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
);
296 result
= tags
[ table
.currentRow() ];
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
);
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
);
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
);
336 result
= slider
.value();
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
);
354 label
.setText (text
);
355 KDatePicker
dateWidget( vbox
);
356 handleXGeometry(&dlg
);
358 bool retcode
= (dlg
.exec() == QDialog::Accepted
);
361 result
= dateWidget
.date();