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

0001 /*
0002  * SPDX-FileCopyrightText: 2023 Srirupa Datta <srirupa.sps@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "page_bundle_saver.h"
0008 #include "ui_pagebundlesaver.h"
0009 #include "dlg_create_bundle.h"
0010 
0011 #include <kis_config.h>
0012 #include <KoFileDialog.h>
0013 
0014 PageBundleSaver::PageBundleSaver(KoResourceBundleSP bundle, QWidget *parent) :
0015     QWizardPage(parent),
0016     m_ui(new Ui::PageBundleSaver)
0017     , m_bundle(bundle)
0018 {
0019     m_ui->setupUi(this);
0020 
0021     if (m_bundle) {
0022         QFileInfo fileInfo(m_bundle->filename());
0023         QString directory = fileInfo.path();
0024         m_ui->lblSaveLocation->setText(directory);
0025 
0026         m_bundleStorage = new KisBundleStorage(m_bundle->filename());
0027         QMap<QString, int> m_count;
0028         QStringList resourceTypes = QStringList() << ResourceType::Brushes << ResourceType::PaintOpPresets << ResourceType::Gradients << ResourceType::GamutMasks;
0029         #if defined HAVE_SEEXPR
0030         resourceTypes << ResourceType::SeExprScripts;
0031         #endif
0032         resourceTypes << ResourceType::Patterns << ResourceType::Palettes << ResourceType::Workspaces;
0033 
0034         m_resourceCount = "";
0035         QSet<QString> set;
0036         m_tags = "";
0037 
0038         for (const QString &type : resourceTypes) {
0039             QSharedPointer<KisResourceStorage::ResourceIterator> iter = m_bundleStorage->resources(type);
0040             int count = 0;
0041             while (iter->hasNext()) {
0042                 iter->next();
0043                 count++;
0044             }
0045             m_loaded_count[type] = count;
0046             if (count != 0) {
0047                 m_count[type] = count;
0048                 m_resourceCount += ResourceName::resourceTypeToName(type) + ": " + QString::number(count) + "<br>";
0049             }
0050 
0051             QSharedPointer<KisResourceStorage::TagIterator> tagIter = m_bundleStorage->tags(type);
0052             while (tagIter->hasNext()) {
0053                 tagIter->next();
0054                 set.insert(tagIter->tag()->name());
0055                 m_loaded_tags.insert(tagIter->tag()->name());
0056             }
0057         }
0058 
0059         if (!m_resourceCount.isEmpty()) {
0060             m_ui->lblDetails->setText("<b>Resources</b><br>" + m_resourceCount);
0061         }
0062 
0063         if (!set.isEmpty()) {
0064             m_tags = "<b>Tags</b><br>" + set.toList().join(", ") + "<br>";
0065         }
0066 
0067         m_ui->lblDetails->setText(m_ui->lblDetails->text() + m_tags);
0068 
0069 
0070     } else {
0071         KisConfig cfg(true);
0072         m_ui->lblSaveLocation->setText(cfg.readEntry<QString>("BundleExportLocation", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)));
0073     }
0074 
0075     connect(m_ui->bnSelectSaveLocation, SIGNAL(clicked()), SLOT(selectSaveLocation()));
0076 
0077 }
0078 
0079 PageBundleSaver::~PageBundleSaver()
0080 {
0081     delete m_ui;
0082 }
0083 
0084 QString PageBundleSaver::saveLocation() const
0085 {
0086     return m_ui->lblSaveLocation->text();
0087 }
0088 
0089 void PageBundleSaver::selectSaveLocation()
0090 {
0091     KoFileDialog dialog(this, KoFileDialog::OpenDirectory, "resourcebundlesavelocation");
0092     dialog.setDefaultDir(m_ui->lblSaveLocation->text());
0093     dialog.setCaption(i18n("Select a directory to save the bundle"));
0094     QString location = dialog.filename();
0095     m_ui->lblSaveLocation->setText(location);
0096 }
0097 
0098 void PageBundleSaver::showWarning()
0099 {
0100      m_ui->lblSaveLocation->setStyleSheet(QString(" border: 1px solid red"));
0101 }
0102 
0103 void PageBundleSaver::onCountUpdated()
0104 {
0105     DlgCreateBundle *wizard = qobject_cast<DlgCreateBundle*>(this->wizard());
0106     // QString bundleDetails;
0107     m_resourceCount = "";
0108     QMap<QString, int> map = wizard->m_count;
0109     bool resPresent = false;
0110     for (auto i = map.cbegin(), end = map.cend(); i != end; ++i) {
0111         if (i.value() != 0) {
0112             if (!resPresent) {
0113                 resPresent = true;
0114                 m_resourceCount = m_resourceCount + "<b>Resources</b>" + "<br>";
0115             }
0116             m_resourceCount = m_resourceCount + ResourceName::resourceTypeToName(i.key()) + ": " + QString::number(i.value()) + "<br>";
0117         }
0118     }
0119     m_ui->lblDetails->setText(m_resourceCount + m_tags);
0120 }
0121 
0122 void PageBundleSaver::onTagsUpdated()
0123 {
0124     DlgCreateBundle *wizard = qobject_cast<DlgCreateBundle*>(this->wizard());
0125     m_tags = "";
0126     QSet<QString> set = wizard->m_tags;
0127 
0128     bool tagPresent = false;
0129     for (QSet<QString>::const_iterator it = set.constBegin(); it != set.constEnd(); ++it) {
0130         if (!tagPresent) {
0131             tagPresent = true;
0132             m_tags = m_tags + "<b>Tags</b>" + "<br>";
0133         }
0134         if (it + 1 == set.constEnd()) {
0135             m_tags = m_tags + *it;
0136         } else {
0137             m_tags = m_tags + *it + ", ";
0138         }
0139     }
0140 
0141     m_tags = m_tags + "<br>";
0142     m_ui->lblDetails->setText(m_resourceCount + m_tags);
0143 }
0144 
0145 void PageBundleSaver::removeWarning()
0146 {
0147      m_ui->lblSaveLocation->setStyleSheet(QString(""));
0148 }