summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRJ Ryan <rryan@mixxx.org>2013-12-24 01:37:25 -0500
committerRJ Ryan <rryan@mixxx.org>2013-12-24 01:37:25 -0500
commit8e069765f06b9d8fdc9a05d9b2c87fbbce8bdf3d (patch)
treef42510380519c38ffbd425138175b4b5e0a3b2b4 /src
parent873cc4852eed94acbb20492e87fc70061f1a6847 (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')
-rw-r--r--src/widget/wdisplay.cpp97
-rw-r--r--src/widget/wdisplay.h24
-rw-r--r--src/widget/wknob.cpp195
-rw-r--r--src/widget/wknob.h42
4 files changed, 156 insertions, 202 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);
}
}
diff --git a/src/widget/wdisplay.h b/src/widget/wdisplay.h
index 1d33387b11..2634e2f90a 100644
--- a/src/widget/wdisplay.h
+++ b/src/widget/wdisplay.h
@@ -33,18 +33,38 @@ class WDisplay : public WWidget {
void setup(QDomNode node);
- void setPixmap(int iPos, const QString& filename);
+ protected:
+ void paintEvent(QPaintEvent*);
+
+ int numPixmaps() const {
+ return m_pixmaps.size();
+ }
private:
+
+ void setPixmap(QVector<QPixmap*>* pPixmaps, int iPos,
+ const QString& filename);
+
+ void setPixmapBackground(const QString& filename);
+
void setPositions(int iNoPos);
+ virtual int getActivePixmapIndex() const;
+
// Free existing pixmaps.
void resetPositions();
- void paintEvent(QPaintEvent*);
+ // Associated background pixmap
+ QPixmap* m_pPixmapBack;
// List of associated pixmaps.
QVector<QPixmap*> m_pixmaps;
+
+ // Whether disabled pixmaps are loaded.
+ bool m_bDisabledLoaded;
+
+ // List of disabled pixmaps.
+ QVector<QPixmap*> m_disabledPixmaps;
};
#endif
diff --git a/src/widget/wknob.cpp b/src/widget/wknob.cpp
index 4395363ce0..23e7b22d12 100644
--- a/src/widget/wknob.cpp
+++ b/src/widget/wknob.cpp
@@ -27,107 +27,15 @@
#include "defs.h"
#include "widget/wpixmapstore.h"
-WKnob::WKnob(QWidget * parent)
- : WWidget(parent),
- m_bRightButtonPressed(false),
- m_iPos(0),
- m_iNoPos(0),
- m_pPixmaps(NULL),
- m_pPixmapBack(NULL),
- m_bDisabledLoaded(false) {
+WKnob::WKnob(QWidget* pParent)
+ : WDisplay(pParent),
+ m_bRightButtonPressed(false) {
}
-WKnob::~WKnob()
-{
- resetPositions();
- if (m_pPixmapBack) {
- WPixmapStore::deletePixmap(m_pPixmapBack);
- }
-}
-
-void WKnob::setup(QDomNode node)
-{
- // Set background pixmap if available
- if (!selectNode(node, "BackPath").isNull())
- setPixmapBackground(getPath(selectNodeQString(node, "BackPath")));
-
- // Number of states. Depends if disabled pics are defined as well
- setPositions(selectNodeInt(node, "NumberStates"),
- !selectNode(node, "DisabledPath").isNull());
-
- // Load knob pixmaps
- QString path = selectNodeQString(node, "Path");
- for (int i=0; i<m_iNoPos; ++i)
- setPixmap(i, getPath(path.arg(i)));
-
- // See if disabled images is defined, and load them...
- if (!selectNode(node, "DisabledPath").isNull())
- {
- path = selectNodeQString(node, "DisabledPath");
- for (int i=0; i<m_iNoPos; ++i)
- setPixmap(i+m_iNoPos, getPath(path.arg(i)));
- m_bDisabledLoaded = true;
- }
-}
-
-void WKnob::setPositions(int iNoPos, bool bIncludingDisabled)
-{
- resetPositions();
-
- m_iNoPos = iNoPos;
- m_iPos = 0;
-
- if (m_iNoPos>0)
- {
- int pics = m_iNoPos;
- if (bIncludingDisabled)
- pics *= 2;
-
- m_pPixmaps = new QPixmap*[pics];
- for (int i=0; i<pics; i++)
- m_pPixmaps[i] = 0;
- }
+WKnob::~WKnob() {
}
-void WKnob::resetPositions()
-{
- if (m_pPixmaps)
- {
- int pics = m_iNoPos;
- if( m_bDisabledLoaded ){
- pics *= 2;
- }
- for (int i=0; i<pics; i++) {
- if (m_pPixmaps[i]) {
- WPixmapStore::deletePixmap(m_pPixmaps[i]);
- }
- }
- delete [] m_pPixmaps;
- m_pPixmaps = NULL;
- }
-}
-
-void WKnob::setPixmap(int iPos, const QString &filename)
-{
- m_pPixmaps[iPos] = WPixmapStore::getPixmap(filename);
- if (!m_pPixmaps[iPos])
- qDebug() << "WKnob: Error loading pixmap" << filename;
- setFixedSize(m_pPixmaps[iPos]->size());
-}
-
-void WKnob::setPixmapBackground(const QString &filename)
-{
- // Load background pixmap
- if (m_pPixmapBack) {
- WPixmapStore::deletePixmap(m_pPixmapBack);
- }
- m_pPixmapBack = WPixmapStore::getPixmap(filename);
- if (!m_pPixmapBack)
- qDebug() << "WKnob: Error loading background pixmap:" << filename;
-}
-
-void WKnob::mouseMoveEvent(QMouseEvent * e)
-{
+void WKnob::mouseMoveEvent(QMouseEvent* e) {
if (!m_bRightButtonPressed) {
QPoint cur(e->globalPos());
QPoint diff(cur - m_startPos);
@@ -144,55 +52,52 @@ void WKnob::mouseMoveEvent(QMouseEvent * e)
m_value += dist;
QCursor::setPos(m_startPos);
- if (m_value>127.)
- m_value = 127.;
- else if (m_value<0.)
- m_value = 0.;
+ if (m_value > 127.0)
+ m_value = 127.0;
+ else if (m_value < 0.0)
+ m_value = 0.0;
+
emit(valueChangedLeftDown(m_value));
update();
}
}
-void WKnob::mousePressEvent(QMouseEvent * e)
-{
+void WKnob::mousePressEvent(QMouseEvent* e) {
switch (e->button()) {
- case Qt::RightButton:
- emit(valueReset());
- m_bRightButtonPressed = true;
- break;
- case Qt::LeftButton:
- case Qt::MidButton:
- m_startPos = e->globalPos();
- QApplication::setOverrideCursor(Qt::BlankCursor);
- break;
- default:
- break;
+ case Qt::RightButton:
+ emit(valueReset());
+ m_bRightButtonPressed = true;
+ break;
+ case Qt::LeftButton:
+ case Qt::MidButton:
+ m_startPos = e->globalPos();
+ QApplication::setOverrideCursor(Qt::BlankCursor);
+ break;
+ default:
+ break;
}
}
-void WKnob::mouseReleaseEvent(QMouseEvent * e)
-{
+void WKnob::mouseReleaseEvent(QMouseEvent* e) {
switch (e->button()) {
- case Qt::LeftButton:
- case Qt::MidButton:
- QCursor::setPos(m_startPos);
- QApplication::restoreOverrideCursor();
- emit(valueChangedLeftUp(m_value));
- break;
- case Qt::RightButton:
- m_bRightButtonPressed = false;
- //emit(valueChangedRightUp(m_fValue));
- break;
- default:
- break;
+ case Qt::LeftButton:
+ case Qt::MidButton:
+ QCursor::setPos(m_startPos);
+ QApplication::restoreOverrideCursor();
+ emit(valueChangedLeftUp(m_value));
+ break;
+ case Qt::RightButton:
+ m_bRightButtonPressed = false;
+ //emit(valueChangedRightUp(m_fValue));
+ break;
+ default:
+ break;
}
-
update();
}
-void WKnob::wheelEvent(QWheelEvent *e)
-{
- double wheelDirection = ((QWheelEvent *)e)->delta() / 120.;
+void WKnob::wheelEvent(QWheelEvent *e) {
+ double wheelDirection = e->delta() / 120.;
double newValue = getValue() + wheelDirection;
// Clamp to [0.0, 127.0]
@@ -205,26 +110,8 @@ void WKnob::wheelEvent(QWheelEvent *e)
//e->ignore();
}
-void WKnob::paintEvent(QPaintEvent *)
-{
- if (m_pPixmaps)
- {
- int idx = (int)(((m_value-64.)*(((float)m_iNoPos-1.)/127.))+((float)m_iNoPos/2.));
- // Range check
- if (idx>(m_iNoPos-1))
- idx = m_iNoPos-1;
- else if (idx<0)
- idx = 0;
-
- // Disabled pixmaps are placed ahead of normal pixmaps in the buffer. Use them
- // if the widget is disabled and the disabled pixmaps are loaded.
- if (m_bOff && m_bDisabledLoaded)
- idx += m_iNoPos;
-
- QPainter p(this);
- // Paint background
- //p.drawPixmap(0, 0, m_pPixmapBack);
- // Paint button
- p.drawPixmap(0, 0, *m_pPixmaps[idx]);
- }
+int WKnob::getActivePixmapIndex() const {
+ // TODO(rryan): Ew.
+ int iNoPos = numPixmaps();
+ return (int)(((m_value-64.)*(((float)iNoPos-1.)/127.))+((float)iNoPos/2.));
}
diff --git a/src/widget/wknob.h b/src/widget/wknob.h
index e22a7e61f5..11e45147bf 100644
--- a/src/widget/wknob.h
+++ b/src/widget/wknob.h
@@ -24,46 +24,28 @@
#include <QMouseEvent>
#include <QWheelEvent>
-#include "widget/wwidget.h"
+#include "widget/wdisplay.h"
-/**
- *@author Tue & Ken Haste Andersen
- */
-
-class WKnob : public WWidget {
+class WKnob : public WDisplay {
Q_OBJECT
-public:
- WKnob(QWidget *parent=0);
- ~WKnob();
- void setup(QDomNode node);
- void setPositions(int iNoPos, bool bIncludingDisabled=false);
- void setPixmap(int iPos, const QString &filename);
- /** Associates a background pixmap with the widget. This is only needed if the knob
- * pixmaps contains alpha channel values. */
- void setPixmapBackground(const QString &filename);
+ public:
+ WKnob(QWidget* pParent=NULL);
+ virtual ~WKnob();
+
+ protected:
void wheelEvent(QWheelEvent *e);
-private:
- /** Set position number to zero and deallocate pixmaps */
- void resetPositions();
void mouseMoveEvent(QMouseEvent *e);
void mousePressEvent(QMouseEvent *e);
void mouseReleaseEvent(QMouseEvent *e);
- void paintEvent(QPaintEvent *);
+
+ private:
+ int getActivePixmapIndex() const;
// True if right mouse button is pressed.
bool m_bRightButtonPressed;
- /** Current position */
- int m_iPos;
- /** Number of positions associated with this knob */
- int m_iNoPos;
- /** Array of associated pixmaps */
- QPixmap **m_pPixmaps;
- /** Associated background pixmap */
- QPixmap *m_pPixmapBack;
- /** Starting point when left mouse button is pressed */
+
+ // Starting point when left mouse button is pressed
QPoint m_startPos;
- /** True if disabled pixmaps is loaded */
- bool m_bDisabledLoaded;
};
#endif