File indexing completed on 2021-12-21 13:28:02

0001 /**
0002  * Copyright (C) 2003 Frerich Raabe <raabe@kde.org>
0003  *
0004  * This program is free software; you can redistribute it and/or modify it under
0005  * the terms of the GNU General Public License as published by the Free Software
0006  * Foundation; either version 2 of the License, or (at your option) any later
0007  * version.
0008  *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.
0012  *
0013  * You should have received a copy of the GNU General Public License along with
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.
0015  */
0016 
0017 #include "tagguesserconfigdlg.h"
0018 #include "tagguesser.h"
0019 
0020 #include <KLocalizedString>
0021 #include <klineedit.h>
0022 
0023 #include <QIcon>
0024 #include <QKeyEvent>
0025 #include <QStringListModel>
0026 #include <QVBoxLayout>
0027 #include <QPushButton>
0028 #include <QDialogButtonBox>
0029 
0030 #include "iconsupport.h"
0031 
0032 TagGuesserConfigDlg::TagGuesserConfigDlg(QWidget *parent, const char *name)
0033   : QDialog(parent)
0034   , m_child(new TagGuesserConfigDlgWidget(this))
0035   , m_tagSchemeModel(new QStringListModel(TagGuesser::schemeStrings(), m_child->lvSchemes))
0036 {
0037     using namespace IconSupport; // ""_icon
0038 
0039     setObjectName(QLatin1String(name));
0040     setModal(true);
0041     setWindowTitle(i18n("Tag Guesser Configuration"));
0042 
0043     auto vboxLayout = new QVBoxLayout(this);
0044     vboxLayout->addWidget(m_child);
0045 
0046     m_child->bMoveUp->setIcon("arrow-up"_icon);
0047     m_child->bMoveDown->setIcon("arrow-down"_icon);
0048 
0049     m_child->lvSchemes->setModel(m_tagSchemeModel);
0050     m_child->lvSchemes->setHeaderHidden(true);
0051 
0052     connect(m_child->lvSchemes, SIGNAL(clicked(QModelIndex)), this, SLOT(slotCurrentChanged(QModelIndex)));
0053     connect(m_child->bMoveUp, SIGNAL(clicked()), this, SLOT(slotMoveUpClicked()));
0054     connect(m_child->bMoveDown, SIGNAL(clicked()), this, SLOT(slotMoveDownClicked()));
0055     connect(m_child->bAdd, SIGNAL(clicked()), this, SLOT(slotAddClicked()));
0056     connect(m_child->bModify, SIGNAL(clicked()), this, SLOT(slotModifyClicked()));
0057     connect(m_child->bRemove, SIGNAL(clicked()), this, SLOT(slotRemoveClicked()));
0058     connect(m_child->dlgButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0059     connect(m_child->dlgButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0060 
0061     resize( 400, 300 );
0062 }
0063 
0064 void TagGuesserConfigDlg::slotCurrentChanged(QModelIndex item)
0065 {
0066     m_child->bRemove->setEnabled(m_tagSchemeModel->rowCount() != 0);
0067 
0068     // Ensure up/down buttons are appropriately enabled.
0069 
0070     if (!m_tagSchemeModel->rowCount() || item == m_tagSchemeModel->index(0, 0, QModelIndex()))
0071         m_child->bMoveUp->setEnabled(false);
0072     else
0073         m_child->bMoveUp->setEnabled(true);
0074 
0075     if (!m_tagSchemeModel->rowCount() || item == m_tagSchemeModel->index(m_tagSchemeModel->rowCount(QModelIndex())-1, 0, QModelIndex()))
0076         m_child->bMoveDown->setEnabled(false);
0077     else
0078         m_child->bMoveDown->setEnabled(true);
0079 }
0080 
0081 void TagGuesserConfigDlg::slotMoveUpClicked()
0082 {
0083     QModelIndex currentItem = m_child->lvSchemes->currentIndex();
0084     int row = currentItem.row();
0085 
0086     m_tagSchemeModel->insertRow(row - 1); // Insert in front of item above
0087     row++; // Now we're one row down
0088 
0089     QModelIndex newItem = m_tagSchemeModel->index(row - 2, 0);
0090 
0091     // Copy over, then delete old item
0092     currentItem = m_tagSchemeModel->index(row, 0);
0093     m_tagSchemeModel->setData(newItem, m_tagSchemeModel->data(currentItem, Qt::DisplayRole), Qt::DisplayRole);
0094     m_tagSchemeModel->removeRow(row);
0095 
0096     m_child->lvSchemes->setCurrentIndex(newItem);
0097     slotCurrentChanged(newItem);
0098 }
0099 
0100 void TagGuesserConfigDlg::slotMoveDownClicked()
0101 {
0102     QModelIndex currentItem = m_child->lvSchemes->currentIndex();
0103     int row = currentItem.row();
0104 
0105     m_tagSchemeModel->insertRow(row + 2); // Insert in front of 2 items below
0106 
0107     QModelIndex newItem = m_tagSchemeModel->index(row + 2, 0);
0108 
0109     // Copy over, then delete old item
0110     currentItem = m_tagSchemeModel->index(row, 0);
0111     m_tagSchemeModel->setData(newItem, m_tagSchemeModel->data(currentItem, Qt::DisplayRole), Qt::DisplayRole);
0112     m_tagSchemeModel->removeRow(row);
0113 
0114     newItem = m_tagSchemeModel->index(row + 1, 0);
0115     m_child->lvSchemes->setCurrentIndex(newItem);
0116     slotCurrentChanged(newItem);
0117 }
0118 
0119 void TagGuesserConfigDlg::slotAddClicked()
0120 {
0121     m_tagSchemeModel->insertRow(0, QModelIndex());
0122     m_child->lvSchemes->setCurrentIndex(m_tagSchemeModel->index(0, 0, QModelIndex()));
0123     m_child->lvSchemes->edit(m_child->lvSchemes->currentIndex());
0124     slotCurrentChanged(m_child->lvSchemes->currentIndex());
0125 }
0126 
0127 void TagGuesserConfigDlg::slotModifyClicked()
0128 {
0129     m_child->lvSchemes->edit(m_child->lvSchemes->currentIndex());
0130 }
0131 
0132 void TagGuesserConfigDlg::slotRemoveClicked()
0133 {
0134     m_tagSchemeModel->removeRow(m_child->lvSchemes->currentIndex().row(), QModelIndex());
0135     slotCurrentChanged(m_child->lvSchemes->currentIndex());
0136 }
0137 
0138 void TagGuesserConfigDlg::accept()
0139 {
0140     TagGuesser::setSchemeStrings(m_tagSchemeModel->stringList());
0141     QDialog::accept();
0142 }
0143 
0144 // vim: set et sw=4 tw=0 sta: