Files

358 lines
13 KiB
C++
Raw Permalink Normal View History

/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2020-02-02 20:13:31 +08:00
*
* Copyright (C) 2019 Tianjin KYLIN Information Technology Co., Ltd.
*
* 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 2 of the License, or
* (at your option) 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
2020-01-17 10:20:22 +08:00
#include "homepagewidget.h"
#include "ui_homepagewidget.h"
#include <QLabel>
#include <QSignalMapper>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QListWidget>
2020-11-09 03:02:33 -06:00
#include <QDBusInterface>
2020-01-17 10:20:22 +08:00
#include <QDebug>
2021-05-20 15:33:35 +08:00
#include <QSvgRenderer>
2020-01-17 10:20:22 +08:00
#include "mainwindow.h"
2021-09-26 16:07:02 +08:00
#include "mhomedelegate.h"
2020-01-17 10:20:22 +08:00
#include "utils/keyvalueconverter.h"
#include "component/clicklabel.h"
#include "utils/functionselect.h"
#include "component/hoverwidget.h"
2020-11-09 03:02:33 -06:00
#include "./utils/utils.h"
2020-01-17 10:20:22 +08:00
HomePageWidget::HomePageWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::HomePageWidget)
{
ui->setupUi(this);
// 获取主窗口
2020-01-17 10:20:22 +08:00
this->setParent(parent);
pmainWindow = (MainWindow *)parentWidget();
// 初始化首页
2020-01-17 10:20:22 +08:00
initUI();
2021-06-15 16:19:57 +08:00
setMagins();
2020-01-17 10:20:22 +08:00
}
HomePageWidget::~HomePageWidget()
{
delete ui;
ui = nullptr;
2020-01-17 10:20:22 +08:00
}
2021-06-15 16:19:57 +08:00
void HomePageWidget::initUI() {
2020-01-17 10:20:22 +08:00
ui->listWidget->setResizeMode(QListView::Adjust);
ui->listWidget->setViewMode(QListView::IconMode);
ui->listWidget->setMovement(QListView::Static);
ui->listWidget->setSpacing(0);
ui->listWidget->setFocusPolicy(Qt::NoFocus);
ui->listWidget->setSelectionMode(QAbstractItemView::NoSelection);
2020-11-09 03:02:33 -06:00
mModuleMap = Utils::getModuleHideStatus();
2020-01-17 10:20:22 +08:00
//构建枚举键值转换对象
KeyValueConverter * kvConverter = new KeyValueConverter(); //继承QObject,No Delete
//初始化功能列表数据
FunctionSelect::initValue();
QSignalMapper * moduleSignalMapper = new QSignalMapper(this);
2021-06-15 16:19:57 +08:00
2020-11-09 03:02:33 -06:00
for (int moduleIndex = 0; moduleIndex < TOTALMODULES; moduleIndex++) {
2020-01-17 10:20:22 +08:00
//获取插件QMap
QMap<QString, QObject *> moduleMap;
moduleMap = pmainWindow->exportModule(moduleIndex);
//获取当前模块名
QString modulenameString = kvConverter->keycodeTokeystring(moduleIndex).toLower();
QString modulenamei18nString = kvConverter->keycodeTokeyi18nstring(moduleIndex);
2020-11-09 03:02:33 -06:00
if (mModuleMap.keys().contains(modulenameString)) {
if (!mModuleMap[modulenameString].toBool()) {
continue;
}
}
//构建首页8个模块
2020-01-17 10:20:22 +08:00
//基础Widget
QWidget * baseWidget = new QWidget;
baseWidget->setAttribute(Qt::WA_DeleteOnClose);
baseWidget->setObjectName("itemBaseWidget");
2020-04-17 14:00:44 +08:00
baseWidget->setStyleSheet("QWidget#itemBaseWidget{background: palette(base);}");
2020-01-17 10:20:22 +08:00
//baseWidget 的顶级布局
QVBoxLayout * baseVerLayout = new QVBoxLayout(baseWidget);
baseVerLayout->setMargin(0);
baseVerLayout->setSpacing(0);
//包含显示内容的布局
QBoxLayout * majorHorLayout = new QHBoxLayout();
majorHorLayout->setMargin(0);
majorHorLayout->setSpacing(0);
//内容Widget的构建
2020-04-21 15:16:35 +08:00
ResHoverWidget * widget = new ResHoverWidget(modulenameString);
widget->setMinimumWidth(320);
widget->setMinimumHeight(80);
2020-01-17 10:20:22 +08:00
widget->setAttribute(Qt::WA_DeleteOnClose);
widget->setObjectName("itemWidget");
2021-03-18 16:29:17 +08:00
widget->setStyleSheet("ResHoverWidget:hover:!pressed#itemWidget{background: palette(Highlight); border-radius: 4px;}");
2021-01-21 11:09:06 +08:00
connect(widget, &ResHoverWidget::widgetClicked, [=](QString moduleName) {
int moduleIndex = kvConverter->keystringTokeycode(moduleName);
//获取模块的第一项跳转
QString firstFunc;
QList<FuncInfo> tmpList = FunctionSelect::funcinfoList[moduleIndex];
2021-01-21 11:09:06 +08:00
for (FuncInfo tmpStruct : tmpList) {
2021-01-21 18:49:17 +08:00
if (moduleMap.keys().contains(tmpStruct.namei18nString)) {
if (mModuleMap.isEmpty() || mModuleMap[tmpStruct.nameString.toLower()].toBool()) {
firstFunc = tmpStruct.namei18nString;
//跳转
pmainWindow->functionBtnClicked(moduleMap.value(firstFunc));
break;
}
}
}
});
2020-01-17 10:20:22 +08:00
QHBoxLayout * mainHorLayout = new QHBoxLayout(widget);
mainHorLayout->setMargin(16);
mainHorLayout->setSpacing(16);
QLabel * logoLabel = new QLabel(widget);
logoLabel->setFixedSize(48, 48);
2020-01-17 10:20:22 +08:00
logoLabel->setObjectName("logoLabel");
logoLabel->setScaledContents(true);
QString path = (QString(":/img/homepage/%1.svg").arg(modulenameString));
2021-03-18 16:29:17 +08:00
QPixmap pix = loadSvg(path, HIGHLIGHT);
logoLabel->setPixmap(pix);
2020-01-17 10:20:22 +08:00
QVBoxLayout * rightVerLayout = new QVBoxLayout();
rightVerLayout->setContentsMargins(0, 2, 0, 0);
rightVerLayout->setSpacing(8);
QLabel * titleLabel = new QLabel(widget);
titleLabel->setObjectName("mptitleLabel");
titleLabel->setText(modulenamei18nString);
2020-04-17 14:00:44 +08:00
2020-01-17 10:20:22 +08:00
QHBoxLayout * funcHorLayout = new QHBoxLayout();
//循环填充模块下属功能
QList<FuncInfo> tmpList = FunctionSelect::funcinfoList[moduleIndex];
for (int funcIndex = 0; funcIndex < tmpList.size(); funcIndex++){
FuncInfo single = tmpList.at(funcIndex);
//跳过插件不存在的功能项
if (!moduleMap.contains(single.namei18nString)){
continue;
}
2020-02-27 20:58:54 +08:00
//跳过不在首页显示的功能
2020-04-10 18:08:35 +08:00
if (!single.mainShow)
continue;
2020-02-27 20:58:54 +08:00
2020-11-09 03:02:33 -06:00
if (mModuleMap.keys().contains(single.nameString.toLower())) {
if (!mModuleMap[single.nameString.toLower()].toBool()) {
continue;
}
}
2020-01-17 10:20:22 +08:00
ClickLabel * label = new ClickLabel(single.namei18nString, widget);
2020-04-17 14:50:06 +08:00
label->setStyleSheet("color: palette(Shadow);");
2020-01-17 10:20:22 +08:00
connect(label, SIGNAL(clicked()), moduleSignalMapper, SLOT(map()));
moduleSignalMapper->setMapping(label, moduleMap[single.namei18nString]);
funcHorLayout->addWidget(label);
}
rightVerLayout->addStretch();
rightVerLayout->addWidget(titleLabel);
rightVerLayout->addLayout(funcHorLayout);
rightVerLayout->addStretch();
mainHorLayout->addWidget(logoLabel);
mainHorLayout->addLayout(rightVerLayout);
mainHorLayout->addStretch();
widget->setLayout(mainHorLayout);
majorHorLayout->addWidget(widget);
majorHorLayout->addStretch();
baseVerLayout->addLayout(majorHorLayout);
baseVerLayout->addStretch();
baseWidget->setLayout(baseVerLayout);
//悬浮改变Widget状态
2020-04-21 15:16:35 +08:00
connect(widget, &ResHoverWidget::enterWidget, this, [=](QString mname){
2020-11-09 03:02:33 -06:00
Q_UNUSED(mname)
2020-04-21 15:16:35 +08:00
ResHoverWidget * w = dynamic_cast<ResHoverWidget *>(QObject::sender());
QPixmap cgPix = loadSvg(path, WHITE);
logoLabel->setPixmap(cgPix);
2020-01-17 10:20:22 +08:00
titleLabel->setStyleSheet("color: palette(Light);");
2020-04-17 14:00:44 +08:00
2020-01-17 10:20:22 +08:00
QList<ClickLabel *> clabelList = w->findChildren<ClickLabel *>();
2020-11-09 03:02:33 -06:00
for (ClickLabel * tmpLabel : clabelList) {
tmpLabel->setStyleSheet("color: palette(Light);");
2020-01-17 10:20:22 +08:00
}
});
//还原状态
2020-11-09 03:02:33 -06:00
connect(widget, &ResHoverWidget::leaveWidget, this, [=](QString mname) {
Q_UNUSED(mname)
2020-04-21 15:16:35 +08:00
ResHoverWidget * w = dynamic_cast<ResHoverWidget *>(QObject::sender());
2021-03-18 16:29:17 +08:00
QPixmap cgPix = loadSvg(path, HIGHLIGHT);
logoLabel->setPixmap(cgPix);
2020-01-17 10:20:22 +08:00
2020-05-20 20:16:56 +08:00
titleLabel->setStyleSheet("color: palette(windowText);");
2020-01-17 10:20:22 +08:00
QList<ClickLabel *> clabelList = w->findChildren<ClickLabel *>();
for (ClickLabel * tmpLabel : clabelList){
2020-05-20 20:16:56 +08:00
tmpLabel->setStyleSheet("color: palette(Shadow);");
2020-01-17 10:20:22 +08:00
}
});
2021-09-26 16:07:02 +08:00
mHomeDelegate *delegate = new mHomeDelegate;
ui->listWidget->setItemDelegate(delegate);
2020-01-17 10:20:22 +08:00
QListWidgetItem * item = new QListWidgetItem(ui->listWidget);
item->setSizeHint(QSize(360, 100));
ui->listWidget->addItem(item);
ui->listWidget->setItemWidget(item, baseWidget);
}
connect(moduleSignalMapper, SIGNAL(mapped(QObject*)), pmainWindow, SLOT(functionBtnClicked(QObject*)));
// connect(ui->listWidget, SIGNAL(itemPressed(QListWidgetItem *)), this, SLOT(slotItemPressed(QListWidgetItem *)));
}
const QPixmap HomePageWidget::loadSvg(const QString &fileName, COLOR color)
{
int size = 48;
const auto ratio = qApp->devicePixelRatio();
if ( 2 == ratio) {
size = 96;
2020-06-18 10:53:49 +08:00
} else if (3 == ratio) {
size = 144;
}
QPixmap pixmap(size, size);
QSvgRenderer renderer(fileName);
pixmap.fill(Qt::transparent);
QPainter painter;
painter.begin(&pixmap);
renderer.render(&painter);
painter.end();
pixmap.setDevicePixelRatio(ratio);
return drawSymbolicColoredPixmap(pixmap, color);
}
QPixmap HomePageWidget::drawSymbolicColoredPixmap(const QPixmap &source, COLOR cgColor)
{
QImage img = source.toImage();
for (int x = 0; x < img.width(); x++) {
for (int y = 0; y < img.height(); y++) {
auto color = img.pixelColor(x, y);
if (color.alpha() > 0) {
if ( WHITE == cgColor) {
color.setRed(255);
color.setGreen(255);
color.setBlue(255);
img.setPixelColor(x, y, color);
} else if( BLACK == cgColor) {
color.setRed(0);
color.setGreen(0);
color.setBlue(0);
img.setPixelColor(x, y, color);
} else if (GRAY == cgColor) {
color.setRed(152);
color.setGreen(163);
color.setBlue(164);
img.setPixelColor(x, y, color);
2021-03-18 16:29:17 +08:00
} else if(HIGHLIGHT == cgColor){
QColor highLightColor = palette().color(QPalette::Highlight);
color.setRed(highLightColor.red());
color.setGreen(highLightColor.green());
color.setBlue(highLightColor.blue());
img.setPixelColor(x, y, color);
}
else {
return source;
}
}
}
}
return QPixmap::fromImage(img);
}
2020-01-17 10:20:22 +08:00
2021-06-15 16:19:57 +08:00
void HomePageWidget::setMagins()
{
int screenWidth= Utils::sizeOnCursor().width();
int marginLR = (screenWidth > 1440) ? 260 : 169;
ui->homepageLayout->setContentsMargins(marginLR, 0, 0, 0);
}
/*
* 问题:点击“语言和地区”最终跳转至“时间和日期”页面;点击“默认应用”最总跳转至“显示器”页面
* 解决:屏蔽ClickLabel的点击传递
* 备注:首页需要实现伸缩模块居中效果,改为使用留布局来实现,所以如下方法将来无法使用
*/
void HomePageWidget::slotItemPressed(QListWidgetItem *item)
{
if(item == nullptr)
return;
int moduleIndex = ui->listWidget->currentRow();
QMap<QString, QObject *> moduleMap;
moduleMap = pmainWindow->exportModule(moduleIndex);
QWidget *currentWidget = QApplication::widgetAt(QCursor::pos());
QLabel *label = dynamic_cast<QLabel*>(currentWidget);
2020-04-21 15:16:35 +08:00
ResHoverWidget* hoverWidget = dynamic_cast<ResHoverWidget*>(currentWidget);
if(label != nullptr && hoverWidget != nullptr)
return;
if(label != nullptr || hoverWidget != nullptr)
{
QString targetString = "";
QList<FuncInfo> tmpList = FunctionSelect::funcinfoList[moduleIndex];
for (int funcIndex = 0; funcIndex < tmpList.size(); funcIndex++) {
FuncInfo single = tmpList.at(funcIndex);
//跳过插件不存在的功能项
if (!moduleMap.contains(single.namei18nString)) {
continue;
}
// if targetString is already been found, there is no need to continue this cycle
if(!targetString.isEmpty())
break;
targetString = single.namei18nString;
}
if(targetString .isEmpty()) {
pmainWindow->functionBtnClicked(moduleMap.first()); //QMap是无序的,不能保证跳转到第一项
} else {
pmainWindow->functionBtnClicked(moduleMap[targetString]);
}
}
2020-01-17 10:20:22 +08:00
}