2022-05-17 20:57:09 +08:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2022 ~ 2022 Deepin Technology Co., Ltd.
|
|
|
|
|
*
|
|
|
|
|
* Author: donghualin <donghualin@uniontech.com>
|
|
|
|
|
*
|
|
|
|
|
* Maintainer: donghualin <donghualin@uniontech.com>
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2022-05-12 17:09:10 +08:00
|
|
|
|
#include "expandiconwidget.h"
|
|
|
|
|
#include "tray_gridview.h"
|
|
|
|
|
#include "tray_model.h"
|
|
|
|
|
#include "tray_delegate.h"
|
|
|
|
|
#include "dockpopupwindow.h"
|
2022-06-15 13:35:50 +00:00
|
|
|
|
#include "imageutil.h"
|
2022-05-12 17:09:10 +08:00
|
|
|
|
|
|
|
|
|
#include <DGuiApplicationHelper>
|
|
|
|
|
#include <DRegionMonitor>
|
2022-05-17 20:57:09 +08:00
|
|
|
|
#include <QBitmap>
|
2022-05-12 17:09:10 +08:00
|
|
|
|
#include <QPainter>
|
2022-05-17 20:57:09 +08:00
|
|
|
|
#include <QPainterPath>
|
2022-05-12 17:09:10 +08:00
|
|
|
|
|
|
|
|
|
#include <xcb/xproto.h>
|
|
|
|
|
|
|
|
|
|
DGUI_USE_NAMESPACE
|
|
|
|
|
|
|
|
|
|
ExpandIconWidget::ExpandIconWidget(QWidget *parent, Qt::WindowFlags f)
|
|
|
|
|
: BaseTrayWidget(parent, f)
|
|
|
|
|
, m_regionInter(new DRegionMonitor(this))
|
|
|
|
|
, m_position(Dock::Position::Bottom)
|
|
|
|
|
{
|
2022-11-02 06:57:46 +00:00
|
|
|
|
connect(m_regionInter, &DRegionMonitor::buttonPress, this, [ = ](const QPoint &mousePos, const int flag) {
|
|
|
|
|
TrayGridWidget *gridView = popupTrayView();
|
|
|
|
|
// 如果当前是隐藏,那么在点击任何地方都隐藏
|
|
|
|
|
if (!isVisible()) {
|
|
|
|
|
gridView->hide();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((flag != DRegionMonitor::WatchedFlags::Button_Left) && (flag != DRegionMonitor::WatchedFlags::Button_Right))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
QPoint ptPos = parentWidget()->mapToGlobal(this->pos());
|
|
|
|
|
const QRect rect = QRect(ptPos, size());
|
|
|
|
|
if (rect.contains(mousePos))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const QRect rctView(gridView->pos(), gridView->size());
|
|
|
|
|
if (rctView.contains(mousePos))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
gridView->hide();
|
|
|
|
|
});
|
2022-05-12 17:09:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ExpandIconWidget::~ExpandIconWidget()
|
|
|
|
|
{
|
2022-11-02 06:57:46 +00:00
|
|
|
|
TrayGridWidget *gridView = popupTrayView();
|
|
|
|
|
gridView->setOwnerWidget(nullptr);
|
|
|
|
|
setTrayPanelVisible(false);
|
2022-05-12 17:09:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-02 06:57:46 +00:00
|
|
|
|
void ExpandIconWidget::setPositon(Dock::Position position)
|
2022-05-12 17:09:10 +08:00
|
|
|
|
{
|
2022-11-02 06:57:46 +00:00
|
|
|
|
if (m_position != position)
|
|
|
|
|
m_position = position;
|
2022-05-17 20:57:09 +08:00
|
|
|
|
|
2022-11-02 06:57:46 +00:00
|
|
|
|
TrayGridWidget::setPosition(position);
|
2022-05-12 17:09:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ExpandIconWidget::sendClick(uint8_t mouseButton, int x, int y)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(x);
|
|
|
|
|
Q_UNUSED(y);
|
|
|
|
|
|
2022-11-02 06:57:46 +00:00
|
|
|
|
// 如果当前图标不可见,则不让展开托盘列表
|
|
|
|
|
if (popupTrayView()->trayView()->model()->rowCount() == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2022-05-12 17:09:10 +08:00
|
|
|
|
if (mouseButton != XCB_BUTTON_INDEX_1)
|
|
|
|
|
return;
|
|
|
|
|
|
2022-10-19 03:50:12 +00:00
|
|
|
|
QWidget *gridParentView = popupTrayView();
|
|
|
|
|
setTrayPanelVisible(!gridParentView->isVisible());
|
2022-05-12 17:09:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ExpandIconWidget::setTrayPanelVisible(bool visible)
|
|
|
|
|
{
|
2022-11-02 06:57:46 +00:00
|
|
|
|
TrayGridWidget *gridParentView = popupTrayView();
|
2022-05-12 17:09:10 +08:00
|
|
|
|
if (visible) {
|
2022-11-02 06:57:46 +00:00
|
|
|
|
gridParentView->resetPosition();
|
2022-10-19 03:50:12 +00:00
|
|
|
|
gridParentView->show();
|
2022-05-12 17:09:10 +08:00
|
|
|
|
m_regionInter->registerRegion();
|
|
|
|
|
} else {
|
2022-10-19 03:50:12 +00:00
|
|
|
|
gridParentView->hide();
|
2022-05-12 17:09:10 +08:00
|
|
|
|
m_regionInter->unregisterRegion();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QPixmap ExpandIconWidget::icon()
|
|
|
|
|
{
|
|
|
|
|
return QPixmap(dropIconFile());
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 03:50:12 +00:00
|
|
|
|
void ExpandIconWidget::paintEvent(QPaintEvent *event)
|
2022-05-12 17:09:10 +08:00
|
|
|
|
{
|
2022-11-02 06:57:46 +00:00
|
|
|
|
TrayGridWidget *gridView = popupTrayView();
|
|
|
|
|
if (gridView->trayView()->model()->rowCount() == 0)
|
2022-10-19 03:50:12 +00:00
|
|
|
|
return BaseTrayWidget::paintEvent(event);
|
|
|
|
|
|
2022-05-12 17:09:10 +08:00
|
|
|
|
QPainter painter(this);
|
2022-06-15 13:35:50 +00:00
|
|
|
|
QPixmap pixmap = ImageUtil::loadSvg(dropIconFile(), QSize(ICON_SIZE, ICON_SIZE));
|
|
|
|
|
QRect rectOfPixmap(rect().x() + (rect().width() - ICON_SIZE) / 2,
|
|
|
|
|
rect().y() + (rect().height() - ICON_SIZE) / 2,
|
|
|
|
|
ICON_SIZE, ICON_SIZE);
|
|
|
|
|
|
|
|
|
|
painter.drawPixmap(rectOfPixmap, pixmap);
|
2022-11-02 06:57:46 +00:00
|
|
|
|
|
|
|
|
|
gridView->setOwnerWidget(this);
|
2022-05-12 17:09:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QString ExpandIconWidget::dropIconFile() const
|
|
|
|
|
{
|
|
|
|
|
QString arrow;
|
|
|
|
|
switch (m_position) {
|
|
|
|
|
case Dock::Position::Bottom: {
|
|
|
|
|
arrow = "up";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Dock::Position::Top: {
|
|
|
|
|
arrow = "down";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Dock::Position::Left: {
|
|
|
|
|
arrow = "right";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Dock::Position::Right: {
|
|
|
|
|
arrow = "left";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString iconFile = QString(":/icons/resources/arrow-%1").arg(arrow);
|
|
|
|
|
if (DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType)
|
|
|
|
|
iconFile += QString("-dark");
|
|
|
|
|
|
|
|
|
|
return iconFile + ".svg";
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 03:50:12 +00:00
|
|
|
|
TrayGridWidget *ExpandIconWidget::popupTrayView()
|
2022-05-17 20:57:09 +08:00
|
|
|
|
{
|
2022-10-19 03:50:12 +00:00
|
|
|
|
static TrayGridWidget *gridParentView = nullptr;
|
|
|
|
|
if (gridParentView)
|
|
|
|
|
return gridParentView;
|
|
|
|
|
|
|
|
|
|
gridParentView = new TrayGridWidget(nullptr);
|
|
|
|
|
TrayGridView *trayView = new TrayGridView(gridParentView);
|
|
|
|
|
TrayDelegate *trayDelegate = new TrayDelegate(trayView, trayView);
|
2022-11-02 06:57:46 +00:00
|
|
|
|
TrayModel *trayModel = TrayModel::getIconModel();
|
2022-10-19 03:50:12 +00:00
|
|
|
|
gridParentView->setTrayGridView(trayView);
|
|
|
|
|
|
|
|
|
|
gridParentView->setWindowFlags(Qt::FramelessWindowHint | Qt::Tool);
|
|
|
|
|
trayView->setModel(trayModel);
|
|
|
|
|
trayView->setItemDelegate(trayDelegate);
|
|
|
|
|
trayView->setSpacing(ITEM_SPACING);
|
|
|
|
|
trayView->setDragDistance(2);
|
|
|
|
|
|
|
|
|
|
QVBoxLayout *layout = new QVBoxLayout(gridParentView);
|
|
|
|
|
layout->setContentsMargins(ITEM_SPACING, ITEM_SPACING, ITEM_SPACING, ITEM_SPACING);
|
|
|
|
|
layout->setSpacing(0);
|
|
|
|
|
layout->addWidget(trayView);
|
|
|
|
|
|
|
|
|
|
auto rowCountChanged = [ = ] {
|
|
|
|
|
int count = trayModel->rowCount();
|
|
|
|
|
if (count > 0)
|
2022-11-02 06:57:46 +00:00
|
|
|
|
gridParentView->resetPosition();
|
2022-10-19 03:50:12 +00:00
|
|
|
|
else if (gridParentView->isVisible())
|
|
|
|
|
gridParentView->hide();
|
|
|
|
|
};
|
|
|
|
|
|
2022-11-02 06:57:46 +00:00
|
|
|
|
connect(trayModel, &TrayModel::rowCountChanged, gridParentView, rowCountChanged);
|
2022-10-19 03:50:12 +00:00
|
|
|
|
|
2022-11-02 06:57:46 +00:00
|
|
|
|
connect(trayDelegate, &TrayDelegate::removeRow, trayView, [ = ](const QModelIndex &index) {
|
2022-10-19 03:50:12 +00:00
|
|
|
|
trayView->model()->removeRow(index.row(),index.parent());
|
|
|
|
|
});
|
|
|
|
|
connect(trayView, &TrayGridView::requestRemove, trayModel, &TrayModel::removeRow);
|
2022-11-02 06:57:46 +00:00
|
|
|
|
connect(trayModel, &TrayModel::requestOpenEditor, trayView, [ trayView ](const QModelIndex &index) {
|
|
|
|
|
trayView->openPersistentEditor(index);
|
2022-10-19 03:50:12 +00:00
|
|
|
|
});
|
|
|
|
|
|
2022-11-02 06:57:46 +00:00
|
|
|
|
QMetaObject::invokeMethod(gridParentView, rowCountChanged, Qt::QueuedConnection);
|
2022-10-19 03:50:12 +00:00
|
|
|
|
|
|
|
|
|
return gridParentView;
|
2022-05-12 17:09:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-17 20:57:09 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief 圆角窗体的绘制
|
|
|
|
|
* @param parent
|
|
|
|
|
*/
|
2022-06-21 16:24:39 +08:00
|
|
|
|
|
2022-11-02 06:57:46 +00:00
|
|
|
|
Dock::Position TrayGridWidget::m_position = Dock::Position::Bottom;
|
|
|
|
|
|
2022-10-19 03:50:12 +00:00
|
|
|
|
TrayGridWidget::TrayGridWidget(QWidget *parent)
|
2022-05-17 20:57:09 +08:00
|
|
|
|
: QWidget (parent)
|
2022-06-21 16:24:39 +08:00
|
|
|
|
, m_dockInter(new DockInter(dockServiceName(), dockServicePath(), QDBusConnection::sessionBus(), this))
|
2022-10-19 03:50:12 +00:00
|
|
|
|
, m_trayGridView(nullptr)
|
2022-11-02 06:57:46 +00:00
|
|
|
|
, m_ownerWidget(nullptr)
|
2022-05-17 20:57:09 +08:00
|
|
|
|
{
|
|
|
|
|
setAttribute(Qt::WA_TranslucentBackground);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-02 06:57:46 +00:00
|
|
|
|
void TrayGridWidget::setPosition(const Dock::Position &position)
|
|
|
|
|
{
|
|
|
|
|
m_position = position;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 03:50:12 +00:00
|
|
|
|
void TrayGridWidget::setTrayGridView(TrayGridView *trayView)
|
|
|
|
|
{
|
|
|
|
|
m_trayGridView = trayView;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-02 06:57:46 +00:00
|
|
|
|
void TrayGridWidget::setOwnerWidget(QWidget *widget)
|
|
|
|
|
{
|
|
|
|
|
// 设置所属的Widget,目的是为了计算当前窗体的具体位置
|
|
|
|
|
m_ownerWidget = widget;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 03:50:12 +00:00
|
|
|
|
TrayGridView *TrayGridWidget::trayView() const
|
|
|
|
|
{
|
|
|
|
|
return m_trayGridView;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-02 06:57:46 +00:00
|
|
|
|
void TrayGridWidget::resetPosition()
|
|
|
|
|
{
|
|
|
|
|
// 如果没有设置所属窗体,则无法计算位置
|
|
|
|
|
if (!m_ownerWidget || !m_ownerWidget->parentWidget())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
QWidget *topWidget = m_ownerWidget->topLevelWidget();
|
|
|
|
|
QPoint ptPos = m_ownerWidget->parentWidget()->mapToGlobal(m_ownerWidget->pos());
|
|
|
|
|
switch (m_position) {
|
|
|
|
|
case Dock::Position::Bottom: {
|
|
|
|
|
ptPos.setX(ptPos.x() - width());
|
|
|
|
|
ptPos.setY(topWidget->y() - height());
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Dock::Position::Top: {
|
|
|
|
|
ptPos.setY(topWidget->y() + topWidget->height());
|
|
|
|
|
ptPos.setX(ptPos.x() - width());
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Dock::Position::Left: {
|
|
|
|
|
ptPos.setX(topWidget->x() + topWidget->width());
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Dock::Position::Right: {
|
|
|
|
|
ptPos.setX(topWidget->x() - width());
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
m_trayGridView->setFixedSize(m_trayGridView->suitableSize());
|
|
|
|
|
setFixedSize(m_trayGridView->size() + QSize(ITEM_SPACING * 2, ITEM_SPACING * 2));
|
|
|
|
|
move(ptPos);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 03:50:12 +00:00
|
|
|
|
void TrayGridWidget::paintEvent(QPaintEvent *event)
|
2022-05-17 20:57:09 +08:00
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(event);
|
|
|
|
|
|
|
|
|
|
QPainter painter(this);
|
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|
|
|
|
QPainterPath path;
|
|
|
|
|
path.addRoundedRect(rect(), 18, 18);
|
|
|
|
|
painter.setCompositionMode(QPainter::CompositionMode_Xor);
|
|
|
|
|
painter.setClipPath(path);
|
|
|
|
|
|
|
|
|
|
painter.fillPath(path, maskColor());
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 03:50:12 +00:00
|
|
|
|
QColor TrayGridWidget::maskColor() const
|
2022-05-17 20:57:09 +08:00
|
|
|
|
{
|
|
|
|
|
QColor color = DGuiApplicationHelper::standardPalette(DGuiApplicationHelper::instance()->themeType()).window().color();
|
|
|
|
|
int maskAlpha(static_cast<int>(255 * m_dockInter->opacity()));
|
|
|
|
|
color.setAlpha(maskAlpha);
|
|
|
|
|
return color;
|
2022-05-12 17:09:10 +08:00
|
|
|
|
}
|