1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
/***************************************************************************
wstatuslight.cpp - description
-------------------
begin : Wed May 30 2007
copyright : (C) 2003 by Tue & Ken Haste Andersen
(C) 2007 by John Sully (converted from WVumeter)
email : jsully@scs.ryerson.ca
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "widget/wstatuslight.h"
#include <QPaintEvent>
#include <QStylePainter>
#include <QStyleOption>
#include <QtDebug>
#include <QPixmap>
WStatusLight::WStatusLight(QWidget * parent)
: WWidget(parent),
m_iPos(0) {
setNoPos(0);
}
WStatusLight::~WStatusLight() {
}
void WStatusLight::setNoPos(int iNoPos) {
// If pixmap array is already allocated, delete it.
if (!m_pixmaps.empty()) {
// Clear references to existing pixmaps.
m_pixmaps.resize(0);
}
// values less than 2 make no sense (need at least off, on)
if (iNoPos < 2) {
iNoPos = 2;
}
m_pixmaps.resize(iNoPos);
}
WStatusLight::SizeMode WStatusLight::SizeModeFromString(QString str) {
if (str.toUpper() == "FIXED" ) {
return FIXED;
} else if (str.toUpper() == "RESIZE") {
return RESIZE;
}
qWarning() << "Unknown status light size mode " << str << ", using FIXED";
return FIXED;
}
void WStatusLight::setup(QDomNode node, const SkinContext& context) {
// Number of states. Add one to account for the background.
setNoPos(context.selectInt(node, "NumberPos") + 1);
// Set pixmaps
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 (context.hasNode(node, nodeName)) {
QString mode = context.selectAttributeString(
context.selectElement(node, nodeName), "sizemode", "FIXED");
setPixmap(i, context.getSkinPath(context.selectString(node, nodeName)),
SizeModeFromString(mode));
} else if (i == 0 && context.hasNode(node, "PathBack")) {
QString mode = context.selectAttributeString(
context.selectElement(node, "PathBack"), "sizemode", "FIXED");
setPixmap(i, context.getSkinPath(context.selectString(node, "PathBack")),
SizeModeFromString(mode));
} else if (i == 1 && context.hasNode(node, "PathStatusLight")) {
QString mode = context.selectAttributeString(
context.selectElement(node, "PathStatusLight"), "sizemode", "FIXED");
setPixmap(i, context.getSkinPath(context.selectString(node, "PathStatusLight")),
SizeModeFromString(mode));
} else {
m_pixmaps[i].clear();
}
}
}
void WStatusLight::setPixmap(int iState, const QString& filename, SizeMode mode) {
if (iState < 0 || iState >= m_pixmaps.size()) {
return;
}
PaintablePointer pPixmap = WPixmapStore::getPaintable(filename,
Paintable::STRETCH);
if (!pPixmap.isNull() && !pPixmap->isNull()) {
m_pixmaps[iState] = pPixmap;
switch (mode) {
case RESIZE:
// Allow the pixmap to stretch or tile.
setBaseSize(pPixmap->size());
break;
case FIXED:
// Set size of widget equal to pixmap size
setFixedSize(pPixmap->size());
break;
default:
setFixedSize(pPixmap->size());
break;
}
} else {
qDebug() << "WStatusLight: Error loading pixmap:" << filename << iState;
m_pixmaps[iState].clear();
}
}
void WStatusLight::onConnectedControlValueChanged(double v) {
int val = static_cast<int>(v);
if (m_iPos == val) {
return;
}
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 {
qDebug() << "Warning: wstatuslight asked for invalid position:"
<< val << "max val:" << m_pixmaps.size()-1;
}
}
void WStatusLight::paintEvent(QPaintEvent *) {
QStyleOption option;
option.initFrom(this);
QStylePainter p(this);
p.drawPrimitive(QStyle::PE_Widget, option);
if (m_iPos < 0 || m_iPos >= m_pixmaps.size()) {
return;
}
PaintablePointer pPixmap = m_pixmaps[m_iPos];
if (pPixmap.isNull() || pPixmap->isNull()) {
return;
}
pPixmap->draw(0, 0, &p);
}
|