1 /***************************************************************************
2 * Copyright (C) 2003 by Sébastien Laoût *
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 the 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 *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
22 #include <qlineedit.h>
24 #include <qsizegrip.h>
25 #include <qpushbutton.h>
27 #include <qsizepolicy.h>
29 #include <Q3HBoxLayout>
30 #include <QResizeEvent>
31 #include <Q3ValueList>
33 #include <Q3VBoxLayout>
34 #include <kopenwith.h>
36 #include <q3whatsthis.h>
37 #include <kiconview.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
)
51 Q3HBoxLayout
*layout
= new Q3HBoxLayout(this, /*margin=*/0, KDialogBase::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);
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"));
99 IconSizeCombo::~IconSizeCombo()
103 int IconSizeCombo::iconSize()
105 switch (currentItem()) {
116 void IconSizeCombo::setSize(int size
)
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);
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
)
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
)
180 KURLLabel::keyPressEvent(event
);
183 /** class IconSizeDialog: */
185 class UndraggableKIconView
: public KIconView
188 UndraggableKIconView(QWidget
* parent
= 0, const char * name
= 0, WFlags f
= 0) : KIconView(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 : KDialogBase(KDialogBase::Swallow
, caption
, KDialogBase::Ok
| KDialogBase::Cancel
,
194 KDialogBase::Ok
, parent
, /*name=*/0, /*modal=*/true, /*separator=*/false)
196 QWidget
*page
= new QWidget(this);
197 Q3VBoxLayout
*topLayout
= new Q3VBoxLayout(page
, /*margin=*/0, spacingHint());
199 QLabel
*label
= new QLabel(message
, page
);
200 topLayout
->addWidget(label
);
202 KIconView
*iconView
= new UndraggableKIconView(page
);
203 iconView
->setItemsMovable(false);
204 iconView
->setSelectionMode(KIconView::Single
);
205 m_size16
= new KIconViewItem(iconView
, 0, i18n("16 by 16 pixels"), DesktopIcon(icon
, 16));
206 m_size22
= new KIconViewItem(iconView
, m_size16
, i18n("22 by 22 pixels"), DesktopIcon(icon
, 22));
207 m_size32
= new KIconViewItem(iconView
, m_size22
, i18n("32 by 32 pixels"), DesktopIcon(icon
, 32));
208 m_size48
= new KIconViewItem(iconView
, m_size32
, i18n("48 by 48 pixels"), DesktopIcon(icon
, 48));
209 m_size64
= new KIconViewItem(iconView
, m_size48
, i18n("64 by 64 pixels"), DesktopIcon(icon
, 64));
210 m_size128
= new KIconViewItem(iconView
, m_size64
, i18n("128 by 128 pixels"), DesktopIcon(icon
, 128));
211 iconView
->setMinimumWidth(m_size16
->width() + m_size22
->width() + m_size32
->width() + m_size48
->width() + m_size64
->width() + m_size128
->width() +
212 (6+2) * iconView
->spacing() + 20);
213 iconView
->setMinimumHeight(m_size128
->height() + 2 * iconView
->spacing() + 20);
214 topLayout
->addWidget(iconView
);
216 case 16: iconView
->setSelected(m_size16
, true); m_iconSize
= 16; break;
217 case 22: iconView
->setSelected(m_size22
, true); m_iconSize
= 22; break;
219 case 32: iconView
->setSelected(m_size32
, true); m_iconSize
= 32; break;
220 case 48: iconView
->setSelected(m_size48
, true); m_iconSize
= 48; break;
221 case 64: iconView
->setSelected(m_size64
, true); m_iconSize
= 64; break;
222 case 128: iconView
->setSelected(m_size128
, true); m_iconSize
= 128; break;
225 connect( iconView
, SIGNAL(executed(Q3IconViewItem
*)), this, SLOT(choose(Q3IconViewItem
*)) );
226 connect( iconView
, SIGNAL(returnPressed(Q3IconViewItem
*)), this, SLOT(choose(Q3IconViewItem
*)) );
227 connect( iconView
, SIGNAL(selectionChanged()), this, SLOT(slotSelectionChanged()) );
232 IconSizeDialog::~IconSizeDialog()
236 void IconSizeDialog::slotSelectionChanged()
238 // Change m_iconSize to the new selected one:
239 if (m_size16
->isSelected()) { m_iconSize
= 16; return; }
240 if (m_size22
->isSelected()) { m_iconSize
= 22; return; }
241 if (m_size32
->isSelected()) { m_iconSize
= 32; return; }
242 if (m_size48
->isSelected()) { m_iconSize
= 48; return; }
243 if (m_size64
->isSelected()) { m_iconSize
= 64; return; }
244 if (m_size128
->isSelected()) { m_iconSize
= 128; return; }
246 // But if user unselected the item (by eg. right clicking a free space), reselect the last one:
247 switch (m_iconSize
) {
248 case 16: m_size16
->setSelected(true); m_iconSize
= 16; break;
249 case 22: m_size22
->setSelected(true); m_iconSize
= 22; break;
251 case 32: m_size32
->setSelected(true); m_iconSize
= 32; break;
252 case 48: m_size48
->setSelected(true); m_iconSize
= 48; break;
253 case 64: m_size64
->setSelected(true); m_iconSize
= 64; break;
254 case 128: m_size128
->setSelected(true); m_iconSize
= 128; break;
258 void IconSizeDialog::choose(Q3IconViewItem
*)
260 actionButton(KDialogBase::Ok
)->animateClick();
263 void IconSizeDialog::slotCancel()
266 KDialogBase::slotCancel();
269 /** class FontSizeCombo: */
271 FontSizeCombo::FontSizeCombo(bool rw
, bool withDefault
, QWidget
*parent
, const char *name
)
272 : KComboBox(rw
, parent
, name
), m_withDefault(withDefault
)
275 insertItem(i18n("(Default)"));
277 QFontDatabase fontDB
;
278 Q3ValueList
<int> sizes
= fontDB
.standardSizes();
279 for (Q3ValueList
<int>::Iterator it
= sizes
.begin(); it
!= sizes
.end(); ++it
)
280 insertItem(QString::number(*it
));
282 // connect( this, SIGNAL(acivated(const QString&)), this, SLOT(textChangedInCombo(const QString&)) );
283 connect( this, SIGNAL(textChanged(const QString
&)), this, SLOT(textChangedInCombo(const QString
&)) );
285 // TODO: 01617 void KFontSizeAction::setFontSize( int size )
288 FontSizeCombo::~FontSizeCombo()
292 void FontSizeCombo::textChangedInCombo(const QString
&text
)
295 int size
= text
.toInt(&ok
);
297 emit
sizeChanged(size
);
300 void FontSizeCombo::keyPressEvent(QKeyEvent
*event
)
302 if (event
->key() == Qt::Key_Escape
)
303 emit
escapePressed();
304 else if (event
->key() == Qt::Key_Return
)
305 emit
returnPressed2();
307 KComboBox::keyPressEvent(event
);
310 void FontSizeCombo::setFontSize(int size
)
312 setCurrentText(QString::number(size
));
314 // TODO: SEE KFontSizeAction::setFontSize( int size ) !!! for a more complete method!
317 int FontSizeCombo::fontSize()
320 int size
= currentText().toInt(&ok
);
324 size
= text(currentItem()).toInt(&ok
);
328 return font().pointSize();
331 #include "variouswidgets.moc"