1 /* This file is part of the KDE libraries
2 Copyright (C) 2005 Waldo Bastian <bastian@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
19 #include "krichtextlabel.h"
20 #include <QTextDocument>
23 #include <q3stylesheet.h>
24 #include <q3simplerichtext.h>
25 #include <qtextdocument.h>
29 #include <kglobalsettings.h>
31 static QString
qrichtextify( const QString
& text
)
33 if ( text
.isEmpty() || text
[0] == '<' )
36 QStringList lines
= QStringList::split('\n', text
);
37 for(QStringList::Iterator it
= lines
.begin(); it
!= lines
.end(); ++it
)
39 *it
= Qt::convertFromPlainText( *it
, Qt::WhiteSpaceNormal
);
42 return lines
.join(QString::null
); //krazy:exclude=nullstrassign for old broken gcc
45 KRichTextLabel::KRichTextLabel( const QString
&text
, QWidget
*parent
)
47 m_defaultWidth
= qMin(400, KGlobalSettings::desktopGeometry(this).width()*2/5);
52 KRichTextLabel::KRichTextLabel( QWidget
*parent
)
54 m_defaultWidth
= qMin(400, KGlobalSettings::desktopGeometry(this).width()*2/5);
58 void KRichTextLabel::setDefaultWidth(int defaultWidth
)
60 m_defaultWidth
= defaultWidth
;
64 QSizePolicy
KRichTextLabel::sizePolicy() const
66 return QSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Minimum
, false);
69 QSize
KRichTextLabel::minimumSizeHint() const
71 QString qt_text
= qrichtextify( text() );
75 rt
.setHtml( qt_text
);
77 pref_width
= m_defaultWidth
;
78 rt
.setTextWidth(pref_width
);
79 int used_width
= rt
.idealWidth();
80 if (used_width
<= pref_width
)
84 int new_width
= (used_width
* 9) / 10;
85 rt
.setTextWidth(new_width
);
86 int new_height
= rt
.size().height();
87 if (new_height
> pref_height
)
89 used_width
= rt
.idealWidth();
90 if (used_width
> new_width
)
93 pref_width
= used_width
;
97 if (used_width
> (pref_width
*2))
98 pref_width
= pref_width
*2;
100 pref_width
= used_width
;
103 return QSize(pref_width
, rt
.size().height());
106 QSize
KRichTextLabel::sizeHint() const
108 return minimumSizeHint();
111 void KRichTextLabel::setText( const QString
&text
) {
112 QLabel::setText(text
);
115 void KRichTextLabel::virtual_hook( int, void* )
116 { /*BASE::virtual_hook( id, data );*/ }
118 #include "krichtextlabel.moc"