1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
/*
* Copyright (c) 2006 Cyrille Berger <cberger@cberger.net>
*
* 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 "kis_tool_move_selection.h"
#include <stdlib.h>
#include <tqpoint.h>
#include <tdeaction.h>
#include <kcommand.h>
#include <tdelocale.h>
#include <tqcolor.h>
#include "kis_canvas_subject.h"
#include "kis_cursor.h"
#include "kis_image.h"
#include "kis_layer.h"
#include "kis_paint_layer.h"
#include "kis_paint_device.h"
#include "kis_button_press_event.h"
#include "kis_button_release_event.h"
#include "kis_move_event.h"
#include "kis_selection.h"
#include "kis_selection_manager.h"
#include "kis_undo_adapter.h"
class KisSelectionOffsetCommand : public KNamedCommand {
typedef KNamedCommand super;
public:
KisSelectionOffsetCommand(KisSelectionSP layer, const TQPoint& oldpos, const TQPoint& newpos);
virtual ~KisSelectionOffsetCommand();
virtual void execute();
virtual void unexecute();
private:
void moveTo(const TQPoint& pos);
private:
KisSelectionSP m_layer;
TQPoint m_oldPos;
TQPoint m_newPos;
};
KisSelectionOffsetCommand::KisSelectionOffsetCommand(KisSelectionSP layer, const TQPoint& oldpos, const TQPoint& newpos) :
super(i18n("Move Layer"))
{
m_layer = layer;
m_oldPos = oldpos;
m_newPos = newpos;
}
KisSelectionOffsetCommand::~KisSelectionOffsetCommand()
{
}
void KisSelectionOffsetCommand::execute()
{
moveTo(m_newPos);
}
void KisSelectionOffsetCommand::unexecute()
{
moveTo(m_oldPos);
}
void KisSelectionOffsetCommand::moveTo(const TQPoint& pos)
{
if (m_layer->undoAdapter()) {
m_layer->undoAdapter()->setUndo(false);
}
m_layer->setX(pos.x());
m_layer->setY(pos.y());
m_layer->parentPaintDevice()->setDirty();
if (m_layer->undoAdapter()) {
m_layer->undoAdapter()->setUndo(true);
}
}
KisToolMoveSelection::KisToolMoveSelection()
: super(i18n("Move Selection Tool"))
{
setName("tool_move_selection");
m_subject = 0;
setCursor(KisCursor::moveCursor());
}
KisToolMoveSelection::~KisToolMoveSelection()
{
}
void KisToolMoveSelection::update(KisCanvasSubject *subject)
{
m_subject = subject;
super::update(subject);
m_dragging = false;
}
void KisToolMoveSelection::buttonPress(KisButtonPressEvent *e)
{
m_dragging = false;
if (m_subject && e->button() == TQt::LeftButton) {
TQPoint pos = e->pos().floorTQPoint();
KisImageSP img = m_subject->currentImg();
KisPaintLayerSP lay;
if (!img || !(lay = dynamic_cast<KisPaintLayer*>( img->activeLayer().data() )))
return;
m_dragStart = pos;
if ( !lay->visible() || !lay->paintDevice()->hasSelection())
return;
KisSelectionSP sel = lay->paintDevice()->selection();
m_dragging = true;
m_dragStart.setX(pos.x());
m_dragStart.setY(pos.y());
m_layerStart.setX(sel->getX());
m_layerStart.setY(sel->getY());
m_layerPosition = m_layerStart;
}
}
void KisToolMoveSelection::move(KisMoveEvent *e)
{
if (m_subject && m_dragging) {
TQPoint pos = e->pos().floorTQPoint();
if((e->state() & TQt::AltButton) || (e->state() & TQt::ControlButton)) {
if(fabs(pos.x() - m_dragStart.x()) > fabs(pos.y() - m_dragStart.y()))
pos.setY(m_dragStart.y());
else
pos.setX(m_dragStart.x());
}
KisImageSP img = m_subject->currentImg();
KisPaintLayerSP lay = dynamic_cast<KisPaintLayer*>(m_subject->currentImg()->activeLayer().data());
if(!lay) return;
KisSelectionSP sel = lay->paintDevice()->selection();
TQRect rc;
pos -= m_dragStart; // convert to delta
rc = sel->selectedRect();
sel->setX(sel->getX() + pos.x());
sel->setY(sel->getY() + pos.y());
rc = rc.unite(sel->selectedRect());
m_layerPosition = TQPoint(sel->getX(), sel->getY());
m_dragStart = e->pos().floorTQPoint();
lay->paintDevice()->setDirty(rc);
}
}
void KisToolMoveSelection::buttonRelease(KisButtonReleaseEvent *e)
{
if (m_subject && e->button() == TQt::LeftButton && m_dragging) {
m_dragging = false;
KisImageSP img = m_subject->currentImg();
if(!img) return;
KisPaintLayerSP lay = dynamic_cast<KisPaintLayer*>(img->activeLayer().data());
if (lay->paintDevice()->hasSelection()) {
KisSelectionSP dev = lay->paintDevice()->selection();
m_dragging = false;
if (img->undo()) {
KCommand *cmd = new KisSelectionOffsetCommand( dev, m_layerStart, m_layerPosition);
TQ_CHECK_PTR(cmd);
KisUndoAdapter *adapter = img->undoAdapter();
if (adapter) {
adapter->addCommand(cmd);
} else {
delete cmd;
}
}
img->setModified();
lay->setDirty();
}
}
}
void KisToolMoveSelection::setup(TDEActionCollection *collection)
{
m_action = static_cast<TDERadioAction *>(collection->action(name()));
if (m_action == 0) {
m_action = new TDERadioAction(i18n("&Move selection"),
"tool_move",
TQt::SHIFT+TQt::Key_V,
this,
TQT_SLOT(activate()),
collection,
name());
m_action->setToolTip(i18n("Move the selection"));
m_action->setExclusiveGroup("tools");
m_ownAction = true;
}
}
#include "kis_tool_move_selection.moc"
|