13 #include "textlabel.moc"
16 #include <QMouseEvent>
17 #include <QGraphicsSceneMouseEvent>
23 :
Meter(k, x, y, w, h),
24 alignment(Qt::AlignLeft),
34 scrollType(ScrollNone),
38 origPoint = QPoint(x, y);
44 clip = Qt::TextDontClip;
46 if (h <= 0 || w <= 0) {
54 :
Meter(k, 0, 0, 0, 0),
55 alignment(Qt::AlignLeft),
65 scrollType(ScrollNone),
101 void TextLabel::calculateTextSize()
105 prepareGeometryChange();
107 QFontMetrics fm(font);
108 lineHeight = fm.height();
109 textSize.setWidth(0);
110 textSize.setHeight(lineHeight * value.count());
111 QStringList::Iterator it = value.begin();
113 while (it != value.end()) {
115 if (tmp > textSize.width())
116 textSize.setWidth(tmp);
121 if ((
getWidth() <= 0) || !m_sizeGiven) {
125 if ((
getHeight() <= 0) || !m_sizeGiven) {
130 if (alignment == Qt::AlignLeft) {
133 else if (alignment == Qt::AlignRight) {
134 setX(origPoint.x() - textSize.width());
136 else if (alignment == Qt::AlignHCenter) {
137 setX(origPoint.x() - textSize.width() / 2);
146 value = text.split(
'\n');
153 value = QStringList(QString::number(v));
176 return font.family();
181 font.setPixelSize(size);
187 return font.pixelSize();
192 QString a = align.toUpper();
193 if (a ==
"LEFT" || a.isEmpty())
194 alignment = Qt::AlignLeft;
196 alignment = Qt::AlignRight;
198 alignment = Qt::AlignHCenter;
205 if (alignment == Qt::AlignHCenter)
207 else if (alignment == Qt::AlignRight)
215 font.setFixedPitch(fp);
220 return font.fixedPitch();
236 QString scroll = a.toUpper();
238 if (scroll ==
"NONE")
240 else if (a ==
"NORMAL")
242 else if (a ==
"BACKANDFORTH")
244 else if (a ==
"ONEPASS")
255 switch (scrollType) {
261 x = -1 * textSize.width();
262 else if (speed.x() < 0)
266 x = -1 * textSize.height();
267 else if (speed.y() < 0)
270 scrollPos = QPoint(x, y);
277 scrollPos = QPoint(0, 0);
286 int TextLabel::drawText(QPainter *p,
int x,
int y,
int width,
int height,
291 p->drawText(x + shadow, y + shadow, width, height,
292 alignment | clip | Qt::TextExpandTabs, text);
296 p->drawText(x, y, width, height, alignment | clip |
297 Qt::TextExpandTabs, text);
302 bool TextLabel::calculateScrollCoords(
const QRect &meterRect, QRect &textRect,
303 QPoint &next,
int x,
int y)
306 (scrollSpeed.x() != 0 && textSize.width() <
getWidth() ||
307 scrollSpeed.y() != 0 && textSize.height() <
getHeight()))
313 if (pauseCounter < 1) {
314 scrollPos += scrollSpeed;
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());
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;
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;
336 }
else if (!textRect.intersects(meterRect)) {
359 QRect meterRect(0, 0, width, height);
365 p->setClipRect(0, 0, width, height);
366 if (!calculateScrollCoords(meterRect, textRect, next, 0, 0)) {
367 p->setClipping(
false);
370 width = textSize.width();
371 height = textSize.height();
374 QStringList::Iterator it = value.begin();
375 while (it != value.end() && (row <= height || height == -1)) {
376 drawText(p, 0, 0 + i, width, height, *it);
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());
393 p->setClipping(
false);
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();
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;
415 if (!program.isEmpty())
416 KRun::runCommand(program,0L);
426 m_clickable = enable;
435 const QString &middleMouseButton,
436 const QString &rightMouseButton)
438 m_leftMouseButtonAction = leftMouseButton;
439 m_middleMouseButtonAction = middleMouseButton;
440 m_rightMouseButtonAction = rightMouseButton;
442 QGraphicsItem::setCursor(QCursor(Qt::PointingHandCursor));
452 QFontMetrics fm(font);
453 return fm.width(value[0]);
458 return origPoint.x();
463 return origPoint.y();
486 origPoint = QPoint(x, y);
488 if (height <= 0 || width <= 0) {
QColor getBGColor() const
virtual int getHeight() const
virtual QRectF boundingRect() const
void setAlignment(const QString &)
virtual int getWidth() const
void paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *widget)
virtual void setSize(int x, int y, int width, int height)
void setScroll(ScrollType type, QPoint speed, int gap, int pause)
virtual void setHeight(int)
void setValue(const QString &text)
virtual QColor getColor() const
QColor getBGColor() const
void setFont(const QString &)
void allowClick(bool enable)
virtual void setWidth(int)
TextLabel(Karamba *k, int x, int y, int w, int h)
void setEnabled(bool enable)
void attachClickArea(const QString &leftMouseButton, const QString &middleMouseButton, const QString &rightMouseButton)
void setBGColor(QColor clr)
void setSize(int x, int y, int width, int height)
bool mouseEvent(QEvent *e)
virtual void setColor(QColor color)
void setTextProps(TextField *)
QString getAlignment() const
bool getFixedPitch() const
QRectF boundingRect() const