richtextlabel.cpp
00001 /*************************************************************************** 00002 * Copyright (C) 2003 by Wilfried Huss * 00003 * Wilfried.Huss@gmx.at * 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU General Public License as published by * 00007 * the Free Software Foundation; either version 2 of the License, or * 00008 * (at your option) any later version. * 00009 ***************************************************************************/ 00010 00011 #include <krun.h> 00012 #include <kdebug.h> 00013 #include "karamba.h" 00014 #include "richtextlabel.h" 00015 00016 RichTextLabel::RichTextLabel(karamba* k) : 00017 Meter(k, 0, 0, 100, 100), 00018 text(0), 00019 source(""), 00020 colorGrp(k->colorGroup()), 00021 underlineLinks(false) 00022 { 00023 originalSize = TQSize(0, 0); 00024 } 00025 00026 RichTextLabel::RichTextLabel(karamba* k, int x, int y, int w, int h) : 00027 Meter(k, x, y, w, h), 00028 text(0), 00029 source(""), 00030 colorGrp(k->colorGroup()), 00031 underlineLinks(false) 00032 { 00033 kdDebug() << k_funcinfo << x << ", " << y << ", " << w << ", " << h << endl; 00034 originalSize = TQSize(w, h); 00035 } 00036 00037 RichTextLabel::~RichTextLabel() 00038 { 00039 if (text != 0) 00040 { 00041 delete text; 00042 text = 0; 00043 } 00044 } 00045 00046 void RichTextLabel::setText(TQString t, bool linkUnderline) 00047 { 00048 source = t; 00049 if (text != 0) 00050 { 00051 delete text; 00052 text = 0; 00053 } 00054 else 00055 { 00056 // set underlineLinks only when RichTextLabel is created, not 00057 // when text is changed. 00058 underlineLinks = linkUnderline; 00059 } 00060 00061 text = new TQSimpleRichText(t, font, m_karamba->theme().path(), 00062 0, // default TQStyleSheet* 00063 0, // default TQMimeSourceFactory 00064 -1, // no pageBreak 00065 TQt::blue, // (has no effect) link Color 00066 underlineLinks); 00067 00068 // set the text to a reasonable size 00069 text->adjustSize(); 00070 if(originalSize.width() < 1) 00071 setWidth(text->width()); 00072 else 00073 text->setWidth(getWidth()); 00074 if(originalSize.height() < 1) 00075 setHeight(text->height()); 00076 } 00077 00078 void RichTextLabel::setValue(TQString text) 00079 { 00080 setText(text); 00081 } 00082 00083 void RichTextLabel::setValue(long v) 00084 { 00085 setText(TQString::number(v)); 00086 } 00087 00088 void RichTextLabel::setFont(TQString f) 00089 { 00090 font.setFamily(f); 00091 if(text != 0) 00092 text->setDefaultFont(font); 00093 } 00094 00095 TQString RichTextLabel::getFont() const 00096 { 00097 return font.family(); 00098 } 00099 00100 void RichTextLabel::setFontSize(int size) 00101 { 00102 font.setPixelSize(size); 00103 if(text != 0) 00104 text->setDefaultFont(font); 00105 } 00106 00107 int RichTextLabel::getFontSize() const 00108 { 00109 return font.pixelSize(); 00110 } 00111 00112 void RichTextLabel::setFixedPitch(bool fp) 00113 { 00114 font.setFixedPitch(fp); 00115 if(text != 0) 00116 text->setDefaultFont(font); 00117 } 00118 00119 bool RichTextLabel::getFixedPitch() const 00120 { 00121 return font.fixedPitch(); 00122 } 00123 00124 void RichTextLabel::setTextProps(TextField* t) 00125 { 00126 if(t) 00127 { 00128 setFontSize(t->getFontSize()); 00129 setFont(t->getFont()); 00130 colorGrp.setColor(TQColorGroup::Text, t->getColor()); 00131 } 00132 } 00133 00134 void RichTextLabel::setWidth(int width) 00135 { 00136 Meter::setWidth(width); 00137 // rearrange text 00138 text->setWidth(getWidth()); 00139 if(originalSize.height() < 1) 00140 setHeight(text->height()); 00141 } 00142 00143 void RichTextLabel::mUpdate(TQPainter* p) 00144 { 00145 if (hidden || text == 0) 00146 { 00147 return; 00148 } 00149 00150 int x = getX(); 00151 int y = getY(); 00152 int w = getWidth(); 00153 int h = getHeight(); 00154 TQRect clipRect(x, y, w, h); 00155 text->draw(p, x, y, clipRect, colorGrp, 0 /* no background */); 00156 } 00157 00158 bool RichTextLabel::click(TQMouseEvent* e) 00159 { 00160 if (hidden) 00161 { 00162 return false; 00163 } 00164 TQPoint point(e->x() - getX(), e->y() - getY()); 00165 TQString anchor = text->anchorAt(point); 00166 if (anchor[0] != '#') 00167 { 00168 if (e->button() == Qt::LeftButton) 00169 { 00170 KRun :: runCommand(anchor); 00171 } 00172 return false; 00173 } 00174 else 00175 { 00176 //call callback meterClicked 00177 return true; 00178 } 00179 } 00180 00181 TQString RichTextLabel::anchorAt(int x, int y) 00182 { 00183 TQPoint point(x - getX(), y - getY()); 00184 TQString anchor = text->anchorAt(point); 00185 if (anchor[0] == '#') 00186 { 00187 return anchor.remove(0, 1); 00188 } 00189 else 00190 { 00191 // ASSERT: should never happen 00192 return ""; 00193 } 00194 } 00195 00196 bool RichTextLabel::insideActiveArea(int x, int y) 00197 { 00198 TQPoint point(x - getX(), y - getY()); 00199 return text->anchorAt(point) != ""; // && text -> inText(point); 00200 } 00201 00202 void RichTextLabel::setColorGroup(const TQColorGroup &colorg) 00203 { 00204 colorGrp = colorg; 00205 } 00206 00207 const TQColorGroup & RichTextLabel::getColorGroup() const 00208 { 00209 return colorGrp; 00210 } 00211 00212 #include "richtextlabel.moc"