• 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
MarbleGraphicsItem.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 #include "MarbleGraphicsItem.h"
12 
13 #include "MarbleGraphicsItem_p.h"
14 
15 // Marble
16 #include "MarbleDebug.h"
17 #include "ViewportParams.h"
18 
19 // Qt
20 #include <QList>
21 #include <QSet>
22 #include <QPainter>
23 #include <QPixmap>
24 #include <QMouseEvent>
25 
26 using namespace Marble;
27 
28 MarbleGraphicsItem::MarbleGraphicsItem( MarbleGraphicsItemPrivate *d_ptr )
29  : d( d_ptr )
30 {
31 }
32 
33 MarbleGraphicsItem::~MarbleGraphicsItem()
34 {
35  delete d;
36 }
37 
38 bool MarbleGraphicsItem::paintEvent( QPainter *painter, const ViewportParams *viewport )
39 {
40  if ( !p()->m_visibility ) {
41  return true;
42  }
43 
44  if ( p()->m_repaintNeeded ) {
45  p()->updateChildPositions();
46  p()->m_pixmap = QPixmap();
47  p()->m_repaintNeeded = false;
48  }
49 
50  setProjection( viewport );
51 
52  if ( p()->positions().size() == 0 ) {
53  return true;
54  }
55 
56  // At the moment, as GraphicsItems can't be zoomed or rotated ItemCoordinateCache
57  // and DeviceCoordianteCache is exactly the same
58  if ( ItemCoordinateCache == cacheMode()
59  || DeviceCoordinateCache == cacheMode() )
60  {
61  const QSize neededPixmapSize = size().toSize() + QSize( 1, 1 ); // adding a pixel for rounding errors
62  if ( p()->m_pixmap.size() != neededPixmapSize ) {
63 
64  if ( p()->m_pixmap.size() != neededPixmapSize ) {
65  if ( size().isValid() && !size().isNull() ) {
66  p()->m_pixmap = QPixmap( neededPixmapSize );
67  }
68  else {
69  mDebug() << "Warning: Invalid pixmap size suggested: " << d->m_size;
70  }
71  }
72 
73  p()->m_pixmap.fill( Qt::transparent );
74  QPainter pixmapPainter( &p()->m_pixmap );
75  // We paint in best quality here, as we only have to paint once.
76  pixmapPainter.setRenderHint( QPainter::Antialiasing, true );
77  // The cache image will get a 0.5 pixel bounding to save antialiasing effects.
78  pixmapPainter.translate( 0.5, 0.5 );
79  paint( &pixmapPainter );
80 
81  // Paint children
82  foreach ( MarbleGraphicsItem *item, p()->m_children ) {
83  item->paintEvent( &pixmapPainter, viewport );
84  }
85  }
86 
87  foreach( const QPointF& position, p()->positions() ) {
88  painter->drawPixmap( position, p()->m_pixmap );
89  }
90  }
91  else {
92  foreach( const QPointF& position, p()->positions() ) {
93  painter->save();
94 
95  painter->translate( position );
96  paint( painter );
97 
98  // Paint children
99  foreach ( MarbleGraphicsItem *item, p()->m_children ) {
100  item->paintEvent( painter, viewport );
101  }
102 
103  painter->restore();
104  }
105  }
106 
107  return true;
108 }
109 
110 bool MarbleGraphicsItem::contains( const QPointF& point ) const
111 {
112  foreach( const QRectF& rect, d->boundingRects() ) {
113  if( rect.contains( point ) )
114  return true;
115  }
116  return false;
117 }
118 
119 QList<QRectF> MarbleGraphicsItemPrivate::boundingRects() const
120 {
121  QList<QRectF> list;
122 
123  foreach( const QPointF &point, positions() ) {
124  QRectF rect( point, m_size );
125  if( rect.x() < 0 )
126  rect.setLeft( 0 );
127  if( rect.y() < 0 )
128  rect.setTop( 0 );
129 
130  list.append( rect );
131  }
132 
133  return list;
134 }
135 
136 QSizeF MarbleGraphicsItem::size() const
137 {
138  return p()->m_size;
139 }
140 
141 AbstractMarbleGraphicsLayout *MarbleGraphicsItem::layout() const
142 {
143  return p()->m_layout;
144 }
145 
146 void MarbleGraphicsItem::setLayout( AbstractMarbleGraphicsLayout *layout )
147 {
148  // Deleting the old layout
149  delete p()->m_layout;
150  p()->m_layout = layout;
151  update();
152 }
153 
154 MarbleGraphicsItem::CacheMode MarbleGraphicsItem::cacheMode() const
155 {
156  return p()->m_cacheMode;
157 }
158 
159 void MarbleGraphicsItem::setCacheMode( CacheMode mode )
160 {
161  p()->m_cacheMode = mode;
162  if ( p()->m_cacheMode == NoCache ) {
163  p()->m_repaintNeeded = true;
164  }
165 }
166 
167 void MarbleGraphicsItem::update()
168 {
169  p()->m_repaintNeeded = true;
170 
171  // Update the parent.
172  if ( p()->m_parent ) {
173  p()->m_parent->update();
174  }
175 }
176 
177 bool MarbleGraphicsItem::visible() const
178 {
179  return p()->m_visibility;
180 }
181 
182 void MarbleGraphicsItem::setVisible( bool visible )
183 {
184  p()->m_visibility = visible;
185 }
186 
187 void MarbleGraphicsItem::hide()
188 {
189  setVisible( false );
190 }
191 
192 void MarbleGraphicsItem::show()
193 {
194  setVisible( true );
195 }
196 
197 void MarbleGraphicsItem::setSize( const QSizeF& size )
198 {
199  if ( p()->m_size != size ) {
200  p()->m_size = size;
201  update();
202  }
203 }
204 
205 QSizeF MarbleGraphicsItem::contentSize() const
206 {
207  return size();
208 }
209 
210 void MarbleGraphicsItem::setContentSize( const QSizeF& size )
211 {
212  setSize( size );
213 }
214 
215 QRectF MarbleGraphicsItem::contentRect() const
216 {
217  return QRectF( QPointF( 0, 0 ), contentSize() );
218 }
219 
220 void MarbleGraphicsItem::paint( QPainter *painter )
221 {
222  Q_UNUSED( painter );
223 }
224 
225 bool MarbleGraphicsItem::eventFilter( QObject *object, QEvent *e )
226 {
227  if ( ! ( e->type() == QEvent::MouseButtonDblClick
228  || e->type() == QEvent::MouseMove
229  || e->type() == QEvent::MouseButtonPress
230  || e->type() == QEvent::MouseButtonRelease ) )
231  {
232  return false;
233  }
234 
235  QMouseEvent *event = static_cast<QMouseEvent*> (e);
236 
237  if( !p()->m_children.isEmpty() ) {
238  QList<QPointF> absolutePositions = p()->absolutePositions();
239 
240  foreach( const QPointF& absolutePosition, absolutePositions ) {
241  QPoint shiftedPos = event->pos() - absolutePosition.toPoint();
242 
243  if ( QRect( QPoint( 0, 0 ), size().toSize() ).contains( shiftedPos ) ) {
244  foreach( MarbleGraphicsItem *child, p()->m_children ) {
245  QList<QRectF> childRects = child->d->boundingRects();
246 
247  foreach( const QRectF& childRect, childRects ) {
248  if( childRect.toRect().contains( shiftedPos ) ) {
249  if( child->eventFilter( object, e ) ) {
250  return true;
251  }
252  }
253  }
254  }
255  }
256  }
257  }
258 
259  return false;
260 }
261 
262 MarbleGraphicsItemPrivate *MarbleGraphicsItem::p()
263 {
264  return d;
265 }
266 
267 const MarbleGraphicsItemPrivate *MarbleGraphicsItem::p() const
268 {
269  return d;
270 }
271 
272 void MarbleGraphicsItem::setProjection( const ViewportParams *viewport )
273 {
274  p()->setProjection( viewport );
275 }
QEvent
QPixmap::size
QSize size() const
Marble::MarbleGraphicsItemPrivate::setProjection
virtual void setProjection(const ViewportParams *viewport)=0
QEvent::type
Type type() const
QRectF::toRect
QRect toRect() const
Marble::MarbleGraphicsItem::cacheMode
CacheMode cacheMode() const
Returns the cache mode of the item.
Definition: MarbleGraphicsItem.cpp:154
QPainter::setRenderHint
void setRenderHint(RenderHint hint, bool on)
Marble::MarbleGraphicsItem::contentSize
virtual QSizeF contentSize() const
Returns the size of the content of the MarbleGraphicsItem.
Definition: MarbleGraphicsItem.cpp:205
Marble::MarbleGraphicsItem::setSize
void setSize(const QSizeF &size)
Set the size of the item.
Definition: MarbleGraphicsItem.cpp:197
QPixmap::fill
void fill(const QColor &color)
QRectF::x
qreal x() const
QRectF::y
qreal y() const
MarbleGraphicsItem_p.h
Marble::MarbleGraphicsItemPrivate::m_children
QSet< MarbleGraphicsItem * > m_children
Definition: MarbleGraphicsItem_p.h:109
Marble::MarbleGraphicsItem::show
void show()
Shows the item.
Definition: MarbleGraphicsItem.cpp:192
QSizeF::isValid
bool isValid() const
QPainter::save
void save()
Marble::MarbleGraphicsItem::size
QSizeF size() const
Returns the size of the item.
Definition: MarbleGraphicsItem.cpp:136
QPoint
QMouseEvent
Marble::MarbleGraphicsItem::setLayout
void setLayout(AbstractMarbleGraphicsLayout *layout)
Set the layout of the graphics item.
Definition: MarbleGraphicsItem.cpp:146
MarbleDebug.h
QRectF::setLeft
void setLeft(qreal x)
Marble::MarbleGraphicsItem::setContentSize
virtual void setContentSize(const QSizeF &size)
Set the size of the content of the item.
Definition: MarbleGraphicsItem.cpp:210
QPointF
Marble::MarbleGraphicsItemPrivate::m_parent
MarbleGraphicsItem *const m_parent
Definition: MarbleGraphicsItem_p.h:107
Marble::MarbleGraphicsItemPrivate::absolutePositions
virtual QList< QPointF > absolutePositions() const =0
Marble::MarbleGraphicsItemPrivate::m_visibility
bool m_visibility
Definition: MarbleGraphicsItem_p.h:104
MarbleGraphicsItem.h
QRect
Marble::MarbleGraphicsItem::contains
bool contains(const QPointF &point) const
Returns true if the Item contains point in parent coordinates.
Definition: MarbleGraphicsItem.cpp:110
Marble::MarbleGraphicsItemPrivate::m_pixmap
QPixmap m_pixmap
Definition: MarbleGraphicsItem_p.h:102
QList::append
void append(const T &value)
Marble::MarbleGraphicsItem::NoCache
Definition: MarbleGraphicsItem.h:40
QObject
QSizeF::toSize
QSize toSize() const
QPainter::drawPixmap
void drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
QPainter
Marble::MarbleGraphicsItem::ItemCoordinateCache
Definition: MarbleGraphicsItem.h:41
Marble::MarbleGraphicsItem
Definition: MarbleGraphicsItem.h:34
QRect::contains
bool contains(const QPoint &point, bool proper) const
QList
Marble::MarbleGraphicsItem::visible
bool visible() const
Returns if the item is visible.
Definition: MarbleGraphicsItem.cpp:177
Marble::MarbleGraphicsItem::MarbleGraphicsItem
MarbleGraphicsItem(MarbleGraphicsItemPrivate *d_ptr)
Definition: MarbleGraphicsItem.cpp:28
Marble::ViewportParams
A public class that controls what is visible in the viewport of a Marble map.
Definition: ViewportParams.h:44
QPixmap
Marble::MarbleGraphicsItem::d
MarbleGraphicsItemPrivate *const d
Definition: MarbleGraphicsItem.h:144
Marble::MarbleGraphicsItemPrivate::boundingRects
QList< QRectF > boundingRects() const
Used to get the set of screen bounding rects.
Definition: MarbleGraphicsItem.cpp:119
QSize
ViewportParams.h
This file contains the headers for ViewportParams.
Marble::MarbleGraphicsItemPrivate::m_cacheMode
MarbleGraphicsItem::CacheMode m_cacheMode
Definition: MarbleGraphicsItem_p.h:100
QPainter::restore
void restore()
Marble::MarbleGraphicsItem::DeviceCoordinateCache
Definition: MarbleGraphicsItem.h:42
Marble::MarbleGraphicsItemPrivate::positions
virtual QList< QPointF > positions() const =0
QSizeF
QPointF::toPoint
QPoint toPoint() const
Marble::MarbleGraphicsItem::setCacheMode
void setCacheMode(CacheMode mode)
Set the cache mode of the item.
Definition: MarbleGraphicsItem.cpp:159
Marble::MarbleGraphicsItemPrivate::updateChildPositions
void updateChildPositions()
Definition: MarbleGraphicsItem_p.h:83
QRectF
QRectF::setTop
void setTop(qreal y)
Marble::MarbleGraphicsItem::setVisible
void setVisible(bool visible)
Makes the item visible or invisible, depending on visible.
Definition: MarbleGraphicsItem.cpp:182
Marble::MarbleGraphicsItem::contentRect
virtual QRectF contentRect() const
Returns the rect of the content in item coordinates.
Definition: MarbleGraphicsItem.cpp:215
QPainter::translate
void translate(const QPointF &offset)
Marble::MarbleGraphicsItem::update
void update()
Marks the item and all parent items as invalid.
Definition: MarbleGraphicsItem.cpp:167
Marble::MarbleGraphicsItemPrivate::m_size
QSizeF m_size
Definition: MarbleGraphicsItem_p.h:96
Marble::MarbleGraphicsItem::layout
AbstractMarbleGraphicsLayout * layout() const
Returns the layout of the MarbleGraphicsItem.
Definition: MarbleGraphicsItem.cpp:141
QRectF::contains
bool contains(const QPointF &point) const
Marble::MarbleGraphicsItem::paintEvent
bool paintEvent(QPainter *painter, const ViewportParams *viewport)
Paints the item on the screen in view coordinates.
Definition: MarbleGraphicsItem.cpp:38
Marble::MarbleGraphicsItemPrivate::m_layout
AbstractMarbleGraphicsLayout * m_layout
Definition: MarbleGraphicsItem_p.h:112
Marble::MarbleGraphicsItemPrivate
Definition: MarbleGraphicsItem_p.h:30
Marble::MarbleGraphicsItem::paint
virtual void paint(QPainter *painter)
Paints the item in item coordinates.
Definition: MarbleGraphicsItem.cpp:220
QSizeF::isNull
bool isNull() const
Marble::MarbleGraphicsItem::CacheMode
CacheMode
Definition: MarbleGraphicsItem.h:39
Marble::MarbleGraphicsItem::eventFilter
virtual bool eventFilter(QObject *object, QEvent *e)
Definition: MarbleGraphicsItem.cpp:225
Marble::MarbleGraphicsItemPrivate::m_repaintNeeded
bool m_repaintNeeded
Definition: MarbleGraphicsItem_p.h:98
Marble::mDebug
QDebug mDebug()
a function to replace qDebug() in Marble library code
Definition: MarbleDebug.cpp:36
Marble::AbstractMarbleGraphicsLayout
Definition: AbstractMarbleGraphicsLayout.h:22
Marble::MarbleGraphicsItem::hide
void hide()
Hides the item.
Definition: MarbleGraphicsItem.cpp:187
Marble::MarbleGraphicsItem::setProjection
virtual void setProjection(const ViewportParams *viewport)
Definition: MarbleGraphicsItem.cpp:272
Marble::MarbleGraphicsItem::~MarbleGraphicsItem
virtual ~MarbleGraphicsItem()
Definition: MarbleGraphicsItem.cpp:33
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:13:40 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