[8058cd]: libgui / ScaleWidget.cpp Maximize Restore History

Download this file

ScaleWidget.cpp    254 lines (208 with data), 7.4 kB

  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
/***************************************************************************
ScaleWidget.cpp - widget for drawing a scale under an image
-------------------
begin : Sep 18 2001
copyright : (C) 2001 by Thomas Eschenbacher
email : Thomas Eschenbacher <thomas.eschenbacher@gmx.de>
***************************************************************************/
/***************************************************************************
* *
* 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "config.h"
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <QtGlobal>
// #include <QImage>
// #include <QDir>
#include <QPainter>
#include <QPaintEvent>
#include <kiconloader.h>
#include <kstandarddirs.h>
#include "ScaleWidget.h"
#define FONTSIZE 6
//***************************************************************************
ScaleWidget::ScaleWidget(QWidget *parent)
:QWidget(parent), m_low(0), m_high(100), m_logmode(false),
m_unittext("%")
{
KIconLoader icon_loader;
m_scalefont = icon_loader.loadIcon("minifont.xpm", KIconLoader::Small);
}
//***************************************************************************
ScaleWidget::ScaleWidget(QWidget *parent, int low, int high,
const QString &unit)
:QWidget(parent), m_low(low), m_high(high), m_logmode(false),
m_unittext(unit)
{
KIconLoader icon_loader;
m_scalefont = icon_loader.loadIcon("minifont.xpm", KIconLoader::Small);
}
//***************************************************************************
ScaleWidget::~ScaleWidget()
{
}
//***************************************************************************
void ScaleWidget::setUnit(const QString &text)
{
m_unittext = text;
repaint();
}
//***************************************************************************
void ScaleWidget::setLogMode(bool log)
{
if (m_logmode == log) return;
m_logmode = log;
repaint();
}
//***************************************************************************
void ScaleWidget::setMinMax(int min, int max)
{
if ((m_low == min) && (m_high == max)) return;
m_low = min;
m_high = max;
repaint();
}
//***************************************************************************
void ScaleWidget::paintText(QPainter &p, int x, int y, int ofs,
bool reverse, const QString &text)
{
int len = text.length();
int pos; // position within the font bitmap
if (reverse) x -= ofs;
for (int i = 0; i < len; i++) {
pos = (reverse) ? (len-1-i) : i;
int c = text.at(pos).toLatin1();
pos = 40; // default = space
switch (c) {
case '\'': pos = 36; break;
case '"': pos = 37; break;
case 176: pos = 38; break;
case '.': pos = 39; break;
case ' ': pos = 40; break;
case '%': pos = 41; break;
case '-': pos = 42; break;
default:
if ((c > 64) && (c < 91)) pos = c-65; // letter
if ((c > 96) && (c < 123)) pos = c-97; // letter
if ((c > 47) && (c < 58)) pos = c-48+26; // number
}
p.drawPixmap(x, y, m_scalefont, pos*FONTSIZE, 0, FONTSIZE, FONTSIZE);
x += (reverse) ? (-ofs) : (+ofs);
}
}
//***************************************************************************
void ScaleWidget::drawLog(QPainter &p, int w, int h, bool inverse)
{
// only use base 10 for now, tested with others too,
// but not configurable through a property
const int base = 10;
int dir = (inverse) ? -1 : +1;
p.setPen(palette().dark().color());
p.drawLine (0, dir*(h-1), dir*w, dir*(h-1));
p.drawLine (dir*(w-1), 0, dir*(w-1), dir*(h-1));
p.setPen(palette().text().color());
int a, x;
const int h2 = h;
Q_ASSERT(m_low >= 0);
Q_ASSERT(m_high > m_low);
int dec_lo = (m_low) ? (int)floor(log(m_low)/log(base)) : 0;
int dec_hi = (int)ceil(log(m_high)/log(base));
int decades = qAbs(dec_hi - dec_lo) + 1;
// check if we have enough space for the small lines within a decade
int w1 = (int)(w / decades); // pixels per decade
bool small_lines = (w1 - (int)((double)w1 * log(base-1)/log(base))) > 1;
// print the lines
for (a = 0; a < decades; a++) {
// big line, for each decade
x = (int)((w-1) * a / decades);
p.drawLine (dir*x, dir*1, dir*x, dir*(h2-2));
w1 = (int)((w-1) * (a+1) / decades) - x + 1;
if (small_lines) {
// small lines, within the decade
for (int i=1; i < base; i++) {
int x1 = x + (int)((double)w1 * log(i)/log(base));
p.drawLine (dir*x1, dir*1, dir*x1, dir*((h2/2)-2));
}
}
}
// print the text
for (a = 0; a < decades; a++) {
QString buf = "%1 %2";
int value = (int)pow(base, dec_lo+a);
buf = buf.arg(value).arg(m_unittext);
x = ((w-1) * a)/decades;
paintText(p, dir*(x+2), dir*(h-FONTSIZE-2), FONTSIZE, inverse, buf);
}
}
//***************************************************************************
void ScaleWidget::drawLinear(QPainter &p, int w, int h, bool inverse)
{
int dir = (inverse) ? -1 : +1;
p.setPen(palette().dark().color());
p.drawLine(0, dir*(h-1), dir*w, dir*(h-1));
p.drawLine(dir*(w-1), 0, dir*(w-1), dir*(h - 1));
p.setPen(palette().text().color());
int a, x;
double ofs;
double t = w - 1;
int h2 = h;
// print the lines
while ((t / 10 > 1) && (h2 > 0)) {
for (ofs = 0; ofs < w - 1; ofs += t) {
for (a = 0; a < 4; a++) {
x = (int)(ofs + (t * a / 4));
p.drawLine (dir*x, dir*1, dir*x, dir*(h2-2));
}
}
h2 >>= 1;
t /= 4;
}
// print the text
for (a = 0; a < 4; a++) {
QString buf = "%1 %2";
int value = m_low + (((m_high - m_low)* (inverse ? (4-a) : a))/4);
buf = buf.arg(value).arg(m_unittext);
x = ((w-1) * a)/4;
paintText(p, dir*(x+2), dir*(h-FONTSIZE-2), FONTSIZE, inverse, buf);
}
}
//***************************************************************************
void ScaleWidget::paintEvent(QPaintEvent *)
{
bool inverse = false;
int h = height();
int w = width();
QPainter p;
p.begin(this);
p.save();
p.setPen(palette().light().color());
p.drawLine(0, 0, w, 0);
if (h > w) {
p.setWindow(-w, 0, w, h);
p.rotate(-90);
h = width();
w = height();
inverse = true;
}
(m_logmode) ? drawLog(p, w, h, inverse) : drawLinear(p, w, h, inverse);
p.restore();
p.end();
}
//***************************************************************************
QSize ScaleWidget::sizeHint() const
{
return QSize(4*FONTSIZE, 4*FONTSIZE);
}
//***************************************************************************
QSize ScaleWidget::minimumSize() const
{
return QSize(5*2*FONTSIZE, 5*2*FONTSIZE);
}
//***************************************************************************
//***************************************************************************