File indexing completed on 2024-04-28 05:52:07

0001 /*
0002     SPDX-FileCopyrightText: 2007-2009 Stefan Böhmann <kde@hilefoks.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #include "settings.h"
0007 
0008 #include "toplevel.h"
0009 #include "tealistmodel.h"
0010 
0011 #include <QDialogButtonBox>
0012 #include <QScreen>
0013 #include <QHashIterator>
0014 #include <QPushButton>
0015 #include <QString>
0016 #include <QVBoxLayout>
0017 
0018 #include <KConfigGroup>
0019 #include <KHelpClient>
0020 #include <KLocalizedString>
0021 #include <KSharedConfig>
0022 
0023 class SettingsUI : public QWidget, public Ui::SettingsWidget
0024 {
0025 
0026     public:
0027         explicit SettingsUI(QWidget *parent = nullptr)
0028           : QWidget( parent )
0029         {
0030             setupUi( this );
0031         }
0032 };
0033 
0034 SettingsDialog::SettingsDialog(TopLevel *toplevel, const QList<Tea> &teas)
0035   : QDialog()
0036   , mUi(new SettingsUI( this ))
0037   , m_toplevel(toplevel)
0038 
0039 {
0040     setWindowTitle( i18n( "Configure Tea Cooker" ) );
0041 
0042     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel|QDialogButtonBox::Help, this);
0043     QVBoxLayout *mainLayout = new QVBoxLayout(this);
0044     mainLayout->addWidget(mUi);
0045     buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
0046     buttonBox->button(QDialogButtonBox::Ok)->setShortcut(Qt::CTRL | Qt::Key_Return);
0047     connect(buttonBox, &QDialogButtonBox::accepted, this, &SettingsDialog::accept);
0048     connect(buttonBox, &QDialogButtonBox::rejected, this, &SettingsDialog::reject);
0049     mainLayout->addWidget(buttonBox);
0050 
0051     connect(buttonBox, &QDialogButtonBox::helpRequested, this, &SettingsDialog::showHelp);
0052 
0053     buttonBox->button(QDialogButtonBox::Ok)->setWhatsThis(i18n( "Save changes and close dialog."  ));
0054     buttonBox->button(QDialogButtonBox::Cancel)->setWhatsThis(i18n( "Close dialog without saving changes."  ));
0055     buttonBox->button(QDialogButtonBox::Help)->setWhatsThis(i18n( "Show help page for this dialog."  ));
0056 
0057     KSharedConfigPtr config = KSharedConfig::openConfig();
0058     KConfigGroup group( config, QStringLiteral("General") );
0059 
0060     restoreGeometry(group.readEntry<QByteArray>("Geometry", QByteArray()));
0061 
0062     const QSize desktopSize = qApp->primaryScreen()->size();
0063     int x=group.readEntry( "SettingsDialogXPos", desktopSize.width()/2 - width()/2 );
0064     int y=group.readEntry( "SettingsDialogYPos", desktopSize.height()/2 - height()/2 );
0065 
0066     x = qMin( qMax( 0, x ), desktopSize.width() - width() );
0067     x = qMin( qMax( 0, y ), desktopSize.height() - height() );
0068     move( QPoint( x, y ) );
0069 
0070     bool popup=group.readEntry( "UsePopup", true );
0071     bool autohide=group.readEntry( "PopupAutoHide", false );
0072     int autohidetime=group.readEntry( "PopupAutoHideTime", 30 );
0073     bool reminder=group.readEntry( "UseReminder", false );
0074     int remindertime=group.readEntry( "ReminderTime", 60 );
0075     bool vis=group.readEntry( "UseVisualize", true );
0076 
0077     mUi->popupCheckBox->setChecked( popup );
0078     mUi->autohideCheckBox->setChecked( autohide );
0079     mUi->reminderCheckBox->setChecked( reminder );
0080     mUi->visualizeCheckBox->setChecked( vis );
0081 
0082     mUi->autohideSpinBox->setValue( autohidetime );
0083     mUi->reminderSpinBox->setValue( remindertime );
0084     mUi->autohideSpinBox->setSuffix( ki18ncp( "Auto hide popup after", " second", " seconds") );
0085     mUi->reminderSpinBox->setSuffix( ki18ncp( "Reminder every", " second", " seconds") );
0086 
0087     mUi->autohideCheckBox->setEnabled( popup );
0088     mUi->autohideSpinBox->setEnabled( autohide );
0089     mUi->reminderSpinBox->setEnabled( reminder );
0090 
0091     m_model=new TeaListModel( teas, this );
0092     mUi->tealistTreeView->setModel( m_model );
0093 
0094     connect(mUi->tealistTreeView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &SettingsDialog::updateSelection);
0095 
0096     mUi->removeButton->setEnabled( false );
0097     mUi->upButton->setEnabled( false );
0098     mUi->downButton->setEnabled( false );
0099 
0100     mUi->newButton->setIcon( QIcon::fromTheme( QLatin1String(  "list-add" ) ) );
0101     mUi->removeButton->setIcon( QIcon::fromTheme( QLatin1String(  "edit-delete" ) ) );
0102     mUi->upButton->setIcon( QIcon::fromTheme( QLatin1String(  "arrow-up" ) ) );
0103     mUi->downButton->setIcon( QIcon::fromTheme( QLatin1String(  "arrow-down" ) ) );
0104 
0105     connect(mUi->popupCheckBox, &QCheckBox::toggled, this, &SettingsDialog::checkPopupButtonState);
0106 
0107     connect(mUi->newButton, &QToolButton::clicked, this, &SettingsDialog::newButtonClicked);
0108     connect(mUi->removeButton, &QToolButton::clicked, this, &SettingsDialog::removeButtonClicked);
0109     connect(mUi->upButton, &QToolButton::clicked, this, &SettingsDialog::upButtonClicked);
0110     connect(mUi->downButton, &QToolButton::clicked, this, &SettingsDialog::downButtonClicked);
0111 
0112     connect(mUi->teaNameEdit, &QLineEdit::textChanged, this, &SettingsDialog::nameValueChanged);
0113     connect(mUi->minutesSpin, &QSpinBox::valueChanged, this, &SettingsDialog::timeValueChanged);
0114     connect(mUi->secondsSpin, &QSpinBox::valueChanged, this, &SettingsDialog::timeValueChanged);
0115 }
0116 
0117 SettingsDialog::~SettingsDialog()
0118 {
0119     delete m_model;
0120     delete mUi;
0121 }
0122 
0123 void SettingsDialog::showHelp()
0124 {
0125     KHelpClient::invokeHelp();
0126 }
0127 
0128 void SettingsDialog::accept()
0129 {
0130     KSharedConfigPtr config = KSharedConfig::openConfig();
0131     KConfigGroup group( config, QStringLiteral("General") );
0132     group.writeEntry( "SettingsDialogXPos", x() );
0133     group.writeEntry( "SettingsDialogYPos", y() );
0134 
0135     hide();
0136     group.writeEntry("Geometry", saveGeometry());
0137 
0138     group.writeEntry( "UsePopup",          mUi->popupCheckBox->checkState() == Qt::Checked );
0139     group.writeEntry( "PopupAutoHide",     mUi->autohideCheckBox->checkState() == Qt::Checked );
0140     group.writeEntry( "PopupAutoHideTime", mUi->autohideSpinBox->value() );
0141     group.writeEntry( "UseReminder",       mUi->reminderCheckBox->checkState() == Qt::Checked );
0142     group.writeEntry( "ReminderTime",      mUi->reminderSpinBox->value() );
0143     group.writeEntry( "UseVisualize",      mUi->visualizeCheckBox->checkState() == Qt::Checked );
0144 
0145     config->sync();
0146     m_toplevel->setTeaList( m_model->getTeaList() );
0147 }
0148 
0149 
0150 void SettingsDialog::checkPopupButtonState(bool b) {
0151     mUi->autohideCheckBox->setEnabled( b );
0152 
0153     if( !b ) {
0154         mUi->autohideSpinBox->setEnabled( b );
0155     }
0156     else if( mUi->autohideCheckBox->checkState() == 2 ) {
0157         mUi->autohideSpinBox->setEnabled( b );
0158     }
0159 }
0160 
0161 void SettingsDialog::newButtonClicked()
0162 {
0163     int count = m_model->rowCount();
0164     m_model->insertRows( count, 1 );
0165 
0166     QItemSelectionModel *sm = mUi->tealistTreeView->selectionModel();
0167     QItemSelection selection( m_model->index( count, 0 ), m_model->index( count, 1 ) );
0168     sm->select( selection, QItemSelectionModel::Clear | QItemSelectionModel::Select );
0169 }
0170 
0171 
0172 void SettingsDialog::removeButtonClicked()
0173 {
0174     const QModelIndexList indexes = mUi->tealistTreeView->selectionModel()->selectedIndexes();
0175 
0176     for (const QModelIndex &index : indexes) {
0177         // Only delete a row when column==0, otherwise the row will be delete
0178         // multiple times (the loop iterate over every cell, not over rows).
0179         if( index.column() == 0 ) {
0180             m_model->removeRows( index.row(), 1 );
0181         }
0182     }
0183 }
0184 
0185 
0186 void SettingsDialog::upButtonClicked()
0187 {
0188     moveSelectedItem( true );
0189 }
0190 
0191 
0192 void SettingsDialog::downButtonClicked()
0193 {
0194     moveSelectedItem( false );
0195 }
0196 
0197 
0198 void SettingsDialog::moveSelectedItem(bool moveup)
0199 {
0200     QItemSelectionModel *sm = mUi->tealistTreeView->selectionModel();
0201     QModelIndexList items = sm->selection().indexes();
0202 
0203     if( !items.isEmpty() ) {
0204         QString name = m_model->data( m_model->index( items.at(0).row(), 0 ), Qt::EditRole).toString();
0205         unsigned time = m_model->data( m_model->index( items.at(0).row(), 1 ), Qt::EditRole).toUInt();
0206         int pos = items.at(0).row();
0207 
0208         moveup ? --pos : ++pos;
0209 
0210         removeButtonClicked();
0211 
0212         m_model->insertRows( pos, 1 );
0213         m_model->setData( m_model->index( pos, 0 ), name, Qt::EditRole );
0214         m_model->setData( m_model->index( pos, 1 ), time, Qt::EditRole );
0215 
0216         QItemSelection selection( m_model->index( pos, 0 ), m_model->index( pos, 1 ) );
0217         sm->select( selection, QItemSelectionModel::Clear | QItemSelectionModel::Select );
0218     }
0219 }
0220 
0221 
0222 void SettingsDialog::updateSelection(const QItemSelection &selected, const QItemSelection &deselected)
0223 {
0224     Q_UNUSED(deselected)
0225     QModelIndexList items = selected.indexes();
0226 
0227     QString name;
0228     unsigned time=0;
0229 
0230     bool state = !items.isEmpty();
0231 
0232     mUi->teaPropertiesGroup->setEnabled( state );
0233     mUi->teaNameEdit->setEnabled( state );
0234     mUi->minutesSpin->setEnabled( state );
0235     mUi->secondsSpin->setEnabled( state );
0236     mUi->removeButton->setEnabled( state );
0237 
0238     if( state ) {
0239         name = m_model->data( m_model->index( items.at(0).row(), 0 ), Qt::EditRole ).toString();
0240         time = m_model->data( m_model->index( items.at(0).row(), 1 ), Qt::EditRole ).toUInt();
0241 
0242         mUi->upButton->setEnabled( items.at(0).row() > 0 );
0243         mUi->downButton->setEnabled( items.at(0).row() < ( m_model->rowCount() - 1 ) );
0244     }
0245     else {
0246         mUi->upButton->setEnabled( false );
0247         mUi->downButton->setEnabled( false );
0248     }
0249 
0250     mUi->teaNameEdit->setText( name );
0251     mUi->minutesSpin->setValue( time / 60 );
0252     mUi->secondsSpin->setValue( time % 60 );
0253 }
0254 
0255 
0256 void SettingsDialog::timeValueChanged()
0257 {
0258     QModelIndexList items = mUi->tealistTreeView->selectionModel()->selection().indexes();
0259 
0260     if( !items.isEmpty() ) {
0261         int time = mUi->secondsSpin->value();
0262         time += mUi->minutesSpin->value() * 60;
0263 
0264         if( time <= 0 ) {
0265             time = 1;
0266             mUi->secondsSpin->setValue( time );
0267         }
0268         m_model->setData( m_model->index( items.at(0).row(), 1 ), time, Qt::EditRole );
0269     }
0270 }
0271 
0272 
0273 void SettingsDialog::nameValueChanged(const QString &text)
0274 {
0275     QModelIndexList items = mUi->tealistTreeView->selectionModel()->selection().indexes();
0276 
0277     if( !items.isEmpty() ) {
0278         m_model->setData( m_model->index( items.at(0).row(), 0 ), text, Qt::EditRole );
0279     }
0280 }
0281 
0282 // kate: word-wrap off; encoding utf-8; indent-width 4; tab-width 4; line-numbers on; mixed-indent off; remove-trailing-space-save on; replace-tabs-save on; replace-tabs on; space-indent on;
0283 // vim:set spell et sw=4 ts=4 nowrap cino=l1,cs,U1: