KDE3Support
k3activelabel.cpp
Go to the documentation of this file.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 "k3activelabel.h" 00021 00022 #include <Q3SimpleRichText> 00023 #include <QFocusEvent> 00024 00025 #include <ktoolinvocation.h> 00026 00027 class K3ActiveLabelPrivate 00028 { 00029 public: 00030 K3ActiveLabelPrivate(K3ActiveLabel *qq); 00031 00032 void updatePalette(); 00033 00034 K3ActiveLabel *q; 00035 }; 00036 00037 K3ActiveLabelPrivate::K3ActiveLabelPrivate(K3ActiveLabel *qq) 00038 : q(qq) 00039 { 00040 q->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 00041 q->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 00042 q->setFrameStyle(QFrame::NoFrame); 00043 q->setFocusPolicy(Qt::TabFocus); 00044 updatePalette(); 00045 } 00046 00047 void K3ActiveLabelPrivate::updatePalette() 00048 { 00049 QPalette p = q->palette(); 00050 p.setBrush(QPalette::Base, p.brush(QPalette::Normal, QPalette::Background)); 00051 p.setColor(QPalette::Text, p.color(QPalette::Normal, QPalette::Foreground)); 00052 q->setPalette(p); 00053 } 00054 00055 K3ActiveLabel::K3ActiveLabel(QWidget * parent) 00056 : KTextBrowser(parent),d(new K3ActiveLabelPrivate(this)) 00057 { 00058 } 00059 00060 K3ActiveLabel::K3ActiveLabel(const QString &text, QWidget * parent) 00061 : KTextBrowser(parent),d(new K3ActiveLabelPrivate(this)) 00062 { 00063 setHtml(text); 00064 } 00065 00066 K3ActiveLabel::~K3ActiveLabel() 00067 { 00068 delete d; 00069 } 00070 00071 void K3ActiveLabel::focusInEvent( QFocusEvent* fe ) 00072 { 00073 KTextBrowser::focusInEvent(fe); 00074 if(fe->reason() == Qt::TabFocusReason || fe->reason() == Qt::BacktabFocusReason) 00075 selectAll(); 00076 } 00077 00078 void K3ActiveLabel::focusOutEvent( QFocusEvent* fe ) 00079 { 00080 KTextBrowser::focusOutEvent(fe); 00081 if(fe->reason() == Qt::TabFocusReason || fe->reason() == Qt::BacktabFocusReason) 00082 selectAll(); //TODO reimplement: deselect text 00083 } 00084 00085 void K3ActiveLabel::keyPressEvent( QKeyEvent *e ) 00086 { 00087 switch ( e->key() ) 00088 { 00089 case Qt::Key_Down: 00090 case Qt::Key_Up: 00091 case Qt::Key_Left: 00092 case Qt::Key_Right: 00093 // jump over QTextEdit's key navigation breakage. 00094 // we're not interested in keyboard navigation within the text 00095 QWidget::keyPressEvent( e ); 00096 break; 00097 default: 00098 KTextBrowser::keyPressEvent( e ); 00099 } 00100 } 00101 00102 bool K3ActiveLabel::event(QEvent *e) 00103 { 00104 // call the base implementation first so it updates 00105 // our palette 00106 const bool result = KTextBrowser::event(e); 00107 if (e->type() == QEvent::ApplicationPaletteChange) { 00108 d->updatePalette(); 00109 } 00110 return result; 00111 } 00112 00113 QSize K3ActiveLabel::minimumSizeHint() const 00114 { 00115 QSize ms = minimumSize(); 00116 if ((ms.width() > 0) && (ms.height() > 0)) 00117 return ms; 00118 00119 int w = 400; 00120 if (ms.width() > 0) 00121 w = ms.width(); 00122 00123 QString txt = toHtml(); 00124 Q3SimpleRichText rt(txt, font()); 00125 rt.setWidth(w - 2*frameWidth() - 10); 00126 w = 10 + rt.widthUsed() + 2*frameWidth(); 00127 if (w < ms.width()) 00128 w = ms.width(); 00129 int h = rt.height() + 2*frameWidth(); 00130 if ( h < ms.height()) 00131 h = ms.height(); 00132 00133 return QSize(w, h); 00134 } 00135 00136 QSize K3ActiveLabel::sizeHint() const 00137 { 00138 return minimumSizeHint(); 00139 } 00140 00141 #include "k3activelabel.moc"