Updated the README file with some contributor tips.
[basket4.git] / src / variouswidgets.cpp
blobbf6d20471a7070f94c93165934e623cd686fff72
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 ***************************************************************************/
21 #include <qlayout.h>
22 #include <qlineedit.h>
23 #include <qlabel.h>
24 #include <qsizegrip.h>
25 #include <qpushbutton.h>
26 #include <qstring.h>
27 #include <qsizepolicy.h>
28 //Added by qt3to4:
29 #include <Q3HBoxLayout>
30 #include <QResizeEvent>
31 #include <Q3ValueList>
32 #include <QKeyEvent>
33 #include <Q3VBoxLayout>
34 #include <kopenwith.h>
35 #include <klocale.h>
36 #include <q3whatsthis.h>
37 #include <k3iconview.h>
38 #include <kiconloader.h>
39 #include <q3dragobject.h>
40 #include <qfontdatabase.h>
42 #include "variouswidgets.h"
44 /** class RunCommandRequester: */
46 RunCommandRequester::RunCommandRequester(const QString &runCommand, const QString &message, QWidget *parent, const char *name)
47 : QWidget(parent, name)
49 m_message = message;
51 Q3HBoxLayout *layout = new Q3HBoxLayout(this, /*margin=*/0, KDialog::spacingHint());
52 m_runCommand = new QLineEdit(runCommand, this);
53 QPushButton *pb = new QPushButton(/*"C&hoose..."*/i18n("..."), this);
55 pb->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
57 layout->addWidget(m_runCommand);
58 layout->addWidget(pb);
60 connect( pb, SIGNAL(clicked()), this, SLOT(slotSelCommand()) );
63 RunCommandRequester::~RunCommandRequester()
67 void RunCommandRequester::slotSelCommand()
69 KOpenWithDlg *dlg = new KOpenWithDlg(KUrl::List(), m_message, m_runCommand->text(), this);
70 dlg->exec();
71 if ( ! dlg->text().isEmpty() )
72 m_runCommand->setText(dlg->text());
75 QString RunCommandRequester::runCommand()
77 return m_runCommand->text();
80 void RunCommandRequester::setRunCommand(const QString &runCommand)
82 m_runCommand->setText(runCommand);
85 /** class IconSizeCombo: */
87 IconSizeCombo::IconSizeCombo(bool rw, QWidget *parent, const char *name)
88 : QComboBox(rw, parent, name)
90 insertItem(i18n("16 by 16 pixels"));
91 insertItem(i18n("22 by 22 pixels"));
92 insertItem(i18n("32 by 32 pixels"));
93 insertItem(i18n("48 by 48 pixels"));
94 insertItem(i18n("64 by 64 pixels"));
95 insertItem(i18n("128 by 128 pixels"));
96 setCurrentItem(2);
99 IconSizeCombo::~IconSizeCombo()
103 int IconSizeCombo::iconSize()
105 switch (currentItem()) {
106 default:
107 case 0: return 16;
108 case 1: return 22;
109 case 2: return 32;
110 case 3: return 48;
111 case 4: return 64;
112 case 5: return 128;
116 void IconSizeCombo::setSize(int size)
118 switch (size) {
119 default:
120 case 16: setCurrentItem(0); break;
121 case 22: setCurrentItem(1); break;
122 case 32: setCurrentItem(2); break;
123 case 48: setCurrentItem(3); break;
124 case 64: setCurrentItem(4); break;
125 case 128: setCurrentItem(5); break;
129 /** class ViewSizeDialog: */
131 ViewSizeDialog::ViewSizeDialog(QWidget *parent, int w, int h)
132 : QDialog(parent, "ViewSizeDialog")
134 QLabel *label = new QLabel(i18n(
135 "Resize the window to select the image size\n"
136 "and close it or press Escape to accept changes."), this);
137 label->move(8, 8);
138 label->setFixedSize( label->sizeHint() );
140 // setSizeGripEnabled(true) doesn't work (the grip stay at the same place), so we emulate it:
141 m_sizeGrip = new QSizeGrip(this);
142 m_sizeGrip->setFixedSize( m_sizeGrip->sizeHint() );
144 setGeometry(x(), y(), w, h);
147 ViewSizeDialog::~ViewSizeDialog()
151 void ViewSizeDialog::resizeEvent(QResizeEvent *)
153 setCaption( i18n("%1 by %2 pixels").arg(QString::number(width())).arg(QString::number(height())) );
154 m_sizeGrip->move( width() - m_sizeGrip->width(), height() - m_sizeGrip->height() );
157 /** class HelpLabel: */
159 HelpLabel::HelpLabel(const QString &text, const QString &message, QWidget *parent)
160 : KUrlLabel(parent), m_message(message)
162 setText(text);
163 connect( this, SIGNAL(leftClickedUrl()), this, SLOT(showMessage()) );
166 HelpLabel::~HelpLabel()
170 void HelpLabel::showMessage()
172 Q3WhatsThis::display(m_message, mapToGlobal( QPoint(width() / 2, height()) ));
175 void HelpLabel::keyPressEvent(QKeyEvent *event)
177 if (event->key() == Qt::Key_Space)
178 showMessage();
179 else
180 KUrlLabel::keyPressEvent(event);
183 /** class IconSizeDialog: */
185 class UndraggableKIconView : public K3IconView
187 public:
188 UndraggableKIconView(QWidget * parent = 0, const char * name = 0, Qt::WFlags f = 0) : K3IconView(parent, name, f) {}
189 Q3DragObject* dragObject() { return 0; }
192 IconSizeDialog::IconSizeDialog(const QString &caption, const QString &message, const QString &icon, int iconSize, QWidget *parent)
193 : KDialog(parent);
195 // KDialog options
196 setCaption(caption);
197 setButtons(Ok | Cancel);
198 setDefaultButton(Ok);
199 setModal(true);
200 showButtonSeparator(false);
201 connect(this, SIGNAL(cancelClicked()), SLOT(slotCancel()));
203 QWidget *page = new QWidget(this);
204 Q3VBoxLayout *topLayout = new Q3VBoxLayout(page, /*margin=*/0, spacingHint());
206 QLabel *label = new QLabel(message, page);
207 topLayout->addWidget(label);
209 K3IconView *iconView = new UndraggableKIconView(page);
210 iconView->setItemsMovable(false);
211 iconView->setSelectionMode(K3IconView::Single);
212 m_size16 = new K3IconViewItem(iconView, 0, i18n("16 by 16 pixels"), DesktopIcon(icon, 16));
213 m_size22 = new K3IconViewItem(iconView, m_size16, i18n("22 by 22 pixels"), DesktopIcon(icon, 22));
214 m_size32 = new K3IconViewItem(iconView, m_size22, i18n("32 by 32 pixels"), DesktopIcon(icon, 32));
215 m_size48 = new K3IconViewItem(iconView, m_size32, i18n("48 by 48 pixels"), DesktopIcon(icon, 48));
216 m_size64 = new K3IconViewItem(iconView, m_size48, i18n("64 by 64 pixels"), DesktopIcon(icon, 64));
217 m_size128 = new K3IconViewItem(iconView, m_size64, i18n("128 by 128 pixels"), DesktopIcon(icon, 128));
218 iconView->setMinimumWidth(m_size16->width() + m_size22->width() + m_size32->width() + m_size48->width() + m_size64->width() + m_size128->width() +
219 (6+2) * iconView->spacing() + 20);
220 iconView->setMinimumHeight(m_size128->height() + 2 * iconView->spacing() + 20);
221 topLayout->addWidget(iconView);
222 switch (iconSize) {
223 case 16: iconView->setSelected(m_size16, true); m_iconSize = 16; break;
224 case 22: iconView->setSelected(m_size22, true); m_iconSize = 22; break;
225 default:
226 case 32: iconView->setSelected(m_size32, true); m_iconSize = 32; break;
227 case 48: iconView->setSelected(m_size48, true); m_iconSize = 48; break;
228 case 64: iconView->setSelected(m_size64, true); m_iconSize = 64; break;
229 case 128: iconView->setSelected(m_size128, true); m_iconSize = 128; break;
232 connect( iconView, SIGNAL(executed(Q3IconViewItem*)), this, SLOT(choose(Q3IconViewItem*)) );
233 connect( iconView, SIGNAL(returnPressed(Q3IconViewItem*)), this, SLOT(choose(Q3IconViewItem*)) );
234 connect( iconView, SIGNAL(selectionChanged()), this, SLOT(slotSelectionChanged()) );
236 setMainWidget(page);
239 IconSizeDialog::~IconSizeDialog()
243 void IconSizeDialog::slotSelectionChanged()
245 // Change m_iconSize to the new selected one:
246 if (m_size16->isSelected()) { m_iconSize = 16; return; }
247 if (m_size22->isSelected()) { m_iconSize = 22; return; }
248 if (m_size32->isSelected()) { m_iconSize = 32; return; }
249 if (m_size48->isSelected()) { m_iconSize = 48; return; }
250 if (m_size64->isSelected()) { m_iconSize = 64; return; }
251 if (m_size128->isSelected()) { m_iconSize = 128; return; }
253 // But if user unselected the item (by eg. right clicking a free space), reselect the last one:
254 switch (m_iconSize) {
255 case 16: m_size16->setSelected(true); m_iconSize = 16; break;
256 case 22: m_size22->setSelected(true); m_iconSize = 22; break;
257 default:
258 case 32: m_size32->setSelected(true); m_iconSize = 32; break;
259 case 48: m_size48->setSelected(true); m_iconSize = 48; break;
260 case 64: m_size64->setSelected(true); m_iconSize = 64; break;
261 case 128: m_size128->setSelected(true); m_iconSize = 128; break;
265 void IconSizeDialog::choose(Q3IconViewItem*)
267 actionButton(Ok)->animateClick();
270 void IconSizeDialog::slotCancel()
272 m_iconSize = -1;
275 /** class FontSizeCombo: */
277 FontSizeCombo::FontSizeCombo(bool rw, bool withDefault, QWidget *parent, const char *name)
278 : KComboBox(rw, parent, name), m_withDefault(withDefault)
280 if (m_withDefault)
281 insertItem(i18n("(Default)"));
283 QFontDatabase fontDB;
284 Q3ValueList<int> sizes = fontDB.standardSizes();
285 for (Q3ValueList<int>::Iterator it = sizes.begin(); it != sizes.end(); ++it)
286 insertItem(QString::number(*it));
288 // connect( this, SIGNAL(acivated(const QString&)), this, SLOT(textChangedInCombo(const QString&)) );
289 connect( this, SIGNAL(textChanged(const QString&)), this, SLOT(textChangedInCombo(const QString&)) );
291 // TODO: 01617 void KFontSizeAction::setFontSize( int size )
294 FontSizeCombo::~FontSizeCombo()
298 void FontSizeCombo::textChangedInCombo(const QString &text)
300 bool ok = false;
301 int size = text.toInt(&ok);
302 if (ok)
303 emit sizeChanged(size);
306 void FontSizeCombo::keyPressEvent(QKeyEvent *event)
308 if (event->key() == Qt::Key_Escape)
309 emit escapePressed();
310 else if (event->key() == Qt::Key_Return)
311 emit returnPressed2();
312 else
313 KComboBox::keyPressEvent(event);
316 void FontSizeCombo::setFontSize(int size)
318 setCurrentText(QString::number(size));
320 // TODO: SEE KFontSizeAction::setFontSize( int size ) !!! for a more complete method!
323 int FontSizeCombo::fontSize()
325 bool ok = false;
326 int size = currentText().toInt(&ok);
327 if (ok)
328 return size;
330 size = text(currentItem()).toInt(&ok);
331 if (ok)
332 return size;
334 return font().pointSize();
337 #include "variouswidgets.moc"