00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <QApplication>
00021 #include <QBasicTimer>
00022 #include <QDesktopWidget>
00023 #include <QModelIndex>
00024 #include <QPainter>
00025 #include <QPaintEvent>
00026 #include <QPersistentModelIndex>
00027 #include <QStyleOptionViewItem>
00028 #include <QTextDocument>
00029 #include <QTimerEvent>
00030 #include <QToolTip>
00031 #include "KoItemToolTip.h"
00032
00033 class KoItemToolTip::Private
00034 {
00035 public:
00036 QTextDocument *document;
00037 QPersistentModelIndex index;
00038 QPoint pos;
00039 QBasicTimer timer;
00040
00041 Private(): document( 0 ) { }
00042 };
00043
00044 KoItemToolTip::KoItemToolTip()
00045 : d( new Private )
00046 {
00047 d->document = new QTextDocument( this );
00048 setWindowFlags( Qt::FramelessWindowHint | Qt::Tool
00049 | Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint );
00050 setPalette( QToolTip::palette() );
00051 QApplication::instance()->installEventFilter( this );
00052 }
00053
00054 KoItemToolTip::~KoItemToolTip()
00055 {
00056 delete d;
00057 }
00058
00059 void KoItemToolTip::showTip( QWidget *widget, const QPoint &pos, const QStyleOptionViewItem &option, const QModelIndex &index )
00060 {
00061 QTextDocument *doc = createDocument( index );
00062
00063 QPoint p = ( isVisible() && index == d->index ) ? d->pos : pos;
00064
00065 if( !isVisible() || index != d->index || doc->toHtml() != d->document->toHtml() )
00066 {
00067 d->pos = p;
00068 d->index = index;
00069 delete d->document;
00070 d->document = doc;
00071 updatePosition( widget, p, option );
00072 if( !isVisible() )
00073 show();
00074 else
00075 update();
00076 d->timer.start( 10000, this );
00077 }
00078 else
00079 delete doc;
00080 }
00081
00082 void KoItemToolTip::updatePosition( QWidget *widget, const QPoint &pos, const QStyleOptionViewItem &option )
00083 {
00084 const QRect drect = QApplication::desktop()->availableGeometry( widget );
00085 const QSize size = sizeHint();
00086 const int width = size.width(), height = size.height();
00087 const QPoint gpos = widget->mapToGlobal( pos );
00088 const QRect irect( widget->mapToGlobal( option.rect.topLeft() ), option.rect.size() );
00089
00090 int y = gpos.y() + 20;
00091 if( y + height > drect.bottom() )
00092 y = qMax( drect.top(), irect.top() - height );
00093
00094 int x;
00095 if( gpos.x() + width < drect.right() )
00096 x = gpos.x();
00097 else
00098 x = qMax( drect.left(), gpos.x() - width );
00099
00100 move( x, y );
00101
00102 resize( sizeHint() );
00103 }
00104
00105 QSize KoItemToolTip::sizeHint() const
00106 {
00107 return d->document->size().toSize();
00108 }
00109
00110 void KoItemToolTip::paintEvent( QPaintEvent* )
00111 {
00112 QPainter p( this );
00113 p.initFrom( this );
00114 d->document->drawContents( &p, rect() );
00115 p.drawRect( 0, 0, width() - 1, height() - 1 );
00116 }
00117
00118 void KoItemToolTip::timerEvent( QTimerEvent *e )
00119 {
00120 if( e->timerId() == d->timer.timerId() )
00121 {
00122 hide();
00123 }
00124 }
00125
00126 bool KoItemToolTip::eventFilter( QObject *object, QEvent *event )
00127 {
00128 switch( event->type() )
00129 {
00130 case QEvent::KeyPress:
00131 case QEvent::KeyRelease:
00132 case QEvent::MouseButtonPress:
00133 case QEvent::MouseButtonRelease:
00134 case QEvent::FocusIn:
00135 case QEvent::FocusOut:
00136 case QEvent::Enter:
00137 case QEvent::Leave:
00138 hide();
00139 default: break;
00140 }
00141
00142 return super::eventFilter( object, event );
00143 }
00144
00145 #include "KoItemToolTip.moc"