File indexing completed on 2024-12-22 04:15:11

0001 /*
0002  *  SPDX-FileCopyrightText: 2021 Agata Cacko cacko.azh@gmail.com
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 #include "DlgResourceTypeForFile.h"
0007 
0008 
0009 #include <QRadioButton>
0010 #include <QButtonGroup>
0011 #include <QDebug>
0012 
0013 #include <kis_assert.h>
0014 
0015 #include <KisResourceTypeModel.h>
0016 #include <KisResourceTypes.h>
0017 
0018 DlgResourceTypeForFile::DlgResourceTypeForFile(QWidget *parent, QMap<QString, QStringList> resourceTypesForMimetype)
0019     : KoDialog(parent)
0020 {
0021     setCaption(i18n("Set the resource type for files"));
0022 
0023     QVBoxLayout* layout = new QVBoxLayout(parent);
0024 
0025     KisResourceTypeModel model;
0026 
0027     QStringList keys = resourceTypesForMimetype.keys();
0028 
0029     for (int i = 0; i < keys.size(); i++) {
0030         QLabel* label;
0031         label = new QLabel(parent);
0032         if (keys[i] == "image/png") {
0033             label->setText(i18nc("Question in a dialog so the user can choose which resource type the PNG files belong to: brush tips or patterns",
0034                                  "What resource do you want to import these PNG files as?"));
0035         } else if (keys[i] == "image/svg+xml") {
0036             label->setText(i18nc("Question in a dialog so the user can choose which resource type the SVG files belong to: brush tips or patterns or symbols",
0037                                  "What resource do you want to import these SVG files as?"));
0038         } else {
0039             label->setText(i18nc("Question in a dialog so the user can choose which resource type the files of type %1 belongs to; % will be rather untranslatable noun (a mimetype)",
0040                                  "What resource do you want to import these %1 files as?", keys[i]));
0041         }
0042         layout->addWidget(label);
0043 
0044         QButtonGroup* buttonGroup = new QButtonGroup(parent);
0045         m_buttonGroupForMimetype.insert(keys[i], buttonGroup);
0046 
0047         QStringList resourceTypes = resourceTypesForMimetype[keys[i]];
0048         for (int j = 0; j < resourceTypes.size(); j++)
0049         {
0050             QString resourceName = ResourceName::resourceTypeToName(resourceTypes[j]);
0051             QRadioButton* button = new QRadioButton(resourceName, parent);
0052             button->setProperty("resourceType", resourceTypes[j]);
0053             buttonGroup->addButton(button);
0054             layout->addWidget(button);
0055         }
0056     }
0057 
0058 
0059     QWidget* widget = new QWidget(parent);
0060     widget->setLayout(layout);
0061     this->setMainWidget(widget);
0062 
0063     
0064 }
0065 
0066 QString DlgResourceTypeForFile::getResourceTypeForMimetype(QString mimetype)
0067 {
0068     KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(m_buttonGroupForMimetype.contains(mimetype), "");
0069 
0070     QButtonGroup* group = m_buttonGroupForMimetype[mimetype];
0071 
0072     KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(group, "");
0073 
0074     QList<QAbstractButton*> buttons = group->buttons();
0075     for (int i = 0; i < buttons.size(); i++) {
0076         if (buttons[i]->isChecked()) {
0077             QString resourceType = buttons[i]->property(m_propertyName.toStdString().c_str()).toString();
0078             return resourceType;
0079         }
0080     }
0081     KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(false, "");
0082     return "";
0083 }
0084 
0085