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

0001 /* SPDX-FileCopyrightText: 2003-2020 Jesper K. Pedersen <blackie@kde.org>
0002 
0003    SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "CategoryImagePopup.h"
0007 
0008 #include "Window.h"
0009 
0010 #include <DB/CategoryCollection.h>
0011 #include <Viewer/CategoryImageConfig.h>
0012 #include <kpabase/StringSet.h>
0013 
0014 #include <KLocalizedString>
0015 #include <qstringlist.h>
0016 
0017 void MainWindow::CategoryImagePopup::populate(const QImage &image, const DB::FileName &imageName)
0018 {
0019     clear();
0020 
0021     m_image = image;
0022     m_imageInfo = DB::ImageDB::instance()->info(imageName);
0023 
0024     // add the categories
0025     const QList<DB::CategoryPtr> categories = DB::ImageDB::instance()->categoryCollection()->categories();
0026     for (const DB::CategoryPtr &category : categories) {
0027         if (!category->isSpecialCategory()) {
0028             bool categoryMenuEnabled = false;
0029             const QString categoryName = category->name();
0030             QMenu *categoryMenu = new QMenu(this);
0031             categoryMenu->setTitle(category->name());
0032 
0033             // add category members
0034             const Utilities::StringSet members = m_imageInfo->itemsOfCategory(categoryName);
0035             for (const QString &member : members) {
0036                 QAction *action = categoryMenu->addAction(member);
0037                 action->setObjectName(categoryName);
0038                 action->setData(member);
0039                 categoryMenuEnabled = true;
0040             }
0041 
0042             categoryMenu->setEnabled(categoryMenuEnabled);
0043             addMenu(categoryMenu);
0044         }
0045     }
0046 
0047     // Add the Category Editor menu item
0048     QAction *action = addAction(QString::fromLatin1("viewer-show-category-editor"), this, SLOT(makeCategoryImage()));
0049     action->setText(i18n("Show Category Editor"));
0050 }
0051 
0052 void MainWindow::CategoryImagePopup::slotExecuteService(QAction *action)
0053 {
0054     QString categoryName = action->objectName();
0055     QString memberName = action->data().toString();
0056     if (categoryName.isNull())
0057         return;
0058     DB::ImageDB::instance()->categoryCollection()->categoryForName(categoryName)->setCategoryImage(categoryName, memberName, m_image);
0059 }
0060 
0061 void MainWindow::CategoryImagePopup::makeCategoryImage()
0062 {
0063     Viewer::CategoryImageConfig::instance()->setCurrentImage(m_image, m_imageInfo);
0064     Viewer::CategoryImageConfig::instance()->show();
0065 }
0066 
0067 MainWindow::CategoryImagePopup::CategoryImagePopup(QWidget *parent)
0068     : QMenu(parent)
0069 {
0070     setTitle(i18n("Make Category Image"));
0071     connect(this, &CategoryImagePopup::triggered, this, &CategoryImagePopup::slotExecuteService);
0072 }
0073 
0074 // vi:expandtab:tabstop=4 shiftwidth=4:
0075 
0076 #include "moc_CategoryImagePopup.cpp"