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

KDEUI

  • sources
  • kde-4.14
  • kdelibs
  • kdeui
  • util
kpixmapsequenceoverlaypainter.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2009 Sebastian Trueg <trueg@kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "kpixmapsequenceoverlaypainter.h"
21 #include "kpixmapsequence.h"
22 
23 #include <QtGui/QWidget>
24 #include <QtCore/QPoint>
25 #include <QtCore/QRect>
26 #include <QtGui/QPainter>
27 #include <QtCore/QTimer>
28 #include <QtCore/QEvent>
29 #include <QtCore/QPointer>
30 #include <QtCore/QCoreApplication>
31 
32 #include <kdebug.h>
33 
34 
35 class KPixmapSequenceOverlayPainter::Private
36 {
37 public:
38  void _k_timeout();
39  void paintFrame();
40 
41  KPixmapSequence& sequence();
42 
43  QRect pixmapRect();
44 
45  KPixmapSequence m_sequence;
46  QPointer<QWidget> m_widget;
47  Qt::Alignment m_alignment;
48  QPoint m_offset;
49  QRect m_rect;
50 
51  QTimer m_timer;
52  int m_counter;
53 
54  bool m_started;
55 
56  KPixmapSequenceOverlayPainter *q;
57 };
58 
59 
60 void KPixmapSequenceOverlayPainter::Private::_k_timeout()
61 {
62  if (sequence().isEmpty()) {
63  return;
64  }
65  ++m_counter;
66  m_counter %= sequence().frameCount();
67  if (m_widget)
68  m_widget->update(pixmapRect());
69 }
70 
71 
72 void KPixmapSequenceOverlayPainter::Private::paintFrame()
73 {
74  if (m_counter >= sequence().frameCount()) {
75  return;
76  }
77  QPainter p(m_widget);
78  p.drawPixmap(pixmapRect(), sequence().frameAt(m_counter), QRect(QPoint(0, 0), sequence().frameSize()));
79 }
80 
81 
82 KPixmapSequence& KPixmapSequenceOverlayPainter::Private::sequence()
83 {
84  // make sure we have a valid default sequence
85  if(m_sequence.isEmpty())
86  m_sequence = KPixmapSequence("process-working", 22);
87 
88  return m_sequence;
89 }
90 
91 
92 QRect KPixmapSequenceOverlayPainter::Private::pixmapRect()
93 {
94  QRect rect(m_rect);
95  if(!rect.isValid())
96  rect = m_widget->rect();
97 
98  QPoint pos(rect.topLeft());
99  if (m_alignment & Qt::AlignHCenter)
100  pos.setX(rect.center().x() - (sequence().frameSize().width() / 2));
101  else if (m_alignment & Qt::AlignRight)
102  pos.setX(rect.right() - sequence().frameSize().width());
103 
104  if (m_alignment & Qt::AlignVCenter)
105  pos.setY(rect.center().y() - (sequence().frameSize().height() / 2));
106  else if (m_alignment & Qt::AlignBottom)
107  pos.setY(rect.bottom() - sequence().frameSize().height());
108 
109  pos += m_offset;
110 
111  return QRect( pos, sequence().frameSize());
112 }
113 
114 
115 KPixmapSequenceOverlayPainter::KPixmapSequenceOverlayPainter(QObject *parent)
116  : QObject(parent),
117  d(new Private)
118 {
119  d->q = this;
120  d->m_widget = 0;
121  d->m_alignment = Qt::AlignCenter;
122  d->m_started = false;
123  setInterval(200);
124  connect(&d->m_timer, SIGNAL(timeout()), this, SLOT(_k_timeout()));
125 }
126 
127 
128 KPixmapSequenceOverlayPainter::~KPixmapSequenceOverlayPainter()
129 {
130  stop();
131  delete d;
132 }
133 
134 
135 KPixmapSequence KPixmapSequenceOverlayPainter::sequence() const
136 {
137  return d->sequence();
138 }
139 
140 
141 int KPixmapSequenceOverlayPainter::interval() const
142 {
143  return d->m_timer.interval();
144 }
145 
146 
147 QRect KPixmapSequenceOverlayPainter::rect() const
148 {
149  if(d->m_rect.isValid()) {
150  return d->m_rect;
151  }
152  else if(d->m_widget) {
153  return d->m_widget->rect();
154  }
155  else {
156  return QRect();
157  }
158 }
159 
160 
161 Qt::Alignment KPixmapSequenceOverlayPainter::alignment() const
162 {
163  return d->m_alignment;
164 }
165 
166 
167 QPoint KPixmapSequenceOverlayPainter::offset() const
168 {
169  return d->m_offset;
170 }
171 
172 
173 void KPixmapSequenceOverlayPainter::setSequence(const KPixmapSequence &seq)
174 {
175  bool restart = d->m_started;
176  stop();
177  d->m_sequence = seq;
178  if(restart) start();
179 }
180 
181 
182 void KPixmapSequenceOverlayPainter::setInterval(int msecs)
183 {
184  d->m_timer.setInterval(msecs);
185 }
186 
187 
188 void KPixmapSequenceOverlayPainter::setWidget(QWidget *w)
189 {
190  stop();
191  d->m_widget = w;
192 }
193 
194 
195 void KPixmapSequenceOverlayPainter::setRect(const QRect &rect)
196 {
197  bool restart = d->m_started;
198  stop();
199  d->m_rect = rect;
200  if(restart) start();
201 }
202 
203 
204 void KPixmapSequenceOverlayPainter::setAlignment(Qt::Alignment align)
205 {
206  bool restart = d->m_started;
207  stop();
208  d->m_alignment = align;
209  if(restart) start();
210 }
211 
212 
213 void KPixmapSequenceOverlayPainter::setOffset(const QPoint &offset)
214 {
215  bool restart = d->m_started;
216  stop();
217  d->m_offset = offset;
218  if(restart) start();
219 }
220 
221 
222 void KPixmapSequenceOverlayPainter::start()
223 {
224  if (d->m_widget) {
225  stop();
226 
227  d->m_counter = 0;
228  d->m_started = true;
229  d->m_widget->installEventFilter(this);
230  if(d->m_widget->isVisible()) {
231  d->m_timer.start();
232  d->m_widget->update(d->pixmapRect());
233  }
234  }
235 }
236 
237 
238 void KPixmapSequenceOverlayPainter::stop()
239 {
240  d->m_timer.stop();
241  if (d->m_widget) {
242  d->m_started = false;
243  d->m_widget->removeEventFilter(this);
244  d->m_widget->update(d->pixmapRect());
245  }
246 }
247 
248 
249 bool KPixmapSequenceOverlayPainter::eventFilter(QObject *obj, QEvent *event)
250 {
251  if (obj == d->m_widget ) {
252  switch (event->type()) {
253  case QEvent::Paint:
254  // make sure we paint after everyone else including other event filters
255  obj->removeEventFilter(this); // don't recurse...
256  QCoreApplication::sendEvent(obj, event);
257  d->paintFrame();
258  obj->installEventFilter(this); // catch on...
259  return true;
260  break;
261  case QEvent::Hide:
262  d->m_timer.stop();
263  break;
264  case QEvent::Show:
265  if(d->m_started) {
266  d->m_timer.start();
267  d->m_widget->update(d->pixmapRect());
268  }
269  break;
270  default:
271  break;
272  }
273  }
274 
275  return false;
276 }
277 
278 #include "kpixmapsequenceoverlaypainter.moc"
KPixmapSequenceOverlayPainter::interval
int interval() const
The interval between frames.
Definition: kpixmapsequenceoverlaypainter.cpp:141
QEvent
QWidget
KPixmapSequence
Loads and gives access to the frames of a typical multi-row pixmap as often used for spinners...
Definition: kpixmapsequence.h:45
QEvent::type
Type type() const
KPixmapSequenceOverlayPainter::rect
QRect rect() const
The optional rect to draw the pixmaps in.
Definition: kpixmapsequenceoverlaypainter.cpp:147
kdebug.h
KPixmapSequenceOverlayPainter::alignment
Qt::Alignment alignment() const
The alignment of the pixmaps in the rect.
Definition: kpixmapsequenceoverlaypainter.cpp:161
timeout
int timeout
kpixmapsequence.h
QPointer< QWidget >
kpixmapsequenceoverlaypainter.h
QPoint
Qt::Alignment
typedef Alignment
KPixmapSequenceOverlayPainter::setRect
void setRect(const QRect &rect)
Set the rect in which to place the sequence.
Definition: kpixmapsequenceoverlaypainter.cpp:195
KPixmapSequenceOverlayPainter::setWidget
void setWidget(QWidget *w)
Set the widget to draw the overlay on.
Definition: kpixmapsequenceoverlaypainter.cpp:188
KPixmapSequenceOverlayPainter::setInterval
void setInterval(int msecs)
Set the interval between frames.
Definition: kpixmapsequenceoverlaypainter.cpp:182
QRect
KPixmapSequenceOverlayPainter::offset
QPoint offset() const
The optional offset within the rect.
Definition: kpixmapsequenceoverlaypainter.cpp:167
QObject::installEventFilter
void installEventFilter(QObject *filterObj)
QTimer
QObject
QPainter
KPixmapSequenceOverlayPainter::setOffset
void setOffset(const QPoint &offset)
Set the offset relative to the placement determined by alignment and rect.
Definition: kpixmapsequenceoverlaypainter.cpp:213
QCoreApplication::sendEvent
bool sendEvent(QObject *receiver, QEvent *event)
KPixmapSequenceOverlayPainter::setAlignment
void setAlignment(Qt::Alignment align)
Set the alignment of the sequence in rect.
Definition: kpixmapsequenceoverlaypainter.cpp:204
KPixmapSequenceOverlayPainter::start
void start()
Start drawing the sequence.
Definition: kpixmapsequenceoverlaypainter.cpp:222
KPixmapSequenceOverlayPainter::stop
void stop()
Stop drawing the overlay.
Definition: kpixmapsequenceoverlaypainter.cpp:238
KPixmapSequenceOverlayPainter::setSequence
void setSequence(const KPixmapSequence &seq)
Set the sequence to be used.
Definition: kpixmapsequenceoverlaypainter.cpp:173
QPoint::setX
void setX(int x)
KPixmapSequenceOverlayPainter::~KPixmapSequenceOverlayPainter
~KPixmapSequenceOverlayPainter()
Destructor.
Definition: kpixmapsequenceoverlaypainter.cpp:128
KPixmapSequenceOverlayPainter::KPixmapSequenceOverlayPainter
KPixmapSequenceOverlayPainter(QObject *parent=0)
Constructor.
Definition: kpixmapsequenceoverlaypainter.cpp:115
KPixmapSequenceOverlayPainter
Paints a KPixmapSequence on top of any widget at any position.
Definition: kpixmapsequenceoverlaypainter.h:46
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
KPixmapSequenceOverlayPainter::sequence
KPixmapSequence sequence() const
The sequence used to draw the overlay.
Definition: kpixmapsequenceoverlaypainter.cpp:135
QObject::removeEventFilter
void removeEventFilter(QObject *obj)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:24:00 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

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