klaptopdaemon
krichtextlabel.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "krichtextlabel.h"
00020
00021
00022 #include <q3stylesheet.h>
00023 #include <q3simplerichtext.h>
00024
00025 #include <QLabel>
00026
00027 #include <kglobalsettings.h>
00028
00029 static QString qrichtextify( const QString& text )
00030 {
00031 if ( text.isEmpty() || text[0] == '<' )
00032 return text;
00033
00034 QStringList lines = QStringList::split('\n', text);
00035 for(QStringList::Iterator it = lines.begin(); it != lines.end(); ++it)
00036 {
00037 *it = Q3StyleSheet::convertFromPlainText( *it, Q3StyleSheetItem::WhiteSpaceNormal );
00038 }
00039
00040 return lines.join(QString::null);
00041 }
00042
00043 KRichTextLabel::KRichTextLabel( const QString &text , QWidget *parent )
00044 : QLabel ( parent ) {
00045 m_defaultWidth = qMin(400, KGlobalSettings::desktopGeometry(this).width()*2/5);
00046 setWordWrap(true);
00047 setText(text);
00048 }
00049
00050 KRichTextLabel::KRichTextLabel( QWidget *parent )
00051 : QLabel ( parent ) {
00052 m_defaultWidth = qMin(400, KGlobalSettings::desktopGeometry(this).width()*2/5);
00053 setWordWrap(true);
00054 }
00055
00056 void KRichTextLabel::setDefaultWidth(int defaultWidth)
00057 {
00058 m_defaultWidth = defaultWidth;
00059 updateGeometry();
00060 }
00061
00062 QSizePolicy KRichTextLabel::sizePolicy() const
00063 {
00064 return QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum, false);
00065 }
00066
00067 QSize KRichTextLabel::minimumSizeHint() const
00068 {
00069 QString qt_text = qrichtextify( text() );
00070 int pref_width = 0;
00071 int pref_height = 0;
00072 Q3SimpleRichText rt(qt_text, font());
00073 pref_width = m_defaultWidth;
00074 rt.setWidth(pref_width);
00075 int used_width = rt.widthUsed();
00076 if (used_width <= pref_width)
00077 {
00078 while(true)
00079 {
00080 int new_width = (used_width * 9) / 10;
00081 rt.setWidth(new_width);
00082 int new_height = rt.height();
00083 if (new_height > pref_height)
00084 break;
00085 used_width = rt.widthUsed();
00086 if (used_width > new_width)
00087 break;
00088 }
00089 pref_width = used_width;
00090 }
00091 else
00092 {
00093 if (used_width > (pref_width *2))
00094 pref_width = pref_width *2;
00095 else
00096 pref_width = used_width;
00097 }
00098
00099 return QSize(pref_width, rt.height());
00100 }
00101
00102 QSize KRichTextLabel::sizeHint() const
00103 {
00104 return minimumSizeHint();
00105 }
00106
00107 void KRichTextLabel::setText( const QString &text ) {
00108 QLabel::setText(text);
00109 }
00110
00111 void KRichTextLabel::virtual_hook( int, void* )
00112 { }
00113
00114 #include "krichtextlabel.moc"