![]() |
Home · All Namespaces · All Classes |
00001 /**************************************************************************** 00002 ** 00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). 00004 ** All rights reserved. 00005 ** Contact: Nokia Corporation (directui@nokia.com) 00006 ** 00007 ** This file is part of systemui. 00008 ** 00009 ** If you have questions regarding the use of this file, please contact 00010 ** Nokia at directui@nokia.com. 00011 ** 00012 ** This library is free software; you can redistribute it and/or 00013 ** modify it under the terms of the GNU Lesser General Public 00014 ** License version 2.1 as published by the Free Software Foundation 00015 ** and appearing in the file LICENSE.LGPL included in the packaging 00016 ** of this file. 00017 ** 00018 ****************************************************************************/ 00019 #include "volumebar.h" 00020 00021 #include <QTimer> 00022 #include <QDebug> 00023 #include <QRegion> 00024 #include <QPointF> 00025 #include <QSizeF> 00026 #include <QGraphicsSceneMouseEvent> 00027 #include <QPropertyAnimation> 00028 00029 #include <MSceneWindow> 00030 #include <MImageWidget> 00031 #include <MSceneManager> 00032 #include <MLayout> 00033 #include <MLinearLayoutPolicy> 00034 00035 VolumeBar::VolumeBar (QGraphicsItem *parent) : 00036 MStylableWidget (parent), 00037 slider (0), 00038 icon (0), 00039 value (0), 00040 valueMax (0), 00041 animMove (0), 00042 animFadeIn (0), 00043 animFadeOut (0) 00044 { 00045 constructUi (); 00046 setOpacity (0.0); 00047 } 00048 00049 VolumeBar::~VolumeBar () 00050 { 00051 } 00052 00053 void VolumeBar::applyStyle () 00054 { 00055 MStylableWidget::applyStyle (); 00056 00057 int timeout = style ()->visibleTimeout (); 00058 int animDuration = style ()->fadeDuration (); 00059 qreal opacity = style ()->volumebarOpacity (); 00060 00061 if (animDuration * 2 > timeout) 00062 { // handle the error: 00063 qWarning () << "ERROR in VolumeBars CSS: animDuration * 2 > timeout!"; 00064 animDuration = timeout / 2; 00065 } 00066 00067 // first fade out animation started and after its finished we emit animationsFinished () 00068 timer.setInterval (timeout - animDuration); 00069 00070 animFadeIn->setDuration (animDuration); 00071 animFadeOut->setDuration (animDuration); 00072 00073 animFadeIn->setEndValue (opacity); 00074 } 00075 00076 void VolumeBar::constructUi () 00077 { 00078 slider = new MStylableWidget (this); 00079 slider->setObjectName ("FSVolumeBar"); 00080 00081 icon = new MImageWidget (this); 00082 00083 MLayout *layout = new MLayout; 00084 MLinearLayoutPolicy *landscapePolicy = new MLinearLayoutPolicy (layout, Qt::Horizontal); 00085 MLinearLayoutPolicy *portraitPolicy = new MLinearLayoutPolicy (layout, Qt::Vertical); 00086 00087 landscapePolicy->addStretch (2); 00088 landscapePolicy->addItem (icon); 00089 landscapePolicy->addStretch (1); 00090 00091 portraitPolicy->addStretch (2); 00092 portraitPolicy->addItem (icon); 00093 portraitPolicy->addStretch (1); 00094 00095 layout->setLandscapePolicy (landscapePolicy); 00096 layout->setPortraitPolicy (portraitPolicy); 00097 00098 // prepare the fade in & out animations... 00099 animFadeIn = new QPropertyAnimation (this, "opacity"); 00100 animFadeIn->setStartValue (0.0); 00101 00102 animFadeOut = new QPropertyAnimation (this, "opacity"); 00103 animFadeOut->setEndValue (0.0); 00104 00105 connect (&timer, SIGNAL(timeout()), 00106 animFadeOut, SLOT(start())); 00107 connect (animFadeOut, SIGNAL(finished()), SLOT(finishAnimations())); 00108 00109 // .. and prepare the movement animation also 00110 animMove = new QPropertyAnimation (slider, "geometry"); 00111 00112 setLayout (layout); 00113 } 00114 00115 void VolumeBar::mouseMoveEvent (QGraphicsSceneMouseEvent *event) 00116 { 00117 MStylableWidget::mouseMoveEvent (event); 00118 // just pass the event to mousePressEvent handler 00119 mousePressEvent (event); 00120 } 00121 00122 void VolumeBar::mousePressEvent (QGraphicsSceneMouseEvent *event) 00123 { 00124 MStylableWidget::mousePressEvent (event); 00125 qreal percentage = 0.0; 00126 /* 00127 * increase & decrease the volume based on the tap/movement 00128 * coordinates... 00129 */ 00130 if (sceneManager()->orientation() == M::Portrait) 00131 percentage = (geometry().height() - event->pos().y()) / geometry().height(); 00132 else 00133 percentage = (geometry().width() - event->pos().x()) / geometry().width(); 00134 00135 value = valueMax * percentage; 00136 00137 if (value >= valueMax) 00138 value = valueMax - 1; 00139 00140 /* emit volumeChanged signal... */ 00141 emit volumeChanged (value); 00142 /* and the update the UI */ 00143 updateVolume (value, valueMax); 00144 } 00145 00146 void VolumeBar::updateContents () 00147 { 00148 MSceneManager *sm = sceneManager (); 00149 QSize screen = sm->visibleSceneSize (sm->orientation ()); 00150 00151 QSizeF newSize; 00152 QRectF barGeom; 00153 00154 QString iconID = (value == 0) ? 00155 style ()->iconVolumeOff () : 00156 style ()->iconVolume (); 00157 if (icon->imageId () != iconID) 00158 icon->setImage (iconID, style ()->iconVolumeSize ()); 00159 00160 /* 00161 * update the bar geometry based on current screen size, 00162 * and on actual volume-level 00163 */ 00164 if (sm->orientation () == M::Portrait) { 00165 newSize.setHeight (screen.height () * value / (valueMax - 1)); 00166 newSize.setWidth (screen.width ()); 00167 00168 barGeom.setX (0.0); 00169 barGeom.setY (screen.height () - newSize.height ()); 00170 } else { 00171 newSize.setHeight (screen.height ()); 00172 newSize.setWidth (screen.width () * value / (valueMax - 1)); 00173 00174 barGeom.setX (screen.width () - newSize.width ()); 00175 barGeom.setY (0.0); 00176 } 00177 barGeom.setSize (newSize); 00178 00179 if (isOnDisplay () == false) 00180 { 00181 // do not animate unnecessarily when we're not on display 00182 slider->setGeometry (barGeom); 00183 } 00184 else 00185 { 00186 animMove->setStartValue (slider->geometry ()); 00187 animMove->setEndValue (barGeom); 00188 00189 animMove->start (); 00190 } 00191 } 00192 00193 void VolumeBar::updateVolume (int val, int max) 00194 { 00195 timer.stop (); 00196 00197 value = val; 00198 valueMax = max; 00199 00200 updateContents (); 00201 00202 if (animFadeOut->state () != QAbstractAnimation::Stopped) { 00203 // stop the fade-out animation in case if it is not stopped 00204 animFadeOut->stop (); 00205 // start the fade-in animation to get the wanted opacity back 00206 animFadeIn->start (); 00207 } else if (animFadeIn->state () != QAbstractAnimation::Running && (opacity () <= 0.1)) { 00208 // start the fade-in animation (only if needed ^^^) 00209 animFadeIn->start (); 00210 } 00211 00212 timer.start (); 00213 } 00214 00215 void VolumeBar::finishAnimations () 00216 { 00217 emit animationsFinished (); 00218 00219 timer.stop (); 00220 animFadeIn->stop (); 00221 animFadeOut->stop (); 00222 00223 setOpacity (0.0); 00224 } 00225
| Copyright © 2010 Nokia Corporation | Generated on Wed Nov 10 16:04:52 2010 Doxygen 1.6.1 |
MeeGo Touch |
