Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_change_guides_command.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2016 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
8
9#include "kis_guides_config.h"
10#include "KisDocument.h"
11#include <kis_image.h>
12
13#include <QList>
14#include <QListIterator>
15
40
42{
43 int ret = diff(first.horizontalGuideLines(), second.horizontalGuideLines()) +
45
46 if (ret == ADDITION) {
47 q->setText(kundo2_i18n("Add Guide"));
48 } else if (ret == REMOVAL) {
49 q->setText(kundo2_i18n("Remove Guide"));
50 } else if (ret == NO_DIFF || ret == ONE_DIFF) { // meaning we will still merge it
51 // XXX: how to deal with NO_DIFF (the command "should" be removed -- how?)
52 q->setText(kundo2_i18n("Edit Guides"));
53 } else {
54 return false;
55 }
56 return true;
57}
58
60{
61 if (first.size() == second.size()) {
62 int diffCount = 0;
63 for (int i = 0; i < first.size(); ++i) {
64 if (first[i] != second[i]) {
65 ++diffCount;
66 if (diffCount > 1) {
67 return OTHER_DIFF;
68 }
69 }
70 }
71 return diffCount == 0 ? NO_DIFF : ONE_DIFF;
72 } else if (first.size() - second.size() == -1) { // added a guide
73 QList<qreal> beforeRemoval = second;
74 beforeRemoval.takeLast();
75 return first == beforeRemoval ? ADDITION : OTHER_DIFF;
76 } else if (first.size() - second.size() == 1) { // removed a guide
77 bool skippedItem = false;
78 for (QListIterator<qreal> i(first), j(second); i.hasNext() && j.hasNext(); ) {
79 qreal curFirst = i.next();
80 qreal curSecond = j.next();
81 if (!skippedItem && curFirst != curSecond) {
82 curFirst = i.next(); // try to go to the next item and see if it matches
83 }
84 if (curFirst != curSecond) {
85 return OTHER_DIFF;
86 }
87 }
88 // here we conclude only one guide is removed
89 return REMOVAL;
90 } else {
91 return OTHER_DIFF;
92 }
93}
94
96{
97 KisGuidesConfig curConfig = doc->guidesConfig();
99 curConfig.setVerticalGuideLines(config.verticalGuideLines());
100 doc->setGuidesConfig(curConfig);
101}
102
104 : KUndo2Command(kundo2_i18n("Edit Guides")),
105 m_d(new Private(doc, this))
106{
107 m_d->oldGuides = oldGuides;
108 m_d->newGuides = newGuides;
109 // update the undo command text
110 m_d->sameOrOnlyMovedOneGuideBetween(m_d->oldGuides, m_d->newGuides);
111}
112
116
118{
119 m_d->switchTo(m_d->oldGuides);
120}
121
123{
124 if (m_d->firstRedo) {
125 m_d->firstRedo = false;
126 return;
127 }
128 m_d->switchTo(m_d->newGuides);
129}
130
132{
133 return 1863;
134}
135
void setText(const KUndo2MagicString &text)
const QScopedPointer< Private > m_d
KisChangeGuidesCommand(KisDocument *doc, const KisGuidesConfig &oldGuides, const KisGuidesConfig &newGuides)
void setHorizontalGuideLines(const QList< qreal > &lines)
Set the positions of the horizontal guide lines.
const QList< qreal > & verticalGuideLines() const
Returns the list of vertical guide lines.
void setVerticalGuideLines(const QList< qreal > &lines)
Set the positions of the vertical guide lines.
const QList< qreal > & horizontalGuideLines() const
Returns the list of horizontal guide lines.
KUndo2MagicString kundo2_i18n(const char *text)
void switchTo(const KisGuidesConfig &config)
bool sameOrOnlyMovedOneGuideBetween(const KisGuidesConfig &first, const KisGuidesConfig &second)
Private(KisDocument *_doc, KisChangeGuidesCommand *q)
Status diff(const QList< qreal > &first, const QList< qreal > &second)