kivio

tool_text.cpp
1/*
2 * Kivio - Visual Modelling and Flowcharting
3 * Copyright (C) 2000-2003 theKompany.com & Dave Marotti,
4 * Peter Simonsson
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program 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 this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20#include "tool_text.h"
21
22#include <tqcursor.h>
23#include <kiconloader.h>
24#include <kstandarddirs.h>
25#include <kdebug.h>
26#include <KoPoint.h>
27#include <tdelocale.h>
28#include <KoZoomHandler.h>
29#include <kinputdialog.h>
30
31#include "kivio_view.h"
32#include "kivio_canvas.h"
33#include "kivio_page.h"
34#include "kivio_doc.h"
35
36#include "kivio_stencil_spawner_set.h"
37#include "kivio_stencil_spawner.h"
38#include "kivio_custom_drag_data.h"
39#include "kivio_layer.h"
40#include "kivio_point.h"
41#include "kivio_stencil.h"
42#include "kivio_factory.h"
43#include "kivio_command.h"
44#include "kivio_pluginmanager.h"
45#include "mousetoolaction.h"
46#include "stenciltexteditor.h"
47
48
49TextTool::TextTool( KivioView* parent ) : Kivio::MouseTool(parent, "Text Mouse Tool")
50{
51 m_textAction = new Kivio::MouseToolAction( i18n("Text Tool"), "text", Key_F2, actionCollection(), "text" );
52 connect(m_textAction, TQ_SIGNAL(toggled(bool)), this, TQ_SLOT(setActivated(bool)));
53 connect(m_textAction, TQ_SIGNAL(doubleClicked()), this, TQ_SLOT(makePermanent()));
54
55 m_permanent = false;
56 m_mode = stmNone;
57
58 TQPixmap pix = BarIcon("kivio_text_cursor",KivioFactory::global());
59 m_pTextCursor = new TQCursor(pix,2,2);
60}
61
62TextTool::~TextTool()
63{
64 delete m_pTextCursor;
65}
66
67
74bool TextTool::processEvent(TQEvent* e)
75{
76 switch (e->type())
77 {
78 case TQEvent::MouseButtonPress:
79 mousePress( (TQMouseEvent*)e );
80 return true;
81 break;
82
83 case TQEvent::MouseButtonRelease:
84 mouseRelease( (TQMouseEvent*)e );
85 return true;
86 break;
87
88 case TQEvent::MouseMove:
89 mouseMove( (TQMouseEvent*)e );
90 return true;
91 break;
92
93 default:
94 break;
95 }
96
97 return false;
98}
99
100void TextTool::setActivated(bool a)
101{
102 if(a) {
103 emit activated(this);
104 m_textAction->setChecked(true);
105 view()->canvasWidget()->setCursor(*m_pTextCursor);
106 m_mode = stmNone;
107 } else {
108 m_textAction->setChecked(false);
109 m_permanent = false;
110 }
111}
112
113void TextTool::text(TQRect r)
114{
115 // Calculate the start and end clicks in terms of page coordinates
116 KoPoint startPoint = view()->canvasWidget()->mapFromScreen( TQPoint( r.x(), r.y() ) );
117 KoPoint releasePoint = view()->canvasWidget()->mapFromScreen( TQPoint( r.x() + r.width(), r.y() + r.height() ) );
118
119 // Calculate the x,y position of the textion box
120 float x = startPoint.x() < releasePoint.x() ? startPoint.x() : releasePoint.x();
121 float y = startPoint.y() < releasePoint.y() ? startPoint.y() : releasePoint.y();
122
123 // Calculate the w/h of the textion box
124 float w = releasePoint.x() - startPoint.x();
125
126 if( w < 0.0 ) {
127 w *= -1.0;
128 }
129
130 float h = releasePoint.y() - startPoint.y();
131
132 if( h < 0.0 ) {
133 h *= -1.0;
134 }
135
136 KivioDoc* doc = view()->doc();
137 KivioPage* page = view()->activePage();
138
139 KivioStencilSpawner* ss = doc->findInternalStencilSpawner("Dave Marotti - Text");
140
141 if (!ss) {
142 return;
143 }
144
145 KivioStencil* stencil = ss->newStencil();
146 stencil->setType(kstText);
147 stencil->setPosition(x,y);
148 stencil->setDimensions(w,h);
149 stencil->setText("");
150 stencil->setTextFont(doc->defaultFont());
151 page->unselectAllStencils();
152 page->addStencil(stencil);
153 page->selectStencil(stencil);
154
155 doc->updateView(page);
156
157 applyToolAction(page->selectedStencils());
158
159 if (stencil->text().isEmpty()) {
160 page->deleteSelectedStencils();
161 doc->updateView(page);
162 }
163}
164
165void TextTool::mousePress( TQMouseEvent *e )
166{
167 if(e->button() == TQt::LeftButton) {
168 KoPoint pagePoint = view()->canvasWidget()->mapFromScreen(e->pos());
169 int colType;
170 KivioPage *page = view()->activePage();
171 KivioStencil* stencil = page->checkForStencil( &pagePoint, &colType, 0.0, false);
172
173 if(stencil) {
174 applyToolAction(stencil, pagePoint);
175 } else if(startRubberBanding(e)) {
176 m_mode = stmDrawRubber;
177 }
178 }
179}
180
181
185bool TextTool::startRubberBanding( TQMouseEvent *e )
186{
187 view()->canvasWidget()->startRectDraw( e->pos(), KivioCanvas::Rubber );
188 view()->canvasWidget()->repaint();
189 m_startPoint = e->pos();
190
191 return true;
192}
193
194void TextTool::mouseMove( TQMouseEvent * e )
195{
196 switch( m_mode )
197 {
198 case stmDrawRubber:
199 continueRubberBanding(e);
200 break;
201
202 default:
203 break;
204 }
205}
206
207void TextTool::continueRubberBanding( TQMouseEvent *e )
208{
209 view()->canvasWidget()->continueRectDraw( e->pos(), KivioCanvas::Rubber );
210}
211
212void TextTool::mouseRelease( TQMouseEvent *e )
213{
214 m_releasePoint = e->pos();
215
216 switch( m_mode )
217 {
218 case stmDrawRubber:
219 endRubberBanding(e);
220 break;
221 }
222
223 m_mode = stmNone;
224
225 view()->canvasWidget()->repaint();
226}
227
228void TextTool::endRubberBanding(TQMouseEvent */*e*/)
229{
230 // End the rubber-band drawing
231 view()->canvasWidget()->endRectDraw();
232 TQRect rect;
233
234 if( m_startPoint != m_releasePoint ) {
235 rect = view()->canvasWidget()->rect();
236 } else { // Behave a bit more sensible when clicking the canvas...
237 rect.setTopLeft(m_startPoint);
238 rect.setWidth(view()->zoomHandler()->zoomItX(100));
239 rect.setHeight(view()->zoomHandler()->zoomItY(20));
240 }
241
242 text(rect);
243
244 if(!m_permanent) {
245 view()->pluginManager()->activateDefaultTool();
246 }
247}
248
249void TextTool::applyToolAction(TQPtrList<KivioStencil>* stencils)
250{
251 if(stencils->isEmpty()) {
252 return;
253 }
254
255 KivioStencil* stencil = stencils->first();
256 bool ok = false;
257
258 while(stencil) {
259 if(stencil->hasTextBox()) {
260 ok = true;
261 break;
262 }
263
264 stencil = stencils->next();
265 }
266
267 if(!ok) {
268 return;
269 }
270
271 Kivio::StencilTextEditor editor(i18n("Edit Text"), view(), "StencilTextEditor");
272 editor.setFont(stencil->textFont());
273 editor.setFontColor(stencil->textColor());
274 editor.setBackgroundColor(stencil->bgColor());
275 editor.setText(stencil->text());
276 editor.setHorizontalAlign(static_cast<TQt::AlignmentFlags>(stencil->hTextAlign()));
277 editor.setVerticalAlign(static_cast<TQt::AlignmentFlags>(stencil->vTextAlign()));
278
279 if(editor.exec() == TQDialog::Accepted) {
280 KMacroCommand* macroCmd = new KMacroCommand(i18n("Change Stencil Text and Formatting"));
281 bool changed = false;
282 TQString text = editor.text();
283 TQFont font = editor.font();
284 TQColor textColor = editor.fontColor();
285 int halignment = editor.horizontalAlignment();
286 int valignment = editor.verticalAlignment();
287 bool changeFont = (stencil->textFont() != font);
288 bool changeTextColor = (stencil->textColor() != textColor);
289 bool changeHAlignment = (stencil->hTextAlign() != halignment);
290 bool changeVAlignment = (stencil->vTextAlign() != valignment);
291
292
293 while( stencil )
294 {
295 if(stencil->text() != text)
296 {
297 KivioChangeStencilTextCommand *cmd = new KivioChangeStencilTextCommand(i18n("Change Stencil Text"),
298 stencil, stencil->text(), text, view()->activePage());
299 stencil->setText(text);
300 macroCmd->addCommand(cmd);
301 changed = true;
302 }
303
304 if(changeFont && (stencil->textFont() != font)) {
305 KivioChangeStencilFontCommand* cmd = new KivioChangeStencilFontCommand(i18n("Change Stencil Font"),
306 view()->activePage(), stencil, stencil->textFont(), font);
307 stencil->setTextFont(font);
308 macroCmd->addCommand(cmd);
309 changed = true;
310 }
311
312 if(changeTextColor && (stencil->textColor() != textColor)) {
313 KivioChangeStencilColorCommand* cmd = new KivioChangeStencilColorCommand(i18n("Change Stencil Text Color"),
314 view()->activePage(), stencil, stencil->textColor(), textColor, KivioChangeStencilColorCommand::CT_TEXTCOLOR);
315 stencil->setTextColor(textColor);
316 macroCmd->addCommand(cmd);
317 changed = true;
318 }
319
320 if(changeHAlignment && (stencil->hTextAlign() != halignment)) {
321 KivioChangeStencilHAlignmentCommand* cmd = new KivioChangeStencilHAlignmentCommand(
322 i18n("Change Stencil Horizontal Alignment"), view()->activePage(), stencil, stencil->hTextAlign(), halignment);
323 stencil->setHTextAlign(halignment);
324 macroCmd->addCommand(cmd);
325 changed = true;
326 }
327
328 if(changeVAlignment && (stencil->vTextAlign() != valignment)) {
329 KivioChangeStencilVAlignmentCommand* cmd = new KivioChangeStencilVAlignmentCommand(
330 i18n("Change Stencil Vertical Alignment"), view()->activePage(), stencil, stencil->vTextAlign(), valignment);
331 stencil->setVTextAlign(valignment);
332 macroCmd->addCommand(cmd);
333 changed = true;
334 }
335
336 stencil = stencils->next();
337 }
338
339 if(changed) {
340 view()->doc()->addCommand(macroCmd);
341 view()->doc()->updateView(view()->activePage());
342 } else {
343 delete macroCmd;
344 }
345 }
346}
347
348void TextTool::applyToolAction(KivioStencil* stencil, const KoPoint& pos)
349{
350 if(!stencil) {
351 return;
352 }
353
354 TQString name = stencil->getTextBoxName(pos);
355
356 if(name.isEmpty()) {
357 return;
358 }
359
360 Kivio::StencilTextEditor editor(i18n("Edit Text"), view(), "StencilTextEditor");
361 editor.setFont(stencil->textFont(name));
362 editor.setFontColor(stencil->textColor(name));
363 editor.setBackgroundColor(stencil->bgColor());
364 editor.setText(stencil->text(name));
365 editor.setHorizontalAlign(static_cast<TQt::AlignmentFlags>(stencil->hTextAlign(name)));
366 editor.setVerticalAlign(static_cast<TQt::AlignmentFlags>(stencil->vTextAlign(name)));
367
368 if(editor.exec() == TQDialog::Accepted) {
369 KMacroCommand* macroCmd = new KMacroCommand(i18n("Change Stencil Text and Formatting"));
370 bool changed = false;
371 TQString text = editor.text();
372
373 if(stencil->text(name) != text)
374 {
375 KivioChangeStencilTextCommand *cmd = new KivioChangeStencilTextCommand(i18n("Change Stencil Text"),
376 stencil, stencil->text(name), text, view()->activePage(), name);
377 stencil->setText(text, name);
378 macroCmd->addCommand(cmd);
379 changed = true;
380 }
381
382 TQFont font = editor.font();
383
384 if(stencil->textFont(name) != font) {
385 KivioChangeStencilFontCommand* cmd = new KivioChangeStencilFontCommand(i18n("Change Stencil Font"),
386 view()->activePage(), stencil, stencil->textFont(name), font, name);
387 stencil->setTextFont(name, font);
388 macroCmd->addCommand(cmd);
389 changed = true;
390 }
391
392 TQColor textColor = editor.fontColor();
393
394 if(stencil->textColor(name) != textColor) {
395 KivioChangeStencilColorCommand* cmd = new KivioChangeStencilColorCommand(i18n("Change Stencil Text Color"),
396 view()->activePage(), stencil, stencil->textColor(name), textColor,
397 KivioChangeStencilColorCommand::CT_TEXTCOLOR, name);
398 stencil->setTextColor(name, textColor);
399 macroCmd->addCommand(cmd);
400 changed = true;
401 }
402
403 int halignment = editor.horizontalAlignment();
404
405 if(stencil->hTextAlign(name) != halignment) {
406 KivioChangeStencilHAlignmentCommand* cmd = new KivioChangeStencilHAlignmentCommand(
407 i18n("Change Stencil Horizontal Alignment"), view()->activePage(), stencil,
408 stencil->hTextAlign(name), halignment, name);
409 stencil->setHTextAlign(name, halignment);
410 macroCmd->addCommand(cmd);
411 changed = true;
412 }
413
414 int valignment = editor.verticalAlignment();
415
416 if(stencil->vTextAlign(name) != valignment) {
417 KivioChangeStencilVAlignmentCommand* cmd = new KivioChangeStencilVAlignmentCommand(
418 i18n("Change Stencil Vertical Alignment"), view()->activePage(), stencil,
419 stencil->vTextAlign(name), valignment, name);
420 stencil->setVTextAlign(name, valignment);
421 macroCmd->addCommand(cmd);
422 changed = true;
423 }
424
425 if(changed) {
426 view()->doc()->addCommand(macroCmd);
427 view()->doc()->updateView(view()->activePage());
428 } else {
429 delete macroCmd;
430 }
431 }
432}
433
434void TextTool::makePermanent()
435{
436 m_permanent = true;
437}
438
439#include "tool_text.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