| 1 |
/* |
| 2 |
* Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co., Ltd. |
| 3 |
* |
| 4 |
* Author: ZhangYong <[email protected]> |
| 5 |
* |
| 6 |
* Maintainer: ZhangYong <[email protected]> |
| 7 |
* |
| 8 |
* This program is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
#include "imagebutton.h" |
| 22 |
#include "application.h" |
| 23 |
#include <QApplication> |
| 24 |
#include <QDesktopWidget> |
| 25 |
#include <QFrame> |
| 26 |
#include <QFile> |
| 27 |
#include <QHelpEvent> |
| 28 |
#include <QDebug> |
| 29 |
#include <QHBoxLayout> |
| 30 |
#include <QTimer> |
| 31 |
|
| 32 |
ImageButton::ImageButton(QWidget *parent) |
| 33 |
: DImageButton(parent), m_tooltipVisiable(false) |
| 34 |
{ |
| 35 |
onThemeChanged(dApp->viewerTheme->getCurrentTheme()); |
| 36 |
connect(dApp->viewerTheme, &ViewerThemeManager::viewerThemeChanged, this, |
| 37 |
&ImageButton::onThemeChanged); |
| 38 |
} |
| 39 |
|
| 40 |
ImageButton::ImageButton(const QString &normalPic, const QString &hoverPic, |
| 41 |
const QString &pressPic, const QString &disablePic, |
| 42 |
QWidget *parent) |
| 43 |
: DImageButton(normalPic, hoverPic, pressPic, parent) |
| 44 |
, m_tooltipVisiable(false) |
| 45 |
, m_disablePic_(disablePic) |
| 46 |
{ |
| 47 |
onThemeChanged(dApp->viewerTheme->getCurrentTheme()); |
| 48 |
connect(dApp->viewerTheme, &ViewerThemeManager::viewerThemeChanged, this, |
| 49 |
&ImageButton::onThemeChanged); |
| 50 |
} |
| 51 |
|
| 52 |
//void ImageButton::setDisablePic(const QString &path) |
| 53 |
//{ |
| 54 |
// m_disablePic_ = path; |
| 55 |
//} |
| 56 |
|
| 57 |
void ImageButton::setDisabled(bool d) |
| 58 |
{ |
| 59 |
// if (d) { |
| 60 |
// setNormalPic(m_disablePic_); |
| 61 |
// } |
| 62 |
// else { |
| 63 |
// setNormalPic(this->getNormalPic()); |
| 64 |
// } |
| 65 |
DImageButton::setDisabled(d); |
| 66 |
} |
| 67 |
|
| 68 |
bool ImageButton::event(QEvent *e) |
| 69 |
{ |
| 70 |
if (e->type() == QEvent::ToolTip) { |
| 71 |
if (QHelpEvent *he = static_cast<QHelpEvent *>(e)) { |
| 72 |
showTooltip(he->globalPos()); |
| 73 |
|
| 74 |
return false; |
| 75 |
} |
| 76 |
} else if (e->type() == QEvent::Leave) { |
| 77 |
emit mouseLeave(); |
| 78 |
DImageButton::leaveEvent(e); |
| 79 |
} else if (e->type() == QEvent::MouseButtonPress) { |
| 80 |
emit mouseLeave(); |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
return DImageButton::event(e); |
| 85 |
} |
| 86 |
|
| 87 |
void ImageButton::onThemeChanged(ViewerThemeManager::AppTheme theme) |
| 88 |
{ |
| 89 |
Q_UNUSED(theme) |
| 90 |
} |
| 91 |
|
| 92 |
void ImageButton::setTooltipVisible(bool visible) |
| 93 |
{ |
| 94 |
m_tooltipVisiable = visible; |
| 95 |
} |
| 96 |
|
| 97 |
//bool ImageButton::tooltipVisible() { |
| 98 |
// return m_tooltipVisiable; |
| 99 |
//} |
| 100 |
|
| 101 |
void ImageButton::enterEvent(QEvent *e) |
| 102 |
{ |
| 103 |
if (isEnabled()) { |
| 104 |
DImageButton::enterEvent(e); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
void ImageButton::showTooltip(const QPoint &gPos) |
| 109 |
{ |
| 110 |
Q_UNUSED(gPos) |
| 111 |
return; |
| 112 |
//未设置toolTip,toolTip()始终为空 |
| 113 |
/*if (toolTip().trimmed().isEmpty() || m_tooltipVisiable) { |
| 114 |
return; |
| 115 |
} else { |
| 116 |
m_tooltipVisiable = true; |
| 117 |
} |
| 118 |
|
| 119 |
QFrame *tf = new QFrame(); |
| 120 |
// tf->setStyleSheet(this->styleSheet()); |
| 121 |
tf->setWindowFlags(Qt::ToolTip); |
| 122 |
tf->setAttribute(Qt::WA_TranslucentBackground); |
| 123 |
QLabel *tl = new QLabel(tf); |
| 124 |
tl->setObjectName("ButtonTooltip"); |
| 125 |
tl->setText(toolTip()); |
| 126 |
QHBoxLayout *layout = new QHBoxLayout(tf); |
| 127 |
layout->setContentsMargins(0, 0, 0, 0); |
| 128 |
layout->addWidget(tl); |
| 129 |
|
| 130 |
tf->show(); |
| 131 |
QRect dr = qApp->desktop()->geometry(); |
| 132 |
int y = gPos.y() + tf->height(); |
| 133 |
if (y > dr.y() + dr.height()) { |
| 134 |
y = gPos.y() - tf->height() - 10; |
| 135 |
} |
| 136 |
tf->move(gPos.x() - tf->width() / 3, y - tf->height() / 3); |
| 137 |
|
| 138 |
QTimer::singleShot(5000, tf, SLOT(deleteLater())); |
| 139 |
|
| 140 |
connect(tf, &QFrame::destroyed, this, [ = ] { |
| 141 |
m_tooltipVisiable = false; |
| 142 |
}); |
| 143 |
|
| 144 |
connect(this, &ImageButton::mouseLeave, tf, &QFrame::deleteLater);*/ |
| 145 |
} |