• 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/richtextlabel.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2003 by Wilfried Huss <Wilfried.Huss@gmx.at> *
3  * Copyright (C) 2003 Matt Broadstone <mbroadst@gmail.com> *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  ***************************************************************************/
10 
11 #include "richtextlabel.h"
12 #include "richtextlabel.moc"
13 
14 #include "karamba.h"
15 #include "textlabel.h"
16 
17 #include <QApplication>
18 #include <QTextCursor>
19 #include <QTextCharFormat>
20 #include <QAbstractTextDocumentLayout>
21 #include <QGraphicsSceneMouseEvent>
22 #include <QPainter>
23 
24 #include <KRun>
25 
26 RichTextLabel::RichTextLabel(Karamba* k)
27  : Meter(k, 0, 0, 0, 0),
28  text(0),
29  source(""),
30  colorGrp(QApplication::palette()),
31  underlineLinks(false)
32 {
33  originalSize = QSize(0, 0);
34 }
35 
36 RichTextLabel::RichTextLabel(Karamba* k, int x, int y, int w, int h)
37  : Meter(k, x, y, w, h),
38  text(0),
39  source(""),
40  colorGrp(QApplication::palette()),
41  underlineLinks(false)
42 {
43  originalSize = QSize(w, h);
44 }
45 
46 RichTextLabel::~RichTextLabel()
47 {
48  if (text != 0) {
49  delete text;
50  text = 0;
51  }
52 }
53 
54 void RichTextLabel::setText(const QString &t, bool linkUnderline)
55 {
56  source = t;
57  if (text != 0) {
58  delete text;
59  text = 0;
60  } else {
61  // set underlineLinks only when RichTextLabel is created, not
62  // when text is changed.
63  underlineLinks = linkUnderline;
64  }
65 
66  text = new QTextDocument();
67  text->setHtml(t);
68  text->setDefaultFont(font);
69  text->setTextWidth(getWidth());
70 
71  QTextCharFormat format;
72  format.setForeground(QBrush(colorGrp.color(QPalette::Text)));
73  QTextCursor cursor(text);
74  cursor.select(QTextCursor::Document);
75  cursor.mergeCharFormat(format);
76 
77  if (getWidth() < 1) {
78  text->adjustSize();
79  Meter::setWidth((int)text->textWidth());
80  Meter::setHeight((int)text->size().height());
81  }
82 }
83 
84 void RichTextLabel::setValue(const QString &text)
85 {
86  setText(text);
87 }
88 
89 void RichTextLabel::setValue(int v)
90 {
91  setText(QString::number(v));
92 }
93 
94 void RichTextLabel::setFont(const QString &f)
95 {
96  font.setFamily(f);
97  if (text != 0)
98  text->setDefaultFont(font);
99 }
100 
101 QString RichTextLabel::getFont() const
102 {
103  return font.family();
104 }
105 
106 void RichTextLabel::setFontSize(int size)
107 {
108  font.setPixelSize(size);
109  if (text != 0)
110  text->setDefaultFont(font);
111 }
112 
113 int RichTextLabel::getFontSize() const
114 {
115  return font.pixelSize();
116 }
117 
118 void RichTextLabel::setFixedPitch(bool fp)
119 {
120  font.setFixedPitch(fp);
121  if (text != 0)
122  text->setDefaultFont(font);
123 }
124 
125 bool RichTextLabel::getFixedPitch() const
126 {
127  return font.fixedPitch();
128 }
129 
130 void RichTextLabel::setTextProps(TextField* t)
131 {
132  if (t) {
133  setFontSize(t->getFontSize());
134  setFont(t->getFont());
135  colorGrp.setColor(QPalette::Text, t->getColor());
136 
137  QTextCharFormat format;
138  format.setForeground(QBrush(colorGrp.color(QPalette::Text)));
139  QTextCursor cursor(text);
140  cursor.select(QTextCursor::Document);
141  cursor.mergeCharFormat(format);
142  }
143 }
144 
145 void RichTextLabel::setWidth(int width)
146 {
147  Meter::setWidth(width);
148  text->setTextWidth(getWidth());
149 }
150 
151 void RichTextLabel::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
152  QWidget *widget)
153 {
154  Q_UNUSED(option);
155  Q_UNUSED(widget);
156 
157  if (m_hidden || text == 0)
158  return;
159 
160  int w = getWidth();
161  int h = getHeight();
162 
163  text->drawContents(painter, QRect(0, 0, w, h));
164 }
165 
166 bool RichTextLabel::mouseEvent(QEvent *e)
167 {
168  Qt::MouseButtons button;
169  QPointF pos;
170  if (QGraphicsSceneMouseEvent *event = dynamic_cast<QGraphicsSceneMouseEvent*>(e)) {
171  button = event->button();
172  pos = mapFromParent(event->pos());
173  } else if (QGraphicsSceneWheelEvent *event = dynamic_cast<QGraphicsSceneWheelEvent*>(e)) {
174  button = event->buttons();
175  pos = mapFromParent(event->pos());
176  }
177 
178  QString link = text->documentLayout()->anchorAt(pos);
179 
180  if (link[0] != '#') {
181  if (button == Qt::LeftButton)
182  KRun::runCommand(link,0L);
183 
184  return false;
185  } else
186  return true;
187 }
188 
189 QString RichTextLabel::getAnchor(QPointF point)
190 {
191  QPointF pos = point - boundingRect().topLeft();
192  return text->documentLayout()->anchorAt(pos).remove(0, 1);
193 }
194 
195 void RichTextLabel::setColorGroup(const QPalette &colorg)
196 {
197  colorGrp = colorg;
198 }
199 
200 const QPalette& RichTextLabel::getColorGroup() const
201 {
202  return colorGrp;
203 }
204 
RichTextLabel::setValue
void setValue(const QString &text)
Definition: meters/richtextlabel.cpp:84
Meter::getHeight
virtual int getHeight() const
Definition: meters/meter.cpp:97
RichTextLabel::mouseEvent
bool mouseEvent(QEvent *event)
Definition: meters/richtextlabel.cpp:166
TextField::getFontSize
int getFontSize() const
Definition: textfield.cpp:99
RichTextLabel::getFixedPitch
bool getFixedPitch() const
Definition: meters/richtextlabel.cpp:125
Meter::boundingRect
virtual QRectF boundingRect() const
Definition: meters/meter.cpp:193
Meter::getWidth
virtual int getWidth() const
Definition: meters/meter.cpp:83
RichTextLabel::setWidth
void setWidth(int width)
Definition: meters/richtextlabel.cpp:145
TextField::getFont
QString getFont() const
Definition: textfield.cpp:88
RichTextLabel::getFontSize
int getFontSize() const
Definition: meters/richtextlabel.cpp:113
RichTextLabel::setTextProps
void setTextProps(TextField *t)
Definition: meters/richtextlabel.cpp:130
QWidget
Meter::setHeight
virtual void setHeight(int)
Definition: meters/meter.cpp:102
TextField::getColor
QColor getColor() const
Definition: textfield.cpp:65
Karamba
Definition: karamba.h:52
RichTextLabel::paint
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=0)
Definition: meters/richtextlabel.cpp:151
richtextlabel.h
RichTextLabel::~RichTextLabel
~RichTextLabel()
Definition: meters/richtextlabel.cpp:46
RichTextLabel::setFontSize
void setFontSize(int)
Definition: meters/richtextlabel.cpp:106
Meter::m_hidden
bool m_hidden
Definition: meters/meter.h:78
textlabel.h
Meter::setWidth
virtual void setWidth(int)
Definition: meters/meter.cpp:88
RichTextLabel::setFont
void setFont(const QString &font)
Definition: meters/richtextlabel.cpp:94
RichTextLabel::RichTextLabel
RichTextLabel(Karamba *)
Definition: meters/richtextlabel.cpp:26
RichTextLabel::getColorGroup
const QPalette & getColorGroup() const
Definition: meters/richtextlabel.cpp:200
RichTextLabel::setColorGroup
void setColorGroup(const QPalette &colorg)
Definition: meters/richtextlabel.cpp:195
Meter
Definition: meters/meter.h:23
RichTextLabel::getAnchor
QString getAnchor(QPointF point)
Definition: meters/richtextlabel.cpp:189
RichTextLabel::setFixedPitch
void setFixedPitch(bool)
Definition: meters/richtextlabel.cpp:118
RichTextLabel::getFont
QString getFont() const
Definition: meters/richtextlabel.cpp:101
TextField
Ralph M.
Definition: textfield.h:22
RichTextLabel::setText
void setText(const QString &text, bool linkUnderline=false)
Definition: meters/richtextlabel.cpp:54
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