|
| 1 | +/* |
| 2 | + Copyright 2021, Mitch Curtis |
| 3 | +
|
| 4 | + This file is part of Slate. |
| 5 | +
|
| 6 | + Slate is free software: you can redistribute it and/or modify |
| 7 | + it under the terms of the GNU General Public License as published by |
| 8 | + the Free Software Foundation, either version 3 of the License, or |
| 9 | + (at your option) any later version. |
| 10 | +
|
| 11 | + Slate is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + GNU General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU General Public License |
| 17 | + along with Slate. If not, see <http://www.gnu.org/licenses/>. |
| 18 | +*/ |
| 19 | + |
| 20 | +#include "guidemodel.h" |
| 21 | + |
| 22 | +#include <QLoggingCategory> |
| 23 | + |
| 24 | +#include "imagecanvas.h" |
| 25 | +#include "project.h" |
| 26 | + |
| 27 | +Q_LOGGING_CATEGORY(lcGuideModel, "app.guideModel") |
| 28 | + |
| 29 | +GuideModel::GuideModel(QObject *parent) : |
| 30 | + QAbstractListModel(parent) |
| 31 | +{ |
| 32 | +} |
| 33 | + |
| 34 | +GuideModel::~GuideModel() |
| 35 | +{ |
| 36 | +} |
| 37 | + |
| 38 | +Project *GuideModel::project() const |
| 39 | +{ |
| 40 | + return mProject; |
| 41 | +} |
| 42 | + |
| 43 | +void GuideModel::setProject(Project *project) |
| 44 | +{ |
| 45 | + qCDebug(lcGuideModel) << "project changed to" << project; |
| 46 | + |
| 47 | + if (project == mProject) |
| 48 | + return; |
| 49 | + |
| 50 | + if (mProject) |
| 51 | + mProject->disconnect(this); |
| 52 | + |
| 53 | + beginResetModel(); |
| 54 | + |
| 55 | + mProject = project; |
| 56 | + |
| 57 | + endResetModel(); |
| 58 | + emit projectChanged(); |
| 59 | + |
| 60 | + if (mProject) { |
| 61 | + connect(mProject, &Project::preGuidesAdded, this, &GuideModel::onPreGuidesAdded); |
| 62 | + connect(mProject, &Project::postGuidesAdded, this, &GuideModel::onPostGuidesAdded); |
| 63 | + connect(mProject, &Project::guideMoved, this, &GuideModel::onGuideMoved); |
| 64 | + connect(mProject, &Project::preGuidesRemoved, this, &GuideModel::onPreGuidesRemoved); |
| 65 | + connect(mProject, &Project::postGuidesRemoved, this, &GuideModel::onPostGuidesRemoved); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +ImageCanvas *GuideModel::canvas() const |
| 70 | +{ |
| 71 | + return mCanvas; |
| 72 | +} |
| 73 | + |
| 74 | +void GuideModel::setCanvas(ImageCanvas *canvas) |
| 75 | +{ |
| 76 | + if (canvas == mCanvas) |
| 77 | + return; |
| 78 | + |
| 79 | + if (mCanvas) |
| 80 | + disconnect(mCanvas, &ImageCanvas::existingGuideDragged, this, &GuideModel::onGuideMoved); |
| 81 | + |
| 82 | + mCanvas = canvas; |
| 83 | + |
| 84 | + if (mCanvas) |
| 85 | + connect(mCanvas, &ImageCanvas::existingGuideDragged, this, &GuideModel::onGuideMoved); |
| 86 | + |
| 87 | + emit canvasChanged(); |
| 88 | +} |
| 89 | + |
| 90 | +QVariant GuideModel::data(const QModelIndex &index, int role) const |
| 91 | +{ |
| 92 | + if (!mProject || !checkIndex(index, CheckIndexOption::IndexIsValid)) |
| 93 | + return QVariant(); |
| 94 | + |
| 95 | + const Guide guide = mProject->guides().at(index.row()); |
| 96 | + |
| 97 | + switch (role) { |
| 98 | + case OrientationRole: |
| 99 | + return QVariant::fromValue(guide.orientation()); |
| 100 | + case PositionRole: { |
| 101 | + // If this is an existing guide that is currently being dragged, draw it in its dragged position. |
| 102 | + // It's OK if canvas is null; the user can't drag while things are starting up. |
| 103 | + const bool draggingExistingGuide = mCanvas && mCanvas->pressedGuideIndex() != -1 && mCanvas->pressedGuideIndex() == index.row(); |
| 104 | + const bool vertical = guide.orientation() == Qt::Vertical; |
| 105 | + const int guidePosition = draggingExistingGuide |
| 106 | + ? (vertical ? mCanvas->cursorSceneX() : mCanvas->cursorSceneY()) : guide.position(); |
| 107 | + return QVariant::fromValue(guidePosition); |
| 108 | + } default: |
| 109 | + break; |
| 110 | + } |
| 111 | + |
| 112 | + return QVariant(); |
| 113 | +} |
| 114 | + |
| 115 | +int GuideModel::rowCount(const QModelIndex &) const |
| 116 | +{ |
| 117 | + if (!mProject) |
| 118 | + return 0; |
| 119 | + |
| 120 | + return mProject->guides().size(); |
| 121 | +} |
| 122 | + |
| 123 | +int GuideModel::columnCount(const QModelIndex &) const |
| 124 | +{ |
| 125 | + return 1; |
| 126 | +} |
| 127 | + |
| 128 | +QHash<int, QByteArray> GuideModel::roleNames() const |
| 129 | +{ |
| 130 | + QHash<int, QByteArray> names; |
| 131 | + names.insert(OrientationRole, "orientation"); |
| 132 | + names.insert(PositionRole, "position"); |
| 133 | + return names; |
| 134 | +} |
| 135 | + |
| 136 | +void GuideModel::onProjectChanged() |
| 137 | +{ |
| 138 | +} |
| 139 | + |
| 140 | +void GuideModel::onPreGuidesAdded(int index, int count) |
| 141 | +{ |
| 142 | + beginInsertRows(QModelIndex(), index, index + count - 1); |
| 143 | +} |
| 144 | + |
| 145 | +void GuideModel::onPostGuidesAdded() |
| 146 | +{ |
| 147 | + endInsertRows(); |
| 148 | +} |
| 149 | + |
| 150 | +void GuideModel::onPreGuidesRemoved() |
| 151 | +{ |
| 152 | + qCDebug(lcGuideModel) << "guides about to be removed (" << this << "); about to call beginResetModel()..."; |
| 153 | + beginResetModel(); |
| 154 | + qCDebug(lcGuideModel) << "... called beginResetModel()"; |
| 155 | +} |
| 156 | + |
| 157 | +void GuideModel::onPostGuidesRemoved() |
| 158 | +{ |
| 159 | + qCDebug(lcGuideModel) << "guides removed (" << this << "); about to call endResetModel()..."; |
| 160 | + endResetModel(); |
| 161 | + qCDebug(lcGuideModel) << "... called endResetModel()"; |
| 162 | +} |
| 163 | + |
| 164 | +void GuideModel::onGuideMoved(int index) |
| 165 | +{ |
| 166 | + QVector<int> roles; |
| 167 | + roles.append(PositionRole); |
| 168 | + const QModelIndex modelIndex(createIndex(index, 0)); |
| 169 | + emit dataChanged(modelIndex, modelIndex, roles); |
| 170 | +} |
0 commit comments