diff --git a/src/touchscreen-settings/autotabletmodewidget.cpp b/src/touchscreen-settings/autotabletmodewidget.cpp new file mode 100644 index 0000000..d1fbf3a --- /dev/null +++ b/src/touchscreen-settings/autotabletmodewidget.cpp @@ -0,0 +1,136 @@ +/* -*- 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 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 "autotabletmodewidget.h" + +#include +#include +#include +#include +#include +#include +#include + +AutoTabletModeWidget::AutoTabletModeWidget(const QString& autoSwitchTabletText, QWidget* parent) + : QFrame(parent) +{ + setContentsMargins(16, 10, 16, 10); + setFrameShape(QFrame::Shape::Box); + + initLayout(); + initAutoTabletModeWidget(autoSwitchTabletText); +} + +void AutoTabletModeWidget::initLayout() +{ + QVBoxLayout* tabletModeVLayout = new QVBoxLayout; + tabletModeVLayout->setMargin(0); + setLayout(tabletModeVLayout); +} + +void AutoTabletModeWidget::initAutoTabletModeWidget(const QString& autoSwitchTabletText) +{ + QWidget* autoSwitchTabletWidget = createAutoTabletModeWidget(autoSwitchTabletText); + autoSwitchTabletWidget->setMinimumHeight(40); + layout()->addWidget(autoSwitchTabletWidget); +} + +QWidget* AutoTabletModeWidget::createAutoTabletModeWidget(const QString& autoSwitchTabletText) +{ + QWidget* autoTabletModeWidget = new QWidget; + QLabel* tabletModeText = new QLabel(autoSwitchTabletText); + initTabletModeButton(autoTabletModeWidget); + + QHBoxLayout* autoTabletModeLayout = new QHBoxLayout(autoTabletModeWidget); + autoTabletModeLayout->setContentsMargins(0, 0, 0, 0); + autoTabletModeLayout->addWidget(tabletModeText); + autoTabletModeLayout->addStretch(); + autoTabletModeLayout->addWidget(m_autoTabletModeButton); + autoTabletModeWidget->setLayout(autoTabletModeLayout); + + return autoTabletModeWidget; +} + +void AutoTabletModeWidget::initTabletModeButton(QWidget* widget) +{ + m_autoTabletModeButton = new kdk::KSwitchButton(widget); + m_autoTabletModeButton->setChecked(getAutoSwitchTablet()); + connect(m_autoTabletModeButton, &kdk::KSwitchButton::clicked, this, [this](bool state) { + setAutoSwitchTablet(state); + ukcc::UkccCommon::buriedSettings("AutoSwitchTabletModeButton", "settings", state ? "true":"false"); + }); + + QDBusConnection::sessionBus().connect("com.kylin.statusmanager.interface", + "/", + "com.kylin.statusmanager.interface", + "modemonitor_change_signal", + this, + SLOT(setAutoSwitchTabletBtn(bool))); +} + +bool AutoTabletModeWidget::getAutoSwitchTablet() const +{ + QDBusInterface statusManager("com.kylin.statusmanager.interface", + "/", + "com.kylin.statusmanager.interface"); + + if(!statusManager.isValid()) { + qWarning() << "com.kylin.statusmanager.interface is invalid"; + return false; + } + + QDBusReply reply = statusManager.call("get_modemonitor"); + if (!reply.isValid()) { + qWarning() << reply.error(); + return false; + } + + return reply.value(); +} + +void AutoTabletModeWidget::setAutoSwitchTablet(bool state) +{ + QDBusInterface statusManager("com.kylin.statusmanager.interface", + "/", + "com.kylin.statusmanager.interface"); + + if(!statusManager.isValid()) { + qWarning() << "com.kylin.statusmanager.interface is invalid"; + return; + } + + QDBusMessage res = statusManager.call("set_modemonitor", + QVariant(state), + QVariant("Touch Screen Plugin"), + QVariant("Auto Switch Tablet Mode Button")); + + if (res.type() == QDBusMessage::ErrorMessage) { + qWarning() << res.errorName() << res.errorMessage(); + } +} + +void AutoTabletModeWidget::setAutoSwitchTabletBtn(bool checked) +{ + if (!m_autoTabletModeButton) + return; + if (m_autoTabletModeButton->isChecked() == checked) + return; + m_autoTabletModeButton->setChecked(checked); +} diff --git a/src/touchscreen-settings/autotabletmodewidget.h b/src/touchscreen-settings/autotabletmodewidget.h new file mode 100644 index 0000000..d0d72b4 --- /dev/null +++ b/src/touchscreen-settings/autotabletmodewidget.h @@ -0,0 +1,49 @@ +/* -*- 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 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. + * + */ + +#ifndef AUTOTABLETMODEWIDGET_H +#define AUTOTABLETMODEWIDGET_H + +#include + +namespace kdk { +class KSwitchButton; +} +class AutoTabletModeWidget : public QFrame +{ + Q_OBJECT +public: + explicit AutoTabletModeWidget(const QString& autoSwitchTabletText, QWidget* parent = nullptr); + ~AutoTabletModeWidget() = default; + +private: + void initLayout(); + void initAutoTabletModeWidget(const QString& autoSwitchTabletText); + QWidget* createAutoTabletModeWidget(const QString& autoSwitchTabletText); + void initTabletModeButton(QWidget* widget); + + bool getAutoSwitchTablet() const; + void setAutoSwitchTablet(bool state); + void setAutoSwitchTabletBtn(bool checked); + + kdk::KSwitchButton* m_autoTabletModeButton; +}; + +#endif // AUTOTABLETMODEWIDGET_H diff --git a/src/touchscreen-settings/tabletmodewidget.cpp b/src/touchscreen-settings/tabletmodewidget.cpp index 5621da7..5a98bc1 100644 --- a/src/touchscreen-settings/tabletmodewidget.cpp +++ b/src/touchscreen-settings/tabletmodewidget.cpp @@ -81,9 +81,9 @@ QWidget* TabletModeWidget::createTabletModeWidget() m_tabletModeButton = new kdk::KSwitchButton(tabletModeWidget); m_tabletModeButton->setChecked(getTabletModeEnabled()); m_tabletModeButton->setEnabled(!getCurrentTabletMode()); - connect(m_tabletModeButton, &kdk::KSwitchButton::clicked, this, [this](bool checked) { - setTabletModeEnabled(checked); - ukcc::UkccCommon::buriedSettings("TouchScreen", "setTabletModeEnabled", "settings", checked ? "true":"false"); + connect(m_tabletModeButton, &kdk::KSwitchButton::clicked, this, [this](bool enabled) { + setTabletModeEnabled(enabled); + ukcc::UkccCommon::buriedSettings("TouchScreen", "setTabletModeEnabled", "settings", enabled ? "true":"false"); }); QHBoxLayout* tabletModeLayout = new QHBoxLayout(tabletModeWidget); diff --git a/src/touchscreen-settings/touchscreen-settings.pro b/src/touchscreen-settings/touchscreen-settings.pro index 2482af3..468b8ad 100644 --- a/src/touchscreen-settings/touchscreen-settings.pro +++ b/src/touchscreen-settings/touchscreen-settings.pro @@ -36,12 +36,14 @@ include(../settings-common/settings-common.pri) #include(video-widget/video-widget.pri) SOURCES += \ + autotabletmodewidget.cpp \ gestureguidance.cpp \ tabletmodewidget.cpp \ touchscreen-settings.cpp \ touchscreen.cpp HEADERS += \ + autotabletmodewidget.h \ gestureguidance.h \ tabletmodewidget.h \ touchscreen-settings.h \ diff --git a/src/touchscreen-settings/touchscreen.cpp b/src/touchscreen-settings/touchscreen.cpp index e06fbec..d8972ef 100644 --- a/src/touchscreen-settings/touchscreen.cpp +++ b/src/touchscreen-settings/touchscreen.cpp @@ -39,6 +39,7 @@ #include "touchscreen-settings.h" #include "settingscommon.h" #include "tabletmodewidget.h" +#include "autotabletmodewidget.h" #define KYLIN_USER_GUIDE_PATH "/" #define KYLIN_USER_GUIDE_SERVICE "com.kylinUserGuide.hotel" @@ -58,6 +59,7 @@ TouchScreen::TouchScreen(QList gestureInfos, { ui->setupUi(this); initUI(); + initConnection(); loadGif(); } @@ -111,13 +113,16 @@ void TouchScreen::initUI() TabletModeWidget* tabletModeWidget = new TabletModeWidget; ui->verticalLayout->insertWidget(2, tabletModeWidget); - initAutoTabletModeWidget(); + if (getTabletModeEnabled()) { + m_autoTabletModeWidget = new AutoTabletModeWidget(m_autoSwitchText); + ui->verticalLayout->insertWidget(3, m_autoTabletModeWidget); + } //! \note Do not show tablet mode if os without tablet mode if (!SettingsCommon::isTabletProductFeat()) { tabletModeWidget->deleteLater(); + m_autoTabletModeWidget->deleteLater(); ui->switchTablet_label->hide(); - ui->switchTablet_frame->hide(); ui->horizontalLayout_4->deleteLater(); } @@ -145,29 +150,39 @@ void TouchScreen::initUI() }); } -void TouchScreen::initAutoTabletModeWidget() +void TouchScreen::initConnection() { - QHBoxLayout* switchTabletLayout = new QHBoxLayout(); - ui->switchTablet_frame->setFrameShape(QFrame::Shape::Box); - ui->switchTablet_frame->setLayout(switchTabletLayout); - QLabel* autoSwtich = new QLabel(m_autoSwitchText); - switchTabletLayout->addWidget(autoSwtich); - switchTabletLayout->addStretch(); - switchTabletLayout->setContentsMargins(16, 0, 16, 0); - m_autoSwitchTablet = new kdk::KSwitchButton(); - m_autoSwitchTablet->setChecked(this->autoSwitchTablet()); - switchTabletLayout->addWidget(m_autoSwitchTablet); - connect(m_autoSwitchTablet, &kdk::KSwitchButton::clicked, this, [this](bool checked) { - setAutoSwitchTablet(checked); - buriedSettings("AutoSwitchTabletModeButton", "settings", checked ? "true":"false"); - }); - QDBusConnection::sessionBus().connect("com.kylin.statusmanager.interface", - "/", - "com.kylin.statusmanager.interface", - "modemonitor_change_signal", - this, - SLOT(setAutoSwitchTabletBtn(bool))); + "/", + "com.kylin.statusmanager.interface", + "isTabletModeEnabledChanged", + this, + SLOT(onTabletModeEnabledChanged(bool))); +} + +void TouchScreen::onTabletModeEnabledChanged(bool enabled) +{ + if (enabled && nullptr == m_autoTabletModeWidget) { + m_autoTabletModeWidget = new AutoTabletModeWidget(m_autoSwitchText); + ui->verticalLayout->insertWidget(3, m_autoTabletModeWidget); + } else if (!enabled && m_autoTabletModeWidget != nullptr) { + delete m_autoTabletModeWidget; + m_autoTabletModeWidget = nullptr; + } +} + +bool TouchScreen::getTabletModeEnabled() const +{ + QDBusInterface statusManager("com.kylin.statusmanager.interface", + "/", + "com.kylin.statusmanager.interface"); + QDBusReply reply = statusManager.call("isTabletModeEnabled"); + if (!reply.isValid()) { + qWarning() << reply.error(); + return false; + } + + return reply.value(); } void TouchScreen::buriedSettings(QString settingsName, QString action, QString value) const @@ -205,54 +220,6 @@ void TouchScreen::stopGif() m_movie->jumpToFrame(0); } -bool TouchScreen::autoSwitchTablet() -{ - QDBusInterface statusManager("com.kylin.statusmanager.interface", - "/", - "com.kylin.statusmanager.interface"); - QDBusMessage res = statusManager.call("get_modemonitor"); - if (res.type() == QDBusMessage::ErrorMessage) { - qWarning() << res.errorName() << res.errorMessage(); - } - else if(res.type() == QDBusMessage::ReplyMessage) { - QDBusReply reply = res; - return reply.value(); - } - return false; -} - -void TouchScreen::setAutoSwitchTablet(bool state) -{ - QDBusInterface statusManager("com.kylin.statusmanager.interface", - "/", - "com.kylin.statusmanager.interface"); - QDBusMessage res = statusManager.call("set_modemonitor", - QVariant(state), - QVariant("Touch Screen Plugin"), - QVariant("Auto Switch Tablet Mode Button")); - - if (res.type() == QDBusMessage::ErrorMessage) { - qWarning() << res.errorName() << res.errorMessage(); - } - else if (res.type() == QDBusMessage::ReplyMessage) { - QDBusReply reply = res; - if (!reply) { - qWarning() << "Set auto switch tablet mode error." - << "Function set_modemonitor of dbus servies com.kylin.statusmanager.interface" - << "return" << reply; - } - } -} - -void TouchScreen::setAutoSwitchTabletBtn(bool checked) -{ - if (!m_autoSwitchTablet) - return; - if (m_autoSwitchTablet->isChecked() == checked) - return; - m_autoSwitchTablet->setChecked(checked); -} - // // 下面这种方式可以加载 mp4 文件,但是控制面板加载会报 QXcbConnection 的错误 // // 用 demo 加载插件不会出现此问题,暂时没有思路 //void TouchScreen::loadVideo() diff --git a/src/touchscreen-settings/touchscreen.h b/src/touchscreen-settings/touchscreen.h index 23ce0f5..cfc9e5a 100644 --- a/src/touchscreen-settings/touchscreen.h +++ b/src/touchscreen-settings/touchscreen.h @@ -65,6 +65,7 @@ class QMovie; class QGSettings; class GestureWidget; class TouchGestureInfo; +class AutoTabletModeWidget; class TouchScreen : public QWidget { Q_OBJECT @@ -82,27 +83,26 @@ signals: void iconThemeChangedSignal(); private slots: - void setAutoSwitchTabletBtn(bool checked); + void onTabletModeEnabledChanged(bool enabled); private: GestureWidget * createGestureWidget(const TouchGestureInfo& gestureInfo); QList createGestureWidgets(); void initUI(); - void initAutoTabletModeWidget(); + void initConnection(); void loadGif(); void playGif(const QString& gifPath); - bool autoSwitchTablet(); - void setAutoSwitchTablet(bool state); + bool getTabletModeEnabled() const; void buriedSettings(QString settingsName, QString action, QString value) const; Ui::TouchScreen *ui; quint32 m_cycleNum; QMovie *m_movie; QGSettings *m_iconThemeGSettings; - kdk::KSwitchButton* m_autoSwitchTablet; QList m_gestureInfos; const QString m_modeTypeText; const QString m_autoSwitchText; + AutoTabletModeWidget* m_autoTabletModeWidget; }; diff --git a/src/touchscreen-settings/touchscreen.ui b/src/touchscreen-settings/touchscreen.ui index 298e2ad..e6d3fd0 100644 --- a/src/touchscreen-settings/touchscreen.ui +++ b/src/touchscreen-settings/touchscreen.ui @@ -16,7 +16,7 @@ - + 0 @@ -75,34 +75,6 @@ - - - - - 0 - 0 - - - - - 0 - 60 - - - - - 0 - 0 - - - - QFrame::StyledPanel - - - QFrame::Raised - - -