chalk

kis_strategy_move.cpp
1/*
2 * Copyright (c) 2002 Patrick Julien <freak@codepimps.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19#include <tqpoint.h>
20#include <tqcolor.h>
21
22#include <tdeaction.h>
23#include <kcommand.h>
24#include <tdelocale.h>
25#include <kdebug.h>
26
27#include "kis_canvas_controller.h"
28#include "kis_canvas_subject.h"
29#include "kis_image.h"
30#include "kis_layer.h"
31#include "kis_strategy_move.h"
32#include "kis_undo_adapter.h"
33
34KisStrategyMove::KisStrategyMove()
35{
36 reset(0);
37}
38
39KisStrategyMove::KisStrategyMove(KisCanvasSubject *subject)
40{
41 reset(subject);
42}
43
44KisStrategyMove::~KisStrategyMove()
45{
46}
47
48void KisStrategyMove::reset(KisCanvasSubject *subject)
49{
50 m_subject = subject;
51 m_dragging = false;
52
53 if (m_subject) {
54 m_controller = subject->canvasController();
55 } else {
56 m_controller = 0;
57 }
58}
59
60void KisStrategyMove::startDrag(const TQPoint& pos)
61{
62 // pos is the user chosen handle point
63
64 if (m_subject) {
65 KisImageSP img;
66 KisLayerSP dev;
67
68 if (!(img = m_subject->currentImg()))
69 return;
70
71 dev = img->activeLayer();
72
73 if (!dev || !dev->visible())
74 return;
75
76 m_dragging = true;
77 m_dragStart.setX(pos.x());
78 m_dragStart.setY(pos.y());
79 m_layerStart.setX(dev->x());
80 m_layerStart.setY(dev->y());
81 m_layerPosition = m_layerStart;
82 }
83}
84
85void KisStrategyMove::drag(const TQPoint& original)
86{
87 // original is the position of the user chosen handle point
88
89 if (m_subject && m_dragging) {
90 KisImageSP img = m_subject->currentImg();
91 KisLayerSP dev;
92
93 if (img && (dev = img->activeLayer())) {
94 TQPoint pos = original;
95 TQRect rc;
96
97 pos -= m_dragStart; // convert to delta
98 rc = dev->extent();
99 dev->setX(dev->x() + pos.x());
100 dev->setY(dev->y() + pos.y());
101 rc = rc.unite(dev->extent());
102
103 m_layerPosition = TQPoint(dev->x(), dev->y());
104 m_dragStart = original;
105
106 dev->setDirty(rc);
107 }
108 }
109}
110
111void KisStrategyMove::endDrag(const TQPoint& pos, bool undo)
112{
113 if (m_subject && m_dragging) {
114 KisImageSP img = m_subject->currentImg();
115 KisLayerSP dev;
116
117 if (img && (dev = img->activeLayer())) {
118 drag(pos);
119 m_dragging = false;
120
121 if (undo && img->undo()) {
122 KCommand *cmd = dev->moveCommand(m_layerStart, m_layerPosition);
123 TQ_CHECK_PTR(cmd);
124
125 KisUndoAdapter *adapter = img->undoAdapter();
126 if (adapter) {
127 adapter->addCommand(cmd);
128 } else {
129 delete cmd;
130 }
131 }
132 img->setModified();
133 }
134 }
135}
136
137void KisStrategyMove::simpleMove(const TQPoint& pt1, const TQPoint& pt2)
138{
139 startDrag(pt1);
140 endDrag(pt2);
141}
142
143void KisStrategyMove::simpleMove(TQ_INT32 x1, TQ_INT32 y1, TQ_INT32 x2, TQ_INT32 y2)
144{
145 startDrag(TQPoint(x1, y1));
146 endDrag(TQPoint(x2, y2));
147}
148
KisCanvasSubject is part of the Observer pattern.
virtual KisCanvasController * canvasController() const =0