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