Qt 4.8
abstractviewinspector.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 "abstractviewinspector.h"
43 
44 #include "abstracttool.h"
46 
47 #include <QtDeclarative/QDeclarativeEngine>
48 #include <QtDeclarative/QDeclarativeComponent>
49 #include <QtDeclarative/private/qdeclarativedebughelper_p.h>
50 #include "QtDeclarative/private/qdeclarativeinspectorservice_p.h"
51 
52 #include <QtGui/QVBoxLayout>
53 #include <QtGui/QMouseEvent>
54 #include <QtGui/QWidget>
55 
56 namespace QmlJSDebugger {
57 
59  QObject(parent),
60  m_currentTool(0),
61  m_showAppOnTop(false),
62  m_designModeBehavior(false),
63  m_animationPaused(false),
64  m_slowDownFactor(1.0),
65  m_debugService(0)
66 {
69  this, SLOT(handleMessage(QByteArray)));
70 }
71 
73  const QStringList &importList,
74  const QString &filename)
75 {
76  if (!parent)
77  return;
78 
79  QString imports;
80  foreach (const QString &s, importList) {
81  imports += s;
82  imports += QLatin1Char('\n');
83  }
84 
85  QDeclarativeContext *parentContext = declarativeEngine()->contextForObject(parent);
87  QByteArray constructedQml = QString(imports + qml).toLatin1();
88 
89  component.setData(constructedQml, QUrl::fromLocalFile(filename));
90  QObject *newObject = component.create(parentContext);
91  if (newObject)
92  reparentQmlObject(newObject, parent);
93 }
94 
96 {
98 }
99 
101 {
102  if (m_designModeBehavior == value)
103  return;
104 
105  m_designModeBehavior = value;
107  sendDesignModeBehavior(value);
108 }
109 
111 {
112  Q_ASSERT(slowDownFactor > 0);
113  if (m_slowDownFactor == slowDownFactor)
114  return;
115 
116  animationSpeedChangeRequested(slowDownFactor);
117  sendAnimationSpeed(slowDownFactor);
118 }
119 
121 {
122  if (m_animationPaused == paused)
123  return;
124 
126  sendAnimationPaused(paused);
127 }
128 
130 {
131  if (m_slowDownFactor != factor) {
132  m_slowDownFactor = factor;
133  emit animationSpeedChanged(factor);
134  }
135 
136  const float effectiveFactor = m_animationPaused ? 0 : factor;
138 }
139 
141 {
142  if (m_animationPaused != paused) {
143  m_animationPaused = paused;
145  }
146 
147  const float effectiveFactor = paused ? 0 : m_slowDownFactor;
149 }
150 
152 {
153  if (viewWidget()) {
155  Qt::WindowFlags flags = window->windowFlags();
156  if (appOnTop)
157  flags |= Qt::WindowStaysOnTopHint;
158  else
159  flags &= ~Qt::WindowStaysOnTopHint;
160 
161  window->setWindowFlags(flags);
162  window->show();
163  }
164 
165  m_showAppOnTop = appOnTop;
166  sendShowAppOnTop(appOnTop);
167 
168  emit showAppOnTopChanged(appOnTop);
169 }
170 
172 {
174 }
175 
177 {
179 }
180 
182 {
184 }
185 
187 {
189 }
190 
192 {
193  if (!designModeBehavior())
194  return QObject::eventFilter(obj, event);
195 
196  switch (event->type()) {
197  case QEvent::Leave:
198  if (leaveEvent(event))
199  return true;
200  break;
202  if (mousePressEvent(static_cast<QMouseEvent*>(event)))
203  return true;
204  break;
205  case QEvent::MouseMove:
206  if (mouseMoveEvent(static_cast<QMouseEvent*>(event)))
207  return true;
208  break;
210  if (mouseReleaseEvent(static_cast<QMouseEvent*>(event)))
211  return true;
212  break;
213  case QEvent::KeyPress:
214  if (keyPressEvent(static_cast<QKeyEvent*>(event)))
215  return true;
216  break;
217  case QEvent::KeyRelease:
218  if (keyReleaseEvent(static_cast<QKeyEvent*>(event)))
219  return true;
220  break;
222  if (mouseDoubleClickEvent(static_cast<QMouseEvent*>(event)))
223  return true;
224  break;
225  case QEvent::Wheel:
226  if (wheelEvent(static_cast<QWheelEvent*>(event)))
227  return true;
228  break;
229  default:
230  break;
231  }
232 
233  return QObject::eventFilter(obj, event);
234 }
235 
237 {
238  m_currentTool->leaveEvent(event);
239  return true;
240 }
241 
243 {
245  return true;
246 }
247 
249 {
250  if (event->buttons()) {
252  } else {
254  }
255  return true;
256 }
257 
259 {
261  return true;
262 }
263 
265 {
267  return true;
268 }
269 
271 {
272  switch (event->key()) {
273  case Qt::Key_V:
275  break;
276 // disabled because multiselection does not do anything useful without design mode
277 // case Qt::Key_M:
278 // changeTool(InspectorProtocol::SelectMarqueeTool);
279 // break;
280  case Qt::Key_I:
282  break;
283  case Qt::Key_Z:
285  break;
286  case Qt::Key_Space:
288  break;
289  default:
290  break;
291  }
292 
294  return true;
295 }
296 
298 {
300  return true;
301 }
302 
304 {
305  m_currentTool->wheelEvent(event);
306  return true;
307 }
308 
310 {
311  QDataStream ds(message);
312 
314  ds >> type;
315 
316  switch (type) {
318  int itemCount = 0;
319  ds >> itemCount;
320 
321  QList<QObject*> selectedObjects;
322  for (int i = 0; i < itemCount; ++i) {
323  int debugId = -1;
324  ds >> debugId;
326  selectedObjects << obj;
327  }
328 
329  changeCurrentObjects(selectedObjects);
330  break;
331  }
333  reloadView();
334  break;
335  }
337  qreal speed;
338  ds >> speed;
340  break;
341  }
343  bool paused;
344  ds >> paused;
346  break;
347  }
350  ds >> tool;
351  changeTool(tool);
352  break;
353  }
355  bool inDesignMode;
356  ds >> inDesignMode;
357  setDesignModeBehavior(inDesignMode);
358  break;
359  }
361  bool showOnTop;
362  ds >> showOnTop;
363  setShowAppOnTop(showOnTop);
364  break;
365  }
367  QString qml;
368  int parentId;
369  QString filename;
370  QStringList imports;
371  ds >> qml >> parentId >> imports >> filename;
373  imports, filename);
374  break;
375  }
377  int debugId;
378  ds >> debugId;
380  obj->deleteLater();
381  break;
382  }
384  int debugId, newParent;
385  ds >> debugId >> newParent;
388  break;
389  }
391  int itemCount;
392  ds >> itemCount;
394  for (int i = 0; i < itemCount; ++i) {
395  int itemDebugId;
396  QString itemIdString;
397  ds >> itemDebugId
398  >> itemIdString;
399 
400  m_stringIdForObjectId.insert(itemDebugId, itemIdString);
401  }
402  break;
403  }
406  break;
407  }
408  default:
409  qWarning() << "Warning: Not handling message:" << type;
410  }
411 }
412 
414 {
415  QByteArray message;
416  QDataStream ds(&message, QIODevice::WriteOnly);
417 
419  << inDesignMode;
420 
421  m_debugService->sendMessage(message);
422 }
423 
425 {
426  QByteArray message;
427  QDataStream ds(&message, QIODevice::WriteOnly);
428 
430  << objects.length();
431 
432  foreach (QObject *object, objects) {
433  int id = QDeclarativeDebugService::idForObject(object);
434  ds << id;
435  }
436 
437  m_debugService->sendMessage(message);
438 }
439 
441 {
442  QByteArray message;
443  QDataStream ds(&message, QIODevice::WriteOnly);
444 
446  << toolId;
447 
448  m_debugService->sendMessage(message);
449 }
450 
452 {
453  QByteArray message;
454  QDataStream ds(&message, QIODevice::WriteOnly);
455 
457  << slowDownFactor;
458 
459  m_debugService->sendMessage(message);
460 }
461 
463 {
464  QByteArray message;
465  QDataStream ds(&message, QIODevice::WriteOnly);
466 
468  << paused;
469 
470  m_debugService->sendMessage(message);
471 }
472 
474 {
475  QByteArray message;
476  QDataStream ds(&message, QIODevice::WriteOnly);
477 
479 
480  m_debugService->sendMessage(message);
481 }
482 
484 {
485  QByteArray message;
486  QDataStream ds(&message, QIODevice::WriteOnly);
487 
489 
490  m_debugService->sendMessage(message);
491 }
492 
494 {
495  QByteArray message;
496  QDataStream ds(&message, QIODevice::WriteOnly);
497 
499  << color;
500 
501  m_debugService->sendMessage(message);
502 }
503 
505 {
506  const int id = QDeclarativeDebugService::idForObject(obj);
507  return m_stringIdForObjectId.value(id);
508 }
509 
510 } // namespace QmlJSDebugger
511 
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
The QKeyEvent class describes a key event.
Definition: qevent.h:224
static void setAnimationSlowDownFactor(qreal factor)
int type
Definition: qmetatype.cpp:239
double qreal
Definition: qglobal.h:1193
static QObject * objectForId(int)
Returns the object for unique id.
void clear()
Removes all items from the hash.
Definition: qhash.h:574
QString idStringForObject(QObject *obj) const
virtual void wheelEvent(QWheelEvent *event)=0
The QWheelEvent class contains parameters that describe a wheel event.
Definition: qevent.h:139
virtual void keyPressEvent(QKeyEvent *event)=0
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
#define SLOT(a)
Definition: qobjectdefs.h:226
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
void setData(const QByteArray &, const QUrl &baseUrl)
Sets the QDeclarativeComponent to use the given QML data.
virtual void mouseMoveEvent(QMouseEvent *event)=0
static QDeclarativeInspectorService * instance()
The QString class provides a Unicode character string.
Definition: qstring.h:83
void sendMessage(const QByteArray &message)
virtual void mouseDoubleClickEvent(QMouseEvent *event)=0
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
void animationPausedChanged(bool paused)
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
virtual void reparentQmlObject(QObject *object, QObject *newParent)=0
virtual bool keyReleaseEvent(QKeyEvent *keyEvent)
virtual QObject * create(QDeclarativeContext *context=0)
Create an object instance from this component.
virtual bool mousePressEvent(QMouseEvent *event)
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:753
void sendAnimationSpeed(qreal slowDownFactor)
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
NSWindow * window
The QDeclarativeComponent class encapsulates a QML component definition.
void createQmlObject(const QString &qml, QObject *parent, const QStringList &importList, const QString &filename=QString())
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
#define emit
Definition: qobjectdefs.h:76
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
void setWindowFlags(Qt::WindowFlags type)
Definition: qwidget.cpp:10399
Q_CORE_EXPORT void qWarning(const char *,...)
QDeclarativeInspectorService * m_debugService
virtual bool mouseMoveEvent(QMouseEvent *event)
virtual QWidget * viewWidget() const =0
QByteArray toLatin1() const Q_REQUIRED_RESULT
Returns a Latin-1 representation of the string as a QByteArray.
Definition: qstring.cpp:3993
static qreal component(const QPointF &point, unsigned int i)
void show()
Shows the widget and its child widgets.
void handleMessage(const QByteArray &message)
virtual bool keyPressEvent(QKeyEvent *event)
virtual bool eventFilter(QObject *, QEvent *)
Filters events if this object has been installed as an event filter for the watched object...
Definition: qobject.cpp:1375
virtual void changeCurrentObjects(const QList< QObject *> &objects)=0
The QMouseEvent class contains parameters that describe a mouse event.
Definition: qevent.h:85
virtual bool mouseDoubleClickEvent(QMouseEvent *event)
bool eventFilter(QObject *, QEvent *)
Filters events if this object has been installed as an event filter for the watched object...
static int idForObject(QObject *)
Returns a unique id for object.
Qt::MouseButtons buttons() const
Returns the button state when the event was generated.
Definition: qevent.h:102
void sendCurrentObjects(const QList< QObject *> &)
void designModeBehaviorChanged(bool inDesignMode)
virtual bool wheelEvent(QWheelEvent *event)
void sendCurrentTool(Constants::DesignTool toolId)
virtual void leaveEvent(QEvent *event)=0
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
int length() const
This function is identical to count().
Definition: qlist.h:281
Definition: qnamespace.h:54
The QDeclarativeContext class defines a context within a QML engine.
virtual bool mouseReleaseEvent(QMouseEvent *event)
QWidget * window() const
Returns the window for this widget, i.e.
Definition: qwidget.cpp:4492
virtual QDeclarativeEngine * declarativeEngine() const =0
virtual void mousePressEvent(QMouseEvent *event)=0
static QUrl fromLocalFile(const QString &localfile)
Returns a QUrl representation of localFile, interpreted as a local file.
Definition: qurl.cpp:6374
void showAppOnTopChanged(bool showAppOnTop)
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
virtual void mouseReleaseEvent(QMouseEvent *event)=0
static QDeclarativeContext * contextForObject(const QObject *)
Returns the QDeclarativeContext for the object, or 0 if no context has been set.
Qt::WindowFlags windowFlags() const
Window flags are a combination of a type (e.
Definition: qwidget.h:939
void clearComponentCache()
Clears the engine&#39;s internal component cache.
virtual void changeTool(InspectorProtocol::Tool tool)=0
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
virtual void keyReleaseEvent(QKeyEvent *keyEvent)=0
void animationSpeedChanged(qreal factor)
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
virtual void hoverMoveEvent(QMouseEvent *event)=0
The QList class is a template class that provides lists.
Definition: qdatastream.h:62