Debian Codegraph @

src/tuner.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2016, Mike Walters <[email protected]>
3 *
4 * This file is part of inspectrum.
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <cstdlib>
21 #include "tuner.h"
22
23 Tuner::Tuner(int height, QObject * parent) : height(height), QObject::QObject(parent)
24 {
25 minCursor = new Cursor(Qt::Horizontal, Qt::SizeVerCursor, this);
26 cfCursor = new Cursor(Qt::Horizontal, Qt::SizeAllCursor, this);
27 maxCursor = new Cursor(Qt::Horizontal, Qt::SizeVerCursor, this);
28 connect(minCursor, &Cursor::posChanged, this, &Tuner::cursorMoved);
29 connect(cfCursor, &Cursor::posChanged, this, &Tuner::cursorMoved);
30 connect(maxCursor, &Cursor::posChanged, this, &Tuner::cursorMoved);
31
32 cfCursor->setPos(100);
33 _deviation = 10;
34 updateCursors();
35 }
36
37 int Tuner::centre()
38 {
39 return cfCursor->pos();
40 }
41
42 void Tuner::cursorMoved()
43 {
44 Cursor *sender = static_cast<Cursor*>(QObject::sender());
45 if (sender != cfCursor) {
46 // Limit cursor positions to within plot
47 auto posRange = range_t<int>{0, height};
48 sender->setPos(posRange.clip(sender->pos()));
49
50 // Limit deviation range to half of total BW (either side of centre)
51 auto deviationRange = range_t<int>{2, height / 2};
52 _deviation = deviationRange.clip(std::abs(sender->pos() - cfCursor->pos()));
53 } else {
54 auto cfRange = range_t<int>{_deviation, height - _deviation};
55 sender->setPos(cfRange.clip(sender->pos()));
56 }
57
58 updateCursors();
59 }
60
61 int Tuner::deviation()
62 {
63 return _deviation;
64 }
65
66 bool Tuner::mouseEvent(QEvent::Type type, QMouseEvent *event)
67 {
68 if (cfCursor->mouseEvent(type, event))
69 return true;
70 if (minCursor->mouseEvent(type, event))
71 return true;
72 if (maxCursor->mouseEvent(type, event))
73 return true;
74
75 return false;
76 }
77
78 void Tuner::leaveEvent()
79 {
80 cfCursor->leaveEvent();
81 minCursor->leaveEvent();
82 maxCursor->leaveEvent();
83 }
84
85 void Tuner::paintFront(QPainter &painter, QRect &rect, range_t<size_t> sampleRange)
86 {
87 painter.save();
88
89 QRect cursorRect(rect.left(), rect.top() + minCursor->pos(), rect.right(), maxCursor->pos() - minCursor->pos());
90
91 // Draw translucent white fill for highlight
92 painter.fillRect(
93 cursorRect,
94 QBrush(QColor(255, 255, 255, 50))
95 );
96
97 // Draw tuner edges
98 painter.setPen(QPen(Qt::white, 1, Qt::SolidLine));
99 painter.drawLine(rect.left(), rect.top() + minCursor->pos(), rect.right(), rect.top() + minCursor->pos());
100 painter.drawLine(rect.left(), rect.top() + maxCursor->pos(), rect.right(), rect.top() + maxCursor->pos());
101
102 // Draw centre freq
103 painter.setPen(QPen(Qt::red, 1, Qt::SolidLine));
104 painter.drawLine(rect.left(), rect.top() + cfCursor->pos(), rect.right(), rect.top() + cfCursor->pos());
105
106 painter.restore();
107 }
108
109 void Tuner::setCentre(int centre)
110 {
111 cfCursor->setPos(centre);
112 updateCursors();
113 }
114
115 void Tuner::setDeviation(int dev)
116 {
117 _deviation = std::max(1, dev);
118 updateCursors();
119 }
120
121 void Tuner::setHeight(int height)
122 {
123 this->height = height;
124 }
125
126 void Tuner::updateCursors()
127 {
128 minCursor->setPos(cfCursor->pos() - _deviation);
129 maxCursor->setPos(cfCursor->pos() + _deviation);
130 emit tunerMoved();
131 }