File indexing completed on 2024-04-21 15:05:26

0001 /*
0002     SPDX-FileCopyrightText: 2017 Elvis Angelaccio <elvis.angelaccio@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "ktooltipwidget_test.h"
0008 
0009 #include <QApplication>
0010 #include <QPushButton>
0011 #include <QSpinBox>
0012 #include <QVBoxLayout>
0013 
0014 // Doxygen will generate code snippets from this file.
0015 // We can't use i18n() here, but we want it to show up in the apidox.
0016 #define i18n QStringLiteral
0017 
0018 TestDialog::TestDialog(QWidget *parent)
0019     : QDialog(parent)
0020 {
0021     m_view.setColumnCount(1);
0022     m_view.setRootIsDecorated(false);
0023     m_view.setMouseTracking(true);
0024 
0025     auto item = new QTreeWidgetItem(&m_view);
0026     item->setText(0, QStringLiteral("Hover me! (first item)"));
0027     auto item2 = new QTreeWidgetItem(&m_view);
0028     item2->setText(0, QStringLiteral("Hover me! (second item)"));
0029 
0030     auto spinBox = new QSpinBox(this);
0031     spinBox->setPrefix(QStringLiteral("Hide delay (ms): "));
0032     spinBox->setMaximum(5000);
0033     spinBox->setValue(500);
0034     connect(spinBox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), &m_tooltipWidget, &KToolTipWidget::setHideDelay);
0035     auto layout = new QVBoxLayout(this);
0036     layout->addWidget(spinBox);
0037     layout->addWidget(&m_view);
0038 
0039     //! [show_tooltip_widget]
0040     connect(&m_view, &QAbstractItemView::entered, this, [=](const QModelIndex &index) {
0041         auto rect = m_view.visualRect(index);
0042         const auto pos = m_view.viewport()->mapToGlobal(rect.topLeft());
0043         rect.moveTo(pos);
0044         auto button = new QPushButton(i18n("Push me (row %1)").arg(index.row()), this);
0045         m_tooltipWidget.showBelow(rect, button, m_view.nativeParentWidget()->windowHandle());
0046     });
0047     //! [show_tooltip_widget]
0048 
0049     connect(&m_view, &QAbstractItemView::viewportEntered, &m_tooltipWidget, &KToolTipWidget::hideLater);
0050 }
0051 
0052 int main(int argc, char *argv[])
0053 {
0054     QApplication app(argc, argv);
0055     app.setAttribute(Qt::AA_UseHighDpiPixmaps, true);
0056 
0057     TestDialog dialog;
0058     QObject::connect(&dialog, &QDialog::finished, &app, &QCoreApplication::quit);
0059     dialog.show();
0060 
0061     return app.exec();
0062 }
0063 
0064 #include "moc_ktooltipwidget_test.cpp"