refs #446, moved common parts for GUI applications in a utility function

* for init
* for parser
This commit is contained in:
Klaus Basan
2015-06-19 23:35:54 +02:00
committed by Mathew Sutcliffe
parent 091c61c909
commit 08670c9e89
4 changed files with 106 additions and 110 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -1,5 +1,13 @@
#include "swiftcore.h"
/* Copyright (C) 2013
* swift Project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include "swiftcore.h"
#include "blackcore/context_runtime.h"
#include "blackcore/context_settings.h"
#include "blackcore/context_application.h"
@@ -12,6 +20,7 @@
#include "blackmisc/project.h"
#include "blackmisc/loghandler.h"
#include "blackmisc/filelogger.h"
#include "blackgui/guiutility.h"
#include "blackgui/stylesheetutility.h"
#include <QApplication>
@@ -34,9 +43,7 @@ CommandLineParseResult parseCommandLine(QCommandLineParser &parser, CSwiftCore::
parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
parser.addOption({{"s", "session"}, QCoreApplication::translate("main", "Use session bus.")});
parser.addOption({{"y", "system"}, QCoreApplication::translate("main", "Use system bus.")});
parser.addOption({{"p", "p2p"}, QCoreApplication::translate("main", "Use P2P bus with <address>."),
QCoreApplication::translate("main", "address")
});
parser.addOption({{"p", "p2p"}, QCoreApplication::translate("main", "Use P2P bus with <address>."), QCoreApplication::translate("main", "address") });
parser.addOption({{"m", "minimized"}, QCoreApplication::translate("main", "Start minimized in system tray.")});
if (!parser.parse(QCoreApplication::arguments()))
@@ -68,6 +75,7 @@ CommandLineParseResult parseCommandLine(QCommandLineParser &parser, CSwiftCore::
if (parser.isSet("p2p"))
{
const QString address = CDBusServer::fixAddressToDBusAddress(parser.value("p2p"));
Q_UNUSED(address);
if (parser.isSet("session") || parser.isSet("system"))
{
@@ -80,21 +88,15 @@ CommandLineParseResult parseCommandLine(QCommandLineParser &parser, CSwiftCore::
{
setup->m_minimzed = true;
}
return CommandLineOk;
}
int main(int argc, char *argv[])
{
CRuntime::registerMetadata(); // register metadata
QApplication a(argc, argv);
QApplication::setApplicationName("swiftcore");
QApplication::setApplicationVersion(CProject::version());
CFileLogger fileLogger(QStringLiteral("swiftcore"), QString(), &a);
fileLogger.changeLogPattern(CLogPattern().withSeverityAtOrAbove(CStatusMessage::SeverityDebug));
CGuiUtility::initSwiftGuiApplication(a, "swiftcore", CIcons::swiftNova24());
QCommandLineParser parser;
parser.setApplicationDescription("swiftcore control");
parser.setApplicationDescription("swiftcore");
parser.addHelpOption();
parser.addVersionOption();
@@ -105,66 +107,23 @@ int main(int argc, char *argv[])
case CommandLineOk:
break;
case CommandLineError:
#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
CGuiUtility::commandLineErrorMessage(errorMessage, parser);
return 1;
case 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
CGuiUtility::commandLineVersionRequested();
return 0;
case CommandLineHelpRequested:
#ifdef Q_OS_WIN
QMessageBox::warning(0, QGuiApplication::applicationDisplayName(),
"<html><head/><body><pre>"
+ parser.helpText() + "</pre></body></html>");
CGuiUtility::commandLineHelpRequested(parser);
return 0;
#else
parser.showHelp();
Q_UNREACHABLE();
#endif
}
if (!QSystemTrayIcon::isSystemTrayAvailable())
{
QMessageBox::critical(0, QObject::tr("Systray"),
QObject::tr("I couldn't detect any system tray on this system."));
QMessageBox::critical(0, QObject::tr("Systray"), QObject::tr("I couldn't detect any system tray on this system."));
return 1;
}
// Translations
QFile file(":blackmisc/translations/blackmisc_i18n_de.qm");
CLogMessage("swift.standardgui.main").debug() << (file.exists() ? "Found translations in resources" : "No translations in resources");
QTranslator translator;
if (translator.load("blackmisc_i18n_de", ":blackmisc/translations/"))
{
CLogMessage("swift.standardgui.main").debug() << "Translator loaded";
}
QIcon icon(BlackMisc::CIcons::swift24());
QApplication::setWindowIcon(icon);
const QString s = CStyleSheetUtility::instance().styles(
{
CStyleSheetUtility::fileNameFonts(),
CStyleSheetUtility::fileNameSwiftCore()
}
);
a.installTranslator(&translator);
a.setStyleSheet(s);
CSwiftCore w(setup);
if (!setup.m_minimzed) w.show();
if (!setup.m_minimzed) { w.show(); }
return a.exec();
}

View File

@@ -13,6 +13,7 @@
#include "blackgui/stylesheetutility.h"
#include "blackcore/blackcorefreefunctions.h"
#include "blackcore/context_runtime_config.h"
#include "blackgui/guiutility.h"
#include "blackmisc/blackmiscfreefunctions.h"
#include "blackmisc/logmessage.h"
#include "blackmisc/icons.h"
@@ -28,60 +29,17 @@ using namespace BlackGui;
using namespace BlackMisc;
using namespace BlackCore;
/*!
* \brief Main
* \param argc
* \param argv
* \return
*/
int main(int argc, char *argv[])
{
// register
BlackMisc::initResources();
BlackMisc::registerMetadata();
BlackMisc::Simulation::registerMetadata();
BlackCore::registerMetadata();
// BlackMisc::displayAllUserMetatypesTypes();
// application
QApplication a(argc, argv);
CLogHandler::instance()->install();
CLogHandler::instance()->enableConsoleOutput(true);
CLogHandler::instance()->handlerForPattern(
CLogPattern::anyOf({ CLogCategory::contextSlot(), CLogCategory::context() })
)->enableConsoleOutput(false);
CLogHandler::instance()->handlerForPattern(
CLogPattern::anyOf({ CLogCategory::contextSlot(), CLogCategory::context() }).withSeverityAtOrAbove(CStatusMessage::SeverityInfo)
)->enableConsoleOutput(true);
CFileLogger fileLogger(QStringLiteral("swiftgui_std"), QString(), &a);
fileLogger.changeLogPattern(CLogPattern().withSeverityAtOrAbove(CStatusMessage::SeverityDebug));
// Translations
QFile file(":blackmisc/translations/blackmisc_i18n_de.qm");
CLogMessage("swift.standardgui.main").debug() << (file.exists() ? "Found translations in resources" : "No translations in resources");
QTranslator translator;
if (translator.load("blackmisc_i18n_de", ":blackmisc/translations/"))
{
CLogMessage("swift.standardgui.main").debug() << "Translator loaded";
}
QIcon icon(BlackMisc::CIcons::swift24());
QApplication::setWindowIcon(icon);
const QString s = CStyleSheetUtility::instance().styles(
{
CStyleSheetUtility::fileNameFonts(),
CStyleSheetUtility::fileNameMainWindow()
}
);
a.installTranslator(&translator);
a.setStyleSheet(s);
CGuiUtility::initSwiftGuiApplication(a, "swiftgui", CIcons::swift24());
// modes
BlackGui::CEnableForFramelessWindow::WindowMode windowMode;
// Dialog to decide external or internal core
CIntroWindow intro;
intro.setWindowIcon(icon);
intro.setWindowIcon(CIcons::swift24());
BlackCore::CRuntimeConfig runtimeConfig;
if (intro.exec() == QDialog::Rejected)
{