File: scale.cpp

package info (click to toggle)
sdrangelove 0.0.1.20140824-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,560 kB
  • ctags: 2,668
  • sloc: cpp: 17,760; ansic: 142; makefile: 17; sh: 1
file content (129 lines) | stat: -rw-r--r-- 4,215 bytes parent folder | download | duplicates (3)
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
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany //
// written by Christian Daniel                                                   //
//                                                                               //
// 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 as version 3 of the License, or                  //
//                                                                               //
// 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 V3 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 <QPainter>
#include "gui/scale.h"

Scale::Scale(QWidget* parent) :
	QWidget(parent)
{
}

void Scale::setOrientation(Qt::Orientation orientation)
{
	m_orientation = orientation;
	m_scaleEngine.setOrientation(orientation);
	m_scaleEngine.setFont(font());
	QFontMetrics fm(font());
	switch(m_orientation) {
		case Qt::Horizontal:
			m_scaleEngine.setSize(width());
			setMinimumWidth(0);
			setMaximumWidth(QWIDGETSIZE_MAX);
			setMinimumHeight(3 + fontMetrics().ascent());
			setMaximumHeight(3 + fontMetrics().ascent());
			break;
		case Qt::Vertical:
			m_scaleEngine.setSize(height());
			setMinimumWidth(30);
			setMaximumWidth(30);
			setMinimumHeight(0);
			setMaximumHeight(QWIDGETSIZE_MAX);
			break;
	}
}

void Scale::setRange(Unit::Physical physicalUnit, float rangeMin, float rangeMax)
{
	m_scaleEngine.setRange(physicalUnit, rangeMin, rangeMax);
	update();
}

void Scale::paintEvent(QPaintEvent*)
{
	QPainter painter(this);
	const ScaleEngine::TickList& tickList = m_scaleEngine.getTickList();
	QFontMetricsF fontMetrics(font());
	const ScaleEngine::Tick* tick;
	int i;
	float bottomLine;

	switch(m_orientation) {
		case Qt::Horizontal: {
			painter.setPen(Qt::black);

			// Zwischenlinien für x-Achse zeichnen
			for(i = 0; i < tickList.count(); i++) {
				tick = &tickList[i];
				if(!tick->major)
					painter.drawLine(QLineF(tick->pos, 0, tick->pos, 1));
			}

			// Skala am Rand zeichnen
			painter.drawLine(QLineF(0, 0, width() - 1, 0));

			// Hauptlinien und Beschriftung für x-Achse zeichnen
			for(i = 0; i < tickList.count(); i++) {
				tick = &tickList[i];
				if(tick->major) {
					painter.drawLine(QLineF(tick->pos - 1, 0, tick->pos - 1, 3));
					if(tick->textSize > 0) {
						painter.drawText(QPointF(tick->textPos, 3 + fontMetrics.ascent()), tick->text);
					}
				}
			}
			break;
		}
		case Qt::Vertical: {
			bottomLine = height() - 1;
			painter.setPen(Qt::black);

			// Zwischenlinien für y-Achse zeichnen
			for(i = 0; i < tickList.count(); i++) {
				tick = &tickList[i];
				if(!tick->major)
					painter.drawLine(QLineF(width() - 2, bottomLine - tick->pos, width() - 1, bottomLine - tick->pos));
			}

			// Skala am Rand zeichnen
			painter.drawLine(QLineF(width() - 1, 0, width() - 1, height() - 1));

			// Hauptlinien und Beschriftung für y-Achse zeichnen
			for(i = 0; i < tickList.count(); i++) {
				tick = &tickList[i];
				if(tick->major) {
					painter.drawLine(QLineF(width() - 4, bottomLine - tick->pos, width() - 1, bottomLine - tick->pos));
					if(tick->textSize > 0)
						painter.drawText(QPointF(width() - 4 - tick->textSize, bottomLine - tick->textPos), tick->text);
				}
			}

		}
	}
}

void Scale::resizeEvent(QResizeEvent*)
{
	switch(m_orientation) {
		case Qt::Horizontal:
			m_scaleEngine.setSize(width());
			break;
		case Qt::Vertical:
			m_scaleEngine.setSize(height());
			break;
	}
}