玉兔远程控制 0.1.0-bate8
载入中...
搜索中...
未找到
main.cpp
1// Author: Kang Lin <kl222@126.com>
2
16#include <QtGlobal>
17#include <QLoggingCategory>
18#include <QApplication>
19#include <QSettings>
20#include <QDebug>
21#include <QtGlobal>
22#include <QSharedPointer>
23#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
24 #include <QRegularExpression>
25 #include <QRandomGenerator>
26#endif
27#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
28 #include <QRandomGenerator>
29#endif
30#if defined(Q_OS_ANDROID) && (QT_VERSION >= QT_VERSION_CHECK(5, 7, 0)) && (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
31#include <QtAndroid>
32#endif
33
34#include "RabbitCommonTools.h"
35
36#ifdef HAVE_UPDATE
37#include "FrmUpdater.h"
38#endif
39#ifdef BUILD_QUIWidget
40 #include "QUIWidget/QUIWidget.h"
41#endif
42
43#include "StatsAppUsage.h"
44#include "mainwindow.h"
45
46static Q_LOGGING_CATEGORY(log, "App.Main")
47
48// Android平台的专用初始化函数(必须在QGuiApplication之前)
49void InitAndroidVirtualKeyboard()
50{
51 // 设置输入法模块(必须在QGuiApplication之前)
52 qputenv("QT_IM_MODULE", "qtvirtualkeyboard");
53
54 // Android特定环境变量
55 //qputenv("QT_VIRTUALKEYBOARD_ANDROID_HEIGHT", "400"); // 设置键盘高度
56 // 设置键盘样式
57 //qputenv("QT_VIRTUALKEYBOARD_STYLE", QByteArray("default")); // 可选: default, retro, etc.
58
59 // 设置布局(可选中文)
60 //qputenv("QT_VIRTUALKEYBOARD_LAYOUT", "en_US");
61 // qputenv("QT_VIRTUALKEYBOARD_LAYOUT", "zh_CN");
62
63 // 禁用硬件键盘检测(强制显示虚拟键盘)
64 //qputenv("QT_VIRTUALKEYBOARD_FORCE_ANDROID", "1");
65
66 // 设置手写识别(如果需要)
67 //qputenv("QT_VIRTUALKEYBOARD_HANDWRITING", QByteArray("1"));
68
69 // 启用调试
70 //qputenv("QT_DEBUG_IM", "1");
71}
72
73int main(int argc, char *argv[])
74{
75 int nRet = 0;
76
77// #if HAVE_VirtualKeyboard && defined(Q_OS_ANDROID)
78// InitAndroidVirtualKeyboard();
79// #endif
80
81 // 检查环境变量是否设置成功
82 QByteArray imModule = qgetenv("QT_IM_MODULE");
83 qDebug(log) << "QT_IM_MODULE set to:" << imModule;
84
85 QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
86
87 /* 移到 snapcraft.yaml 和 io.github.KangLin.RabbitRemoteControl.yml
88 // 修复 qtwebengine 沙箱权限问题
89 if(!qEnvironmentVariable("SNAP").isEmpty()) {
90 qputenv("QTWEBENGINE_DISABLE_SANDBOX", "1");
91 }//*/
92
93 //qputenv("QT_MEDIA_BACKEND", "ffmpeg");
94
95#if (defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID) \
96 && (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)))
97 /* 修复使用 Qt6 时,最大化时,工具栏位置错误。
98 现在很多 linux 用 wayland 或 wayland-egl 作为桌面显示,这样会出现一个问题,
99 由于没有坐标系统,导致无边框窗体无法拖动和定位(即 QWidge::move 失效)。
100 (Qt6 开始强制默认优先用 wayland ,Qt5 默认有 xcb 则优先用 xcb),
101 所以需要在 main 函数最前面加一行 `qputenv("QT_QPA_PLATFORM", "xcb")`;
102 */
103 QString szPlatform = qEnvironmentVariable("QT_QPA_PLATFORM");
104 if(szPlatform.isEmpty()
105 || -1 != szPlatform.indexOf(QRegularExpression("wayland.*")))
106 qputenv("QT_QPA_PLATFORM", "xcb");
107 qInfo(log) << "QT_QPA_PLATFORM:" << szPlatform << "\nCurrent Qt Platform is:"
108 << qEnvironmentVariable("QT_QPA_PLATFORM")
109 << "; This can be modified with the environment variables QT_QPA_PLATFORM:\n"
110 << "export QT_QPA_PLATFORM=xcb\n Optional: xcb; vnc";
111#endif
112#if (QT_VERSION > QT_VERSION_CHECK(5,6,0)) && (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
113 QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
114#endif
115#if defined(Q_OS_ANDROID) && (QT_VERSION >= QT_VERSION_CHECK(5, 7, 0)) && (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
116 QtAndroid::hideSplashScreen();
117#endif
118
119//#if defined (_DEBUG) || !defined(BUILD_SHARED_LIBS)
120// Q_INIT_RESOURCE(translations_RabbitRemoteControlApp);
121//#endif
122
123 QApplication::setApplicationVersion(RabbitRemoteControl_VERSION);
124 QApplication::setApplicationName("RabbitRemoteControl");
125#if QT_VERSION >= QT_VERSION_CHECK(5, 7, 0)
126 QApplication::setDesktopFileName(QLatin1String("io.github.KangLin.RabbitRemoteControl.desktop"));
127#endif
128
129 QApplication app(argc, argv);
130
131 QInputMethod *inputMethod = app.inputMethod();
132 qDebug(log) << "Input method available:" << (inputMethod != nullptr);
133 if(inputMethod)
134 qDebug(log) << "Input method locale:" << inputMethod->locale();
135
136 RabbitCommon::CTools::Instance()->Init();
137
138 qInfo(log) << app.applicationName() + " " + app.applicationVersion()
139 + " " + QObject::tr("Start") + " ......"
140 << "\n" << app.arguments();
141
142 QSharedPointer<QTranslator> tApp =
143 RabbitCommon::CTools::Instance()->InstallTranslator("RabbitRemoteControlApp");
144
145 app.setApplicationDisplayName(QObject::tr("Rabbit Remote Control"));
146 app.setOrganizationName(QObject::tr("Kang Lin Studio"));
147
148#ifdef HAVE_UPDATE
149 // Check update version
150 QScopedPointer<CFrmUpdater> pUpdater(new CFrmUpdater());
151#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
152 if(qEnvironmentVariable("SNAP").isEmpty()
153 && qEnvironmentVariable("FLATPAK_ID").isEmpty())
154#else
155 if(qgetenv("SNAP").isEmpty()
156 && qgetenv("FLATPAK_ID").isEmpty())
157#endif
158 {
159 if(pUpdater) {
160 pUpdater->setAttribute(Qt::WA_DeleteOnClose, false);
161 QIcon icon = QIcon::fromTheme("app");
162 if(!icon.isNull()) {
163 auto sizeList = icon.availableSizes();
164 if(!sizeList.isEmpty()) {
165 QPixmap p = icon.pixmap(*sizeList.begin());
166 pUpdater->SetTitle(p.toImage());
167 }
168 }
169 if(app.arguments().length() > 1) {
170 try {
171 pUpdater->GenerateUpdateJson();
172 pUpdater->GenerateUpdateXml();
173 } catch(...) {
174 qCritical(log) << "Generate update fail";
175 }
176 qInfo(log) << app.applicationName() + " " + app.applicationVersion()
177 + " " + QObject::tr("Generate update json file End");
178 return 0;
179 }
180 } else {
181 qCritical(log) << "new CFrmUpdater() fail";
182 }
183 }
184#endif // #ifdef HAVE_UPDATE
185
186 CStatsAppUsage* pStats = nullptr;
187 /*
188 int randomInt = 0;
189 int nMin = 10;
190 int nMax = 50;
191#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
192 randomInt = QRandomGenerator::global()->bounded(nMin, nMax);
193#else
194 qsrand(static_cast<uint>(QTime::currentTime().msec())); // 使用当前时间的毫秒数来设置种子
195 randomInt = nMin + qrand() % (nMax - nMin + 1); // 生成一个0到99之间的随机数
196#endif
197 QTimer::singleShot(randomInt, [&](){
198 pStats = new CStatsAppUsage("v" + QApplication::applicationVersion());
199 app.processEvents();
200 });//*/
201
202 MainWindow* w = new MainWindow();
203
204 try {
205 //w->setWindowIcon(QIcon::themeName("app"));
206 //w->setWindowTitle(app.applicationDisplayName());
207 app.processEvents();
208 RC_SHOW_WINDOW(w);
209 app.processEvents();
210 // For time-consuming operations
211 nRet = w->Initial();
212 if(!nRet)
213 nRet = app.exec();
214 } catch (std::exception &e) {
215 qCritical(log) << "exception:" << e.what();
216 } catch(...) {
217 qCritical(log) << "exception:";
218 }
219
220 delete w;
221 if(pStats) {
222 pStats->Stop();
223 delete pStats;
224 }
225
226 RabbitCommon::CTools::Instance()->Clean();
227 if(tApp)
228 RabbitCommon::CTools::Instance()->RemoveTranslator(tApp);
229//#if defined (_DEBUG) || !defined(BUILD_SHARED_LIBS)
230// Q_CLEANUP_RESOURCE(translations_RabbitRemoteControlApp);
231//#endif
232
233 qInfo(log) << app.applicationName() + " " + app.applicationVersion() + " " + "End";
234 return nRet;
235}
The MainWindow class
Definition mainwindow.h:36
int Initial()
For time-consuming operations