diff options
author | RJ Ryan <rryan@mixxx.org> | 2013-12-24 11:54:18 -0500 |
---|---|---|
committer | RJ Ryan <rryan@mixxx.org> | 2013-12-24 11:57:43 -0500 |
commit | cdbe4e0f1ff2ab7a017195264ce27ede4d6fdf87 (patch) | |
tree | 9188b717cd79124343c74da95b5150f94f22ec3b /src/widget | |
parent | e2b5e1cd18367cc06318d8516bb36003fe76b9b4 (diff) |
Refactor WStatusLight to use a QVector of pixmap pointers.
Diffstat (limited to 'src/widget')
-rw-r--r-- | src/widget/wstatuslight.cpp | 86 | ||||
-rw-r--r-- | src/widget/wstatuslight.h | 20 |
2 files changed, 54 insertions, 52 deletions
diff --git a/src/widget/wstatuslight.cpp b/src/widget/wstatuslight.cpp index 43301255e4..017cb30431 100644 --- a/src/widget/wstatuslight.cpp +++ b/src/widget/wstatuslight.cpp @@ -16,8 +16,7 @@ * * ***************************************************************************/ -#include "wstatuslight.h" -#include "wpixmapstore.h" +#include "widget/wstatuslight.h" #include <QPaintEvent> #include <QPainter> @@ -25,37 +24,35 @@ #include <QPixmap> WStatusLight::WStatusLight(QWidget * parent) - : WWidget(parent) { - m_pPixmapSLs = NULL; - m_iNoPos = 0; - m_iPos = 0; - setNoPos(m_iNoPos); + : WWidget(parent), + m_iPos(0) { + setNoPos(0); } WStatusLight::~WStatusLight() { - delete [] m_pPixmapSLs; } void WStatusLight::setNoPos(int iNoPos) { - // If pixmap array is already allocated, delete it - if (m_pPixmapSLs != NULL) { - delete [] m_pPixmapSLs; + // If pixmap array is already allocated, delete it. + if (!m_pixmaps.empty()) { + // Clear references to existing pixmaps. + m_pixmaps.resize(0); } - if (iNoPos < 2) - iNoPos = 2; //values less than 2 make no sense (need at least off, on) - m_iNoPos = iNoPos; + // values less than 2 make no sense (need at least off, on) + if (iNoPos < 2) { + iNoPos = 2; + } + m_pixmaps.resize(iNoPos); m_value = 0.; - m_pPixmapSLs = new QPixmapPointer[m_iNoPos]; } -void WStatusLight::setup(QDomNode node) -{ - // Number of states. Add one to account for the background +void WStatusLight::setup(QDomNode node) { + // Number of states. Add one to account for the background. setNoPos(selectNodeInt(node, "NumberPos") + 1); // Set pixmaps - for (int i = 0; i < m_iNoPos; ++i) { + for (int i = 0; i < m_pixmaps.size(); ++i) { // Accept either PathStatusLight or PathStatusLight1 for value 1, QString nodeName = QString("PathStatusLight%1").arg(i); if (!selectNode(node, nodeName).isNull()) { @@ -65,59 +62,60 @@ void WStatusLight::setup(QDomNode node) } else if (i == 1 && !selectNode(node, "PathStatusLight").isNull()) { setPixmap(i, getPath(selectNodeQString(node, "PathStatusLight"))); } else { - m_pPixmapSLs[i].clear(); + m_pixmaps[i].clear(); } } } -void WStatusLight::setPixmap(int iState, const QString &filename) -{ - if (iState < 0 || iState >= m_iNoPos) { +void WStatusLight::setPixmap(int iState, const QString& filename) { + if (iState < 0 || iState >= m_pixmaps.size()) { return; } - int pixIdx = iState; QPixmapPointer pPixmap = WPixmapStore::getPixmap(filename); - if (pPixmap != NULL && !pPixmap->isNull()) { - m_pPixmapSLs[pixIdx] = pPixmap; + if (!pPixmap.isNull() && !pPixmap->isNull()) { + m_pixmaps[iState] = pPixmap; // Set size of widget equal to pixmap size setFixedSize(pPixmap->size()); } else { qDebug() << "WStatusLight: Error loading pixmap:" << filename << iState; - m_pPixmapSLs[pixIdx].clear(); + m_pixmaps[iState].clear(); } } -void WStatusLight::setValue(double v) -{ - int val = (int)v; - if (m_iPos == val) +void WStatusLight::setValue(double v) { + int val = static_cast<int>(v); + if (m_iPos == val) { return; + } - if (m_iNoPos == 2) { - //original behavior for two-state lights: any non-zero value is "on" + if (m_pixmaps.size() == 2) { + // original behavior for two-state lights: any non-zero value is "on" m_iPos = val > 0 ? 1 : 0; update(); + } else if (val < m_pixmaps.size() && val >= 0) { + // multi-state behavior: values must be correct + m_iPos = val; + update(); } else { - //multi-state behavior: values must be correct - if (val < m_iNoPos && val >= 0) { - m_iPos = val; - update(); - } else { - qDebug() << "Warning: wstatuslight asked for invalid position:" << val << "max val:" << m_iNoPos-1; - } + qDebug() << "Warning: wstatuslight asked for invalid position:" + << val << "max val:" << m_pixmaps.size()-1; } } void WStatusLight::paintEvent(QPaintEvent *) { - if (m_iPos < 0 || m_iPos >= m_iNoPos) { + if (m_iPos < 0 || m_iPos >= m_pixmaps.size()) { return; } - QPainter p(this); - if (m_pPixmapSLs[m_iPos] != NULL && !m_pPixmapSLs[m_iPos]->isNull()) { - p.drawPixmap(0, 0, *m_pPixmapSLs[m_iPos]); + QPixmapPointer pPixmap = m_pixmaps[m_iPos]; + + if (pPixmap.isNull() || pPixmap->isNull()) { + return; } + + QPainter p(this); + p.drawPixmap(0, 0, *pPixmap); } diff --git a/src/widget/wstatuslight.h b/src/widget/wstatuslight.h index 17992df5db..07d3a409f0 100644 --- a/src/widget/wstatuslight.h +++ b/src/widget/wstatuslight.h @@ -25,6 +25,7 @@ #include <QString> #include <QDomNode> #include <QPixmap> +#include <QVector> #include "widget/wwidget.h" #include "widget/wpixmapstore.h" @@ -36,21 +37,24 @@ class WStatusLight : public WWidget { virtual ~WStatusLight(); void setup(QDomNode node); - void setPixmap(int iState, const QString &filename); - void setNoPos(int iNoPos); public slots: void setValue(double v); - private: + protected: void paintEvent(QPaintEvent *); - /** Current position */ + private: + void setPixmap(int iState, const QString &filename); + void setNoPos(int iNoPos); + + // Current position int m_iPos; - /** Number of positions associated with this light */ - int m_iNoPos; - /** Associated pixmaps */ - QPixmapPointer* m_pPixmapSLs; + + QPixmapPointer m_pPixmapBackground; + + // Associated pixmaps + QVector<QPixmapPointer> m_pixmaps; }; #endif |