diff options
author | RJ Ryan <rryan@mixxx.org> | 2013-12-24 02:42:05 -0500 |
---|---|---|
committer | RJ Ryan <rryan@mixxx.org> | 2013-12-24 02:42:05 -0500 |
commit | 31910722ffab602bfed98333cd31a10fc0163475 (patch) | |
tree | 2294bfb3a12796056a546a2472726652d222a9f3 | |
parent | 8e069765f06b9d8fdc9a05d9b2c87fbbce8bdf3d (diff) |
Get rid of manual reference counting in WPixmapStore and just use QSharedPointer.
-rw-r--r-- | src/widget/wdisplay.cpp | 35 | ||||
-rw-r--r-- | src/widget/wdisplay.h | 11 | ||||
-rw-r--r-- | src/widget/wpixmapstore.cpp | 85 | ||||
-rw-r--r-- | src/widget/wpixmapstore.h | 23 | ||||
-rw-r--r-- | src/widget/wpushbutton.cpp | 19 | ||||
-rw-r--r-- | src/widget/wpushbutton.h | 15 | ||||
-rw-r--r-- | src/widget/wslidercomposed.cpp | 10 | ||||
-rw-r--r-- | src/widget/wslidercomposed.h | 5 | ||||
-rw-r--r-- | src/widget/wstatuslight.cpp | 29 | ||||
-rw-r--r-- | src/widget/wstatuslight.h | 18 | ||||
-rw-r--r-- | src/widget/wvumeter.cpp | 9 | ||||
-rw-r--r-- | src/widget/wvumeter.h | 22 | ||||
-rw-r--r-- | src/widget/wwidgetgroup.cpp | 1 | ||||
-rw-r--r-- | src/widget/wwidgetgroup.h | 4 |
14 files changed, 98 insertions, 188 deletions
diff --git a/src/widget/wdisplay.cpp b/src/widget/wdisplay.cpp index 471df62ea2..ec8906fa44 100644 --- a/src/widget/wdisplay.cpp +++ b/src/widget/wdisplay.cpp @@ -76,49 +76,28 @@ void WDisplay::setPositions(int iNoPos) { } void WDisplay::resetPositions() { - for (int i = 0; i < m_pixmaps.size(); ++i) { - QPixmap* pPixmap = m_pixmaps[i]; - if (pPixmap) { - WPixmapStore::deletePixmap(pPixmap); - } - } - for (int i = 0; i < m_disabledPixmaps.size(); ++i) { - QPixmap* pPixmap = m_disabledPixmaps[i]; - if (pPixmap) { - WPixmapStore::deletePixmap(pPixmap); - } - } - if (m_pPixmapBack) { - WPixmapStore::deletePixmap(m_pPixmapBack); - m_pPixmapBack = NULL; - } + m_pPixmapBack.clear(); m_pixmaps.resize(0); m_disabledPixmaps.resize(0); } void WDisplay::setPixmapBackground(const QString& filename) { - // Free the pixmap if it is already allocated. - if (m_pPixmapBack) { - WPixmapStore::deletePixmap(m_pPixmapBack); - m_pPixmapBack = NULL; - } - m_pPixmapBack = WPixmapStore::getPixmap(filename); - if (m_pPixmapBack == NULL || m_pPixmapBack->isNull()) { + if (m_pPixmapBack.isNull() || m_pPixmapBack->isNull()) { qDebug() << metaObject()->className() << "Error loading background pixmap:" << filename; } } -void WDisplay::setPixmap(QVector<QPixmap*>* pPixmaps, int iPos, +void WDisplay::setPixmap(QVector<QPixmapPointer>* pPixmaps, int iPos, const QString& filename) { if (iPos < 0 || iPos >= pPixmaps->size()) { return; } - QPixmap* pPixmap = WPixmapStore::getPixmap(filename); + QPixmapPointer pPixmap = WPixmapStore::getPixmap(filename); - if (pPixmap == NULL || pPixmap->isNull()) { + if (pPixmap.isNull() || pPixmap->isNull()) { qDebug() << metaObject()->className() << "Error loading pixmap:" << filename; } else { @@ -141,7 +120,7 @@ void WDisplay::paintEvent(QPaintEvent* ) { // If we are disabled, use the disabled pixmaps. If not, use the regular // pixmaps. - const QVector<QPixmap*>& pixmaps = (m_bOff && m_bDisabledLoaded) ? + const QVector<QPixmapPointer>& pixmaps = (m_bOff && m_bDisabledLoaded) ? m_disabledPixmaps : m_pixmaps; if (pixmaps.empty()) { @@ -157,7 +136,7 @@ void WDisplay::paintEvent(QPaintEvent* ) { idx = pixmaps.size() - 1; } - QPixmap* pPixmap = pixmaps[idx]; + QPixmapPointer pPixmap = pixmaps[idx]; if (pPixmap) { p.drawPixmap(0, 0, *pPixmap); } diff --git a/src/widget/wdisplay.h b/src/widget/wdisplay.h index 2634e2f90a..f94b0f54c9 100644 --- a/src/widget/wdisplay.h +++ b/src/widget/wdisplay.h @@ -24,11 +24,12 @@ #include <QString> #include "widget/wwidget.h" +#include "widget/wpixmapstore.h" class WDisplay : public WWidget { Q_OBJECT public: - WDisplay(QWidget *parent=0); + WDisplay(QWidget *parent=NULL); virtual ~WDisplay(); void setup(QDomNode node); @@ -42,7 +43,7 @@ class WDisplay : public WWidget { private: - void setPixmap(QVector<QPixmap*>* pPixmaps, int iPos, + void setPixmap(QVector<QPixmapPointer>* pPixmaps, int iPos, const QString& filename); void setPixmapBackground(const QString& filename); @@ -55,16 +56,16 @@ class WDisplay : public WWidget { void resetPositions(); // Associated background pixmap - QPixmap* m_pPixmapBack; + QPixmapPointer m_pPixmapBack; // List of associated pixmaps. - QVector<QPixmap*> m_pixmaps; + QVector<QPixmapPointer> m_pixmaps; // Whether disabled pixmaps are loaded. bool m_bDisabledLoaded; // List of disabled pixmaps. - QVector<QPixmap*> m_disabledPixmaps; + QVector<QPixmapPointer> m_disabledPixmaps; }; #endif diff --git a/src/widget/wpixmapstore.cpp b/src/widget/wpixmapstore.cpp index f4df47e4e1..15a7cbb1d0 100644 --- a/src/widget/wpixmapstore.cpp +++ b/src/widget/wpixmapstore.cpp @@ -15,89 +15,60 @@ * * ***************************************************************************/ -#include "wpixmapstore.h" +#include "widget/wpixmapstore.h" -#include <QPixmap> #include <QtDebug> // static -QHash<QString, WPixmapStore::PixmapInfoType*> WPixmapStore::m_dictionary; +QHash<QString, QWeakPixmapPointer> WPixmapStore::m_pixmapCache; QSharedPointer<ImgSource> WPixmapStore::m_loader = QSharedPointer<ImgSource>(); // static -QPixmap * WPixmapStore::getPixmap(const QString &fileName) { - // Search for pixmap in list - PixmapInfoType* info = NULL; - - QHash<QString, PixmapInfoType*>::iterator it = m_dictionary.find(fileName); - if (it != m_dictionary.end()) { - info = it.value(); - info->instCount++; - //qDebug() << "WPixmapStore returning cached pixmap for:" << fileName; - return info->pixmap; +QPixmapPointer WPixmapStore::getPixmap(const QString& fileName) { + // See if we have a cached value for the pixmap. + QPixmapPointer pPixmap = m_pixmapCache.value(fileName, QPixmapPointer()); + if (pPixmap) { + return pPixmap; } - // Pixmap wasn't found, construct it + // Otherwise, construct it with the pixmap loader. //qDebug() << "WPixmapStore Loading pixmap from file" << fileName; - QPixmap* loadedPixmap = getPixmapNoCache(fileName); if (loadedPixmap == NULL || loadedPixmap->isNull()) { - qDebug() << "WPixmapStore couldn't load:" << fileName << (loadedPixmap == NULL); - delete loadedPixmap; - return NULL; + qDebug() << "WPixmapStore couldn't load:" << fileName + << (loadedPixmap == NULL); + return QPixmapPointer(); } - info = new PixmapInfoType; - info->pixmap = loadedPixmap; - info->instCount = 1; - m_dictionary.insert(fileName, info); - return info->pixmap; + QPixmapPointer pixmapPointer = QPixmapPointer(loadedPixmap); + m_pixmapCache[fileName] = pixmapPointer; + return pixmapPointer; } // static -QPixmap * WPixmapStore::getPixmapNoCache(const QString& fileName) { - QPixmap* pPixmap; +QPixmap* WPixmapStore::getPixmapNoCache(const QString& fileName) { + QPixmap* pPixmap = NULL; if (m_loader) { - QImage * img = m_loader->getImage(fileName); + QImage* img = m_loader->getImage(fileName); #if QT_VERSION >= 0x040700 - pPixmap = new QPixmap(); - pPixmap->convertFromImage(*img); -#else - pPixmap = new QPixmap(QPixmap::fromImage(*img)); -#endif + pPixmap = new QPixmap(); + pPixmap->convertFromImage(*img); +#else + pPixmap = new QPixmap(QPixmap::fromImage(*img)); +#endif delete img; } else { pPixmap = new QPixmap(fileName); } - return pPixmap; -} - -// static -void WPixmapStore::deletePixmap(QPixmap * p) -{ - // Search for pixmap in list - PixmapInfoType *info = NULL; - QMutableHashIterator<QString, PixmapInfoType*> it(m_dictionary); - - while (it.hasNext()) - { - info = it.next().value(); - if (p == info->pixmap) - { - info->instCount--; - if (info->instCount<1) - { - it.remove(); - delete info->pixmap; - delete info; - } - - break; - } - } + return pPixmap; } void WPixmapStore::setLoader(QSharedPointer<ImgSource> ld) { m_loader = ld; + + // We shouldn't hand out pointers to existing pixmaps anymore since our + // loader has changed. The pixmaps will get freed once all the widgets + // referring to them are destroyed. + m_pixmapCache.clear(); } diff --git a/src/widget/wpixmapstore.h b/src/widget/wpixmapstore.h index d5fc741b3a..c057f3d1b6 100644 --- a/src/widget/wpixmapstore.h +++ b/src/widget/wpixmapstore.h @@ -18,32 +18,23 @@ #ifndef WPIXMAPSTORE_H #define WPIXMAPSTORE_H +#include <QPixmap> #include <QHash> #include <QSharedPointer> #include "skin/imgsource.h" -/** - * - *@author Tue & Ken Haste Andersen - */ - -class QPixmap; +typedef QSharedPointer<QPixmap> QPixmapPointer; +typedef QWeakPointer<QPixmap> QWeakPixmapPointer; class WPixmapStore { public: - static QPixmap *getPixmap(const QString &fileName); - static QPixmap *getPixmapNoCache(const QString &fileName); - static void deletePixmap(QPixmap *p); + static QPixmapPointer getPixmap(const QString &fileName); + static QPixmap* getPixmapNoCache(const QString &fileName); static void setLoader(QSharedPointer<ImgSource> ld); - private: - struct PixmapInfoType { - QPixmap *pixmap; - int instCount; - }; - /** Dictionary of pixmaps already instantiated */ - static QHash<QString, PixmapInfoType*> m_dictionary; + private: + static QHash<QString, QWeakPixmapPointer> m_pixmapCache; static QSharedPointer<ImgSource> m_loader; }; diff --git a/src/widget/wpushbutton.cpp b/src/widget/wpushbutton.cpp index e619b6fc05..fb145e9a6b 100644 --- a/src/widget/wpushbutton.cpp +++ b/src/widget/wpushbutton.cpp @@ -36,7 +36,6 @@ WPushButton::WPushButton(QWidget* parent) m_bLeftClickForcePush(false), m_bRightClickForcePush(false), m_pPixmaps(NULL), - m_pPixmapBack(NULL), m_leftButtonMode(ControlPushButton::PUSH), m_rightButtonMode(ControlPushButton::PUSH) { setStates(0); @@ -49,20 +48,13 @@ WPushButton::WPushButton(QWidget *parent, ControlPushButton::ButtonMode leftButt m_bLeftClickForcePush(false), m_bRightClickForcePush(false), m_pPixmaps(NULL), - m_pPixmapBack(NULL), m_leftButtonMode(leftButtonMode), m_rightButtonMode(rightButtonMode) { setStates(0); } WPushButton::~WPushButton() { - for (int i = 0; i < 2 * m_iNoStates; i++) { - WPixmapStore::deletePixmap(m_pPixmaps[i]); - } - delete [] m_pPixmaps; - - WPixmapStore::deletePixmap(m_pPixmapBack); } void WPushButton::setup(QDomNode node) { @@ -141,10 +133,9 @@ void WPushButton::setStates(int iStates) { m_pPixmaps = NULL; if (iStates > 0) { - m_pPixmaps = new QPixmap*[2 * m_iNoStates]; - for (int i = 0; i < (2 * m_iNoStates); ++i) { - m_pPixmaps[i] = NULL; - } + m_pPixmaps = new QPixmapPointer[2 * m_iNoStates]; + // No need to clear each pointer since the QSharedPointer constructor + // clears them. } } @@ -184,11 +175,11 @@ void WPushButton::setValue(double v) { void WPushButton::paintEvent(QPaintEvent *) { double value = m_value; - if (m_iNoStates > 0) { + if (m_iNoStates > 0) { int idx = (((int)value % m_iNoStates) * 2) + m_bPressed; if (m_pPixmaps[idx]) { QPainter p(this); - if(m_pPixmapBack) { + if (m_pPixmapBack) { p.drawPixmap(0, 0, *m_pPixmapBack); } p.drawPixmap(0, 0, *m_pPixmaps[idx]); diff --git a/src/widget/wpushbutton.h b/src/widget/wpushbutton.h index 8b5242b626..2384372b37 100644 --- a/src/widget/wpushbutton.h +++ b/src/widget/wpushbutton.h @@ -27,21 +27,18 @@ #include <QTimer> #include "widget/wwidget.h" +#include "widget/wpixmapstore.h" #include "controlpushbutton.h" -/** - *@author Tue & Ken Haste Andersen - */ - -class WPushButton : public WWidget -{ +class WPushButton : public WWidget { Q_OBJECT public: WPushButton(QWidget *parent=0); // Used by WPushButtonTest. WPushButton(QWidget *parent, ControlPushButton::ButtonMode leftButtonMode, ControlPushButton::ButtonMode rightButtonMode); - ~WPushButton(); + virtual ~WPushButton(); + void setup(QDomNode node); // Sets the number of states associated with this button, and removes @@ -72,9 +69,9 @@ class WPushButton : public WWidget // Number of states associated with this button int m_iNoStates; // Array of associated pixmaps - QPixmap** m_pPixmaps; + QPixmapPointer* m_pPixmaps; // Associated background pixmap - QPixmap* m_pPixmapBack; + QPixmapPointer m_pPixmapBack; // short click toggle button long click push button ControlPushButton::ButtonMode m_leftButtonMode; ControlPushButton::ButtonMode m_rightButtonMode; diff --git a/src/widget/wslidercomposed.cpp b/src/widget/wslidercomposed.cpp index 97ccc0e435..8614da97f5 100644 --- a/src/widget/wslidercomposed.cpp +++ b/src/widget/wslidercomposed.cpp @@ -93,14 +93,8 @@ void WSliderComposed::setPixmaps(bool bHorizontal, const QString &filenameSlider } void WSliderComposed::unsetPixmaps() { - if (m_pSlider) { - WPixmapStore::deletePixmap(m_pSlider); - m_pSlider = NULL; - } - if (m_pHandle) { - WPixmapStore::deletePixmap(m_pHandle); - m_pHandle = NULL; - } + m_pSlider.clear(); + m_pHandle.clear(); } void WSliderComposed::mouseMoveEvent(QMouseEvent * e) { diff --git a/src/widget/wslidercomposed.h b/src/widget/wslidercomposed.h index ebcd17f158..12ac2a6bda 100644 --- a/src/widget/wslidercomposed.h +++ b/src/widget/wslidercomposed.h @@ -26,6 +26,7 @@ #include <QMouseEvent> #include "widget/wwidget.h" +#include "widget/wpixmapstore.h" /** * A widget for a slider composed of a background pixmap and a handle. @@ -66,9 +67,9 @@ private: /** True if slider is dragged. Only used when m_bEventWhileDrag is false */ bool m_bDrag; /** Pointer to pixmap of the slider */ - QPixmap *m_pSlider; + QPixmapPointer m_pSlider; /** Pointer to pixmap of the handle */ - QPixmap *m_pHandle; + QPixmapPointer m_pHandle; }; #endif diff --git a/src/widget/wstatuslight.cpp b/src/widget/wstatuslight.cpp index 5181d2a697..43301255e4 100644 --- a/src/widget/wstatuslight.cpp +++ b/src/widget/wstatuslight.cpp @@ -24,29 +24,21 @@ #include <QtDebug> #include <QPixmap> -WStatusLight::WStatusLight(QWidget * parent) : WWidget(parent) -{ +WStatusLight::WStatusLight(QWidget * parent) + : WWidget(parent) { m_pPixmapSLs = NULL; m_iNoPos = 0; m_iPos = 0; setNoPos(m_iNoPos); } -WStatusLight::~WStatusLight() -{ - for (int i = 0; i < m_iNoPos; i++) { - WPixmapStore::deletePixmap(m_pPixmapSLs[i]); - } +WStatusLight::~WStatusLight() { delete [] m_pPixmapSLs; } -void WStatusLight::setNoPos(int iNoPos) -{ +void WStatusLight::setNoPos(int iNoPos) { // If pixmap array is already allocated, delete it if (m_pPixmapSLs != NULL) { - for (int i = 0; i < m_iNoPos; i++) { - WPixmapStore::deletePixmap(m_pPixmapSLs[i]); - } delete [] m_pPixmapSLs; } @@ -54,11 +46,7 @@ void WStatusLight::setNoPos(int iNoPos) iNoPos = 2; //values less than 2 make no sense (need at least off, on) m_iNoPos = iNoPos; m_value = 0.; - - m_pPixmapSLs = new QPixmap*[m_iNoPos]; - for (int i = 0; i < m_iNoPos; ++i) { - m_pPixmapSLs[i] = NULL; - } + m_pPixmapSLs = new QPixmapPointer[m_iNoPos]; } void WStatusLight::setup(QDomNode node) @@ -77,7 +65,7 @@ void WStatusLight::setup(QDomNode node) } else if (i == 1 && !selectNode(node, "PathStatusLight").isNull()) { setPixmap(i, getPath(selectNodeQString(node, "PathStatusLight"))); } else { - m_pPixmapSLs[i] = NULL; + m_pPixmapSLs[i].clear(); } } } @@ -89,7 +77,7 @@ void WStatusLight::setPixmap(int iState, const QString &filename) } int pixIdx = iState; - QPixmap* pPixmap = WPixmapStore::getPixmap(filename); + QPixmapPointer pPixmap = WPixmapStore::getPixmap(filename); if (pPixmap != NULL && !pPixmap->isNull()) { m_pPixmapSLs[pixIdx] = pPixmap; @@ -98,8 +86,7 @@ void WStatusLight::setPixmap(int iState, const QString &filename) setFixedSize(pPixmap->size()); } else { qDebug() << "WStatusLight: Error loading pixmap:" << filename << iState; - WPixmapStore::deletePixmap(pPixmap); - m_pPixmapSLs[pixIdx] = NULL; + m_pPixmapSLs[pixIdx].clear(); } } diff --git a/src/widget/wstatuslight.h b/src/widget/wstatuslight.h index f01b844ead..17992df5db 100644 --- a/src/widget/wstatuslight.h +++ b/src/widget/wstatuslight.h @@ -27,22 +27,22 @@ #include <QPixmap> #include "widget/wwidget.h" - -/** - *@author John Sully - */ +#include "widget/wpixmapstore.h" class WStatusLight : public WWidget { Q_OBJECT -public: + public: WStatusLight(QWidget *parent=0); - ~WStatusLight(); + virtual ~WStatusLight(); + void setup(QDomNode node); void setPixmap(int iState, const QString &filename); void setNoPos(int iNoPos); -public slots: + + public slots: void setValue(double v); -private: + + private: void paintEvent(QPaintEvent *); /** Current position */ @@ -50,7 +50,7 @@ private: /** Number of positions associated with this light */ int m_iNoPos; /** Associated pixmaps */ - QPixmap **m_pPixmapSLs; + QPixmapPointer* m_pPixmapSLs; }; #endif diff --git a/src/widget/wvumeter.cpp b/src/widget/wvumeter.cpp index f10c579f44..d662f21d93 100644 --- a/src/widget/wvumeter.cpp +++ b/src/widget/wvumeter.cpp @@ -73,12 +73,9 @@ void WVuMeter::setup(QDomNode node) m_iPeakFallTime = DEFAULT_FALLTIME; } -void WVuMeter::resetPositions() -{ - WPixmapStore::deletePixmap(m_pPixmapBack); - m_pPixmapBack = NULL; - WPixmapStore::deletePixmap(m_pPixmapVu); - m_pPixmapVu = NULL; +void WVuMeter::resetPositions() { + m_pPixmapBack.clear(); + m_pPixmapVu.clear(); } void WVuMeter::setPixmaps(const QString &backFilename, const QString &vuFilename, bool bHorizontal) diff --git a/src/widget/wvumeter.h b/src/widget/wvumeter.h index b70deaaa73..e8a89e59d4 100644 --- a/src/widget/wvumeter.h +++ b/src/widget/wvumeter.h @@ -18,8 +18,6 @@ #ifndef WVUMETER_H #define WVUMETER_H -#include "widget/wwidget.h" - #include <QPixmap> #include <QString> #include <QPaintEvent> @@ -27,23 +25,24 @@ #include <QWidget> #include <QDomNode> -/** - *@author Tue & Ken Haste Andersen - */ +#include "widget/wwidget.h" +#include "widget/wpixmapstore.h" class WVuMeter : public WWidget { Q_OBJECT -public: + public: WVuMeter(QWidget *parent=0); - ~WVuMeter(); + virtual ~WVuMeter(); + void setup(QDomNode node); - void setPixmaps(const QString &backFilename, const QString &vuFilename, bool bHorizontal=false); + void setPixmaps(const QString &backFilename, const QString &vuFilename, + bool bHorizontal=false); void setValue(double fValue); -protected slots: + protected slots: void updateState(int msecsElapsed); -private: + private: /** Set position number to zero and deallocate pixmaps */ void resetPositions(); void paintEvent(QPaintEvent *); @@ -54,7 +53,8 @@ private: /** Number of positions associated with this knob */ int m_iNoPos; /** Associated pixmaps */ - QPixmap *m_pPixmapBack, *m_pPixmapVu; + QPixmapPointer m_pPixmapBack; + QPixmapPointer m_pPixmapVu; /** True if it's a horizontal vu meter */ bool m_bHorizontal; diff --git a/src/widget/wwidgetgroup.cpp b/src/widget/wwidgetgroup.cpp index 2d4dbedd97..d0ec8218ce 100644 --- a/src/widget/wwidgetgroup.cpp +++ b/src/widget/wwidgetgroup.cpp @@ -14,7 +14,6 @@ WWidgetGroup::WWidgetGroup(QWidget* parent) } WWidgetGroup::~WWidgetGroup() { - WPixmapStore::deletePixmap(m_pPixmapBack); } void WWidgetGroup::setup(QDomNode node) { diff --git a/src/widget/wwidgetgroup.h b/src/widget/wwidgetgroup.h index 4eb7995396..79d2d642b7 100644 --- a/src/widget/wwidgetgroup.h +++ b/src/widget/wwidgetgroup.h @@ -9,6 +9,8 @@ #include <QString> #include <QWidget> +#include "widget/wpixmapstore.h" + class WWidgetGroup : public QGroupBox { Q_OBJECT public: @@ -24,7 +26,7 @@ class WWidgetGroup : public QGroupBox { private: // Associated background pixmap - QPixmap *m_pPixmapBack; + QPixmapPointer m_pPixmapBack; QPixmap m_pixmapBackScaled; }; |