File: freqctrl.cpp

package info (click to toggle)
gqrx-sdr 2.6-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,700 kB
  • ctags: 2,724
  • sloc: cpp: 22,413; ansic: 564; xml: 25; makefile: 4
file content (903 lines) | stat: -rw-r--r-- 28,476 bytes parent folder | download | duplicates (2)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
/////////////////////////////////////////////////////////////////////
// freqctrl.cpp: implementation of the CFreqCtrl class.
//
//  This class implements a frequency control widget to set/change
//frequency data.
//
// History:
//  2010-09-15  Initial creation MSW
//  2011-03-27  Initial release
//  2011-04-17  Fixed bug with m_Oldfreq being set after emit instead of before
/////////////////////////////////////////////////////////////////////

//==========================================================================================
// + + +   This Software is released under the "Simplified BSD License"  + + +
// Copyright 2010 Moe Wheatley.
// Copyright 2012 Alexandru Csete
// All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are
//permitted provided that the following conditions are met:
//
//   1. Redistributions of source code must retain the above copyright notice, this list of
//    conditions and the following disclaimer.
//
//   2. Redistributions in binary form must reproduce the above copyright notice, this list
//    of conditions and the following disclaimer in the documentation and/or other materials
//    provided with the distribution.
//
//THIS SOFTWARE IS PROVIDED BY Moe Wheatley ``AS IS'' AND ANY EXPRESS OR IMPLIED
//WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
//FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Moe Wheatley OR
//CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
//CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
//SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
//ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
//ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//The views and conclusions contained in the software and documentation are those of the
//authors and should not be interpreted as representing official policies, either expressed
//or implied, of Moe Wheatley.
//==========================================================================================

#include <QDebug>
#include "freqctrl.h"


//Manual adjustment of Font size as percent of control height
#define DIGIT_SIZE_PERCENT 90
#define UNITS_SIZE_PERCENT 60

//adjustment for separation between digits
#define SEPRATIO_N 100  //separation rectangle size ratio numerator times 100
#define SEPRATIO_D 3    //separation rectangle size ratio denominator

#define STATUS_TIP \
    "Scroll or left-click to increase/decrease digit. " \
    "Right-click to clear digits."

/////////////////////////////////////////////////////////////////////
// Constructor/Destructor
/////////////////////////////////////////////////////////////////////
CFreqCtrl::CFreqCtrl(QWidget *parent) :
    QFrame(parent)
{
    setAutoFillBackground(false);
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    setFocusPolicy(Qt::StrongFocus);
    setMouseTracking(true);
    m_BkColor = QColor(0x1F, 0x1D, 0x1D, 0xFF);
    m_DigitColor = QColor(0xFF, 0xFF, 0xFF, 0xFF);
    m_HighlightColor = QColor(0x5A, 0x5A, 0x5A, 0xFF);
    m_UnitsColor = Qt::gray;
    m_freq = 146123456;
    setup(10, 1, 4000000000U, 1, UNITS_MHZ);
    m_Oldfreq = 0;
    m_LastLeadZeroPos = 0;
    m_LRMouseFreqSel = false;
    m_ActiveEditDigit = -1;
    m_ResetLowerDigits = true;
    m_UnitsFont = QFont("Arial", 12, QFont::Normal);
    m_DigitFont = QFont("Arial", 12, QFont::Normal);

    setStatusTip(tr(STATUS_TIP));
}

CFreqCtrl::~CFreqCtrl()
{
}

/////////////////////////////////////////////////////////////////////
//  Size hint stuff
/////////////////////////////////////////////////////////////////////
QSize CFreqCtrl::minimumSizeHint() const
{
    return QSize(100, 20);
}

QSize CFreqCtrl::sizeHint() const
{
    return QSize(100, 20);
}

/////////////////////////////////////////////////////////////////////
//  Various helper functions
/////////////////////////////////////////////////////////////////////
bool CFreqCtrl::inRect(QRect &rect, QPoint &point)
{
    if ((point.x() < rect.right()) && (point.x() > rect.x()) &&
        (point.y() < rect.bottom()) && (point.y() > rect.y()))
        return true;
    else
        return false;
}

//////////////////////////////////////////////////////////////////////////////
//  Setup various parameters for the control
//////////////////////////////////////////////////////////////////////////////
void CFreqCtrl::setup(int NumDigits, qint64 Minf, qint64 Maxf,int MinStep, FUNITS UnitsType)
{
    int i;
    qint64 pwr = 1;
    m_LastEditDigit = 0;
    m_Oldfreq = -1;
    m_NumDigits = NumDigits;

    if (m_NumDigits > MAX_DIGITS)
        m_NumDigits = MAX_DIGITS;

    if (m_NumDigits < MIN_DIGITS)
        m_NumDigits = MIN_DIGITS;

    m_UnitString = "";
    m_MinStep = MinStep;

    if (m_MinStep == 0)
        m_MinStep = 1;

    m_MinFreq = Minf;
    m_MaxFreq = Maxf;

    if (m_freq < m_MinFreq)
        m_freq = m_MinFreq;

    if (m_freq > m_MaxFreq)
        m_freq = m_MaxFreq;

    for (i = 0; i < m_NumDigits; i++)
    {
        m_DigitInfo[i].weight = pwr;
        m_DigitInfo[i].incval = pwr;
        m_DigitInfo[i].modified = true;
        m_DigitInfo[i].editmode = false;
        m_DigitInfo[i].val = 0;
        pwr *= 10;
    }

    if (m_MaxFreq > pwr)
        m_MaxFreq = pwr-1;

    m_MaxFreq = m_MaxFreq - m_MaxFreq%m_MinStep;

    if (m_MinFreq > pwr)
        m_MinFreq = 1;

    m_MinFreq = m_MinFreq - m_MinFreq % m_MinStep;
    m_DigStart = 0;

    switch (UnitsType)
    {
        case UNITS_HZ:
            m_DecPos = 0;
            m_UnitString = "Hz ";
            break;
        case UNITS_KHZ:
            m_DecPos = 3;
            m_UnitString = "kHz";
            break;
        case UNITS_MHZ:
            m_DecPos = 6;
            m_UnitString = "MHz";
            break;
        case UNITS_GHZ:
            m_DecPos = 9;
            m_UnitString = "GHz";
            break;
        case UNITS_SEC:
            m_DecPos = 6;
            m_UnitString = "Sec";
            break;
        case UNITS_MSEC:
            m_DecPos = 3;
            m_UnitString = "mS ";
            break;
        case UNITS_USEC:
            m_DecPos = 0;
            m_UnitString = "uS ";
            break;
        case UNITS_NSEC:
            m_DecPos = 0;
            m_UnitString = "nS ";
            break;
    }

    for (i = m_NumDigits-1; i >= 0; i--)
    {
        if (m_DigitInfo[i].weight <= m_MinStep)
        {
            if (m_DigStart == 0)
            {
                m_DigitInfo[i].incval = m_MinStep;
                m_DigStart = i;
            }
            else
            {
                if ((m_MinStep%m_DigitInfo[i+1].weight) != 0)
                    m_DigStart = i;
                m_DigitInfo[i].incval = 0;
            }
        }
    }

    m_NumSeps = (m_NumDigits-1)/3 - m_DigStart/3;
}

//////////////////////////////////////////////////////////////////////////////
//  Sets the frequency and individual digit values
//////////////////////////////////////////////////////////////////////////////
void CFreqCtrl::setFrequency(qint64 freq)
{
    int i;
    qint64 acc = 0;
    qint64 rem;
    int val;

    if (freq == m_Oldfreq)
        return;

    if (freq < m_MinFreq)
        freq = m_MinFreq;

    if (freq > m_MaxFreq)
        freq = m_MaxFreq;

    m_freq = freq - freq % m_MinStep;
    rem = m_freq;
    m_LeadZeroPos = m_NumDigits;

    for (i = m_NumDigits-1; i >= m_DigStart; i--)
    {
        val = (int)(rem / m_DigitInfo[i].weight);
        if (m_DigitInfo[i].val != val)
        {
            m_DigitInfo[i].val = val;
            m_DigitInfo[i].modified = true;
        }
        rem = rem - val*m_DigitInfo[i].weight;
        acc += val;
        if ((acc == 0) && (i > m_DecPos))
        {
            m_LeadZeroPos = i;
        }
    }

    // If the sign changed and the frequency is less than 1 unit,
    // redraw the leading zero to get the correct sign.
    if ((m_Oldfreq ^ m_freq) < 0 && m_DigitInfo[m_LeadZeroPos - 1].val == 0)
        m_DigitInfo[m_LeadZeroPos - 1].modified = true;

    // When frequency is negative all non-zero digits that
    // have changed will have a negative sign. This loop will
    // change all digits back to positive, except the one at
    // position m_leadZeroPos-1. If that position is zero,
    // it will be checked in the drawing method, drawDigits().
    /** TBC if this works for all configurations */
    if (m_freq < 0)
    {
        if (m_DigitInfo[m_LeadZeroPos-1].val > 0)
            m_DigitInfo[m_LeadZeroPos-1].val = -m_DigitInfo[m_LeadZeroPos-1].val;

        for (i = 0; i < (m_LeadZeroPos-1); i++)
        {
            if (m_DigitInfo[i].val < 0)
                m_DigitInfo[i].val = -m_DigitInfo[i].val;
        }
    }

    // signal the new frequency to world
    m_Oldfreq = m_freq;
    emit newFrequency(m_freq);
    updateCtrl(m_LastLeadZeroPos != m_LeadZeroPos);
    m_LastLeadZeroPos = m_LeadZeroPos;
}

//////////////////////////////////////////////////////////////////////////////
//  Sets the Digit and comma and decimal pt color
//////////////////////////////////////////////////////////////////////////////
void CFreqCtrl::setDigitColor(QColor cr)
{
    m_UpdateAll = true;
    m_DigitColor = cr;
    for(int i=m_DigStart; i<m_NumDigits; i++)
        m_DigitInfo[i].modified = true;
    updateCtrl(true);
}

//////////////////////////////////////////////////////////////////////////////
//  Sets the Digit units
//////////////////////////////////////////////////////////////////////////////
void CFreqCtrl::setUnits(FUNITS units)
{
    switch(units)
    {
        case UNITS_HZ:
            m_DecPos = 0;
            m_UnitString = "Hz ";
            break;
        case UNITS_KHZ:
            m_DecPos = 3;
            m_UnitString = "kHz";
            break;
        case UNITS_MHZ:
            m_DecPos = 6;
            m_UnitString = "MHz";
            break;
        case UNITS_GHZ:
            m_DecPos = 9;
            m_UnitString = "GHz";
            break;
        case UNITS_SEC:
            m_DecPos = 6;
            m_UnitString = "Sec";
            break;
        case UNITS_MSEC:
            m_DecPos = 3;
            m_UnitString = "mS ";
            break;
        case UNITS_USEC:
            m_DecPos = 0;
            m_UnitString = "uS ";
            break;
        case UNITS_NSEC:
            m_DecPos = 0;
            m_UnitString = "nS ";
            break;
    }
    m_UpdateAll = true;
    updateCtrl(true);
}

//////////////////////////////////////////////////////////////////////////////
//  Sets the Background color
//////////////////////////////////////////////////////////////////////////////
void CFreqCtrl::setBkColor(QColor cr)
{
    m_UpdateAll = true;
    m_BkColor = cr;

    for (int i = m_DigStart; i < m_NumDigits; i++)
        m_DigitInfo[i].modified = true;

    updateCtrl(true);
}

//////////////////////////////////////////////////////////////////////////////
//  Sets the Units text color
//////////////////////////////////////////////////////////////////////////////
void CFreqCtrl::setUnitsColor(QColor cr)
{
    m_UpdateAll = true;
    m_UnitsColor = cr;
    updateCtrl(true);
}

//////////////////////////////////////////////////////////////////////////////
//  Sets the Mouse edit selection text background color
//////////////////////////////////////////////////////////////////////////////
void CFreqCtrl::setHighlightColor(QColor cr)
{
    m_UpdateAll = true;
    m_HighlightColor = cr;
    updateCtrl(true);
}


//////////////////////////////////////////////////////////////////////////////
//  call to force control to redraw
//////////////////////////////////////////////////////////////////////////////
void CFreqCtrl::updateCtrl(bool all)
{
    if (all)
    {
        m_UpdateAll = true;
        for (int i = m_DigStart; i < m_NumDigits; i++)
            m_DigitInfo[i].modified = true;
    }
    update();
}


/////////////////////////////////////////////////////////////////////
//  Various Event overrides
/////////////////////////////////////////////////////////////////////
void CFreqCtrl::resizeEvent(QResizeEvent* )
{
//qDebug() <<rect.width() << rect.height();
    m_Pixmap = QPixmap(size());     //resize pixmap to current control size
    m_Pixmap.fill(m_BkColor);
    m_UpdateAll = true;
}

void CFreqCtrl::leaveEvent( QEvent *  )
{   //called when mouse cursor leaves this control so deactivate any highlights
    if(m_ActiveEditDigit>=0)
    {
        if( m_DigitInfo[m_ActiveEditDigit].editmode )
        {
            m_DigitInfo[m_ActiveEditDigit].editmode = false;
            m_DigitInfo[m_ActiveEditDigit].modified = true;
            m_ActiveEditDigit = -1;
            updateCtrl(false);
        }
    }
}

/////////////////////////////////////////////////////////////////////
//  main draw event for this control
/////////////////////////////////////////////////////////////////////
void CFreqCtrl::paintEvent(QPaintEvent *)
{
    QPainter painter(&m_Pixmap);

    if (m_UpdateAll) //if need to redraw everything
    {
        drawBkGround(painter);
        m_UpdateAll = false;
    }
    // draw any modified digits to the m_MemDC
    drawDigits(painter);
    //now draw pixmap onto screen
    QPainter scrnpainter(this);
    scrnpainter.drawPixmap(0,0,m_Pixmap);   //blt to the screen(flickers like a candle, why?)
}

/////////////////////////////////////////////////////////////////////
//  Mouse Event overrides
/////////////////////////////////////////////////////////////////////
void CFreqCtrl::mouseMoveEvent(QMouseEvent * event)
{
QPoint pt = event->pos();
    //find which digit is to be edited
    if (isActiveWindow())
    {
        if (!hasFocus())
            setFocus(Qt::MouseFocusReason);

        for (int i = m_DigStart; i < m_NumDigits; i++)
        {
            if (inRect(m_DigitInfo[i].dQRect, pt))
            {
                if (!m_DigitInfo[i].editmode)
                {
                    m_DigitInfo[i].editmode = true;
                    m_ActiveEditDigit = i;
                }
            }
            else
            {       //un-highlight the previous digit if moved off it
                if (m_DigitInfo[i].editmode)
                {
                    m_DigitInfo[i].editmode = false;
                    m_DigitInfo[i].modified = true;
                }
            }
        }

        updateCtrl(false);
    }
}

//////////////////////////////////////////////////////////////////////////////
//  Service mouse button clicks to inc or dec the selected frequency
//////////////////////////////////////////////////////////////////////////////
void CFreqCtrl::mousePressEvent(QMouseEvent * event)
{
    QPoint pt = event->pos();
    if (event->button() == Qt::LeftButton)
    {
        for (int i = m_DigStart; i < m_NumDigits; i++)
        {
            if (inRect(m_DigitInfo[i].dQRect, pt))    //if in i'th digit
            {
                if (m_LRMouseFreqSel)
                {
                    incFreq();
                }
                else
                {
                    if (pt.y() < m_DigitInfo[i].dQRect.bottom()/2)   //top half?
                        incFreq();
                    else
                        decFreq();          //bottom half
                }
            }
        }
    }
    else if (event->button() == Qt::RightButton)
    {
        for (int i = m_DigStart; i < m_NumDigits; i++)
        {
            if (inRect(m_DigitInfo[i].dQRect, pt))    //if in i'th digit
            {
                if (m_LRMouseFreqSel)
                {
                    decFreq();
                }
                else
                {
                    clearFreq();
                }
            }
        }
    }
}

/////////////////////////////////////////////////////////////////////
//  Mouse Wheel Event overrides
/////////////////////////////////////////////////////////////////////
void CFreqCtrl::wheelEvent(QWheelEvent * event)
{
    QPoint pt = event->pos();
    int numDegrees = event->delta() / 8;
    int numSteps = numDegrees / 15;

    for (int i = m_DigStart; i < m_NumDigits; i++)
    {
        if (inRect(m_DigitInfo[i].dQRect, pt))    //if in i'th digit
        {
            if (numSteps > 0)
                incFreq();
            else if (numSteps < 0)
                decFreq();
        }
    }
}


/////////////////////////////////////////////////////////////////////
//  Keyboard Event overrides
/////////////////////////////////////////////////////////////////////
void CFreqCtrl::keyPressEvent( QKeyEvent * event )
{
    //call base class if dont over ride key
    bool fSkipMsg = false;
    qint64 tmp;

    //qDebug() <<event->key();

    switch(event->key())
    {
        case Qt::Key_0:
        case Qt::Key_1:
        case Qt::Key_2:
        case Qt::Key_3:
        case Qt::Key_4:
        case Qt::Key_5:
        case Qt::Key_6:
        case Qt::Key_7:
        case Qt::Key_8:
        case Qt::Key_9:
            if (m_ActiveEditDigit >= 0)
            {
                if (m_DigitInfo[m_ActiveEditDigit].editmode)
                {
                    tmp = (m_freq/m_DigitInfo[m_ActiveEditDigit].weight) % 10;
                    m_freq -= tmp*m_DigitInfo[m_ActiveEditDigit].weight;
                    m_freq = m_freq+(event->key()-'0')*m_DigitInfo[m_ActiveEditDigit].weight;
                    setFrequency(m_freq);
                }
            }
            moveCursorRight();
            fSkipMsg = true;
            break;
        case Qt::Key_Left:
            if (m_ActiveEditDigit != -1)
            {
                moveCursorLeft();
                fSkipMsg = true;
            }
            break;
        case Qt::Key_Up:
            if (m_ActiveEditDigit != -1)
            {
                incFreq();
                fSkipMsg = true;
            }
            break;
        case Qt::Key_Down:
            if (m_ActiveEditDigit != -1)
            {
                decFreq();
                fSkipMsg = true;
            }
            break;
        case Qt::Key_Right:
            if (m_ActiveEditDigit != -1)
            {
                moveCursorRight();
                fSkipMsg = true;
            }
            break;
        case Qt::Key_Home:
            cursorHome();
            fSkipMsg = true;
            break;
        case Qt::Key_End:
            cursorEnd();
            fSkipMsg = true;
            break;
        default:
            break;
    }
    if (!fSkipMsg)
        QFrame::keyPressEvent(event);
}


//////////////////////////////////////////////////////////////////////////////
//  Calculates all the rectangles for the digits, separators, and units text
//    and creates the fonts for them.
//////////////////////////////////////////////////////////////////////////////
void CFreqCtrl::drawBkGround(QPainter &Painter)
{
    QRect rect(0, 0, width(), height());

    //qDebug() <<rect;
    int cellwidth = 100*rect.width()/(100*(m_NumDigits+2)+(m_NumSeps*SEPRATIO_N)/SEPRATIO_D);
    int sepwidth = (SEPRATIO_N*cellwidth)/(100*SEPRATIO_D);
    //qDebug() <<cellwidth <<sepwidth;

    m_UnitsRect.setRect(rect.right()-2*cellwidth,
                        rect.top(),
                        2*cellwidth,
                        rect.height());
    Painter.fillRect(m_UnitsRect, m_BkColor);

    // draw units text
    m_UnitsFont.setPixelSize((UNITS_SIZE_PERCENT*rect.height())/100);
    m_UnitsFont.setFamily("Arial");
    Painter.setFont(m_UnitsFont );
    Painter.setPen(m_UnitsColor);
    Painter.drawText(m_UnitsRect, Qt::AlignHCenter|Qt::AlignVCenter, m_UnitString);

    m_DigitFont.setPixelSize((DIGIT_SIZE_PERCENT*rect.height())/100);
    m_DigitFont.setFamily("Arial");
    Painter.setFont(m_DigitFont);
    Painter.setPen(m_DigitColor);

    int digpos = rect.right() - 2*cellwidth - 1;    //starting digit x position
    for (int i = m_DigStart; i < m_NumDigits; i++)
    {
        if ((i > m_DigStart) && ((i%3)==0))
        {
            m_SepRect[i].setCoords(digpos - sepwidth,
                                   rect.top(),
                                   digpos,
                                   rect.bottom());
            Painter.fillRect(m_SepRect[i], m_BkColor);
            digpos -= sepwidth;
            if (i==m_DecPos)
                Painter.drawText(m_SepRect[i], Qt::AlignHCenter|Qt::AlignVCenter, ".");
            else
                if (i>m_DecPos && i<m_LeadZeroPos)
                    Painter.drawText(m_SepRect[i], Qt::AlignHCenter|Qt::AlignVCenter, ",");
                else
                    if (i<m_LeadZeroPos)
                        Painter.drawText(m_SepRect[i], Qt::AlignHCenter|Qt::AlignVCenter, " ");
        }
        else
        {
            m_SepRect[i].setCoords(0, 0, 0, 0);
        }
        m_DigitInfo[i].dQRect.setCoords(digpos - cellwidth,
                                        rect.top(),
                                        digpos,
                                        rect.bottom());
        digpos -= cellwidth;
    }

}

//////////////////////////////////////////////////////////////////////////////
//  Draws just the Digits that have been modified
//////////////////////////////////////////////////////////////////////////////
void CFreqCtrl::drawDigits(QPainter &Painter)
{
    Painter.setFont(m_DigitFont);
    m_FirstEditableDigit = m_DigStart;

    for (int i = m_DigStart; i < m_NumDigits; i++)
    {
        if (m_DigitInfo[i].incval == 0)
            m_FirstEditableDigit++;

        if (m_DigitInfo[i].modified || m_DigitInfo[i].editmode)
        {
            if (m_DigitInfo[i].editmode && m_DigitInfo[i].incval != 0)
                Painter.fillRect(m_DigitInfo[i].dQRect, m_HighlightColor);
            else
                Painter.fillRect(m_DigitInfo[i].dQRect, m_BkColor);

            if (i >= m_LeadZeroPos)
                Painter.setPen(m_BkColor);
            else
                Painter.setPen(m_DigitColor);

            if (m_freq < 0 && i == m_LeadZeroPos - 1 && m_DigitInfo[i].val == 0)
                Painter.drawText(m_DigitInfo[i].dQRect, Qt::AlignHCenter|Qt::AlignVCenter,
                                 QString("-0"));
            else
                Painter.drawText(m_DigitInfo[i].dQRect, Qt::AlignHCenter|Qt::AlignVCenter,
                                 QString().number(m_DigitInfo[i].val));
            m_DigitInfo[i].modified = false;
        }
    }
}

//////////////////////////////////////////////////////////////////////////////
//  Increment just the digit active in edit mode
//////////////////////////////////////////////////////////////////////////////
void CFreqCtrl::incDigit()
{
    /** FIXME: no longer used? */
    int tmp;
    qint64 tmpl;

    if (m_ActiveEditDigit >= 0)
    {
        if (m_DigitInfo[m_ActiveEditDigit].editmode)
        {
            if (m_DigitInfo[m_ActiveEditDigit].weight == m_DigitInfo[m_ActiveEditDigit].incval)
            {
                // get the current digit value
                tmp = (int)((m_freq/m_DigitInfo[m_ActiveEditDigit].weight) % 10);
                // set the current digit value to zero
                m_freq -= tmp*m_DigitInfo[m_ActiveEditDigit].weight;
                tmp++;
                if (tmp > 9)
                    tmp = 0;
                m_freq = m_freq + (qint64)tmp*m_DigitInfo[m_ActiveEditDigit].weight;
            }
            else
            {
                tmp = (int)((m_freq/m_DigitInfo[m_ActiveEditDigit+1].weight) % 10);
                tmpl = m_freq + m_DigitInfo[m_ActiveEditDigit].incval;
                if (tmp != (int)((tmpl/m_DigitInfo[m_ActiveEditDigit+1].weight) % 10))
                {
                    tmpl -= m_DigitInfo[m_ActiveEditDigit+1].weight;
                }
                m_freq = tmpl;
            }
            setFrequency(m_freq);
        }
    }
}

//////////////////////////////////////////////////////////////////////////////
//  Increment the frequency by this digit active in edit mode
//////////////////////////////////////////////////////////////////////////////
void CFreqCtrl::incFreq()
{
    if (m_ActiveEditDigit >= 0)
    {
        if (m_DigitInfo[m_ActiveEditDigit].editmode)
        {
            m_freq += m_DigitInfo[m_ActiveEditDigit].incval;
            if (m_ResetLowerDigits) {
                /* Set digits below the active one to 0 */
                m_freq = m_freq - m_freq%m_DigitInfo[m_ActiveEditDigit].weight;
            }
            setFrequency(m_freq);
            m_LastEditDigit = m_ActiveEditDigit;
        }
    }
}

//////////////////////////////////////////////////////////////////////////////
//  Decrement the digit active in edit mode
//////////////////////////////////////////////////////////////////////////////
void CFreqCtrl::decDigit()
{
    /** FIXME: no longer used? */
    int tmp;
    qint64 tmpl;

    if (m_ActiveEditDigit >= 0)
    {
        if (m_DigitInfo[m_ActiveEditDigit].editmode)
        {
            if (m_DigitInfo[m_ActiveEditDigit].weight == m_DigitInfo[m_ActiveEditDigit].incval)
            {
                // get the current digit value
                tmp = (int)((m_freq/m_DigitInfo[m_ActiveEditDigit].weight) % 10);
                // set the current digit value to zero
                m_freq -= tmp*m_DigitInfo[m_ActiveEditDigit].weight;
                tmp--;
                if (tmp < 0)
                    tmp = 9;
                m_freq = m_freq+(qint64)tmp*m_DigitInfo[m_ActiveEditDigit].weight;
            }
            else
            {
                tmp = (int)((m_freq/m_DigitInfo[m_ActiveEditDigit+1].weight) % 10);
                tmpl = m_freq - m_DigitInfo[m_ActiveEditDigit].incval;
                if (tmp != (int)((tmpl/m_DigitInfo[m_ActiveEditDigit+1].weight)%10))
                {
                    tmpl += m_DigitInfo[m_ActiveEditDigit+1].weight;
                }
                m_freq = tmpl;
            }
            setFrequency(m_freq);
        }
    }
}

//////////////////////////////////////////////////////////////////////////////
//  Decrement the frequency by this digit active in edit mode
//////////////////////////////////////////////////////////////////////////////
void CFreqCtrl::decFreq()
{
    if (m_ActiveEditDigit >= 0)
    {
        if (m_DigitInfo[m_ActiveEditDigit].editmode)
        {
            m_freq -= m_DigitInfo[m_ActiveEditDigit].incval;
            if (m_ResetLowerDigits)
            {
                /* digits below the active one are reset to 0 */
                m_freq = m_freq - m_freq%m_DigitInfo[m_ActiveEditDigit].weight;
            }

            setFrequency(m_freq);
            m_LastEditDigit = m_ActiveEditDigit;
        }
    }
}

//////////////////////////////////////////////////////////////////////////////
//  Clear the selected digit and the digits below (i.e. set them to 0)
//////////////////////////////////////////////////////////////////////////////
void CFreqCtrl::clearFreq()
{
    if (m_ActiveEditDigit >= 0)
    {
        if (m_DigitInfo[m_ActiveEditDigit].editmode)
        {
            m_freq -= m_DigitInfo[m_ActiveEditDigit].val *
                    m_DigitInfo[m_ActiveEditDigit].incval;

            /* digits below the active one are reset to 0 */
            m_freq -= m_freq % m_DigitInfo[m_ActiveEditDigit].weight;

            setFrequency(m_freq);
            m_LastEditDigit = m_ActiveEditDigit;
        }
    }

}

/////////////////////////////////////////////////////////////////////
//  Cursor move routines for arrow key editing
/////////////////////////////////////////////////////////////////////
void CFreqCtrl::moveCursorLeft()
{
    if ((m_ActiveEditDigit >= 0) && (m_ActiveEditDigit < m_NumDigits-1))
    {
        cursor().setPos(mapToGlobal(m_DigitInfo[++m_ActiveEditDigit].dQRect.center()));
    }
}

void CFreqCtrl::moveCursorRight()
{
    if (m_ActiveEditDigit > m_FirstEditableDigit)
    {
        cursor().setPos(mapToGlobal(m_DigitInfo[--m_ActiveEditDigit].dQRect.center()));
    }
}

void CFreqCtrl::cursorHome()
{
    if (m_ActiveEditDigit >= 0)
    {
        cursor().setPos(mapToGlobal( m_DigitInfo[m_NumDigits-1].dQRect.center()));
    }
}

void CFreqCtrl::cursorEnd()
{
    if (m_ActiveEditDigit > 0)
    {
        cursor().setPos(mapToGlobal(m_DigitInfo[m_FirstEditableDigit].dQRect.center()));
    }
}