File indexing completed on 2024-12-22 04:15:05
0001 /* 0002 * SPDX-FileCopyrightText: 2018 Boudewijn Rempt <boud@valdyas.org> 0003 * SPDX-FileCopyrightText: 2023 L. E. Segovia <amy@amyspark.me> 0004 * 0005 * SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 #include "DlgDbExplorer.h" 0009 0010 #include <klocalizedstring.h> 0011 #include <kis_debug.h> 0012 0013 #include <QDataWidgetMapper> 0014 #include <QTableView> 0015 #include <QtSql> 0016 #include <QStyledItemDelegate> 0017 0018 #include <KisResourceItemDelegate.h> 0019 #include <KisResourceItemListView.h> 0020 #include <KisResourceModel.h> 0021 #include <KisResourceTypeModel.h> 0022 #include <KisStorageModel.h> 0023 #include <KisTagFilterResourceProxyModel.h> 0024 #include <KisTagModel.h> 0025 #include <KisTagResourceModel.h> 0026 0027 #include "KisResourceCacheDb.h" 0028 #include "KisResourceModelProvider.h" 0029 #include "TableModel.h" 0030 0031 DlgDbExplorer::DlgDbExplorer(QWidget *parent) 0032 : KoDialog(parent) 0033 { 0034 setCaption(i18n("Resources Cache Database Explorer")); 0035 0036 setButtons(Ok); 0037 0038 m_page = new WdgDbExplorer(this); 0039 Q_CHECK_PTR(m_page); 0040 0041 setMainWidget(m_page); 0042 0043 m_resourceTypeModel = new KisResourceTypeModel(this); 0044 m_tagModel = new KisTagModel("", this); 0045 0046 { 0047 KisStorageModel *model = new KisStorageModel(this); 0048 TableDelegate *delegate = new TableDelegate(m_page->tableResources); 0049 m_page->tableStorages->setModel(model); 0050 delegate->addDateTimeColumn(3); // creation date 0051 delegate->addBooleanColumn(4); // preinstalled 0052 delegate->addBooleanColumn(5); // active 0053 m_page->tableStorages->setItemDelegate(delegate); 0054 m_page->tableStorages->hideColumn(0); 0055 m_page->tableStorages->setSelectionMode(QAbstractItemView::SingleSelection); 0056 m_page->tableStorages->resizeColumnsToContents(); 0057 } 0058 0059 { 0060 KisResourceModel *resourcesModel = new KisResourceModel(ResourceType::Brushes, this); 0061 TableDelegate *resourcesDelegate = new TableDelegate(m_page->tableResources); 0062 resourcesDelegate->addBooleanColumn(6); // Status 0063 resourcesDelegate->addBooleanColumn(12); // Dirty 0064 resourcesDelegate->addBooleanColumn(14); // Active 0065 m_page->tableResources->setModel(resourcesModel); 0066 m_page->tableResources->setItemDelegate(resourcesDelegate); 0067 m_page->tableResources->hideColumn(0); 0068 m_page->tableResources->setSelectionMode(QAbstractItemView::SingleSelection); 0069 0070 m_page->cmbResourceTypes->setModel(m_resourceTypeModel); 0071 m_page->cmbResourceTypes->setModelColumn(KisResourceTypeModel::Name); 0072 0073 connect(m_page->cmbResourceTypes, SIGNAL(activated(int)), SLOT(slotTbResourceTypeSelected(int))); 0074 connect(m_page->tableResources, SIGNAL(clicked(QModelIndex)), SLOT(slotTbResourceItemSelected())); 0075 } 0076 0077 { 0078 TableModel *resourceVersionsModel = new TableModel(this, QSqlDatabase::database()); 0079 TableDelegate *versionsDelegate = new TableDelegate(m_page->tableVersions); 0080 resourceVersionsModel->setTable("versioned_resources"); 0081 resourceVersionsModel->setHeaderData(0, Qt::Horizontal, i18n("Id")); 0082 resourceVersionsModel->setHeaderData(1, Qt::Horizontal, i18n("Resource ID")); 0083 resourceVersionsModel->setHeaderData(2, Qt::Horizontal, i18n("Storage ID")); 0084 resourceVersionsModel->setHeaderData(3, Qt::Horizontal, i18n("Version")); 0085 resourceVersionsModel->setHeaderData(4, Qt::Horizontal, i18n("File name")); 0086 resourceVersionsModel->setHeaderData(5, Qt::Horizontal, i18n("md5sum")); 0087 resourceVersionsModel->setHeaderData(6, Qt::Horizontal, i18n("Creation Date")); 0088 resourceVersionsModel->addDateTimeColumn(6); 0089 versionsDelegate->addDateTimeColumn(6); 0090 resourceVersionsModel->select(); 0091 0092 m_page->tableVersions->setModel(resourceVersionsModel); 0093 m_page->tableVersions->hideColumn(0); 0094 m_page->tableVersions->setItemDelegate(versionsDelegate); 0095 m_page->tableVersions->setSelectionMode(QAbstractItemView::NoSelection); 0096 m_page->tableVersions->resizeColumnsToContents(); 0097 } 0098 0099 { 0100 TableModel *tagsModel = new TableModel(this, QSqlDatabase::database()); 0101 TableDelegate *tagsDelegate = new TableDelegate(m_page->tableStorages); 0102 tagsModel->setTable("tags"); 0103 tagsModel->setHeaderData(0, Qt::Horizontal, i18n("Id")); 0104 tagsModel->setHeaderData(1, Qt::Horizontal, i18n("Type")); 0105 tagsModel->setRelation(1, QSqlRelation("resource_types", "id", "name")); 0106 tagsModel->setHeaderData(2, Qt::Horizontal, i18n("Tag")); 0107 tagsModel->setHeaderData(3, Qt::Horizontal, i18n("Name")); 0108 tagsModel->setHeaderData(4, Qt::Horizontal, i18n("Comment")); 0109 tagsModel->setHeaderData(5, Qt::Horizontal, i18n("File name")); 0110 tagsModel->setHeaderData(6, Qt::Horizontal, i18n("Active")); 0111 tagsModel->setHeaderData(7, Qt::Horizontal, i18n("Thumbnail")); 0112 tagsModel->setHeaderData(8, Qt::Horizontal, i18n("Display Name")); 0113 tagsModel->addBooleanColumn(6); 0114 tagsDelegate->addBooleanColumn(6); 0115 tagsModel->select(); 0116 0117 m_page->tableTags->setModel(tagsModel); 0118 m_page->tableTags->hideColumn(0); 0119 m_page->tableTags->setItemDelegate(tagsDelegate); 0120 m_page->tableTags->setSelectionMode(QAbstractItemView::SingleSelection); 0121 m_page->tableTags->resizeColumnsToContents(); 0122 0123 connect(m_page->tableTags, &QTableView::clicked, this, &DlgDbExplorer::slotTbTagSelected); 0124 } 0125 0126 { 0127 KisTagResourceModel *model = new KisTagResourceModel(ResourceType::Brushes, this); 0128 TableDelegate *delegate = new TableDelegate(m_page->tableTaggedResources); 0129 delegate->addBooleanColumn(6); // status 0130 delegate->addBooleanColumn(12); // dirty 0131 delegate->addBooleanColumn(14); // resource active 0132 delegate->addBooleanColumn(15); // storage active 0133 delegate->addBooleanColumn(20); // resource active 0134 delegate->addBooleanColumn(21); // tag active 0135 delegate->addBooleanColumn(22); // storage active 0136 0137 m_page->tableTaggedResources->setModel(model); 0138 m_page->tableTaggedResources->setItemDelegate(delegate); 0139 m_page->tableTaggedResources->setSelectionMode(QAbstractItemView::NoSelection); 0140 m_page->tableTaggedResources->resizeColumnsToContents(); 0141 } 0142 0143 { 0144 TableModel *versionModel = new TableModel(this, QSqlDatabase::database()); 0145 TableDelegate *delegate = new TableDelegate(m_page->tableSchema); 0146 versionModel->setTable("version_information"); 0147 versionModel->setHeaderData(0, Qt::Horizontal, i18n("Id")); 0148 versionModel->setHeaderData(1, Qt::Horizontal, i18n("Database version")); 0149 versionModel->setHeaderData(2, Qt::Horizontal, i18n("Krita version")); 0150 versionModel->setHeaderData(3, Qt::Horizontal, i18n("Upgrade date")); 0151 versionModel->addDateTimeColumn(3); 0152 delegate->addDateTimeColumn(3); 0153 versionModel->select(); 0154 0155 const QSqlRecord r = versionModel->record(0); 0156 m_page->lblDatabaseVersion->setText(r.value(1).toString()); 0157 m_page->lblKritaVersion->setText(r.value(2).toString()); 0158 m_page->lblCreationDate->setText(QDateTime::fromSecsSinceEpoch(r.value(3).value<int>()).toString()); 0159 0160 m_page->tableSchema->setModel(versionModel); 0161 m_page->tableSchema->hideColumn(0); 0162 m_page->tableSchema->setItemDelegate(delegate); 0163 m_page->tableSchema->setSelectionMode(QAbstractItemView::NoSelection); 0164 m_page->tableSchema->resizeColumnsToContents(); 0165 } 0166 0167 0168 { 0169 m_page->cmbRvResourceTypes->setModel(m_resourceTypeModel); 0170 m_page->cmbRvResourceTypes->setModelColumn(KisResourceTypeModel::Name); 0171 connect(m_page->cmbRvResourceTypes, SIGNAL(activated(int)), SLOT(slotRvResourceTypeSelected(int))); 0172 0173 m_page->cmbRvTags->setModelColumn(KisAllTagsModel::Name); 0174 m_page->cmbRvTags->setModel(m_tagModel); 0175 connect(m_page->cmbRvTags, SIGNAL(activated(int)), SLOT(slotRvTagSelected(int))); 0176 0177 m_page->cmbRvResourceTypes->setCurrentIndex(0); 0178 slotRvResourceTypeSelected(0); 0179 0180 m_page->resourceItemView->setItemDelegate(new KisResourceItemDelegate(this)); 0181 m_page->resourceItemView->setSelectionMode(QAbstractItemView::SingleSelection); 0182 } 0183 0184 } 0185 0186 DlgDbExplorer::~DlgDbExplorer() 0187 { 0188 } 0189 0190 void DlgDbExplorer::updateTagModel(const QString& resourceType) 0191 { 0192 m_tagModel = new KisTagModel(resourceType, this); 0193 m_page->cmbRvTags->setModelColumn(KisAllTagsModel::Name); 0194 m_page->cmbRvTags->setModel(m_tagModel); 0195 m_page->cmbRvTags->update(); 0196 qDebug() << "number of tags in " << resourceType << " tag model: " << m_tagModel->rowCount(); 0197 } 0198 0199 void DlgDbExplorer::slotRvResourceTypeSelected(int index) 0200 { 0201 QModelIndex idx = m_page->cmbResourceTypes->model()->index(index, KisResourceTypeModel::ResourceType); 0202 QString resourceType = idx.data(Qt::DisplayRole).toString(); 0203 0204 updateTagModel(resourceType); 0205 0206 KisTagFilterResourceProxyModel *tagFilterModel = new KisTagFilterResourceProxyModel(resourceType, this); 0207 0208 m_filterProxyModel = tagFilterModel; 0209 0210 m_page->resourceItemView->setModel(tagFilterModel); 0211 } 0212 0213 void DlgDbExplorer::slotTbResourceTypeSelected(int index) 0214 { 0215 QModelIndex idx = m_page->cmbRvResourceTypes->model()->index(index, KisResourceTypeModel::ResourceType); 0216 QString resourceType = idx.data(Qt::DisplayRole).toString(); 0217 0218 m_tagModel = new KisTagModel(resourceType, this); 0219 0220 KisResourceModel *resourceModel = new KisResourceModel(resourceType, this); 0221 m_page->tableResources->setModel(resourceModel); 0222 m_page->tableResources->setCurrentIndex(m_page->tableResources->model()->index(0, 0)); 0223 slotTbResourceItemSelected(); 0224 m_page->tableResources->resizeColumnsToContents(); 0225 } 0226 0227 void DlgDbExplorer::slotTbResourceItemSelected() 0228 { 0229 if (m_page->tableResources->selectionModel()->selectedIndexes().isEmpty()) return; 0230 0231 QModelIndex idx = m_page->tableResources->selectionModel()->selectedIndexes().first(); 0232 0233 QImage thumb = idx.data(Qt::UserRole + KisAbstractResourceModel::Thumbnail).value<QImage>(); 0234 Qt::TransformationMode mode = Qt::SmoothTransformation; 0235 if (thumb.size().width() < 100 && thumb.size().height() < 100) { 0236 mode = Qt::FastTransformation; 0237 } 0238 0239 if (!thumb.isNull()) { 0240 m_page->lblThumbnail->setPixmap(QPixmap::fromImage(thumb.scaled(100, 100, Qt::KeepAspectRatio, mode))); 0241 } else { 0242 m_page->lblThumbnail->setPixmap(QPixmap()); 0243 } 0244 0245 TableModel *model = dynamic_cast<TableModel *>(m_page->tableVersions->model()); 0246 0247 if (model) { 0248 const QString md5 = idx.data(Qt::UserRole + KisAbstractResourceModel::MD5).value<QString>(); 0249 model->setFilter(QString("md5sum='%1'").arg(md5)); 0250 m_page->tableVersions->resizeColumnsToContents(); 0251 } 0252 } 0253 0254 void DlgDbExplorer::slotTbTagSelected(const QModelIndex &index) 0255 { 0256 // 0 == tag ID 0257 const QModelIndex idx = m_page->tableTags->model()->index(index.row(), 0); 0258 // get tag's resource type 0259 const QModelIndex idx2 = m_page->tableTags->model()->index(index.row(), 1); 0260 0261 KisTagResourceModel *newModel = new KisTagResourceModel(idx2.data().value<QString>(), this); 0262 newModel->setResourceFilter(KisTagResourceModel::ShowAllResources); 0263 newModel->setTagsFilter(QVector<int>{idx.data().value<int>()}); 0264 QAbstractItemModel *oldModel = m_page->tableTaggedResources->model(); 0265 m_page->tableTaggedResources->setModel(newModel); 0266 m_page->tableTaggedResources->resizeColumnsToContents(); 0267 oldModel->deleteLater(); 0268 } 0269 0270 void DlgDbExplorer::slotRvTagSelected(int index) 0271 { 0272 qDebug() << "selected tag" << index; 0273 QModelIndex idx = m_tagModel->index(index, 0); 0274 KisTagSP tag = m_tagModel->tagForIndex(idx); 0275 0276 if (m_filterProxyModel && !tag.isNull() && tag->valid()) { 0277 m_filterProxyModel->setTagFilter(tag); 0278 m_page->tableTaggedResources->resizeColumnsToContents(); 0279 } 0280 }