Krazy/EBN: changes for null strings
[kphotoalbum.git] / ThumbnailView / ThumbnailToolTip.cpp
blobbdabfe96ccc085f2a865f65fe94f38ef66b72f3c
1 /* Copyright (C) 2003-2009 Jesper K. Pedersen <blackie@kde.org>
3 This program is free software; you can redistribute it and/or
4 modify it under the terms of the GNU General Public
5 License as published by the Free Software Foundation; either
6 version 2 of the License, or (at your option) any later version.
8 This program 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 General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; see the file COPYING. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
19 #include "ThumbnailToolTip.h"
21 #include <QDesktopWidget>
22 #include <QTemporaryFile>
23 #include <QApplication>
24 #include <QCursor>
26 #include "DB/ImageDB.h"
27 #include "DB/ImageInfo.h"
28 #include "ImageManager/Manager.h"
29 #include "Settings/SettingsData.h"
30 #include "ThumbnailWidget.h"
31 #include "Utilities/Util.h"
33 QTemporaryFile* _tmpFileForThumbnailView = 0;
35 /**
36 \class ThumbnailToolTip
37 This class takes care of showing tooltips for the individual items in the thumbnail view.
38 I tried implementing this with QToolTip::maybeTip() on the iconview, but it had the
39 disadvantages that either the tooltip would not follow the
40 mouse( and would therefore stand on top of the image), or it flickered.
43 ThumbnailView::ThumbnailToolTip::ThumbnailToolTip( ThumbnailWidget* view )
44 : QLabel( view, Qt::WStyle_Customize | Qt::WStyle_NoBorder | Qt::WType_TopLevel
45 | Qt::WX11BypassWM
46 | Qt::WStyle_Tool ), _view( view ),
47 _widthInverse( false ), _heightInverse( false )
49 setAlignment( Qt::AlignLeft | Qt::AlignTop );
50 setFrameStyle( Q3Frame::Box | Q3Frame::Plain );
51 setLineWidth(1);
52 setMargin(1);
54 setWindowOpacity(0.8);
55 setAutoFillBackground(true);
56 QPalette p = palette();
57 p.setColor(QPalette::Background, QColor(0,0,0,170)); // r,g,b,A
58 p.setColor(QPalette::WindowText, Qt::white );
59 setPalette(p);
62 bool ThumbnailView::ThumbnailToolTip::eventFilter( QObject* o , QEvent* event )
64 if ( o == _view->viewport() && event->type() == QEvent::Leave )
65 hide();
67 else if ( event->type() == QEvent::MouseMove )
68 showToolTips( false );
69 return false;
72 void ThumbnailView::ThumbnailToolTip::showToolTips( bool force )
74 DB::ResultId id = _view->mediaIdUnderCursor();
75 if ( id.isNull() )
76 return;
78 QString fileName = id.fetchInfo()->fileName(DB::AbsolutePath);
79 if ( force || (fileName != _currentFileName) ) {
80 if ( loadImage( fileName ) ) {
81 setText( QString() );
82 int size = Settings::SettingsData::instance()->previewSize();
83 if ( size != 0 ) {
84 setText( QString::fromLatin1("<table cols=\"2\" cellpadding=\"10\"><tr><td><img src=\"%1\"></td><td>%2</td></tr>")
85 .arg(_tmpFileForThumbnailView->fileName()).
86 arg(Utilities::createInfoText( DB::ImageDB::instance()->info( fileName, DB::AbsolutePath ), 0 ) ) );
88 else {
89 setText( QString::fromLatin1("<p>%1</p>").arg( Utilities::createInfoText( DB::ImageDB::instance()->info( fileName, DB::AbsolutePath ), 0 ) ) );
91 setWordWrap( true );
94 _currentFileName = fileName;
95 resize( sizeHint() );
96 _view->setFocus();
99 placeWindow();
100 show();
104 void ThumbnailView::ThumbnailToolTip::setActive( bool b )
106 if ( b ) {
107 showToolTips(true);
108 _view->viewport()->installEventFilter( this );
109 show();
111 else {
112 _view->viewport()->removeEventFilter( this );
113 hide();
117 void ThumbnailView::ThumbnailToolTip::placeWindow()
119 // First try to set the position.
120 QPoint pos = QCursor::pos() + QPoint( 20, 20 );
121 if ( _widthInverse )
122 pos.setX( pos.x() - 30 - width() );
123 if ( _heightInverse )
124 pos.setY( pos.y() - 30 - height() );
126 QRect geom = qApp->desktop()->screenGeometry( QCursor::pos() );
128 // Now test whether the window moved outside the screen
129 if ( _widthInverse ) {
130 if ( pos.x() < geom.x() ) {
131 pos.setX( QCursor::pos().x() + 20 );
132 _widthInverse = false;
135 else {
136 if ( pos.x() + width() > geom.right() ) {
137 pos.setX( QCursor::pos().x() - width() );
138 _widthInverse = true;
142 if ( _heightInverse ) {
143 if ( pos.y() < geom.y() ) {
144 pos.setY( QCursor::pos().y() + 10 );
145 _heightInverse = false;
148 else {
149 if ( pos.y() + height() > geom.bottom() ) {
150 pos.setY( QCursor::pos().y() - 10 - height() );
151 _heightInverse = true;
155 move( pos );
159 bool ThumbnailView::ThumbnailToolTip::loadImage( const QString& fileName )
161 int size = Settings::SettingsData::instance()->previewSize();
162 DB::ImageInfoPtr info = DB::ImageDB::instance()->info( fileName, DB::AbsolutePath );
163 if ( size != 0 ) {
164 if ( fileName != _currentFileName ) {
165 ImageManager::ImageRequest* request = new ImageManager::ImageRequest( fileName, QSize( size, size ), info->angle(), this );
166 // request->setCache(); // TODO: do caching in callback.
167 request->setPriority( ImageManager::Viewer );
168 ImageManager::Manager::instance()->load( request );
169 return false;
172 return true;
175 void ThumbnailView::ThumbnailToolTip::pixmapLoaded( const QString& fileName, const QSize& /*size*/,
176 const QSize& /*fullSize*/, int /*angle*/, const QImage& image, const bool /*loadedOK*/)
178 delete _tmpFileForThumbnailView;
179 _tmpFileForThumbnailView = new QTemporaryFile(this);
180 _tmpFileForThumbnailView->open();
182 image.save(_tmpFileForThumbnailView, "PNG" );
183 if ( fileName == _currentFileName )
184 showToolTips(true);
187 #include "ThumbnailToolTip.moc"