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"
22 #include <q3stylesheet.h>
23 #include <q3simplerichtext.h>
24 #include <qtextdocument.h>
28 #include <kglobalsettings.h>
30 static QString
qrichtextify( const QString
& text
)
32 if ( text
.isEmpty() || text
[0] == '<' )
35 QStringList lines
= QStringList::split('\n', text
);
36 for(QStringList::Iterator it
= lines
.begin(); it
!= lines
.end(); ++it
)
38 *it
= Qt::convertFromPlainText( *it
, Qt::WhiteSpaceNormal
);
41 return lines
.join(QString::null
); //krazy:exclude=nullstrassign for old broken gcc
44 KRichTextLabel::KRichTextLabel( const QString
&text
, QWidget
*parent
)
46 m_defaultWidth
= qMin(400, KGlobalSettings::desktopGeometry(this).width()*2/5);
51 KRichTextLabel::KRichTextLabel( QWidget
*parent
)
53 m_defaultWidth
= qMin(400, KGlobalSettings::desktopGeometry(this).width()*2/5);
57 void KRichTextLabel::setDefaultWidth(int defaultWidth
)
59 m_defaultWidth
= defaultWidth
;
63 QSizePolicy
KRichTextLabel::sizePolicy() const
65 return QSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Minimum
, false);
68 QSize
KRichTextLabel::minimumSizeHint() const
70 QString qt_text
= qrichtextify( text() );
73 Q3SimpleRichText
rt(qt_text
, font());
74 pref_width
= m_defaultWidth
;
75 rt
.setWidth(pref_width
);
76 int used_width
= rt
.widthUsed();
77 if (used_width
<= pref_width
)
81 int new_width
= (used_width
* 9) / 10;
82 rt
.setWidth(new_width
);
83 int new_height
= rt
.height();
84 if (new_height
> pref_height
)
86 used_width
= rt
.widthUsed();
87 if (used_width
> new_width
)
90 pref_width
= used_width
;
94 if (used_width
> (pref_width
*2))
95 pref_width
= pref_width
*2;
97 pref_width
= used_width
;
100 return QSize(pref_width
, rt
.height());
103 QSize
KRichTextLabel::sizeHint() const
105 return minimumSizeHint();
108 void KRichTextLabel::setText( const QString
&text
) {
109 QLabel::setText(text
);
112 void KRichTextLabel::virtual_hook( int, void* )
113 { /*BASE::virtual_hook( id, data );*/ }
115 #include "krichtextlabel.moc"