File indexing completed on 2024-05-19 05:35:17

0001 //////////////////////////////////////////////////////////////////////////////
0002 // oxygenexceptionlistwidget.cpp
0003 // -------------------
0004 //
0005 // SPDX-FileCopyrightText: 2009 Hugo Pereira Da Costa <hugo.pereira@free.fr>
0006 //
0007 // SPDX-License-Identifier: MIT
0008 //////////////////////////////////////////////////////////////////////////////
0009 
0010 #include "oxygenexceptionlistwidget.h"
0011 #include "oxygenexceptiondialog.h"
0012 
0013 #include <KLocalizedString>
0014 
0015 #include <QIcon>
0016 #include <QMessageBox>
0017 #include <QPointer>
0018 
0019 //__________________________________________________________
0020 namespace Oxygen
0021 {
0022 //__________________________________________________________
0023 ExceptionListWidget::ExceptionListWidget(QWidget *parent)
0024     : QWidget(parent)
0025 {
0026     // ui
0027     m_ui.setupUi(this);
0028 
0029     // list
0030     m_ui.exceptionListView->setAllColumnsShowFocus(true);
0031     m_ui.exceptionListView->setRootIsDecorated(false);
0032     m_ui.exceptionListView->setSortingEnabled(false);
0033     m_ui.exceptionListView->setModel(&model());
0034     m_ui.exceptionListView->sortByColumn(ExceptionModel::ColumnType, Qt::AscendingOrder);
0035     m_ui.exceptionListView->setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Ignored));
0036 
0037     m_ui.moveUpButton->setIcon(QIcon::fromTheme(QStringLiteral("arrow-up")));
0038     m_ui.moveDownButton->setIcon(QIcon::fromTheme(QStringLiteral("arrow-down")));
0039     m_ui.addButton->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
0040     m_ui.removeButton->setIcon(QIcon::fromTheme(QStringLiteral("list-remove")));
0041     m_ui.editButton->setIcon(QIcon::fromTheme(QStringLiteral("edit-rename")));
0042 
0043     connect(m_ui.addButton, SIGNAL(clicked()), SLOT(add()));
0044     connect(m_ui.editButton, SIGNAL(clicked()), SLOT(edit()));
0045     connect(m_ui.removeButton, SIGNAL(clicked()), SLOT(remove()));
0046     connect(m_ui.moveUpButton, SIGNAL(clicked()), SLOT(up()));
0047     connect(m_ui.moveDownButton, SIGNAL(clicked()), SLOT(down()));
0048 
0049     connect(m_ui.exceptionListView, SIGNAL(activated(QModelIndex)), SLOT(edit()));
0050     connect(m_ui.exceptionListView, SIGNAL(clicked(QModelIndex)), SLOT(toggle(QModelIndex)));
0051     connect(m_ui.exceptionListView->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), SLOT(updateButtons()));
0052 
0053     updateButtons();
0054     resizeColumns();
0055 }
0056 
0057 //__________________________________________________________
0058 void ExceptionListWidget::setExceptions(const InternalSettingsList &exceptions)
0059 {
0060     model().set(exceptions);
0061     resizeColumns();
0062     setChanged(false);
0063 }
0064 
0065 //__________________________________________________________
0066 InternalSettingsList ExceptionListWidget::exceptions(void)
0067 {
0068     return model().get();
0069     setChanged(false);
0070 }
0071 
0072 //__________________________________________________________
0073 void ExceptionListWidget::updateButtons(void)
0074 {
0075     bool hasSelection(!m_ui.exceptionListView->selectionModel()->selectedRows().empty());
0076     m_ui.removeButton->setEnabled(hasSelection);
0077     m_ui.editButton->setEnabled(hasSelection);
0078 
0079     m_ui.moveUpButton->setEnabled(hasSelection && !m_ui.exceptionListView->selectionModel()->isRowSelected(0, QModelIndex()));
0080     m_ui.moveDownButton->setEnabled(hasSelection && !m_ui.exceptionListView->selectionModel()->isRowSelected(model().rowCount() - 1, QModelIndex()));
0081 }
0082 
0083 //_______________________________________________________
0084 void ExceptionListWidget::add(void)
0085 {
0086     QPointer<ExceptionDialog> dialog = new ExceptionDialog(this);
0087     dialog->setWindowTitle(i18n("New Exception - Oxygen Settings"));
0088     InternalSettingsPtr exception(new InternalSettings());
0089 
0090     exception->load();
0091 
0092     dialog->setException(exception);
0093 
0094     // run dialog and check existence
0095     if (!dialog->exec()) {
0096         delete dialog;
0097         return;
0098     }
0099 
0100     dialog->save();
0101     delete dialog;
0102 
0103     // check exceptions
0104     if (!checkException(exception))
0105         return;
0106 
0107     // create new item
0108     model().add(exception);
0109     setChanged(true);
0110 
0111     // make sure item is selected
0112     QModelIndex index(model().index(exception));
0113     if (index != m_ui.exceptionListView->selectionModel()->currentIndex()) {
0114         m_ui.exceptionListView->selectionModel()->select(index, QItemSelectionModel::Clear | QItemSelectionModel::Select | QItemSelectionModel::Rows);
0115         m_ui.exceptionListView->selectionModel()->setCurrentIndex(index, QItemSelectionModel::Current | QItemSelectionModel::Rows);
0116     }
0117 
0118     resizeColumns();
0119     return;
0120 }
0121 
0122 //_______________________________________________________
0123 void ExceptionListWidget::edit(void)
0124 {
0125     // retrieve selection
0126     QModelIndex current(m_ui.exceptionListView->selectionModel()->currentIndex());
0127     if (!model().contains(current))
0128         return;
0129 
0130     InternalSettingsPtr exception(model().get(current));
0131 
0132     // create dialog
0133     QPointer<ExceptionDialog> dialog(new ExceptionDialog(this));
0134     dialog->setWindowTitle(i18n("Edit Exception - Oxygen Settings"));
0135     dialog->setException(exception);
0136 
0137     // map dialog
0138     if (!dialog->exec()) {
0139         delete dialog;
0140         return;
0141     }
0142 
0143     // check modifications
0144     if (!dialog->isChanged())
0145         return;
0146 
0147     // retrieve exception
0148     dialog->save();
0149     delete dialog;
0150 
0151     // check new exception validity
0152     checkException(exception);
0153     resizeColumns();
0154 
0155     setChanged(true);
0156 
0157     return;
0158 }
0159 
0160 //_______________________________________________________
0161 void ExceptionListWidget::remove(void)
0162 {
0163     // confirmation dialog
0164     {
0165         QMessageBox messageBox(QMessageBox::Question,
0166                                i18n("Question - Oxygen Settings"),
0167                                i18n("Remove selected exception?"),
0168                                QMessageBox::Yes | QMessageBox::Cancel);
0169         messageBox.button(QMessageBox::Yes)->setText(i18n("Remove"));
0170         messageBox.setDefaultButton(QMessageBox::Cancel);
0171         if (messageBox.exec() == QMessageBox::Cancel)
0172             return;
0173     }
0174 
0175     // remove
0176     model().remove(model().get(m_ui.exceptionListView->selectionModel()->selectedRows()));
0177     resizeColumns();
0178     updateButtons();
0179 
0180     setChanged(true);
0181 
0182     return;
0183 }
0184 
0185 //_______________________________________________________
0186 void ExceptionListWidget::toggle(const QModelIndex &index)
0187 {
0188     if (!model().contains(index))
0189         return;
0190     if (index.column() != ExceptionModel::ColumnEnabled)
0191         return;
0192 
0193     // get matching exception
0194     InternalSettingsPtr exception(model().get(index));
0195     exception->setEnabled(!exception->enabled());
0196     setChanged(true);
0197     return;
0198 }
0199 
0200 //_______________________________________________________
0201 void ExceptionListWidget::up(void)
0202 {
0203     InternalSettingsList selection(model().get(m_ui.exceptionListView->selectionModel()->selectedRows()));
0204     if (selection.empty()) {
0205         return;
0206     }
0207 
0208     // retrieve selected indexes in list and store in model
0209     QModelIndexList selectedIndices(m_ui.exceptionListView->selectionModel()->selectedRows());
0210     InternalSettingsList selectedExceptions(model().get(selectedIndices));
0211 
0212     InternalSettingsList currentException(model().get());
0213     InternalSettingsList newExceptions;
0214 
0215     for (InternalSettingsList::const_iterator iter = currentException.constBegin(); iter != currentException.constEnd(); ++iter) {
0216         // check if new list is not empty, current index is selected and last index is not.
0217         // if yes, move.
0218         if (!(newExceptions.empty() || selectedIndices.indexOf(model().index(*iter)) == -1
0219               || selectedIndices.indexOf(model().index(newExceptions.back())) != -1)) {
0220             InternalSettingsPtr last(newExceptions.back());
0221             newExceptions.removeLast();
0222             newExceptions.append(*iter);
0223             newExceptions.append(last);
0224         } else
0225             newExceptions.append(*iter);
0226     }
0227 
0228     model().set(newExceptions);
0229 
0230     // restore selection
0231     m_ui.exceptionListView->selectionModel()->select(model().index(selectedExceptions.front()),
0232                                                      QItemSelectionModel::Clear | QItemSelectionModel::Select | QItemSelectionModel::Rows);
0233     for (InternalSettingsList::const_iterator iter = selectedExceptions.constBegin(); iter != selectedExceptions.constEnd(); ++iter) {
0234         m_ui.exceptionListView->selectionModel()->select(model().index(*iter), QItemSelectionModel::Select | QItemSelectionModel::Rows);
0235     }
0236 
0237     setChanged(true);
0238 
0239     return;
0240 }
0241 
0242 //_______________________________________________________
0243 void ExceptionListWidget::down(void)
0244 {
0245     InternalSettingsList selection(model().get(m_ui.exceptionListView->selectionModel()->selectedRows()));
0246     if (selection.empty()) {
0247         return;
0248     }
0249 
0250     // retrieve selected indexes in list and store in model
0251     QModelIndexList selectedIndices(m_ui.exceptionListView->selectionModel()->selectedIndexes());
0252     InternalSettingsList selectedExceptions(model().get(selectedIndices));
0253 
0254     InternalSettingsList currentExceptions(model().get());
0255     InternalSettingsList newExceptions;
0256 
0257     InternalSettingsListIterator iter(currentExceptions);
0258     iter.toBack();
0259     while (iter.hasPrevious()) {
0260         InternalSettingsPtr current(iter.previous());
0261 
0262         // check if new list is not empty, current index is selected and last index is not.
0263         // if yes, move.
0264         if (!(newExceptions.empty() || selectedIndices.indexOf(model().index(current)) == -1
0265               || selectedIndices.indexOf(model().index(newExceptions.front())) != -1)) {
0266             InternalSettingsPtr first(newExceptions.front());
0267             newExceptions.removeFirst();
0268             newExceptions.prepend(current);
0269             newExceptions.prepend(first);
0270 
0271         } else
0272             newExceptions.prepend(current);
0273     }
0274 
0275     model().set(newExceptions);
0276 
0277     // restore selection
0278     m_ui.exceptionListView->selectionModel()->select(model().index(selectedExceptions.front()),
0279                                                      QItemSelectionModel::Clear | QItemSelectionModel::Select | QItemSelectionModel::Rows);
0280     for (InternalSettingsList::const_iterator iter = selectedExceptions.constBegin(); iter != selectedExceptions.constEnd(); ++iter) {
0281         m_ui.exceptionListView->selectionModel()->select(model().index(*iter), QItemSelectionModel::Select | QItemSelectionModel::Rows);
0282     }
0283 
0284     setChanged(true);
0285 
0286     return;
0287 }
0288 
0289 //_______________________________________________________
0290 void ExceptionListWidget::resizeColumns(void) const
0291 {
0292     m_ui.exceptionListView->resizeColumnToContents(ExceptionModel::ColumnEnabled);
0293     m_ui.exceptionListView->resizeColumnToContents(ExceptionModel::ColumnType);
0294     m_ui.exceptionListView->resizeColumnToContents(ExceptionModel::ColumnRegExp);
0295 }
0296 
0297 //_______________________________________________________
0298 bool ExceptionListWidget::checkException(InternalSettingsPtr exception)
0299 {
0300     while (exception->exceptionPattern().isEmpty() || !QRegularExpression(exception->exceptionPattern()).isValid()) {
0301         QMessageBox::warning(this, i18n("Warning - Oxygen Settings"), i18n("Regular Expression syntax is incorrect"));
0302         QPointer<ExceptionDialog> dialog(new ExceptionDialog(this));
0303         dialog->setException(exception);
0304         if (dialog->exec() == QDialog::Rejected) {
0305             delete dialog;
0306             return false;
0307         }
0308 
0309         dialog->save();
0310         delete dialog;
0311     }
0312 
0313     return true;
0314 }
0315 }