File indexing completed on 2024-04-28 15:40:13

0001 // SPDX-FileCopyrightText: 2004-2022 Jesper K. Pedersen <jesper.pedersen@kdab.com>
0002 // SPDX-FileCopyrightText: 2006 Tuomas Suutari <tuomas@nepnep.net>
0003 // SPDX-FileCopyrightText: 2007 Dirk Mueller <mueller@kde.org>
0004 // SPDX-FileCopyrightText: 2013-2023 Johannes Zarl-Zierl <johannes@zarl-zierl.at>
0005 // SPDX-FileCopyrightText: 2015-2020 Tobias Leupold <tl@stonemx.de>
0006 // SPDX-FileCopyrightText: 2018 Antoni Bella PĂ©rez <antonibella5@yahoo.com>
0007 // SPDX-FileCopyrightText: 2018 Yuri Chornoivan <yurchor@ukr.net>
0008 //
0009 // SPDX-License-Identifier: GPL-2.0-or-later
0010 
0011 #include "TokenEditor.h"
0012 
0013 #include <DB/Category.h>
0014 #include <DB/CategoryCollection.h>
0015 #include <DB/ImageDB.h>
0016 #include <DB/search/ImageSearchInfo.h>
0017 #include <kpabase/SettingsData.h>
0018 
0019 #include <KLocalizedString>
0020 #include <QDialogButtonBox>
0021 #include <QGridLayout>
0022 #include <QHBoxLayout>
0023 #include <QPushButton>
0024 #include <QVBoxLayout>
0025 #include <qcheckbox.h>
0026 #include <qlabel.h>
0027 #include <qlayout.h>
0028 
0029 using namespace MainWindow;
0030 
0031 TokenEditor::TokenEditor(QWidget *parent)
0032     : QDialog(parent)
0033 {
0034     setWindowTitle(i18nc("@title:window", "Remove Tokens"));
0035     QVBoxLayout *dialogLayout = new QVBoxLayout(this);
0036 
0037     QWidget *mainContents = new QWidget;
0038     QVBoxLayout *vlay = new QVBoxLayout(mainContents);
0039 
0040     QLabel *label = new QLabel(i18n("Select tokens to remove from all images and videos:"));
0041     vlay->addWidget(label);
0042 
0043     QGridLayout *grid = new QGridLayout;
0044     vlay->addLayout(grid);
0045 
0046     int index = 0;
0047     for (int ch = 'A'; ch <= 'Z'; ch++, index++) {
0048         QChar token = QChar::fromLatin1((char)ch);
0049         QCheckBox *box = new QCheckBox(token);
0050         grid->addWidget(box, index / 5, index % 5);
0051         m_checkBoxes.append(box);
0052     }
0053 
0054     QHBoxLayout *hlay = new QHBoxLayout;
0055     vlay->addLayout(hlay);
0056 
0057     hlay->addStretch(1);
0058     QPushButton *selectAll = new QPushButton(i18n("Select All"));
0059     QPushButton *selectNone = new QPushButton(i18n("Select None"));
0060     hlay->addWidget(selectAll);
0061     hlay->addWidget(selectNone);
0062 
0063     connect(selectAll, &QPushButton::clicked, this, &TokenEditor::selectAll);
0064     connect(selectNone, &QPushButton::clicked, this, &TokenEditor::selectNone);
0065 
0066     dialogLayout->addWidget(mainContents);
0067 
0068     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0069     buttonBox->button(QDialogButtonBox::Ok)->setShortcut(Qt::CTRL | Qt::Key_Return);
0070     connect(buttonBox, &QDialogButtonBox::accepted, this, &TokenEditor::accept);
0071     connect(buttonBox, &QDialogButtonBox::rejected, this, &TokenEditor::reject);
0072     dialogLayout->addWidget(buttonBox);
0073 }
0074 
0075 void TokenEditor::show()
0076 {
0077     QStringList tokens = tokensInUse();
0078 
0079     for (QCheckBox *box : qAsConst(m_checkBoxes)) {
0080         box->setChecked(false);
0081         QString txt = box->text().remove(QString::fromLatin1("&"));
0082         box->setEnabled(tokens.contains(txt));
0083     }
0084     QDialog::show();
0085 }
0086 
0087 void TokenEditor::selectAll()
0088 {
0089     for (QCheckBox *box : qAsConst(m_checkBoxes)) {
0090         box->setChecked(true);
0091     }
0092 }
0093 
0094 void TokenEditor::selectNone()
0095 {
0096     for (QCheckBox *box : qAsConst(m_checkBoxes)) {
0097         box->setChecked(false);
0098     }
0099 }
0100 
0101 /**
0102    I would love to use Settings::optionValue, but that method does not
0103     forget about an item once it has seen it, which is really what it should
0104     do anyway, otherwise it would be way to expensive in use.
0105 */
0106 QStringList TokenEditor::tokensInUse()
0107 {
0108     QStringList res;
0109     DB::CategoryPtr tokensCategory = DB::ImageDB::instance()->categoryCollection()->categoryForSpecial(DB::Category::TokensCategory);
0110     QMap<QString, DB::CountWithRange> map = DB::ImageDB::instance()->classify(DB::ImageSearchInfo(), tokensCategory->name(), DB::anyMediaType);
0111     for (auto it = map.constBegin(); it != map.constEnd(); ++it) {
0112         if (it.value().count > 0)
0113             res.append(it.key());
0114     }
0115     return res;
0116 }
0117 
0118 void TokenEditor::accept()
0119 {
0120     DB::CategoryPtr tokensCategory = DB::ImageDB::instance()->categoryCollection()->categoryForSpecial(DB::Category::TokensCategory);
0121     for (const QCheckBox *box : qAsConst(m_checkBoxes)) {
0122         if (box->isChecked() && box->isEnabled()) {
0123             QString txt = box->text().remove(QString::fromLatin1("&"));
0124             tokensCategory->removeItem(txt);
0125             // re-add the token so that it is still available e.g. in the annotation dialog list select:
0126             tokensCategory->addItem(txt);
0127         }
0128     }
0129     QDialog::accept();
0130 }
0131 
0132 // vi:expandtab:tabstop=4 shiftwidth=4:
0133 
0134 #include "moc_TokenEditor.cpp"