kylin-nm/plugins/proxy/applistwidget.cpp

148 lines
4.1 KiB
C++
Raw Normal View History

2022-12-08 14:00:51 +08:00
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* 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 3 of the License, or
2022-12-08 14:00:51 +08:00
* (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 "applistwidget.h"
#include <QDebug>
AppListWidget::AppListWidget(QString path, QWidget *parent)
: m_path(path), QWidget(parent)
{
initUI();
initDbus();
}
AppListWidget::~AppListWidget()
{
delete m_dbusInterface;
2022-12-08 14:00:51 +08:00
}
/**
* @brief AppListWidget::setAppChecked
* @param flag 使
*/
void AppListWidget::setAppChecked(bool flag)
{
m_checkBox->setChecked(flag);
}
/**
* @brief AppListWidget::setAppIcon
* @param icon
2022-12-08 14:00:51 +08:00
*/
void AppListWidget::setAppIcon(const QIcon &icon)
2022-12-08 14:00:51 +08:00
{
m_iconBtn->setIcon(icon);
2022-12-08 14:00:51 +08:00
}
/**
* @brief AppListWidget::setAppName
* @param text
*/
void AppListWidget::setAppName(const QString &text)
{
m_nameLabel->setText(text);
}
/**
* @brief AppListWidget::onAppCheckStateChanged
* checkBox是否选中
*/
void AppListWidget::onAppCheckStateChanged()
{
if (m_checkBox->isChecked()) {
AddAppProxyConfig();
} else {
RemoveAppProxyConfig();
}
}
/**
* @brief AppListWidget::AddAppProxyConfig
*
*/
void AppListWidget::AddAppProxyConfig()
{
if(m_dbusInterface == nullptr || !m_dbusInterface->isValid()) {
2022-12-08 14:00:51 +08:00
qWarning ()<< "init AppProxy dbus error";
return;
2022-12-08 14:00:51 +08:00
}
qDebug() << "call QDBusInterface addAppIntoProxy";
m_dbusInterface->call("addAppIntoProxy", m_path);
}
/**
* @brief AppListWidget::RemoveAppProxyConfig
*
*/
void AppListWidget::RemoveAppProxyConfig()
{
if(m_dbusInterface == nullptr || !m_dbusInterface->isValid()) {
2022-12-08 14:00:51 +08:00
qWarning ()<< "init AppProxy dbus error";
return;
2022-12-08 14:00:51 +08:00
}
qDebug() << "call QDBusInterface delAppIntoProxy";
m_dbusInterface->call("delAppIntoProxy", m_path);
}
void AppListWidget::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
m_checkBox->setChecked(!m_checkBox->isChecked());
onAppCheckStateChanged();
}
return QWidget::mousePressEvent(event);
}
void AppListWidget::initUI()
{
QHBoxLayout *mainLayout = new QHBoxLayout(this);
mainLayout->setContentsMargins(17, 0, 17, 0);
mainLayout->setSpacing(8);
m_checkBox = new QCheckBox(this);
m_checkBox->setAttribute(Qt::WA_TransparentForMouseEvents, true); //m_checkBox不响应鼠标事件,将其传递给父窗口
m_iconBtn = new QToolButton(this);
m_iconBtn->setToolButtonStyle(Qt::ToolButtonStyle::ToolButtonIconOnly);
m_iconBtn->setAttribute(Qt::WA_TranslucentBackground, true); //透明
m_iconBtn->setAttribute(Qt::WA_TransparentForMouseEvents, true);
m_iconBtn->setAutoRaise(true);
m_iconBtn->setFixedSize(24, 24);
m_iconBtn->setIconSize(QSize(24, 24));
2022-12-08 14:00:51 +08:00
m_nameLabel = new QLabel(this);
mainLayout->addWidget(m_checkBox);
mainLayout->addWidget(m_iconBtn);
2022-12-08 14:00:51 +08:00
mainLayout->addWidget(m_nameLabel);
mainLayout->addStretch();
}
void AppListWidget::initDbus()
{
m_dbusInterface = new QDBusInterface("org.ukui.SettingsDaemon",
"/org/ukui/SettingsDaemon/AppProxy",
"org.ukui.SettingsDaemon.AppProxy",
QDBusConnection::sessionBus());
if(!m_dbusInterface->isValid()) {
qWarning() << qPrintable(QDBusConnection::sessionBus().lastError().message());
}
2022-12-08 14:00:51 +08:00
}