• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

rocs/VisualEditor

  • sources
  • kde-4.14
  • kdeedu
  • rocs
  • VisualEditor
  • Actions
SelectMoveHandAction.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Rocs.
3  Copyright 2008-2011 Tomaz Canabrava <tomaz.canabrava@gmail.com>
4  Copyright 2008 Ugo Sangiori <ugorox@gmail.com>
5  Copyright 2011 Andreas Cord-Landwehr <cola@uni-paderborn.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include "SelectMoveHandAction.h"
22 
23 #include "Scene/GraphScene.h"
24 #include "Scene/DataItem.h"
25 #include "DataStructure.h"
26 #include "DocumentManager.h"
27 #include "Data.h"
28 #include "Document.h"
29 
30 #include <QGraphicsView>
31 #include <QKeyEvent>
32 
33 #include <KLocale>
34 #include <KDebug>
35 
36 SelectMoveHandAction::SelectMoveHandAction(GraphScene *scene, QObject *parent)
37  : AbstractAction(scene, parent),
38  _currentItem(0),
39  _selectionMode(false)
40 {
41  setText(i18nc("@action:intoolbar", "Move"));
42  setToolTip(i18nc("@info:tooltip", "Select and move items."));
43  setIcon(KIcon("rocsselectmove"));
44  _name = "rocs-hand-select-move";
45 
46  connect(_graphScene, SIGNAL(keyPressed(QKeyEvent*)), this, SLOT(executeKeyPress(QKeyEvent*)));
47  connect(_graphScene, SIGNAL(keyReleased(QKeyEvent*)), this, SLOT(executeKeyRelease(QKeyEvent*)));
48 }
49 
50 SelectMoveHandAction::~SelectMoveHandAction()
51 {
52 }
53 
54 bool SelectMoveHandAction::executePress(QPointF pos)
55 {
56  if (!DocumentManager::self().activeDocument()->activeDataStructure()) {
57  return false;
58  }
59 
60  // check if there is item at click-position,
61  // if no item is selected, then enable rectangle selection mode and return
62  if (!(_currentItem = qgraphicsitem_cast<DataItem*>(_graphScene->itemAt(pos)))) {
63  _graphScene->views().at(0)->setInteractive(true);
64  _graphScene->views().at(0)->setDragMode(QGraphicsView::RubberBandDrag);
65  return true;
66  }
67 
68  // if item is not selected, deselect everything else and select it
69  if (!_selectionMode && _currentItem->isSelected() == false) {
70  foreach(QGraphicsItem * item, _graphScene->selectedItems()) {
71  item->setSelected(false);
72  }
73  }
74  _currentItem->setSelected(true);
75 
76  // switch to move mode for items that are currently selected
77  _deltas.clear();
78  foreach(QGraphicsItem * item, _graphScene->selectedItems()) {
79  if (DataItem *dItem = qgraphicsitem_cast<DataItem*>(item)) {
80  QPointF delta = pos - QPointF(dItem->data()->x(), dItem->data()->y());
81  _deltas.insert(dItem, delta);
82  }
83  }
84 
85  if (_graphScene->items().count() > 600) { // 5! + 25 items on screen
86  _graphScene->views().at(0)->setRenderHints(QPainter::Antialiasing
87  & QPainter::TextAntialiasing);
88  }
89  return true;
90 }
91 
92 bool SelectMoveHandAction::executeMove(QPointF pos)
93 {
94  // only move if we are in move mode: i.e., a data item is selected
95  if (!_currentItem || !_graphScene->selectedItems().contains(_currentItem)) {
96  return false;
97  }
98 
99  foreach(QGraphicsItem *item, _graphScene->selectedItems()) {
100  if (DataItem *dItem = qgraphicsitem_cast<DataItem*>(item)) {
101  dItem->data() -> setPos(pos.x() - _deltas.value(dItem).x(),
102  pos.y() - _deltas.value(dItem).y());
103  }
104  }
105 
106  return true;
107 }
108 
109 bool SelectMoveHandAction::executeRelease(QPointF pos)
110 {
111  _graphScene->views().at(0)->setDragMode(QGraphicsView::NoDrag);
112 
113  if (!_currentItem) {
114  return false;
115  }
116 
117  foreach(QGraphicsItem * item, _graphScene->selectedItems()) {
118  if (DataItem *dItem = qgraphicsitem_cast<DataItem*>(item)) {
119  dItem->data() -> setPos(pos.x() - _deltas.value(dItem).x(),
120  pos.y() - _deltas.value(dItem).y());
121  }
122  }
123 
124  _currentItem = 0;
125  if (_graphScene->items().count() > 600) { // 5! + 25 itens on screen
126  _graphScene->views()[0]->setRenderHint(QPainter::Antialiasing);
127  }
128  return true;
129 }
130 
131 bool SelectMoveHandAction::executeKeyPress(QKeyEvent* keyEvent)
132 {
133  Q_ASSERT(keyEvent->type() == QEvent::KeyPress);
134 
135  // consider key sequences
136  if (keyEvent->matches(QKeySequence::SelectAll)) {
137  foreach(QGraphicsItem * item, _graphScene->items()) {
138  if (DataItem *dItem = qgraphicsitem_cast<DataItem*>(item)) {
139  dItem->setSelected(true);
140  }
141  }
142  return true;
143  }
144 
145  // consider single key press events
146  QPointF move;
147  switch (keyEvent->key()) {
148  case Qt::Key_Escape: {
149  _graphScene->setAction(this);
150  break;
151  }
152  case Qt::Key_Up: {
153  moveSelectedItems(QPointF(0, -10));
154  break;
155  }
156  case Qt::Key_Down: {
157  moveSelectedItems(QPointF(0, 10));
158  break;
159  }
160  case Qt::Key_Left: {
161  moveSelectedItems(QPointF(-10, 0));
162  break;
163  }
164  case Qt::Key_Right: {
165  moveSelectedItems(QPointF(10, 0));
166  break;
167  }
168  case Qt::Key_Control: {
169  _selectionMode = true;
170  break;
171  }
172  default:
173  return false;
174  }
175  return true;
176 }
177 
178 void SelectMoveHandAction::moveSelectedItems(QPointF movement)
179 {
180  foreach(QGraphicsItem * item, _graphScene->selectedItems()) {
181  if (DataItem *dItem = qgraphicsitem_cast<DataItem*>(item)) {
182  dItem->data()-> setPos(dItem->data()->x() + movement.x(),
183  dItem->data()->y() + movement.y());
184  }
185  }
186 }
187 
188 bool SelectMoveHandAction::executeKeyRelease(QKeyEvent* keyEvent)
189 {
190  Q_ASSERT(keyEvent->type() == QEvent::KeyRelease);
191 
192  if (keyEvent->key() == Qt::Key_Control) {
193  _selectionMode = false;
194  return true;
195  }
196  return false;
197 }
QGraphicsItem::setSelected
void setSelected(bool selected)
QEvent::type
Type type() const
QGraphicsScene::itemAt
QGraphicsItem * itemAt(const QPointF &position) const
QGraphicsScene::selectedItems
QList< QGraphicsItem * > selectedItems() const
SelectMoveHandAction::SelectMoveHandAction
SelectMoveHandAction(GraphScene *scene, QObject *parent=0)
Default constructor.
Definition: SelectMoveHandAction.cpp:36
QGraphicsScene::items
QList< QGraphicsItem * > items() const
QGraphicsItem
SelectMoveHandAction::executeMove
bool executeMove(QPointF pos)
Executed when the mouse is moved at the scene scene.
Definition: SelectMoveHandAction.cpp:92
GraphScene.h
QMap::clear
void clear()
AbstractAction
the base class for custom actions. This class provides the basic functionality for all custom actions...
Definition: AbstractAction.h:36
QPointF
QPointF::x
qreal x() const
QPointF::y
qreal y() const
SelectMoveHandAction::~SelectMoveHandAction
~SelectMoveHandAction()
Default Destructor.
Definition: SelectMoveHandAction.cpp:50
GraphScene
Definition: GraphScene.h:38
QObject
AbstractAction::_name
QString _name
Definition: AbstractAction.h:90
QGraphicsItem::isSelected
bool isSelected() const
QGraphicsScene::views
QList< QGraphicsView * > views() const
SelectMoveHandAction::executeKeyPress
bool executeKeyPress(QKeyEvent *keyEvent)
Executed when a key is pressed.
Definition: SelectMoveHandAction.cpp:131
QKeyEvent::matches
bool matches(QKeySequence::StandardKey key) const
QKeyEvent::key
int key() const
QKeyEvent
SelectMoveHandAction::executeRelease
bool executeRelease(QPointF pos)
Executed when the left mouse button is released.
Definition: SelectMoveHandAction.cpp:109
AbstractAction::_graphScene
GraphScene * _graphScene
Definition: AbstractAction.h:88
GraphScene::setAction
void setAction(QAction *action)
Definition: GraphScene.cpp:69
DataItem
Definition: DataItem.h:38
DataItem.h
QMap::insert
iterator insert(const Key &key, const T &value)
SelectMoveHandAction.h
SelectMoveHandAction::executePress
bool executePress(QPointF pos)
Executed when the left mouse button is pressed at the scene.
Definition: SelectMoveHandAction.cpp:54
QMap::value
const T value(const Key &key) const
SelectMoveHandAction::executeKeyRelease
bool executeKeyRelease(QKeyEvent *keyEvent)
Executed when a key is released.
Definition: SelectMoveHandAction.cpp:188
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:16:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

rocs/VisualEditor

Skip menu "rocs/VisualEditor"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal