Files
qt5-legatus-legacy/Lts/Application/THP/main.cpp
T
2026-08-13 18:33:10 +02:00

167 lines
4.3 KiB
C++
Executable File

/**
* @RDE_HEADER@
* Main entry for thp viewer
* $Id$
* $HeadURL$
* $Author$
*/
#include <nglapplication/nglsingleapplication.h>
#include <QUrl>
#include <QDir>
#include <QMessageBox>
#include <QNetworkProxy>
#include <QSplashScreen>
#include <LtsCommandLineParser/LtsCommandLineParser.h>
#include "thp_viewer.h"
#include <NGLLogger/INGLLogger.h>
#include <ngltranslator/ngltranslator.h>
#ifdef Q_OS_WIN
# include <windows.h>
#endif
class CmdLineParser : public LtsCommandLineParser::LtsCommandLineParser
{
public:
CmdLineParser()
: LtsCommandLineParser(ProvideCommandLineParameterOptions::None)
{
addOption({{QStringLiteral("l"), QStringLiteral("lang")},
QStringLiteral("Language iso code to use (de_DE, en_GB, en_US)"),
QStringLiteral("<locale iso code>")
});
addOption({{QStringLiteral("t"), QStringLiteral("alwaysontop")},
QStringLiteral("Set as always on top window")
});
}
bool useAsAlwaysOnTop() const
{
return isSet(QStringLiteral("t"));
}
QString langIsoCode() const { return value(QStringLiteral("l")); }
};
int main(int argc, char **argv)
{
QNetworkProxy::setApplicationProxy(QNetworkProxy());
NGLSingleApplication app(argc, argv);
app.createLogger("THP", "log4cxx_thp_viewer.props");
app.setOrganizationName(QStringLiteral("RME"));
app.setApplicationName(QStringLiteral("thp_viewer"));
app.setAttribute(Qt::AA_UseHighDpiPixmaps);
QDir::setSearchPaths(QLatin1String("icon"), QStringList(QLatin1String(":/style/")));
Q_INIT_RESOURCE(rme_light_style);
// Parse Options
CmdLineParser options;
if (!options.init())
return -1;
// Set Translation
QString langIsoCode = options.langIsoCode();
if (langIsoCode.isEmpty())
langIsoCode = QLocale::system().name();
NGL::setTranslationLocale(langIsoCode.toLatin1());
//check for optional alyways on top flag
bool alwaysOnTop = options.useAsAlwaysOnTop();
QString index;
const QStringList args = options.positionalArguments();
// QString searchDir = QCoreApplication::applicationDirPath();
QString searchDir(QDir::currentPath());
if (args.count() == 1)
{
QFileInfo fi(args.first());
if (fi.isDir())
searchDir = fi.absolutePath();
else
index = fi.absoluteFilePath();
}
if (index.isEmpty())
{
const auto indexFiles = { QStringLiteral("index_%1.html").arg(langIsoCode),
QStringLiteral("index.html") };
for (const QString &indexFile : indexFiles)
{
QDir d(searchDir);
index = d.absoluteFilePath(indexFile);
while (!d.exists(index))
{
if (!d.cdUp())
break;
index = d.absoluteFilePath(indexFile);
}
if (d.exists(index))
break;
}
}
QFileInfo fi(index);
QString basePath = fi.path() + QLatin1Char('/');
if (!fi.exists() || !fi.isFile())
{
fprintf(stderr, "%s <path_to_html> basePath=%s\n", argv[0], basePath.toStdString().c_str());
QMessageBox::critical(nullptr,
QObject::tr("File missing"),
QObject::tr("Could not find a valid index.html - aborting"));
return -1;
}
index = fi.absoluteFilePath();
if (app.sendMessage(index))
return 0;
int ret = 1;
{
QString splashfile = basePath;
static const auto extension = {QStringLiteral("gif"),
QStringLiteral("jpg"),
QStringLiteral("png")};
for (const auto &ext : extension)
{
const QString fn = splashfile + QStringLiteral("/wappen.") + ext;
if (!QFile::exists(fn))
continue;
splashfile = fn;
break;
}
const QPixmap splashPixmap(splashfile);
QSplashScreen splash((splashPixmap));
if (!splashPixmap.isNull())
splash.show();
#ifdef BUILD_STATIC_LIBRARIES
Q_INIT_RESOURCE(libsymbol);
Q_INIT_RESOURCE(nglresources);
#endif
NGL::ThpViewer view(QUrl::fromLocalFile(index));
if (alwaysOnTop)
{
view.setWindowFlag(Qt::WindowStaysOnTopHint, true);
view.setWindowFlag(Qt::WindowMinimizeButtonHint, false);
}
if (!view.init())
return ret;
view.center();
QObject::connect(&app, &NGLSingleApplication::messageReceived,
&view, &NGL::ThpViewer::onNewThp);
view.show();
splash.finish(&view);
ret = app.exec();
}
return ret;
}