From e51bcbd3336894fe5aed99e41f687c9878e62daf Mon Sep 17 00:00:00 2001 From: liuyuanpeng Date: Mon, 18 Jul 2022 09:38:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=B1=8F=E4=BF=9D=E6=8F=92?= =?UTF-8?q?=E4=BB=B6=EF=BC=8C=E4=BF=AE=E6=94=B9=E9=94=81=E5=B1=8F=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E5=B1=8F=E4=BF=9D=E6=96=B9=E5=BC=8F=E7=94=B1=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E5=91=BD=E4=BB=A4=E6=94=B9=E4=B8=BA=E9=9D=99=E6=80=81?= =?UTF-8?q?=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 +- .../LoadCustomPlugin/LoadCustomPlugin.pro | 23 ++++ examples/LoadCustomPlugin/main.cpp | 11 ++ examples/LoadCustomPlugin/widget.cpp | 46 +++++++ examples/LoadCustomPlugin/widget.h | 25 ++++ examples/LoadCustomPlugin/widget.ui | 43 ++++++ screensaver/CMakeLists.txt | 70 +++++++++- screensaver/customplugin.cpp | 22 ++++ screensaver/customplugin.h | 21 +++ screensaver/main.cpp | 18 +-- screensaver/screensaver.cpp | 43 +++--- screensaver/screensaver.h | 10 +- screensaver/screensaverplugin.h | 27 ++++ screensaver/weathermanager.h | 122 ------------------ src/CMakeLists.txt | 7 +- src/configuration.h | 2 +- src/fullbackgroundwidget.cpp | 14 +- src/{screensaver.cpp => screensavermode.cpp} | 2 +- src/{screensaver.h => screensavermode.h} | 6 +- src/screensaverwidget.cpp | 28 +++- src/screensaverwidget.h | 5 +- src/ukui-screensaver.pro | 4 +- src/weathermanager.cpp | 84 +++++++++--- src/weathermanager.h | 12 +- 24 files changed, 441 insertions(+), 206 deletions(-) create mode 100644 examples/LoadCustomPlugin/LoadCustomPlugin.pro create mode 100644 examples/LoadCustomPlugin/main.cpp create mode 100644 examples/LoadCustomPlugin/widget.cpp create mode 100644 examples/LoadCustomPlugin/widget.h create mode 100644 examples/LoadCustomPlugin/widget.ui create mode 100644 screensaver/customplugin.cpp create mode 100644 screensaver/customplugin.h create mode 100644 screensaver/screensaverplugin.h delete mode 100644 screensaver/weathermanager.h rename src/{screensaver.cpp => screensavermode.cpp} (99%) rename src/{screensaver.h => screensavermode.h} (95%) diff --git a/CMakeLists.txt b/CMakeLists.txt index e1950f7..bb61332 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -67,5 +67,5 @@ add_subdirectory(screensaver-focus-helper) add_subdirectory(Common) add_subdirectory(KylinNM) -add_dependencies(ukui-screensaver-dialog BiometricAuth VirtualKeyboard Common) +add_dependencies(ukui-screensaver-dialog BiometricAuth VirtualKeyboard Common Screensaver) add_dependencies(ukui-screensaver-default Common) diff --git a/examples/LoadCustomPlugin/LoadCustomPlugin.pro b/examples/LoadCustomPlugin/LoadCustomPlugin.pro new file mode 100644 index 0000000..ff6574b --- /dev/null +++ b/examples/LoadCustomPlugin/LoadCustomPlugin.pro @@ -0,0 +1,23 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2022-07-01T18:13:09 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = LoadCustomPlugin +TEMPLATE = app + +SOURCES += \ + main.cpp \ + widget.cpp + +HEADERS += \ + widget.h + +FORMS += \ + widget.ui + diff --git a/examples/LoadCustomPlugin/main.cpp b/examples/LoadCustomPlugin/main.cpp new file mode 100644 index 0000000..4d6c97b --- /dev/null +++ b/examples/LoadCustomPlugin/main.cpp @@ -0,0 +1,11 @@ +#include "widget.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + Widget w; + w.show(); + + return a.exec(); +} diff --git a/examples/LoadCustomPlugin/widget.cpp b/examples/LoadCustomPlugin/widget.cpp new file mode 100644 index 0000000..45a1213 --- /dev/null +++ b/examples/LoadCustomPlugin/widget.cpp @@ -0,0 +1,46 @@ +#pragma execution_character_set("utf-8") + +#include "widget.h" +#include "ui_widget.h" +#include +#include +#include +#include +#include + +Widget::Widget(QWidget *parent) : + QWidget(parent), + ui(new Ui::Widget) +{ + ui->setupUi(this); + connect(ui->load_plugin_btn,SIGNAL(clicked(bool)),this, SLOT(slot_load_plugin())); +} + +Widget::~Widget() +{ + delete ui; +} + +void Widget::slot_load_plugin() +{ + /* + QString file_path = QFileDialog::getOpenFileName(NULL,"加载插件",".","dll (*.dll *.so)"); + if(file_path.isEmpty()) + { + return; + } + */ + QPluginLoader pluginLoader("/usr/lib/ukui-screensaver/libscreensaver-default.so"); + pluginLoader.load(); + QObject* plugin = pluginLoader.instance(); + if (plugin) { + std::unique_ptr interface_ptr = std::unique_ptr(qobject_cast(plugin)); + QWidget* widget = interface_ptr->createWidget(false,this); + widget->setFixedHeight(180); + widget->setFixedWidth(300); + QHBoxLayout* mainLayout = new QHBoxLayout(this); + + mainLayout->addWidget(widget); + ui->widget->setLayout(mainLayout); + } +} diff --git a/examples/LoadCustomPlugin/widget.h b/examples/LoadCustomPlugin/widget.h new file mode 100644 index 0000000..43d6a87 --- /dev/null +++ b/examples/LoadCustomPlugin/widget.h @@ -0,0 +1,25 @@ +#ifndef WIDGET_H +#define WIDGET_H + +#include + +namespace Ui { +class Widget; +} + +class Widget : public QWidget +{ + Q_OBJECT + +public: + explicit Widget(QWidget *parent = 0); + ~Widget(); + +public slots: + void slot_load_plugin(); + +private: + Ui::Widget *ui; +}; + +#endif // WIDGET_H diff --git a/examples/LoadCustomPlugin/widget.ui b/examples/LoadCustomPlugin/widget.ui new file mode 100644 index 0000000..22adeca --- /dev/null +++ b/examples/LoadCustomPlugin/widget.ui @@ -0,0 +1,43 @@ + + + Widget + + + + 0 + 0 + 400 + 300 + + + + Widget + + + + + 140 + 30 + 111 + 31 + + + + LoadPlugin + + + + + + 30 + 90 + 331 + 141 + + + + + + + + diff --git a/screensaver/CMakeLists.txt b/screensaver/CMakeLists.txt index 7bce0d7..f6ac32a 100644 --- a/screensaver/CMakeLists.txt +++ b/screensaver/CMakeLists.txt @@ -29,7 +29,7 @@ qt5_wrap_cpp(screensaver_SRC cyclelabel.h scconfiguration.h sleeptime.h - weathermanager.h + ../src/weathermanager.h ../src/networkwatcher.h ) set(screensaver_SRC @@ -41,14 +41,76 @@ set(screensaver_SRC cyclelabel.cpp scconfiguration.cpp sleeptime.cpp - weathermanager.cpp + ../src/weathermanager.cpp ../src/networkwatcher.cpp ) add_executable(ukui-screensaver-default ${screensaver_SRC}) target_link_libraries(ukui-screensaver-default Qt5::Core Qt5::Widgets Qt5::X11Extras Qt5::Xml Qt5::Network ${EXTRA_LIBS}) +qt5_add_resources(screensaver_Plugin_SRC + default.qrc + ) + +qt5_wrap_cpp(screensaver_Plugin_SRC + chinesedate.h + screensaver.h + mbackground.h + cyclelabel.h + scconfiguration.h + sleeptime.h + ../src/weathermanager.h + ../src/networkwatcher.h + customplugin.h + screensaverplugin.h + ) +set(screensaver_Plugin_SRC + ${screensaver_Plugin_SRC} + chinesedate.cpp + mbackground.cpp + screensaver.cpp + cyclelabel.cpp + scconfiguration.cpp + sleeptime.cpp + ../src/weathermanager.cpp + ../src/networkwatcher.cpp + customplugin.cpp + ) + +add_library(screensaver-default SHARED ${screensaver_Plugin_SRC}) +target_link_libraries(screensaver-default Qt5::Core Qt5::Widgets Qt5::X11Extras Qt5::Xml Qt5::Network ${EXTRA_LIBS}) + +qt5_add_resources(Screensaver_SRC + default.qrc + ) + +qt5_wrap_cpp(Screensaver_SRC + chinesedate.h + screensaver.h + mbackground.h + cyclelabel.h + scconfiguration.h + sleeptime.h + ../src/weathermanager.h + ../src/networkwatcher.h + ) +set(Screensaver_SRC + ${screensaver_Plugin_SRC} + chinesedate.cpp + mbackground.cpp + screensaver.cpp + cyclelabel.cpp + scconfiguration.cpp + sleeptime.cpp + ../src/weathermanager.cpp + ../src/networkwatcher.cpp + ) + +add_library(Screensaver STATIC ${Screensaver_SRC}) +target_link_libraries(Screensaver Qt5::Core Qt5::Widgets Qt5::X11Extras Qt5::Xml Qt5::Network ${EXTRA_LIBS}) + install(TARGETS ukui-screensaver-default + screensaver-default DESTINATION lib/ukui-screensaver) install(FILES @@ -56,3 +118,7 @@ install(FILES language/screensaver-en_US.ini language/screensaver-jd.ini DESTINATION share/ukui-screensaver/language) + +install(FILES + screensaverplugin.h + DESTINATION include/ukui-screensaver/) diff --git a/screensaver/customplugin.cpp b/screensaver/customplugin.cpp new file mode 100644 index 0000000..afacc06 --- /dev/null +++ b/screensaver/customplugin.cpp @@ -0,0 +1,22 @@ +#include "customplugin.h" +#include "screensaver.h" + +CustomPlugin::CustomPlugin(QObject *parent):QObject(parent) +{ + +} + +QString CustomPlugin::name() const +{ + return "screensaver-default"; +} + +QWidget* CustomPlugin::createWidget(bool isScreensaver,QWidget* parent) +{ + return new Screensaver(isScreensaver,parent); +} + +QString CustomPlugin::displayName() const +{ + return "screensaver-default"; +} diff --git a/screensaver/customplugin.h b/screensaver/customplugin.h new file mode 100644 index 0000000..2d041f7 --- /dev/null +++ b/screensaver/customplugin.h @@ -0,0 +1,21 @@ +#ifndef CUSTOMPLUGIN_H +#define CUSTOMPLUGIN_H + +#include "screensaverplugin.h" +#include + +class CustomPlugin : public QObject, ScreensaverPlugin +{ + Q_OBJECT + //声明QT识别的唯一标识符 + Q_PLUGIN_METADATA(IID "org.ukui.screensaver.screensaver-default1.0.0") + //声明实现的插件接口 + Q_INTERFACES(ScreensaverPlugin) +public: + CustomPlugin(QObject* parent = 0); + QString name() const override; + QWidget* createWidget(bool isScreensaver,QWidget* parent) override; + QString displayName() const override; +}; + +#endif // CUSTOMPLUGIN_H diff --git a/screensaver/main.cpp b/screensaver/main.cpp index eb40b19..c7d725f 100644 --- a/screensaver/main.cpp +++ b/screensaver/main.cpp @@ -34,7 +34,6 @@ #include "config.h" -#define WORKING_DIRECTORY "/usr/share/ukui-screensaver" bool bControlFlg = false;//是否控制面板窗口 int main(int argc, char *argv[]) @@ -44,19 +43,11 @@ int main(int argc, char *argv[]) QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); #endif QApplication a(argc, argv); - prctl(PR_SET_PDEATHSIG, SIGHUP); - //加载翻译文件 - QString locale = QLocale::system().name(); - QTranslator translator; - QString qmFile = QString(WORKING_DIRECTORY"/i18n_qm/%1.qm").arg(locale); - translator.load(qmFile); - a.installTranslator(&translator); - qDebug() << "load translation file " << qmFile; - + prctl(PR_SET_PDEATHSIG, SIGHUP); QCommandLineParser parser; QString windowId; - Screensaver s; + Screensaver s(false); XWindowAttributes xwa; parser.setApplicationDescription("Test helper"); @@ -88,7 +79,7 @@ int main(int argc, char *argv[]) s.winId(); s.windowHandle()->setParent(window); XGetWindowAttributes (QX11Info::display(), wid, &xwa); - +/* #ifndef USE_INTEL XClassHint ch; ch.res_name = NULL; @@ -96,10 +87,9 @@ int main(int argc, char *argv[]) XGetClassHint (QX11Info::display(), wid, &ch); if(ch.res_name && strcmp(ch.res_name,"ukui-control-center")==0){ bControlFlg = true; - s.addClickedEvent(); } #endif - +*/ //获取屏保所在屏幕对应的缩放比例。 for(auto screen : QGuiApplication::screens()) { diff --git a/screensaver/screensaver.cpp b/screensaver/screensaver.cpp index e481abe..e128efb 100644 --- a/screensaver/screensaver.cpp +++ b/screensaver/screensaver.cpp @@ -57,6 +57,7 @@ #include "commonfunc.h" #include "config.h" +#include "../src/weathermanager.h" #define TIME_TYPE_SCHEMA "org.ukui.control-center.panel.plugins" #define THEME_TYPE_SCHENA "org.ukui.style" @@ -66,11 +67,13 @@ #define KEY_MESSAGE_SHOW_ENABLED "show-message-enabled" #define KEY_HOURSYSTEM "hoursystem" #define KEY_DATE_FORMAT "date" +#define WORKING_DIRECTORY "/usr/share/ukui-screensaver" QTime Screensaver::m_currentTime = QTime::currentTime(); extern bool bControlFlg; -Screensaver::Screensaver(QWidget *parent): +Screensaver::Screensaver(bool isscreensaver,QWidget *parent): + isScreensaver(isscreensaver), QWidget(parent), switchTimer(nullptr), backgroundPath(""), @@ -93,9 +96,16 @@ Screensaver::Screensaver(QWidget *parent): hasChanged(false), process(nullptr), screenLabel(nullptr), - respondClick(false), - m_weatherManager(new WeatherManager(this)) + respondClick(false) { + //加载翻译文件 + QString locale = QLocale::system().name(); + QTranslator translator; + QString qmFile = QString(WORKING_DIRECTORY"/i18n_qm/%1.qm").arg(locale); + translator.load(qmFile); + qApp->installTranslator(&translator); + qDebug() << "load translation file " << qmFile; + installEventFilter(this); // setWindowFlags(Qt::X11BypassWindowManagerHint); setUpdateCenterWidget(); @@ -367,7 +377,7 @@ bool Screensaver::eventFilter(QObject *obj, QEvent *event) #ifndef USE_INTEL if(obj == this){ if(event->type()==QEvent::MouseButtonPress){ - if(respondClick){ + if(!isScreensaver){ if(!process){ process = new QProcess(this); } @@ -495,7 +505,7 @@ void Screensaver::resizeEvent(QResizeEvent */*event*/) { float scale = 1.0; scale = (float)width()/1920; - if(width() < 600 || height()<400){//当显示在控制面板上时,字体缩小三倍。 + if((width() < 600 || height()<400) && !isScreensaver){//当显示在控制面板上时,字体缩小三倍。 if(flag == 0) { QList labelList = this->findChildren(); @@ -520,25 +530,6 @@ void Screensaver::resizeEvent(QResizeEvent */*event*/) flag = 1; #ifndef USE_INTEL if(myTextWidget){ - -// QColor highLightColor = palette().color(QPalette::Base); -// QString stringColor = QString("rgba(%1,%2,%3,82%)") -// .arg(highLightColor.red()) -// .arg(highLightColor.green()) -// .arg(highLightColor.blue()); -// QColor textColor = palette().color(QPalette::Text); -// QString textString = QString("rgb(%1,%2,%3)") -// .arg(textColor.red()) -// .arg(textColor.green()) -// .arg(textColor.blue()); -// QColor borderColor = palette().color(QPalette::BrightText); -// QString borderString = QString("rgba(%1,%2,%3,85%)") -// .arg(borderColor.red()) -// .arg(borderColor.green()) -// .arg(borderColor.blue()); - -// myTextLabel->setStyleSheet(QString("font-size:5px;border-radius: 2px;background: %1;color: %2;padding: 4px 8px 4px 8px;border-width: 1px;border-style: solid;border-color:%3;") \ -// .arg(stringColor).arg(textString).arg(borderString)); if(curStyle == "ukui-dark" || curStyle == "ukui-black"){ myTextLabel->setStyleSheet(QString("QLabel{font-size:5px;border-radius: 2px;padding: 4px 8px 4px 8px;border-width: 1px;\ background: rgba(0, 0, 0, %1); color:#FFFFFF; border-radius:16px}").arg(blur_Num * 0.01)); @@ -710,7 +701,7 @@ void Screensaver::updateBackgroundPath() void Screensaver::enterEvent(QEvent*){ // qDebug() << "enter ScreenSaver::enterEvent"; //当前是否是控制面板窗口 - if(bControlFlg){ + if(!isScreensaver){ setPreviewText(true); } } @@ -853,10 +844,12 @@ void Screensaver::initUI() if(qssFile.open(QIODevice::ReadOnly)) { setStyleSheet(qssFile.readAll()); + qDebug()< + +class ScreensaverPlugin +{ +public: + virtual ~ScreensaverPlugin() {} + //插件实例的名称 + virtual QString name() const = 0; + + //创建UI的实例 + virtual QWidget* createWidget(bool isScreensaver,QWidget* parent) = 0; + + //获得插件的展示名称 + virtual QString displayName() const = 0; +}; + +//定义了在QT系统中该接口的全局唯一的ID +//实现该SDK的插件也要定义相同的ID +//接口的ID中包含了版本信息,通过该ID我们可以区别不同版本的SDK和插件 +//Q_DECLARE_INTERFACE宏将类型和ID关联起来,这样QT就可以验证加载的插件是否可以转换成MyPluginInterface类型 +#define interface_iid "org.ukui.screensaver.screensaver-default1.0.0" +Q_DECLARE_INTERFACE(ScreensaverPlugin, interface_iid) + +#endif // FILTER_H diff --git a/screensaver/weathermanager.h b/screensaver/weathermanager.h deleted file mode 100644 index 3f29c40..0000000 --- a/screensaver/weathermanager.h +++ /dev/null @@ -1,122 +0,0 @@ -/* - * Copyright (C) 2020 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, 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, see . - * - * Authors: ZHAI Kang-ning -**/ -#ifndef WEATHERMANAGER_H -#define WEATHERMANAGER_H - -#include -#include -#include -#include - -#include "../src/networkwatcher.h" - -class QNetworkAccessManager; -class QNetworkReply; -class LocalWeatherInfo; - -class WeatherManager : public QObject -{ - Q_OBJECT -public: - explicit WeatherManager(QObject *parent = nullptr); - -Q_SIGNALS: - void onWeatherUpdate(QString city, QString cond, QString temperature); - -private Q_SLOTS: - void replyFinished(QNetworkReply *); - void onNetworkStateChanged(uint state); - -public: - void getWeather(); - QPixmap getWeatherIcon(); - QPixmap getWeatherIcon(QString cond); - - QString getCityName(); - QString getCond(); - QString getTemperature(); - -private: - bool updateLocation();//更新位置,从用户设置获取城市信息,如有多个,只取第一个,未对接 - void weatherRequest(); - - bool getLogcalWeather(); - QString getLogcalCityId(); - -private: - QString m_city_id; // "101030100" 默认天津 - QString m_city_name; - QString m_cond_txt; //天气条件 晴、阴等 - QString m_temperature;//温度 10、20等 - - QNetworkAccessManager *m_net_manager; - QTimer *m_timer; - QGSettings *m_settings; - - LocalWeatherInfo *m_local_weather_info; - NetWorkWatcher *m_networkWatcher; - - int m_networkTryNum = 0; -}; - -class LocalWeatherInfo : QObject -{ - //"1920-08-27 10:17:42,101310204,澄迈,小雨,95%,25℃,北风,1级," 时间,城市编码,城市名称,天气,湿度,温度,风向,风力 - Q_OBJECT -public: - explicit LocalWeatherInfo(QObject *parent = nullptr); - -private: - QString m_update_time; - QString m_city_id; - QString m_city_name; - QString m_cond_text; - QString m_air_humidity; - QString m_temperature; - QString m_wind_direction; - QString m_wind_force; -public: - bool isTimeValid(); - - void setTime(QString time); - QString getTime(); - - void setCityId(QString cityId); - QString getCityId(); - - void setCityName(QString cityName); - QString getCityName(); - - void setCondText(QString condText); - QString getCondText(); - - void setAirHumidity(QString airHumidity); - QString getAirHumidity(); - - void setTemperature(QString temperature); - QString getTemperature(); - - void setWindDirection(QString windDirection); - QString getWindDirection(); - - void setWindForce(QString windForce); - QString getWindForce(); -}; - -#endif // WEATHERMANAGER_H diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4b841ce..6308c7c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -13,6 +13,7 @@ include_directories(${PROJECT_BINARY_DIR}) include_directories(${PROJECT_SOURCE_DIR}/VirtualKeyboard/src) include_directories(${PROJECT_SOURCE_DIR}/BiometricAuth) include_directories(${PROJECT_SOURCE_DIR}/Common) +include_directories(${PROJECT_SOURCE_DIR}/screensaver) include_directories(${PROJECT_SOURCE_DIR}/KylinNM) include_directories(${PROJECT_SOURCE_DIR}/KylinNM/src) include_directories(${PROJECT_SOURCE_DIR}/KylinNM/hot-spot) @@ -53,6 +54,7 @@ qt5_wrap_ui(dialog_SRC qt5_add_resources(dialog_SRC assets.qrc ../KylinNM/nmqrc.qrc #暂时将麒麟网络的资源文件放到这里,否则显示不出来,暂时不知道原因 + ../screensaver/default.qrc ) # 头文件中包含了Xlib.h,需要单独拿出来处理,不知道原因 @@ -65,7 +67,7 @@ qt5_wrap_cpp(dialog_SRC screensaverwidget.h auth.h auth-pam.h - screensaver.h + screensavermode.h xeventmonitor.h monitorwatcher.h configuration.h @@ -113,7 +115,7 @@ set(dialog_SRC monitorwatcher.cpp grab-x11.cpp configuration.cpp - screensaver.cpp + screensavermode.cpp powermanager.cpp utils.cpp users.cpp @@ -158,6 +160,7 @@ target_link_libraries(ukui-screensaver-dialog Common Kylin-nm ukui-log4qt + Screensaver ) link_libraries(libmatemixer.so glib-2.0.so) diff --git a/src/configuration.h b/src/configuration.h index a138775..e0f63e6 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -25,7 +25,7 @@ //#include "gsettings.h" #include -#include "screensaver.h" +#include "screensavermode.h" class QGSettings; diff --git a/src/fullbackgroundwidget.cpp b/src/fullbackgroundwidget.cpp index fcb7354..6eedaed 100644 --- a/src/fullbackgroundwidget.cpp +++ b/src/fullbackgroundwidget.cpp @@ -53,7 +53,7 @@ #include "xeventmonitor.h" #include "monitorwatcher.h" #include "configuration.h" -#include "screensaver.h" +#include "screensavermode.h" #include "screensaverwidget.h" #include "grab-x11.h" #include "tabletlockwidget.h" @@ -721,11 +721,11 @@ void FullBackgroundWidget::showScreensaver() { ScreenSaver *saver = configuration->getScreensaver(); /*锁屏设置的Qt::WA_TranslucentBackground属性会导致第三方屏保变得透明,因此在使用第三方屏保时 - * 取消该属性,清除屏保时再设置回来*/ - if(saver->path != "/usr/lib/ukui-screensaver/ukui-screensaver-default") - { - setAttribute(Qt::WA_TranslucentBackground,false); - } + * 取消该属性,清除屏保时再设置回来*/ + if(saver->path != "/usr/lib/ukui-screensaver/ukui-screensaver-default") + { + setAttribute(Qt::WA_TranslucentBackground,false); + } ScreenSaverWidget *saverWidget = new ScreenSaverWidget(saver, this); qDebug() << " new ScreenSaverWidget"; @@ -745,7 +745,7 @@ void FullBackgroundWidget::showScreensaver() if(lockWidget) { lockWidget->stopAuth(); - lockWidget->hide(); + lockWidget->hide(); } } diff --git a/src/screensaver.cpp b/src/screensavermode.cpp similarity index 99% rename from src/screensaver.cpp rename to src/screensavermode.cpp index 744d33b..37eab3c 100644 --- a/src/screensaver.cpp +++ b/src/screensavermode.cpp @@ -15,7 +15,7 @@ * along with this program; if not, see . * **/ -#include "screensaver.h" +#include "screensavermode.h" #include #include #include diff --git a/src/screensaver.h b/src/screensavermode.h similarity index 95% rename from src/screensaver.h rename to src/screensavermode.h index 2476042..51c738e 100644 --- a/src/screensaver.h +++ b/src/screensavermode.h @@ -15,8 +15,8 @@ * along with this program; if not, see . * **/ -#ifndef SCREENSAVER_H -#define SCREENSAVER_H +#ifndef SCREENSAVER_MODE_H +#define SCREENSAVER_MODE_H #include #include @@ -74,4 +74,4 @@ Q_DECLARE_METATYPE(ScreenSaver) QDebug &operator<<(QDebug stream, const ScreenSaver &screensaver); -#endif // SCREENSAVER_H +#endif // SCREENSAVER_MODE_H diff --git a/src/screensaverwidget.cpp b/src/screensaverwidget.cpp index c232201..f204a33 100644 --- a/src/screensaverwidget.cpp +++ b/src/screensaverwidget.cpp @@ -24,6 +24,7 @@ #include #include #include +#include "screensaver.h" #include #include ScreenSaverWidget::ScreenSaverWidget(ScreenSaver *screensaver, QWidget *parent) @@ -33,6 +34,7 @@ ScreenSaverWidget::ScreenSaverWidget(ScreenSaver *screensaver, QWidget *parent) closing(false) { qDebug() << "ScreenSaverWidget::ScreenSaverWidget"; + setAttribute(Qt::WA_DeleteOnClose); setMouseTracking(true); setFocus(); this->installEventFilter(this); @@ -149,12 +151,30 @@ bool ScreenSaverWidget::eventFilter(QObject *obj, QEvent *event) /* Embed xscreensavers */ void ScreenSaverWidget::embedXScreensaver(const QString &path) { - QString cmd = path + " -window-id " + QString::number(winId()); - if(process.state() == QProcess::NotRunning) - process.start(cmd); + qDebug()<<"embedXScreensaver path = "<mode == SAVER_SINGLE){ + QString cmd = path + " -window-id " + QString::number(winId()); + if(process.state() == QProcess::NotRunning) + process.start(cmd); + }else if(screensaver->mode == SAVER_DEFAULT || screensaver->mode == SAVER_DEFAULT_CUSTOM){ + if(!m_screensaver){ + m_screensaver = new Screensaver(true,this); + //m_screensaver->resize(1366,768); + //m_screensaver->setGeometry(this->geometry()); + // m_screensaver->setGeometry(0,0,1920,1080); + + } + } } - +void ScreenSaverWidget::resizeEvent(QResizeEvent *event) +{ + if(m_screensaver){ + m_screensaver->setGeometry(this->geometry()); + } +} void ScreenSaverWidget::onBackgroundChanged(const QString &/*path*/) { diff --git a/src/screensaverwidget.h b/src/screensaverwidget.h index a76d428..7099cba 100644 --- a/src/screensaverwidget.h +++ b/src/screensaverwidget.h @@ -20,8 +20,9 @@ #include #include -#include "screensaver.h" +#include "screensavermode.h" +class Screensaver; class ScreenSaverWidget : public QWidget { Q_OBJECT @@ -34,6 +35,7 @@ protected: void closeEvent(QCloseEvent *); void paintEvent(QPaintEvent *event); bool eventFilter(QObject *obj, QEvent *event); + void resizeEvent(QResizeEvent *event); private: void embedXScreensaver(const QString &path); @@ -46,6 +48,7 @@ private: bool closing; float opacity; QProcess process; + Screensaver *m_screensaver = nullptr; }; #endif // SCREENSAVERWIDGET_H diff --git a/src/ukui-screensaver.pro b/src/ukui-screensaver.pro index f8b4b43..7e844bf 100644 --- a/src/ukui-screensaver.pro +++ b/src/ukui-screensaver.pro @@ -44,7 +44,7 @@ SOURCES += \ auxiliary.cpp \ configuration.cpp \ screensaverwidget.cpp \ - screensaver.cpp \ + screensavermode.cpp \ event_monitor.cpp \ monitorwatcher.cpp @@ -58,7 +58,7 @@ HEADERS += \ auxiliary.h \ configuration.h \ screensaverwidget.h \ - screensaver.h \ + screensavermode.h \ event_monitor.h \ monitorwatcher.h diff --git a/src/weathermanager.cpp b/src/weathermanager.cpp index e1738f2..506b34c 100644 --- a/src/weathermanager.cpp +++ b/src/weathermanager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 Tianjin KYLIN Information Technology Co., Ltd. + * Copyright (C) 2020 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 @@ -40,17 +40,18 @@ const int weatherReqInterval = 1000 * 60 * 20; //定时更新天气,和麒麟天 const QByteArray schemaWeather = "org.china-weather-data.settings"; static const QMap weatherMap { - {"NA", "55"}, - {"晴", "0"}, - {"多云", "1"}, - {"阴", "2"}, - {"阵雨", "3"}, - {"雷阵雨", "4"}, - {"小雨", "7"}, - {"中雨", "8"}, - {"大雨", "9"}, - {"中到大雨", "9"}, - {"雪", "13"}, + {"晴", "100"},{"多云", "101"},{"少云", "102"},{"晴间多云", "103"},{"阴", "104"}, + {"有风", "200"},{"平静", "201"},{"微风", "202"},{"和风", "203"},{"清风", "204"},{"强风劲风", "205"},{"疾风", "206"},{"大风", "207"},{"烈风", "208"},{"风暴", "209"}, + {"狂暴风", "210"},{"飓风", "211"},{"龙卷风", "212"},{"热带风暴", "213"}, + {"阵雨", "300"},{"强阵雨", "301"},{"雷阵雨", "302"},{"强雷阵雨", "303"},{"雷阵雨伴有冰雹", "304"},{"小雨", "305"},{"中雨", "306"},{"大雨", "307"},{"极端降雨", "308"},{"毛毛雨细雨", "309"}, + {"暴雨", "310"},{"大暴雨", "311"},{"特大暴雨", "312"},{"冻雨", "313"},{"小到中雨", "314"},{"中到大雨", "315"},{"大到暴雨", "316"},{"暴雨到大暴雨", "317"},{"大暴雨到特大暴雨", "318"}, + {"雨", "399"}, + {"小雪", "400"},{"中雪", "401"},{"大雪", "402"},{"暴雪", "403"},{"雨夹雪", "404"},{"雨雪天气", "405"},{"阵雨夹雪", "406"},{"阵雪", "407"},{"小到中雪", "408"},{"中到大雪", "409"}, + {"大到暴雪", "410"},{"雪", "499"}, + {"薄雾", "500"},{"雾", "501"},{"霾", "502"},{"扬沙", "503"},{"浮尘", "504"},{"沙尘暴", "507"},{"强沙尘暴", "508"},{"大雾", "509"}, + {"强浓雾", "510"},{"中度霾", "511"},{"重度霾", "512"},{"严重霾", "513"},{"大雾", "514"},{"特强浓雾", "515"}, + {"热", "900"},{"冷", "901"}, + {"未知", "999"} }; WeatherManager::WeatherManager(QObject *parent) : QObject(parent) @@ -66,6 +67,20 @@ WeatherManager::WeatherManager(QObject *parent) : QObject(parent) m_local_weather_info = new LocalWeatherInfo(this); connect(m_timer, &QTimer::timeout, this, &WeatherManager::weatherRequest); + + m_networkWatcher = new NetWorkWatcher(this); + connect(m_networkWatcher, &NetWorkWatcher::NetworkStateChanged, this, &WeatherManager::onNetworkStateChanged); + + m_networkWatcher->checkOnline(); +} + +void WeatherManager::onNetworkStateChanged(uint state) +{ + qDebug() << state; + if (NM_STATE_CONNECTED_GLOBAL != state) + emit onWeatherUpdate("天气不可用", NULL, NULL); + else + getWeather(); } void WeatherManager::getWeather() @@ -96,6 +111,9 @@ bool WeatherManager::updateLocation() emit onWeatherUpdate(m_local_weather_info->getCityName(), m_local_weather_info->getCondText(), m_local_weather_info->getTemperature()); + + + m_networkWatcher->checkOnline(); return true; } m_city_id = getLogcalCityId(); @@ -153,6 +171,23 @@ QString WeatherManager::getLogcalCityId() void WeatherManager::replyFinished(QNetworkReply *reply) { + if(reply != nullptr && reply->error() != QNetworkReply::NoError) + { + qWarning() << "[WeatherManager][replyFinished] get weather error:(" + << reply->error() << ")" << reply->errorString(); + if (m_networkTryNum < 15) + { + m_networkTryNum++; + QTimer::singleShot(1000, this, [=]{ + weatherRequest(); + }); + } else { + m_networkTryNum = 0; + } + emit onWeatherUpdate("天气不可用", "", ""); + return; + } + //注:天气信息只解析了锁屏需要展示的部分 QByteArray BA; QJsonDocument JD; @@ -180,13 +215,14 @@ void WeatherManager::replyFinished(QNetworkReply *reply) } if (now.contains("tmp")){ - m_temperature = now.mid(4); + m_temperature = now.mid(4) + "°C"; } } emit onWeatherUpdate(m_city_name, m_cond_txt, m_temperature); } } else { qWarning() << "get weather info error : " << JPE.errorString(); + emit onWeatherUpdate("天气不可用", "", ""); } reply->deleteLater(); @@ -202,17 +238,33 @@ QPixmap WeatherManager::getWeatherIcon(QString cond) if (cond.isEmpty()) { qWarning() << "cond info is unknown"; - return QPixmap(":/image/assets/weather/55.png").scaled(32,32); + return QPixmap(":/weather/assets/weather-icon/999.svg").scaled(32,32); } //根据m_cond_txt QString numStr = weatherMap.value(cond); if (!numStr.isEmpty()) { qDebug() << "----------------numStr=" + numStr; - return QPixmap(":/image/assets/weather/" + numStr +".png").scaled(32,32); + return QPixmap(":/weather/assets/weather-icon/" + numStr +".svg").scaled(32,32); } - return QPixmap(":/image/assets/weather/55.png").scaled(32,32); + qWarning() << "天气为|" << cond << "|"; + return QPixmap(":/weather/assets/weather-icon/999.svg").scaled(32,32); +} + +QString WeatherManager::getCityName() +{ + return ""; +} + +QString WeatherManager::getCond() +{ + return ""; +} + +QString WeatherManager::getTemperature() +{ + return ""; } LocalWeatherInfo::LocalWeatherInfo(QObject *parent) diff --git a/src/weathermanager.h b/src/weathermanager.h index b1d2783..3f29c40 100644 --- a/src/weathermanager.h +++ b/src/weathermanager.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 Tianjin KYLIN Information Technology Co., Ltd. + * Copyright (C) 2020 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 @@ -24,6 +24,8 @@ #include #include +#include "../src/networkwatcher.h" + class QNetworkAccessManager; class QNetworkReply; class LocalWeatherInfo; @@ -39,12 +41,17 @@ Q_SIGNALS: private Q_SLOTS: void replyFinished(QNetworkReply *); + void onNetworkStateChanged(uint state); public: void getWeather(); QPixmap getWeatherIcon(); QPixmap getWeatherIcon(QString cond); + QString getCityName(); + QString getCond(); + QString getTemperature(); + private: bool updateLocation();//更新位置,从用户设置获取城市信息,如有多个,只取第一个,未对接 void weatherRequest(); @@ -63,6 +70,9 @@ private: QGSettings *m_settings; LocalWeatherInfo *m_local_weather_info; + NetWorkWatcher *m_networkWatcher; + + int m_networkTryNum = 0; }; class LocalWeatherInfo : QObject