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

marble

  • sources
  • kde-4.14
  • kdeedu
  • marble
  • src
  • lib
  • marble
  • graphicsview
ScreenGraphicsItem.cpp
Go to the documentation of this file.
1 //
2 // This file is part of the Marble Virtual Globe.
3 //
4 // This program is free software licensed under the GNU LGPL. You can
5 // find a copy of this license in LICENSE.txt in the top directory of
6 // the source code.
7 //
8 // Copyright 2009-2010 Bastian Holst <bastianholst@gmx.de>
9 //
10 
11 // Self
12 #include "ScreenGraphicsItem.h"
13 #include "ScreenGraphicsItem_p.h"
14 
15 // Marble
16 #include "MarbleDebug.h"
17 #include "MarbleWidget.h"
18 
19 // Qt
20 #include <QMouseEvent>
21 
22 
23 using namespace Marble;
24 
25 ScreenGraphicsItem::ScreenGraphicsItem( MarbleGraphicsItem *parent )
26  : MarbleGraphicsItem( new ScreenGraphicsItemPrivate( this, parent ) )
27 {
28 }
29 
30 ScreenGraphicsItem::ScreenGraphicsItem( ScreenGraphicsItemPrivate *d_ptr )
31  : MarbleGraphicsItem( d_ptr )
32 {
33 }
34 
35 ScreenGraphicsItem::~ScreenGraphicsItem()
36 {
37 }
38 
39 QPointF ScreenGraphicsItem::position() const
40 {
41  return p()->m_position;
42 }
43 
44 void ScreenGraphicsItem::setPosition( const QPointF& position )
45 {
46  p()->m_position = position;
47 }
48 
49 QPointF ScreenGraphicsItem::positivePosition() const
50 {
51  return p()->positivePosition();
52 }
53 
54 QList<QPointF> ScreenGraphicsItem::absolutePositions() const
55 {
56  return p()->absolutePositions();
57 }
58 
59 ScreenGraphicsItem::GraphicsItemFlags ScreenGraphicsItem::flags() const
60 {
61  return p()->m_flags;
62 }
63 
64 void ScreenGraphicsItem::setFlags( ScreenGraphicsItem::GraphicsItemFlags flags )
65 {
66  p()->m_flags = flags;
67 }
68 
69 bool ScreenGraphicsItem::eventFilter( QObject *object, QEvent *e )
70 {
71  MarbleWidget *widget = dynamic_cast<MarbleWidget*>(object);
72  if ( !widget ) {
73  return MarbleGraphicsItem::eventFilter( object, e );
74  }
75 
76  if ( !p()->m_floatItemMoving ) {
77  if ( MarbleGraphicsItem::eventFilter( object, e ) ) {
78  return true;
79  }
80 
81  if ( !visible() || !p()->isMovable() ) {
82  return false;
83  }
84 
85  if ( e->type() == QEvent::MouseMove ) {
86  return false;
87  }
88 
89  // Move ScreenGraphicsItem
90  if ( e->type() == QEvent::MouseButtonPress )
91  {
92  QMouseEvent *event = static_cast<QMouseEvent*>(e);
93 
94  // Click and move above a float item triggers moving the float item
95  if ( contains( event->pos() ) ) {
96  if ( event->button() == Qt::LeftButton ) {
97  p()->m_floatItemMoveStartPos = event->pos();
98  p()->m_floatItemMoving = true;
99  return true;
100  }
101  }
102  }
103 
104  return false;
105  }
106  else {
107  // Move ScreenGraphicsItem
108  if ( e->type() == QEvent::MouseMove
109  || e->type() == QEvent::MouseButtonPress
110  || e->type() == QEvent::MouseButtonRelease )
111  {
112  QMouseEvent *event = static_cast<QMouseEvent*>( e );
113  // The rect the item was painted on before. We add one pixel as antialiasing could
114  // result into painting on these pixels to.
115  QRectF floatItemRect = QRectF( positivePosition() - QPoint( 1, 1 ),
116  size() + QSize( 2, 2 ) );
117 
118  if ( e->type() == QEvent::MouseMove && event->buttons() & Qt::LeftButton ) {
119  const QPoint &point = event->pos();
120  QPointF position = positivePosition();
121  qreal newX = qMax<qreal>( 0, position.x()+point.x()-p()->m_floatItemMoveStartPos.x() );
122  qreal newY = qMax<qreal>( 0, position.y()+point.y()-p()->m_floatItemMoveStartPos.y() );
123 
124  // docking behavior
125  const qreal dockArea = 60.0; // Alignment area width/height
126  const qreal dockJump = 30.0; // Alignment indicator jump size
127  if ( widget->width()-size().width()-newX < dockArea ) {
128  newX = qMin( qreal( -1.0 ), size().width() + newX-widget->width() );
129  if ( p()->m_floatItemMoveStartPos.x() < event->pos().x() ) {
130  // Indicate change to right alignment with a short jump
131  newX = qMax( newX, -(dockArea-dockJump) );
132  }
133  }
134  if ( widget->height()-size().height()-newY < dockArea ) {
135  newY = qMin( qreal( -1.0 ), size().height() + newY-widget->height() );
136  if (p()->m_floatItemMoveStartPos.y()<event->pos().y()) {
137  // Indicate change to bottom alignment with a short jump
138  newY = qMax( newY, -( dockArea - dockJump ) );
139  }
140  }
141 
142  setPosition( QPointF( newX,newY ) );
143  // The rect the item will be painted on now. We add one pixel as
144  // antialiasing could result into painting on these pixels to.
145  QRect newFloatItemRect = QRectF( positivePosition() - QPoint( 1, 1 ),
146  size() + QSize( 2, 2 ) ).toRect();
147  p()->m_floatItemMoveStartPos = event->pos();
148  QRegion dirtyRegion( floatItemRect.toRect() );
149  dirtyRegion = dirtyRegion.united( newFloatItemRect );
150 
151  widget->setAttribute( Qt::WA_NoSystemBackground, false );
152  widget->update(dirtyRegion);
153  widget->setAttribute( Qt::WA_NoSystemBackground, widget->viewport()->mapCoversViewport() );
154  return true;
155  }
156 
157  if ( e->type() == QEvent::MouseButtonRelease ) {
158  p()->m_floatItemMoving = false;
159  }
160 
161  // Use a special cursor as long as the item is moved
162  if ( p()->m_floatItemMoving ) {
163  widget->setCursor(QCursor(Qt::SizeAllCursor));
164  return true;
165  }
166  }
167 
168  return MarbleGraphicsItem::eventFilter( object, e );
169  }
170 }
171 
172 ScreenGraphicsItemPrivate *ScreenGraphicsItem::p()
173 {
174  return static_cast<ScreenGraphicsItemPrivate *>( d );
175 }
176 
177 const ScreenGraphicsItemPrivate *ScreenGraphicsItem::p() const
178 {
179  return static_cast<const ScreenGraphicsItemPrivate *>( d );
180 }
Marble::ScreenGraphicsItem::ScreenGraphicsItem
ScreenGraphicsItem(MarbleGraphicsItem *parent=0)
Definition: ScreenGraphicsItem.cpp:25
QEvent
Marble::ScreenGraphicsItemPrivate::m_floatItemMoving
bool m_floatItemMoving
Definition: ScreenGraphicsItem_p.h:110
QEvent::type
Type type() const
QRectF::toRect
QRect toRect() const
QWidget::setCursor
void setCursor(const QCursor &)
Marble::ScreenGraphicsItem::absolutePositions
QList< QPointF > absolutePositions() const
Return the absolute position of the ScreenGraphicsItem.
Definition: ScreenGraphicsItem.cpp:54
Marble::ScreenGraphicsItemPrivate
Definition: ScreenGraphicsItem_p.h:24
Marble::ScreenGraphicsItem::~ScreenGraphicsItem
virtual ~ScreenGraphicsItem()
Definition: ScreenGraphicsItem.cpp:35
Marble::ScreenGraphicsItemPrivate::m_floatItemMoveStartPos
QPoint m_floatItemMoveStartPos
Definition: ScreenGraphicsItem_p.h:109
QWidget::setAttribute
void setAttribute(Qt::WidgetAttribute attribute, bool on)
Marble::MarbleGraphicsItem::size
QSizeF size() const
Returns the size of the item.
Definition: MarbleGraphicsItem.cpp:136
QPoint
QMouseEvent
MarbleDebug.h
QWidget::update
void update()
QPoint::x
int x() const
QPoint::y
int y() const
Marble::ScreenGraphicsItemPrivate::m_flags
ScreenGraphicsItem::GraphicsItemFlags m_flags
Definition: ScreenGraphicsItem_p.h:112
QPointF
Marble::MarbleWidget
A widget class that displays a view of the earth.
Definition: MarbleWidget.h:104
Marble::ScreenGraphicsItem::flags
GraphicsItemFlags flags() const
Returns the flags of the item.
Definition: ScreenGraphicsItem.cpp:59
QWidget::width
width
Marble::ScreenGraphicsItemPrivate::positivePosition
QPointF positivePosition() const
Definition: ScreenGraphicsItem_p.h:49
QRect
Marble::MarbleGraphicsItem::contains
bool contains(const QPointF &point) const
Returns true if the Item contains point in parent coordinates.
Definition: MarbleGraphicsItem.cpp:110
QPointF::x
qreal x() const
QPointF::y
qreal y() const
ScreenGraphicsItem_p.h
QObject
Marble::ViewportParams::mapCoversViewport
bool mapCoversViewport() const
Definition: ViewportParams.cpp:398
Marble::MarbleGraphicsItem
Definition: MarbleGraphicsItem.h:34
QList< QPointF >
Marble::MarbleGraphicsItem::visible
bool visible() const
Returns if the item is visible.
Definition: MarbleGraphicsItem.cpp:177
ScreenGraphicsItem.h
Marble::MarbleWidget::viewport
ViewportParams * viewport()
Definition: MarbleWidget.cpp:299
Marble::ScreenGraphicsItem::positivePosition
QPointF positivePosition() const
Return the positive position of the ScreenGraphicsItem.
Definition: ScreenGraphicsItem.cpp:49
Marble::MarbleGraphicsItem::d
MarbleGraphicsItemPrivate *const d
Definition: MarbleGraphicsItem.h:144
QSize
Marble::ScreenGraphicsItem::setFlags
void setFlags(GraphicsItemFlags flags)
Sets the flags to flags.
Definition: ScreenGraphicsItem.cpp:64
Marble::ScreenGraphicsItem::eventFilter
virtual bool eventFilter(QObject *, QEvent *)
Definition: ScreenGraphicsItem.cpp:69
Marble::ScreenGraphicsItemPrivate::m_position
QPointF m_position
Definition: ScreenGraphicsItem_p.h:105
QRectF
Marble::ScreenGraphicsItem::setPosition
void setPosition(const QPointF &position)
Set the position of the ScreenGraphicsItem.
Definition: ScreenGraphicsItem.cpp:44
Marble::ScreenGraphicsItem::position
QPointF position() const
Return the position of the ScreenGraphicsItem.
Definition: ScreenGraphicsItem.cpp:39
MarbleWidget.h
This file contains the headers for MarbleWidget.
QSizeF::height
qreal height() const
Marble::MarbleGraphicsItem::eventFilter
virtual bool eventFilter(QObject *object, QEvent *e)
Definition: MarbleGraphicsItem.cpp:225
QCursor
Marble::ScreenGraphicsItemPrivate::absolutePositions
QList< QPointF > absolutePositions() const
Definition: ScreenGraphicsItem_p.h:66
QRegion::united
QRegion united(const QRegion &r) const
QSizeF::width
qreal width() const
QWidget::height
height
QRegion
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:13:41 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

marble

Skip menu "marble"
  • Main Page
  • Namespace List
  • Namespace Members
  • 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