SoDaRadio-5.0.3-master:8901fb5
freqlabel.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2017 Matthew H. Reilly (kb1vc)
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
8 
9  Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11  Redistributions in binary form must reproduce the above copyright
12  notice, this list of conditions and the following disclaimer in
13  the documentation and/or other materials provided with the
14  distribution.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 #include <cmath>
30 #include "freqlabel.hpp"
31 #include <iostream>
32 
33 using namespace GUISoDa;
34 
36  Qt::WindowFlags f)
37  : QLabel(parent), incdec_position(2)
38 {
39  (void) f;
40  setFreq(144.295e6);
41 
42  setTextFormat(Qt::RichText);
43 }
44 
46 
48  QString ret;
49  int num_digs;
50  unsigned long ifrac = frac_freq;
51  // start at the left hand side
52  bool first_nonzero = false;
53  for(num_digs = 0; num_digs < 6; num_digs++) {
54  int dig = ifrac % 10;
55  ifrac = ifrac / 10;
56  if(num_digs == 3) ret = " " + ret;
57  if(incdec_position == num_digs) {
58  ret = "<u>" + QString::number(dig) + "</u>" + ret;
59  }
60  else {
61  ret = QString::number(dig) + ret;
62  }
63  }
64 
65  // add the decimal point
66  ret = "." + ret;
67 
68  unsigned long ifreq = int_freq;
69  for(; num_digs < 11; num_digs++) {
70  int dig = ifreq % 10;
71  ifreq = ifreq / 10;
72 
73  if(((num_digs % 3) == 0) && (num_digs > 6)) {
74  ret = "," + ret;
75  }
76  if(incdec_position == num_digs) {
77  ret = "<u>" + QString::number(dig) + "</u>" + ret;
78  }
79  else {
80  ret = QString::number(dig) + ret;
81  }
82  }
83 
84  while(ret.size() < 14) {
85  ret = " " + ret;
86  }
87  return ret;
88 }
89 
90 void GUISoDa::FreqLabel::setFreq(double hzfreq)
91 {
92  double freq = hzfreq * 1e-6;
93  frequency = hzfreq;
94 
95  int_freq = lround(floor(freq));
96  double fr = freq - floor(freq);
97  frac_freq = lround(fr * 1e6);
98  QString flab = freq2String();
99 
100  setText(flab);
101 
102  // now set the text width/height
103  QRect r = fontMetrics().boundingRect(flab);
104  disp_w = r.width();
105  disp_h = r.height();
106 }
107 
108 void GUISoDa::FreqLabel::mousePressEvent(QMouseEvent * event)
109 {
110  int px = event->x();
111  int py = event->y();
112  int wh = height();
113  int ww = width();
114 
115  unsigned long incr = 0;
116  if(event->button() == Qt::LeftButton) {
117  if(py > (wh >> 1)) {
118  incr = incr - 1;
119  }
120  else if(py < (wh >> 1)) {
121  incr = incr + 1;
122  }
123  }
124 
125  if(event->button() == Qt::RightButton) {
126  if(px < (ww >> 1)) {
127  incdec_position += 1;
128  if(incdec_position > 10) incdec_position = 10;
129  }
130  else if(px > (ww >> 1)) {
131  incdec_position -= 1;
132  if(incdec_position < 0) incdec_position = 0;
133  }
134  }
135 
136  if(incr != 0) {
137  if(incdec_position < 6) {
138  for(int i = 0; i < incdec_position; i++) {
139  incr = incr * 10;
140  }
141  frac_freq += incr;
142  }
143  else {
144  for(int i = 6; i < incdec_position; i++) {
145  incr = incr * 10;
146  }
147  int_freq += incr;
148  if(int_freq < 0) {
149  int_freq -= incr;
150  // move adj to right
151  incdec_position -= 1;
152  }
153  }
154  }
155  if(frac_freq >= 1000000) {
156  frac_freq -= 1000000;
157  int_freq += 1;
158  }
159  if(frac_freq < 0) {
160  frac_freq += 1000000;
161  int_freq -= 1;
162  }
163 
164  if(int_freq > 99999) int_freq = 99999;
165  if(int_freq < 0) int_freq = 0;
166 
168 
169  emit newFreq(frequency);
170 }
double updateFrequency()
Definition: freqlabel.hpp:66
QString freq2String()
Definition: freqlabel.cpp:47
void setFreq(double freq)
Definition: freqlabel.cpp:90
void mousePressEvent(QMouseEvent *event)
Definition: freqlabel.cpp:108
FreqLabel(QWidget *parent=Q_NULLPTR, Qt::WindowFlags f=Qt::WindowFlags())
Definition: freqlabel.cpp:35
void newFreq(double freq)