23#include <kiconloader.h>
24#include <kstandarddirs.h>
28#include <KoZoomHandler.h>
29#include <kinputdialog.h>
31#include "kivio_view.h"
32#include "kivio_canvas.h"
33#include "kivio_page.h"
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"
49TextTool::TextTool( KivioView* parent ) : Kivio::
MouseTool(parent,
"Text Mouse Tool")
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()));
58 TQPixmap pix = BarIcon(
"kivio_text_cursor",KivioFactory::global());
59 m_pTextCursor =
new TQCursor(pix,2,2);
74bool TextTool::processEvent(TQEvent* e)
78 case TQEvent::MouseButtonPress:
79 mousePress( (TQMouseEvent*)e );
83 case TQEvent::MouseButtonRelease:
84 mouseRelease( (TQMouseEvent*)e );
88 case TQEvent::MouseMove:
89 mouseMove( (TQMouseEvent*)e );
100void TextTool::setActivated(
bool a)
104 m_textAction->setChecked(
true);
105 view()->canvasWidget()->setCursor(*m_pTextCursor);
108 m_textAction->setChecked(
false);
113void TextTool::text(TQRect r)
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() ) );
120 float x = startPoint.x() < releasePoint.x() ? startPoint.x() : releasePoint.x();
121 float y = startPoint.y() < releasePoint.y() ? startPoint.y() : releasePoint.y();
124 float w = releasePoint.x() - startPoint.x();
130 float h = releasePoint.y() - startPoint.y();
136 KivioDoc* doc = view()->doc();
137 KivioPage* page = view()->activePage();
139 KivioStencilSpawner* ss = doc->findInternalStencilSpawner(
"Dave Marotti - Text");
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);
155 doc->updateView(page);
157 applyToolAction(page->selectedStencils());
159 if (stencil->text().isEmpty()) {
160 page->deleteSelectedStencils();
161 doc->updateView(page);
165void TextTool::mousePress( TQMouseEvent *e )
167 if(e->button() == TQt::LeftButton) {
168 KoPoint pagePoint = view()->canvasWidget()->mapFromScreen(e->pos());
170 KivioPage *page = view()->activePage();
171 KivioStencil* stencil = page->checkForStencil( &pagePoint, &colType, 0.0,
false);
174 applyToolAction(stencil, pagePoint);
175 }
else if(startRubberBanding(e)) {
176 m_mode = stmDrawRubber;
185bool TextTool::startRubberBanding( TQMouseEvent *e )
187 view()->canvasWidget()->startRectDraw( e->pos(), KivioCanvas::Rubber );
188 view()->canvasWidget()->repaint();
189 m_startPoint = e->pos();
194void TextTool::mouseMove( TQMouseEvent * e )
199 continueRubberBanding(e);
207void TextTool::continueRubberBanding( TQMouseEvent *e )
209 view()->canvasWidget()->continueRectDraw( e->pos(), KivioCanvas::Rubber );
212void TextTool::mouseRelease( TQMouseEvent *e )
214 m_releasePoint = e->pos();
225 view()->canvasWidget()->repaint();
228void TextTool::endRubberBanding(TQMouseEvent *)
231 view()->canvasWidget()->endRectDraw();
234 if( m_startPoint != m_releasePoint ) {
235 rect = view()->canvasWidget()->rect();
237 rect.setTopLeft(m_startPoint);
238 rect.setWidth(view()->zoomHandler()->zoomItX(100));
239 rect.setHeight(view()->zoomHandler()->zoomItY(20));
245 view()->pluginManager()->activateDefaultTool();
249void TextTool::applyToolAction(TQPtrList<KivioStencil>* stencils)
251 if(stencils->isEmpty()) {
255 KivioStencil* stencil = stencils->first();
259 if(stencil->hasTextBox()) {
264 stencil = stencils->next();
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()));
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);
295 if(stencil->text() != text)
297 KivioChangeStencilTextCommand *cmd =
new KivioChangeStencilTextCommand(i18n(
"Change Stencil Text"),
298 stencil, stencil->text(), text, view()->activePage());
299 stencil->setText(text);
300 macroCmd->addCommand(cmd);
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);
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);
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);
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);
336 stencil = stencils->next();
340 view()->doc()->addCommand(macroCmd);
341 view()->doc()->updateView(view()->activePage());
348void TextTool::applyToolAction(KivioStencil* stencil,
const KoPoint& pos)
354 TQString name = stencil->getTextBoxName(pos);
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)));
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();
373 if(stencil->text(name) != text)
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);
382 TQFont font = editor.font();
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);
392 TQColor textColor = editor.fontColor();
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);
403 int halignment = editor.horizontalAlignment();
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);
414 int valignment = editor.verticalAlignment();
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);
426 view()->doc()->addCommand(macroCmd);
427 view()->doc()->updateView(view()->activePage());
434void TextTool::makePermanent()
439#include "tool_text.moc"