1 /***************************************************************************
2 * Copyright (C) 2003 by Sébastien Laoût *
3 * slaout@linux62.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 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 "variouswidgets.h"
22
23 #include <QtCore/QList>
24 #include <QtCore/QPointer>
25 #include <QtCore/QString>
26 #include <QLabel>
27 #include <QSizeGrip>
28 #include <QPushButton>
29 #include <QSizePolicy>
30 #include <QHBoxLayout>
31 #include <QtGui/QResizeEvent>
32 #include <QtGui/QKeyEvent>
33 #include <QVBoxLayout>
34 #include <QWhatsThis>
35 #include <QtGui/QDrag>
36 #include <QtGui/QFontDatabase>
37 #include <QLineEdit>
38 #include <QListWidget>
39 #include <QLocale>
40 #include <QPushButton>
41 #include <QDialogButtonBox>
42
43 #include <KOpenWithDialog>
44 #include <KConfigGroup>
45 #include <KIconLoader>
46 #include <KLocalizedString>
47
48 /** class RunCommandRequester: */
49
RunCommandRequester(const QString & runCommand,const QString & message,QWidget * parent)50 RunCommandRequester::RunCommandRequester(const QString &runCommand, const QString &message, QWidget *parent)
51 : QWidget(parent)
52 {
53 m_message = message;
54
55 QHBoxLayout *layout = new QHBoxLayout(this);
56 m_runCommand = new QLineEdit(runCommand, this);
57 QPushButton *pb = new QPushButton(/*"C&hoose..."*/i18n("..."), this);
58
59 pb->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
60
61 layout->addWidget(m_runCommand);
62 layout->addWidget(pb);
63
64 connect(pb, SIGNAL(clicked()), this, SLOT(slotSelCommand()));
65 }
66
~RunCommandRequester()67 RunCommandRequester::~RunCommandRequester()
68 {
69 }
70
slotSelCommand()71 void RunCommandRequester::slotSelCommand()
72 {
73 QPointer<KOpenWithDialog> dlg = new KOpenWithDialog(QList<QUrl>(), m_message, m_runCommand->text(), this);
74 dlg->exec();
75 if (! dlg->text().isEmpty())
76 m_runCommand->setText(dlg->text());
77 }
78
runCommand()79 QString RunCommandRequester::runCommand()
80 {
81 return m_runCommand->text();
82 }
83
setRunCommand(const QString & runCommand)84 void RunCommandRequester::setRunCommand(const QString &runCommand)
85 {
86 m_runCommand->setText(runCommand);
87 }
88
89 /** class IconSizeCombo: */
90
IconSizeCombo(QWidget * parent)91 IconSizeCombo::IconSizeCombo(QWidget *parent)
92 : KComboBox(parent)
93 {
94 addItem(i18n("16 by 16 pixels"));
95 addItem(i18n("22 by 22 pixels"));
96 addItem(i18n("32 by 32 pixels"));
97 addItem(i18n("48 by 48 pixels"));
98 addItem(i18n("64 by 64 pixels"));
99 addItem(i18n("128 by 128 pixels"));
100 setCurrentIndex(2);
101 }
102
~IconSizeCombo()103 IconSizeCombo::~IconSizeCombo()
104 {
105 }
106
iconSize()107 int IconSizeCombo::iconSize()
108 {
109 switch (currentIndex()) {
110 default:
111 case 0: return 16;
112 case 1: return 22;
113 case 2: return 32;
114 case 3: return 48;
115 case 4: return 64;
116 case 5: return 128;
117 }
118 }
119
setSize(int size)120 void IconSizeCombo::setSize(int size)
121 {
122 switch (size) {
123 default:
124 case 16: setCurrentIndex(0); break;
125 case 22: setCurrentIndex(1); break;
126 case 32: setCurrentIndex(2); break;
127 case 48: setCurrentIndex(3); break;
128 case 64: setCurrentIndex(4); break;
129 case 128: setCurrentIndex(5); break;
130 }
131 }
132
133 /** class ViewSizeDialog: */
134
ViewSizeDialog(QWidget * parent,int w,int h)135 ViewSizeDialog::ViewSizeDialog(QWidget *parent, int w, int h)
136 : QDialog(parent)
137 {
138 QLabel *label = new QLabel(i18n(
139 "Resize the window to select the image size\n"
140 "and close it or press Escape to accept changes."), this);
141 label->move(8, 8);
142 label->setFixedSize(label->sizeHint());
143
144 // setSizeGripEnabled(true) doesn't work (the grip stay at the same place), so we emulate it:
145 m_sizeGrip = new QSizeGrip(this);
146 m_sizeGrip->setFixedSize(m_sizeGrip->sizeHint());
147
148 setGeometry(x(), y(), w, h);
149 }
150
~ViewSizeDialog()151 ViewSizeDialog::~ViewSizeDialog()
152 {
153 }
154
resizeEvent(QResizeEvent *)155 void ViewSizeDialog::resizeEvent(QResizeEvent *)
156 {
157 setWindowTitle(i18n("%1 by %2 pixels", QString::number(width()), QString::number(height())));
158 m_sizeGrip->move(width() - m_sizeGrip->width(), height() - m_sizeGrip->height());
159 }
160
161 /** class HelpLabel: */
162
HelpLabel(const QString & text,const QString & message,QWidget * parent)163 HelpLabel::HelpLabel(const QString &text, const QString &message, QWidget *parent)
164 : KUrlLabel(parent), m_message(message)
165 {
166 setText(text);
167 setWhatsThis(m_message);
168 connect(this, SIGNAL(leftClickedUrl()), this, SLOT(display()));
169 }
170
~HelpLabel()171 HelpLabel::~HelpLabel()
172 {
173 }
174
display()175 void HelpLabel::display()
176 {
177 QWhatsThis::showText(mapToGlobal(QPoint(width() / 2, height())), m_message);
178 }
179
180 /** class IconSizeDialog: */
181
182 class UndraggableKIconView : public QListWidget
183 {
184 public:
UndraggableKIconView(QWidget * parent=0)185 UndraggableKIconView(QWidget * parent = 0) : QListWidget(parent) {
186 this->setViewMode(QListView::IconMode);
187 this->setMovement(QListView::Static);
188 this->setSelectionMode(QAbstractItemView::SingleSelection);
189 this->setWrapping(false);
190 }
dragObject()191 QDrag* dragObject() {
192 return 0;
193 }
194 };
195
IconSizeDialog(const QString & caption,const QString & message,const QString & icon,int iconSize,QWidget * parent)196 IconSizeDialog::IconSizeDialog(const QString &caption, const QString &message, const QString &icon, int iconSize, QWidget *parent)
197 : QDialog(parent)
198 {
199 // QDialog options
200 setWindowTitle(caption);
201
202 QWidget *mainWidget = new QWidget(this);
203 QVBoxLayout *mainLayout = new QVBoxLayout;
204 setLayout(mainLayout);
205 mainLayout->addWidget(mainWidget);
206
207 setModal(true);
208
209 QWidget *page = new QWidget(this);
210 QVBoxLayout *topLayout = new QVBoxLayout(page);
211
212 QLabel *label = new QLabel(message, page);
213 topLayout->addWidget(label);
214
215 QListWidget *iconView = new UndraggableKIconView(page);
216
217 m_size16 = new QListWidgetItem(DesktopIcon(icon, 16), i18n("16 by 16 pixels"), iconView);
218 m_size22 = new QListWidgetItem(DesktopIcon(icon, 22), i18n("22 by 22 pixels"), iconView);
219 m_size32 = new QListWidgetItem(DesktopIcon(icon, 32), i18n("32 by 32 pixels"), iconView);
220 m_size48 = new QListWidgetItem(DesktopIcon(icon, 48), i18n("48 by 48 pixels"), iconView);
221 m_size64 = new QListWidgetItem(DesktopIcon(icon, 64), i18n("64 by 64 pixels"), iconView);
222 m_size128 = new QListWidgetItem(DesktopIcon(icon, 128), i18n("128 by 128 pixels"), iconView);
223 iconView->setIconSize(QSize(128, 128));
224 iconView->setMinimumSize(QSize(128*6 + (6 + 2) * iconView->spacing() + 20, m_size128->sizeHint().height() + 2 * iconView->spacing() + 20));
225 topLayout->addWidget(iconView);
226 switch (iconSize) {
227 case 16: m_size16->setSelected(true); m_iconSize = 16; break;
228 case 22: m_size22->setSelected(true); m_iconSize = 22; break;
229 default:
230 case 32: m_size32->setSelected(true); m_iconSize = 32; break;
231 case 48: m_size48->setSelected(true); m_iconSize = 48; break;
232 case 64: m_size64->setSelected(true); m_iconSize = 64; break;
233 case 128: m_size128->setSelected(true); m_iconSize = 128; break;
234 }
235
236 connect(iconView, SIGNAL(executed(QListWidgetItem*)), this, SLOT(choose(QListWidgetItem*)));
237 connect(iconView, SIGNAL(itemActivated(QListWidgetItem*)), this, SLOT(choose(QListWidgetItem*)));
238 connect(iconView, SIGNAL(itemSelectionChanged()), this, SLOT(slotSelectionChanged()));
239
240 mainLayout->addWidget(page);
241
242 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel, this);
243 okButton = buttonBox->button(QDialogButtonBox::Ok);
244 okButton->setDefault(true);
245 okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
246 connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
247 connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
248 mainLayout->addWidget(buttonBox);
249 connect(buttonBox->button(QDialogButtonBox::Cancel), SIGNAL(clicked()), SLOT(slotCancel()));
250 }
251
~IconSizeDialog()252 IconSizeDialog::~IconSizeDialog()
253 {
254 }
255
slotSelectionChanged()256 void IconSizeDialog::slotSelectionChanged()
257 {
258 // Change m_iconSize to the new selected one:
259 if (m_size16->isSelected()) {
260 m_iconSize = 16; return;
261 }
262 if (m_size22->isSelected()) {
263 m_iconSize = 22; return;
264 }
265 if (m_size32->isSelected()) {
266 m_iconSize = 32; return;
267 }
268 if (m_size48->isSelected()) {
269 m_iconSize = 48; return;
270 }
271 if (m_size64->isSelected()) {
272 m_iconSize = 64; return;
273 }
274 if (m_size128->isSelected()) {
275 m_iconSize = 128; return;
276 }
277
278 // But if user unselected the item (by eg. right clicking a free space), reselect the last one:
279 switch (m_iconSize) {
280 case 16: m_size16->setSelected(true); m_iconSize = 16; break;
281 case 22: m_size22->setSelected(true); m_iconSize = 22; break;
282 default:
283 case 32: m_size32->setSelected(true); m_iconSize = 32; break;
284 case 48: m_size48->setSelected(true); m_iconSize = 48; break;
285 case 64: m_size64->setSelected(true); m_iconSize = 64; break;
286 case 128: m_size128->setSelected(true); m_iconSize = 128; break;
287 }
288 }
289
choose(QListWidgetItem *)290 void IconSizeDialog::choose(QListWidgetItem *)
291 {
292 okButton->animateClick();
293 }
294
slotCancel()295 void IconSizeDialog::slotCancel()
296 {
297 m_iconSize = -1;
298 }
299
300 /** class FontSizeCombo: */
301
FontSizeCombo(bool rw,bool withDefault,QWidget * parent)302 FontSizeCombo::FontSizeCombo(bool rw, bool withDefault, QWidget *parent)
303 : KComboBox(rw, parent), m_withDefault(withDefault)
304 {
305 if (m_withDefault)
306 addItem(i18n("(Default)"));
307
308 QFontDatabase fontDB;
309 QList<int> sizes = fontDB.standardSizes();
310 for (QList<int>::Iterator it = sizes.begin(); it != sizes.end(); ++it)
311 addItem(QString::number(*it));
312
313 // connect( this, SIGNAL(acivated(const QString&)), this, SLOT(textChangedInCombo(const QString&)) );
314 connect(this, SIGNAL(editTextChanged(const QString&)), this, SLOT(textChangedInCombo(const QString&)));
315
316 // TODO: 01617 void KFontSizeAction::setFontSize( int size )
317 }
318
~FontSizeCombo()319 FontSizeCombo::~FontSizeCombo()
320 {
321 }
322
textChangedInCombo(const QString & text)323 void FontSizeCombo::textChangedInCombo(const QString &text)
324 {
325 bool ok = false;
326 int size = text.toInt(&ok);
327 if (ok)
328 emit sizeChanged(size);
329 }
330
keyPressEvent(QKeyEvent * event)331 void FontSizeCombo::keyPressEvent(QKeyEvent *event)
332 {
333 if (event->key() == Qt::Key_Escape)
334 emit escapePressed();
335 else if (event->key() == Qt::Key_Return)
336 emit returnPressed2();
337 else
338 KComboBox::keyPressEvent(event);
339 }
340
setFontSize(qreal size)341 void FontSizeCombo::setFontSize(qreal size)
342 {
343 setItemText(currentIndex(), QString::number(size));
344
345 // TODO: SEE KFontSizeAction::setFontSize( int size ) !!! for a more complete method!
346 }
347
fontSize()348 qreal FontSizeCombo::fontSize()
349 {
350 bool ok = false;
351 int size = currentText().toInt(&ok);
352 if (ok)
353 return size;
354
355 size = currentText().toInt(&ok);
356 if (ok)
357 return size;
358
359 return font().pointSize();
360 }
361
362