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

0001 /**
0002  * SPDX-FileCopyrightText: (C) 2003 by Sébastien Laoût <slaout@linux62.org>
0003  * SPDX-License-Identifier: GPL-2.0-or-later
0004  */
0005 
0006 #include "basketproperties.h"
0007 
0008 #include <QApplication>
0009 #include <QButtonGroup>
0010 #include <QDialogButtonBox>
0011 #include <QGridLayout>
0012 #include <QGroupBox>
0013 #include <QHBoxLayout>
0014 #include <QLabel>
0015 #include <QLineEdit>
0016 #include <QLocale>
0017 #include <QPushButton>
0018 #include <QRadioButton>
0019 #include <QStyle>
0020 #include <QVBoxLayout>
0021 #include <QtCore/QStringList>
0022 #include <QtGui/QPixmap>
0023 
0024 #include <KComboBox>
0025 #include <KConfigGroup>
0026 #include <KIconDialog>
0027 #include <KIconLoader>
0028 #include <KLocalizedString>
0029 #include <KShortcutWidget>
0030 
0031 #include "backgroundmanager.h"
0032 #include "basketscene.h"
0033 #include "gitwrapper.h"
0034 #include "global.h"
0035 #include "kcolorcombo2.h"
0036 #include "variouswidgets.h"
0037 
0038 #include "ui_basketproperties.h"
0039 
0040 BasketPropertiesDialog::BasketPropertiesDialog(BasketScene *basket, QWidget *parent)
0041     : QDialog(parent)
0042     , Ui::BasketPropertiesUi()
0043     , m_basket(basket)
0044 {
0045     // Set up dialog options
0046     setWindowTitle(i18n("Basket Properties"));
0047     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Apply, this);
0048     QWidget *mainWidget = new QWidget(this);
0049     setupUi(mainWidget);
0050     QVBoxLayout *mainLayout = new QVBoxLayout;
0051     setLayout(mainLayout);
0052     mainLayout->addWidget(mainWidget);
0053     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0054     okButton->setDefault(true);
0055     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0056     connect(buttonBox, &QDialogButtonBox::accepted, this, &BasketPropertiesDialog::accept);
0057     connect(buttonBox, &QDialogButtonBox::rejected, this, &BasketPropertiesDialog::reject);
0058     mainLayout->addWidget(buttonBox);
0059     okButton->setDefault(true);
0060     setObjectName("BasketProperties");
0061     setModal(true);
0062 
0063     Ui::BasketPropertiesUi *propsUi = dynamic_cast<Ui::BasketPropertiesUi *>(this); // cast to remove name ambiguity
0064     propsUi->icon->setIconType(KIconLoader::NoGroup, KIconLoader::Application);
0065     propsUi->icon->setIconSize(16);
0066     propsUi->icon->setIcon(m_basket->icon());
0067 
0068     int size = qMax(propsUi->icon->sizeHint().width(), propsUi->icon->sizeHint().height());
0069     propsUi->icon->setFixedSize(size, size); // Make it square!
0070     propsUi->icon->setToolTip(i18n("Icon"));
0071     propsUi->name->setText(m_basket->basketName());
0072     propsUi->name->setMinimumWidth(propsUi->name->fontMetrics().maxWidth() * 20);
0073     propsUi->name->setToolTip(i18n("Name"));
0074 
0075     // Appearance:
0076     m_backgroundColor = new KColorCombo2(m_basket->backgroundColorSetting(), palette().color(QPalette::Base), appearanceGroup);
0077     m_textColor = new KColorCombo2(m_basket->textColorSetting(), palette().color(QPalette::Text), appearanceGroup);
0078 
0079     bgColorLbl->setBuddy(m_backgroundColor);
0080     txtColorLbl->setBuddy(m_textColor);
0081 
0082     appearanceLayout->addWidget(m_backgroundColor, 1, 2);
0083     appearanceLayout->addWidget(m_textColor, 2, 2);
0084 
0085     setTabOrder(backgroundImage, m_backgroundColor);
0086     setTabOrder(m_backgroundColor, m_textColor);
0087     setTabOrder(m_textColor, columnForm);
0088 
0089     backgroundImage->addItem(i18n("(None)"));
0090     m_backgroundImagesMap.insert(0, QString());
0091     backgroundImage->setIconSize(QSize(100, 75));
0092     QStringList backgrounds = Global::backgroundManager->imageNames();
0093     int index = 1;
0094     for (QStringList::Iterator it = backgrounds.begin(); it != backgrounds.end(); ++it) {
0095         QPixmap *preview = Global::backgroundManager->preview(*it);
0096         if (preview) {
0097             m_backgroundImagesMap.insert(index, *it);
0098             backgroundImage->insertItem(index, *it);
0099             backgroundImage->setItemData(index, *preview, Qt::DecorationRole);
0100             if (m_basket->backgroundImageName() == *it)
0101                 backgroundImage->setCurrentIndex(index);
0102             index++;
0103         }
0104     }
0105     //  m_backgroundImage->insertItem(i18n("Other..."), -1);
0106     int BUTTON_MARGIN = qApp->style()->pixelMetric(QStyle::PM_ButtonMargin);
0107     backgroundImage->setMaxVisibleItems(50 /*75 * 6 / m_backgroundImage->sizeHint().height()*/);
0108     backgroundImage->setMinimumHeight(75 + 2 * BUTTON_MARGIN);
0109 
0110     // Disposition:
0111 
0112     columnCount->setRange(1, 20);
0113     columnCount->setValue(m_basket->columnsCount());
0114     connect(columnCount, SIGNAL(valueChanged(int)), this, SLOT(selectColumnsLayout()));
0115     
0116     int height = qMax(mindMap->sizeHint().height(), columnCount->sizeHint().height()); // Make all radioButtons vertically equally-spaced!
0117     mindMap->setMinimumSize(mindMap->sizeHint().width(), height);                      // Because the m_columnCount can be higher, and make radio1 and radio2 more spaced than radio2 and radio3.
0118 
0119     if (!m_basket->isFreeLayout())
0120         columnForm->setChecked(true);
0121     else if (m_basket->isMindMap())
0122         mindMap->setChecked(true);
0123     else
0124         freeForm->setChecked(true);
0125 
0126     mindMap->hide();
0127 
0128     // Keyboard Shortcut:
0129     QList<QKeySequence> shortcuts {m_basket->shortcut()};
0130     shortcut->setShortcut(shortcuts);
0131 
0132     HelpLabel *helpLabel = new HelpLabel(i18n("Learn some tips..."),
0133                                          i18n("<p><strong>Easily Remember your Shortcuts</strong>:<br>"
0134                                               "With the first option, giving the basket a shortcut of the form <strong>Alt+Letter</strong> will underline that letter in the basket tree.<br>"
0135                                               "For instance, if you are assigning the shortcut <i>Alt+T</i> to a basket named <i>Tips</i>, the basket will be displayed as <i><u>T</u>ips</i> in the tree. "
0136                                               "It helps you visualize the shortcuts to remember them more quickly.</p>"
0137                                               "<p><strong>Local vs Global</strong>:<br>"
0138                                               "The first option allows you to show the basket while the main window is active. "
0139                                               "Global shortcuts are valid from anywhere, even if the window is hidden.</p>"
0140                                               "<p><strong>Show vs Switch</strong>:<br>"
0141                                               "The last option makes this basket the current one without opening the main window. "
0142                                               "It is useful in addition to the configurable global shortcuts, eg. to paste the clipboard or the selection into the current basket from anywhere.</p>"),
0143                                          nullptr);
0144 
0145     shortcutLayout->addWidget(helpLabel);
0146     connect(shortcut, &KShortcutWidget::shortcutChanged, this, &BasketPropertiesDialog::capturedShortcut);
0147 
0148     setTabOrder(columnCount, shortcut);
0149     setTabOrder(shortcut, helpLabel);
0150     setTabOrder(helpLabel, showBasket);
0151 
0152     switch (m_basket->shortcutAction()) {
0153     default:
0154     case 0:
0155         showBasket->setChecked(true);
0156         break;
0157     case 1:
0158         globalButton->setChecked(true);
0159         break;
0160     case 2:
0161         switchButton->setChecked(true);
0162         break;
0163     }
0164 
0165     // Connect the Ok and Apply buttons to actually apply the changes
0166     connect(okButton, &QPushButton::clicked, this, &BasketPropertiesDialog::applyChanges);
0167     connect(buttonBox->button(QDialogButtonBox::Apply), &QPushButton::clicked, this, &BasketPropertiesDialog::applyChanges);
0168 }
0169 
0170 BasketPropertiesDialog::~BasketPropertiesDialog()
0171 {
0172 }
0173 
0174 void BasketPropertiesDialog::ensurePolished()
0175 {
0176     QWidget::ensurePolished();
0177     Ui::BasketPropertiesUi *propsUi = dynamic_cast<Ui::BasketPropertiesUi *>(this);
0178     propsUi->name->setFocus();
0179 }
0180 
0181 void BasketPropertiesDialog::applyChanges()
0182 {
0183     if (columnForm->isChecked()) {
0184         m_basket->setDisposition(0, columnCount->value());
0185     } else if (freeForm->isChecked()) {
0186         m_basket->setDisposition(1, columnCount->value());
0187     } else {
0188         m_basket->setDisposition(2, columnCount->value());
0189     }
0190 
0191     if (showBasket->isChecked()) {
0192         m_basket->setShortcut(shortcut->shortcut()[0], 0);
0193     } else if (globalButton->isChecked()) {
0194         m_basket->setShortcut(shortcut->shortcut()[0], 1);
0195     } else if (switchButton->isChecked()) {
0196         m_basket->setShortcut(shortcut->shortcut()[0], 2);
0197     }
0198 
0199     Ui::BasketPropertiesUi *propsUi = dynamic_cast<Ui::BasketPropertiesUi *>(this);
0200     // Should be called LAST, because it will emit the propertiesChanged() signal and the tree will be able to show the newly set Alt+Letter shortcut:
0201     m_basket->setAppearance(propsUi->icon->icon(), propsUi->name->text(), m_backgroundImagesMap[backgroundImage->currentIndex()], m_backgroundColor->color(), m_textColor->color());
0202     GitWrapper::commitBasket(m_basket);
0203     m_basket->save();
0204 }
0205 
0206 void BasketPropertiesDialog::capturedShortcut(const QList<QKeySequence> &sc)
0207 {
0208     // TODO: Validate it!
0209     shortcut->setShortcut(sc);
0210 }
0211 
0212 void BasketPropertiesDialog::selectColumnsLayout()
0213 {
0214     columnForm->setChecked(true);
0215 }