diff --git a/plugins/dockers/snapshotdocker/CMakeLists.txt b/plugins/dockers/snapshotdocker/CMakeLists.txt index 7516df8e14..0d3c5ba7fa 100644 --- a/plugins/dockers/snapshotdocker/CMakeLists.txt +++ b/plugins/dockers/snapshotdocker/CMakeLists.txt @@ -1,9 +1,10 @@ set(kritasnapshotdocker_SOURCES KisSnapshotModel.cpp SnapshotDocker.cpp SnapshotPlugin.cpp + KisSnapshotView.cpp ) add_library(kritasnapshotdocker MODULE ${kritasnapshotdocker_SOURCES}) target_link_libraries(kritasnapshotdocker kritaimage kritaui) install(TARGETS kritasnapshotdocker DESTINATION ${KRITA_PLUGIN_INSTALL_DIR}) \ No newline at end of file diff --git a/plugins/dockers/snapshotdocker/KisSnapshotModel.cpp b/plugins/dockers/snapshotdocker/KisSnapshotModel.cpp index 9d27342b48..9b95b76511 100644 --- a/plugins/dockers/snapshotdocker/KisSnapshotModel.cpp +++ b/plugins/dockers/snapshotdocker/KisSnapshotModel.cpp @@ -1,176 +1,196 @@ /* * Copyright (c) 2019 Tusooa Zhu * * 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 "KisSnapshotModel.h" #include #include #include #include #include #include #include #include struct KisSnapshotModel::Private { Private(); virtual ~Private(); QPointer curDocument(); bool switchToDocument(QPointer doc); using DocPList = QList > >; DocPList curDocList; QMap, DocPList> documentGroups; QPointer curCanvas; }; KisSnapshotModel::Private::Private() { } KisSnapshotModel::Private::~Private() { } QPointer KisSnapshotModel::Private::curDocument() { if (curCanvas && curCanvas->imageView()) { return curCanvas->imageView()->document(); } return 0; } bool KisSnapshotModel::Private::switchToDocument(QPointer doc) { if (curCanvas && curCanvas->imageView()) { KisView *view = curCanvas->imageView(); KisDocument *curDoc = curDocument(); if (curDoc && doc) { curDoc->copyFromDocument(*doc); } // FIXME: more things need to be done return true; } return false; } KisSnapshotModel::KisSnapshotModel() : QAbstractListModel() , m_d(new Private) { } KisSnapshotModel::~KisSnapshotModel() { } int KisSnapshotModel::rowCount(const QModelIndex &parent) const { if (parent.isValid()) { return 0; } else { return m_d->curDocList.size(); } } QVariant KisSnapshotModel::data(const QModelIndex &index, int role) const { if (!index.isValid() || index.row() >= rowCount(QModelIndex())) { return QVariant(); } int i = index.row(); switch (role) { case Qt::DisplayRole: case Qt::EditRole: return m_d->curDocList[i].first; break; } return QVariant(); } bool KisSnapshotModel::setData(const QModelIndex &index, const QVariant &value, int role) { + if (!index.isValid() || index.row() >= rowCount(QModelIndex())) { + return false; + } + int i = index.row(); + switch (role) { + case Qt::DisplayRole: + case Qt::EditRole: + m_d->curDocList[i].first = value.toString(); + emit dataChanged(index, index); + return true; + break; + } return false; } void KisSnapshotModel::setCanvas(QPointer canvas) { if (m_d->curCanvas == canvas) { return; } if (m_d->curCanvas) { if (m_d->curDocument()) { m_d->documentGroups.insert(m_d->curDocument(), m_d->curDocList); } else { Q_FOREACH (auto const &i, m_d->curDocList) { delete i.second.data(); } } } if (!m_d->curDocList.isEmpty()) { beginRemoveRows(QModelIndex(), 0, m_d->curDocList.size() - 1); m_d->curDocList.clear(); endRemoveRows(); } m_d->curCanvas = canvas; QPointer curDoc = m_d->curDocument(); if (curDoc) { QMap, Private::DocPList>::const_iterator i = m_d->documentGroups.constFind(curDoc); if (i != m_d->documentGroups.constEnd()) { Private::DocPList docList = i.value(); beginInsertRows(QModelIndex(), docList.size(), docList.size()); m_d->curDocList = docList; endInsertRows(); } // we have not found any existing group containing the current document } } bool KisSnapshotModel::slotCreateSnapshot() { QPointer clonedDoc(m_d->curDocument()->lockAndCloneForSaving()); if (clonedDoc) { beginInsertRows(QModelIndex(), m_d->curDocList.size(), m_d->curDocList.size()); m_d->curDocList << qMakePair(i18n("Snapshot"), clonedDoc); endInsertRows(); return true; } return false; } -bool KisSnapshotModel::slotRemoveActivatedSnapshot() +bool KisSnapshotModel::slotRemoveSnapshot(const QModelIndex &index) { - return false; + if (!index.isValid() || index.row() >= m_d->curDocList.size()) { + return false; + } + int i = index.row(); + beginRemoveRows(QModelIndex(), i, i); + QPair > pair = m_d->curDocList.takeAt(i); + endRemoveRows(); + delete pair.second.data(); + return true; } -bool KisSnapshotModel::slotSwitchToActivatedSnapshot(const QModelIndex &index) +bool KisSnapshotModel::slotSwitchToSnapshot(const QModelIndex &index) { if (!index.isValid() || index.row() >= m_d->curDocList.size()) { return false; } return m_d->switchToDocument(m_d->curDocList[index.row()].second); } diff --git a/plugins/dockers/snapshotdocker/KisSnapshotModel.h b/plugins/dockers/snapshotdocker/KisSnapshotModel.h index ada554a9ae..e58b579952 100644 --- a/plugins/dockers/snapshotdocker/KisSnapshotModel.h +++ b/plugins/dockers/snapshotdocker/KisSnapshotModel.h @@ -1,49 +1,49 @@ /* * Copyright (c) 2019 Tusooa Zhu * * 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 KIS_SNAPSHOT_MODEL_H_ #define KIS_SNAPSHOT_MODEL_H_ #include #include #include #include class KisSnapshotModel : public QAbstractListModel { public: KisSnapshotModel(); ~KisSnapshotModel() override; int rowCount(const QModelIndex &parent) const override; QVariant data(const QModelIndex &index, int role) const override; bool setData(const QModelIndex &index, const QVariant &value, int role) override; void setCanvas(QPointer canvas); public Q_SLOTS: bool slotCreateSnapshot(); - bool slotRemoveActivatedSnapshot(); - bool slotSwitchToActivatedSnapshot(const QModelIndex &index); + bool slotRemoveSnapshot(const QModelIndex &index); + bool slotSwitchToSnapshot(const QModelIndex &index); private: struct Private; QScopedPointer m_d; }; #endif // KIS_SNAPSHOT_MODEL_H_ diff --git a/plugins/dockers/snapshotdocker/KisSnapshotView.cpp b/plugins/dockers/snapshotdocker/KisSnapshotView.cpp new file mode 100644 index 0000000000..75ef7c1bd7 --- /dev/null +++ b/plugins/dockers/snapshotdocker/KisSnapshotView.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2019 Tusooa Zhu + * + * 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 "KisSnapshotView.h" +#include "KisSnapshotModel.h" + +#include + +struct KisSnapshotView::Private +{ + KisSnapshotModel *model; +}; + +KisSnapshotView::KisSnapshotView() + : QListView() + , m_d(new Private) +{ +} + +KisSnapshotView::~KisSnapshotView() +{ +} + +void KisSnapshotView::setModel(QAbstractItemModel *model) +{ + KisSnapshotModel *snapshotModel = dynamic_cast(model); + if (snapshotModel) { + QListView::setModel(model); + m_d->model = snapshotModel; + } +} + +void KisSnapshotView::slotSwitchToSelectedSnapshot() +{ + KIS_ASSERT_RECOVER_RETURN(m_d->model); + QModelIndexList indexes = selectedIndexes(); + if (indexes.size() != 1) { + return; + } + m_d->model->slotSwitchToSnapshot(indexes[0]); +} + +void KisSnapshotView::slotRemoveSelectedSnapshot() +{ + KIS_ASSERT_RECOVER_RETURN(m_d->model); + QModelIndexList indexes = selectedIndexes(); + Q_FOREACH (QModelIndex index, indexes) { + m_d->model->slotRemoveSnapshot(index); + } +} + diff --git a/plugins/dockers/snapshotdocker/KisSnapshotModel.h b/plugins/dockers/snapshotdocker/KisSnapshotView.h similarity index 55% copy from plugins/dockers/snapshotdocker/KisSnapshotModel.h copy to plugins/dockers/snapshotdocker/KisSnapshotView.h index ada554a9ae..3539329acb 100644 --- a/plugins/dockers/snapshotdocker/KisSnapshotModel.h +++ b/plugins/dockers/snapshotdocker/KisSnapshotView.h @@ -1,49 +1,40 @@ /* * Copyright (c) 2019 Tusooa Zhu * * 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 KIS_SNAPSHOT_MODEL_H_ -#define KIS_SNAPSHOT_MODEL_H_ +#ifndef KIS_SNAPSHOT_VIEW_H_ +#define KIS_SNAPSHOT_VIEW_H_ -#include -#include -#include +#include -#include - -class KisSnapshotModel : public QAbstractListModel +class KisSnapshotView : public QListView { public: - KisSnapshotModel(); - ~KisSnapshotModel() override; + KisSnapshotView(); + ~KisSnapshotView() override; - int rowCount(const QModelIndex &parent) const override; - QVariant data(const QModelIndex &index, int role) const override; - bool setData(const QModelIndex &index, const QVariant &value, int role) override; - void setCanvas(QPointer canvas); + void setModel(QAbstractItemModel *model) override; public Q_SLOTS: - bool slotCreateSnapshot(); - bool slotRemoveActivatedSnapshot(); - bool slotSwitchToActivatedSnapshot(const QModelIndex &index); - + void slotSwitchToSelectedSnapshot(); + void slotRemoveSelectedSnapshot(); private: struct Private; QScopedPointer m_d; }; -#endif // KIS_SNAPSHOT_MODEL_H_ +#endif diff --git a/plugins/dockers/snapshotdocker/SnapshotDocker.cpp b/plugins/dockers/snapshotdocker/SnapshotDocker.cpp index d304138ae8..c28f888637 100644 --- a/plugins/dockers/snapshotdocker/SnapshotDocker.cpp +++ b/plugins/dockers/snapshotdocker/SnapshotDocker.cpp @@ -1,101 +1,108 @@ /* * Copyright (c) 2019 Tusooa Zhu * * 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 "SnapshotDocker.h" #include #include #include #include #include #include "KisSnapshotModel.h" +#include "KisSnapshotView.h" #include #include struct SnapshotDocker::Private { Private(); ~Private(); QScopedPointer model; - QPointer view; + QPointer view; QPointer canvas; QPointer bnAdd; + QPointer bnSwitchTo; QPointer bnRemove; }; SnapshotDocker::Private::Private() : model(new KisSnapshotModel) - , view(new QListView) + , view(new KisSnapshotView) , canvas(0) , bnAdd(new QToolButton) + , bnSwitchTo(new QToolButton) , bnRemove(new QToolButton) { } SnapshotDocker::Private::~Private() { } SnapshotDocker::SnapshotDocker() : QDockWidget() , m_d(new Private) { QWidget *widget = new QWidget(this); QVBoxLayout *mainLayout = new QVBoxLayout(widget); - connect(m_d->view, &QListView::activated, m_d->model.data(), &KisSnapshotModel::slotSwitchToActivatedSnapshot); + //connect(m_d->view, &QListView::activated, m_d->model.data(), &KisSnapshotModel::slotSwitchToActivatedSnapshot); + connect(m_d->view, &KisSnapshotView::doubleClicked, m_d->view, QOverload::of(&KisSnapshotView::edit)); m_d->view->setModel(m_d->model.data()); mainLayout->addWidget(m_d->view); QHBoxLayout *buttonsLayout = new QHBoxLayout(widget); m_d->bnAdd->setIcon(KisIconUtils::loadIcon("addlayer")); connect(m_d->bnAdd, &QToolButton::clicked, m_d->model.data(), &KisSnapshotModel::slotCreateSnapshot); buttonsLayout->addWidget(m_d->bnAdd); + m_d->bnSwitchTo->setIcon(KisIconUtils::loadIcon("draw-freehand")); /// XXX: which icon to use? + connect(m_d->bnSwitchTo, &QToolButton::clicked, m_d->view, &KisSnapshotView::slotSwitchToSelectedSnapshot); + buttonsLayout->addWidget(m_d->bnSwitchTo); m_d->bnRemove->setIcon(KisIconUtils::loadIcon("deletelayer")); - connect(m_d->bnRemove, &QToolButton::clicked, m_d->model.data(), &KisSnapshotModel::slotRemoveActivatedSnapshot); + connect(m_d->bnRemove, &QToolButton::clicked, m_d->view, &KisSnapshotView::slotRemoveSelectedSnapshot); buttonsLayout->addWidget(m_d->bnRemove); mainLayout->addLayout(buttonsLayout); setWidget(widget); setWindowTitle(i18n("Snapshot Docker")); } SnapshotDocker::~SnapshotDocker() { } void SnapshotDocker::setCanvas(KoCanvasBase *canvas) { KisCanvas2 *c = dynamic_cast(canvas); if (c) { if (m_d->canvas == c) { return; } } m_d->canvas = c; m_d->model->setCanvas(c); } void SnapshotDocker::unsetCanvas() { setCanvas(0); } #include "SnapshotDocker.moc" diff --git a/plugins/dockers/snapshotdocker/SnapshotDocker.h b/plugins/dockers/snapshotdocker/SnapshotDocker.h index 9227ea4d4f..b3ccfe8b31 100644 --- a/plugins/dockers/snapshotdocker/SnapshotDocker.h +++ b/plugins/dockers/snapshotdocker/SnapshotDocker.h @@ -1,49 +1,47 @@ /* This file is part of the KDE project * Copyright (C) 2010 Matus Talcik * * 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 SNAPSHOT_DOCKER_H_ #define SNAPSHOT_DOCKER_H_ #include #include #include #include #include #include class SnapshotDocker : public QDockWidget, public KoCanvasObserverBase { Q_OBJECT public: SnapshotDocker(); ~SnapshotDocker() override; - + QString observerName() override { return "SnapshotDocker"; } void setCanvas(KoCanvasBase *canvas) override; void unsetCanvas() override; -private Q_SLOTS: - private: struct Private; QScopedPointer m_d; }; #endif