Inserimento Led, Modifiche Layout
[qmemory] / qled.cpp
1 /***************************************************************************
2  *   Copyright (C) 2010 by P. Sereno                                       *
3  *   http://www.sereno-online.com                                          *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU Lesser General Public License           *
7  *   version 2.1 as published by the Free Software Foundation              *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU Lesser General Public License for more details.                   *
13  *   http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.               *
14  ***************************************************************************/
15   
16 #include <QColor>
17 #include <QtGlobal>
18 #include <QtGui>
19 #include <QPolygon>
20 #include <QtSvg>
21 #include <QSvgRenderer>
22 #include <QDebug>
23 #include "qled.h"
24
25
26 /*!
27   \brief QLed: this is the QLed constructor.
28   \param parent: The Parent Widget
29 */
30 QLed::QLed(QWidget *parent)
31     : QWidget(parent)
32 {
33    m_value=false;
34    m_onColor=Red;
35    m_offColor=Grey;
36    m_shape=Circle;
37    setMinimumSize(QSize(50,50));
38    shapes << ":/resources/circle_" << ":/resources/square_" << ":/resources/triang_" << ":/resources/round_";
39    colors << "red.svg" << "green.svg" << "yellow.svg" << "grey.svg" << "orange.svg" << "purple.svg" << "blue.svg";
40
41 }
42
43
44 /*!
45   \brief paintEvent: painting method
46   \param QPaintEvent *
47   \return void
48 */
49 void QLed::paintEvent(QPaintEvent *)
50 {
51     qDebug()<<"pain";
52     QSvgRenderer *renderer = new QSvgRenderer();
53     QString ledShapeAndColor;
54     QPainter painter(this);
55     painter.setRenderHint(QPainter::Antialiasing, true);
56
57     ledShapeAndColor=shapes[m_shape];
58
59     if(m_value)
60         ledShapeAndColor.append(colors[m_onColor]);
61     else
62         ledShapeAndColor.append(colors[m_offColor]);
63
64     renderer->load(ledShapeAndColor);
65     renderer->render(&painter);
66
67 }
68
69
70 /*!
71   \brief setOnColor: this method allows to change the On color {Red,Green,Yellow,Grey,Orange,Purple,blue}
72   \param ledColor newColor
73   \return void
74 */
75 void QLed::setOnColor(ledColor newColor)
76 {
77    m_onColor=newColor;
78    update();
79 }
80
81
82 /*!
83   \brief setOffColor: this method allows to change the Off color {Red,Green,Yellow,Grey,Orange,Purple,blue}
84   \param ledColor newColor
85   \return void
86 */
87 void QLed::setOffColor(ledColor newColor)
88 {
89    m_offColor=newColor;
90    update();
91 }
92
93
94 /*!
95   \brief setShape: this method allows to change the led shape {Circle,Square,Triangle,Rounded rectangle}
96   \param ledColor newColor
97   \return void
98 */
99 void QLed::setShape(ledShape newShape)
100 {
101    m_shape=newShape;
102    update();
103 }
104
105
106 /*!
107   \brief setValue: this method allows to set the led value {true,false}
108   \param ledColor newColor
109   \return void
110 */
111 void QLed::setValue(bool value)
112 {
113    m_value=value;
114    update();
115 }
116
117
118 /*!
119   \brief toggleValue: this method toggles the led value
120   \param ledColor newColor
121   \return void
122 */
123 void QLed::toggleValue()
124
125         m_value=!m_value;
126         update();
127         return; 
128 }