diff --git a/libs/resources/KisStorageModel.cpp b/libs/resources/KisStorageModel.cpp index 2c2994c9bc..0418646712 100644 --- a/libs/resources/KisStorageModel.cpp +++ b/libs/resources/KisStorageModel.cpp @@ -1,170 +1,168 @@ /* * 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 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"); 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"); default: ; } } return v; } bool KisStorageModel::setData(const QModelIndex &index, const QVariant &value, int role) { - qDebug() << "attempting to set data..." << index; if (index.isValid()) { if (role == Qt::CheckStateRole) { - qDebug() << "checkstate role" << value.toBool(); 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; } 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" "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/resourcewidgets/KisStorageChooserWidget.cpp b/libs/resourcewidgets/KisStorageChooserWidget.cpp index d47d3c01d7..fd2343f461 100644 --- a/libs/resourcewidgets/KisStorageChooserWidget.cpp +++ b/libs/resourcewidgets/KisStorageChooserWidget.cpp @@ -1,139 +1,106 @@ /* This file is part of the KDE project * Copyright (C) 2019 Wolthera van Hövell tot Westerflier * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library 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 * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include #include #include #include #include -#include -#include -#include + #include -#include -#include #include "KisStorageChooserWidget.h" #include "KisStorageModel.h" #include -KisStorageChooserDelegate::KisStorageChooserDelegate(QAbstractItemView *itemView, QObject *parent) - : KWidgetItemDelegate(itemView, parent) -{ -} - -QList KisStorageChooserDelegate::createItemWidgets(const QModelIndex &index) const +KisStorageChooserDelegate::KisStorageChooserDelegate(QObject *parent) + : QAbstractItemDelegate(parent) { - QList widgetList; - QWidget *page = new QWidget; - QHBoxLayout *layout = new QHBoxLayout(page); - - QCheckBox *checkBox = new QCheckBox; - checkBox->setProperty("storageItem", index); - QLabel *thumbnail = new QLabel; - QLabel *filename = new QLabel; - filename->setWordWrap(true); - - layout->addWidget(checkBox); - layout->addWidget(thumbnail); - layout->addWidget(filename); - - page->setFixedSize(400, 100); - - return widgetList << page; } -void KisStorageChooserDelegate::updateItemWidgets(const QList widgets, const QStyleOptionViewItem &option, const QPersistentModelIndex &index) const +void KisStorageChooserDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { - if (option.rect == QRect()) return; + QString location = index.data(Qt::UserRole + KisStorageModel::Location).value(); + bool active = index.data(Qt::UserRole + KisStorageModel::Active).value(); - QWidget* page= widgets[0]; - QHBoxLayout* layout = qobject_cast(page->layout()); - QCheckBox *checkBox = qobject_cast(layout->itemAt(0)->widget()); - QLabel *thumbnail = qobject_cast(layout->itemAt(1)->widget()); - QLabel *filename = qobject_cast(layout->itemAt(2)->widget()); - - checkBox->setChecked(index.data(Qt::UserRole + KisStorageModel::Active).value()); - checkBox->setFixedWidth(checkBox->height()); - connect(checkBox, SIGNAL(toggled(bool)), this, SLOT(toggleStorage(bool)), Qt::UniqueConnection); + if (option.state & QStyle::State_Selected) { + painter->fillRect(option.rect, option.palette.highlight()); + } QString storageType = index.data(Qt::UserRole + KisStorageModel::StorageType).value(); + QPixmap picture = QPixmap(option.decorationSize); if (storageType == "Folder") { - thumbnail->setPixmap(koIcon("document-open").pixmap(option.decorationSize)); + picture = koIcon("document-open").pixmap(option.decorationSize); } else if (storageType == "Memory") { - thumbnail->setPixmap(koIcon("document-new").pixmap(option.decorationSize)); + picture = koIcon("document-new").pixmap(option.decorationSize); } else { - thumbnail->setPixmap(koIcon("bundle_archive").pixmap(option.decorationSize)); + picture = koIcon("bundle_archive").pixmap(option.decorationSize); } - thumbnail->setFixedSize(option.decorationSize); - filename->setText(index.data(Qt::UserRole + KisStorageModel::Location).value()); - - // move the page _up_ otherwise it will draw relative to the actual position - page->setGeometry(option.rect.translated(0, -option.rect.y())); -} - -void KisStorageChooserDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const -{ - QStyleOptionViewItem opt = option; - itemView()->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, 0); - /** - QString location = i; if (location.isEmpty()) { location = QString::number(index.row()); } QColor penColor(option.palette.text().color()); - //penColor.setAlphaF(0.6); + if (!active) { + penColor.setAlphaF(0.6); + } painter->setPen(penColor); painter->drawText(option.rect, 0, location); - QApplication::style()->drawControl(QStyle::CE_PushButtonBevel, &option, painter, 0); - **/ -} -QSize KisStorageChooserDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const -{ - return QSize(400, 100); + if (!active) { + QApplication::style()->drawControl(QStyle::CE_PushButton, &option, painter, 0); + } + } -void KisStorageChooserDelegate::toggleStorage(bool toggle) +QSize KisStorageChooserDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { - QModelIndex index = sender()->property("storageItem").value(); - if (index.isValid()) { - KisStorageModel::instance()->setData(index, QVariant(toggle), Qt::CheckStateRole); - } + int w = 400; + int h = option.decorationSize.height(); + return QSize(w, h); } KisStorageChooserWidget::KisStorageChooserWidget(QWidget *parent) : KisPopupButton(parent) { QListView *view = new QListView(this); -// view->setModel(KisStorageModel::instance()); -// view->setIconSize(QSize(80, 80)); -// view->setItemDelegate(new KisStorageChooserDelegate(view, this)); + view->setModel(KisStorageModel::instance()); + view->setIconSize(QSize(32, 32)); + view->setItemDelegate(new KisStorageChooserDelegate(this)); + view->setSelectionMode(QAbstractItemView::SingleSelection); + connect(view, SIGNAL(clicked(QModelIndex)), this, SLOT(activated(QModelIndex))); this->setPopupWidget(view); } +void KisStorageChooserWidget::activated(const QModelIndex &index) +{ + if (!index.isValid()) return; + + bool active = index.data(Qt::UserRole + KisStorageModel::Active).value(); + KisStorageModel::instance()->setData(index, !active, Qt::CheckStateRole); +} + KisStorageChooserWidget::~KisStorageChooserWidget() { } diff --git a/libs/resourcewidgets/KisStorageChooserWidget.h b/libs/resourcewidgets/KisStorageChooserWidget.h index c5ffbca97a..5c22f50eb4 100644 --- a/libs/resourcewidgets/KisStorageChooserWidget.h +++ b/libs/resourcewidgets/KisStorageChooserWidget.h @@ -1,62 +1,57 @@ /* This file is part of the KDE project * Copyright (C) 2019 Wolthera van Hövell tot Westerflier * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library 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 * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #ifndef KISSTORAGECHOOSERWIDGET_H #define KISSTORAGECHOOSERWIDGET_H #include -#include +#include #include #include "kritaresourcewidgets_export.h" -class KRITARESOURCEWIDGETS_EXPORT KisStorageChooserDelegate : public KWidgetItemDelegate +class KRITARESOURCEWIDGETS_EXPORT KisStorageChooserDelegate : public QAbstractItemDelegate { Q_OBJECT public: - explicit KisStorageChooserDelegate(QAbstractItemView *itemView, QObject *parent = 0); + explicit KisStorageChooserDelegate(QObject *parent = 0); ~KisStorageChooserDelegate() override {} - QList createItemWidgets(const QModelIndex& index) const override; - - void updateItemWidgets(const QList widgets, - const QStyleOptionViewItem &option, - const QPersistentModelIndex &index) const override; - void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const override; QSize sizeHint ( const QStyleOptionViewItem &, const QModelIndex & ) const override; -private Q_SLOTS: - void toggleStorage(bool toggle); - }; class KRITARESOURCEWIDGETS_EXPORT KisStorageChooserWidget : public KisPopupButton { Q_OBJECT public: KisStorageChooserWidget(QWidget *parent = 0); ~KisStorageChooserWidget(); + +private Q_SLOTS: + void activated(const QModelIndex &index); + }; #endif // KISSTORAGECHOOSERWIDGET_H