kivio

kiviotargettool.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 Peter Simonsson <psn@linux.se>,
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19#include "kiviotargettool.h"
20
21#include <tqcursor.h>
22
23#include <tdelocale.h>
24
25#include "kivio_stencil.h"
26#include "kivio_view.h"
27#include "kivio_canvas.h"
28#include "kivio_page.h"
29#include "kivio_doc.h"
30#include "mousetoolaction.h"
31#include "kivio_pluginmanager.h"
32#include "kivio_command.h"
33
34namespace Kivio {
35 TargetTool::TargetTool(KivioView* parent) : MouseTool(parent, "Add Target Mouse Tool")
36 {
37 m_targetAction = new Kivio::MouseToolAction(i18n("Add Connector Target"),
38 "add_target", 0, actionCollection(), "addTargetTool");
39 connect(m_targetAction, TQ_SIGNAL(toggled(bool)), this, TQ_SLOT(setActivated(bool)));
40 connect(m_targetAction, TQ_SIGNAL(doubleClicked()), this, TQ_SLOT(makePermanent()));
41
42 m_permanent = false;
43 }
44
45 TargetTool::~TargetTool()
46 {
47 }
48
49 bool TargetTool::processEvent(TQEvent* e)
50 {
51 if(e->type() == TQEvent::MouseButtonPress) {
52 mousePress(static_cast<TQMouseEvent*>(e));
53 return true;
54 } else if(e->type() == TQEvent::MouseMove) {
55 mouseMove(static_cast<TQMouseEvent*>(e));
56 return true;
57 }
58
59 return false;
60 }
61
62 void TargetTool::setActivated(bool a)
63 {
64 if(a) {
65 m_targetAction->setChecked(true);
66 emit activated(this);
67 } else if(m_targetAction->isChecked()) {
68 m_targetAction->setChecked(false);
69 m_permanent = false;
70 }
71 }
72
73 void TargetTool::applyToolAction(KivioStencil* stencil, const KoPoint& pos)
74 {
75 KivioAddConnectorTargetCommand* command = new KivioAddConnectorTargetCommand(i18n("Add Connector Target"), view()->activePage(), stencil, pos);
76 command->execute();
77 view()->doc()->addCommand(command);
78 }
79
80 void TargetTool::mousePress(TQMouseEvent* e)
81 {
82 KoPoint p = view()->canvasWidget()->mapFromScreen(e->pos());
83 int colType;
84 KivioStencil* stencil = view()->canvasWidget()->activePage()->checkForStencil(&p, &colType, 0, false);
85
86 if(stencil) {
87 applyToolAction(stencil, p);
88
89 if(!m_permanent) {
90 view()->pluginManager()->activateDefaultTool();
91 }
92 }
93 }
94
95 void TargetTool::mouseMove(TQMouseEvent* e)
96 {
97 KoPoint p = view()->canvasWidget()->mapFromScreen(e->pos());
98 int colType;
99
100 if(view()->canvasWidget()->activePage()->checkForStencil(&p, &colType, 0, false)) {
101 view()->canvasWidget()->setCursor(TQt::CrossCursor);
102 } else {
103 view()->canvasWidget()->setCursor(TQt::ForbiddenCursor);
104 }
105 }
106
107 void TargetTool::makePermanent()
108 {
109 m_permanent = true;
110 }
111}
112
113#include "kiviotargettool.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