kwave  18.07.70
Kwave::ScaleWidget Class Reference

#include <ScaleWidget.h>

Inheritance diagram for Kwave::ScaleWidget:
Inheritance graph
Collaboration diagram for Kwave::ScaleWidget:
Collaboration graph

Public Member Functions

 ScaleWidget (QWidget *parent)
 
 ScaleWidget (QWidget *parent, int low, int high, const QString &unit)
 
virtual ~ScaleWidget () Q_DECL_OVERRIDE
 
void setMinMax (int min, int max)
 
void setUnit (const QString &text)
 
void setLogMode (bool log)
 
virtual QSize minimumSize () const
 
virtual QSize sizeHint () const Q_DECL_OVERRIDE
 

Protected Member Functions

virtual void paintEvent (QPaintEvent *) Q_DECL_OVERRIDE
 
void drawLinear (QPainter &p, int w, int h, bool inverse)
 
void drawLog (QPainter &p, int w, int h, bool inverse)
 
void paintText (QPainter &p, int x, int y, bool reverse, const QString &text)
 

Private Attributes

int m_low
 
int m_high
 
bool m_logmode
 
QString m_unittext
 

Detailed Description

Definition at line 33 of file ScaleWidget.h.

Constructor & Destructor Documentation

◆ ScaleWidget() [1/2]

Kwave::ScaleWidget::ScaleWidget ( QWidget *  parent)
explicit

Primitve constructor for usage in a Qt designer's dialog

Parameters
parentthe widget's parent widget

Definition at line 37 of file ScaleWidget.cpp.

38  :QWidget(parent), m_low(0), m_high(100), m_logmode(false),
39  m_unittext(_("%"))
40 {
41 }
#define _(m)
Definition: memcpy.c:66

◆ ScaleWidget() [2/2]

Kwave::ScaleWidget::ScaleWidget ( QWidget *  parent,
int  low,
int  high,
const QString &  unit 
)

Constructor with initialization.

Parameters
parentthe widget's parent widget
lowleft/lower border value
highright/upper border value
unittext of the units to show

Definition at line 44 of file ScaleWidget.cpp.

46  :QWidget(parent), m_low(low), m_high(high), m_logmode(false),
47  m_unittext(unit)
48 {
49 }

◆ ~ScaleWidget()

Kwave::ScaleWidget::~ScaleWidget ( )
virtual

Destructor

Definition at line 52 of file ScaleWidget.cpp.

53 {
54 }

Member Function Documentation

◆ drawLinear()

void Kwave::ScaleWidget::drawLinear ( QPainter &  p,
int  w,
int  h,
bool  inverse 
)
protected

Draws a linear scale

Parameters
preference to the painter
wwidth of the drawing area in pixels
hheight of the drawing area in pixels
inverseof true, the coordinate system is rotated to be upside-down and the scale has to be drawn mirrored in x and y axis.

Definition at line 164 of file ScaleWidget.cpp.

References _, FONTSIZE, m_high, m_low, m_unittext, paintText(), and Kwave::toInt().

Referenced by paintEvent().

165 {
166  int dir = (inverse) ? -1 : +1;
167 
168  p.setPen(palette().dark().color());
169  p.drawLine(0, dir * (h - 1), dir * w, dir * (h - 1));
170  p.drawLine(dir * (w - 1), 0, dir * (w - 1), dir * (h - 1));
171 
172  p.setPen(palette().text().color());
173 
174  int a, x;
175  double ofs;
176  double t = w - 1;
177  int h2 = h;
178 
179  // print the lines
180  while ((t / 10 > 1) && (h2 > 0)) {
181  for (ofs = 0; ofs < w - 1; ofs += t) {
182  for (a = 0; a < 4; ++a) {
183  x = Kwave::toInt(ofs + (t * a / 4));
184  p.drawLine (dir * x, dir * 1, dir * x, dir * (h2 - 2));
185  }
186  }
187  h2 >>= 1;
188  t /= 4;
189  }
190 
191  // print the text
192  for (a = 0; a < 4; ++a) {
193  QString buf = _("%1 %2");
194  int value = m_low + (((m_high - m_low)* (inverse ? (4 - a) : a)) / 4);
195  buf = buf.arg(value).arg(m_unittext);
196  x = ((w - 1) * a) / 4;
197  paintText(p, dir * (x + 4), dir * (h - FONTSIZE - 4), inverse, buf);
198  }
199 
200 }
#define FONTSIZE
Definition: ScaleWidget.cpp:34
int toInt(T x)
Definition: Utils.h:127
#define _(m)
Definition: memcpy.c:66
void paintText(QPainter &p, int x, int y, bool reverse, const QString &text)
Definition: ScaleWidget.cpp:81
Here is the call graph for this function:
Here is the caller graph for this function:

◆ drawLog()

void Kwave::ScaleWidget::drawLog ( QPainter &  p,
int  w,
int  h,
bool  inverse 
)
protected
Todo:
implementation of logarithmic scale

Definition at line 107 of file ScaleWidget.cpp.

References _, FONTSIZE, m_high, m_low, m_unittext, paintText(), and Kwave::toInt().

Referenced by paintEvent().

108 {
109  // only use base 10 for now, tested with others too,
110  // but not configurable through a property
111  const int base = 10;
112 
113  int dir = (inverse) ? -1 : +1;
114 
115  p.setPen(palette().dark().color());
116  p.drawLine (0, dir*(h-1), dir*w, dir*(h-1));
117  p.drawLine (dir*(w-1), 0, dir*(w-1), dir*(h-1));
118 
119  p.setPen(palette().text().color());
120 
121  int a, x;
122  const int h2 = h;
123 
124  Q_ASSERT(m_low >= 0);
125  Q_ASSERT(m_high > m_low);
126 
127  int dec_lo = (m_low) ? Kwave::toInt(floor(log(m_low)/log(base))) : 0;
128  int dec_hi = Kwave::toInt(ceil(log(m_high)/log(base)));
129  int decades = qAbs(dec_hi - dec_lo) + 1;
130 
131  // check if we have enough space for the small lines within a decade
132  int w1 = Kwave::toInt(w / decades); // pixels per decade
133  bool small_lines = (w1 - Kwave::toInt(
134  static_cast<double>(w1) * log(base-1)/log(base))) > 1;
135 
136  // print the lines
137  for (a = 0; a < decades; ++a) {
138  // big line, for each decade
139  x = Kwave::toInt((w-1) * a / decades);
140  p.drawLine (dir * x, dir * 1, dir * x, dir * (h2 - 2));
141 
142  w1 = Kwave::toInt((w - 1) * (a + 1) / decades) - x + 1;
143  if (small_lines) {
144  // small lines, within the decade
145  for (int i = 1; i < base; i++) {
146  int x1 = x + Kwave::toInt(static_cast<double>(w1) *
147  log(i) / log(base));
148  p.drawLine (dir * x1, dir * 1, dir * x1, dir * ((h2 / 2) - 2));
149  }
150  }
151  }
152 
153  // print the text
154  for (a = 0; a < decades; ++a) {
155  QString buf = _("%1 %2");
156  int value = Kwave::toInt(pow(base, dec_lo + a));
157  buf = buf.arg(value).arg(m_unittext);
158  x = ((w - 1) * a) / decades;
159  paintText(p, dir * (x + 4), dir * (h - FONTSIZE - 4), inverse, buf);
160  }
161 }
#define FONTSIZE
Definition: ScaleWidget.cpp:34
int toInt(T x)
Definition: Utils.h:127
#define _(m)
Definition: memcpy.c:66
void paintText(QPainter &p, int x, int y, bool reverse, const QString &text)
Definition: ScaleWidget.cpp:81
Here is the call graph for this function:
Here is the caller graph for this function:

◆ minimumSize()

QSize Kwave::ScaleWidget::minimumSize ( ) const
virtual

minimum size of the widtget,

See also
QWidget::minimumSize()

Definition at line 237 of file ScaleWidget.cpp.

References FONTSIZE.

238 {
239  return QSize(5 * 2 * FONTSIZE, 5 * 2 * FONTSIZE);
240 }
#define FONTSIZE
Definition: ScaleWidget.cpp:34

◆ paintEvent()

void Kwave::ScaleWidget::paintEvent ( QPaintEvent *  )
protectedvirtual

Draws the widget.

See also
QWidget::paintEvent

Definition at line 203 of file ScaleWidget.cpp.

References drawLinear(), drawLog(), and m_logmode.

204 {
205  bool inverse = false;
206  int h = height();
207  int w = width();
208  QPainter p;
209 
210  p.begin(this);
211  p.save();
212  p.setPen(palette().light().color());
213 
214  p.drawLine(0, 0, w, 0);
215  if (h > w) {
216  p.setWindow(-w, 0, w, h);
217  p.rotate(-90);
218  h = width();
219  w = height();
220 
221  inverse = true;
222  }
223 
224  (m_logmode) ? drawLog(p, w, h, inverse) : drawLinear(p, w, h, inverse);
225 
226  p.restore();
227  p.end();
228 }
void drawLog(QPainter &p, int w, int h, bool inverse)
void drawLinear(QPainter &p, int w, int h, bool inverse)
Here is the call graph for this function:

◆ paintText()

void Kwave::ScaleWidget::paintText ( QPainter &  p,
int  x,
int  y,
bool  reverse,
const QString &  text 
)
protected

Painting routine for own small font with fixed size There are Problems with smaller displays using QFont, sizes are not correct.

Parameters
preference to the painter
xcoordinate of the left edge of the first character
ycoordinate of the lower edge of the first character
reverseif true, print reverse: x is right edge of the text, like "align right".
textthe text to be printed. Must only contain known characters that are present in the font bitmap, like numbers, letters and some special chars like "%", space, dot and comma.

Definition at line 81 of file ScaleWidget.cpp.

References FONTSIZE, and rect().

Referenced by drawLinear(), and drawLog().

83 {
84  QFont font;
85  font.setStyleHint(QFont::SansSerif);
86  font.setFixedPitch(true);
87  font.setPixelSize(FONTSIZE);
88  font.setWeight(0);
89  font.setStyle(QFont::StyleNormal);
90  p.setFont(font);
91  QFontMetrics fm(font);
92 
93  if (reverse) {
94  x += FONTSIZE + 2;
95  y -= FONTSIZE + 2;
96  }
97 
98  QRect rect = fm.boundingRect(text);
99  const int th = rect.height();
100  const int tw = rect.width();
101  p.drawText(x, y, tw, th,
102  ((reverse) ? Qt::AlignLeft : Qt::AlignRight) | Qt::AlignBottom,
103  text);
104 }
#define FONTSIZE
Definition: ScaleWidget.cpp:34
static double rect(double param)
Definition: Functions.cpp:29
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setLogMode()

void Kwave::ScaleWidget::setLogMode ( bool  log)

Sets logarithmic or linear mode.

Parameters
logif true, set logarithmic mode, if not select linear mode

Definition at line 64 of file ScaleWidget.cpp.

References m_logmode.

65 {
66  if (m_logmode == log) return;
67  m_logmode = log;
68  repaint();
69 }

◆ setMinMax()

void Kwave::ScaleWidget::setMinMax ( int  min,
int  max 
)

Sets the border values.

Parameters
minleft/lower border value
maxright/upper border value

Definition at line 72 of file ScaleWidget.cpp.

References m_high, and m_low.

Referenced by Kwave::SonagramWindow::updateScaleWidgets().

73 {
74  if ((m_low == min) && (m_high == max)) return;
75  m_low = min;
76  m_high = max;
77  repaint();
78 }
Here is the caller graph for this function:

◆ setUnit()

void Kwave::ScaleWidget::setUnit ( const QString &  text)

Set the text of the units.

Parameters
textthe units to show

Definition at line 57 of file ScaleWidget.cpp.

References m_unittext.

58 {
59  m_unittext = text;
60  repaint();
61 }

◆ sizeHint()

QSize Kwave::ScaleWidget::sizeHint ( ) const
virtual

optimal size for the widget,

See also
QWidget::sizeHint()

Definition at line 231 of file ScaleWidget.cpp.

References FONTSIZE.

Referenced by Kwave::SonagramWindow::SonagramWindow().

232 {
233  return QSize(4 * FONTSIZE, 4 * FONTSIZE);
234 }
#define FONTSIZE
Definition: ScaleWidget.cpp:34
Here is the caller graph for this function:

Member Data Documentation

◆ m_high

int Kwave::ScaleWidget::m_high
private

Upper boundary value

Definition at line 128 of file ScaleWidget.h.

Referenced by drawLinear(), drawLog(), and setMinMax().

◆ m_logmode

bool Kwave::ScaleWidget::m_logmode
private

If true, logarithmic mode, linear mode if false

Definition at line 131 of file ScaleWidget.h.

Referenced by paintEvent(), and setLogMode().

◆ m_low

int Kwave::ScaleWidget::m_low
private

Lower boundary value

Definition at line 125 of file ScaleWidget.h.

Referenced by drawLinear(), drawLog(), and setMinMax().

◆ m_unittext

QString Kwave::ScaleWidget::m_unittext
private

String containing the name of the unit

Definition at line 134 of file ScaleWidget.h.

Referenced by drawLinear(), drawLog(), and setUnit().


The documentation for this class was generated from the following files: