File indexing completed on 2024-04-28 16:44:48

0001 /* SPDX-FileCopyrightText: 2009 Michael Jansen <kde@michael-jansen.biz>
0002 
0003    SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 #include "window_definition_list_widget.h"
0007 #include "window_definition_widget.h"
0008 #include "windows_helper/window_selection_rules.h"
0009 
0010 #include "kdebug.h"
0011 
0012 WindowDefinitionListWidget::WindowDefinitionListWidget(QWidget *parent)
0013     : HotkeysWidgetIFace(parent)
0014     , _windowdefs(nullptr)
0015     , _working(nullptr)
0016     , _changed(false)
0017 {
0018     ui.setupUi(this);
0019 
0020     connect(ui.edit_button, SIGNAL(clicked(bool)), SLOT(slotEdit(bool)));
0021 
0022     connect(ui.delete_button, SIGNAL(clicked(bool)), SLOT(slotDelete(bool)));
0023 
0024     connect(ui.duplicate_button, SIGNAL(clicked(bool)), SLOT(slotDuplicate(bool)));
0025 
0026     connect(ui.new_button, SIGNAL(clicked(bool)), SLOT(slotNew(bool)));
0027 }
0028 
0029 WindowDefinitionListWidget::WindowDefinitionListWidget(KHotKeys::Windowdef_list *windowdef, QWidget *parent)
0030     : HotkeysWidgetIFace(parent)
0031     , _windowdefs(nullptr)
0032     , _working(nullptr)
0033     , _changed(false)
0034 {
0035     ui.setupUi(this);
0036 
0037     setWindowDefinitions(windowdef);
0038 
0039     connect(ui.edit_button, SIGNAL(clicked(bool)), SLOT(slotEdit(bool)));
0040 
0041     connect(ui.delete_button, SIGNAL(clicked(bool)), SLOT(slotDelete(bool)));
0042 
0043     connect(ui.duplicate_button, SIGNAL(clicked(bool)), SLOT(slotDuplicate(bool)));
0044 
0045     connect(ui.new_button, SIGNAL(clicked(bool)), SLOT(slotNew(bool)));
0046 }
0047 
0048 WindowDefinitionListWidget::~WindowDefinitionListWidget()
0049 {
0050     delete _working;
0051 }
0052 
0053 void WindowDefinitionListWidget::doCopyFromObject()
0054 {
0055     // We are asked to copy again from object. Recreate our working copy
0056     if (_working)
0057         delete _working;
0058     _working = _windowdefs->copy();
0059 
0060     ui.comment->setText(_working->comment());
0061 
0062     for (KHotKeys::Windowdef_list::ConstIterator it(_working->constBegin()); it != _working->constEnd(); ++it) {
0063         new QListWidgetItem((*it)->description(), ui.list);
0064     }
0065 
0066     emitChanged(false);
0067 }
0068 
0069 void WindowDefinitionListWidget::doCopyToObject()
0070 {
0071     // Delete the old content
0072     qDeleteAll(*_windowdefs);
0073     _windowdefs->clear();
0074 
0075     _windowdefs->set_comment(ui.comment->text());
0076 
0077     for (int i = 0; i < _working->size(); ++i) {
0078         _windowdefs->append(_working->at(i)->copy());
0079     }
0080 
0081     // Reset our _changed state
0082     _changed = false;
0083     emitChanged(false);
0084 }
0085 
0086 void WindowDefinitionListWidget::emitChanged(bool chgd)
0087 {
0088     if (_changed == chgd)
0089         return;
0090 
0091     // emitChanged will never reset _changed to false because we have
0092     // currently no way to compare the contents of _working and _windowdefs.
0093     // That's why we say once changed -> always changed.
0094     _changed = chgd || _changed;
0095 
0096     emit changed(_changed);
0097 }
0098 
0099 bool WindowDefinitionListWidget::isChanged() const
0100 {
0101     return _changed;
0102 }
0103 
0104 void WindowDefinitionListWidget::slotDelete(bool)
0105 {
0106     // TODO: Deactivate buttons if nothing is selected
0107     if (ui.list->currentRow() == -1)
0108         return;
0109 
0110     Q_ASSERT(ui.list->currentRow() < _working->count());
0111 
0112     KHotKeys::Windowdef *def = _working->at(ui.list->currentRow());
0113     KHotKeys::Windowdef_simple *sim = dynamic_cast<KHotKeys::Windowdef_simple *>(def);
0114     Q_ASSERT(sim);
0115 
0116     // Remove it from the list
0117     ui.list->takeItem(ui.list->currentRow());
0118 
0119     // delete it
0120     _working->removeAll(sim);
0121     delete sim;
0122 
0123     emitChanged(true);
0124 
0125     return;
0126 }
0127 
0128 void WindowDefinitionListWidget::slotDuplicate(bool)
0129 {
0130     // TODO: Deactivate buttons if nothing is selected
0131     if (ui.list->currentRow() == -1)
0132         return;
0133 
0134     Q_ASSERT(ui.list->currentRow() < _working->count());
0135 
0136     // Get the template
0137     KHotKeys::Windowdef *def = _working->at(ui.list->currentRow());
0138     KHotKeys::Windowdef_simple *orig = dynamic_cast<KHotKeys::Windowdef_simple *>(def);
0139     Q_ASSERT(orig);
0140 
0141     // Create a copy
0142     KHotKeys::Windowdef_simple *sim = orig->copy();
0143     Q_ASSERT(sim);
0144 
0145     WindowDefinitionDialog dialog(sim, this);
0146     switch (dialog.exec()) {
0147     case QDialog::Accepted:
0148         // Update our list if necessary
0149         new QListWidgetItem(sim->description(), ui.list);
0150         _working->append(sim);
0151         emitChanged(true);
0152         break;
0153 
0154     case QDialog::Rejected:
0155         // Nothing to do
0156         delete sim;
0157         break;
0158 
0159     default:
0160         Q_ASSERT(false);
0161         delete sim;
0162     }
0163 }
0164 
0165 void WindowDefinitionListWidget::slotEdit(bool)
0166 {
0167     // TODO: Deactivate buttons if nothing is selected
0168     if (ui.list->currentRow() == -1)
0169         return;
0170 
0171     Q_ASSERT(ui.list->currentRow() < _working->count());
0172 
0173     QListWidgetItem *item = ui.list->currentItem();
0174     KHotKeys::Windowdef *def = _working->at(ui.list->currentRow());
0175     KHotKeys::Windowdef_simple *sim = dynamic_cast<KHotKeys::Windowdef_simple *>(def);
0176 
0177     Q_ASSERT(sim);
0178     if (!sim)
0179         return;
0180 
0181     WindowDefinitionDialog dialog(sim, this);
0182     switch (dialog.exec()) {
0183     case QDialog::Accepted:
0184         // Update our list if necessary
0185         item->setText(sim->description());
0186         emitChanged(true);
0187         break;
0188 
0189     case QDialog::Rejected:
0190         // Nothing to do
0191         break;
0192 
0193     default:
0194         Q_ASSERT(false);
0195     }
0196 }
0197 
0198 void WindowDefinitionListWidget::slotNew(bool)
0199 {
0200     KHotKeys::Windowdef_simple *sim = new KHotKeys::Windowdef_simple();
0201 
0202     WindowDefinitionDialog dialog(sim, this);
0203     switch (dialog.exec()) {
0204     case QDialog::Accepted:
0205         // Update our list if necessary
0206         new QListWidgetItem(sim->description(), ui.list);
0207         _working->append(sim);
0208         emitChanged(true);
0209         break;
0210 
0211     case QDialog::Rejected:
0212         // Nothing to do
0213         delete sim;
0214         break;
0215 
0216     default:
0217         Q_ASSERT(false);
0218         delete sim;
0219     }
0220 }
0221 
0222 void WindowDefinitionListWidget::setWindowDefinitions(KHotKeys::Windowdef_list *list)
0223 {
0224     Q_ASSERT(list);
0225     _windowdefs = list;
0226 }
0227 
0228 #include "moc_window_definition_list_widget.cpp"