diff options
author | RJ Ryan <rryan@mixxx.org> | 2013-12-24 01:37:25 -0500 |
---|---|---|
committer | RJ Ryan <rryan@mixxx.org> | 2013-12-24 01:37:25 -0500 |
commit | 8e069765f06b9d8fdc9a05d9b2c87fbbce8bdf3d (patch) | |
tree | f42510380519c38ffbd425138175b4b5e0a3b2b4 /src/widget/wdisplay.cpp | |
parent | 873cc4852eed94acbb20492e87fc70061f1a6847 (diff) |
Make WKnob inherit from WDisplay.
WKnob shared a bunch of common logic with WDisplay since it's basically a
WDisplay with input. WDisplay now has support for backgrounds and "disabled"
widget support.
Diffstat (limited to 'src/widget/wdisplay.cpp')
-rw-r--r-- | src/widget/wdisplay.cpp | 97 |
1 files changed, 81 insertions, 16 deletions
diff --git a/src/widget/wdisplay.cpp b/src/widget/wdisplay.cpp index e7a20fafdb..471df62ea2 100644 --- a/src/widget/wdisplay.cpp +++ b/src/widget/wdisplay.cpp @@ -25,7 +25,9 @@ #include "widget/wpixmapstore.h" WDisplay::WDisplay(QWidget * parent) - : WWidget(parent) { + : WWidget(parent), + m_pPixmapBack(NULL), + m_bDisabledLoaded(false) { setPositions(0); } @@ -34,13 +36,29 @@ WDisplay::~WDisplay() { } void WDisplay::setup(QDomNode node) { + // Set background pixmap if available + if (!selectNode(node, "BackPath").isNull()) { + setPixmapBackground(getPath(selectNodeQString(node, "BackPath"))); + } + // Number of states setPositions(selectNodeInt(node, "NumberStates")); + // Load knob pixmaps QString path = selectNodeQString(node, "Path"); for (int i = 0; i < m_pixmaps.size(); ++i) { - setPixmap(i, getPath(path.arg(i))); + setPixmap(&m_pixmaps, i, getPath(path.arg(i))); + } + + // See if disabled images is defined, and load them... + if (!selectNode(node, "DisabledPath").isNull()) { + QString disabledPath = selectNodeQString(node, "DisabledPath"); + for (int i = 0; i < m_disabledPixmaps.size(); ++i) { + setPixmap(&m_disabledPixmaps, i, + getPath(disabledPath.arg(i))); + } + m_bDisabledLoaded = true; } } @@ -54,6 +72,7 @@ void WDisplay::setPositions(int iNoPos) { // QVector inserts NULLs for the new pixmaps. m_pixmaps.resize(iNoPos); + m_disabledPixmaps.resize(iNoPos); } void WDisplay::resetPositions() { @@ -63,37 +82,83 @@ void WDisplay::resetPositions() { 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_pixmaps.resize(0); + m_disabledPixmaps.resize(0); } -void WDisplay::setPixmap(int iPos, const QString& filename) { - if (iPos < 0 || iPos >= m_pixmaps.size()) { +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()) { + qDebug() << metaObject()->className() + << "Error loading background pixmap:" << filename; + } +} + +void WDisplay::setPixmap(QVector<QPixmap*>* pPixmaps, int iPos, + const QString& filename) { + if (iPos < 0 || iPos >= pPixmaps->size()) { return; } - m_pixmaps[iPos] = WPixmapStore::getPixmap(filename); - if (!m_pixmaps[iPos]) - qDebug() << "WDisplay: Error loading pixmap" << filename; - else - setFixedSize(m_pixmaps[iPos]->size()); + + QPixmap* pPixmap = WPixmapStore::getPixmap(filename); + + if (pPixmap == NULL || pPixmap->isNull()) { + qDebug() << metaObject()->className() + << "Error loading pixmap:" << filename; + } else { + (*pPixmaps)[iPos] = pPixmap; + setFixedSize(pPixmap->size()); + } } -void WDisplay::paintEvent(QPaintEvent* ) { - int idx = static_cast<int>( +int WDisplay::getActivePixmapIndex() const { + return static_cast<int>( m_value * static_cast<double>(m_pixmaps.size()) / 128.0); +} - if (m_pixmaps.empty()) { +void WDisplay::paintEvent(QPaintEvent* ) { + QPainter p(this); + + if (m_pPixmapBack) { + p.drawPixmap(0, 0, *m_pPixmapBack); + } + + // If we are disabled, use the disabled pixmaps. If not, use the regular + // pixmaps. + const QVector<QPixmap*>& pixmaps = (m_bOff && m_bDisabledLoaded) ? + m_disabledPixmaps : m_pixmaps; + + if (pixmaps.empty()) { return; } + int idx = getActivePixmapIndex(); + + // Clamp active pixmap index to valid ranges. if (idx < 0) { idx = 0; - } else if (idx >= m_pixmaps.size()) { - idx = m_pixmaps.size() - 1; + } else if (idx >= pixmaps.size()) { + idx = pixmaps.size() - 1; } - QPixmap* pPixmap = m_pixmaps[idx]; + QPixmap* pPixmap = pixmaps[idx]; if (pPixmap) { - QPainter p(this); p.drawPixmap(0, 0, *pPixmap); } } |