addstenciltool.cpp
1/*
2 * Kivio - Visual Modelling and Flowcharting
3 * Copyright (C) 2005 Peter Simonsson <psn@linux.se>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19#include "addstenciltool.h"
20
21#include <tqevent.h>
22#include <tqcursor.h>
23
24#include "kdebug.h"
25
26#include "kivio_canvas.h"
27#include "kivio_stencil_spawner.h"
28#include "kivio_stencil.h"
29#include "kivio_page.h"
30#include "kivio_doc.h"
31#include "kivio_pluginmanager.h"
32
33namespace Kivio {
34
35AddStencilTool::AddStencilTool(KivioView* parent)
36 : MouseTool(parent, "Add Stencil Mouse Tool")
37{
38 m_leftMouseButtonPressed = false;
39 m_stencil = 0;
40 m_spawner = 0;
41}
42
43AddStencilTool::~AddStencilTool()
44{
45 delete m_stencil;
46 m_stencil = 0;
47}
48
49bool AddStencilTool::processEvent(TQEvent* e)
50{
51 switch(e->type()) {
52 case TQEvent::MouseButtonPress:
53 {
54 TQMouseEvent* me = static_cast<TQMouseEvent*>(e);
55
56 if(me->button() == TQt::LeftButton) {
57 m_leftMouseButtonPressed = true;
58 createStencil(me->pos());
59 return true;
60 }
61
62 break;
63 }
64 case TQEvent::MouseButtonRelease:
65 {
66 TQMouseEvent* me = static_cast<TQMouseEvent*>(e);
67
68 if(m_leftMouseButtonPressed && (me->button() == TQt::LeftButton)) {
69 m_leftMouseButtonPressed = false;
70 endOperation(me->pos());
71 return true;
72 }
73
74 break;
75 }
76 case TQEvent::MouseMove:
77 {
78 TQMouseEvent* me = static_cast<TQMouseEvent*>(e);
79
80 if(m_leftMouseButtonPressed) {
81 resize(me->pos());
82 return true;
83 }
84
85 break;
86 }
87 default:
88 break;
89 }
90
91 return false;
92}
93
94void AddStencilTool::setActivated(bool a)
95{
96 if(a) {
97 m_leftMouseButtonPressed = false;
98 view()->canvasWidget()->setCursor(TQt::CrossCursor);
99 emit activated(this);
100 } else {
101 view()->canvasWidget()->unsetCursor();
102 m_spawner = 0;
103 delete m_stencil;
104 m_stencil = 0;
105 }
106}
107
108void AddStencilTool::activateNewStencil(KivioStencilSpawner* spawner)
109{
110 m_spawner = spawner;
111 setActivated(true);
112}
113
114void AddStencilTool::createStencil(const TQPoint& position)
115{
116 if(!m_spawner) {
117 return;
118 }
119
120 m_startPoint = view()->canvasWidget()->mapFromScreen(position);
121 m_startPoint = view()->canvasWidget()->snapToGridAndGuides(m_startPoint);
122
123 m_stencil = m_spawner->newStencil();
124
125 if(!m_stencil) {
126 return;
127 }
128
129 m_stencil->setPosition(m_startPoint.x(), m_startPoint.y());
130 m_originalSize.setWidth(m_stencil->w());
131 m_originalSize.setHeight(m_stencil->h());
132 m_stencil->setW(1);
133 m_stencil->setH(1);
134 view()->canvasWidget()->beginUnclippedSpawnerPainter();
135 view()->canvasWidget()->drawStencilXOR(m_stencil);
136}
137
138void AddStencilTool::endOperation(const TQPoint& position)
139{
140 if(!m_stencil) {
141 return;
142 }
143
144 KoPoint endPoint = view()->canvasWidget()->mapFromScreen(position);
145 endPoint = view()->canvasWidget()->snapToGridAndGuides(endPoint);
146
147 if(m_startPoint == endPoint) {
148 m_stencil->setW(m_originalSize.width());
149 m_stencil->setH(m_originalSize.height());
150 }
151
152 KivioPage* page = view()->canvasWidget()->activePage();
153 page->unselectAllStencils();
154 page->addStencil(m_stencil);
155 page->selectStencil(m_stencil);
156 m_stencil = 0;
157
158 view()->canvasWidget()->endUnclippedSpawnerPainter();
159 view()->doc()->updateView(page);
160
161 view()->pluginManager()->activateDefaultTool();
162}
163
164void AddStencilTool::resize(const TQPoint& position)
165{
166 KoPoint endPoint = view()->canvasWidget()->mapFromScreen(position);
167 endPoint = view()->canvasWidget()->snapToGridAndGuides(endPoint);
168
169 view()->canvasWidget()->drawStencilXOR(m_stencil);
170 m_stencil->setW(endPoint.x() - m_startPoint.x());
171 m_stencil->setH(endPoint.y() - m_startPoint.y());
172 view()->canvasWidget()->drawStencilXOR(m_stencil);
173}
174
175}
176
177#include "addstenciltool.moc"
Baseclass for mouse tools (like eg the select tool)
void activated(Kivio::MouseTool *)
this should be emited when the tool wants to be active