/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * Copyright (C) 2022 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. * */ #include "singlepage.h" #include #include #include #include #include #include SinglePage::SinglePage(QWidget *parent) : QWidget(parent) { initUI(); initWindowProperties(); initTransparency(); } SinglePage::~SinglePage() { } void SinglePage::initUI() { m_mainLayout = new QVBoxLayout(this); m_mainLayout->setContentsMargins(MAIN_LAYOUT_MARGINS); m_mainLayout->setSpacing(MAIN_LAYOUT_SPACING); this->setLayout(m_mainLayout); m_titleFrame = new QFrame(this); m_titleFrame->setFixedHeight(TITLE_FRAME_HEIGHT); m_titleLayout = new QHBoxLayout(m_titleFrame); m_titleLayout->setContentsMargins(TITLE_LAYOUT_MARGINS); m_titleFrame->setLayout(m_titleLayout); m_titleLabel = new QLabel(m_titleFrame); m_titleLayout->addWidget(m_titleLabel); m_titleLayout->addStretch(); m_netDivider = new Divider(this); m_listFrame = new QFrame(this); m_listLayout = new QVBoxLayout(m_listFrame); m_listLayout->setContentsMargins(NET_LAYOUT_MARGINS); m_listFrame->setLayout(m_listLayout); m_listWidget = new QListWidget(m_listFrame); m_listLayout->addWidget(m_listWidget); m_setDivider = new Divider(this); m_settingsFrame = new QFrame(this); m_settingsFrame->setFixedHeight(TITLE_FRAME_HEIGHT); m_settingsLayout = new QHBoxLayout(m_settingsFrame); m_settingsLayout->setContentsMargins(SETTINGS_LAYOUT_MARGINS); m_settingsLabel = new KyLable(m_settingsFrame); m_settingsLabel->setCursor(Qt::PointingHandCursor); m_settingsLabel->setText(tr("Settings")); m_settingsLabel->setScaledContents(true); m_settingsLayout->addWidget(m_settingsLabel); m_settingsLayout->addStretch(); m_settingsFrame->setLayout(m_settingsLayout); m_mainLayout->addWidget(m_titleFrame); m_mainLayout->addWidget(m_netDivider); m_mainLayout->addWidget(m_listFrame); m_mainLayout->addWidget(m_setDivider); m_mainLayout->addWidget(m_settingsFrame); } void SinglePage::initWindowProperties() { QPalette pal = m_listFrame->palette(); pal.setBrush(QPalette::Base, QColor(0,0,0,0)); //背景透明 m_listFrame->setPalette(pal); this->setFixedWidth(MAX_WIDTH); this->setAttribute(Qt::WA_TranslucentBackground); this->setProperty("useStyleWindowManager", false); //禁用拖动 //绘制毛玻璃特效 QString platform = QGuiApplication::platformName(); if(!platform.startsWith(QLatin1String("wayland"),Qt::CaseInsensitive)) { QPainterPath path; auto rect = this->rect(); path.addRoundedRect(rect, 12, 12); path.addRect(rect); KWindowEffects::enableBlurBehind(this->winId(), true, QRegion(path.toFillPolygon().toPolygon())); //背景模糊 } } void SinglePage::showDesktopNotify(const QString &message, QString soundName) { QDBusInterface iface("org.freedesktop.Notifications", "/org/freedesktop/Notifications", "org.freedesktop.Notifications", QDBusConnection::sessionBus()); QStringList actions; //跳转动作 actions.append("kylin-vpn"); actions.append("default"); //默认动作:点击消息体时打开麒麟录音 QMap hints; if (!soundName.isEmpty()) { hints.insert("sound-name",soundName); //添加声音 } QList args; args<<(tr("Kylin VPN")) <<((unsigned int) 0) <palette().window().color(); QPainterPath rectPath; col.setAlphaF(m_transparency); rectPath.addRoundedRect(this->rect(),12,12); painter.setBrush(col); painter.drawPath(rectPath); KWindowEffects::enableBlurBehind(this->winId(), true, QRegion(rectPath.toFillPolygon().toPolygon())); //背景模糊 } void SinglePage::initTransparency() { if(QGSettings::isSchemaInstalled(QByteArray(TRANSPARENCY_GSETTINGS))) { m_transGsettings = new QGSettings(QByteArray(TRANSPARENCY_GSETTINGS)); if(m_transGsettings->keys().contains(TRANSPARENCY)) { m_transparency = m_transGsettings->get(TRANSPARENCY).toDouble() + 0.15; m_transparency = (m_transparency > 1) ? 1 : m_transparency; connect(m_transGsettings, &QGSettings::changed, this, &SinglePage::onTransChanged); } } } void SinglePage::onTransChanged() { m_transparency = m_transGsettings->get("transparency").toDouble() + 0.15; m_transparency = (m_transparency > 1) ? 1 : m_transparency; paintWithTrans(); } void SinglePage::paintWithTrans() { QPalette pal = this->palette(); QColor color = qApp->palette().base().color(); color.setAlphaF(m_transparency); pal.setColor(QPalette::Window, color); this->setPalette(pal); } void SinglePage::keyPressEvent(QKeyEvent *event) { if (event->key() == Qt::Key_Escape) { this->hide(); } return QWidget::keyPressEvent(event); }