krop's commit fixes my problem in a better way, reverting
[kdepim.git] / korganizer / kodecorationlabel.cpp
blobf78295ace6bde5f5f3403dddeceab910582d63f0
1 /*
2 This file is part of KOrganizer.
3 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
4 Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
5 Copyright (C) 2007 Loïc Corbasson <loic.corbasson@gmail.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 As a special exception, permission is given to link this program
22 with any edition of Qt, and distribute the resulting executable,
23 without including the source code for Qt in the source distribution.
26 #include "kodecorationlabel.h"
28 #include <KToolInvocation>
30 #include <QtGui/QMouseEvent>
31 #include <QtGui/QResizeEvent>
33 #include "kodecorationlabel.moc"
35 KODecorationLabel::KODecorationLabel( KOrg::CalendarDecoration::Element *e,
36 QWidget *parent )
37 : QLabel( parent ), mAutomaticSqueeze( true ), mDecorationElement( e ),
38 mShortText( e->shortText() ), mLongText( e->longText() ),
39 mExtensiveText( e->extensiveText() )
41 mPixmap = e->newPixmap( size() );
42 mUrl = e->url();
43 setUrl( e->url() );
45 connect( e, SIGNAL( gotNewExtensiveText( const QString & ) ),
46 this, SLOT( setExtensiveText( const QString & ) ) );
47 connect( e, SIGNAL( gotNewLongText( const QString & ) ),
48 this, SLOT( setLongText( const QString & ) ) );
49 connect( e, SIGNAL( gotNewPixmap( const QPixmap & ) ),
50 this, SLOT( setPixmap( const QPixmap & ) ) );
51 connect( e, SIGNAL( gotNewShortText( const QString & ) ),
52 this, SLOT( setShortText( const QString & ) ) );
53 connect( e, SIGNAL( gotNewUrl( const KUrl & ) ),
54 this, SLOT( setUrl( const KUrl & ) ) );
55 squeezeContentsToLabel();
58 KODecorationLabel::KODecorationLabel( const QString &shortText,
59 const QString &longText,
60 const QString &extensiveText,
61 const QPixmap &pixmap,
62 const KUrl &url,
63 QWidget *parent )
64 : QLabel( parent ), mAutomaticSqueeze( true ), mShortText( shortText ),
65 mLongText( longText ), mExtensiveText( extensiveText ),
66 mPixmap( pixmap )
68 setUrl( url );
70 squeezeContentsToLabel();
73 KODecorationLabel::~KODecorationLabel()
77 void KODecorationLabel::mouseReleaseEvent( QMouseEvent *event )
79 QLabel::mouseReleaseEvent( event );
81 switch ( event->button() ) {
82 case Qt::LeftButton:
83 if ( ! mUrl.isEmpty() ) {
84 KToolInvocation::invokeBrowser( mUrl.url() );
85 setForegroundRole( QPalette::LinkVisited );
87 break;
88 case Qt::MidButton:
89 case Qt::RightButton:
90 default:
91 break;
95 void KODecorationLabel::resizeEvent( QResizeEvent *event )
97 mPixmap = mDecorationElement->newPixmap( event->size() );
98 QLabel::resizeEvent( event );
99 squeezeContentsToLabel();
102 void KODecorationLabel::setExtensiveText( const QString &text )
104 mExtensiveText = text;
105 squeezeContentsToLabel();
108 void KODecorationLabel::setLongText( const QString &text )
110 mLongText = text;
111 squeezeContentsToLabel();
114 void KODecorationLabel::setPixmap( const QPixmap &pixmap )
116 mPixmap = pixmap.scaled( size(), Qt::KeepAspectRatio );
117 squeezeContentsToLabel();
120 void KODecorationLabel::setShortText( const QString &text )
122 mShortText = text;
123 squeezeContentsToLabel();
126 void KODecorationLabel::setText( const QString &text )
128 setLongText( text );
131 void KODecorationLabel::setUrl( const KUrl &url )
133 mUrl = url;
134 QFont f = font();
135 if ( url.isEmpty() ) {
136 setForegroundRole( QPalette::WindowText );
137 f.setUnderline( false );
138 setCursor( QCursor( Qt::ArrowCursor ) );
139 } else {
140 setForegroundRole( QPalette::Link );
141 f.setUnderline( true );
142 setCursor( QCursor( Qt::PointingHandCursor ) );
144 setFont( f );
147 void KODecorationLabel::squeezeContentsToLabel()
149 if ( !mAutomaticSqueeze ) { // The content type to use has been set manually
150 return;
153 QFontMetrics fm( fontMetrics() );
155 int labelWidth = size().width();
156 int longTextWidth = fm.width(mLongText);
157 int extensiveTextWidth = fm.width(mExtensiveText);
159 if ( ! mPixmap.isNull() ) {
160 usePixmap( true );
161 } else if ( ( !mExtensiveText.isEmpty() ) && ( extensiveTextWidth <= labelWidth ) ) {
162 useExtensiveText( true );
163 } else if ( ( !mLongText.isEmpty() ) && ( longTextWidth <= labelWidth ) ) {
164 useLongText( true );
165 } else {
166 useShortText( true );
169 setAlignment( Qt::AlignCenter );
170 setWordWrap( true );
171 QSize msh = QLabel::minimumSizeHint();
172 msh.setHeight( fontMetrics().lineSpacing() );
173 msh.setWidth( 0 );
174 setMinimumSize( msh );
175 setSizePolicy( sizePolicy().horizontalPolicy(),
176 QSizePolicy::MinimumExpanding );
179 void KODecorationLabel::useDefaultText()
181 mAutomaticSqueeze = false;
182 squeezeContentsToLabel();
185 void KODecorationLabel::useExtensiveText( bool allowAutomaticSqueeze )
187 mAutomaticSqueeze = allowAutomaticSqueeze;
188 QLabel::setText( mExtensiveText );
189 setToolTip( QString() );
192 void KODecorationLabel::useLongText( bool allowAutomaticSqueeze )
194 mAutomaticSqueeze = allowAutomaticSqueeze;
195 QLabel::setText( mLongText );
196 setToolTip( mExtensiveText.isEmpty() ? QString() : mExtensiveText );
199 void KODecorationLabel::usePixmap( bool allowAutomaticSqueeze )
201 mAutomaticSqueeze = allowAutomaticSqueeze;
202 QLabel::setPixmap( mPixmap );
203 setToolTip( mExtensiveText.isEmpty() ? mLongText : mExtensiveText );
206 void KODecorationLabel::useShortText( bool allowAutomaticSqueeze )
208 mAutomaticSqueeze = allowAutomaticSqueeze;
209 QLabel::setText( mShortText );
210 setToolTip( mExtensiveText.isEmpty() ? mLongText : mExtensiveText );