forked from linuxdeepin/deepin-calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
162 lines (145 loc) · 6.11 KB
/
main.cpp
File metadata and controls
162 lines (145 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* Copyright (C) 2017 ~ 2017 Deepin Technology Co., Ltd.
*
* Author: rekols <rekols@foxmail.com>
*
* 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
* 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/>.
*/
#include <QDBusInterface>
#include <QDate>
#include <QDebug>
#include <QDir>
#include <QIcon>
#include <QSettings>
#include <DApplication>
#include <DApplicationSettings>
#include <DGuiApplicationHelper>
#include <DLog>
#include <DWidgetUtil>
#include <DWindowManagerHelper>
#include "src/mainwindow.h"
#include "environments.h"
#include "src/utils.h"
DWIDGET_USE_NAMESPACE
static QString g_appPath; //全局路径
static bool oldversion = false; //旧版本升级新版本
//20200702 edit for bug-36389,旧版本有设置过的第一次读取后不再读取themetype.cfg
//获取配置文件主题类型,并重新设置
DGuiApplicationHelper::ColorType getThemeTypeSetting()
{
//需要找到自己程序的配置文件路径,并读取配置,这里只是用home路径下themeType.cfg文件举例,具体配置文件根据自身项目情况
QString t_appDir = g_appPath + QDir::separator() + "themetype.cfg";
QFile t_configFile(t_appDir);
int t_readType = 0;
if (t_configFile.exists()) {
t_configFile.open(QIODevice::ReadOnly | QIODevice::Text);
QByteArray t_readBuf = t_configFile.readAll();
t_readType = QString(t_readBuf).toInt();
t_configFile.remove();
oldversion = true;
}
//获取读到的主题类型,并返回设置
switch (t_readType) {
case 0:
// 跟随系统主题
return DGuiApplicationHelper::UnknownType;
case 1:
// 浅色主题
return DGuiApplicationHelper::LightType;
case 2:
// 深色主题
return DGuiApplicationHelper::DarkType;
default:
return DGuiApplicationHelper::UnknownType;
}
}
//保存当前主题类型配置文件
//void saveThemeTypeSetting(int type)
//{
// //需要找到自己程序的配置文件路径,并写入配置,这里只是用home路径下themeType.cfg文件举例,具体配置文件根据自身项目情况
// QString t_appDir = g_appPath + QDir::separator() + "themetype.cfg";
// QFile t_configFile(t_appDir);
// t_configFile.open(QIODevice::WriteOnly | QIODevice::Text);
// //直接将主题类型保存到配置文件,具体配置key-value组合根据自身项目情况
// QString t_typeStr = QString::number(type);
// t_configFile.write(t_typeStr.toUtf8());
// t_configFile.close();
//}
int main(int argc, char *argv[])
{
// DApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
DApplication app(argc, argv);
app.setAttribute(Qt::AA_UseHighDpiPixmaps);
g_appPath = QDir::homePath() + QDir::separator() + "." + qApp->applicationName();
QDir t_appDir;
t_appDir.mkpath(g_appPath);
app.loadTranslator();
app.setOrganizationName("deepin");
app.setApplicationName("deepin-calculator");
app.setApplicationAcknowledgementPage(
"https://www.deepin.org/acknowledgments/deepin-calculator");
QIcon t_icon = QIcon::fromTheme("deepin-calculator");
app.setProductIcon(t_icon);
app.setProductName(DApplication::translate("MainWindow", "Calculator"));
app.setApplicationDescription(
DApplication::translate("MainWindow",
"Calculator is an easy to use desktop calculator, supporting standard and scientific modes."));
app.setApplicationVersion(VERSION);
app.setQuitOnLastWindowClosed(true);
app.setApplicationDisplayName(QObject::tr("Calculator"));
// app.setStyle("chameleon");
QDBusConnection dbus = QDBusConnection::sessionBus();
dbus.registerService("com.deepin.calculator");
using namespace Dtk::Core;
Dtk::Core::DLogManager::registerConsoleAppender();
Dtk::Core::DLogManager::registerFileAppender();
QCommandLineParser cmdParser;
cmdParser.setApplicationDescription("deepin-calculator");
cmdParser.addHelpOption();
cmdParser.addVersionOption();
cmdParser.process(app);
MainWindow window;
DSettings *m_dsettings = DSettings::instance(&window);
if (app.setSingleInstance(app.applicationName(), DApplication::UserScope)) {
Dtk::Widget::moveToCenter(&window);
m_dsettings->setOption("windowX", window.pos().x());
m_dsettings->setOption("windowY", window.pos().y());
} else {
window.move(m_dsettings->getOption("windowX").toInt() + 10, m_dsettings->getOption("windowY").toInt() + 10);
}
DGuiApplicationHelper::ColorType oldpalette = getThemeTypeSetting();
DApplicationSettings savetheme(&app);
if (oldversion == true) {
DGuiApplicationHelper::instance()->setPaletteType(oldpalette);
}
// 应用已保存的主题设置
// DGuiApplicationHelper::ColorType t_type = DGuiApplicationHelper::instance()->themeType();
// saveThemeTypeSetting(t_type);
//监听当前应用主题切换事件
// QObject::connect(DGuiApplicationHelper::instance(),
// &DGuiApplicationHelper::paletteTypeChanged,
// [](DGuiApplicationHelper::ColorType type) {
// qDebug() << type;
// // 保存程序的主题设置 type : 0,系统主题, 1,浅色主题, 2,深色主题
// saveThemeTypeSetting(type);
// DGuiApplicationHelper::instance()->setPaletteType(type);
// });
// 20200330 主题记忆更改为规范代码
// DApplicationSettings savetheme(&app);
// Register debus service.
dbus.registerObject("/com/deepin/calculator", &window, QDBusConnection::ExportScriptableSlots);
window.show();
return app.exec();
}