File indexing completed on 2024-04-28 04:21:20
0001 // SPDX-FileCopyrightText: 2009-2022 Jesper K. Pedersen <jesper.pedersen@kdab.com> 0002 // SPDX-FileCopyrightText: 2013-2023 Johannes Zarl-Zierl <johannes@zarl-zierl.at> 0003 // SPDX-FileCopyrightText: 2014-2015, 2020 Tobias Leupold <tl@stonemx.de> 0004 // 0005 // SPDX-License-Identifier: GPL-2.0-or-later 0006 0007 #include "UntaggedGroupBox.h" 0008 0009 #include <DB/CategoryCollection.h> 0010 #include <DB/ImageDB.h> 0011 #include <kpabase/SettingsData.h> 0012 0013 #include <KLocalizedString> 0014 #include <QCheckBox> 0015 #include <QComboBox> 0016 #include <QDebug> 0017 #include <QGridLayout> 0018 #include <QLabel> 0019 #include <QMessageBox> 0020 0021 Settings::UntaggedGroupBox::UntaggedGroupBox(QWidget *parent) 0022 : QGroupBox(i18n("Untagged Images"), parent) 0023 { 0024 setWhatsThis(i18n("If a tag is selected here, it will be added to new (untagged) images " 0025 "automatically, so that they can be easily found. It will be removed as " 0026 "soon as the image has been annotated.")); 0027 0028 QGridLayout *grid = new QGridLayout(this); 0029 int row = -1; 0030 0031 QLabel *label = new QLabel(i18n("Category:")); 0032 grid->addWidget(label, ++row, 0); 0033 0034 m_category = new QComboBox; 0035 grid->addWidget(m_category, row, 1); 0036 connect(m_category, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &UntaggedGroupBox::populateTagsCombo); 0037 0038 label = new QLabel(i18n("Tag:")); 0039 grid->addWidget(label, ++row, 0); 0040 0041 m_tag = new QComboBox; 0042 grid->addWidget(m_tag, row, 1); 0043 m_tag->setEditable(true); 0044 0045 m_showUntaggedImagesTag = new QCheckBox(i18n("Show the untagged images tag as a normal tag")); 0046 grid->addWidget(m_showUntaggedImagesTag, ++row, 0, 1, 2); 0047 0048 grid->setColumnStretch(1, 1); 0049 } 0050 0051 void Settings::UntaggedGroupBox::populateCategoryComboBox() 0052 { 0053 m_category->clear(); 0054 m_category->addItem(i18n("None Selected")); 0055 const auto categories = DB::ImageDB::instance()->categoryCollection()->categories(); 0056 for (DB::CategoryPtr category : categories) { 0057 if (!category->isSpecialCategory()) { 0058 m_category->addItem(category->name(), category->name()); 0059 } 0060 } 0061 } 0062 0063 void Settings::UntaggedGroupBox::populateTagsCombo() 0064 { 0065 m_tag->clear(); 0066 const QString currentCategory = m_category->itemData(m_category->currentIndex()).value<QString>(); 0067 const auto categoryPtr = DB::ImageDB::instance()->categoryCollection()->categoryForName(currentCategory); 0068 0069 // categoryPtr is invalid even though currentCategory is set, when the category is not yet saved to the database... 0070 if (currentCategory.isEmpty() || !categoryPtr) { 0071 m_tag->setEnabled(false); 0072 } else { 0073 m_tag->setEnabled(true); 0074 const QStringList items = categoryPtr->items(); 0075 m_tag->addItems(items); 0076 } 0077 } 0078 0079 void Settings::UntaggedGroupBox::loadSettings(Settings::SettingsData *opt) 0080 { 0081 populateCategoryComboBox(); 0082 0083 const QString category = opt->untaggedCategory(); 0084 const QString tag = opt->untaggedTag(); 0085 0086 int categoryIndex = m_category->findData(category); 0087 if (categoryIndex == -1) 0088 categoryIndex = 0; 0089 0090 m_category->setCurrentIndex(categoryIndex); 0091 populateTagsCombo(); 0092 0093 if (categoryIndex != 0) { 0094 int tagIndex = m_tag->findText(tag); 0095 if (tagIndex == -1) { 0096 m_tag->addItem(tag); 0097 tagIndex = m_tag->findText(tag); 0098 Q_ASSERT(tagIndex != -1); 0099 } 0100 m_tag->setCurrentIndex(tagIndex); 0101 } 0102 0103 m_showUntaggedImagesTag->setChecked(opt->untaggedImagesTagVisible()); 0104 } 0105 0106 void Settings::UntaggedGroupBox::saveSettings(Settings::SettingsData *opt) 0107 { 0108 const QString category = m_category->itemData(m_category->currentIndex()).value<QString>(); 0109 QString untaggedTag = m_tag->currentText().simplified(); 0110 0111 if (!category.isEmpty()) { 0112 // Add a new tag if the entered one is not in the DB yet 0113 DB::CategoryPtr categoryPointer = DB::ImageDB::instance()->categoryCollection()->categoryForName(category); 0114 if (!categoryPointer->items().contains(untaggedTag)) { 0115 categoryPointer->addItem(untaggedTag); 0116 QMessageBox::information(this, 0117 i18n("New tag added"), 0118 i18n("<p>The new tag \"%1\" has been added to the category \"%2\" and will be used " 0119 "for untagged images now.</p>" 0120 "<p>Please save now, so that this tag will be stored in the database. " 0121 "Otherwise, it will be lost, and you will get an error about this tag being " 0122 "not present on the next start.</p>", 0123 untaggedTag, category)); 0124 } 0125 0126 opt->setUntaggedCategory(category); 0127 opt->setUntaggedTag(untaggedTag); 0128 } else { 0129 // If no untagged images tag is selected, remove the setting by using an empty string 0130 opt->setUntaggedCategory(QString()); 0131 opt->setUntaggedTag(QString()); 0132 } 0133 0134 opt->setUntaggedImagesTagVisible(m_showUntaggedImagesTag->isChecked()); 0135 } 0136 0137 void Settings::UntaggedGroupBox::categoryAdded(const QString &categoryName) 0138 { 0139 m_category->addItem(categoryName); 0140 } 0141 0142 void Settings::UntaggedGroupBox::categoryDeleted(const QString &categoryName) 0143 { 0144 if (categoryName == m_category->itemData(m_category->currentIndex()).value<QString>()) { 0145 m_category->setCurrentIndex(0); 0146 } 0147 0148 m_category->removeItem(m_category->findText(categoryName)); 0149 } 0150 0151 void Settings::UntaggedGroupBox::categoryRenamed(const QString &oldCategoryName, const QString &newCategoryName) 0152 { 0153 const int index = m_category->findText(oldCategoryName); 0154 m_category->setItemText(index, newCategoryName); 0155 m_category->setItemData(index, newCategoryName); 0156 } 0157 0158 // vi:expandtab:tabstop=4 shiftwidth=4: 0159 0160 #include "moc_UntaggedGroupBox.cpp"