实现屏保插件,修改锁屏加载屏保方式由执行命令改为静态调用

This commit is contained in:
liuyuanpeng 2022-07-18 09:38:06 +08:00
parent 0a1533d27e
commit e51bcbd333
24 changed files with 441 additions and 206 deletions

View File

@ -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)

View File

@ -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

View File

@ -0,0 +1,11 @@
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}

View File

@ -0,0 +1,46 @@
#pragma execution_character_set("utf-8")
#include "widget.h"
#include "ui_widget.h"
#include <QFileDialog>
#include <QPluginLoader>
#include <QHBoxLayout>
#include <memory>
#include <ukui-screensaver/screensaverplugin.h>
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<ScreensaverPlugin> interface_ptr = std::unique_ptr<ScreensaverPlugin>(qobject_cast<ScreensaverPlugin*>(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);
}
}

View File

@ -0,0 +1,25 @@
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
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

View File

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<widget class="QPushButton" name="load_plugin_btn">
<property name="geometry">
<rect>
<x>140</x>
<y>30</y>
<width>111</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>LoadPlugin</string>
</property>
</widget>
<widget class="QWidget" name="widget" native="true">
<property name="geometry">
<rect>
<x>30</x>
<y>90</y>
<width>331</width>
<height>141</height>
</rect>
</property>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>

View File

@ -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/)

View File

@ -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";
}

View File

@ -0,0 +1,21 @@
#ifndef CUSTOMPLUGIN_H
#define CUSTOMPLUGIN_H
#include "screensaverplugin.h"
#include <QObject>
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

View File

@ -34,7 +34,6 @@
#include "config.h"
#define WORKING_DIRECTORY "/usr/share/ukui-screensaver"
bool bControlFlg = false;//是否控制面板窗口
int main(int argc, char *argv[])
@ -45,18 +44,10 @@ int main(int argc, char *argv[])
#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;
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())
{

View File

@ -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<QLabel*> labelList = this->findChildren<QLabel *>();
@ -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()<<qssFile.readAll();
}
qssFile.close();
#ifdef USE_INTEL
m_weatherManager = new WeatherManager(this);
setWeatherLayout();
setDatelayout();
setCenterWidget();

View File

@ -37,14 +37,15 @@
#include "checkbutton.h"
#include "scconfiguration.h"
#include "cyclelabel.h"
#include "weathermanager.h"
class WeatherManager;
class Screensaver : public QWidget
{
Q_OBJECT
public:
explicit Screensaver(QWidget *parent = 0);
explicit Screensaver(bool isScreensaver,QWidget *parent = 0);
~Screensaver();
void addClickedEvent();
@ -135,7 +136,7 @@ private:
int blur_Num;
QString curStyle;
WeatherManager *m_weatherManager;
WeatherManager *m_weatherManager=nullptr;
QWidget *m_weatherLaout;
QLabel *m_weatherIcon;
QLabel *m_weatherArea;
@ -153,7 +154,8 @@ private:
int delayTime;
QTimer *movieTimer = nullptr;
int currentCount = 0;
bool bControlFlg = true;
bool isScreensaver = false;
protected:
void paintEvent(QPaintEvent *event);
void resizeEvent(QResizeEvent *event);

View File

@ -0,0 +1,27 @@
#ifndef SCREENSAVER_PLUGIN_H
#define SCREENSAVER_PLUGIN_H
#include <QWidget>
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

View File

@ -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 <http://www.gnu.org/licenses/>.
*
* Authors: ZHAI Kang-ning <zhaikangning@kylinos.cn>
**/
#ifndef WEATHERMANAGER_H
#define WEATHERMANAGER_H
#include <QObject>
#include <functional>
#include <QTimer>
#include <QGSettings>
#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

View File

@ -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)

View File

@ -25,7 +25,7 @@
//#include "gsettings.h"
#include <QObject>
#include "screensaver.h"
#include "screensavermode.h"
class QGSettings;

View File

@ -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();
}
}

View File

@ -15,7 +15,7 @@
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
**/
#include "screensaver.h"
#include "screensavermode.h"
#include <QFileInfo>
#include <QDir>
#include <QImageReader>

View File

@ -15,8 +15,8 @@
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
**/
#ifndef SCREENSAVER_H
#define SCREENSAVER_H
#ifndef SCREENSAVER_MODE_H
#define SCREENSAVER_MODE_H
#include <QObject>
#include <QTimer>
@ -74,4 +74,4 @@ Q_DECLARE_METATYPE(ScreenSaver)
QDebug &operator<<(QDebug stream, const ScreenSaver &screensaver);
#endif // SCREENSAVER_H
#endif // SCREENSAVER_MODE_H

View File

@ -24,6 +24,7 @@
#include <QPainter>
#include <QKeyEvent>
#include <QtX11Extras/QX11Info>
#include "screensaver.h"
#include <X11/Xlib.h>
#include <sys/prctl.h>
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 = "<<path;
/*屏保模式为ukui或者自定义时直接加载对象为其他屏保时使用调用命令方式实现*/
if(screensaver->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*/)
{

View File

@ -20,8 +20,9 @@
#include <QWidget>
#include <QProcess>
#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

View File

@ -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

View File

@ -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<QString, QString> 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)

View File

@ -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 <QTimer>
#include <QGSettings>
#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