Qt 4.8
liveselectiontool.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "liveselectiontool.h"
43 #include "livelayeritem.h"
44 
45 #include "../qdeclarativeviewinspector_p.h"
46 
47 #include <QtGui/QApplication>
48 #include <QtGui/QWheelEvent>
49 #include <QtGui/QMouseEvent>
50 #include <QtGui/QClipboard>
51 #include <QtGui/QMenu>
52 #include <QtGui/QAction>
53 #include <QtGui/QGraphicsObject>
54 
55 #include <QtDeclarative/QDeclarativeItem>
56 #include <QtDeclarative/QDeclarativeEngine>
57 
58 #include <QtCore/QDebug>
59 
60 namespace QmlJSDebugger {
61 
63  AbstractLiveEditTool(editorView),
64  m_rubberbandSelectionMode(false),
65  m_rubberbandSelectionManipulator(
66  QDeclarativeViewInspectorPrivate::get(editorView)->manipulatorLayer, editorView),
67  m_singleSelectionManipulator(editorView),
68  m_selectionIndicator(editorView,
69  QDeclarativeViewInspectorPrivate::get(editorView)->manipulatorLayer),
70  //m_resizeIndicator(editorView->manipulatorLayer()),
71  m_selectOnlyContentItems(true)
72 {
73 
74 }
75 
77 {
78 }
79 
81 {
83 }
84 
86  modifiers)
87 {
90  if (modifiers.testFlag(Qt::ControlModifier)) {
92  } else if (modifiers.testFlag(Qt::ShiftModifier)) {
94  }
95  return selectionType;
96 }
97 
99 {
100  QDeclarativeViewInspectorPrivate *inspectorPrivate
102  const QList<QGraphicsItem*> selectedItems = inspectorPrivate->selectedItems();
103 
104  if (selectedItems.isEmpty())
105  return false;
106 
107  foreach (QGraphicsItem *item, itemList)
108  if (selectedItems.contains(item))
109  return true;
110 
111  return false;
112 }
113 
115 {
116  QDeclarativeViewInspectorPrivate *inspectorPrivate
118  QList<QGraphicsItem*> itemList = inspectorPrivate->selectableItems(event->pos());
120 
121  if (event->buttons() & Qt::LeftButton) {
123 
126  } else {
129  }
130  } else if (event->buttons() & Qt::RightButton) {
131  createContextMenu(itemList, event->globalPos());
132  }
133 }
134 
136 {
137  QMenu contextMenu;
138  connect(&contextMenu, SIGNAL(hovered(QAction*)),
140 
141  m_contextMenuItemList = itemList;
142 
143  contextMenu.addAction(tr("Items"));
144  contextMenu.addSeparator();
145  int shortcutKey = Qt::Key_1;
146  int i = 0;
147 
148  foreach (QGraphicsItem * const item, itemList) {
149  QString itemTitle = titleForItem(item);
150  QAction *elementAction = contextMenu.addAction(itemTitle, this,
152 
153  if (inspector()->selectedItems().contains(item)) {
154  QFont boldFont = elementAction->font();
155  boldFont.setBold(true);
156  elementAction->setFont(boldFont);
157  }
158 
159  elementAction->setData(i);
160 
161  if (shortcutKey <= Qt::Key_9) {
162  elementAction->setShortcut(QKeySequence(shortcutKey));
163  shortcutKey++;
164  }
165 
166  ++i;
167  }
168  // add root item separately
169  // QString itemTitle = QString(tr("%1")).arg(titleForItem(view()->currentRootItem()));
170  // contextMenu.addAction(itemTitle, this, SLOT(contextMenuElementSelected()));
171  // m_contextMenuItemList.append(view()->currentRootItem());
172 
173  contextMenu.exec(globalPos);
175 }
176 
178 {
179  QAction *senderAction = static_cast<QAction*>(sender());
180  int itemListIndex = senderAction->data().toInt();
181  if (itemListIndex >= 0 && itemListIndex < m_contextMenuItemList.length()) {
182 
183  QPointF updatePt(0, 0);
184  QGraphicsItem *item = m_contextMenuItemList.at(itemListIndex);
187  QList<QGraphicsItem*>() << item,
188  false);
190  }
191 }
192 
194 {
195  int itemListIndex = action->data().toInt();
196  if (itemListIndex >= 0 && itemListIndex < m_contextMenuItemList.length()) {
197  QGraphicsObject *item = m_contextMenuItemList.at(itemListIndex)->toGraphicsObject();
199  }
200 }
201 
203 {
205  QPointF mouseMovementVector = m_singleSelectionManipulator.beginPoint() - event->pos();
206 
207  if ((mouseMovementVector.toPoint().manhattanLength() > Constants::DragStartDistance)
209  {
211  //view()->changeToMoveTool(m_singleSelectionManipulator.beginPoint());
212  return;
213  }
215  QPointF mouseMovementVector = m_rubberbandSelectionManipulator.beginPoint() - event->pos();
216 
217  if ((mouseMovementVector.toPoint().manhattanLength() > Constants::DragStartDistance)
220 
221  if (event->modifiers().testFlag(Qt::ControlModifier))
224  else if (event->modifiers().testFlag(Qt::ShiftModifier))
227  else
230  }
231  }
232 }
233 
235 {
236 // ### commented out until move tool is re-enabled
237 // QList<QGraphicsItem*> itemList = view()->items(event->pos());
238 // if (!itemList.isEmpty() && !m_rubberbandSelectionMode) {
239 //
240 // foreach (QGraphicsItem *item, itemList) {
241 // if (item->type() == Constants::ResizeHandleItemType) {
242 // ResizeHandleItem* resizeHandle = ResizeHandleItem::fromGraphicsItem(item);
243 // if (resizeHandle)
244 // view()->changeTool(Constants::ResizeToolMode);
245 // return;
246 // }
247 // }
248 // if (topSelectedItemIsMovable(itemList))
249 // view()->changeTool(Constants::MoveToolMode);
250 // }
251  QDeclarativeViewInspectorPrivate *inspectorPrivate
253 
254  QList<QGraphicsItem*> selectableItemList = inspectorPrivate->selectableItems(event->pos());
255  if (!selectableItemList.isEmpty()) {
256  QGraphicsObject *item = selectableItemList.first()->toGraphicsObject();
257  if (item)
259 
260  return;
261  }
262 
264 }
265 
267 {
270  }
272 
273  QPointF mouseMovementVector = m_rubberbandSelectionManipulator.beginPoint() - event->pos();
274  if (mouseMovementVector.toPoint().manhattanLength() < Constants::DragStartDistance) {
276 
277  if (event->modifiers().testFlag(Qt::ControlModifier))
280  else if (event->modifiers().testFlag(Qt::ShiftModifier))
283  else
286 
288  } else {
290 
291  if (event->modifiers().testFlag(Qt::ControlModifier))
294  else if (event->modifiers().testFlag(Qt::ShiftModifier))
297  else
300 
302  }
303  }
304 }
305 
307 {
308  switch (event->key()) {
309  case Qt::Key_Left:
310  case Qt::Key_Right:
311  case Qt::Key_Up:
312  case Qt::Key_Down:
313  // disabled for now, cannot move stuff yet.
314  //view()->changeTool(Constants::MoveToolMode);
315  //view()->currentTool()->keyPressEvent(event);
316  break;
317  }
318 }
319 
321 {
323  return;
324 
325  QDeclarativeViewInspectorPrivate *inspectorPrivate
327  QList<QGraphicsItem*> itemList = inspectorPrivate->selectableItems(event->pos());
328 
329  if (itemList.isEmpty())
330  return;
331 
332  int selectedIdx = 0;
333  if (!inspector()->selectedItems().isEmpty()) {
334  selectedIdx = itemList.indexOf(inspector()->selectedItems().first());
335  if (selectedIdx >= 0) {
336  if (event->delta() > 0) {
337  selectedIdx++;
338  if (selectedIdx == itemList.length())
339  selectedIdx = 0;
340  } else if (event->delta() < 0) {
341  selectedIdx--;
342  if (selectedIdx == -1)
343  selectedIdx = itemList.length() - 1;
344  }
345  } else {
346  selectedIdx = 0;
347  }
348  }
349 
350  QPointF updatePt(0, 0);
353  QList<QGraphicsItem*>() << itemList.at(selectedIdx),
354  false);
356 
357 }
358 
359 void LiveSelectionTool::setSelectOnlyContentItems(bool selectOnlyContentItems)
360 {
361  m_selectOnlyContentItems = selectOnlyContentItems;
362 }
363 
365 {
366 #ifndef QT_NO_CURSOR
368 #endif
372  //m_resizeIndicator.clear();
373 }
374 
376 {
377  foreach (const QWeakPointer<QGraphicsObject> &obj, m_selectedItemList) {
378  if (!obj.isNull()) {
379  disconnect(obj.data(), SIGNAL(xChanged()), this, SLOT(repaintBoundingRects()));
380  disconnect(obj.data(), SIGNAL(yChanged()), this, SLOT(repaintBoundingRects()));
381  disconnect(obj.data(), SIGNAL(widthChanged()), this, SLOT(repaintBoundingRects()));
382  disconnect(obj.data(), SIGNAL(heightChanged()), this, SLOT(repaintBoundingRects()));
383  disconnect(obj.data(), SIGNAL(rotationChanged()), this, SLOT(repaintBoundingRects()));
384  }
385  }
386 
387  QList<QGraphicsObject*> objects = toGraphicsObjectList(itemList);
389 
390  foreach (QGraphicsObject *obj, objects) {
392  connect(obj, SIGNAL(xChanged()), this, SLOT(repaintBoundingRects()));
393  connect(obj, SIGNAL(yChanged()), this, SLOT(repaintBoundingRects()));
394  connect(obj, SIGNAL(widthChanged()), this, SLOT(repaintBoundingRects()));
395  connect(obj, SIGNAL(heightChanged()), this, SLOT(repaintBoundingRects()));
396  connect(obj, SIGNAL(rotationChanged()), this, SLOT(repaintBoundingRects()));
397  }
398 
400  //m_resizeIndicator.setItems(toGraphicsObjectList(itemList));
401 }
402 
404 {
406 }
407 
409 {
411 
412  if (event->modifiers().testFlag(Qt::ControlModifier))
415  else if (event->modifiers().testFlag(Qt::ShiftModifier))
418  else
421 
423 }
424 
425 } // namespace QmlJSDebugger
QList< QGraphicsItem * > m_contextMenuItemList
QList< QWeakPointer< QGraphicsObject > > m_selectedItemList
bool alreadySelected(const QList< QGraphicsItem *> &itemList) const
void mouseReleaseEvent(QMouseEvent *event)
The QKeyEvent class describes a key event.
Definition: qevent.h:224
void selectedItemsChanged(const QList< QGraphicsItem *> &itemList)
void setShortcut(const QKeySequence &shortcut)
Definition: qaction.cpp:450
void setFont(const QFont &font)
Definition: qaction.cpp:623
void mousePressEvent(QMouseEvent *event)
The QWheelEvent class contains parameters that describe a wheel event.
Definition: qevent.h:139
void start()
Sets this time to the current time.
Definition: qdatetime.cpp:2070
void setItems(const QList< QWeakPointer< QGraphicsObject > > &itemList)
#define SLOT(a)
Definition: qobjectdefs.h:226
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
Definition: qgraphicsitem.h:89
void selectUnderPoint(QMouseEvent *event)
void keyPressEvent(QKeyEvent *event)
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool isNull() const
Returns true if this object is holding a reference to a null pointer.
QAction * addAction(const QString &text)
This convenience function creates a new action with text.
Definition: qmenu.cpp:1453
virtual bool event(QEvent *)
This virtual function receives events to an object and should return true if the event e was recogniz...
Definition: qobject.cpp:1200
T * data() const
LiveSingleSelectionManipulator m_singleSelectionManipulator
void setData(const QVariant &var)
Sets the action&#39;s internal data to the given userData.
Definition: qaction.cpp:1297
const QPoint & pos() const
Returns the position of the mouse cursor, relative to the widget that received the event...
Definition: qevent.h:95
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
QObject * sender() const
Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; othe...
Definition: qobject.cpp:2327
int toInt(bool *ok=0) const
Returns the variant as an int if the variant has type() Int , Bool , ByteArray , Char ...
Definition: qvariant.cpp:2625
QGraphicsObject * toGraphicsObject()
Return the graphics item cast to a QGraphicsObject, if the class is actually a graphics object...
int key() const
Returns the code of the key that was pressed or released.
Definition: qevent.h:231
#define SIGNAL(a)
Definition: qobjectdefs.h:227
static const int DragStartDistance
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
void setBold(bool)
If enable is true sets the font&#39;s weight to QFont::Bold ; otherwise sets the weight to QFont::Normal...
Definition: qfont.h:352
QBool contains(const T &t) const
Returns true if the list contains an occurrence of value; otherwise returns false.
Definition: qlist.h:880
void wheelEvent(QWheelEvent *event)
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
LiveSelectionIndicator m_selectionIndicator
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
int manhattanLength() const
Returns the sum of the absolute values of x() and y(), traditionally known as the "Manhattan length" ...
Definition: qpoint.cpp:489
QFont font
the action&#39;s font
Definition: qaction.h:81
void hoverMoveEvent(QMouseEvent *event)
int delta() const
Returns the distance that the wheel is rotated, in eighths of a degree.
Definition: qevent.h:150
QAction * addSeparator()
This convenience function creates a new separator action, i.e.
Definition: qmenu.cpp:1583
void setCursor(const QCursor &)
Definition: qwidget.cpp:5290
void clear()
Removes all items from the list.
Definition: qlist.h:764
static QList< QGraphicsObject * > toGraphicsObjectList(const QList< QGraphicsItem *> &itemList)
Qt::Orientation orientation() const
Returns the wheel&#39;s orientation.
Definition: qevent.h:159
QList< QGraphicsItem * > selectableItems(const QPoint &pos) const
The QMouseEvent class contains parameters that describe a mouse event.
Definition: qevent.h:85
void mouseMoveEvent(QMouseEvent *event)
const QPoint & pos() const
Returns the position of the mouse cursor relative to the widget that received the event...
Definition: qevent.h:151
T & first()
Returns a reference to the first item in the list.
Definition: qlist.h:282
QAction * exec()
Executes this menu synchronously.
Definition: qmenu.cpp:2101
LiveSelectionTool(QDeclarativeViewInspector *editorView)
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
Disconnects signal in object sender from method in object receiver.
Definition: qobject.cpp:2895
void contextMenuElementHovered(QAction *action)
int elapsed() const
Returns the number of milliseconds that have elapsed since the last time start() or restart() was cal...
Definition: qdatetime.cpp:2123
The QKeySequence class encapsulates a key sequence as used by shortcuts.
Definition: qkeysequence.h:72
Qt::MouseButtons buttons() const
Returns the button state when the event was generated.
Definition: qevent.h:102
int indexOf(const T &t, int from=0) const
Returns the index position of the first occurrence of value in the list, searching forward from index...
Definition: qlist.h:847
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72
The QFont class specifies a font used for drawing text.
Definition: qfont.h:64
QPoint toPoint() const
Rounds the coordinates of this point to the nearest integer, and returns a QPoint object with the rou...
Definition: qpoint.h:376
void createContextMenu(const QList< QGraphicsItem *> &itemList, QPoint globalPos)
int length() const
This function is identical to count().
Definition: qlist.h:281
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
QVariant data() const
Returns the user data as set in QAction::setData.
Definition: qaction.cpp:1280
static const char * get(QDBusError::ErrorType code)
Definition: qdbuserror.cpp:141
void highlight(const QList< QGraphicsObject *> &item)
QDeclarativeViewInspector * inspector() const
LiveRubberBandSelectionManipulator m_rubberbandSelectionManipulator
Qt::KeyboardModifiers modifiers() const
Returns the keyboard modifier flags that existed immediately before the event occurred.
Definition: qevent.h:79
The QGraphicsObject class provides a base class for all graphics items that require signals...
const QPoint & globalPos() const
Returns the global position of the mouse cursor at the time of the event.
Definition: qevent.h:96
void select(SelectionType selectionType, const QList< QGraphicsItem *> &items, bool selectOnlyContentItems)
The QAction class provides an abstract user interface action that can be inserted into widgets...
Definition: qaction.h:64
static QDeclarativeViewInspectorPrivate * get(QDeclarativeViewInspector *v)
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
void setSelectOnlyContentItems(bool selectOnlyContentItems)
QString titleForItem(QGraphicsItem *item)
LiveSingleSelectionManipulator::SelectionType getSelectionType(Qt::KeyboardModifiers modifiers)