Krita Source Code Documentation
Loading...
Searching...
No Matches
DlgResourceManager.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2021 Mathias Wein <lynx.mw+kde@gmail.com>
3 * SPDX-FileCopyrightText: 2023 Srirupa Datta <srirupa.sps@gmail.com>
4 *
5 * SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
9
10#include "ui_WdgDlgResourceManager.h"
11
12#include <QItemSelection>
13#include <QPainter>
14
15#include <kis_action.h>
16#include <kis_action_manager.h>
18#include <KisStorageModel.h>
19#include <KisTagModel.h>
20#include <KisResourceModel.h>
22#include <kis_assert.h>
24#include <wdgtagselection.h>
25#include <kis_paintop_factory.h>
27#include <dlg_create_bundle.h>
28#include <ResourceImporter.h>
29#include <KisResourceLocator.h>
30
32 : KoDialog(parent)
33 , m_ui(new Ui::WdgDlgResourceManager)
34 , m_actionManager(actionMgr)
35 , m_tagsController(0)
36{
37 setCaption(i18n("Manage Resources"));
38 m_page = new QWidget(this);
39 m_ui->setupUi(m_page);
40 resize(m_page->size());
44
46 m_ui->formLayout->addWidget(m_wdgResourcePreview);
47
48 connect(m_wdgResourcePreview, SIGNAL(signalResourcesSelectionChanged(QModelIndex)), this, SLOT(slotResourcesSelectionChanged(QModelIndex)));
49
50 connect(m_ui->btnDeleteResource, SIGNAL(clicked(bool)), SLOT(slotDeleteResources()));
51 connect(m_ui->btnCreateBundle, SIGNAL(clicked(bool)), SLOT(slotCreateBundle()));
52 connect(m_ui->btnOpenResourceFolder, SIGNAL(clicked(bool)), SLOT(slotOpenResourceFolder()));
53 connect(m_ui->btnImportResources, SIGNAL(clicked(bool)), SLOT(slotImportResources()));
54 connect(m_ui->btnExtractTagsToResourceFolder, SIGNAL(clicked(bool)), SLOT(slotSaveTags()));
55
56
57 m_tagsController.reset(new KisWdgTagSelectionControllerOneResource(m_ui->wdgResourcesTags, true));
58
59#ifdef Q_OS_ANDROID
60 // TODO(sh_zam): Opening a directory can cause a crash. A ContentProvider is needed for this.
61 m_ui->btnOpenResourceFolder->setEnabled(false);
62 m_ui->btnOpenResourceFolder->setVisible(false);
63#endif
64
65 // make sure the panel is properly cleared. -Amy
67}
68
72
74{
75 Q_UNUSED(index);
78 if (list.size() == 1) {
79 const QModelIndex idx = list[0];
80 m_ui->lblFilename->setText(model->data(idx, Qt::UserRole + KisAllResourcesModel::Filename).toString());
81 m_ui->lneName->setText(model->data(idx, Qt::UserRole + KisAllResourcesModel::Name).toString());
82 m_ui->lblLocation->setText(model->data(idx, Qt::UserRole + KisAllResourcesModel::Location).toString());
83 m_ui->lblId->setText(model->data(idx, Qt::UserRole + KisAllResourcesModel::Id).toString());
84
85 const QSize thumbSize = m_ui->lblThumbnail->size();
86
87 QImage thumbLabel = m_thumbnailPainter.getReadyThumbnail(idx, thumbSize*devicePixelRatioF(), palette());
88 thumbLabel.setDevicePixelRatio(devicePixelRatioF());
89
90 const QPixmap pix = QPixmap::fromImage(thumbLabel);
91 m_ui->lblThumbnail->setScaledContents(true);
92 m_ui->lblThumbnail->setPixmap(pix);
93
94 const QMap<QString, QVariant> metadata =
95 model->data(idx, Qt::UserRole + KisAllResourcesModel::MetaData).toMap();
96
97 m_ui->lblMetadata->setDisabled(false);
98 m_ui->lblFilename->setDisabled(false);
99 m_ui->lblLocation->setDisabled(false);
100 m_ui->lblThumbnail->setDisabled(false);
101 m_ui->lneName->setDisabled(false);
102 m_ui->lblId->setDisabled(false);
103 m_ui->lblMetadata->setText(constructMetadata(metadata, m_wdgResourcePreview->getCurrentResourceType()));
104 } else if (list.size() > 1) {
105
106 QString commonLocation = model->data(list.first(), Qt::UserRole + KisAllResourcesModel::Location).toString();
107 bool commonLocationFound = true;
108 Q_FOREACH(QModelIndex idx, list) {
109 QString location = model->data(idx, Qt::UserRole + KisAllResourcesModel::Location).toString();
110 if (location != commonLocation) {
111 commonLocationFound = false;
112 }
113 }
114
115 QString multipleSelectedText = i18nc("In Resource manager, this is text shown instead of filename, name or location, "
116 "when multiple resources are shown so there is no one specific filename", "(Multiple selected)");
117
118 m_ui->lblId->setText(multipleSelectedText);
119 m_ui->lblMetadata->setText("");
120 m_ui->lblFilename->setText(multipleSelectedText);
121 m_ui->lblLocation->setText(commonLocationFound ? commonLocation : multipleSelectedText);
122 m_ui->lneName->setText(multipleSelectedText);
123 m_ui->lblThumbnail->setText(multipleSelectedText);
124 QPixmap pix;
125 m_ui->lblThumbnail->setPixmap(pix);
126
127 m_ui->lblMetadata->setDisabled(true);
128 m_ui->lblFilename->setDisabled(true);
129 m_ui->lblLocation->setDisabled(!commonLocationFound);
130 m_ui->lblThumbnail->setDisabled(true);
131 m_ui->lneName->setDisabled(true);
132 m_ui->lblId->setDisabled(true);
133 } else {
134 QString noneSelectedText = i18nc("In Resource manager, this is text shown instead of filename, name or location, "
135 "when no resource is shown so there is no specific filename", "(None selected)");
136
137 m_ui->lblId->setText(noneSelectedText);
138 m_ui->lblMetadata->setText(noneSelectedText);
139 m_ui->lblFilename->setText(noneSelectedText);
140 m_ui->lblLocation->setText(noneSelectedText);
141 m_ui->lneName->setText(noneSelectedText);
142 m_ui->lblThumbnail->setText(noneSelectedText);
143 m_ui->lblThumbnail->setPixmap({});
144
145 m_ui->lblMetadata->setDisabled(true);
146 m_ui->lblFilename->setDisabled(true);
147 m_ui->lblLocation->setDisabled(true);
148 m_ui->lblThumbnail->setDisabled(true);
149 m_ui->lneName->setDisabled(true);
150 m_ui->lblId->setDisabled(true);
151 }
152
153 QList<int> resourceIds;
154 Q_FOREACH(QModelIndex idx, list) {
155 int resourceId = model->data(idx, Qt::UserRole + KisAllResourcesModel::Id).toInt();
156 resourceIds << resourceId;
157 }
159 m_tagsController->setResourceIds(m_wdgResourcePreview->getCurrentResourceType(), resourceIds);
160}
161
163{
165 QMap<QString, KisTagFilterResourceProxyModel*> resourceProxyModelsForResourceType = m_wdgResourcePreview->getResourceProxyModelsForResourceType();
166
167 if (!resourceProxyModelsForResourceType.contains(m_wdgResourcePreview->getCurrentResourceType()) || list.empty()) {
168 return;
169 }
170 KisTagFilterResourceProxyModel *model = resourceProxyModelsForResourceType[m_wdgResourcePreview->getCurrentResourceType()];
172
173 if (static_cast<QAbstractItemModel*>(model) != m_wdgResourcePreview->getModel()) {
174 qCritical() << "wrong item model!";
175 return;
176 }
177
178 // deleting a resource with "Show deleted resources" disabled will update the proxy model
179 // and next index in selection now points at wrong item.
180 QList<int> resourceIds;
181 Q_FOREACH (QModelIndex index, list) {
182 int resourceId = model->data(index, Qt::UserRole + KisResourceModel::Id).toInt();
183 resourceIds.append(resourceId);
184 }
185
186 Q_FOREACH (int resourceId, resourceIds) {
187
188 QModelIndex index = allModel->indexForResourceId(resourceId);
189 allModel->setResourceActive(index, m_undeleteMode);
190 }
191
193}
194
196{
197 ResourceImporter importer(this);
198 importer.importResources();
199
200}
201
203{
204 if (m_actionManager) {
205 KisAction *action = m_actionManager->actionByName("open_resources_directory");
206 action->trigger();
207 }
208}
209
211{
212 DlgCreateBundle* dlg = new DlgCreateBundle(0, this);
213 dlg->exec();
214}
215
216
221
223{
224 bool allActive = true;
225 bool allInactive = true;
226
227 for(QModelIndex index: list) {
228 bool active = index.data(Qt::UserRole + KisAllResourcesModel::ResourceActive).toBool();
229 allActive = allActive && active;
230 allInactive = allInactive && !active;
231 }
232
233 // if nothing selected or selected are mixed active/inactive state
234 if (allActive == allInactive) {
235 m_ui->btnDeleteResource->setEnabled(false);
236 }
237 // either all are active or all are inactive
238 else {
239 m_undeleteMode = allInactive;
240 m_ui->btnDeleteResource->setEnabled(true);
241 if (m_undeleteMode) {
242 m_ui->btnDeleteResource->setText(i18n("Undelete Resources"));
243 } else {
244 m_ui->btnDeleteResource->setText(i18n("Delete Resources"));
245 }
246 }
247}
248
249QString DlgResourceManager::constructMetadata(const QMap<QString, QVariant> &metadata, const QString &resourceType)
250{
251 QString response;
252 if (resourceType == ResourceType::PaintOpPresets) {
253 QString paintopKey = "paintopid";
254 QString paintopId = metadata.contains(paintopKey) ? metadata[paintopKey].toString() : "";
255 if (!paintopId.isEmpty()) {
256
257 KisPaintOpFactory* factory = KisPaintOpRegistry::instance()->get(paintopId);
258 if (factory) {
259 QString name = factory->name();
260 response.append(name);
261 } else {
262 response.append(i18nc("Brush engine type, in resource manager", "Engine: "));
263 response.append(paintopId);
264 }
265 }
266
267
268 } else if (resourceType == ResourceType::GamutMasks) {
269 QString descriptionKey = "description";
270 QString description = metadata.contains(descriptionKey) ? metadata[descriptionKey].toString() : "";
271 response.append(description);
272 } else {
273 Q_FOREACH(QString key, metadata.keys()) {
274 response.append(key).append(": ").append(metadata[key].toString()).append("\n");
275 }
276 }
277 return response;
278
279}
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
DlgResourceManager(KisActionManager *actionMgr, QWidget *parent=0)
void updateDeleteButtonState(const QModelIndexList &list)
QScopedPointer< KisWdgTagSelectionControllerOneResource > m_tagsController
KisActionManager * m_actionManager
WdgResourcePreview * m_wdgResourcePreview
void slotResourcesSelectionChanged(QModelIndex selected)
KisResourceThumbnailPainter m_thumbnailPainter
QScopedPointer< Ui::WdgDlgResourceManager > m_ui
static QString constructMetadata(const QMap< QString, QVariant > &metadata, const QString &resourceType)
@ ResourceActive
Whether the current resource is active.
@ MetaData
MetaData is a map of key, value pairs that is associated with this resource.
A KisActionManager class keeps track of KisActions. These actions are always associated with the GUI....
KisAction * actionByName(const QString &name) const
The KisAllresourcesModel class provides access to the cache database for a particular resource type....
QModelIndex indexForResourceId(int resourceId) const override
indexFromResource
bool setResourceActive(const QModelIndex &index, bool value) override
setResourceActive changes 'active' state of the resource
virtual QString name() const =0
static KisPaintOpRegistry * instance()
static void saveTags()
saveTags saves all tags to .tag files in the resource folder
static KisResourceLocator * instance()
static KisAllResourcesModel * resourceModel(const QString &resourceType)
QImage getReadyThumbnail(const QModelIndex &index, QSize rect, const QPalette &palette) const
The KisTagFilterResourceProxyModel class filters the resources by tag or resource name.
A dialog base class with standard buttons and predefined layouts.
Definition KoDialog.h:116
void setMainWidget(QWidget *widget)
Definition KoDialog.cpp:354
virtual void setCaption(const QString &caption)
Definition KoDialog.cpp:498
void setButtons(ButtonCodes buttonMask)
Definition KoDialog.cpp:195
void setDefaultButton(ButtonCode id)
Definition KoDialog.cpp:302
@ Close
Show Close-button. (this button closes the dialog)
Definition KoDialog.h:131
T get(const QString &id) const
void importResources(QString startPath="")
QMap< QString, KisTagFilterResourceProxyModel * > getResourceProxyModelsForResourceType()
QModelIndexList getResourceItemsSelected()
QAbstractItemModel * getModel()
const QString GamutMasks
const QString PaintOpPresets
rgba palette[MAX_PALETTE]
Definition palette.c:35