author | simon <simon> | 2002-12-14 18:18:20 (UTC) |
---|---|---|
committer | simon <simon> | 2002-12-14 18:18:20 (UTC) |
commit | 7c854ad1b909f37c7314ef4ac2061500d02af16d (patch) (side-by-side diff) | |
tree | 24528d72c0f2d36d6777090d7d93e2536ffb120d | |
parent | ae7a270c56fa0fff6d3f530c80532c54c51be596 (diff) | |
download | opie-7c854ad1b909f37c7314ef4ac2061500d02af16d.zip opie-7c854ad1b909f37c7314ef4ac2061500d02af16d.tar.gz opie-7c854ad1b909f37c7314ef4ac2061500d02af16d.tar.bz2 |
- reduce memory usage a little bit by not storing the images after they
got converted to pixmaps
-rw-r--r-- | noncore/multimedia/opieplayer2/mediawidget.cpp | 1 | ||||
-rw-r--r-- | noncore/multimedia/opieplayer2/skin.cpp | 6 | ||||
-rw-r--r-- | noncore/multimedia/opieplayer2/skin.h | 4 |
3 files changed, 10 insertions, 1 deletions
diff --git a/noncore/multimedia/opieplayer2/mediawidget.cpp b/noncore/multimedia/opieplayer2/mediawidget.cpp index edef2a7..46e7b6e 100644 --- a/noncore/multimedia/opieplayer2/mediawidget.cpp +++ b/noncore/multimedia/opieplayer2/mediawidget.cpp @@ -1,260 +1,261 @@ /* Copyright (C) 2002 Simon Hausmann <simon@lst.de> (C) 2002 Max Reiss <harlekin@handhelds.org> (C) 2002 L. Potter <ljp@llornkcor.com> (C) 2002 Holger Freyther <zecke@handhelds.org> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "mediawidget.h" #include "playlistwidget.h" #include "skin.h" MediaWidget::MediaWidget( PlayListWidget &_playList, MediaPlayerState &_mediaPlayerState, QWidget *parent, const char *name ) : QWidget( parent, name ), mediaPlayerState( _mediaPlayerState ), playList( _playList ) { connect( &mediaPlayerState, SIGNAL( displayTypeChanged( MediaPlayerState::DisplayType ) ), this, SLOT( setDisplayType( MediaPlayerState::DisplayType ) ) ); connect( &mediaPlayerState, SIGNAL( lengthChanged( long ) ), this, SLOT( setLength( long ) ) ); connect( &mediaPlayerState, SIGNAL( playingToggled( bool ) ), this, SLOT( setPlaying( bool ) ) ); setBackgroundMode( NoBackground ); } MediaWidget::~MediaWidget() { } void MediaWidget::setupButtons( const SkinButtonInfo *skinInfo, uint buttonCount, const Skin &skin ) { buttonMask = skin.buttonMask( skinInfo, buttonCount ); buttons.clear(); buttons.reserve( buttonCount ); for ( uint i = 0; i < buttonCount; ++i ) { Button button = setupButton( skinInfo[ i ], skin ); buttons.push_back( button ); } } MediaWidget::Button MediaWidget::setupButton( const SkinButtonInfo &buttonInfo, const Skin &skin ) { Button button; button.command = buttonInfo.command; button.type = buttonInfo.type; button.mask = skin.buttonMaskImage( buttonInfo.fileName ); return button; } void MediaWidget::loadDefaultSkin( const GUIInfo &guiInfo ) { Skin skin( guiInfo.fileNameInfix ); + skin.setCachable( false ); loadSkin( guiInfo.buttonInfo, guiInfo.buttonCount, skin ); } void MediaWidget::loadSkin( const SkinButtonInfo *skinInfo, uint buttonCount, const Skin &skin ) { backgroundPixmap = skin.backgroundPixmap(); buttonUpImage = skin.buttonUpImage(); buttonDownImage = skin.buttonDownImage(); setupButtons( skinInfo, buttonCount, skin ); } void MediaWidget::closeEvent( QCloseEvent * ) { mediaPlayerState.setList(); } void MediaWidget::paintEvent( QPaintEvent *pe ) { QPainter p( this ); if ( mediaPlayerState.isFullscreen() ) { // Clear the background p.setBrush( QBrush( Qt::black ) ); return; } QPixmap buffer( size() ); QPainter bufferedPainter( &buffer ); bufferedPainter.drawTiledPixmap( rect(), backgroundPixmap, QPoint( 0, 0 ) ); paintAllButtons( bufferedPainter ); p.drawPixmap( 0, 0, buffer ); } void MediaWidget::resizeEvent( QResizeEvent *e ) { QPixmap pixUp = combineImageWithBackground( buttonUpImage, backgroundPixmap, upperLeftOfButtonMask ); QPixmap pixDn = combineImageWithBackground( buttonDownImage, backgroundPixmap, upperLeftOfButtonMask ); for ( ButtonVector::iterator it = buttons.begin(); it != buttons.end(); ++it ) { Button &button = *it; if ( button.mask.isNull() ) continue; button.pixUp = addMaskToPixmap( pixUp, button.mask ); button.pixDown = addMaskToPixmap( pixDn, button.mask ); } QWidget::resizeEvent( e ); } MediaWidget::Button *MediaWidget::buttonAt( const QPoint &position ) { if ( position.x() <= 0 || position.y() <= 0 || position.x() >= buttonMask.width() || position.y() >= buttonMask.height() ) return 0; int pixelIdx = buttonMask.pixelIndex( position.x(), position.y() ); for ( ButtonVector::iterator it = buttons.begin(); it != buttons.end(); ++it ) if ( it->command + 1 == pixelIdx ) return &( *it ); return 0; } void MediaWidget::mousePressEvent( QMouseEvent *event ) { Button *button = buttonAt( event->pos() - upperLeftOfButtonMask ); if ( !button ) { QWidget::mousePressEvent( event ); return; } switch ( button->command ) { case VolumeUp: emit moreClicked(); return; case VolumeDown: emit lessClicked(); return; case Back: emit backClicked(); return; case Forward: emit forwardClicked(); return; default: break; } } void MediaWidget::mouseReleaseEvent( QMouseEvent *event ) { Button *button = buttonAt( event->pos() - upperLeftOfButtonMask ); if ( !button ) { QWidget::mouseReleaseEvent( event ); return; } if ( button->type == ToggleButton ) toggleButton( *button ); handleCommand( button->command, button->isDown ); } void MediaWidget::makeVisible() { } void MediaWidget::handleCommand( Command command, bool buttonDown ) { switch ( command ) { case Play: mediaPlayerState.togglePaused(); return; case Stop: mediaPlayerState.setPlaying(FALSE); return; case Next: if( playList.currentTab() == PlayListWidget::CurrentPlayList ) mediaPlayerState.setNext(); return; case Previous: if( playList.currentTab() == PlayListWidget::CurrentPlayList ) mediaPlayerState.setPrev(); return; case Loop: mediaPlayerState.setLooping( buttonDown ); return; case VolumeUp: emit moreReleased(); return; case VolumeDown: emit lessReleased(); return; case PlayList: mediaPlayerState.setList(); return; case Forward: emit forwardReleased(); return; case Back: emit backReleased(); return; case FullScreen: mediaPlayerState.setFullscreen( true ); makeVisible(); return; default: assert( false ); } } bool MediaWidget::isOverButton( const QPoint &position, int buttonId ) const { return ( position.x() > 0 && position.y() > 0 && position.x() < buttonMask.width() && position.y() < buttonMask.height() && buttonMask.pixelIndex( position.x(), position.y() ) == buttonId + 1 ); } void MediaWidget::paintAllButtons( QPainter &p ) { for ( ButtonVector::const_iterator it = buttons.begin(); it != buttons.end(); ++it ) paintButton( p, *it ); } void MediaWidget::paintButton( const Button &button ) { QPainter p( this ); paintButton( p, button ); } void MediaWidget::paintButton( QPainter &p, const Button &button ) { if ( button.isDown ) p.drawPixmap( upperLeftOfButtonMask, button.pixDown ); else p.drawPixmap( upperLeftOfButtonMask, button.pixUp ); } void MediaWidget::setToggleButton( Command command, bool down ) { for ( ButtonVector::iterator it = buttons.begin(); it != buttons.end(); ++it ) if ( it->command == command ) { setToggleButton( *it, down ); return; } } void MediaWidget::setToggleButton( Button &button, bool down ) { if ( down != button.isDown ) toggleButton( button ); } void MediaWidget::toggleButton( Button &button ) { button.isDown = !button.isDown; paintButton( button ); } QPixmap MediaWidget::combineImageWithBackground( const QImage &image, const QPixmap &background, const QPoint &offset ) { QPixmap pix( image.size() ); QPainter p( &pix ); p.drawTiledPixmap( pix.rect(), background, offset ); p.drawImage( 0, 0, image ); return pix; } QPixmap MediaWidget::addMaskToPixmap( const QPixmap &pix, const QBitmap &mask ) { QPixmap result( pix ); result.setMask( mask ); return result; } /* vim: et sw=4 ts=4 */ diff --git a/noncore/multimedia/opieplayer2/skin.cpp b/noncore/multimedia/opieplayer2/skin.cpp index a8f4ae9..44f5ca2 100644 --- a/noncore/multimedia/opieplayer2/skin.cpp +++ b/noncore/multimedia/opieplayer2/skin.cpp @@ -1,320 +1,324 @@ /* Copyright (C) 2002 Simon Hausmann <simon@lst.de> (C) 2002 Max Reiss <harlekin@handhelds.org> (C) 2002 L. Potter <ljp@llornkcor.com> (C) 2002 Holger Freyther <zecke@handhelds.org> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "skin.h" #include "singleton.h" #include <qcache.h> #include <qmap.h> #include <qtimer.h> #include <qpe/resource.h> #include <qpe/config.h> #include <assert.h> struct SkinData { typedef QMap<QString, QImage> ButtonMaskImageMap; QPixmap backgroundPixmap; QImage buttonUpImage; QImage buttonDownImage; QImage buttonMask; ButtonMaskImageMap buttonMasks; }; class SkinCache : public Singleton<SkinCache> { public: SkinCache(); SkinData *lookupAndTake( const QString &skinPath, const QString &fileNameInfix ); void store( const QString &skinPath, const QString &fileNameInfix, SkinData *data ); private: typedef QCache<SkinData> DataCache; typedef QCache<QPixmap> BackgroundPixmapCache; template <class CacheType> void store( const QCache<CacheType> &cache, const QString &key, CacheType *data ); DataCache m_cache; BackgroundPixmapCache m_backgroundPixmapCache; }; Skin::Skin( const QString &name, const QString &fileNameInfix ) : m_fileNameInfix( fileNameInfix ) { init( name ); } Skin::Skin( const QString &fileNameInfix ) : m_fileNameInfix( fileNameInfix ) { init( defaultSkinName() ); } Skin::~Skin() { - SkinCache::self().store( m_skinPath, m_fileNameInfix, d ); + if ( m_isCachable ) + SkinCache::self().store( m_skinPath, m_fileNameInfix, d ); + else + delete d; } void Skin::init( const QString &name ) { + m_isCachable = true; m_skinPath = "opieplayer2/skins/" + name; d = SkinCache::self().lookupAndTake( m_skinPath, m_fileNameInfix ); } QPixmap Skin::backgroundPixmap() const { if ( d->backgroundPixmap.isNull() ) d->backgroundPixmap = loadImage( QString( "%1/background" ).arg( m_skinPath ) ); return d->backgroundPixmap; } QImage Skin::buttonUpImage() const { if ( d->buttonUpImage.isNull() ) d->buttonUpImage = loadImage( QString( "%1/skin%2_up" ).arg( m_skinPath ).arg( m_fileNameInfix ) ); return d->buttonUpImage; } QImage Skin::buttonDownImage() const { if ( d->buttonDownImage.isNull() ) d->buttonDownImage = loadImage( QString( "%1/skin%2_down" ).arg( m_skinPath ).arg( m_fileNameInfix ) ); return d->buttonDownImage; } QImage Skin::buttonMask( const MediaWidget::SkinButtonInfo *skinButtonInfo, uint buttonCount ) const { if ( !d->buttonMask.isNull() ) return d->buttonMask; QSize buttonAreaSize = buttonUpImage().size(); d->buttonMask = QImage( buttonAreaSize, 8, 255 ); d->buttonMask.fill( 0 ); for ( uint i = 0; i < buttonCount; ++i ) addButtonToMask( skinButtonInfo[ i ].command + 1, buttonMaskImage( skinButtonInfo[ i ].fileName ) ); return d->buttonMask; } void Skin::addButtonToMask( int tag, const QImage &maskImage ) const { if ( maskImage.isNull() ) return; uchar **dest = d->buttonMask.jumpTable(); for ( int y = 0; y < d->buttonMask.height(); y++ ) { uchar *line = dest[y]; for ( int x = 0; x < d->buttonMask.width(); x++ ) if ( !qRed( maskImage.pixel( x, y ) ) ) line[x] = tag; } } QImage Skin::buttonMaskImage( const QString &fileName ) const { SkinData::ButtonMaskImageMap::Iterator it = d->buttonMasks.find( fileName ); if ( it == d->buttonMasks.end() ) { QString prefix = m_skinPath + QString::fromLatin1( "/skin%1_mask_" ).arg( m_fileNameInfix ); QString path = prefix + fileName + ".png"; it = d->buttonMasks.insert( fileName, loadImage( path ) ); } return *it; } QString Skin::defaultSkinName() { Config cfg( "OpiePlayer" ); cfg.setGroup( "Options" ); return cfg.readEntry( "Skin", "default" ); } QImage Skin::loadImage( const QString &fileName ) { return QImage( Resource::findPixmap( fileName ) ); } SkinCache::SkinCache() { // let's say we cache two skins (audio+video) at maximum m_cache.setMaxCost( 2 ); // ... and one background pixmap m_backgroundPixmapCache.setMaxCost( 1 ); } SkinData *SkinCache::lookupAndTake( const QString &skinPath, const QString &fileNameInfix ) { QString key = skinPath + fileNameInfix; SkinData *data = m_cache.take( key ); if ( !data ) data = new SkinData; else qDebug( "SkinCache: hit" ); QPixmap *bgPixmap = m_backgroundPixmapCache.find( skinPath ); if ( bgPixmap ) { qDebug( "SkinCache: hit on bgpixmap" ); data->backgroundPixmap = *bgPixmap; } else data->backgroundPixmap = QPixmap(); return data; } void SkinCache::store( const QString &skinPath, const QString &fileNameInfix, SkinData *data ) { QPixmap *backgroundPixmap = new QPixmap( data->backgroundPixmap ); data->backgroundPixmap = QPixmap(); QString key = skinPath + fileNameInfix; if ( m_cache.find( key, false /*ref*/ ) != 0 || !m_cache.insert( key, data ) ) delete data; if ( m_backgroundPixmapCache.find( skinPath, false /*ref*/ ) != 0 || !m_backgroundPixmapCache.insert( skinPath, backgroundPixmap ) ) delete backgroundPixmap; } SkinLoader::IncrementalLoader::IncrementalLoader( const Info &info ) : m_skin( info.skinName, info.fileNameInfix ), m_info( info ) { m_currentState = LoadBackgroundPixmap; } SkinLoader::IncrementalLoader::LoaderResult SkinLoader::IncrementalLoader::loadStep() { switch ( m_currentState ) { case LoadBackgroundPixmap: qDebug( "load bgpixmap" ); m_skin.backgroundPixmap(); m_currentState = LoadButtonUpImage; break; case LoadButtonUpImage: qDebug( "load upimage" ); m_skin.buttonUpImage(); m_currentState = LoadButtonDownImage; break; case LoadButtonDownImage: qDebug( "load downimage" ); m_skin.buttonDownImage(); m_currentState = LoadButtonMasks; m_currentButton = 0; break; case LoadButtonMasks: qDebug( "load button masks %i", m_currentButton ); m_skin.buttonMaskImage( m_info.buttonInfo[ m_currentButton ].fileName ); m_currentButton++; if ( m_currentButton >= m_info.buttonCount ) m_currentState = LoadButtonMask; break; case LoadButtonMask: qDebug( "load whole mask" ); m_skin.buttonMask( m_info.buttonInfo, m_info.buttonCount ); return LoadingCompleted; } return MoreToCome; } SkinLoader::SkinLoader() : m_currentLoader( 0 ), m_timerId( -1 ) { } SkinLoader::~SkinLoader() { qDebug( "SkinLoader::~SkinLoader()" ); killTimers(); delete m_currentLoader; } void SkinLoader::schedule( const MediaWidget::GUIInfo &guiInfo ) { schedule( Skin::defaultSkinName(), guiInfo ); } void SkinLoader::schedule( const QString &skinName, const MediaWidget::GUIInfo &guiInfo ) { pendingSkins << Info( skinName, guiInfo ); } void SkinLoader::start() { assert( m_timerId == -1 ); m_timerId = startTimer( 100 /* ms */ ); qDebug( "SkinLoader::start() %d jobs", pendingSkins.count() ); } void SkinLoader::timerEvent( QTimerEvent *ev ) { if ( ev->timerId() != m_timerId ) { QObject::timerEvent( ev ); return; } if ( !m_currentLoader ) { if ( pendingSkins.isEmpty() ) { qDebug( "all jobs done" ); killTimer( m_timerId ); m_timerId = -1; // ### qt3: use deleteLater(); QTimer::singleShot( 0, this, SLOT( deleteMe() ) ); return; } Info nfo = *pendingSkins.begin(); pendingSkins.remove( pendingSkins.begin() ); m_currentLoader = new IncrementalLoader( nfo ); qDebug( "new loader %i jobs left", pendingSkins.count() ); } if ( m_currentLoader->loadStep() == IncrementalLoader::LoadingCompleted ) { delete m_currentLoader; m_currentLoader = 0; } qDebug( "finished step" ); } void SkinLoader::deleteMe() { delete this; } /* vim: et sw=4 ts=4 */ diff --git a/noncore/multimedia/opieplayer2/skin.h b/noncore/multimedia/opieplayer2/skin.h index bafebd3..067b6c4 100644 --- a/noncore/multimedia/opieplayer2/skin.h +++ b/noncore/multimedia/opieplayer2/skin.h @@ -1,122 +1,126 @@ /* Copyright (C) 2002 Simon Hausmann <simon@lst.de> (C) 2002 Max Reiss <harlekin@handhelds.org> (C) 2002 L. Potter <ljp@llornkcor.com> (C) 2002 Holger Freyther <zecke@handhelds.org> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef SKIN_H #define SKIN_H #include <qstring.h> #include <qimage.h> #include <qobject.h> #include "mediawidget.h" struct SkinData; class Skin { public: Skin( const QString &name, const QString &fileNameInfix ); Skin( const QString &fileNameInfix ); ~Skin(); + bool isCachable() const { return m_isCachable; } + void setCachable( bool cachable ) { m_isCachable = cachable; } + QPixmap backgroundPixmap() const; QImage buttonUpImage() const; QImage buttonDownImage() const; QImage buttonMask( const MediaWidget::SkinButtonInfo *skinButtonInfo, uint buttonCount ) const; QImage buttonMaskImage( const QString &fileName ) const; static QString defaultSkinName(); private: void init( const QString &name ); void addButtonToMask( int tag, const QImage &maskImage ) const; static QImage loadImage( const QString &fileName ); QString m_fileNameInfix; QString m_skinPath; + bool m_isCachable : 1; SkinData *d; Skin( const Skin & ); Skin &operator=( const Skin & ); }; class SkinLoader : public QObject { Q_OBJECT public: SkinLoader(); virtual ~SkinLoader(); void schedule( const MediaWidget::GUIInfo &guiInfo ); void schedule( const QString &skinName, const MediaWidget::GUIInfo &guiInfo ); void start(); protected: virtual void timerEvent( QTimerEvent *ev ); private slots: void deleteMe(); private: struct Info : public MediaWidget::GUIInfo { Info() {} Info( const QString &_skinName, const MediaWidget::GUIInfo &guiInfo ) : MediaWidget::GUIInfo( guiInfo ), skinName( _skinName ) {} QString skinName; }; typedef QValueList<Info> InfoList; class IncrementalLoader { public: enum LoaderResult { LoadingCompleted, MoreToCome }; IncrementalLoader( const Info &info ); LoaderResult loadStep(); private: enum State { LoadBackgroundPixmap, LoadButtonUpImage, LoadButtonDownImage, LoadButtonMasks, LoadButtonMask }; Skin m_skin; Info m_info; State m_currentState; uint m_currentButton; }; InfoList pendingSkins; IncrementalLoader *m_currentLoader; int m_timerId; }; #endif // SKIN_H /* vim: et sw=4 ts=4 */ |