143 lines
3.6 KiB
C++
143 lines
3.6 KiB
C++
#include "dockbar.h"
|
|
#include <QHBoxLayout>
|
|
#include <QVBoxLayout>
|
|
#include <QPainter>
|
|
#include <QPainterPath>
|
|
#include <QPushButton>
|
|
|
|
DockBar::DockBar(QWidget *parent)
|
|
: QWidget(parent)
|
|
{
|
|
setupUI();
|
|
}
|
|
|
|
DockBar::~DockBar()
|
|
{
|
|
}
|
|
|
|
void DockBar::setupUI()
|
|
{
|
|
setObjectName("dockBar");
|
|
}
|
|
|
|
void DockBar::setApps(const QVector<AppIconData> &apps)
|
|
{
|
|
m_apps = apps;
|
|
createDockIcons();
|
|
}
|
|
|
|
void DockBar::createDockIcons()
|
|
{
|
|
// 清除现有布局
|
|
if (layout())
|
|
{
|
|
QLayoutItem *item;
|
|
while ((item = layout()->takeAt(0)) != nullptr)
|
|
{
|
|
if (item->widget())
|
|
{
|
|
delete item->widget();
|
|
}
|
|
delete item;
|
|
}
|
|
delete layout();
|
|
}
|
|
m_icons.clear();
|
|
|
|
// 创建水平布局
|
|
QHBoxLayout *layout = new QHBoxLayout(this);
|
|
layout->setContentsMargins(30, 8, 30, 8);
|
|
layout->setSpacing(20);
|
|
|
|
layout->addStretch();
|
|
|
|
// 创建Dock图标
|
|
for (const auto &appData : m_apps)
|
|
{
|
|
// 创建图标容器
|
|
QWidget *iconWidget = new QWidget(this);
|
|
iconWidget->setFixedSize(60, 75);
|
|
iconWidget->setCursor(Qt::PointingHandCursor);
|
|
|
|
QVBoxLayout *iconLayout = new QVBoxLayout(iconWidget);
|
|
iconLayout->setContentsMargins(0, 0, 0, 0);
|
|
iconLayout->setSpacing(2);
|
|
|
|
// 图标按钮 - 触摸屏优化尺寸
|
|
QPushButton *btn = new QPushButton(iconWidget);
|
|
btn->setFixedSize(52, 52);
|
|
btn->setProperty("appId", appData.id);
|
|
|
|
QString gradientStart = appData.gradient.size() > 0 ? appData.gradient[0] : appData.color;
|
|
QString gradientEnd = appData.gradient.size() > 1 ? appData.gradient[1] : appData.color;
|
|
|
|
// 获取图标符号
|
|
QString iconText = getIconSymbol(appData.id);
|
|
btn->setText(iconText);
|
|
|
|
btn->setStyleSheet(QString(R"(
|
|
QPushButton {
|
|
background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 %1, stop:1 %2);
|
|
border-radius: 15px;
|
|
font-size: 28px;
|
|
color: white;
|
|
border: none;
|
|
}
|
|
QPushButton:hover {
|
|
background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 %1, stop:1 %1);
|
|
}
|
|
QPushButton:pressed {
|
|
background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 %2, stop:1 %2);
|
|
}
|
|
)")
|
|
.arg(gradientStart, gradientEnd));
|
|
|
|
connect(btn, &QPushButton::clicked, this, [this, appData]()
|
|
{ emit appClicked(appData.id); });
|
|
|
|
iconLayout->addWidget(btn, 0, Qt::AlignCenter);
|
|
|
|
// 名称标签 - 深色文字配合白色背景
|
|
QLabel *nameLabel = new QLabel(appData.name, iconWidget);
|
|
nameLabel->setAlignment(Qt::AlignCenter);
|
|
nameLabel->setStyleSheet("font-size: 11px; color: #555; background: transparent; font-weight: 500;");
|
|
iconLayout->addWidget(nameLabel, 0, Qt::AlignCenter);
|
|
|
|
m_icons.append(iconWidget);
|
|
layout->addWidget(iconWidget);
|
|
}
|
|
|
|
layout->addStretch();
|
|
}
|
|
|
|
QString DockBar::getIconSymbol(const QString &id) const
|
|
{
|
|
static QMap<QString, QString> iconMap = {
|
|
{"dock_sop", "📋"},
|
|
{"dock_rcp63", "🌐"},
|
|
{"dock_network", "🌐"},
|
|
{"dock_settings", "⚙️"}};
|
|
return iconMap.value(id, "📱");
|
|
}
|
|
|
|
void DockBar::paintEvent(QPaintEvent *event)
|
|
{
|
|
Q_UNUSED(event)
|
|
|
|
QPainter painter(this);
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|
|
|
// Android风格Dock背景 - 半透明毛玻璃,增加高度以容纳图标
|
|
QRect bgRect = rect().adjusted(110, 5, -110, -5);
|
|
|
|
// 半透明白色背景
|
|
painter.setPen(Qt::NoPen);
|
|
painter.setBrush(QColor(255, 255, 255, 200));
|
|
painter.drawRoundedRect(bgRect, 30, 30);
|
|
|
|
// 细边框
|
|
painter.setPen(QPen(QColor(255, 255, 255, 100), 1));
|
|
painter.setBrush(Qt::NoBrush);
|
|
painter.drawRoundedRect(bgRect, 30, 30);
|
|
}
|