Warning, file /office/calligra/libs/widgets/KoItemToolTip.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002   Copyright (c) 2006 Gábor Lehel <illissius@gmail.com>
0003 
0004   This library is free software; you can redistribute it and/or
0005   modify it under the terms of the GNU Library General Public
0006   License as published by the Free Software Foundation; either
0007   version 2 of the License, or (at your option) any later version.
0008 
0009   This library is distributed in the hope that it will be useful,
0010   but WITHOUT ANY WARRANTY; without even the implied warranty of
0011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012   Library General Public License for more details.
0013 
0014   You should have received a copy of the GNU Library General Public License
0015   along with this library; see the file COPYING.LIB.  If not, write to
0016   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017   Boston, MA 02110-1301, USA.
0018 */
0019 #include "KoItemToolTip.h"
0020 
0021 #include <QApplication>
0022 #include <QBasicTimer>
0023 #include <QDesktopWidget>
0024 #include <QModelIndex>
0025 #include <QPainter>
0026 #include <QPaintEvent>
0027 #include <QPersistentModelIndex>
0028 #include <QStyleOptionViewItem>
0029 #include <QTextDocument>
0030 #include <QTimerEvent>
0031 #include <QToolTip>
0032 
0033 class Q_DECL_HIDDEN KoItemToolTip::Private
0034 {
0035     public:
0036         QTextDocument *document;
0037         QPersistentModelIndex index;
0038         QPoint pos;
0039         QBasicTimer timer;
0040 
0041         Private(): document(0) { }
0042 };
0043 
0044 KoItemToolTip::KoItemToolTip()
0045     : d(new Private)
0046 {
0047     d->document = new QTextDocument(this);
0048     setWindowFlags(Qt::FramelessWindowHint  | Qt::ToolTip
0049                   | Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint);
0050     QApplication::instance()->installEventFilter(this);
0051 }
0052 
0053 KoItemToolTip::~KoItemToolTip()
0054 {
0055     delete d;
0056 }
0057 
0058 void KoItemToolTip::showTip(QWidget *widget, const QPoint &pos, const QStyleOptionViewItem &option, const QModelIndex &index)
0059 {
0060     QTextDocument *doc = createDocument(index);
0061 
0062     QPoint p = (isVisible() && index == d->index) ? d->pos : pos;
0063 
0064     if (!isVisible() || index != d->index || doc->toHtml() != d->document->toHtml())
0065     {
0066         d->pos = p;
0067         d->index = index;
0068         delete d->document;
0069         d->document = doc;
0070         updatePosition(widget, p, option);
0071         if (!isVisible())
0072             show();
0073         else
0074             update();
0075         d->timer.start(10000, this);
0076     }
0077     else
0078         delete doc;
0079 }
0080 
0081 void KoItemToolTip::updatePosition(QWidget *widget, const QPoint &pos, const QStyleOptionViewItem &option)
0082 {
0083     const QRect drect = QApplication::desktop()->availableGeometry(widget);
0084     const QSize size = sizeHint();
0085     const int width = size.width(), height = size.height();
0086     const QPoint gpos = widget->mapToGlobal(pos);
0087     const QRect irect(widget->mapToGlobal(option.rect.topLeft()), option.rect.size());
0088 
0089     int y = gpos.y() + 20;
0090     if (y + height > drect.bottom())
0091         y = qMax(drect.top(), irect.top() - height);
0092 
0093     int x;
0094     if (gpos.x() + width < drect.right())
0095         x = gpos.x();
0096     else
0097         x = qMax(drect.left(), gpos.x() - width);
0098 
0099     move(x, y);
0100 
0101     resize(sizeHint());
0102  }
0103 
0104 QSize KoItemToolTip::sizeHint() const
0105 {
0106     return d->document->size().toSize();
0107 }
0108 
0109 void KoItemToolTip::paintEvent(QPaintEvent*)
0110 {
0111     QPainter p(this);
0112     p.initFrom(this);
0113     d->document->drawContents(&p, rect());
0114     p.drawRect(0, 0, width() - 1, height() - 1);
0115 }
0116 
0117 void KoItemToolTip::timerEvent(QTimerEvent *e)
0118 {
0119     if (e->timerId() == d->timer.timerId()) {
0120         hide();
0121     }
0122 }
0123 
0124 bool KoItemToolTip::eventFilter(QObject *object, QEvent *event)
0125 {
0126     switch(event->type())
0127     {
0128         case QEvent::KeyPress:
0129         case QEvent::KeyRelease:
0130         case QEvent::MouseButtonPress:
0131         case QEvent::MouseButtonRelease:
0132         case QEvent::FocusIn:
0133         case QEvent::FocusOut:
0134         case QEvent::Enter:
0135         case QEvent::Leave:
0136             hide();
0137         default: break;
0138     }
0139 
0140     return QFrame::eventFilter(object, event);
0141 }