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>
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;
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
46 | Qt::WStyle_Tool
), _view( view
),
47 _widthInverse( false ), _heightInverse( false )
49 setAlignment( Qt::AlignLeft
| Qt::AlignTop
);
50 setFrameStyle( Q3Frame::Box
| Q3Frame::Plain
);
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
);
62 bool ThumbnailView::ThumbnailToolTip::eventFilter( QObject
* o
, QEvent
* event
)
64 if ( o
== _view
->viewport() && event
->type() == QEvent::Leave
)
67 else if ( event
->type() == QEvent::MouseMove
)
68 showToolTips( false );
72 void ThumbnailView::ThumbnailToolTip::showToolTips( bool force
)
74 DB::ResultId id
= _view
->mediaIdUnderCursor();
78 QString fileName
= id
.fetchInfo()->fileName(DB::AbsolutePath
);
79 if ( force
|| (fileName
!= _currentFileName
) ) {
80 if ( loadImage( fileName
) ) {
82 int size
= Settings::SettingsData::instance()->previewSize();
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 ) ) );
89 setText( QString::fromLatin1("<p>%1</p>").arg( Utilities::createInfoText( DB::ImageDB::instance()->info( fileName
, DB::AbsolutePath
), 0 ) ) );
94 _currentFileName
= fileName
;
104 void ThumbnailView::ThumbnailToolTip::setActive( bool b
)
108 _view
->viewport()->installEventFilter( this );
112 _view
->viewport()->removeEventFilter( this );
117 void ThumbnailView::ThumbnailToolTip::placeWindow()
119 // First try to set the position.
120 QPoint pos
= QCursor::pos() + QPoint( 20, 20 );
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;
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;
149 if ( pos
.y() + height() > geom
.bottom() ) {
150 pos
.setY( QCursor::pos().y() - 10 - height() );
151 _heightInverse
= true;
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
);
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
);
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
)
187 #include "ThumbnailToolTip.moc"