• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeutils API Reference
  • KDE Home
  • Contact Us
 

superkaramba

  • sources
  • kde-4.12
  • kdeutils
  • superkaramba
  • src
  • meters
meters/textlabel.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2003 by Hans Karlsson <karlsson.h@home.se> *
3  * Copyright (C) 2007 Matt Broadstone <mbroadst@gmail.com> *
4  * *
5  * *
6  * This program is free software; you can redistribute it and/or modify *
7  * it under the terms of the GNU General Public License as published by *
8  * the Free Software Foundation; either version 2 of the License, or *
9  * (at your option) any later version. *
10  ***************************************************************************/
11 
12 #include "textlabel.h"
13 #include "textlabel.moc"
14 
15 #include <QPainter>
16 #include <QMouseEvent>
17 #include <QGraphicsSceneMouseEvent>
18 #include <QCursor>
19 
20 #include <KRun>
21 
22 TextLabel::TextLabel(Karamba *k, int x, int y, int w, int h)
23  : Meter(k, x, y, w, h),
24  alignment(Qt::AlignLeft),
25  clip(0),
26  bgColor(0, 0, 0),
27  lineHeight(0),
28  shadow(0),
29  scrollSpeed(0, 0),
30  scrollPos(0, 0),
31  scrollGap(0),
32  scrollPause(0),
33  pauseCounter(0),
34  scrollType(ScrollNone),
35  m_clickable(false),
36  m_sizeGiven(true)
37 {
38  origPoint = QPoint(x, y);
39 
40  calculateTextSize();
41  if (h != 0 || w != 0)
42  clip = 0;
43  else
44  clip = Qt::TextDontClip;
45 
46  if (h <= 0 || w <= 0) {
47  setWidth(-1);
48  setHeight(-1);
49  m_sizeGiven = false;
50  }
51 }
52 
53 TextLabel::TextLabel(Karamba *k)
54  : Meter(k, 0, 0, 0, 0),
55  alignment(Qt::AlignLeft),
56  clip(0),
57  bgColor(0, 0, 0),
58  lineHeight(0),
59  shadow(0),
60  scrollSpeed(0, 0),
61  scrollPos(0, 0),
62  scrollGap(0),
63  scrollPause(0),
64  pauseCounter(0),
65  scrollType(ScrollNone),
66  m_clickable(false)
67 {}
68 
69 TextLabel::~TextLabel()
70 {}
71 
72 void TextLabel::show()
73 {
74  Meter::show();
75  setEnabled(true);
76 }
77 
78 void TextLabel::hide()
79 {
80  Meter::hide();
81  setEnabled(false);
82 }
83 
84 void TextLabel::setTextProps(TextField *field)
85 {
86  if (field) {
87  text = *field;
88  //lineHeight = t->getLineHeight();
89  shadow = field->getShadow();
90  alignment = field->getAlignment();
91  setFontSize(field->getFontSize());
92  setFont(field->getFont());
93 
94  setColor(field->getColor());
95  setBGColor(field->getBGColor());
96  }
97 
98  calculateTextSize();
99 }
100 
101 void TextLabel::calculateTextSize()
102 {
103  int tmp;
104 
105  prepareGeometryChange();
106 
107  QFontMetrics fm(font);
108  lineHeight = fm.height();
109  textSize.setWidth(0);
110  textSize.setHeight(lineHeight * value.count());
111  QStringList::Iterator it = value.begin();
112 
113  while (it != value.end()) {
114  tmp = fm.width(*it);
115  if (tmp > textSize.width())
116  textSize.setWidth(tmp);
117 
118  ++it;
119  }
120 
121  if ((getWidth() <= 0) || !m_sizeGiven) {
122  setWidth(textSize.width());
123  }
124 
125  if ((getHeight() <= 0) || !m_sizeGiven) {
126  setHeight(textSize.height());
127  }
128 
129  if (!m_sizeGiven) {
130  if (alignment == Qt::AlignLeft) {
131  setX(origPoint.x());
132  }
133  else if (alignment == Qt::AlignRight) {
134  setX(origPoint.x() - textSize.width());
135  }
136  else if (alignment == Qt::AlignHCenter) {
137  setX(origPoint.x() - textSize.width() / 2);
138  }
139  }
140 
141  update();
142 }
143 
144 void TextLabel::setValue(const QString &text)
145 {
146  value = text.split('\n');
147 
148  calculateTextSize();
149 }
150 
151 void TextLabel::setValue(int v)
152 {
153  value = QStringList(QString::number(v));
154 
155  calculateTextSize();
156 }
157 
158 void TextLabel::setBGColor(QColor clr)
159 {
160  bgColor = clr;
161 }
162 
163 QColor TextLabel::getBGColor() const
164 {
165  return bgColor;
166 }
167 
168 void TextLabel::setFont(const QString &f)
169 {
170  font.setFamily(f);
171  calculateTextSize();
172 }
173 
174 QString TextLabel::getFont() const
175 {
176  return font.family();
177 }
178 
179 void TextLabel::setFontSize(int size)
180 {
181  font.setPixelSize(size);
182  calculateTextSize();
183 }
184 
185 int TextLabel::getFontSize() const
186 {
187  return font.pixelSize();
188 }
189 
190 void TextLabel::setAlignment(const QString &align)
191 {
192  QString a = align.toUpper();
193  if (a == "LEFT" || a.isEmpty())
194  alignment = Qt::AlignLeft;
195  if (a == "RIGHT")
196  alignment = Qt::AlignRight;
197  if (a == "CENTER")
198  alignment = Qt::AlignHCenter;
199 
200  calculateTextSize();
201 }
202 
203 QString TextLabel::getAlignment() const
204 {
205  if (alignment == Qt::AlignHCenter)
206  return "CENTER";
207  else if (alignment == Qt::AlignRight)
208  return "RIGHT";
209  else
210  return "LEFT";
211 }
212 
213 void TextLabel::setFixedPitch(bool fp)
214 {
215  font.setFixedPitch(fp);
216 }
217 
218 bool TextLabel::getFixedPitch() const
219 {
220  return font.fixedPitch();
221 }
222 
223 void TextLabel::setShadow(int s)
224 {
225  shadow = s;
226 }
227 
228 int TextLabel::getShadow() const
229 {
230  return shadow;
231 }
232 
233 void TextLabel::setScroll(const QString &a, const QPoint &speed, int gap, int pause)
234 {
235  ScrollType t = TextLabel::ScrollNone;
236  QString scroll = a.toUpper();
237 
238  if (scroll == "NONE")
239  scroll = TextLabel::ScrollNone;
240  else if (a == "NORMAL")
241  scroll = TextLabel::ScrollNormal;
242  else if (a == "BACKANDFORTH")
243  scroll = TextLabel::ScrollBackAndForth;
244  else if (a == "ONEPASS")
245  scroll = TextLabel::ScrollOnePass;
246 
247  setScroll(t, speed, gap, pause);
248 }
249 
250 void TextLabel::setScroll(ScrollType type, QPoint speed, int gap, int pause)
251 {
252  scrollType = type;
253  scrollSpeed = speed;
254 
255  switch (scrollType) {
256  case ScrollNormal:
257  case ScrollOnePass: {
258  int x = 0, y = 0;
259 
260  if (speed.x() > 0)
261  x = -1 * textSize.width();
262  else if (speed.x() < 0)
263  x = getWidth() - 1;
264 
265  if (speed.y() > 0)
266  x = -1 * textSize.height();
267  else if (speed.y() < 0)
268  x = getHeight() - 1;
269 
270  scrollPos = QPoint(x, y);
271  break;
272  }
273 
274  case ScrollNone:
275  case ScrollBackAndForth:
276  default:
277  scrollPos = QPoint(0, 0);
278  break;
279  }
280 
281  scrollGap = gap;
282  scrollPause = pause;
283  pauseCounter = 1;
284 }
285 
286 int TextLabel::drawText(QPainter *p, int x, int y, int width, int height,
287  const QString &text)
288 {
289  if (shadow != 0) {
290  p->setPen(getBGColor());
291  p->drawText(x + shadow, y + shadow, width, height,
292  alignment | clip | Qt::TextExpandTabs, text);
293  }
294 
295  p->setPen(getColor());
296  p->drawText(x, y, width, height, alignment | clip |
297  Qt::TextExpandTabs, text);
298 
299  return 0;
300 }
301 
302 bool TextLabel::calculateScrollCoords(const QRect &meterRect, QRect &textRect,
303  QPoint &next, int x, int y)
304 {
305  if (scrollType == ScrollBackAndForth &&
306  (scrollSpeed.x() != 0 && textSize.width() < getWidth() ||
307  scrollSpeed.y() != 0 && textSize.height() < getHeight()))
308  return true;
309 
310  x += scrollPos.x();
311  y += scrollPos.y();
312 
313  if (pauseCounter < 1) {
314  scrollPos += scrollSpeed;
315 
316  // -1 | 0 | 1
317  QPoint direction(scrollSpeed.x() / abs((scrollSpeed.x() == 0) ?
318  1 : scrollSpeed.x()),
319  scrollSpeed.y() / abs((scrollSpeed.y() == 0) ?
320  1 : scrollSpeed.y()));
321  next = QPoint(-1 * direction.x() * (scrollGap + textSize.width()),
322  -1 * direction.y() * (scrollGap + textSize.height()));
323  textRect.setCoords(x, y, x + textSize.width(), y + textSize.height());
324 
325  if (scrollType == ScrollBackAndForth) {
326  if (direction.x() < 0 && textRect.right() <= meterRect.right() ||
327  direction.x() > 0 && textRect.left() >= meterRect.left()) {
328  scrollSpeed.setX(scrollSpeed.x() * -1);
329  pauseCounter = scrollPause;
330  }
331  if (direction.y() < 0 && textRect.bottom() <= meterRect.bottom() ||
332  direction.y() > 0 && textRect.top() >= meterRect.top()) {
333  scrollSpeed.setY(scrollSpeed.y() * -1);
334  pauseCounter = scrollPause;
335  }
336  } else if (!textRect.intersects(meterRect)) {
337  if (scrollType == ScrollNormal)
338  scrollPos += next;
339  else if (scrollType == ScrollOnePass)
340  return false;
341  }
342  } else
343  --pauseCounter;
344 
345  return true;
346 }
347 
348 void TextLabel::paint(QPainter *p, const QStyleOptionGraphicsItem *option,
349  QWidget *widget)
350 {
351  Q_UNUSED(option);
352  Q_UNUSED(widget);
353 
354  if (!m_hidden) {
355  int i = 0; // lineHeight;
356  int row = 1;
357  int width = Meter::getWidth();
358  int height = Meter::getHeight();
359  QRect meterRect(0, 0, width, height);
360  QRect textRect;
361  QPoint next;
362 
363  p->setFont(font);
364  if (scrollType != ScrollNone) {
365  p->setClipRect(0, 0, width, height);
366  if (!calculateScrollCoords(meterRect, textRect, next, 0, 0)) {
367  p->setClipping(false);
368  return;
369  }
370  width = textSize.width();
371  height = textSize.height();
372  }
373 
374  QStringList::Iterator it = value.begin();
375  while (it != value.end() && (row <= height || height == -1)) {
376  drawText(p, 0, 0 + i, width, height, *it);
377 
378  // Draw more instances of text if scroll type is normal scroll
379  if (scrollType == ScrollNormal) {
380  textRect.adjust(next.x(), next.y(), next.x(), next.y());
381  while (textRect.intersects(meterRect)) {
382  drawText(p, textRect.x(), textRect.y() + i, width, height, *it);
383  textRect.adjust(next.x(), next.y(), next.x(), next.y());
384  }
385  }
386 
387  i += lineHeight;
388  it++;
389  row++;
390  }
391 
392  if (scrollType != ScrollNone)
393  p->setClipping(false);
394  }
395 }
396 
397 bool TextLabel::mouseEvent(QEvent* e)
398 {
399  if (isEnabled()) {
400  Qt::MouseButtons button;
401  if (QGraphicsSceneMouseEvent *event = dynamic_cast<QGraphicsSceneMouseEvent*>(e)) {
402  button = event->button();
403  } else if (QGraphicsSceneWheelEvent *event = dynamic_cast<QGraphicsSceneWheelEvent*>(e)) {
404  button = event->buttons();
405  }
406 
407  QString program;
408  if (button == Qt::LeftButton)
409  program = m_leftMouseButtonAction;
410  else if (button == Qt::MidButton)
411  program = m_middleMouseButtonAction;
412  else if (button == Qt::RightButton)
413  program = m_rightMouseButtonAction;
414 
415  if (!program.isEmpty())
416  KRun::runCommand(program,0L);
417  else
418  return true;
419  }
420 
421  return false;
422 }
423 
424 void TextLabel::allowClick(bool enable)
425 {
426  m_clickable = enable;
427 }
428 
429 bool TextLabel::clickable()
430 {
431  return m_clickable;
432 }
433 
434 void TextLabel::attachClickArea(const QString &leftMouseButton,
435  const QString &middleMouseButton,
436  const QString &rightMouseButton)
437 {
438  m_leftMouseButtonAction = leftMouseButton;
439  m_middleMouseButtonAction = middleMouseButton;
440  m_rightMouseButtonAction = rightMouseButton;
441 
442  QGraphicsItem::setCursor(QCursor(Qt::PointingHandCursor));
443 }
444 
445 QRectF TextLabel::boundingRect() const
446 {
447  return Meter::boundingRect();
448 }
449 
450 int TextLabel::getTextWidth() const
451 {
452  QFontMetrics fm(font);
453  return fm.width(value[0]);
454 }
455 
456 int TextLabel::getX() const
457 {
458  return origPoint.x();
459 }
460 
461 int TextLabel::getY() const
462 {
463  return origPoint.y();
464 }
465 
466 int TextLabel::getWidth() const
467 {
468  if (!m_sizeGiven) {
469  return -1;
470  } else {
471  return Meter::getWidth();
472  }
473 }
474 
475 int TextLabel::getHeight() const
476 {
477  if (!m_sizeGiven) {
478  return -1;
479  } else {
480  return Meter::getHeight();
481  }
482 }
483 
484 void TextLabel::setSize(int x, int y, int width, int height)
485 {
486  origPoint = QPoint(x, y);
487 
488  if (height <= 0 || width <= 0) {
489  m_sizeGiven = false;
490  } else {
491  m_sizeGiven = true;
492  }
493 
494  Meter::setSize(x, y, width, height);
495 
496  calculateTextSize();
497 }
498 
TextLabel::getBGColor
QColor getBGColor() const
Definition: meters/textlabel.cpp:163
Meter::getHeight
virtual int getHeight() const
Definition: meters/meter.cpp:97
Meter::isEnabled
bool isEnabled() const
Definition: meters/meter.cpp:169
TextLabel::~TextLabel
~TextLabel()
Definition: meters/textlabel.cpp:69
TextField::getFontSize
int getFontSize() const
Definition: textfield.cpp:99
Meter::boundingRect
virtual QRectF boundingRect() const
Definition: meters/meter.cpp:193
TextLabel::setAlignment
void setAlignment(const QString &)
Definition: meters/textlabel.cpp:190
Meter::getWidth
virtual int getWidth() const
Definition: meters/meter.cpp:83
TextField::getShadow
int getShadow() const
Definition: textfield.cpp:150
TextLabel::ScrollBackAndForth
Definition: meters/textlabel.h:24
TextLabel::paint
void paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *widget)
Definition: meters/textlabel.cpp:348
TextField::getFont
QString getFont() const
Definition: textfield.cpp:88
Meter::setSize
virtual void setSize(int x, int y, int width, int height)
Definition: meters/meter.cpp:159
QWidget
TextLabel::setScroll
void setScroll(ScrollType type, QPoint speed, int gap, int pause)
Definition: meters/textlabel.cpp:250
TextLabel::setFixedPitch
void setFixedPitch(bool)
Definition: meters/textlabel.cpp:213
Meter::setHeight
virtual void setHeight(int)
Definition: meters/meter.cpp:102
TextField::getColor
QColor getColor() const
Definition: textfield.cpp:65
Meter::show
virtual void show()
Definition: meters/meter.cpp:179
TextLabel::ScrollNormal
Definition: meters/textlabel.h:23
TextLabel::setValue
void setValue(const QString &text)
Definition: meters/textlabel.cpp:144
Meter::getColor
virtual QColor getColor() const
Definition: meters/meter.cpp:149
TextField::getBGColor
QColor getBGColor() const
Definition: textfield.cpp:75
TextField::getAlignment
int getAlignment() const
Definition: textfield.cpp:120
Karamba
Definition: karamba.h:52
TextLabel::show
virtual void show()
Definition: meters/textlabel.cpp:72
TextLabel::setFont
void setFont(const QString &)
Definition: meters/textlabel.cpp:168
TextLabel::setFontSize
void setFontSize(int)
Definition: meters/textlabel.cpp:179
TextLabel::ScrollNone
Definition: meters/textlabel.h:23
Meter::m_hidden
bool m_hidden
Definition: meters/meter.h:78
TextLabel::allowClick
void allowClick(bool enable)
Definition: meters/textlabel.cpp:424
textlabel.h
Meter::setWidth
virtual void setWidth(int)
Definition: meters/meter.cpp:88
Meter::hide
virtual void hide()
Definition: meters/meter.cpp:186
TextLabel::hide
virtual void hide()
Definition: meters/textlabel.cpp:78
TextLabel::TextLabel
TextLabel(Karamba *k, int x, int y, int w, int h)
Definition: meters/textlabel.cpp:22
TextLabel::ScrollOnePass
Definition: meters/textlabel.h:24
TextLabel::getHeight
int getHeight() const
Definition: meters/textlabel.cpp:475
TextLabel::ScrollType
ScrollType
Definition: meters/textlabel.h:23
Meter::setX
virtual void setX(int)
Definition: meters/meter.cpp:60
TextLabel::getX
int getX() const
Definition: meters/textlabel.cpp:456
Meter::setEnabled
void setEnabled(bool enable)
Definition: meters/meter.cpp:174
TextLabel::attachClickArea
void attachClickArea(const QString &leftMouseButton, const QString &middleMouseButton, const QString &rightMouseButton)
Definition: meters/textlabel.cpp:434
TextLabel::setBGColor
void setBGColor(QColor clr)
Definition: meters/textlabel.cpp:158
TextLabel::setSize
void setSize(int x, int y, int width, int height)
Definition: meters/textlabel.cpp:484
TextLabel::mouseEvent
bool mouseEvent(QEvent *e)
Definition: meters/textlabel.cpp:397
TextLabel::getShadow
int getShadow() const
Definition: meters/textlabel.cpp:228
Meter::setColor
virtual void setColor(QColor color)
Definition: meters/meter.cpp:154
Meter
Definition: meters/meter.h:23
TextLabel::setTextProps
void setTextProps(TextField *)
Definition: meters/textlabel.cpp:84
TextLabel::getAlignment
QString getAlignment() const
Definition: meters/textlabel.cpp:203
TextLabel::getY
int getY() const
Definition: meters/textlabel.cpp:461
TextLabel::clickable
bool clickable()
Definition: meters/textlabel.cpp:429
TextLabel::getWidth
int getWidth() const
Definition: meters/textlabel.cpp:466
TextLabel::getFixedPitch
bool getFixedPitch() const
Definition: meters/textlabel.cpp:218
TextLabel::setShadow
void setShadow(int)
Definition: meters/textlabel.cpp:223
TextLabel::getFontSize
int getFontSize() const
Definition: meters/textlabel.cpp:185
TextField
Ralph M.
Definition: textfield.h:22
TextLabel::boundingRect
QRectF boundingRect() const
Definition: meters/textlabel.cpp:445
TextLabel::getFont
QString getFont() const
Definition: meters/textlabel.cpp:174
TextLabel::getTextWidth
int getTextWidth() const
Definition: meters/textlabel.cpp:450
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:07:20 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

superkaramba

Skip menu "superkaramba"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeutils API Reference

Skip menu "kdeutils API Reference"
  • ark
  • filelight
  • kcalc
  • kcharselect
  • kdf
  • kfloppy
  • kgpg
  • kremotecontrol
  • ktimer
  • kwallet
  • superkaramba
  • sweeper

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal