00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <krun.h>
00012 #include <stdlib.h>
00013 #include "textlabel.h"
00014
00015 TextLabel::TextLabel(karamba *k, int x,int y,int w,int h):
00016 Meter(k, x,y,w,h), alignment(Qt::AlignLeft), clip(0), bgColor(0, 0, 0),
00017 lineHeight(0), shadow(0), scrollSpeed(0, 0), scrollPos(0, 0), scrollGap(0),
00018 scrollPause(0), pauseCounter(0), scrollType(ScrollNone)
00019 {
00020 calculateTextSize();
00021 if( h != 0 || w != 0)
00022 clip = 0;
00023 else
00024 clip = Qt::DontClip;
00025
00026 if( h == 0 || w == 0)
00027 {
00028 setWidth(-1);
00029 setHeight(-1);
00030 }
00031 }
00032
00033 TextLabel::TextLabel(karamba *k):
00034 Meter(k, 0, 0, 0, 0), alignment(Qt::AlignLeft), clip(0), bgColor(0, 0, 0),
00035 lineHeight(0), shadow(0), scrollSpeed(0, 0), scrollPos(0, 0), scrollGap(0),
00036 scrollPause(0), pauseCounter(0), scrollType(ScrollNone)
00037 {
00038 }
00039
00040 TextLabel::~TextLabel()
00041 {
00042 }
00043
00044 void TextLabel::show()
00045 {
00046 Meter::show();
00047 setEnabled(true);
00048 }
00049
00050 void TextLabel::hide()
00051 {
00052 Meter::hide();
00053 setEnabled(false);
00054 }
00055
00056 void TextLabel::setTextProps( TextField* t )
00057 {
00058 if(t)
00059 {
00060 text = *t;
00061
00062 shadow = t->getShadow();
00063 alignment = t->getAlignment();
00064 setFontSize(t->getFontSize());
00065 setFont(t->getFont());
00066
00067 setColor(t->getColor());
00068 setBGColor(t->getBGColor());
00069 }
00070 calculateTextSize();
00071 }
00072
00073 void TextLabel::calculateTextSize()
00074 {
00075 int tmp;
00076 QFontMetrics fm(font);
00077 lineHeight = fm.height();
00078 textSize.setWidth(0);
00079 textSize.setHeight(lineHeight * value.count());
00080 QStringList::Iterator it = value.begin();
00081 while(it != value.end())
00082 {
00083 tmp = fm.width(*it);
00084 if(tmp > textSize.width())
00085 textSize.setWidth(tmp);
00086 ++it;
00087 }
00088 }
00089
00090 void TextLabel::setValue( QString text)
00091 {
00092 value = QStringList::split('\n',text);
00093 calculateTextSize();
00094 }
00095
00096 void TextLabel::setValue( int v)
00097 {
00098 value = QStringList( QString::number( v ) );
00099 calculateTextSize();
00100 }
00101
00102 void TextLabel::setBGColor(QColor clr)
00103 {
00104 bgColor = clr;
00105 }
00106
00107 QColor TextLabel::getBGColor() const
00108 {
00109 return bgColor;
00110 }
00111
00112 void TextLabel::setFont(QString f)
00113 {
00114 font.setFamily(f);
00115 calculateTextSize();
00116 }
00117
00118 QString TextLabel::getFont() const
00119 {
00120 return font.family();
00121 }
00122
00123 void TextLabel::setFontSize(int size)
00124 {
00125 font.setPixelSize(size);
00126 calculateTextSize();
00127 }
00128
00129 int TextLabel::getFontSize() const
00130 {
00131 return font.pixelSize();
00132 }
00133
00134 void TextLabel::setAlignment( QString align )
00135 {
00136 QString a = align.upper();
00137 if( a == "LEFT" || a.isEmpty() )
00138 alignment = Qt::AlignLeft;
00139 if( a == "RIGHT" )
00140 alignment = Qt::AlignRight;
00141 if( a == "CENTER" )
00142 alignment = Qt::AlignHCenter;
00143 }
00144
00145 QString TextLabel::getAlignment() const
00146 {
00147 if( alignment == Qt::AlignHCenter )
00148 return "CENTER";
00149 else if( alignment == Qt::AlignRight )
00150 return "RIGHT";
00151 else
00152 return "LEFT";
00153 }
00154
00155 void TextLabel::setFixedPitch( bool fp)
00156 {
00157 font.setFixedPitch( fp );
00158 }
00159
00160 bool TextLabel::getFixedPitch() const
00161 {
00162 return font.fixedPitch();
00163 }
00164
00165 void TextLabel::setShadow ( int s )
00166 {
00167 shadow = s;
00168 }
00169
00170 int TextLabel::getShadow() const
00171 {
00172 return shadow;
00173 }
00174
00175 void TextLabel::setScroll(char* type, QPoint speed, int gap, int pause)
00176 {
00177 ScrollType t = TextLabel::ScrollNone;
00178 QString a = type;
00179 a = a.upper();
00180 if(a == "NONE")
00181 t = TextLabel::ScrollNone;
00182 else if( a == "NORMAL" )
00183 t = TextLabel::ScrollNormal;
00184 else if( a == "BACKANDFORTH" )
00185 t = TextLabel::ScrollBackAndForth;
00186 else if( a == "ONEPASS" )
00187 t = TextLabel::ScrollOnePass;
00188 setScroll(t, speed, gap, pause);
00189 }
00190
00191 void TextLabel::setScroll(ScrollType type, QPoint speed, int gap, int pause)
00192 {
00193 scrollType = type;
00194 scrollSpeed = speed;
00195 switch(scrollType)
00196 {
00197 case ScrollNormal:
00198 case ScrollOnePass:
00199 {
00200 int x = 0, y = 0;
00201
00202 if(speed.x() > 0)
00203 x = -1 * textSize.width();
00204 else if(speed.x() < 0)
00205 x = getWidth()-1;
00206 if(speed.y() > 0)
00207 x = -1 * textSize.height();
00208 else if(speed.y() < 0)
00209 x = getHeight()-1;
00210 scrollPos = QPoint(x,y);
00211 break;
00212 }
00213 case ScrollNone:
00214 case ScrollBackAndForth:
00215 default:
00216 scrollPos = QPoint(0,0);
00217 break;
00218 }
00219 scrollGap = gap;
00220 scrollPause = pause;
00221 pauseCounter = 1;
00222 }
00223
00224 int TextLabel::drawText(QPainter *p, int x, int y, int width, int height,
00225 QString text)
00226 {
00227 if( shadow != 0)
00228 {
00229 p->setPen(getBGColor());
00230 p->drawText(x + shadow, y + shadow, width, height,
00231 alignment | clip | Qt::ExpandTabs, text);
00232 }
00233 p->setPen(getColor());
00234 p->drawText(x, y, width, height, alignment | clip | Qt::ExpandTabs, text);
00235 return 0;
00236 }
00237
00238 bool TextLabel::calculateScrollCoords(QRect meterRect, QRect &textRect,
00239 QPoint &next, int &x, int &y)
00240 {
00241 if(scrollType == ScrollBackAndForth &&
00242 (scrollSpeed.x() != 0 && textSize.width() < getWidth() ||
00243 scrollSpeed.y() != 0 && textSize.height() < getHeight()))
00244 return true;
00245
00246 x += scrollPos.x();
00247 y += scrollPos.y();
00248
00249 if(pauseCounter < 1)
00250 {
00251 scrollPos += scrollSpeed;
00252
00253
00254 QPoint direction(scrollSpeed.x()/abs((scrollSpeed.x() == 0)?
00255 1:scrollSpeed.x()),
00256 scrollSpeed.y()/abs((scrollSpeed.y() == 0)?
00257 1:scrollSpeed.y()));
00258 next = QPoint(-1 * direction.x() * (scrollGap + textSize.width()),
00259 -1 * direction.y() * (scrollGap + textSize.height()));
00260 textRect.setCoords(x, y, x + textSize.width(), y + textSize.height());
00261
00262 if(scrollType == ScrollBackAndForth)
00263 {
00264 if(direction.x() < 0 && textRect.right() <= meterRect.right() ||
00265 direction.x() > 0 && textRect.left() >= meterRect.left())
00266 {
00267 scrollSpeed.setX(scrollSpeed.x() * -1);
00268 pauseCounter = scrollPause;
00269 }
00270 if(direction.y() < 0 && textRect.bottom() <= meterRect.bottom() ||
00271 direction.y() > 0 && textRect.top() >= meterRect.top())
00272 {
00273 scrollSpeed.setY(scrollSpeed.y() * -1);
00274 pauseCounter = scrollPause;
00275 }
00276 }
00277 else if(!textRect.intersects(meterRect))
00278 {
00279 if(scrollType == ScrollNormal)
00280 scrollPos += next;
00281 else if(scrollType == ScrollOnePass)
00282 return false;
00283 }
00284 }
00285 else
00286 --pauseCounter;
00287 return true;
00288 }
00289
00290 void TextLabel::mUpdate(QPainter *p)
00291 {
00292 if (hidden != 1)
00293 {
00294 int i = 0;
00295 int row = 1;
00296 int x = getX();
00297 int y = getY();
00298 int width = getWidth();
00299 int height = getHeight();
00300 QRect meterRect(x, y, width, height);
00301 QRect textRect;
00302 QPoint next;
00303
00304 p->setFont(font);
00305 if(scrollType != ScrollNone)
00306 {
00307 p->setClipRect(x, y, width, height);
00308 if(!calculateScrollCoords(meterRect, textRect, next, x, y))
00309 {
00310 p->setClipping(false);
00311 return;
00312 }
00313 width = textSize.width();
00314 height = textSize.height();
00315 }
00316 QStringList::Iterator it = value.begin();
00317 while(it != value.end() && (row <= height || height == -1 ))
00318 {
00319 drawText(p, x, y + i, width, height, *it);
00320
00321
00322 if(scrollType == ScrollNormal)
00323 {
00324 textRect.addCoords(next.x(), next.y(), next.x(), next.y());
00325 while(textRect.intersects(meterRect))
00326 {
00327 drawText(p, textRect.x(), textRect.y() + i, width, height, *it);
00328 textRect.addCoords(next.x(), next.y(), next.x(), next.y());
00329 }
00330 }
00331 i += lineHeight;
00332 it++;
00333 row++;
00334 }
00335 if(scrollType != ScrollNone)
00336 p->setClipping(false);
00337 }
00338 }
00339
00340 bool TextLabel::click(QMouseEvent* e)
00341 {
00342 if (getBoundingBox().contains(e -> x(), e -> y()) && isEnabled())
00343 {
00344 QString program;
00345 if (e -> button() == Qt::LeftButton)
00346 {
00347 program = leftButtonAction;
00348 }
00349 else if (e -> button() == Qt::MidButton)
00350 {
00351 program = middleButtonAction;
00352 }
00353 else if (e -> button() == Qt::RightButton)
00354 {
00355 program = rightButtonAction;
00356 }
00357
00358 if( !program.isEmpty() )
00359 {
00360 KRun::runCommand(program);
00361 }
00362 else
00363 {
00364 return true;
00365 }
00366 }
00367 return false;
00368 }
00369
00370 void TextLabel::attachClickArea(QString leftMouseButton,
00371 QString middleMouseButton,
00372 QString rightMouseButton)
00373 {
00374 leftButtonAction = leftMouseButton;
00375 middleButtonAction = middleMouseButton;
00376 rightButtonAction = rightMouseButton;
00377 }
00378
00379 #include "textlabel.moc"