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

Plasma

  • sources
  • kde-4.12
  • kdelibs
  • plasma
  • animations
pixmaptransition.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2009 Mehmet Ali Akmanalp <makmanalp@wpi.edu>
3  * Copyright 2010 Marco Martin <notmart@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Library General Public License as
7  * published by the Free Software Foundation; either version 2, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 
21 #include "pixmaptransition_p.h"
22 
23 #include <QPainter>
24 #include <QPixmap>
25 
26 #include <kdebug.h>
27 
28 #include "paintutils.h"
29 
30 namespace Plasma
31 {
32 
33 PixmapTransition::PixmapTransition(QObject *parent)
34  : EasingAnimation(parent),
35  m_cache(false),
36  m_dirty(false)
37 {
38 }
39 
40 PixmapTransition::~PixmapTransition()
41 {
42 }
43 
44 void PixmapTransition::setStartPixmap(const QPixmap &pixmap)
45 {
46  if (state() == Running) {
47  stop();
48  }
49 
50  m_startPixmap = pixmap;
51 
52  //this will center the pixmaps if needed
53  updateEffectiveTime(0);
54 }
55 
56 QPixmap PixmapTransition::startPixmap() const
57 {
58  return m_startPixmap;
59 }
60 
61 void PixmapTransition::setTargetPixmap(const QPixmap &pixmap)
62 {
63  if (state() == Running) {
64  stop();
65  }
66 
67  m_targetPixmap = pixmap;
68 
69  updateEffectiveTime(0);
70 }
71 
72 void PixmapTransition::setUsesCache(bool cache)
73 {
74  m_cache = cache;
75 }
76 
77 bool PixmapTransition::usesCache() const
78 {
79  return m_cache;
80 }
81 
82 QPixmap PixmapTransition::targetPixmap() const
83 {
84  return m_targetPixmap;
85 }
86 
87 QPixmap PixmapTransition::currentPixmap() const
88 {
89  if (m_cache && !m_dirty) {
90  return m_currentPixmap;
91  }
92 
93  QPixmap currentPixmap;
94  qreal delta = currentTime() / qreal(duration());
95  if (!m_startPixmap.isNull() && !m_targetPixmap.isNull()) {
96  //kDebug() << "transitioning";
97  currentPixmap = Plasma::PaintUtils::transition(m_startPixmap, m_targetPixmap, delta);
98  } else if (m_startPixmap.isNull()) {
99  if (qFuzzyCompare(delta, qreal(1.0))) {
100  currentPixmap = alignedTargetPixmap();
101  return currentPixmap;
102  }
103 
104  if (currentPixmap.isNull()) {
105  currentPixmap = QPixmap(m_pixmapSize);
106  }
107 
108  currentPixmap.fill(QColor(0, 0, 0, (int)(((qreal)255)*delta)));
109  QPainter p(&currentPixmap);
110  p.setCompositionMode(QPainter::CompositionMode_SourceIn);
111  //kDebug() << "painting" << m_targetPixmap.rect() << "into" << m_targetRect << "in size" << currentPixmap.size();
112  p.drawPixmap(m_targetRect, m_targetPixmap);
113  p.end();
114  } else if (m_targetPixmap.isNull()) {
115  currentPixmap = alignedStartPixmap();
116  if (qFuzzyCompare(delta, qreal(1.0))) {
117  return m_currentPixmap;
118  }
119  //kDebug() << "painting" << m_startPixmap.rect() << "into" << m_targetRect << "in size" << currentPixmap.size();
120  QPainter p(&currentPixmap);
121  p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
122  p.fillRect(currentPixmap.rect(), QColor(0, 0, 0, (int)(254 - ((qreal)254)*delta)));
123  p.end();
124  }
125 
126  if (m_cache) {
127  const_cast<PixmapTransition *>(this)->m_currentPixmap = currentPixmap;
128  }
129 
130  return currentPixmap;
131 }
132 
133 QPixmap PixmapTransition::alignedTargetPixmap() const
134 {
135  QPixmap pm(m_pixmapSize);
136  pm.fill(Qt::transparent);
137  QPainter p(&pm);
138  p.drawPixmap(m_targetRect, m_targetPixmap);
139  return pm;
140 }
141 
142 QPixmap PixmapTransition::alignedStartPixmap() const
143 {
144  QPixmap pm(m_pixmapSize);
145  pm.fill(Qt::transparent);
146  QPainter p(&pm);
147  p.drawPixmap(m_startRect, m_startPixmap);
148  return pm;
149 }
150 
151 void PixmapTransition::updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState)
152 {
153  if (oldState == Stopped && newState == Running) {
154  m_targetRect = m_targetPixmap.rect();
155  m_startRect = m_startPixmap.rect();
156  m_pixmapSize = m_startRect.size().expandedTo(m_targetRect.size());
157  QRect actualRect = QRect(QPoint(0,0), m_pixmapSize);
158  m_targetRect.moveCenter(actualRect.center());
159  m_startRect.moveCenter(actualRect.center());
160  } else if (QGraphicsWidget *w = targetWidget()) {
161  w->update();
162  }
163 
164  m_dirty = true;
165 }
166 
167 void PixmapTransition::updateEffectiveTime(int currentTime)
168 {
169  Q_UNUSED(currentTime)
170 
171  m_dirty = true;
172  QGraphicsWidget *w = targetWidget();
173  if (w) {
174  w->update();
175  }
176 }
177 
178 } //namespace Plasma
179 
180 #include "pixmaptransition_p.moc"
QObject
paintutils.h
Plasma::PaintUtils::transition
QPixmap transition(const QPixmap &from, const QPixmap &to, qreal amount)
Blends a pixmap into another.
Definition: paintutils.cpp:208
QGraphicsWidget
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:33 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • 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
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • 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