RedPanda-CPP/RedPandaIDE/main.cpp

197 lines
6.3 KiB
C++
Raw Normal View History

2021-12-26 23:18:28 +08:00
/*
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.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
* (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 <https://www.gnu.org/licenses/>.
*/
2021-04-06 23:10:57 +08:00
#include "mainwindow.h"
#include "settings.h"
2021-04-11 21:33:08 +08:00
#include "systemconsts.h"
#include "utils.h"
2021-04-06 23:10:57 +08:00
#include <QApplication>
#include <QDir>
#include <QTranslator>
#include <QStandardPaths>
#include <QMessageBox>
2021-04-15 11:18:14 +08:00
#include <QStringList>
#include <QAbstractNativeEventFilter>
#include <QDir>
2021-04-24 15:57:45 +08:00
#include "common.h"
2021-06-18 21:48:40 +08:00
#include "colorscheme.h"
#include "iconsmanager.h"
2021-09-04 11:37:04 +08:00
#include "autolinkmanager.h"
2021-09-28 17:17:33 +08:00
#include "platform.h"
#include "parser/parserutils.h"
#include "editorlist.h"
#ifdef Q_OS_WIN
#include <windows.h>
2021-12-24 23:18:20 +08:00
#endif
#ifdef Q_OS_WIN
class WindowLogoutEventFilter : public QAbstractNativeEventFilter {
// QAbstractNativeEventFilter interface
public:
bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) override;
};
2021-10-20 18:05:43 +08:00
bool WindowLogoutEventFilter::nativeEventFilter(const QByteArray & /*eventType*/, void *message, long *result){
MSG * pMsg = static_cast<MSG *>(message);
switch(pMsg->message) {
case WM_QUERYENDSESSION:
if (pMsg->lParam == 0
|| (pMsg->lParam & ENDSESSION_LOGOFF)) {
if (!pMainWindow->close()) {
*result = 0;
} else {
*result = 1;
}
return true;
}
break;
case WM_DPICHANGED:
//todo
break;
}
return false;
}
2021-12-24 23:18:20 +08:00
#endif
QString getSettingFilename(const QString& filepath = QString()) {
2021-04-15 11:18:14 +08:00
QString filename;
if (filepath.isEmpty()) {
if (isGreenEdition()) {
2021-04-20 22:24:33 +08:00
filename = includeTrailingPathDelimiter(QApplication::applicationDirPath()) +
"config/" + APP_SETTSINGS_FILENAME;
2021-04-15 11:18:14 +08:00
} else {
2021-04-20 22:24:33 +08:00
filename =includeTrailingPathDelimiter(QStandardPaths::standardLocations(QStandardPaths::AppDataLocation)[0])
+ APP_SETTSINGS_FILENAME;
2021-04-15 11:18:14 +08:00
}
} else {
filename = filepath;
}
2021-04-15 11:18:14 +08:00
QFileInfo fileInfo(filename);
QDir dir(fileInfo.absoluteDir());
if (!dir.exists()) {
if (!dir.mkpath(dir.absolutePath())) {
QMessageBox::critical(nullptr, QObject::tr("Error"),
QString(QObject::tr("Can't create configuration folder %1")).arg(dir.absolutePath()));
return "";
}
}
if (fileInfo.exists() && !fileInfo.isWritable()) {
QMessageBox::critical(nullptr, QObject::tr("Error"),
2021-04-15 11:18:14 +08:00
QString(QObject::tr("Can't write to configuration file %1")).arg(filename));
return "";
}
return filename;
}
2021-04-06 23:10:57 +08:00
int main(int argc, char *argv[])
{
2021-12-13 12:09:37 +08:00
//QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
2021-04-11 21:33:08 +08:00
QApplication app(argc, argv);
//Translation must be loaded first
QTranslator trans,transQt;
QString settingFilename = getSettingFilename();
if (!isGreenEdition()) {
QDir::setCurrent(QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation)[0]);
}
if (settingFilename.isEmpty())
return -1;
{
2021-08-30 19:52:38 +08:00
QSettings languageSetting(settingFilename,QSettings::IniFormat);
languageSetting.beginGroup(SETTING_ENVIRONMENT);
QString language = languageSetting.value("language",QLocale::system().name()).toString();
if (trans.load("RedPandaIDE_"+language,":/translations")) {
2021-11-17 09:41:44 +08:00
app.installTranslator(&trans);
}
if (transQt.load("qt_"+language,":/translations")) {
app.installTranslator(&transQt);
}
}
2021-04-24 15:57:45 +08:00
qRegisterMetaType<PCompileIssue>("PCompileIssue");
qRegisterMetaType<PCompileIssue>("PCompileIssue&");
2021-08-01 01:06:43 +08:00
qRegisterMetaType<QVector<int>>("QVector<int>");
2021-11-25 09:05:45 +08:00
qRegisterMetaType<QHash<int,QString>>("QHash<int,QString>");
2021-04-24 15:57:45 +08:00
2021-08-07 14:08:51 +08:00
initParser();
2021-06-20 09:27:37 +08:00
try {
2021-06-20 09:27:37 +08:00
SystemConsts systemConsts;
pSystemConsts = &systemConsts;
2021-09-28 17:17:33 +08:00
pCharsetInfoManager = new CharsetInfoManager();
auto charsetInfoManager = std::unique_ptr<CharsetInfoManager>(pCharsetInfoManager);
2021-06-20 09:27:37 +08:00
//load settings
pSettings = new Settings(settingFilename);
2021-06-20 09:27:37 +08:00
auto settings = std::unique_ptr<Settings>(pSettings);
2021-08-07 14:08:51 +08:00
2021-06-20 14:30:47 +08:00
//Color scheme settings must be loaded after translation
pColorManager = new ColorManager();
pIconsManager = new IconsManager();
2021-09-04 11:37:04 +08:00
pAutolinkManager = new AutolinkManager();
2021-09-04 19:27:44 +08:00
try {
pAutolinkManager->load();
} catch (FileError e) {
QMessageBox::critical(nullptr,
QObject::tr("Can't load autolink settings"),
e.reason(),
QMessageBox::Ok);
}
2021-06-20 14:30:47 +08:00
//set default open folder
QDir::setCurrent(pSettings->environment().defaultOpenFolder());
2021-06-20 09:27:37 +08:00
MainWindow mainWindow;
pMainWindow = &mainWindow;
if (app.arguments().count()>1) {
QStringList filesToOpen = app.arguments();
filesToOpen.pop_front();
pMainWindow->openFiles(filesToOpen);
} else {
if (pSettings->editor().autoLoadLastFiles())
pMainWindow->loadLastOpens();
if (pMainWindow->editorList()->pageCount()==0) {
pMainWindow->newEditor();
}
}
2021-06-20 09:27:37 +08:00
mainWindow.show();
#ifdef Q_OS_WIN
WindowLogoutEventFilter filter;
app.installNativeEventFilter(&filter);
2021-12-24 23:18:20 +08:00
#endif
2021-06-20 09:27:37 +08:00
int retCode = app.exec();
QString configDir = pSettings->dirs().config();
2021-06-20 09:27:37 +08:00
// save settings
// settings->compilerSets().saveSets();
if (mainWindow.shouldRemoveAllSettings()) {
settings.release();
delete pSettings;
QDir dir(configDir);
dir.removeRecursively();
}
2021-06-20 09:27:37 +08:00
return retCode;
} catch (BaseError e) {
QMessageBox::critical(nullptr,QApplication::tr("Error"),e.reason());
return -1;
2021-06-20 09:27:37 +08:00
}
2021-04-06 23:10:57 +08:00
}