File indexing completed on 2024-04-21 04:57:00

0001 /***************************************************************************
0002  *   Copyright (C) 2010 Matthias Fuchs <mat69@gmx.net>                     *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  *                                                                         *
0009  *   This program is distributed in the hope that it will be useful,       *
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0012  *   GNU General Public License for more details.                          *
0013  *                                                                         *
0014  *   You should have received a copy of the GNU General Public License     *
0015  *   along with this program; if not, write to the                         *
0016  *   Free Software Foundation, Inc.,                                       *
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
0018  ***************************************************************************/
0019 
0020 #include "integrationpreferences.h"
0021 #include "autopastemodel.h"
0022 #include "settings.h"
0023 
0024 #include <KConfigDialog>
0025 #include <KGuiItem>
0026 #include <KLocalizedString>
0027 #include <KStandardGuiItem>
0028 
0029 IntegrationPreferences::IntegrationPreferences(KConfigDialog *parent, Qt::WindowFlags f)
0030     : QWidget(parent, f)
0031 {
0032     ui.setupUi(this);
0033 
0034     // AutoPaste stuff
0035     ui.type->addItem(QIcon::fromTheme("list-add"), i18n("Include"), AutoPasteModel::Include);
0036     ui.type->addItem(QIcon::fromTheme("list-remove"), i18n("Exclude"), AutoPasteModel::Exclude);
0037 
0038     ui.patternSyntax->addItem(i18n("Escape sequences"), AutoPasteModel::Wildcard);
0039     ui.patternSyntax->addItem(i18n("Regular expression"), AutoPasteModel::RegExp);
0040 
0041     KGuiItem::assign(ui.add, KStandardGuiItem::add());
0042     KGuiItem::assign(ui.remove, KStandardGuiItem::remove());
0043     ui.increase->setIcon(QIcon::fromTheme("arrow-up"));
0044     ui.decrease->setIcon(QIcon::fromTheme("arrow-down"));
0045 
0046     m_model = new AutoPasteModel(this);
0047     m_model->load();
0048     ui.list->setModel(m_model);
0049     auto *delegate = new AutoPasteDelegate(ui.type->model(), ui.patternSyntax->model(), this);
0050     ui.list->setItemDelegate(delegate);
0051 
0052     QByteArray loadedState = QByteArray::fromBase64(Settings::autoPasteHeaderState().toLatin1());
0053     if (Settings::autoPasteHeaderState().isEmpty()) {
0054         ui.list->resizeColumnToContents(AutoPasteModel::Type);
0055     } else if (!loadedState.isNull()) {
0056         ui.list->header()->restoreState(loadedState);
0057     }
0058 
0059     connect(m_model, &AutoPasteModel::dataChanged, this, &IntegrationPreferences::changed);
0060     connect(ui.list->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), this, SLOT(slotUpdateButtons()));
0061     connect(ui.pattern, &KLineEdit::textChanged, this, &IntegrationPreferences::slotUpdateButtons);
0062     connect(ui.pattern, &KLineEdit::returnPressed, this, &IntegrationPreferences::slotAddItem);
0063     connect(ui.add, &QPushButton::clicked, this, &IntegrationPreferences::slotAddItem);
0064     connect(ui.remove, &QPushButton::clicked, this, &IntegrationPreferences::slotRemoveItem);
0065     connect(ui.increase, &QPushButton::clicked, this, &IntegrationPreferences::slotIncreasePriority);
0066     connect(ui.decrease, &QPushButton::clicked, this, &IntegrationPreferences::slotDecreasePriority);
0067     connect(parent, SIGNAL(rejected()), m_model, SLOT(load()));
0068     connect(parent, SIGNAL(settingsChanged(QString)), m_model, SLOT(save()));
0069     connect(parent, SIGNAL(settingsChanged(QString)), m_model, SLOT(save()));
0070     connect(parent, SIGNAL(resetDefaults()), m_model, SLOT(resetDefaults()));
0071 
0072     slotUpdateButtons();
0073 }
0074 
0075 IntegrationPreferences::~IntegrationPreferences()
0076 {
0077 }
0078 
0079 void IntegrationPreferences::slotUpdateButtons()
0080 {
0081     ui.add->setEnabled(!ui.pattern->text().isEmpty());
0082     ui.remove->setEnabled(ui.list->selectionModel()->hasSelection());
0083 
0084     const QModelIndex index = ui.list->currentIndex();
0085     const bool indexValid = index.isValid() && (ui.list->selectionModel()->selectedRows().count() == 1);
0086     ui.increase->setEnabled(indexValid && (index.row() > 0));
0087     ui.decrease->setEnabled(indexValid && (m_model->rowCount() > (index.row() + 1)));
0088 }
0089 
0090 void IntegrationPreferences::slotAddItem()
0091 {
0092     const QString pattern = ui.pattern->text();
0093     if (pattern.isEmpty()) {
0094         return;
0095     }
0096 
0097     AutoPasteModel::TypeData type = static_cast<AutoPasteModel::TypeData>(ui.type->itemData(ui.type->currentIndex()).toInt());
0098     AutoPasteModel::PatternSyntaxData syntax =
0099         static_cast<AutoPasteModel::PatternSyntaxData>(ui.patternSyntax->itemData(ui.patternSyntax->currentIndex()).toInt());
0100     m_model->addItem(type, syntax, pattern);
0101 
0102     ui.pattern->clear();
0103     ui.pattern->setFocus();
0104     Q_EMIT changed();
0105 }
0106 
0107 void IntegrationPreferences::slotRemoveItem()
0108 {
0109     QItemSelectionModel *selection = ui.list->selectionModel();
0110     if (selection->hasSelection()) {
0111         while (selection->selectedRows().count()) {
0112             const QModelIndex index = selection->selectedRows().first();
0113             m_model->removeRow(index.row());
0114         }
0115         Q_EMIT changed();
0116     }
0117 }
0118 
0119 void IntegrationPreferences::slotIncreasePriority()
0120 {
0121     const int row = ui.list->currentIndex().row();
0122     m_model->moveItem(row, row - 1);
0123     slotUpdateButtons();
0124     Q_EMIT changed();
0125 }
0126 
0127 void IntegrationPreferences::slotDecreasePriority()
0128 {
0129     const int row = ui.list->currentIndex().row();
0130     m_model->moveItem(row, row + 2);
0131     slotUpdateButtons();
0132     Q_EMIT changed();
0133 }
0134 
0135 #include "moc_integrationpreferences.cpp"