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

marble

  • sources
  • kde-4.12
  • kdeedu
  • marble
  • src
  • plugins
  • render
  • progress
ProgressFloatItem.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 2010 Dennis Nienhüser <earthwings@gentoo.org>
9 // Copyright 2010,2011 Bernhard Beschow <bbeschow@cs.tu-berlin.de>
10 //
11 
12 #include "ProgressFloatItem.h"
13 
14 #include "MarbleDebug.h"
15 #include "MarbleDirs.h"
16 #include "MarbleModel.h"
17 #include "MarbleWidget.h"
18 #include "ViewportParams.h"
19 #include "HttpDownloadManager.h"
20 
21 #include <QRect>
22 #include <QColor>
23 #include <QMutexLocker>
24 #include <QPaintDevice>
25 #include <QPainter>
26 
27 namespace Marble
28 {
29 
30 ProgressFloatItem::ProgressFloatItem()
31  : AbstractFloatItem( 0 )
32 {
33 }
34 
35 ProgressFloatItem::ProgressFloatItem( const MarbleModel *marbleModel )
36  : AbstractFloatItem( marbleModel, QPointF( -10.5, -150.5 ), QSizeF( 40.0, 40.0 ) ),
37  m_isInitialized( false ),
38  m_totalJobs( 0 ),
39  m_completedJobs ( 0 ),
40  m_completed( 1 ),
41  m_progressHideTimer(),
42  m_progressShowTimer(),
43  m_active( false ),
44  m_fontSize( 0 ),
45  m_repaintTimer()
46 {
47  // This timer is responsible to activate the automatic display with a small delay
48  m_progressShowTimer.setInterval( 250 );
49  m_progressShowTimer.setSingleShot( true );
50  connect( &m_progressShowTimer, SIGNAL(timeout()), this, SLOT(show()) );
51 
52  // This timer is responsible to hide the automatic display when downloads are finished
53  m_progressHideTimer.setInterval( 750 );
54  m_progressHideTimer.setSingleShot( true );
55  connect( &m_progressHideTimer, SIGNAL(timeout()), this, SLOT(hideProgress()) );
56 
57  // Repaint timer
58  m_repaintTimer.setSingleShot( true );
59  m_repaintTimer.setInterval( 1000 );
60  connect( &m_repaintTimer, SIGNAL(timeout()), this, SIGNAL(repaintNeeded()) );
61 
62  // The icon resembles the pie chart
63  QImage canvas( 16, 16, QImage::Format_ARGB32 );
64  canvas.fill( Qt::transparent );
65  QPainter painter( &canvas );
66  painter.setRenderHint( QPainter::Antialiasing, true );
67  painter.setPen( QColor ( Qt::black ) );
68  painter.drawEllipse( 1, 1, 14, 14 );
69  painter.setPen( Qt::NoPen );
70  painter.setBrush( QBrush( QColor( Qt::darkGray ) ) );
71  painter.drawPie( 2, 2, 12, 12, 1440, -1325 ); // 23 percent of a full circle
72  m_icon = QIcon( QPixmap::fromImage( canvas ) );
73 
74  // Plugin is enabled by default
75  setEnabled( true );
76 
77  // Plugin is visible by default on devices with small screens only
78  setVisible( MarbleGlobal::getInstance()->profiles() & MarbleGlobal::SmallScreen );
79 }
80 
81 ProgressFloatItem::~ProgressFloatItem ()
82 {
83  // nothing to do
84 }
85 
86 QStringList ProgressFloatItem::backendTypes() const
87 {
88  return QStringList( "progress" );
89 }
90 
91 QString ProgressFloatItem::name() const
92 {
93  return tr( "Download Progress Indicator" );
94 }
95 
96 QString ProgressFloatItem::guiString() const
97 {
98  return tr( "&Download Progress" );
99 }
100 
101 QString ProgressFloatItem::nameId() const
102 {
103  return QString( "progress" );
104 }
105 
106 QString ProgressFloatItem::version() const
107 {
108  return "1.0";
109 }
110 
111 QString ProgressFloatItem::description() const
112 {
113  return tr( "Shows a pie chart download progress indicator" );
114 }
115 
116 QString ProgressFloatItem::copyrightYears() const
117 {
118  return "2010, 2011";
119 }
120 
121 QList<PluginAuthor> ProgressFloatItem::pluginAuthors() const
122 {
123  return QList<PluginAuthor>()
124  << PluginAuthor( QString::fromUtf8( "Dennis Nienhüser" ), "earthwings@gentoo.org" )
125  << PluginAuthor( "Bernhard Beschow", "bbeschow@cs.tu-berlin.de" );
126 }
127 
128 QIcon ProgressFloatItem::icon() const
129 {
130  return m_icon;
131 }
132 
133 void ProgressFloatItem::initialize()
134 {
135  const HttpDownloadManager* manager = marbleModel()->downloadManager();
136  Q_ASSERT( manager );
137  connect( manager, SIGNAL(progressChanged(int,int)), this, SLOT(handleProgress(int,int)) , Qt::UniqueConnection );
138  connect( manager, SIGNAL(jobRemoved()), this, SLOT(removeProgressItem()), Qt::UniqueConnection );
139 
140  // Calculate font size
141  QFont myFont = font();
142  const QString text = "100%";
143  int fontSize = myFont.pointSize();
144  while( QFontMetrics( myFont ).boundingRect( text ).width() < contentRect().width() - 2 ) {
145  ++fontSize;
146  myFont.setPointSize( fontSize );
147  }
148  m_fontSize = fontSize - 1;
149 
150  m_isInitialized = true;
151 }
152 
153 bool ProgressFloatItem::isInitialized() const
154 {
155  return m_isInitialized;
156 }
157 
158 QPainterPath ProgressFloatItem::backgroundShape() const
159 {
160  QPainterPath path;
161 
162  if ( active() ) {
163  // Circular shape if active, invisible otherwise
164  QRectF rect = contentRect();
165  qreal width = rect.width();
166  qreal height = rect.height();
167  path.addEllipse( marginLeft() + 2 * padding(), marginTop() + 2 * padding(), width, height );
168  }
169 
170  return path;
171 }
172 
173 void ProgressFloatItem::paintContent( QPainter *painter )
174 {
175  // Stop repaint timer if it is already running
176  m_repaintTimer.stop();
177 
178  if ( !active() ) {
179  return;
180  }
181 
182  painter->save();
183 
184  // Paint progress pie
185  int startAngle = 90 * 16; // 12 o' clock
186  int spanAngle = -ceil ( 360 * 16 * m_completed );
187  QRectF rect( contentRect() );
188  rect.adjust( 1, 1, -1, -1 );
189 
190  painter->setBrush( QColor( Qt::white ) );
191  painter->setPen( Qt::NoPen );
192  painter->drawPie( rect, startAngle, spanAngle );
193 
194  // Paint progress label
195  QFont myFont = font();
196  myFont.setPointSize( m_fontSize );
197  QString done = QString::number( (int) ( m_completed * 100 ) ) + '%';
198  int fontWidth = QFontMetrics( myFont ).boundingRect( done ).width();
199  QPointF baseline( padding() + 0.5 * ( rect.width() - fontWidth ), 0.75 * rect.height() );
200  QPainterPath path;
201  path.addText( baseline, myFont, done );
202 
203  painter->setFont( myFont );
204  painter->setBrush( QBrush() );
205  painter->setPen( QPen() );
206  painter->drawPath( path );
207 
208  painter->restore();
209 }
210 
211 bool ProgressFloatItem::eventFilter(QObject *object, QEvent *e)
212 {
213  if ( !enabled() || !visible() ) {
214  return false;
215  }
216 
217  return AbstractFloatItem::eventFilter( object, e );
218 }
219 
220 void ProgressFloatItem::removeProgressItem()
221 {
222  m_jobMutex.lock();
223  ++m_completedJobs;
224  m_jobMutex.unlock();
225 
226  if ( enabled() ) {
227  if ( !active() && !m_progressShowTimer.isActive() ) {
228  m_progressShowTimer.start();
229  m_progressHideTimer.stop();
230  } else if ( active() ) {
231  update();
232  scheduleRepaint();
233  }
234  }
235 }
236 
237 void ProgressFloatItem::handleProgress( int current, int queued )
238 {
239  m_jobMutex.lock();
240  if ( current < 1 ) {
241  m_totalJobs = 0;
242  m_completedJobs = 0;
243  } else {
244  m_totalJobs = qMax<int>( m_totalJobs, queued + current );
245  }
246  m_jobMutex.unlock();
247 
248  if ( enabled() ) {
249  if ( !active() && !m_progressShowTimer.isActive() && m_totalJobs > 0 ) {
250  m_progressShowTimer.start();
251  m_progressHideTimer.stop();
252  } else if ( active() ) {
253  if ( m_totalJobs < 1 || m_completedJobs == m_totalJobs ) {
254  m_progressShowTimer.stop();
255  m_progressHideTimer.start();
256  }
257  update();
258  scheduleRepaint();
259  }
260 
261  m_completed = 1.0;
262  if ( m_totalJobs && m_completedJobs <= m_totalJobs ) {
263  m_completed = (qreal) m_completedJobs / (qreal) m_totalJobs;
264  }
265  }
266 }
267 
268 void ProgressFloatItem::hideProgress()
269 {
270  if ( enabled() ) {
271  setActive( false );
272 
273  update();
274  emit repaintNeeded( QRegion() );
275  }
276 }
277 
278 bool ProgressFloatItem::active() const
279 {
280  return m_active;
281 }
282 
283 void ProgressFloatItem::setActive( bool active )
284 {
285  m_active = active;
286  update();
287 }
288 
289 void ProgressFloatItem::show()
290 {
291  setActive( true );
292 
293  update();
294  emit repaintNeeded( QRegion() );
295 }
296 
297 void ProgressFloatItem::scheduleRepaint()
298 {
299  if ( !m_repaintTimer.isActive() ) {
300  m_repaintTimer.start();
301  }
302 }
303 
304 }
305 
306 Q_EXPORT_PLUGIN2( ProgressFloatItem, Marble::ProgressFloatItem )
307 
308 #include "ProgressFloatItem.moc"
Marble::ProgressFloatItem::guiString
QString guiString() const
String that should be displayed in GUI.
Definition: ProgressFloatItem.cpp:96
QPainter
Marble::AbstractFloatItem::visible
bool visible() const
Check visibility of the float item.
Definition: AbstractFloatItem.cpp:135
Marble::RenderPlugin::repaintNeeded
void repaintNeeded(QRegion dirtyRegion=QRegion())
This signal is emitted if an update of the view is needed.
Marble::ProgressFloatItem
A float item that shows a pie-chart progress indicator when downloads are active. ...
Definition: ProgressFloatItem.h:27
Marble::ProgressFloatItem::backgroundShape
QPainterPath backgroundShape() const
Returns the shape of the background.
Definition: ProgressFloatItem.cpp:158
MarbleModel.h
This file contains the headers for MarbleModel.
Marble::ProgressFloatItem::description
QString description() const
Returns a user description of the plugin.
Definition: ProgressFloatItem.cpp:111
Marble::FrameGraphicsItem::marginLeft
qreal marginLeft() const
Returns the left margin of the item.
Definition: FrameGraphicsItem.cpp:89
Marble::PluginAuthor
Definition: PluginInterface.h:28
Marble::AbstractFloatItem::eventFilter
virtual bool eventFilter(QObject *object, QEvent *e)
Definition: AbstractFloatItem.cpp:161
HttpDownloadManager.h
Marble::ProgressFloatItem::copyrightYears
QString copyrightYears() const
Definition: ProgressFloatItem.cpp:116
Marble::ProgressFloatItem::~ProgressFloatItem
~ProgressFloatItem()
Definition: ProgressFloatItem.cpp:81
QObject
MarbleDebug.h
Marble::ProgressFloatItem::eventFilter
bool eventFilter(QObject *object, QEvent *e)
Definition: ProgressFloatItem.cpp:211
ProgressFloatItem.h
Marble::ProgressFloatItem::backendTypes
QStringList backendTypes() const
Returns the name(s) of the backend that the plugin can render This method should return the name of t...
Definition: ProgressFloatItem.cpp:86
Marble::AbstractFloatItem
The abstract class for float item plugins.
Definition: AbstractFloatItem.h:48
Marble::ProgressFloatItem::initialize
void initialize()
Definition: ProgressFloatItem.cpp:133
Marble::FrameGraphicsItem::contentRect
QRectF contentRect() const
Returns the rect of the content in item coordinates.
Definition: FrameGraphicsItem.cpp:171
MarbleDirs.h
Marble::ProgressFloatItem::ProgressFloatItem
ProgressFloatItem()
Definition: ProgressFloatItem.cpp:30
Marble::ProgressFloatItem::icon
QIcon icon() const
Returns an icon for the plugin.
Definition: ProgressFloatItem.cpp:128
Marble::FrameGraphicsItem::padding
qreal padding() const
Returns the padding of the item.
Definition: FrameGraphicsItem.cpp:125
Q_EXPORT_PLUGIN2
#define Q_EXPORT_PLUGIN2(a, b)
Definition: marble_export.h:34
ViewportParams.h
This file contains the headers for ViewportParams.
Marble::MarbleGlobal::SmallScreen
Definition: MarbleGlobal.h:268
Marble::MarbleGlobal::getInstance
static MarbleGlobal * getInstance()
Definition: MarbleGlobal.cpp:37
Marble::ProgressFloatItem::version
QString version() const
Definition: ProgressFloatItem.cpp:106
Marble::ProgressFloatItem::nameId
QString nameId() const
Returns the unique name of the plugin.
Definition: ProgressFloatItem.cpp:101
Marble::ProgressFloatItem::paintContent
void paintContent(QPainter *painter)
Here the items paint their content.
Definition: ProgressFloatItem.cpp:173
Marble::MarbleModel
The data model (not based on QAbstractModel) for a MarbleWidget.
Definition: MarbleModel.h:96
Marble::RenderPlugin::setEnabled
void setEnabled(bool enabled)
settting enabled
Definition: RenderPlugin.cpp:137
Marble::FrameGraphicsItem::marginTop
qreal marginTop() const
Returns the top margin of the item.
Definition: FrameGraphicsItem.cpp:65
Marble::ProgressFloatItem::pluginAuthors
QList< PluginAuthor > pluginAuthors() const
Definition: ProgressFloatItem.cpp:121
Marble::MarbleGraphicsItem::update
void update()
Marks the item and all parent items as invalid.
Definition: MarbleGraphicsItem.cpp:167
MarbleWidget.h
This file contains the headers for MarbleWidget.
Marble::AbstractFloatItem::font
QFont font() const
current font for rendering
Definition: AbstractFloatItem.cpp:107
Marble::ProgressFloatItem::name
QString name() const
Returns the user-visible name of the plugin.
Definition: ProgressFloatItem.cpp:91
Marble::RenderPlugin::enabled
bool enabled() const
is enabled
Marble::MarbleModel::downloadManager
HttpDownloadManager * downloadManager()
Return the downloadmanager to load missing tiles.
Definition: MarbleModel.cpp:396
Marble::RenderPlugin::marbleModel
const MarbleModel * marbleModel() const
Access to the MarbleModel.
Definition: RenderPlugin.cpp:81
Marble::ProgressFloatItem::isInitialized
bool isInitialized() const
Definition: ProgressFloatItem.cpp:153
Marble::HttpDownloadManager
This class manages scheduled downloads.
Definition: HttpDownloadManager.h:44
Marble::AbstractFloatItem::setVisible
void setVisible(bool visible)
Set visibility of the float item.
Definition: AbstractFloatItem.cpp:128
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:38:52 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
  • kstars
  • libkdeedu
  •   keduvocdocument
  • 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