![]() |
Mixxx
|
00001 /*************************************************************************** 00002 wstatuslight.cpp - description 00003 ------------------- 00004 begin : Wed May 30 2007 00005 copyright : (C) 2003 by Tue & Ken Haste Andersen 00006 (C) 2007 by John Sully (converted from WVumeter) 00007 email : jsully@scs.ryerson.ca 00008 ***************************************************************************/ 00009 00010 /*************************************************************************** 00011 * * 00012 * This program is free software; you can redistribute it and/or modify * 00013 * it under the terms of the GNU General Public License as published by * 00014 * the Free Software Foundation; either version 2 of the License, or * 00015 * (at your option) any later version. * 00016 * * 00017 ***************************************************************************/ 00018 00019 #include "wstatuslight.h" 00020 #include "wpixmapstore.h" 00021 00022 #include <QPaintEvent> 00023 #include <QPainter> 00024 #include <QtDebug> 00025 #include <QPixmap> 00026 00027 WStatusLight::WStatusLight(QWidget * parent) : WWidget(parent) 00028 { 00029 m_pPixmapSLs = NULL; 00030 m_iNoPos = 0; 00031 m_iPos = 0; 00032 setNoPos(m_iNoPos); 00033 } 00034 00035 WStatusLight::~WStatusLight() 00036 { 00037 for (int i = 0; i < m_iNoPos; i++) { 00038 WPixmapStore::deletePixmap(m_pPixmapSLs[i]); 00039 } 00040 delete [] m_pPixmapSLs; 00041 } 00042 00043 void WStatusLight::setNoPos(int iNoPos) 00044 { 00045 // If pixmap array is already allocated, delete it 00046 if (m_pPixmapSLs != NULL) { 00047 for (int i = 0; i < m_iNoPos; i++) { 00048 WPixmapStore::deletePixmap(m_pPixmapSLs[i]); 00049 } 00050 delete [] m_pPixmapSLs; 00051 } 00052 00053 if (iNoPos < 2) 00054 iNoPos = 2; //values less than 2 make no sense (need at least off, on) 00055 m_iNoPos = iNoPos; 00056 m_fValue = 0.; 00057 00058 m_pPixmapSLs = new QPixmap*[m_iNoPos]; 00059 for (int i = 0; i < m_iNoPos; ++i) { 00060 m_pPixmapSLs[i] = NULL; 00061 } 00062 } 00063 00064 void WStatusLight::setup(QDomNode node) 00065 { 00066 // Number of states. Add one to account for the background 00067 setNoPos(selectNodeInt(node, "NumberPos") + 1); 00068 00069 // Set background pixmap if available 00070 if (!selectNode(node, "PathBack").isNull()) { 00071 setPixmap(0, getPath(selectNodeQString(node, "PathBack"))); 00072 } else { 00073 m_pPixmapSLs[0] = NULL; 00074 } 00075 00076 // Set pixmaps 00077 for (int i = 1; i < m_iNoPos; ++i) { 00078 QString nodeName = (i == 1) ? "PathStatusLight" : QString("PathStatusLight%1").arg(i); 00079 if (!selectNode(node, nodeName).isNull()) { 00080 setPixmap(i, getPath(selectNodeQString(node, nodeName))); 00081 } else { 00082 m_pPixmapSLs[i] = NULL; 00083 } 00084 } 00085 } 00086 00087 void WStatusLight::setPixmap(int iState, const QString &filename) 00088 { 00089 if (iState < 0 || iState >= m_iNoPos) { 00090 return; 00091 } 00092 int pixIdx = iState; 00093 00094 QPixmap* pPixmap = WPixmapStore::getPixmap(filename); 00095 00096 if (pPixmap != NULL && !pPixmap->isNull()) { 00097 m_pPixmapSLs[pixIdx] = pPixmap; 00098 00099 // Set size of widget equal to pixmap size 00100 setFixedSize(pPixmap->size()); 00101 } else { 00102 qDebug() << "WStatusLight: Error loading pixmap:" << filename << iState; 00103 WPixmapStore::deletePixmap(pPixmap); 00104 m_pPixmapSLs[pixIdx] = NULL; 00105 } 00106 } 00107 00108 void WStatusLight::setValue(double v) 00109 { 00110 int val = (int)v; 00111 if (m_iPos == val) 00112 return; 00113 00114 if (m_iNoPos == 2) { 00115 //original behavior for two-state lights: any non-zero value is "on" 00116 if (val == 0) 00117 m_iPos = 0; 00118 else 00119 m_iPos = 1; 00120 update(); 00121 } else { 00122 //multi-state behavior: values must be correct 00123 if (val < m_iNoPos && val >= 0) { 00124 m_iPos = val; 00125 update(); 00126 } else { 00127 qDebug() << "Warning: wstatuslight asked for invalid position:" << val << "max val:" << m_iNoPos-1; 00128 } 00129 } 00130 } 00131 00132 void WStatusLight::paintEvent(QPaintEvent *) 00133 { 00134 Q_ASSERT (m_iPos < m_iNoPos); 00135 00136 QPainter p(this); 00137 00138 // Draw the background 00139 if (m_iPos != 0 && m_iNoPos > 0 && m_pPixmapSLs[0] != NULL && !m_pPixmapSLs[0]->isNull()) { 00140 p.drawPixmap(0, 0, *m_pPixmapSLs[0]); 00141 } 00142 00143 if (m_iPos >= 0 && m_iPos < m_iNoPos && m_pPixmapSLs[m_iPos] != NULL && !m_pPixmapSLs[m_iPos]->isNull()) { 00144 p.drawPixmap(0, 0, *m_pPixmapSLs[m_iPos]); 00145 } 00146 }