diff --git a/plugins/dockers/snapshotdocker/KisSnapshotModel.cpp b/plugins/dockers/snapshotdocker/KisSnapshotModel.cpp index 0acce26023..fe27be361b 100644 --- a/plugins/dockers/snapshotdocker/KisSnapshotModel.cpp +++ b/plugins/dockers/snapshotdocker/KisSnapshotModel.cpp @@ -1,198 +1,207 @@ /* * 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 #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); view->viewManager()->nodeManager()->slotNonUiActivatedNode(curDoc->preActivatedNode()); } // 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; } +Qt::ItemFlags KisSnapshotModel::flags(const QModelIndex &index) const +{ + if (!index.isValid()) { + return Qt::ItemIsEnabled; + } + + return QAbstractListModel::flags(index) | Qt::ItemIsEditable; +} + 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()->lockAndCreateSnapshot()); 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::slotRemoveSnapshot(const QModelIndex &index) { 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::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 e58b579952..92addb1671 100644 --- a/plugins/dockers/snapshotdocker/KisSnapshotModel.h +++ b/plugins/dockers/snapshotdocker/KisSnapshotModel.h @@ -1,49 +1,51 @@ /* * 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; + // this function is re-implemented to make the items editable + Qt::ItemFlags flags(const QModelIndex &index) const override; void setCanvas(QPointer canvas); public Q_SLOTS: bool slotCreateSnapshot(); 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 index d0ae0cd9d5..657d2ca7c0 100644 --- a/plugins/dockers/snapshotdocker/KisSnapshotView.cpp +++ b/plugins/dockers/snapshotdocker/KisSnapshotView.cpp @@ -1,67 +1,67 @@ /* * 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) { - setEditTriggers(QAbstractItemView::SelectedClicked); + setEditTriggers(QAbstractItemView::DoubleClicked | QAbstractItemView::SelectedClicked); } 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); } }