File indexing completed on 2024-05-19 04:07:47

0001 /*
0002     SPDX-FileCopyrightText: 2010 Stefan Majewsky <majewsky@gmx.net>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "configdialog.h"
0008 #include "configdialog_p.h"
0009 #include "triggerconfigwidget.h"
0010 #include "../engine/texturehelper.h"
0011 #include "settings.h"
0012 #include <QIcon>
0013 #include <QTimer>
0014 
0015 //BEGIN Palapeli::TriggerComboBox
0016 
0017 Palapeli::TriggerComboBox::TriggerComboBox(QWidget* parent)
0018     : KComboBox(parent)
0019 {
0020   connect(this, &QComboBox::currentIndexChanged, this, &TriggerComboBox::handleCurrentIndexChanged);
0021 }
0022 
0023 QString Palapeli::TriggerComboBox::backgroundKey() const
0024 {
0025     return itemData(currentIndex(), Palapeli::TextureHelper::IdentifierRole).toString();
0026 }
0027 
0028 void Palapeli::TriggerComboBox::setBackgroundKey(const QString& backgroundKey)
0029 {
0030     int index = findData(backgroundKey, Palapeli::TextureHelper::IdentifierRole);
0031     if (index > -1)
0032         setCurrentIndex(index);
0033 }
0034 
0035 void Palapeli::TriggerComboBox::handleCurrentIndexChanged(int index)
0036 {
0037     Q_UNUSED(index)
0038     const QString key = backgroundKey();
0039     Q_EMIT backgroundKeyChanged(key);
0040     Q_EMIT itemTypeChanged(key == QLatin1String("__color__"));
0041 }
0042 
0043 //END Palapeli::TriggerComboBox
0044 //BEGIN Palapeli::ConfigDialog
0045 
0046 Palapeli::ConfigDialog::ConfigDialog(QWidget* parent)
0047     : KConfigDialog(parent, QString(), Settings::self())
0048     , m_triggerPage(new Palapeli::TriggerConfigWidget)
0049     , m_shownForFirstTime(false)
0050 {
0051     //setup page "General Settings"
0052     QWidget* generalPage = new QWidget;
0053     m_generalUi.setupUi(generalPage);
0054 
0055     // Construction of the TextureHelper singleton also loads all Palapeli
0056     // settings or defaults and the combobox contents for ViewBackground.
0057     m_generalUi.kcfg_ViewBackground->setModel(
0058                     Palapeli::TextureHelper::instance());
0059     setupSolutionAreaComboBox();
0060 
0061     addPage(generalPage, i18n("General Settings"))->
0062                 setIcon(QIcon::fromTheme( QStringLiteral( "configure" )));
0063     //setup page "Mouse Interaction"
0064     addPage(m_triggerPage, i18n("Mouse Interaction"))->
0065                 setIcon(QIcon::fromTheme( QStringLiteral( "input-mouse" )));
0066     connect(m_triggerPage, &TriggerConfigWidget::associationsChanged,
0067             this, &ConfigDialog::updateButtons);
0068 }
0069 
0070 bool Palapeli::ConfigDialog::hasChanged()
0071 {
0072     return m_triggerPage->hasChanged();
0073 }
0074 
0075 bool Palapeli::ConfigDialog::isDefault()
0076 {
0077     return m_triggerPage->isDefault();
0078 }
0079 
0080 void Palapeli::ConfigDialog::updateSettings()
0081 {
0082     //schedule update of TextureHelper (but only after KConfigDialog has written the settings, which might happen after this slot call)
0083     QTimer::singleShot(0, Palapeli::TextureHelper::instance(), &TextureHelper::readSettings);
0084     m_triggerPage->updateSettings();
0085 }
0086 
0087 void Palapeli::ConfigDialog::updateWidgets()
0088 {
0089     m_triggerPage->updateWidgets();
0090 }
0091 
0092 void Palapeli::ConfigDialog::updateWidgetsDefault()
0093 {
0094     m_triggerPage->updateWidgetsDefault();
0095 }
0096 
0097 void Palapeli::ConfigDialog::showEvent(QShowEvent* event)
0098 {
0099     KConfigDialog::showEvent(event);
0100     //the dialog is usually created a bit small
0101     if (!m_shownForFirstTime)
0102     {
0103         resize(minimumSize().expandedTo(geometry().size()) + QSize(50, 100));
0104         m_shownForFirstTime = true;
0105     }
0106 }
0107 
0108 void Palapeli::ConfigDialog::setupSolutionAreaComboBox()
0109 {
0110     QComboBox* b = m_generalUi.kcfg_SolutionArea;
0111     b->insertItem(Center,      i18n("Center"),       Center);
0112     b->insertItem(None,        i18n("None"),         None);
0113     b->insertItem(TopLeft,     i18n("Top Left"),     TopLeft);
0114     b->insertItem(TopRight,    i18n("Top Right"),    TopRight);
0115     b->insertItem(BottomLeft,  i18n("Bottom Left"),  BottomLeft);
0116     b->insertItem(BottomRight, i18n("Bottom Right"), BottomRight);
0117     b->setCurrentIndex(Settings::solutionArea());
0118         connect(b, &QComboBox::currentIndexChanged, this, &ConfigDialog::solutionAreaChange);
0119 }
0120 
0121 void Palapeli::ConfigDialog::solutionAreaChange(int index)
0122 {
0123     // Play safe by providing this slot. KConfigSkeleton seems to save the
0124     // index setting if .kcfg says "Int", but it does not officially support
0125     // the QComboBox type.
0126     Settings::setSolutionArea(index);
0127     Settings::self()->save();
0128 }
0129 
0130 //END Palapeli::ConfigDialog
0131 
0132 
0133 //
0134 
0135 #include "moc_configdialog.cpp"
0136 #include "moc_configdialog_p.cpp"