File indexing completed on 2024-05-12 16:02:30

0001 /*
0002   SPDX-FileCopyrightText: 2006 Gábor Lehel <illissius@gmail.com>
0003 
0004   SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KO_ITEM_TOOLTIP_H
0008 #define KO_ITEM_TOOLTIP_H
0009 
0010 #include <QFrame>
0011 #include "kritawidgetutils_export.h"
0012 
0013 class QStyleOptionViewItem;
0014 class QModelIndex;
0015 class QTextDocument;
0016 
0017 /**
0018  * Base class for tooltips that can show extensive information about
0019  * the contents of the data pointed to by something that contains a
0020  * QModelIndex. Subclasses need to use this data to create a
0021  * QTextDocument that is formatted to provide the complete tooltip.
0022  */
0023 class KRITAWIDGETUTILS_EXPORT KoItemToolTip : public QFrame
0024 {
0025     Q_OBJECT
0026 public:
0027     KoItemToolTip();
0028     ~KoItemToolTip() override;
0029     void showTip(QWidget *widget, const QPoint &pos, const QStyleOptionViewItem &option, const QModelIndex &index);
0030 
0031 protected:
0032 
0033     /**
0034      * Re-implement this to provide the actual tooltip contents.
0035      * For instance:
0036      * @code
0037      *    QTextDocument *doc = new QTextDocument(this);
0038      *
0039      *     QImage thumb = index.data(Qt::UserRole + KisAbstractResourceModel::LargeThumbnail).value<QImage>();
0040      *     doc->addResource(QTextDocument::ImageResource, QUrl("data:thumbnail"), thumb);
0041      *
0042      *     QString name = index.data(Qt::DisplayRole).toString();
0043      *
0044      *     const QString image = QString("<img src=\"data:thumbnail\">");
0045      *     const QString body = QString("<h3 align=\"center\">%1</h3>").arg(name) + image;
0046      *     const QString html = QString("<html><body>%1</body></html>").arg(body);
0047      *
0048      *     doc->setHtml(html);
0049      *     doc->setTextWidth(qMin(doc->size().width(), 500.0));
0050      *
0051      *     return doc;
0052      * @endcode
0053      */
0054     virtual QTextDocument *createDocument(const QModelIndex &index) = 0;
0055 
0056 private:
0057     class Private;
0058     Private* const d;
0059 
0060     void updatePosition(QWidget *widget, const QPoint &pos, const QStyleOptionViewItem &option);
0061 
0062 public:
0063     QSize sizeHint() const override;
0064 
0065 protected:
0066     void paintEvent(QPaintEvent *e) override;
0067     void timerEvent(QTimerEvent *e) override;
0068     bool eventFilter(QObject *object, QEvent *event) override;
0069 };
0070 
0071 #endif