[go: up one dir, main page]

Menu

[r89]: / tags / 0.4.0 / Led.cpp  Maximize  Restore  History

Download this file

265 lines (219 with data), 7.0 kB

  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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
Midi Layer
Copyright (C) 2012-2013, António José Soares Maia <maia.ajs@gmail.com>
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 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; If not, see <http://www.gnu.org/licenses/>.
*/
#include "Led.h"
int Led::lastTextLenght = 0;
int Led::lastTextSize = 0;
int Led::lastTextX = 0;
int Led::lastTextY = 0;
int Led::lastWidth = 0;
int Led::lastHeight = 0;
/**
* @brief The Led class Simple rectangle Led widget
*/
Led::Led(QWidget *parent, int width, int height) :
QWidget(parent)
{
this->w = width;
this->h = height;
this->setMinimumSize(width, height);
this->enabled = false;
this->gradient = true;
this->timer = new QTimer();
this->timer->setSingleShot(true);
this->timer->setInterval(100);
// Default colors
this->setColor(QColor("#FF0000"));
this->textColor="#FFFFFF";
this->text="";
this->textFont = font();
// Default font
this->textFont.setFamily("Sans Serif");
this->textOffsetX = 0;
this->textOffsetY = -2;
this->textX = 0;
this->textY = 0;
this->fontSize = 10;
this->textFont.setPixelSize(this->fontSize);
this->setFont(this->textFont);
this->connect(this->timer, SIGNAL(timeout()), this, SLOT(turnOff()));
}
Led::~Led() {
delete timer;
}
/**
* @brief Led::setColor Sets the status ON color LED
* @param color State ON LED color
*/
void Led::setColor(QColor color) {
this->color = color;
this->borderColor = this->color.darker(600);
this->buildBrushes();
this->repaint();
}
void Led::buildBrushes() {
if (this->gradient) {
this->onBrush = QBrush(buidGradient( this->color ));
this->offBrush = QBrush(buidGradient(this->color.darker(300)));
} else {
this->onBrush = QBrush(this->color);
this->offBrush = QBrush(this->offBrush);
}
}
/**
* @brief Led::setColorOff Sets the status OFF color LED. If not defined will
* be automaticly calculated.
* @param color Off state LED color
*/
void Led::setColorOff(QColor color) {
if (this->gradient)
this->offBrush = QBrush(buidGradient(color));
else
this->offBrush = QBrush(color);
this->buildBrushes();
this->repaint();
}
//void Led::setMaxSize(int width, int height) {
// this->width = width;
// this->height = height;
//}
/**
* @brief Led::setText Sets the LED text
* @param text LED text
*/
void Led::setText(QString text) {
this->text = text;
QFontMetrics fontMetrics(this->textFont);
this->labelBoundingRect = fontMetrics.boundingRect(text);
this->update();
}
/**
* @brief Led::setTextColor Sets the LED text color
* @param color text color
*/
void Led::setTextColor(QColor color) {
this->textColor=color;
}
/**
* @brief Led::setTextFontSize Sets LED text font size
* @param fontSize size in pixels
*/
void Led::setTextFontSize(int fontSize) {
this->fontSize=fontSize;
this->textFont.setPixelSize(fontSize);
QFontMetrics fontMetrics(textFont);
this->labelBoundingRect = fontMetrics.boundingRect(text);
}
/**
* @brief Led::setTextOffset Offset coords to adjust text position
* @param textOffsetX x axis offset
* @param textOffsetY y axis offset
*/
void Led::setTextOffset(int textOffsetX, int textOffsetY) {
this->textOffsetX = textOffsetX;
this->textOffsetY = textOffsetY;
}
/**
* @brief Led::flash flash LED (see setFlashInterval)
*/
void Led::flash() {
this->timer->start();
this->enabled = true;
this->update();
}
/**
* @brief Led::setFlashInterval Sets the time of LED flash.
* @param interval time of flash in milliseconds
*/
void Led::setFlashDuration(int interval) {
this->timer->setInterval(interval);
}
/**
* @brief Led::setGradient LED will filled with a gradient aspect (more real)
* @param useGradient defines if LED is filled with gradient
*/
void Led::setGradient(bool useGradient) {
this->gradient = useGradient;
this->buildBrushes();
}
/**
* @brief Led::buidGradient builds a gradient using color base.
* @param color color base for gradient
* @return the bilded gradient
*/
QLinearGradient Led::buidGradient(QColor color) {
QLinearGradient gradient(0, 0, 0, this->height());
gradient.setColorAt(0.0, color);
gradient.setColorAt(1.0, color.darker(150));
return gradient;
}
/**
* @brief Led::turnOff Sets LED to off state
*/
void Led::turnOff() {
this->enabled = false;
this->update();
}
/**
* @brief Led::paintEvent Render widget
*/
void Led::paintEvent(QPaintEvent * /* event */) {
QPainter painter(this);
this->buildBrushes();
if (!text.isEmpty() ) {
int size;
if ( Led::lastTextLenght != this->text.length() ||
Led::lastHeight != this->height() ||
(this->width() > Led::lastWidth+2 || this->width() < Led::lastWidth-2 )
) {
LOG_INFO(QString("widht->%1, last->%2").arg(this->width()).arg(Led::lastWidth));
QFontMetrics fontMetrics(this->textFont);
this->labelBoundingRect = fontMetrics.boundingRect(text);
int height;
height = (int) this->height()*0.8f;
int width;
width = (int) this->width()*0.6f;
size = this->min(height, (int) (2.2*width/this->text.length()) );
if (size<6) {
size = 6;
}
this->textX = (int) ((float)(this->width() -this->labelBoundingRect.width()) * 0.45f);
this->textY = (int) ((float)(this->height()+this->labelBoundingRect.height()) * 0.46f);
Led::lastHeight = this->height();
Led::lastWidth = this->width();
Led::lastTextLenght = this->text.length();
Led::lastTextSize = size;
Led::lastTextX = this->textX;
Led::lastTextY = this->textY;
} else {
size = Led::lastTextSize;
this->textX = Led::lastTextX;
this->textY = Led::lastTextY;
}
this->textFont.setPixelSize(size);
this->setFont(this->textFont);
}
this->enabled ? painter.setBrush(this->onBrush) : painter.setBrush(this->offBrush);
painter.setPen(this->borderColor);
painter.drawRect(0, 0, this->width()-1, this->height()-1);
if (!text.isEmpty()) {
painter.setPen(this->textColor);
painter.drawText( this->textX, this->textY, this->text);
}
}
int Led::min(int a, int b) {
if (a<b)
return a;
return b;
}