File indexing completed on 2022-09-27 16:29:29

0001 /**
0002  * SPDX-FileCopyrightText: (C) 2003 Sébastien Laoût <slaout@linux62.org>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "variouswidgets.h"
0008 
0009 #include <QDialogButtonBox>
0010 #include <QHBoxLayout>
0011 #include <QLabel>
0012 #include <QLineEdit>
0013 #include <QListWidget>
0014 #include <QLocale>
0015 #include <QPushButton>
0016 #include <QSizeGrip>
0017 #include <QSizePolicy>
0018 #include <QVBoxLayout>
0019 #include <QWhatsThis>
0020 #include <QtCore/QList>
0021 #include <QtCore/QPointer>
0022 #include <QtCore/QString>
0023 #include <QtGui/QDrag>
0024 #include <QtGui/QFontDatabase>
0025 #include <QtGui/QKeyEvent>
0026 #include <QtGui/QResizeEvent>
0027 
0028 #include <KConfigGroup>
0029 #include <KIconLoader>
0030 #include <KLocalizedString>
0031 #include <KOpenWithDialog>
0032 
0033 /** class RunCommandRequester: */
0034 
0035 RunCommandRequester::RunCommandRequester(const QString &runCommand, const QString &message, QWidget *parent)
0036     : QWidget(parent)
0037 {
0038     m_message = message;
0039 
0040     QHBoxLayout *layout = new QHBoxLayout(this);
0041     m_runCommand = new QLineEdit(runCommand, this);
0042     QPushButton *pb = new QPushButton(/*"C&hoose..."*/ i18n("..."), this);
0043 
0044     pb->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
0045 
0046     layout->addWidget(m_runCommand);
0047     layout->addWidget(pb);
0048 
0049     connect(pb, &QPushButton::clicked, this, &RunCommandRequester::slotSelCommand);
0050 }
0051 
0052 RunCommandRequester::~RunCommandRequester()
0053 {
0054 }
0055 
0056 void RunCommandRequester::slotSelCommand()
0057 {
0058     QPointer<KOpenWithDialog> dlg = new KOpenWithDialog(QList<QUrl>(), m_message, m_runCommand->text(), this);
0059     dlg->exec();
0060     if (!dlg->text().isEmpty())
0061         m_runCommand->setText(dlg->text());
0062 }
0063 
0064 QString RunCommandRequester::runCommand()
0065 {
0066     return m_runCommand->text();
0067 }
0068 
0069 void RunCommandRequester::setRunCommand(const QString &runCommand)
0070 {
0071     m_runCommand->setText(runCommand);
0072 }
0073 
0074 /** class IconSizeCombo: */
0075 
0076 IconSizeCombo::IconSizeCombo(QWidget *parent)
0077     : KComboBox(parent)
0078 {
0079     addItem(i18n("%1 by %1 pixels", KIconLoader::SizeSmall));
0080     addItem(i18n("%1 by %1 pixels", KIconLoader::SizeSmallMedium));
0081     addItem(i18n("%1 by %1 pixels", KIconLoader::SizeMedium));
0082     addItem(i18n("%1 by %1 pixels", KIconLoader::SizeLarge));
0083     addItem(i18n("%1 by %1 pixels", KIconLoader::SizeHuge));
0084     addItem(i18n("%1 by %1 pixels", KIconLoader::SizeEnormous));
0085     setCurrentIndex(2);
0086 }
0087 
0088 IconSizeCombo::~IconSizeCombo()
0089 {
0090 }
0091 
0092 int IconSizeCombo::iconSize()
0093 {
0094     switch (currentIndex()) {
0095     default:
0096     case 0:
0097         return KIconLoader::SizeSmall;
0098     case 1:
0099         return KIconLoader::SizeSmallMedium;
0100     case 2:
0101         return KIconLoader::SizeMedium;
0102     case 3:
0103         return KIconLoader::SizeLarge;
0104     case 4:
0105         return KIconLoader::SizeHuge;
0106     case 5:
0107         return KIconLoader::SizeEnormous;
0108     }
0109 }
0110 
0111 void IconSizeCombo::setSize(int size)
0112 {
0113     switch (size) {
0114     default:
0115     case KIconLoader::SizeSmall:
0116         setCurrentIndex(0);
0117         break;
0118     case KIconLoader::SizeSmallMedium:
0119         setCurrentIndex(1);
0120         break;
0121     case KIconLoader::SizeMedium:
0122         setCurrentIndex(2);
0123         break;
0124     case KIconLoader::SizeLarge:
0125         setCurrentIndex(3);
0126         break;
0127     case KIconLoader::SizeHuge:
0128         setCurrentIndex(4);
0129         break;
0130     case KIconLoader::SizeEnormous:
0131         setCurrentIndex(5);
0132         break;
0133     }
0134 }
0135 
0136 /** class ViewSizeDialog: */
0137 
0138 ViewSizeDialog::ViewSizeDialog(QWidget *parent, int w, int h)
0139     : QDialog(parent)
0140 {
0141     QLabel *label = new QLabel(i18n("Resize the window to select the image size\n"
0142                                     "and close it or press Escape to accept changes."),
0143                                this);
0144     label->move(8, 8);
0145     label->setFixedSize(label->sizeHint());
0146 
0147     // setSizeGripEnabled(true) doesn't work (the grip stay at the same place), so we emulate it:
0148     m_sizeGrip = new QSizeGrip(this);
0149     m_sizeGrip->setFixedSize(m_sizeGrip->sizeHint());
0150 
0151     setGeometry(x(), y(), w, h);
0152 }
0153 
0154 ViewSizeDialog::~ViewSizeDialog()
0155 {
0156 }
0157 
0158 void ViewSizeDialog::resizeEvent(QResizeEvent *)
0159 {
0160     setWindowTitle(i18n("%1 by %2 pixels", QString::number(width()), QString::number(height())));
0161     m_sizeGrip->move(width() - m_sizeGrip->width(), height() - m_sizeGrip->height());
0162 }
0163 
0164 /** class HelpLabel: */
0165 
0166 HelpLabel::HelpLabel(const QString &text, const QString &message, QWidget *parent)
0167     : KUrlLabel(parent)
0168     , m_message(message)
0169 {
0170     setText(text);
0171     setWhatsThis(m_message);
0172     connect(this, SIGNAL(leftClickedUrl()), this, SLOT(display()));
0173 }
0174 
0175 HelpLabel::~HelpLabel()
0176 {
0177 }
0178 
0179 void HelpLabel::display()
0180 {
0181     QWhatsThis::showText(mapToGlobal(QPoint(width() / 2, height())), m_message);
0182 }
0183 
0184 /** class IconSizeDialog: */
0185 
0186 class UndraggableKIconView : public QListWidget
0187 {
0188 public:
0189     UndraggableKIconView(QWidget *parent = nullptr)
0190         : QListWidget(parent)
0191     {
0192         this->setViewMode(QListView::IconMode);
0193         this->setMovement(QListView::Static);
0194         this->setSelectionMode(QAbstractItemView::SingleSelection);
0195         this->setWrapping(false);
0196     }
0197     QDrag *dragObject()
0198     {
0199         return nullptr;
0200     }
0201 };
0202 
0203 IconSizeDialog::IconSizeDialog(const QString &caption, const QString &message, const QString &icon, int iconSize, QWidget *parent)
0204     : QDialog(parent)
0205 {
0206     // QDialog options
0207     setWindowTitle(caption);
0208 
0209     QWidget *mainWidget = new QWidget(this);
0210     QVBoxLayout *mainLayout = new QVBoxLayout;
0211     setLayout(mainLayout);
0212     mainLayout->addWidget(mainWidget);
0213 
0214     setModal(true);
0215 
0216     QWidget *page = new QWidget(this);
0217     QVBoxLayout *topLayout = new QVBoxLayout(page);
0218 
0219     QLabel *label = new QLabel(message, page);
0220     topLayout->addWidget(label);
0221 
0222     QListWidget *iconView = new UndraggableKIconView(page);
0223 
0224     QIcon desktopIcon = QIcon::fromTheme(icon);
0225     m_size16 = new QListWidgetItem(desktopIcon.pixmap(KIconLoader::SizeSmall), i18n("%1 by %1 pixels", KIconLoader::SizeSmall), iconView);
0226     m_size22 = new QListWidgetItem(desktopIcon.pixmap(KIconLoader::SizeSmallMedium), i18n("%1 by %1 pixels", KIconLoader::SizeSmallMedium), iconView);
0227     m_size32 = new QListWidgetItem(desktopIcon.pixmap(KIconLoader::SizeMedium), i18n("%1 by %1 pixels", KIconLoader::SizeMedium), iconView);
0228     m_size48 = new QListWidgetItem(desktopIcon.pixmap(KIconLoader::SizeLarge), i18n("%1 by %1 pixels", KIconLoader::SizeLarge), iconView);
0229     m_size64 = new QListWidgetItem(desktopIcon.pixmap(KIconLoader::SizeHuge), i18n("%1 by %1 pixels", KIconLoader::SizeHuge), iconView);
0230     m_size128 = new QListWidgetItem(desktopIcon.pixmap(KIconLoader::SizeEnormous), i18n("%1 by %1 pixels", KIconLoader::SizeEnormous), iconView);
0231     iconView->setIconSize(QSize(KIconLoader::SizeEnormous, KIconLoader::SizeEnormous)); // 128x128
0232     iconView->setMinimumSize(QSize(128 * 6 + (6 + 2) * iconView->spacing() + 20, m_size128->sizeHint().height() + 2 * iconView->spacing() + 20));
0233     topLayout->addWidget(iconView);
0234     switch (iconSize) {
0235     case KIconLoader::SizeSmall:
0236         m_size16->setSelected(true);
0237         m_iconSize = KIconLoader::SizeSmall;
0238         break;
0239     case KIconLoader::SizeSmallMedium:
0240         m_size22->setSelected(true);
0241         m_iconSize = KIconLoader::SizeSmallMedium;
0242         break;
0243     default:
0244     case KIconLoader::SizeMedium:
0245         m_size32->setSelected(true);
0246         m_iconSize = KIconLoader::SizeMedium;
0247         break;
0248     case KIconLoader::SizeLarge:
0249         m_size48->setSelected(true);
0250         m_iconSize = KIconLoader::SizeLarge;
0251         break;
0252     case KIconLoader::SizeHuge:
0253         m_size64->setSelected(true);
0254         m_iconSize = KIconLoader::SizeHuge;
0255         break;
0256     case KIconLoader::SizeEnormous:
0257         m_size128->setSelected(true);
0258         m_iconSize = KIconLoader::SizeEnormous;
0259         break;
0260     }
0261 
0262     connect(iconView, SIGNAL(executed(QListWidgetItem *)), this, SLOT(choose(QListWidgetItem *)));
0263     connect(iconView, &QListWidget::itemActivated, this, &IconSizeDialog::choose);
0264     connect(iconView, &QListWidget::itemSelectionChanged, this, &IconSizeDialog::slotSelectionChanged);
0265 
0266     mainLayout->addWidget(page);
0267 
0268     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0269     okButton = buttonBox->button(QDialogButtonBox::Ok);
0270     okButton->setDefault(true);
0271     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0272     connect(buttonBox, &QDialogButtonBox::accepted, this, &IconSizeDialog::accept);
0273     connect(buttonBox, &QDialogButtonBox::rejected, this, &IconSizeDialog::reject);
0274     mainLayout->addWidget(buttonBox);
0275     connect(buttonBox->button(QDialogButtonBox::Cancel), &QPushButton::clicked, this, &IconSizeDialog::slotCancel);
0276 }
0277 
0278 IconSizeDialog::~IconSizeDialog()
0279 {
0280 }
0281 
0282 void IconSizeDialog::slotSelectionChanged()
0283 {
0284     // Change m_iconSize to the new selected one:
0285     if (m_size16->isSelected()) {
0286         m_iconSize = KIconLoader::SizeSmall;
0287         return;
0288     }
0289     if (m_size22->isSelected()) {
0290         m_iconSize = KIconLoader::SizeSmallMedium;
0291         return;
0292     }
0293     if (m_size32->isSelected()) {
0294         m_iconSize = KIconLoader::SizeMedium;
0295         return;
0296     }
0297     if (m_size48->isSelected()) {
0298         m_iconSize = KIconLoader::SizeLarge;
0299         return;
0300     }
0301     if (m_size64->isSelected()) {
0302         m_iconSize = KIconLoader::SizeHuge;
0303         return;
0304     }
0305     if (m_size128->isSelected()) {
0306         m_iconSize = KIconLoader::SizeEnormous;
0307         return;
0308     }
0309 
0310     // But if user unselected the item (by eg. right clicking a free space), reselect the last one:
0311     switch (m_iconSize) {
0312     case KIconLoader::SizeSmall:
0313         m_size16->setSelected(true);
0314         m_iconSize = KIconLoader::SizeSmall;
0315         break;
0316     case KIconLoader::SizeSmallMedium:
0317         m_size22->setSelected(true);
0318         m_iconSize = KIconLoader::SizeSmallMedium;
0319         break;
0320     default:
0321     case KIconLoader::SizeMedium:
0322         m_size32->setSelected(true);
0323         m_iconSize = KIconLoader::SizeMedium;
0324         break;
0325     case KIconLoader::SizeLarge:
0326         m_size48->setSelected(true);
0327         m_iconSize = KIconLoader::SizeLarge;
0328         break;
0329     case KIconLoader::SizeHuge:
0330         m_size64->setSelected(true);
0331         m_iconSize = KIconLoader::SizeHuge;
0332         break;
0333     case KIconLoader::SizeEnormous:
0334         m_size128->setSelected(true);
0335         m_iconSize = KIconLoader::SizeEnormous;
0336         break;
0337     }
0338 }
0339 
0340 void IconSizeDialog::choose(QListWidgetItem *)
0341 {
0342     okButton->animateClick();
0343 }
0344 
0345 void IconSizeDialog::slotCancel()
0346 {
0347     m_iconSize = -1;
0348 }
0349 
0350 /** class FontSizeCombo: */
0351 
0352 FontSizeCombo::FontSizeCombo(bool rw, bool withDefault, QWidget *parent)
0353     : KComboBox(rw, parent)
0354     , m_withDefault(withDefault)
0355 {
0356     if (m_withDefault)
0357         addItem(i18n("(Default)"));
0358 
0359     QFontDatabase fontDB;
0360     QList<int> sizes = fontDB.standardSizes();
0361     for (QList<int>::Iterator it = sizes.begin(); it != sizes.end(); ++it)
0362         addItem(QString::number(*it));
0363 
0364     //connect(this, &FontSizeCombo::activated, this, &FontSizeCombo::textChangedInCombo);
0365     connect(this, &FontSizeCombo::editTextChanged, this, &FontSizeCombo::textChangedInCombo);
0366 
0367     // TODO: 01617 void KFontSizeAction::setFontSize( int size )
0368 }
0369 
0370 FontSizeCombo::~FontSizeCombo()
0371 {
0372 }
0373 
0374 void FontSizeCombo::textChangedInCombo(const QString &text)
0375 {
0376     bool ok = false;
0377     int size = text.toInt(&ok);
0378     if (ok)
0379         Q_EMIT sizeChanged(size);
0380 }
0381 
0382 void FontSizeCombo::keyPressEvent(QKeyEvent *event)
0383 {
0384     if (event->key() == Qt::Key_Escape)
0385         Q_EMIT escapePressed();
0386     else if (event->key() == Qt::Key_Return)
0387         Q_EMIT returnPressed2();
0388     else
0389         KComboBox::keyPressEvent(event);
0390 }
0391 
0392 void FontSizeCombo::setFontSize(qreal size)
0393 {
0394     setItemText(currentIndex(), QString::number(size));
0395 
0396     // TODO: SEE KFontSizeAction::setFontSize( int size ) !!! for a more complete method!
0397 }
0398 
0399 qreal FontSizeCombo::fontSize()
0400 {
0401     bool ok = false;
0402     int size = currentText().toInt(&ok);
0403     if (ok)
0404         return size;
0405 
0406     size = currentText().toInt(&ok);
0407     if (ok)
0408         return size;
0409 
0410     return font().pointSize();
0411 }