File indexing completed on 2024-05-19 05:42:23
0001 // ct_lvtqtc_tooltip.cpp -*-C++-*- 0002 0003 /* 0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk> 0005 // SPDX-License-Identifier: Apache-2.0 0006 // 0007 // Licensed under the Apache License, Version 2.0 (the "License"); 0008 // you may not use this file except in compliance with the License. 0009 // You may obtain a copy of the License at 0010 // 0011 // http://www.apache.org/licenses/LICENSE-2.0 0012 // 0013 // Unless required by applicable law or agreed to in writing, software 0014 // distributed under the License is distributed on an "AS IS" BASIS, 0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 0016 // See the License for the specific language governing permissions and 0017 // limitations under the License. 0018 */ 0019 0020 #include <ct_lvtqtc_tooltip.h> 0021 0022 #include <QBrush> 0023 #include <QDebug> 0024 #include <QFontMetrics> 0025 #include <QGridLayout> 0026 #include <QLabel> 0027 #include <QMouseEvent> 0028 #include <QPainter> 0029 0030 #include <preferences.h> 0031 0032 namespace Codethink::lvtqtc { 0033 0034 // TODO: Make it configurable 0035 constexpr int ICON_SIZE = 5; 0036 0037 struct ToolTipItem::Private { 0038 using ToolTip = QPair<QLabel *, QLabel *>; 0039 QVector<ToolTip> toolTips; 0040 ToolTip entryToolTip; 0041 QLabel *title = nullptr; 0042 QGridLayout *layout = nullptr; 0043 QPoint movementDelta; 0044 bool isDragging = false; 0045 }; 0046 0047 ToolTipItem::ToolTipItem(QWidget *parent): QWidget(parent), d(std::make_unique<ToolTipItem::Private>()) 0048 { 0049 d->title = new QLabel(tr("Information"), this), 0050 0051 d->entryToolTip.first = nullptr; 0052 d->entryToolTip.second = nullptr; 0053 d->layout = new QGridLayout(); 0054 d->layout->setSizeConstraint(QLayout::SetFixedSize); 0055 d->layout->addWidget(d->title, 2, 1, Qt::AlignmentFlag::AlignHCenter); 0056 setLayout(d->layout); 0057 0058 setStyleSheet("QLabel { color: white; }"); 0059 } 0060 0061 void ToolTipItem::addToolTip(const QString& toolTip, const QIcon& icon, const QPixmap& pixmap) 0062 { 0063 const int currRowCount = d->layout->rowCount(); 0064 auto *iconLabel = new QLabel(); 0065 0066 if (!icon.isNull()) { 0067 iconLabel->setPixmap(icon.pixmap(ICON_SIZE, ICON_SIZE)); 0068 d->layout->addWidget(iconLabel, currRowCount, 0); 0069 } else if (!pixmap.isNull()) { 0070 iconLabel->setPixmap(pixmap); 0071 d->layout->addWidget(iconLabel, currRowCount, 0); 0072 } 0073 0074 auto *textLabel = new QLabel(); 0075 textLabel->setText(toolTip); 0076 d->layout->addWidget(textLabel, currRowCount, 1); 0077 0078 d->toolTips.push_back(qMakePair(iconLabel, textLabel)); 0079 } 0080 0081 void ToolTipItem::clear() 0082 { 0083 for (auto t : qAsConst(d->toolTips)) { 0084 delete t.first; 0085 delete t.second; 0086 } 0087 d->toolTips.clear(); 0088 } 0089 0090 ToolTipItem::~ToolTipItem() 0091 { 0092 clear(); 0093 } 0094 0095 void ToolTipItem::persistPos() 0096 { 0097 // TODO: Implement - me. 0098 } 0099 0100 void ToolTipItem::readPos() 0101 { 0102 // TODO: Implement me. 0103 } 0104 0105 void ToolTipItem::paintEvent(QPaintEvent *ev) 0106 { 0107 QPainter painter(this); 0108 painter.setBrush(QBrush(QColor(50, 50, 50, 200))); 0109 painter.setPen(Qt::NoPen); 0110 painter.drawRoundedRect(0, 0, geometry().width(), geometry().height(), 10, 10); 0111 QWidget::paintEvent(ev); 0112 } 0113 0114 void ToolTipItem::mousePressEvent(QMouseEvent *ev) 0115 { 0116 if (ev->modifiers() & Qt::Modifier(Preferences::dragModifier())) { 0117 QLine line(mapToParent(ev->pos()), pos()); 0118 d->movementDelta.setX(line.dx()); 0119 d->movementDelta.setY(line.dy()); 0120 d->isDragging = true; 0121 } 0122 } 0123 0124 void ToolTipItem::mouseMoveEvent(QMouseEvent *ev) 0125 { 0126 if (!d->isDragging) { 0127 return; 0128 } 0129 0130 move(mapToParent(ev->pos()) + d->movementDelta); 0131 } 0132 0133 void ToolTipItem::mouseReleaseEvent(QMouseEvent *ev) 0134 { 0135 Q_UNUSED(ev); 0136 d->isDragging = false; 0137 } 0138 0139 } // end namespace Codethink::lvtqtc