summaryrefslogtreecommitdiffstats
path: root/kivio/plugins/kiviotargettool/kiviotargettool.cpp
blob: c25fb36bd458d38e305319af14a35164ae0540f4 (plain)
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
/* This file is part of the KDE project
   Copyright (C) 2004 Peter Simonsson <[email protected]>,

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/
#include "kiviotargettool.h"

#include <tqcursor.h>

#include <klocale.h>

#include "kivio_stencil.h"
#include "kivio_view.h"
#include "kivio_canvas.h"
#include "kivio_page.h"
#include "kivio_doc.h"
#include "mousetoolaction.h"
#include "kivio_pluginmanager.h"
#include "kivio_command.h"

namespace Kivio {
  TargetTool::TargetTool(KivioView* parent) : MouseTool(parent, "Add Target Mouse Tool")
  {
    m_targetAction = new Kivio::MouseToolAction(i18n("Add Connector Target"),
        "add_target", 0, actionCollection(), "addTargetTool");
    connect(m_targetAction, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(setActivated(bool)));
    connect(m_targetAction, TQT_SIGNAL(doubleClicked()), this, TQT_SLOT(makePermanent()));

    m_permanent = false;
  }
  
  TargetTool::~TargetTool()
  {
  }
  
  bool TargetTool::processEvent(TQEvent* e)
  {
    if(e->type() == TQEvent::MouseButtonPress) {
      mousePress(TQT_TQMOUSEEVENT(e));
      return true;
    } else if(e->type() == TQEvent::MouseMove) {
      mouseMove(TQT_TQMOUSEEVENT(e));
      return true;
    }
    
    return false;
  }
  
  void TargetTool::setActivated(bool a)
  {
    if(a) {
      m_targetAction->setChecked(true);
      emit activated(this);
    } else if(m_targetAction->isChecked()) {
      m_targetAction->setChecked(false);
      m_permanent = false;
    }
  }
  
  void TargetTool::applyToolAction(KivioStencil* stencil, const KoPoint& pos)
  {
    KivioAddConnectorTargetCommand* command = new KivioAddConnectorTargetCommand(i18n("Add Connector Target"), view()->activePage(), stencil, pos);
    command->execute();
    view()->doc()->addCommand(command);
  }

  void TargetTool::mousePress(TQMouseEvent* e)
  {
    KoPoint p = view()->canvasWidget()->mapFromScreen(e->pos());
    int colType;
    KivioStencil* stencil = view()->canvasWidget()->activePage()->checkForStencil(&p, &colType, 0, false);
    
    if(stencil) {
      applyToolAction(stencil, p);
      
      if(!m_permanent) {
        view()->pluginManager()->activateDefaultTool();
      }
    }
  }

  void TargetTool::mouseMove(TQMouseEvent* e)
  {
    KoPoint p = view()->canvasWidget()->mapFromScreen(e->pos());
    int colType;
    
    if(view()->canvasWidget()->activePage()->checkForStencil(&p, &colType, 0, false)) {
      view()->canvasWidget()->setCursor(TQt::CrossCursor);
    } else {
      view()->canvasWidget()->setCursor(TQt::ForbiddenCursor);
    }
  }

  void TargetTool::makePermanent()
  {
    m_permanent = true;
  }
}

#include "kiviotargettool.moc"