00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <konq_filetip.h>
00023
00024 #include <kfileitem.h>
00025 #include <kglobalsettings.h>
00026 #include <kstandarddirs.h>
00027
00028 #include <QLabel>
00029 #include <QToolTip>
00030 #include <QApplication>
00031 #include <QScrollBar>
00032 #include <QLayout>
00033 #include <QPainter>
00034 #include <QScrollArea>
00035 #include <QTimer>
00036 #include <QPixmap>
00037 #include <QGridLayout>
00038 #include <QEvent>
00039 #include <QFrame>
00040 #include <QResizeEvent>
00041
00042 #ifdef Q_WS_X11
00043 #include <fixx11h.h>
00044 #endif
00045
00046
00047 KonqFileTip::KonqFileTip( QScrollArea* parent )
00048 : QFrame( 0 ),
00049 m_on( false ),
00050 m_preview( false ),
00051 m_filter( false ),
00052 m_corner( 0 ),
00053 m_num( 0 ),
00054 m_view( parent ),
00055 m_previewJob( 0 )
00056 {
00057 setWindowFlags( Qt::FramelessWindowHint | Qt::Tool | Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint );
00058 m_iconLabel = new QLabel(this);
00059 m_textLabel = new QLabel(this);
00060 m_textLabel->setAlignment(Qt::AlignLeft | Qt::AlignTop);
00061
00062 QGridLayout* layout = new QGridLayout(this);
00063 layout->setMargin(8);
00064 layout->setSpacing(0);
00065 layout->addWidget(m_iconLabel, 0, 0);
00066 layout->addWidget(m_textLabel, 0, 1);
00067 layout->setSizeConstraint(QLayout::SetFixedSize);
00068
00069 setPalette( QToolTip::palette() );
00070 setContentsMargins( 1, 1, 1, 1 );
00071 setFrameStyle( QFrame::Plain | QFrame::Box );
00072
00073 m_timer = new QTimer(this);
00074
00075 hide();
00076 }
00077
00078 KonqFileTip::~KonqFileTip()
00079 {
00080 if ( m_previewJob ) {
00081 m_previewJob->kill();
00082 m_previewJob = 0;
00083 }
00084 }
00085
00086 void KonqFileTip::setPreview(bool on)
00087 {
00088 m_preview = on;
00089 if(on)
00090 m_iconLabel->show();
00091 else
00092 m_iconLabel->hide();
00093 }
00094
00095 void KonqFileTip::setOptions( bool on, bool preview, int num )
00096 {
00097 setPreview(preview);
00098 m_on = on;
00099 m_num = num;
00100 }
00101
00102 void KonqFileTip::setItem( const KFileItem &item, const QRect &rect, const QPixmap *pixmap )
00103 {
00104 hideTip();
00105
00106 if (!m_on) return;
00107
00108 if ( m_previewJob ) {
00109 m_previewJob->kill();
00110 m_previewJob = 0;
00111 }
00112
00113 m_rect = rect;
00114 m_item = item;
00115
00116 if ( !m_item.isNull() ) {
00117 if (m_preview) {
00118 if ( pixmap )
00119 m_iconLabel->setPixmap( *pixmap );
00120 else
00121 m_iconLabel->setPixmap( QPixmap() );
00122 }
00123
00124
00125
00126 m_timer->disconnect( this );
00127 connect(m_timer, SIGNAL(timeout()), this, SLOT(startDelayed()));
00128 m_timer->setSingleShot( true );
00129 m_timer->start( 300 );
00130 }
00131 }
00132
00133 void KonqFileTip::reposition()
00134 {
00135 if ( m_rect.isEmpty() || !m_view || !m_view->viewport() ) return;
00136
00137 QRect rect = m_rect;
00138
00139 int xOffset = m_view->horizontalScrollBar()->value();
00140 int yOffset = m_view->verticalScrollBar()->value();
00141 QPoint off = m_view->viewport()->mapToGlobal( QPoint( rect.x() - xOffset, rect.y() - yOffset ) );
00142 rect.moveTopRight( off );
00143
00144 QPoint pos = rect.center();
00145
00146
00147
00148
00149
00150
00151 m_corner = 0;
00152
00153 QRect desk = KGlobalSettings::desktopGeometry(rect.center());
00154 if (rect.center().x() + width() > desk.right())
00155 {
00156
00157 if (pos.x() - width() < 0) {
00158 pos.setX(0);
00159 m_corner = 4;
00160 } else {
00161 pos.setX( pos.x() - width() );
00162 m_corner = 1;
00163 }
00164 }
00165
00166 if (rect.bottom() + height() > desk.bottom())
00167 {
00168
00169 pos.setY( rect.top() - height() );
00170 m_corner += 2;
00171 }
00172 else pos.setY( rect.bottom() + 1 );
00173
00174 move( pos );
00175 update();
00176 }
00177
00178 void KonqFileTip::gotPreview( const KFileItem& item, const QPixmap& pixmap )
00179 {
00180 m_previewJob = 0;
00181 if (item.url() != m_item.url()) return;
00182
00183 m_iconLabel -> setPixmap(pixmap);
00184 }
00185
00186 void KonqFileTip::gotPreviewResult()
00187 {
00188 m_previewJob = 0;
00189 }
00190
00191 void KonqFileTip::paintEvent(QPaintEvent *)
00192 {
00193 QPainter p( this );
00194 static const char * const names[] = {
00195 "arrow_topleft",
00196 "arrow_topright",
00197 "arrow_bottomleft",
00198 "arrow_bottomright"
00199 };
00200
00201 if (m_corner >= 4) {
00202 return;
00203 }
00204
00205 if ( m_corners[m_corner].isNull())
00206 m_corners[m_corner].load( KStandardDirs::locate( "data", QString::fromLatin1( "konqueror/pics/%1.png" ).arg( names[m_corner] ) ) );
00207
00208 QPixmap &pix = m_corners[m_corner];
00209
00210 switch ( m_corner )
00211 {
00212 case 0:
00213 p.drawPixmap( 3, 3, pix );
00214 break;
00215 case 1:
00216 p.drawPixmap( width() - pix.width() - 3, 3, pix );
00217 break;
00218 case 2:
00219 p.drawPixmap( 3, height() - pix.height() - 3, pix );
00220 break;
00221 case 3:
00222 p.drawPixmap( width() - pix.width() - 3, height() - pix.height() - 3, pix );
00223 break;
00224 }
00225 }
00226
00227 void KonqFileTip::setFilter( bool enable )
00228 {
00229 if ( enable == m_filter ) return;
00230
00231 if ( enable ) {
00232 qApp->installEventFilter( this );
00233 }
00234 else {
00235 qApp->removeEventFilter( this );
00236 }
00237 m_filter = enable;
00238 }
00239
00240 void KonqFileTip::showTip()
00241 {
00242 QString text = m_item.getToolTipText(m_num);
00243
00244 if ( text.isEmpty() ) return;
00245
00246 m_timer->disconnect( this );
00247 connect(m_timer, SIGNAL(timeout()), this, SLOT(hideTip()));
00248 m_timer->setSingleShot( true );
00249 m_timer->start( 15000 );
00250
00251 m_textLabel->setText( text );
00252
00253 setFilter( true );
00254
00255 reposition();
00256 show();
00257 }
00258
00259 void KonqFileTip::hideTip()
00260 {
00261 m_timer->stop();
00262 setFilter( false );
00263 if ( isVisible() && m_view && m_view->viewport() &&
00264 (m_view->horizontalScrollBar()->isVisible() || m_view->verticalScrollBar()->isVisible()) )
00265 m_view->viewport()->update();
00266 hide();
00267 }
00268 void KonqFileTip::startDelayed()
00269 {
00270 if ( m_preview ) {
00271 KFileItemList oneItem;
00272 oneItem.append( m_item );
00273
00274 m_previewJob = KIO::filePreview( oneItem, 256, 256, 64, 70, true, true, 0);
00275 connect( m_previewJob, SIGNAL( gotPreview(const KFileItem &, const QPixmap &) ),
00276 this, SLOT( gotPreview(const KFileItem &, const QPixmap &) ) );
00277 connect( m_previewJob, SIGNAL( result( KJob * ) ),
00278 this, SLOT( gotPreviewResult() ) );
00279 }
00280
00281 m_timer->disconnect( this );
00282 connect(m_timer, SIGNAL(timeout()), this, SLOT(showTip()));
00283 m_timer->setSingleShot( true );
00284 m_timer->start( 400 );
00285 }
00286
00287 void KonqFileTip::resizeEvent( QResizeEvent* event )
00288 {
00289 QFrame::resizeEvent(event);
00290 reposition();
00291 }
00292
00293 bool KonqFileTip::eventFilter( QObject *, QEvent *e )
00294 {
00295 switch ( e->type() )
00296 {
00297 case QEvent::Leave:
00298 case QEvent::MouseButtonPress:
00299 case QEvent::MouseButtonRelease:
00300 case QEvent::KeyPress:
00301 case QEvent::KeyRelease:
00302 case QEvent::FocusIn:
00303 case QEvent::FocusOut:
00304 case QEvent::Wheel:
00305 hideTip();
00306 default: break;
00307 }
00308
00309 return false;
00310 }
00311
00312 #include "konq_filetip.moc"