summaryrefslogtreecommitdiffstats
path: root/superkaramba/src/richtextlabel.cpp
blob: 9a8fee0974213f3d098015c28f03331f55c865ff (plain)
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
/***************************************************************************
 *   Copyright (C) 2003 by Wilfried Huss                                   *
 *   Wilfried.Huss@gmx.at                                                  *
 *                                                                         *
 *   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 <krun.h>
#include <kdebug.h>
#include "karamba.h"
#include "richtextlabel.h"

RichTextLabel::RichTextLabel(karamba* k) :
    Meter(k, 0, 0, 100, 100),
    text(0),
    source(""),
    colorGrp(k->colorGroup()),
    underlineLinks(false)
{
  originalSize = QSize(0, 0);
}

RichTextLabel::RichTextLabel(karamba* k, int x, int y, int w, int h) :
    Meter(k, x, y, w, h),
    text(0),
    source(""),
    colorGrp(k->colorGroup()),
    underlineLinks(false)
{
  kdDebug() << k_funcinfo << x << ", " << y << ", " << w << ", " << h << endl;
  originalSize = QSize(w, h);
}

RichTextLabel::~RichTextLabel()
{
  if (text != 0)
  {
    delete text;
    text = 0;
  }
}

void RichTextLabel::setText(QString t, bool linkUnderline)
{
  source = t;
  if (text != 0)
  {
    delete text;
    text = 0;
  }
  else
  {
    // set underlineLinks only when RichTextLabel is created, not
    // when text is changed.
    underlineLinks = linkUnderline;
  }

  text = new QSimpleRichText(t, font, m_karamba->theme().path(),
                             0,        // default QStyleSheet*
                             0,        // default QMimeSourceFactory
                             -1,       // no pageBreak
                             Qt::blue, // (has no effect) link Color
                             underlineLinks);

  // set the text to a reasonable size
  text->adjustSize();
  if(originalSize.width() < 1)
    setWidth(text->width());
  else
    text->setWidth(getWidth());
  if(originalSize.height() < 1)
    setHeight(text->height());
}

void RichTextLabel::setValue(QString text)
{
  setText(text);
}

void RichTextLabel::setValue(long v)
{
  setText(QString::number(v));
}

void RichTextLabel::setFont(QString f)
{
  font.setFamily(f);
  if(text != 0)
    text->setDefaultFont(font);
}

QString RichTextLabel::getFont() const
{
  return font.family();
}

void RichTextLabel::setFontSize(int size)
{
  font.setPixelSize(size);
  if(text != 0)
    text->setDefaultFont(font);
}

int RichTextLabel::getFontSize() const
{
  return font.pixelSize();
}

void RichTextLabel::setFixedPitch(bool fp)
{
  font.setFixedPitch(fp);
  if(text != 0)
    text->setDefaultFont(font);
}

bool RichTextLabel::getFixedPitch() const
{
  return font.fixedPitch();
}

void RichTextLabel::setTextProps(TextField* t)
{
  if(t)
  {
    setFontSize(t->getFontSize());
    setFont(t->getFont());
    colorGrp.setColor(QColorGroup::Text, t->getColor());
  }
}

void RichTextLabel::setWidth(int width)
{
  Meter::setWidth(width);
  // rearrange text
  text->setWidth(getWidth());
  if(originalSize.height() < 1)
    setHeight(text->height());
}

void RichTextLabel::mUpdate(QPainter* p)
{
  if (hidden || text == 0)
  {
    return;
  }

  int x = getX();
  int y = getY();
  int w = getWidth();
  int h = getHeight();
  QRect clipRect(x, y, w, h);
  text->draw(p, x, y, clipRect, colorGrp, 0 /* no background */);
}

bool RichTextLabel::click(QMouseEvent* e)
{
  if (hidden)
  {
    return false;
  }
  QPoint point(e->x() - getX(), e->y() - getY());
  QString anchor = text->anchorAt(point);
  if (anchor[0] != '#')
  {
    if (e->button() == Qt::LeftButton)
    {
      KRun :: runCommand(anchor);
    }
    return false;
  }
  else
  {
    //call callback meterClicked
    return true;
  }
}

QString RichTextLabel::anchorAt(int x, int y)
{
  QPoint point(x - getX(), y - getY());
  QString anchor = text->anchorAt(point);
  if (anchor[0] == '#')
  {
    return anchor.remove(0, 1);
  }
  else
  {
    // ASSERT: should never happen
    return "";
  }
}

bool RichTextLabel::insideActiveArea(int x, int y)
{
  QPoint point(x - getX(), y - getY());
  return text->anchorAt(point) != ""; // && text -> inText(point);
}

void RichTextLabel::setColorGroup(const QColorGroup &colorg)
{
  colorGrp = colorg;
}

const QColorGroup & RichTextLabel::getColorGroup() const
{
  return colorGrp;
}

#include "richtextlabel.moc"