mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-04 00:16:51 +08:00
refs #446, moved common parts for GUI applications in a utility function
* for init * for parser
This commit is contained in:
committed by
Mathew Sutcliffe
parent
091c61c909
commit
08670c9e89
@@ -8,12 +8,20 @@
|
||||
*/
|
||||
|
||||
#include "guiutility.h"
|
||||
#include "blackcore/context_runtime.h"
|
||||
#include "blackmisc/filelogger.h"
|
||||
#include "blackmisc/logmessage.h"
|
||||
#include "blackmisc/project.h"
|
||||
#include <QWidget>
|
||||
#include <QApplication>
|
||||
#include <QGuiApplication>
|
||||
#include <QMessageBox>
|
||||
|
||||
using namespace BlackCore;
|
||||
using namespace BlackMisc;
|
||||
|
||||
namespace BlackGui
|
||||
{
|
||||
|
||||
CEnableForFramelessWindow *CGuiUtility::mainApplicationWindow()
|
||||
{
|
||||
QWidgetList tlw = topLevelApplicationWidgetsWithName();
|
||||
@@ -39,6 +47,35 @@ namespace BlackGui
|
||||
return (mw && mw->isFrameless());
|
||||
}
|
||||
|
||||
void CGuiUtility::initSwiftGuiApplication(QApplication &a, const QString &applicationName, const QPixmap &icon)
|
||||
{
|
||||
CRuntime::registerMetadata(); // register metadata
|
||||
CLogHandler::instance()->install(); // make sure we have a log handler!
|
||||
|
||||
QApplication::setApplicationName(applicationName);
|
||||
QApplication::setApplicationVersion(CProject::version());
|
||||
QApplication::setWindowIcon(icon);
|
||||
|
||||
// Logging
|
||||
QString category("swift." + applicationName);
|
||||
|
||||
// Translations
|
||||
QFile file(":blackmisc/translations/blackmisc_i18n_de.qm");
|
||||
CLogMessage(category).debug() << (file.exists() ? "Found translations in resources" : "No translations in resources");
|
||||
QTranslator translator;
|
||||
if (translator.load("blackmisc_i18n_de", ":blackmisc/translations/"))
|
||||
{
|
||||
CLogMessage(category).debug() << "Translator loaded";
|
||||
}
|
||||
|
||||
// File logger
|
||||
CFileLogger fileLogger(applicationName, QString(), &a);
|
||||
fileLogger.changeLogPattern(CLogPattern().withSeverityAtOrAbove(CStatusMessage::SeverityDebug));
|
||||
|
||||
// GUI icon
|
||||
a.installTranslator(&translator);
|
||||
}
|
||||
|
||||
QWidgetList CGuiUtility::topLevelApplicationWidgetsWithName()
|
||||
{
|
||||
QWidgetList tlw = QApplication::topLevelWidgets();
|
||||
@@ -111,4 +148,34 @@ namespace BlackGui
|
||||
// then finally
|
||||
delete layout;
|
||||
}
|
||||
|
||||
void CGuiUtility::commandLineErrorMessage(const QString &errorMessage, const QCommandLineParser &parser)
|
||||
{
|
||||
# ifdef Q_OS_WIN
|
||||
QMessageBox::warning(0, QGuiApplication::applicationDisplayName(), "<html><head/><body><h2>" + errorMessage + "</h2><pre>" + parser.helpText() + "</pre></body></html>");
|
||||
# else
|
||||
fputs(qPrintable(errorMessage), stderr);
|
||||
fputs("\n\n", stderr);
|
||||
fputs(qPrintable(parser.helpText()), stderr);
|
||||
# endif
|
||||
}
|
||||
|
||||
void CGuiUtility::commandLineVersionRequested()
|
||||
{
|
||||
# ifdef Q_OS_WIN
|
||||
QMessageBox::information(0, QGuiApplication::applicationDisplayName(), QGuiApplication::applicationDisplayName() + ' ' + QCoreApplication::applicationVersion());
|
||||
# else
|
||||
printf("%s %s\n", qPrintable(QCoreApplication::applicationName()), qPrintable(QCoreApplication::applicationVersion()));
|
||||
# endif
|
||||
}
|
||||
|
||||
void CGuiUtility::commandLineHelpRequested(QCommandLineParser &parser)
|
||||
{
|
||||
# ifdef Q_OS_WIN
|
||||
QMessageBox::warning(0, QGuiApplication::applicationDisplayName(), "<html><head/><body><pre>" + parser.helpText() + "</pre></body></html>");
|
||||
# else
|
||||
parser.showHelp(); // terminates
|
||||
Q_UNREACHABLE();
|
||||
# endif
|
||||
}
|
||||
} // ns
|
||||
|
||||
@@ -13,8 +13,10 @@
|
||||
#define BLACKGUI_GUIUTILITY_H
|
||||
|
||||
#include "blackgui/blackguiexport.h"
|
||||
#include <QWidgetList>
|
||||
#include "blackmisc/icon.h"
|
||||
#include "enableforframelesswindow.h"
|
||||
#include <QWidgetList>
|
||||
#include <QCommandLineParser>
|
||||
|
||||
namespace BlackGui
|
||||
{
|
||||
@@ -23,7 +25,6 @@ namespace BlackGui
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
//! Main application window
|
||||
static CEnableForFramelessWindow *mainApplicationWindow();
|
||||
|
||||
@@ -51,11 +52,22 @@ namespace BlackGui
|
||||
//! Delete hierarchy of layouts
|
||||
static void deleteLayout(QLayout *layout, bool deleteWidgets);
|
||||
|
||||
//! Message box or command line warning (depending on OS)
|
||||
static void commandLineErrorMessage(const QString &errorMessage, const QCommandLineParser &parser);
|
||||
|
||||
//! Message box or command line version info
|
||||
static void commandLineVersionRequested();
|
||||
|
||||
//! Message box or command line version info
|
||||
static void commandLineHelpRequested(QCommandLineParser &parser);
|
||||
|
||||
//! Standard initialization for a swift GUI application
|
||||
static void initSwiftGuiApplication(QApplication &a, const QString &applicationName, const QPixmap &icon = BlackMisc::CIcons::swift24());
|
||||
|
||||
private:
|
||||
//! Constructor, use static methods only
|
||||
CGuiUtility() {}
|
||||
};
|
||||
|
||||
}
|
||||
} // ns
|
||||
|
||||
#endif // guard
|
||||
|
||||
Reference in New Issue
Block a user