File indexing completed on 2024-04-28 04:21:25

0001 // SPDX-FileCopyrightText: 2003-2020 The KPhotoAlbum Development Team
0002 // SPDX-FileCopyrightText: 2022 Johannes Zarl-Zierl <johannes@zarl-zierl.at>
0003 //
0004 // SPDX-License-Identifier: GPL-2.0-or-later
0005 
0006 #include "CategoryImageConfig.h"
0007 
0008 #include <DB/CategoryCollection.h>
0009 #include <DB/ImageDB.h>
0010 #include <DB/ImageInfo.h>
0011 #include <DB/MemberMap.h>
0012 #include <Utilities/FileUtil.h>
0013 #include <kpabase/SettingsData.h>
0014 
0015 #include <KComboBox>
0016 #include <KLocalizedString>
0017 #include <QDialogButtonBox>
0018 #include <QGridLayout>
0019 #include <QLabel>
0020 #include <QLayout>
0021 #include <QList>
0022 #include <QPixmap>
0023 #include <QPushButton>
0024 #include <QVBoxLayout>
0025 
0026 using Utilities::StringSet;
0027 
0028 Viewer::CategoryImageConfig *Viewer::CategoryImageConfig::s_instance = nullptr;
0029 
0030 Viewer::CategoryImageConfig::CategoryImageConfig()
0031     : m_image(QImage())
0032 {
0033     setWindowTitle(i18nc("@title:window", "Configure Category Image"));
0034     QWidget *top = new QWidget;
0035 
0036     QVBoxLayout *lay1 = new QVBoxLayout(top);
0037     setLayout(lay1);
0038     QGridLayout *lay2 = new QGridLayout;
0039     lay1->addLayout(lay2);
0040 
0041     // Group
0042     QLabel *label = new QLabel(i18nc("@label:listbox As in 'select the tag category'", "Category:"), top);
0043     lay2->addWidget(label, 0, 0);
0044     m_group = new KComboBox(top);
0045     lay2->addWidget(m_group, 0, 1);
0046     connect(m_group, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), this, &CategoryImageConfig::groupChanged);
0047 
0048     // Member
0049     label = new QLabel(i18nc("@label:listbox As in 'select a tag'", "Tag:"), top);
0050     lay2->addWidget(label, 1, 0);
0051     m_member = new KComboBox(top);
0052     lay2->addWidget(m_member, 1, 1);
0053     connect(m_member, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), this, &CategoryImageConfig::memberChanged);
0054 
0055     // Current Value
0056     QGridLayout *lay3 = new QGridLayout;
0057     lay1->addLayout(lay3);
0058     label = new QLabel(i18nc("@label The current category image", "Current image:"), top);
0059     lay3->addWidget(label, 0, 0);
0060 
0061     m_current = new QLabel(top);
0062     m_current->setFixedSize(128, 128);
0063     lay3->addWidget(m_current, 0, 1);
0064 
0065     // New Value
0066     m_imageLabel = new QLabel(i18nc("@label Preview of the new category imape", "New image:"), top);
0067     lay3->addWidget(m_imageLabel, 1, 0);
0068 
0069     m_imageLabel = new QLabel(top);
0070     m_imageLabel->setFixedSize(128, 128);
0071     lay3->addWidget(m_imageLabel, 1, 1);
0072 
0073     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Close);
0074     QPushButton *user1Button = new QPushButton;
0075     user1Button->setText(i18nc("@action:button As in 'Set the category image'", "Set"));
0076     buttonBox->addButton(user1Button, QDialogButtonBox::ActionRole);
0077     connect(user1Button, &QPushButton::clicked, this, &CategoryImageConfig::slotSet);
0078     connect(buttonBox, &QDialogButtonBox::accepted, this, &CategoryImageConfig::accept);
0079     connect(buttonBox, &QDialogButtonBox::rejected, this, &CategoryImageConfig::reject);
0080     lay1->addWidget(buttonBox);
0081 }
0082 
0083 void Viewer::CategoryImageConfig::groupChanged()
0084 {
0085     QString categoryName = currentGroup();
0086     if (categoryName.isNull())
0087         return;
0088 
0089     QString currentText = m_member->currentText();
0090     m_member->clear();
0091     StringSet directMembers = m_info->itemsOfCategory(categoryName);
0092 
0093     StringSet set = directMembers;
0094     QMap<QString, StringSet> map = DB::ImageDB::instance()->memberMap().inverseMap(categoryName);
0095     for (auto directMembersIt = directMembers.begin(); directMembersIt != directMembers.end(); ++directMembersIt) {
0096         set += map[*directMembersIt];
0097     }
0098 
0099     QStringList list(set.begin(), set.end());
0100 
0101     list.sort();
0102     m_member->addItems(list);
0103     int index = list.indexOf(currentText);
0104     if (index != -1)
0105         m_member->setCurrentIndex(index);
0106 
0107     memberChanged();
0108 }
0109 
0110 void Viewer::CategoryImageConfig::memberChanged()
0111 {
0112     QString categoryName = currentGroup();
0113     if (categoryName.isNull())
0114         return;
0115     QPixmap pix = DB::ImageDB::instance()->categoryCollection()->categoryForName(categoryName)->categoryImage(categoryName, m_member->currentText(), 128, 128);
0116     m_current->setPixmap(pix);
0117 }
0118 
0119 void Viewer::CategoryImageConfig::slotSet()
0120 {
0121     QString categoryName = currentGroup();
0122     if (categoryName.isNull())
0123         return;
0124     DB::ImageDB::instance()->categoryCollection()->categoryForName(categoryName)->setCategoryImage(categoryName, m_member->currentText(), m_image);
0125     memberChanged();
0126 }
0127 
0128 QString Viewer::CategoryImageConfig::currentGroup()
0129 {
0130     int index = m_group->currentIndex();
0131     if (index == -1)
0132         return QString();
0133     return m_categoryNames[index];
0134 }
0135 
0136 void Viewer::CategoryImageConfig::setCurrentImage(const QImage &image, const DB::ImageInfoPtr &info)
0137 {
0138     m_image = image;
0139     m_imageLabel->setPixmap(QPixmap::fromImage(image));
0140     m_info = info;
0141     groupChanged();
0142 }
0143 
0144 Viewer::CategoryImageConfig *Viewer::CategoryImageConfig::instance()
0145 {
0146     if (!s_instance)
0147         s_instance = new CategoryImageConfig();
0148     return s_instance;
0149 }
0150 
0151 void Viewer::CategoryImageConfig::show()
0152 {
0153     QString currentCategory = m_group->currentText();
0154     m_group->clear();
0155     m_categoryNames.clear();
0156     QList<DB::CategoryPtr> categories = DB::ImageDB::instance()->categoryCollection()->categories();
0157     int index = 0;
0158     int currentIndex = -1;
0159     for (QList<DB::CategoryPtr>::ConstIterator categoryIt = categories.constBegin(); categoryIt != categories.constEnd(); ++categoryIt) {
0160         if (!(*categoryIt)->isSpecialCategory()) {
0161             m_group->addItem((*categoryIt)->name());
0162             m_categoryNames.push_back((*categoryIt)->name());
0163             if ((*categoryIt)->name() == currentCategory)
0164                 currentIndex = index;
0165             ++index;
0166         }
0167     }
0168 
0169     if (currentIndex != -1)
0170         m_group->setCurrentIndex(currentIndex);
0171     groupChanged();
0172 
0173     QDialog::show();
0174 }
0175 
0176 // vi:expandtab:tabstop=4 shiftwidth=4:
0177 
0178 #include "moc_CategoryImageConfig.cpp"