-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvfodisplay.cpp
176 lines (141 loc) · 4.66 KB
/
vfodisplay.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
/**
** This file is part of the CatRadio project.
** Copyright 2022 Gianfranco Sordetti IZ8EWD <iz8ewd@pianetaradio.it>.
**
** 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 "vfodisplay.h"
#include <QPainter>
#include <QInputEvent>
#include <QDebug>
#include <QtMath>
#include <math.h>
vfoDisplay::vfoDisplay(QWidget *parent) : QWidget(parent)
{
lineColor = QColor(Qt::black);
bgColor = QColor(Qt::white);
textColor = QColor(Qt::black);
vfoDisplayMode = 0;
}
void vfoDisplay::paintEvent(QPaintEvent *)
{
//setMouseTracking(true);
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
QFont font;
font = painter.font();
font.setPointSize(height() - 18);
painter.setFont(font);
drawDisplay(&painter);
drawText(&painter);
}
void vfoDisplay::drawDisplay(QPainter *painter)
{
painter->save();
QPen pen(lineColor, 1, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin);
painter->setPen(pen);
painter->setBrush(bgColor);
painter->drawRect(1, 1, width()-3, height()-2);
painter->restore();
}
void vfoDisplay::drawText(QPainter *painter)
{
painter->save();
painter->setPen(textColor);
QFontMetrics meterFont(painter->font());
double textHeight = meterFont.ascent();
textWidth = meterFont.horizontalAdvance("0",-1);
QPointF textPot;
long val = currentValue;
for (int i = 1; i < 10; i++)
{
val = qFloor(val/10);
int d = val % 10;
if (i==2)
{
textPot = QPointF(width()-3-(textWidth+2)*i-4, textHeight);
painter->drawText(textPot, ".");
}
textPot = QPointF(width()-3-(textWidth+2)*i, textHeight);
if (val==0 && d==0) painter->drawText(textPot, " ");
else painter->drawText(textPot, QString::number(d));
}
painter->restore();
}
void vfoDisplay::setBgColor(QColor color)
{
bgColor = color;
}
void vfoDisplay::setLineColor(QColor color)
{
lineColor = color;
}
void vfoDisplay::setTextColor(QColor color)
{
textColor = color;
}
void vfoDisplay::setValue(unsigned long value)
{
currentValue = value;
update();
}
void vfoDisplay::setMode(int mode)
{
vfoDisplayMode = mode;
}
//* Tuning using mouse buttons
void vfoDisplay::mousePressEvent(QMouseEvent *event)
{
event->accept();
QPoint pointerPos = event->pos();
for (int i = 1; i < 10; i ++)
{
if (pointerPos.x() > (width()-3-(textWidth+2)*i+1) && pointerPos.x() < (width()-3-(textWidth+2)*(i-1)-1))
{
if (vfoDisplayMode && event->button() == Qt::LeftButton) //Up/Down mode
{
if (pointerPos.y() < height()/2) currentValue = currentValue + pow(10,i); //Up
else if (currentValue - pow(10,i) > 0) currentValue = currentValue - pow(10,i); //Down
}
else if (!vfoDisplayMode) //Left/Right mode
{
if (event->button() == Qt::LeftButton) currentValue = currentValue + pow(10,i); //LeftButton
else if (currentValue - pow(10,i) > 0) currentValue = currentValue - pow(10,i); //RightButton
}
break;
}
}
update();
emit on_valueChanged(currentValue);
}
//* Tuning using mouse wheel
void vfoDisplay::wheelEvent(QWheelEvent *event)
{
event->accept();
#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
QPointF pointerPos = event->position();
#else
QPointF pointerPos = event->pos();
#endif
for (int i = 1; i < 10; i ++)
{
if (pointerPos.x() > (width()-3-(textWidth+2)*i+1) && pointerPos.x() < (width()-3-(textWidth+2)*(i-1)-1))
{
if (event->angleDelta().y() > 0) currentValue = currentValue + pow(10,i); //Wheel up
else if (currentValue - pow(10,i) > 0) currentValue = currentValue - pow(10,i); //Wheel down
}
}
update();
emit on_valueChanged(currentValue);
}