kactivelabel.cpp
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 1999 Waldo Bastian (bastian@kde.org) 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; version 2 00007 of the License. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "kactivelabel.h" 00021 00022 #include <tdeapplication.h> 00023 #include <tqregexp.h> 00024 #include <tqwhatsthis.h> 00025 #include <tqsimplerichtext.h> 00026 #include <kdebug.h> 00027 00028 KActiveLabel::KActiveLabel(TQWidget * parent, const char * name) 00029 : TQTextBrowser(parent, name) 00030 { 00031 init(); 00032 } 00033 00034 KActiveLabel::KActiveLabel(const TQString &text, TQWidget * parent, const char * name) 00035 : TQTextBrowser(parent, name) 00036 { 00037 init(); 00038 setText(text); 00039 } 00040 00041 void KActiveLabel::init() 00042 { 00043 setTextFormat(TQt::RichText); 00044 setVScrollBarMode(TQScrollView::AlwaysOff); 00045 setHScrollBarMode(TQScrollView::AlwaysOff); 00046 setFrameStyle(TQFrame::NoFrame); 00047 setFocusPolicy( TQ_TabFocus ); 00048 paletteChanged(); 00049 00050 connect(this, TQT_SIGNAL(linkClicked(const TQString &)), 00051 this, TQT_SLOT(openLink(const TQString &))); 00052 if (kapp) 00053 { 00054 connect(kapp, TQT_SIGNAL(tdedisplayPaletteChanged()), 00055 this, TQT_SLOT(paletteChanged())); 00056 } 00057 } 00058 00059 void KActiveLabel::paletteChanged() 00060 { 00061 TQPalette p = kapp ? kapp->palette() : palette(); 00062 p.setBrush(TQColorGroup::Base, p.brush(TQPalette::Normal, TQColorGroup::Background)); 00063 p.setColor(TQColorGroup::Text, p.color(TQPalette::Normal, TQColorGroup::Foreground)); 00064 setPalette(p); 00065 } 00066 00067 void KActiveLabel::openLink(const TQString & link) 00068 { 00069 TQRegExp whatsthis("whatsthis:/*([^/].*)"); 00070 if (whatsthis.exactMatch(link)) { 00071 TQWhatsThis::display(whatsthis.cap(1)); 00072 return; 00073 } 00074 00075 TQStringList args; 00076 args << "exec" << link; 00077 kapp->tdeinitExec("kfmclient", args); 00078 } 00079 00080 void KActiveLabel::virtual_hook( int, void* ) 00081 { /*BASE::virtual_hook( id, data );*/ } 00082 00083 void KActiveLabel::focusInEvent( TQFocusEvent* fe ) 00084 { 00085 TQTextBrowser::focusInEvent(fe); 00086 if(fe->reason() == TQFocusEvent::Tab || fe->reason() == TQFocusEvent::Backtab) 00087 selectAll(true); 00088 } 00089 00090 void KActiveLabel::focusOutEvent( TQFocusEvent* fe ) 00091 { 00092 TQTextBrowser::focusOutEvent(fe); 00093 if(fe->reason() == TQFocusEvent::Tab || fe->reason() == TQFocusEvent::Backtab) 00094 selectAll(false); 00095 } 00096 00097 void KActiveLabel::keyPressEvent( TQKeyEvent *e ) 00098 { 00099 switch ( e->key() ) 00100 { 00101 case Key_Down: 00102 case Key_Up: 00103 case Key_Left: 00104 case Key_Right: 00105 // jump over QTextEdit's key navigation breakage. 00106 // we're not interested in keyboard navigation within the text 00107 TQWidget::keyPressEvent( e ); 00108 break; 00109 default: 00110 TQTextBrowser::keyPressEvent( e ); 00111 } 00112 } 00113 00114 TQSize KActiveLabel::minimumSizeHint() const 00115 { 00116 TQSize ms = minimumSize(); 00117 if ((ms.width() > 0) && (ms.height() > 0)) 00118 return ms; 00119 00120 int w = 400; 00121 if (ms.width() > 0) 00122 w = ms.width(); 00123 00124 TQString txt = text(); 00125 TQSimpleRichText rt(txt, font()); 00126 rt.setWidth(w - 2*frameWidth() - 10); 00127 w = 10 + rt.widthUsed() + 2*frameWidth(); 00128 if (w < ms.width()) 00129 w = ms.width(); 00130 int h = rt.height() + 2*frameWidth(); 00131 if ( h < ms.height()) 00132 h = ms.height(); 00133 00134 return TQSize(w, h); 00135 } 00136 00137 TQSize KActiveLabel::sizeHint() const 00138 { 00139 return minimumSizeHint(); 00140 } 00141 00142 #include "kactivelabel.moc"