diff --git a/libs/resources/KisStorageModel.cpp b/libs/resources/KisStorageModel.cpp index b2ed43d923..afd93caef0 100644 --- a/libs/resources/KisStorageModel.cpp +++ b/libs/resources/KisStorageModel.cpp @@ -1,193 +1,230 @@ /* * Copyright (c) 2019 boud * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "KisStorageModel.h" #include #include Q_GLOBAL_STATIC(KisStorageModel, s_instance) struct KisStorageModel::Private { int cachedRowCount {-1}; QSqlQuery query; }; KisStorageModel::KisStorageModel(QObject *parent) : QAbstractTableModel(parent) , d(new Private()) { prepareQuery(); } KisStorageModel *KisStorageModel::instance() { return s_instance; } KisStorageModel::~KisStorageModel() { } int KisStorageModel::rowCount(const QModelIndex & /*parent*/) const { if (d->cachedRowCount < 0) { QSqlQuery q; q.prepare("SELECT count(*)\n" "FROM storages\n"); q.exec(); q.first(); const_cast(this)->d->cachedRowCount = q.value(0).toInt(); } return d->cachedRowCount; } int KisStorageModel::columnCount(const QModelIndex &/*parent*/) const { return 6; } QVariant KisStorageModel::data(const QModelIndex &index, int role) const { QVariant v; if (!index.isValid()) return v; if (index.row() > rowCount()) return v; if (index.column() > (int)Active) return v; bool pos = d->query.seek(index.row()); if (pos) { switch(role) { case Qt::DisplayRole: { switch(index.column()) { case Id: return d->query.value("id"); case StorageType: return d->query.value("storage_type"); case Location: return d->query.value("location"); case TimeStamp: return d->query.value("timestamp"); case PreInstalled: return d->query.value("pre_installed"); case Active: return d->query.value("active"); case Thumbnail: { QByteArray ba = d->query.value("thumbnail").toByteArray(); QBuffer buf(&ba); buf.open(QBuffer::ReadOnly); QImage img; img.load(&buf, "PNG"); return QVariant::fromValue(img); } default: return v; } } case Qt::UserRole + Id: return d->query.value("id"); case Qt::UserRole + StorageType: return d->query.value("storage_type"); case Qt::UserRole + Location: return d->query.value("location"); case Qt::UserRole + TimeStamp: return d->query.value("timestamp"); case Qt::UserRole + PreInstalled: return d->query.value("pre_installed"); case Qt::UserRole + Active: return d->query.value("active"); case Qt::UserRole + Thumbnail: { QByteArray ba = d->query.value("thumbnail").toByteArray(); QBuffer buf(&ba); buf.open(QBuffer::ReadOnly); QImage img; img.load(&buf, "PNG"); return QVariant::fromValue(img); } default: ; } } return v; } bool KisStorageModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (index.isValid()) { if (role == Qt::CheckStateRole) { QSqlQuery q; bool r = q.prepare("UPDATE storages\n" "SET active = :active\n" "WHERE id = :id\n"); q.bindValue(":active", value); q.bindValue(":id", index.data(Qt::UserRole + Id)); if (!r) { qWarning() << "Could not prepare KisStorageModel update query" << d->query.lastError(); return false; } r = q.exec(); if (!r) { qWarning() << "Could not execute KisStorageModel update query" << d->query.lastError(); return false; } } } QAbstractTableModel::setData(index, value, role); return prepareQuery(); } Qt::ItemFlags KisStorageModel::flags(const QModelIndex &index) const { return QAbstractTableModel::flags(index) | Qt::ItemIsEditable; } KisResourceStorageSP KisStorageModel::storageForId(const QModelIndex &index) const { return KisResourceLocator::instance()->storageByLocation(index.data(Qt::UserRole + Location).toString()); } +QVariant KisStorageModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + QVariant v = QVariant(); + if (role != Qt::DisplayRole) { + return v; + } + if (orientation == Qt::Horizontal) { + switch(section) { + case 0: + v = i18n("Id"); + break; + case 1: + v = i18n("Type"); + break; + case 2: + v = i18n("Location"); + break; + case 3: + v = i18n("Creation Date"); + break; + case 4: + v = i18n("Preinstalled"); + break; + case 5: + v = i18n("Active"); + break; + case 6: + v = i18n("Thumbnail"); + break; + default: + v = QString::number(section); + } + return v; + } + return QAbstractTableModel::headerData(section, orientation, role); +} + bool KisStorageModel::prepareQuery() { beginResetModel(); bool r = d->query.prepare("SELECT storages.id as id\n" ", storage_types.name as storage_type\n" ", location\n" ", timestamp\n" ", pre_installed\n" ", active\n" ", thumbnail\n" "FROM storages\n" ", storage_types\n" "WHERE storages.storage_type_id = storage_types.id\n"); if (!r) { qWarning() << "Could not prepare KisStorageModel query" << d->query.lastError(); } r = d->query.exec(); if (!r) { qWarning() << "Could not execute KisStorageModel query" << d->query.lastError(); } d->cachedRowCount = -1; endResetModel(); return r; } diff --git a/libs/resources/KisStorageModel.h b/libs/resources/KisStorageModel.h index 61c47be1b3..1c2021536f 100644 --- a/libs/resources/KisStorageModel.h +++ b/libs/resources/KisStorageModel.h @@ -1,68 +1,70 @@ /* * Copyright (c) 2019 boud * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef KISSTORAGEMODEL_H #define KISSTORAGEMODEL_H #include #include #include #include "KisResourceStorage.h" #include "kritaresources_export.h" class KRITARESOURCES_EXPORT KisStorageModel : public QAbstractTableModel { public: enum Columns { Id = 0, StorageType, Location, TimeStamp, PreInstalled, Active, Thumbnail }; static KisStorageModel * instance(); KisStorageModel(QObject *parent = 0); ~KisStorageModel() override; int rowCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role) const override; bool setData(const QModelIndex &index, const QVariant &value, int role) override; Qt::ItemFlags flags(const QModelIndex &index) const override; KisResourceStorageSP storageForId(const QModelIndex &index) const; + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; + private: KisStorageModel(const KisStorageModel&); KisStorageModel operator=(const KisStorageModel&); bool prepareQuery(); struct Private; QScopedPointer d; }; #endif // KISSTORAGEMODEL_H diff --git a/plugins/extensions/resourcemanager/wdgdlgbundlemanager.ui b/plugins/extensions/resourcemanager/wdgdlgbundlemanager.ui index 0c3d5872fc..f8913cf172 100644 --- a/plugins/extensions/resourcemanager/wdgdlgbundlemanager.ui +++ b/plugins/extensions/resourcemanager/wdgdlgbundlemanager.ui @@ -1,362 +1,371 @@ WdgDlgBundleManager 0 0 778 572 48 48 + + true + + + true + Qt::Horizontal 40 20 Import a resource library ... - + + .. Create a resource library ... - + + .. Delete a resource library ... - + + .. 0 0 0 0 0 0 Qt::Horizontal false 10 0 Bundle Name Qt::AutoText Qt::Horizontal 40 20 0 10 0 50 true 0 0 128 128 128 128 QFrame::StyledPanel QFrame::Plain true Qt::AlignCenter Qt::Vertical 20 0 3 0 0 Author: false Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter License: Created: Email: Updated: Website: Qt::Vertical 20 0 tableView bnAdd bnNew bnDelete lblDescription