• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • superkaramba
 

superkaramba

  • superkaramba
  • src
textlabel.cpp
1/***************************************************************************
2 * Copyright (C) 2003 by Hans Karlsson *
3 * karlsson.h@home.se *
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 <krun.h>
12#include <stdlib.h>
13#include "textlabel.h"
14
15TextLabel::TextLabel(karamba *k, int x,int y,int w,int h):
16 Meter(k, x,y,w,h), alignment(TQt::AlignLeft), clip(0), bgColor(0, 0, 0),
17 lineHeight(0), shadow(0), scrollSpeed(0, 0), scrollPos(0, 0), scrollGap(0),
18 scrollPause(0), pauseCounter(0), scrollType(ScrollNone)
19{
20 calculateTextSize();
21 if( h != 0 || w != 0)
22 clip = 0;
23 else
24 clip = TQt::DontClip;
25
26 if( h == 0 || w == 0)
27 {
28 setWidth(-1);
29 setHeight(-1);
30 }
31}
32
33TextLabel::TextLabel(karamba *k):
34 Meter(k, 0, 0, 0, 0), alignment(TQt::AlignLeft), clip(0), bgColor(0, 0, 0),
35 lineHeight(0), shadow(0), scrollSpeed(0, 0), scrollPos(0, 0), scrollGap(0),
36 scrollPause(0), pauseCounter(0), scrollType(ScrollNone)
37{
38}
39
40TextLabel::~TextLabel()
41{
42}
43
44void TextLabel::show()
45{
46 Meter::show();
47 setEnabled(true);
48}
49
50void TextLabel::hide()
51{
52 Meter::hide();
53 setEnabled(false);
54}
55
56void TextLabel::setTextProps( TextField* t )
57{
58 if(t)
59 {
60 text = *t;
61 //lineHeight = t->getLineHeight();
62 shadow = t->getShadow();
63 alignment = t->getAlignment();
64 setFontSize(t->getFontSize());
65 setFont(t->getFont());
66
67 setColor(t->getColor());
68 setBGColor(t->getBGColor());
69 }
70 calculateTextSize();
71}
72
73void TextLabel::calculateTextSize()
74{
75 int tmp;
76 TQFontMetrics fm(font);
77 lineHeight = fm.height();
78 textSize.setWidth(0);
79 textSize.setHeight(lineHeight * value.count());
80 TQStringList::Iterator it = value.begin();
81 while(it != value.end())
82 {
83 tmp = fm.width(*it);
84 if(tmp > textSize.width())
85 textSize.setWidth(tmp);
86 ++it;
87 }
88}
89
90void TextLabel::setValue( TQString text)
91{
92 value = TQStringList::split('\n',text);
93 calculateTextSize();
94}
95
96void TextLabel::setValue( long v)
97{
98 value = TQStringList( TQString::number( v ) );
99 calculateTextSize();
100}
101
102void TextLabel::setBGColor(TQColor clr)
103{
104 bgColor = clr;
105}
106
107TQColor TextLabel::getBGColor() const
108{
109 return bgColor;
110}
111
112void TextLabel::setFont(TQString f)
113{
114 font.setFamily(f);
115 calculateTextSize();
116}
117
118TQString TextLabel::getFont() const
119{
120 return font.family();
121}
122
123void TextLabel::setFontSize(int size)
124{
125 font.setPixelSize(size);
126 calculateTextSize();
127}
128
129int TextLabel::getFontSize() const
130{
131 return font.pixelSize();
132}
133
134void TextLabel::setAlignment( TQString align )
135{
136 TQString a = align.upper();
137 if( a == "LEFT" || a.isEmpty() )
138 alignment = TQt::AlignLeft;
139 if( a == "RIGHT" )
140 alignment = TQt::AlignRight;
141 if( a == "CENTER" )
142 alignment = TQt::AlignHCenter;
143}
144
145TQString TextLabel::getAlignment() const
146{
147 if( alignment == TQt::AlignHCenter )
148 return "CENTER";
149 else if( alignment == TQt::AlignRight )
150 return "RIGHT";
151 else
152 return "LEFT";
153}
154
155void TextLabel::setFixedPitch( bool fp)
156{
157 font.setFixedPitch( fp );
158}
159
160bool TextLabel::getFixedPitch() const
161{
162 return font.fixedPitch();
163}
164
165void TextLabel::setShadow ( int s )
166{
167 shadow = s;
168}
169
170int TextLabel::getShadow() const
171{
172 return shadow;
173}
174
175void TextLabel::setScroll(char* type, TQPoint speed, int gap, int pause)
176{
177 ScrollType t = TextLabel::ScrollNone;
178 TQString a = type;
179 a = a.upper();
180 if(a == "NONE")
181 t = TextLabel::ScrollNone;
182 else if( a == "NORMAL" )
183 t = TextLabel::ScrollNormal;
184 else if( a == "BACKANDFORTH" )
185 t = TextLabel::ScrollBackAndForth;
186 else if( a == "ONEPASS" )
187 t = TextLabel::ScrollOnePass;
188 setScroll(t, speed, gap, pause);
189}
190
191void TextLabel::setScroll(ScrollType type, TQPoint speed, int gap, int pause)
192{
193 scrollType = type;
194 scrollSpeed = speed;
195 switch(scrollType)
196 {
197 case ScrollNormal:
198 case ScrollOnePass:
199 {
200 int x = 0, y = 0;
201
202 if(speed.x() > 0)
203 x = -1 * textSize.width();
204 else if(speed.x() < 0)
205 x = getWidth()-1;
206 if(speed.y() > 0)
207 x = -1 * textSize.height();
208 else if(speed.y() < 0)
209 x = getHeight()-1;
210 scrollPos = TQPoint(x,y);
211 break;
212 }
213 case ScrollNone:
214 case ScrollBackAndForth:
215 default:
216 scrollPos = TQPoint(0,0);
217 break;
218 }
219 scrollGap = gap;
220 scrollPause = pause;
221 pauseCounter = 1;
222}
223
224int TextLabel::drawText(TQPainter *p, int x, int y, int width, int height,
225 TQString text)
226{
227 if( shadow != 0)
228 {
229 p->setPen(getBGColor());
230 p->drawText(x + shadow, y + shadow, width, height,
231 alignment | clip | TQt::ExpandTabs, text);
232 }
233 p->setPen(getColor());
234 p->drawText(x, y, width, height, alignment | clip | TQt::ExpandTabs, text);
235 return 0;
236}
237
238bool TextLabel::calculateScrollCoords(TQRect meterRect, TQRect &textRect,
239 TQPoint &next, int &x, int &y)
240{
241 if(scrollType == ScrollBackAndForth &&
242 (scrollSpeed.x() != 0 && textSize.width() < getWidth() ||
243 scrollSpeed.y() != 0 && textSize.height() < getHeight()))
244 return true;
245
246 x += scrollPos.x();
247 y += scrollPos.y();
248
249 if(pauseCounter < 1)
250 {
251 scrollPos += scrollSpeed;
252
253 // -1 | 0 | 1
254 TQPoint direction(scrollSpeed.x()/abs((scrollSpeed.x() == 0)?
255 1:scrollSpeed.x()),
256 scrollSpeed.y()/abs((scrollSpeed.y() == 0)?
257 1:scrollSpeed.y()));
258 next = TQPoint(-1 * direction.x() * (scrollGap + textSize.width()),
259 -1 * direction.y() * (scrollGap + textSize.height()));
260 textRect.setCoords(x, y, x + textSize.width(), y + textSize.height());
261
262 if(scrollType == ScrollBackAndForth)
263 {
264 if(direction.x() < 0 && textRect.right() <= meterRect.right() ||
265 direction.x() > 0 && textRect.left() >= meterRect.left())
266 {
267 scrollSpeed.setX(scrollSpeed.x() * -1);
268 pauseCounter = scrollPause;
269 }
270 if(direction.y() < 0 && textRect.bottom() <= meterRect.bottom() ||
271 direction.y() > 0 && textRect.top() >= meterRect.top())
272 {
273 scrollSpeed.setY(scrollSpeed.y() * -1);
274 pauseCounter = scrollPause;
275 }
276 }
277 else if(!textRect.intersects(meterRect))
278 {
279 if(scrollType == ScrollNormal)
280 scrollPos += next;
281 else if(scrollType == ScrollOnePass)
282 return false;
283 }
284 }
285 else
286 --pauseCounter;
287 return true;
288}
289
290void TextLabel::mUpdate(TQPainter *p)
291{
292 if (hidden != 1)
293 {
294 int i = 0; //lineHeight;
295 int row = 1;
296 int x = getX();
297 int y = getY();
298 int width = getWidth();
299 int height = getHeight();
300 TQRect meterRect(x, y, width, height);
301 TQRect textRect;
302 TQPoint next;
303
304 p->setFont(font);
305 if(scrollType != ScrollNone)
306 {
307 p->setClipRect(x, y, width, height);
308 if(!calculateScrollCoords(meterRect, textRect, next, x, y))
309 {
310 p->setClipping(false);
311 return;
312 }
313 width = textSize.width();
314 height = textSize.height();
315 }
316 TQStringList::Iterator it = value.begin();
317 while(it != value.end() && (row <= height || height == -1 ))
318 {
319 drawText(p, x, y + i, width, height, *it);
320
321 // Draw more instances of text if scroll type is normal scroll
322 if(scrollType == ScrollNormal)
323 {
324 textRect.addCoords(next.x(), next.y(), next.x(), next.y());
325 while(textRect.intersects(meterRect))
326 {
327 drawText(p, textRect.x(), textRect.y() + i, width, height, *it);
328 textRect.addCoords(next.x(), next.y(), next.x(), next.y());
329 }
330 }
331 i += lineHeight;
332 it++;
333 row++;
334 }
335 if(scrollType != ScrollNone)
336 p->setClipping(false);
337 }
338}
339
340bool TextLabel::click(TQMouseEvent* e)
341{
342 if (getBoundingBox().contains(e -> x(), e -> y()) && isEnabled())
343 {
344 TQString program;
345 if (e -> button() == TQt::LeftButton)
346 {
347 program = leftButtonAction;
348 }
349 else if (e -> button() == TQt::MidButton)
350 {
351 program = middleButtonAction;
352 }
353 else if (e -> button() == TQt::RightButton)
354 {
355 program = rightButtonAction;
356 }
357
358 if( !program.isEmpty() )
359 {
360 KRun::runCommand(program);
361 }
362 else
363 {
364 return true;
365 }
366 }
367 return false;
368}
369
370void TextLabel::attachClickArea(TQString leftMouseButton,
371 TQString middleMouseButton,
372 TQString rightMouseButton)
373{
374 leftButtonAction = leftMouseButton;
375 middleButtonAction = middleMouseButton;
376 rightButtonAction = rightMouseButton;
377}
378
379#include "textlabel.moc"
TextField
Ralph M.
Definition: textfield.h:22

superkaramba

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

superkaramba

Skip menu "superkaramba"
  • kcalc
  •   knumber
  • superkaramba
Generated for superkaramba by doxygen 1.9.4
This website is maintained by Timothy Pearson.