-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomDisplay.cpp
178 lines (154 loc) · 5.92 KB
/
customDisplay.cpp
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
/****************************************************************************
**
** Copyright (C) 2007~2017 Colin Willcocks.
** Copyright (C) 2005~2007 Uco Mesdag.
** All rights reserved.
** This file is part of "GT-8 Fx FloorBoard".
**
** 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.
**
** 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, write to the Free Software Foundation, Inc.,
** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
**
****************************************************************************/
#include <QPainter>
#include "customDisplay.h"
customDisplay::customDisplay(QRect geometry, QWidget *parent)
: QWidget(parent)
{
this->geometry = geometry;
this->font.setFamily("void");
this->setGeometry(geometry);
this->setLabelPosition();
}
void customDisplay::paintEvent(QPaintEvent *)
{
/* Set the default font. */
if(this->font.family() == "void")
{
this->font = this->mainLabel->font();
this->mainPal = this->mainLabel->palette();
this->subPal = this->subLabelLeft->palette();
}
else
{
this->mainLabel->setFont(this->font);
}
while(this->mainLabel->geometry().width() < QFontMetrics(this->mainLabel->font()).width(this->mainLabel->text()))
{
/* Change the font size to make it fit... */
QFont tmpfont = this->mainLabel->font();
int fontSize = tmpfont.pixelSize();
tmpfont.setPixelSize(fontSize - 1);
this->mainLabel->setFont(tmpfont);
if (fontSize <= 1)
{
break;
}
}
/* This paints the background with the border. */
QPen border;
border.setWidth(1);
border.setColor(QColor(150,150,150));
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(border);
painter.setBrush(QColor(0,1,62));
//painter.drawRoundRect(QRectF(0.0, 0.0, geometry.width()-1, geometry.height()-1), 8, 8);
//painter.drawRect(QRect(0.0, 0.0, geometry.width()-1, geometry.height()-1));
/* Draw a custom path. This to have a constant border
radius independant of the rectangle size. */
int radius = 5; // Set the border radius.
QPainterPath roundRectPath;
roundRectPath.moveTo(geometry.width()-1, radius);
roundRectPath.arcTo((geometry.width()-1) - radius*2, 0.0, radius*2, radius*2, 0.0, 90.0);
roundRectPath.lineTo(radius, 0.0);
roundRectPath.arcTo(0.0, 0.0, radius*2, radius*2, 90.0, 90.0);
roundRectPath.lineTo(0.0, (geometry.height()-1) - radius);
roundRectPath.arcTo(0.0, (geometry.height()-1) - (radius*2), radius*2, radius*2, 180.0, 90.0);
roundRectPath.lineTo((geometry.width()-1) - radius, geometry.height()-1);
roundRectPath.arcTo((geometry.width()-1) - (radius*2), (geometry.height()-1) - (radius*2), radius*2, radius*2, 270.0, 90.0);
roundRectPath.closeSubpath();
painter.drawPath(roundRectPath);
}
void customDisplay::setLabelPosition(bool invert)
{
int height = this->geometry.height();
int width = this->geometry.width();
int mainExtra = 3;
int marginWidth = 5;
int marginHeight = 2;
QRect subGeometry, mainGeometry, htmlGeometry;
if(invert)
{
subGeometry = QRect(marginWidth, marginHeight, width - (marginWidth * 2), (height / 2) - marginHeight);
mainGeometry = QRect(marginWidth, (height / 2) - mainExtra, width - (marginWidth * 2), (height / 2) - marginHeight);
}
else
{
mainGeometry = QRect(marginWidth, marginHeight, width - (marginWidth * 2), ((height / 2) - marginHeight) + mainExtra);
subGeometry = QRect(marginWidth, marginHeight + (height / 2), width - (marginWidth * 2), (height / 2) - marginHeight);
}
this->mainLabel = new QLabel(this);
this->mainLabel->setObjectName("displayLarge");
this->mainLabel->setAlignment(Qt::AlignLeft);
this->mainLabel->setGeometry(mainGeometry);
this->subLabelLeft = new QLabel(this);
this->subLabelLeft->setObjectName("displaySmall");
this->subLabelLeft->setAlignment(Qt::AlignLeft);
this->subLabelLeft->setGeometry(subGeometry);
this->subLabelRight = new QLabel(this);
this->subLabelRight->setObjectName("displaySmall");
this->subLabelRight->setAlignment(Qt::AlignRight);
this->subLabelRight->setGeometry(subGeometry);
}
void customDisplay::setMainText(QString mainText, Qt::Alignment alignment)
{
this->mainLabel->setText(mainText);
this->mainLabel->setAlignment(alignment);
}
void customDisplay::setSubText(QString subTextLeft, QString subTextRight)
{
this->subLabelLeft->setText(subTextLeft);
this->subLabelRight->setText(subTextRight);
}
void customDisplay::clearAll()
{
this->mainLabel->clear();
this->subLabelLeft->clear();
this->subLabelRight->clear();
}
void customDisplay::setMainObjectName(QString name)
{
this->mainLabel->setObjectName(name);
}
void customDisplay::setSubObjectName(QString name)
{
this->subLabelLeft->setObjectName(name);
this->subLabelRight->setObjectName(name);
}
void customDisplay::setAllColor(QColor color)
{
QString red, green, blue;;
red = QString::number(color.red(), 10);
green = QString::number(color.green(), 10);
blue = QString::number(color.blue(), 10);
this->mainLabel->setStyleSheet("color: rgb("+ red +","+ green +","+ blue +");");
this->subLabelLeft->setStyleSheet("color: rgb("+ red +","+ green +","+ blue +");");
this->subLabelRight->setStyleSheet("color: rgb("+ red +","+ green +","+ blue +");");
}
void customDisplay::resetAllColor()
{
this->mainLabel->setPalette(this->mainPal);
this->subLabelLeft->setPalette(this->subPal);
this->subLabelRight->setPalette(this->subPal);
}