2022-09-06 11:36:55 +08:00
|
|
|
|
// SPDX-FileCopyrightText: 2011 - 2022 UnionTech Software Technology Co., Ltd.
|
|
|
|
|
//
|
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
2017-09-18 14:33:44 +08:00
|
|
|
|
|
2017-05-23 14:50:32 +08:00
|
|
|
|
#include "floatingpreview.h"
|
|
|
|
|
#include "appsnapshot.h"
|
2017-07-31 17:06:10 +08:00
|
|
|
|
#include "previewcontainer.h"
|
2017-05-23 14:50:32 +08:00
|
|
|
|
|
2019-08-27 15:03:40 +08:00
|
|
|
|
#include <DStyle>
|
|
|
|
|
|
|
|
|
|
#include <QGraphicsEffect>
|
2017-05-23 14:50:32 +08:00
|
|
|
|
#include <QPainter>
|
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
|
2019-08-27 15:03:40 +08:00
|
|
|
|
FloatingPreview::FloatingPreview(QWidget *parent)
|
|
|
|
|
: QWidget(parent)
|
2020-06-13 11:39:32 +08:00
|
|
|
|
, m_closeBtn3D(new DIconButton(this))
|
2021-05-25 13:05:45 +08:00
|
|
|
|
, m_titleBtn(new DPushButton(this))
|
2017-05-23 14:50:32 +08:00
|
|
|
|
{
|
2020-05-06 17:28:16 +08:00
|
|
|
|
m_closeBtn3D->setObjectName("closebutton-3d");
|
2019-03-20 11:23:33 +08:00
|
|
|
|
m_closeBtn3D->setFixedSize(24, 24);
|
2020-06-13 11:39:32 +08:00
|
|
|
|
m_closeBtn3D->setIconSize(QSize(24, 24));
|
|
|
|
|
m_closeBtn3D->setIcon(QIcon(":/icons/resources/close_round_normal.svg"));
|
|
|
|
|
m_closeBtn3D->setFlat(true);
|
|
|
|
|
m_closeBtn3D->installEventFilter(this);
|
2017-05-23 14:50:32 +08:00
|
|
|
|
|
2019-08-27 15:03:40 +08:00
|
|
|
|
m_titleBtn->setBackgroundRole(QPalette::Base);
|
|
|
|
|
m_titleBtn->setForegroundRole(QPalette::Text);
|
|
|
|
|
m_titleBtn->setFocusPolicy(Qt::NoFocus);
|
|
|
|
|
m_titleBtn->setAttribute(Qt::WA_TransparentForMouseEvents);
|
|
|
|
|
|
2017-05-23 14:50:32 +08:00
|
|
|
|
QVBoxLayout *centralLayout = new QVBoxLayout;
|
2019-03-20 11:23:33 +08:00
|
|
|
|
centralLayout->addWidget(m_closeBtn3D);
|
|
|
|
|
centralLayout->setAlignment(m_closeBtn3D, Qt::AlignRight | Qt::AlignTop);
|
2019-08-27 15:03:40 +08:00
|
|
|
|
centralLayout->addWidget(m_titleBtn);
|
|
|
|
|
centralLayout->setAlignment(m_titleBtn, Qt::AlignCenter | Qt::AlignBottom);
|
|
|
|
|
centralLayout->addSpacing(TITLE_MARGIN);
|
2017-05-23 14:50:32 +08:00
|
|
|
|
centralLayout->setMargin(0);
|
|
|
|
|
centralLayout->setSpacing(0);
|
|
|
|
|
|
|
|
|
|
setLayout(centralLayout);
|
|
|
|
|
setFixedSize(SNAP_WIDTH, SNAP_HEIGHT);
|
2017-05-23 15:32:03 +08:00
|
|
|
|
|
2020-06-13 11:39:32 +08:00
|
|
|
|
connect(m_closeBtn3D, &DIconButton::clicked, this, &FloatingPreview::onCloseBtnClicked);
|
2017-05-23 14:50:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-13 11:07:38 +08:00
|
|
|
|
WId FloatingPreview::trackedWid() const
|
|
|
|
|
{
|
|
|
|
|
Q_ASSERT(!m_tracked.isNull());
|
|
|
|
|
|
|
|
|
|
return m_tracked->wid();
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-02 19:54:26 +08:00
|
|
|
|
AppSnapshot *FloatingPreview::trackedWindow()
|
|
|
|
|
{
|
|
|
|
|
return m_tracked;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-14 17:08:36 +08:00
|
|
|
|
void FloatingPreview::setFloatingTitleVisible(bool bVisible)
|
|
|
|
|
{
|
|
|
|
|
m_titleBtn->setVisible(bVisible);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-27 15:03:40 +08:00
|
|
|
|
void FloatingPreview::trackWindow(AppSnapshot *const snap)
|
2017-05-23 14:50:32 +08:00
|
|
|
|
{
|
2021-03-09 15:38:38 +08:00
|
|
|
|
if (!snap)
|
|
|
|
|
return;
|
|
|
|
|
|
2017-05-24 13:34:32 +08:00
|
|
|
|
if (!m_tracked.isNull())
|
|
|
|
|
m_tracked->removeEventFilter(this);
|
2019-08-27 15:03:40 +08:00
|
|
|
|
|
2017-05-24 13:34:32 +08:00
|
|
|
|
snap->installEventFilter(this);
|
2017-05-23 14:50:32 +08:00
|
|
|
|
m_tracked = snap;
|
2019-08-27 15:03:40 +08:00
|
|
|
|
|
2019-03-20 11:23:33 +08:00
|
|
|
|
m_closeBtn3D->setVisible(m_tracked->closeAble());
|
2019-08-27 15:03:40 +08:00
|
|
|
|
|
2021-09-14 17:08:36 +08:00
|
|
|
|
// 显示此标题的前提条件:配置了标题跟随鼠标显示
|
|
|
|
|
// 此对象是共用的,鼠标移动到哪个预览图,title就移动到哪里显示,所以他的text统一snap获取,不再重复计算显示长度
|
|
|
|
|
m_titleBtn->setText(snap->appTitle());
|
2017-05-23 14:50:32 +08:00
|
|
|
|
|
2019-08-27 15:03:40 +08:00
|
|
|
|
QTimer::singleShot(0, this, [ = ] {
|
2021-06-25 20:09:48 +08:00
|
|
|
|
// 此处获取的snap->geometry()有可能是错误的,所以做个判断并且在resizeEvent中也做处理
|
|
|
|
|
if(snap->width() == SNAP_WIDTH)
|
|
|
|
|
setGeometry(snap->geometry());
|
2019-05-22 18:51:09 +08:00
|
|
|
|
});
|
2017-05-23 14:50:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FloatingPreview::paintEvent(QPaintEvent *e)
|
|
|
|
|
{
|
|
|
|
|
QWidget::paintEvent(e);
|
|
|
|
|
|
2017-05-24 10:56:43 +08:00
|
|
|
|
if (m_tracked.isNull())
|
|
|
|
|
return;
|
|
|
|
|
|
2017-12-28 16:57:49 +08:00
|
|
|
|
const QImage &snapshot = m_tracked->snapshot();
|
2018-07-30 11:22:56 +08:00
|
|
|
|
|
2017-06-09 16:18:15 +08:00
|
|
|
|
if (snapshot.isNull())
|
|
|
|
|
return;
|
|
|
|
|
|
2017-05-23 14:50:32 +08:00
|
|
|
|
QPainter painter(this);
|
2017-05-24 14:12:50 +08:00
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
2017-05-23 14:50:32 +08:00
|
|
|
|
|
2019-08-27 15:03:40 +08:00
|
|
|
|
const QRectF r = rect().marginsRemoved(QMargins(BORDER_MARGIN, BORDER_MARGIN, BORDER_MARGIN, BORDER_MARGIN));
|
2017-05-25 12:33:40 +08:00
|
|
|
|
|
2019-08-27 15:03:40 +08:00
|
|
|
|
DStyleHelper dstyle(style());
|
|
|
|
|
const int radius = dstyle.pixelMetric(DStyle::PM_FrameRadius);
|
2017-05-25 12:33:40 +08:00
|
|
|
|
|
2019-08-27 15:03:40 +08:00
|
|
|
|
// 选中外框
|
|
|
|
|
QPen pen;
|
|
|
|
|
pen.setColor(palette().highlight().color());
|
|
|
|
|
pen.setWidth(dstyle.pixelMetric(DStyle::PM_FocusBorderWidth));
|
|
|
|
|
painter.setPen(pen);
|
|
|
|
|
painter.setBrush(Qt::NoBrush);
|
|
|
|
|
painter.drawRoundedRect(r, radius, radius);
|
2017-05-23 14:50:32 +08:00
|
|
|
|
}
|
2017-05-23 15:32:03 +08:00
|
|
|
|
|
2017-05-24 10:24:35 +08:00
|
|
|
|
void FloatingPreview::mouseReleaseEvent(QMouseEvent *e)
|
|
|
|
|
{
|
|
|
|
|
QWidget::mouseReleaseEvent(e);
|
|
|
|
|
|
2019-06-19 16:38:48 +08:00
|
|
|
|
if (m_tracked) {
|
|
|
|
|
emit m_tracked->clicked(m_tracked->wid());
|
|
|
|
|
}
|
2017-05-24 10:24:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-24 13:34:32 +08:00
|
|
|
|
bool FloatingPreview::eventFilter(QObject *watched, QEvent *event)
|
|
|
|
|
{
|
2020-06-13 11:39:32 +08:00
|
|
|
|
if(watched == m_closeBtn3D) {
|
|
|
|
|
if(watched == m_closeBtn3D && (event->type() == QEvent::HoverEnter || event->type() == QEvent::HoverMove)) {
|
|
|
|
|
m_closeBtn3D->setIcon(QIcon(":/icons/resources/close_round_hover.svg"));
|
|
|
|
|
}
|
|
|
|
|
else if (watched == m_closeBtn3D && event->type() == QEvent::HoverLeave) {
|
|
|
|
|
m_closeBtn3D->setIcon(QIcon(":/icons/resources/close_round_normal.svg"));
|
|
|
|
|
}
|
|
|
|
|
else if (watched == m_closeBtn3D && event->type() == QEvent::MouseButtonPress) {
|
|
|
|
|
m_closeBtn3D->setIcon(QIcon(":/icons/resources/close_round_press.svg"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 20:09:48 +08:00
|
|
|
|
if (watched == m_tracked) {
|
2021-07-29 15:34:24 +08:00
|
|
|
|
if (event->type() == QEvent::Destroy) {
|
|
|
|
|
// 此处需要置空,否则当Destroy事件响应结束后,会在FloatingPreview::hideEvent使用m_tracked野指针
|
|
|
|
|
m_tracked = nullptr;
|
2021-06-25 20:09:48 +08:00
|
|
|
|
hide();
|
2021-07-29 15:34:24 +08:00
|
|
|
|
}
|
2021-06-25 20:09:48 +08:00
|
|
|
|
|
|
|
|
|
if (event->type() == QEvent::Resize && m_tracked->width() == SNAP_WIDTH)
|
|
|
|
|
setGeometry(m_tracked->geometry());
|
|
|
|
|
}
|
2017-05-24 13:34:32 +08:00
|
|
|
|
|
|
|
|
|
return QWidget::eventFilter(watched, event);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-02 19:54:26 +08:00
|
|
|
|
void FloatingPreview::hideEvent(QHideEvent *event)
|
|
|
|
|
{
|
2019-06-19 16:38:48 +08:00
|
|
|
|
if (m_tracked) {
|
|
|
|
|
m_tracked->setContentsMargins(0, 0, 0, 0);
|
2020-08-28 10:39:24 +08:00
|
|
|
|
m_tracked->setWindowState();
|
2019-06-19 16:38:48 +08:00
|
|
|
|
}
|
2018-08-02 19:54:26 +08:00
|
|
|
|
|
|
|
|
|
QWidget::hideEvent(event);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-23 15:32:03 +08:00
|
|
|
|
void FloatingPreview::onCloseBtnClicked()
|
|
|
|
|
{
|
|
|
|
|
Q_ASSERT(!m_tracked.isNull());
|
|
|
|
|
|
|
|
|
|
m_tracked->closeWindow();
|
|
|
|
|
}
|