refs #485, first version of a Gui/Core application class

Also specialized GUI application class for standard GUI
This commit is contained in:
Klaus Basan
2016-03-14 23:55:33 +00:00
committed by Mathew Sutcliffe
parent d9aac6427b
commit 158efe819a
27 changed files with 1170 additions and 506 deletions

View File

@@ -0,0 +1,140 @@
/* Copyright (C) 2016
* 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 "guiapplication.h"
#include "blackmisc/project.h"
#include <QApplication>
#include <QMessageBox>
using namespace BlackMisc;
BlackGui::CGuiApplication *sGui = nullptr; // set by constructor
namespace BlackGui
{
CGuiApplication *CGuiApplication::instance()
{
return qobject_cast<CGuiApplication *>(CApplication::instance());
}
CGuiApplication::CGuiApplication(const QString &applicationName, const QPixmap &icon) : CApplication(applicationName)
{
setWindowIcon(icon);
sGui = this;
}
CGuiApplication::~CGuiApplication()
{
sGui = nullptr;
}
void CGuiApplication::addWindowModeOption()
{
this->m_cmdWindowMode = QCommandLineOption(QStringList() << "w" << "window",
QCoreApplication::translate("main", "Windows: (n)ormal, (f)rameless, (t)ool."),
"windowtype");
this->addParserOption(this->m_cmdWindowMode);
}
void CGuiApplication::addWindowStateOption()
{
this->m_cmdWindowStateMinimized = QCommandLineOption({{"m", "minimized"}, QCoreApplication::translate("main", "Start minimized in system tray.")});
this->addParserOption(this->m_cmdWindowStateMinimized);
}
Qt::WindowState CGuiApplication::getWindowState() const
{
if (this->m_cmdWindowStateMinimized.valueName() == "empty") { return Qt::WindowNoState; }
if (this->m_parser.isSet(this->m_cmdWindowStateMinimized)) { return Qt::WindowMinimized; }
return Qt::WindowNoState;
}
CEnableForFramelessWindow::WindowMode CGuiApplication::getWindowMode() const
{
if (this->isParserOptionSet(m_cmdWindowMode))
{
const QString v(this->getParserOptionValue(this->m_cmdWindowMode));
return CEnableForFramelessWindow::stringToWindowMode(v);
}
else
{
return CEnableForFramelessWindow::WindowNormal;
}
}
void CGuiApplication::initMainApplicationWindow(QWidget *mainWindow) const
{
if (!mainWindow) { return; }
const QString name(this->getApplicationNameAndVersion());
mainWindow->setObjectName(QCoreApplication::applicationName());
mainWindow->setWindowTitle(name);
mainWindow->setWindowIcon(m_windowIcon);
mainWindow->setWindowIconText(name);
}
void CGuiApplication::setWindowIcon(const QPixmap &icon)
{
instance()->m_windowIcon = icon;
QApplication::setWindowIcon(icon);
}
void CGuiApplication::exit(int retcode)
{
CApplication::exit(retcode);
}
void CGuiApplication::errorMessage(const QString &errorMessage) const
{
if (CProject::isRunningOnWindowsNtPlatform())
{
QMessageBox::warning(nullptr,
QGuiApplication::applicationDisplayName(),
"<html><head/><body><h2>" + errorMessage + "</h2><pre>" + this->m_parser.helpText() + "</pre></body></html>");
}
else
{
CApplication::errorMessage(errorMessage);
}
}
void CGuiApplication::helpMessage()
{
if (CProject::isRunningOnWindowsNtPlatform())
{
QMessageBox::information(nullptr,
QGuiApplication::applicationDisplayName(),
"<html><head/><body><pre>" + this->m_parser.helpText() + "</pre></body></html>");
}
else
{
CApplication::helpMessage();
}
}
void CGuiApplication::versionMessage() const
{
if (CProject::isRunningOnWindowsNtPlatform())
{
QMessageBox::information(nullptr,
QGuiApplication::applicationDisplayName(),
QGuiApplication::applicationDisplayName() + ' ' + QCoreApplication::applicationVersion());
}
else
{
CApplication::versionMessage();
}
}
bool CGuiApplication::parsingHookIn()
{
// void
return true;
}
} // ns

View File

@@ -0,0 +1,87 @@
/* Copyright (C) 2016
* 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.
*/
//! \file
#ifndef BLACKGUI_GUIAPPLICATION_H
#define BLACKGUI_GUIAPPLICATION_H
#include "blackcore/application.h"
#include "blackgui/enableforframelesswindow.h"
#include "blackgui/blackguiexport.h"
namespace BlackGui
{
/*!
* \brief GUI application, a specialized version of BlackCore::CApplication for GUI applications.
*
* \details Analog to QCoreApplication and QApplication this class provides more details for swift
* GUI applications. It is normally used via the global sGui pointer. As an example of how to extend this
* class see CSwiftGuiStdApplication.
*/
class BLACKGUI_EXPORT CGuiApplication : public BlackCore::CApplication
{
Q_OBJECT
public:
//! Similar to \sa QCoreApplication::instance() returns the single instance
static CGuiApplication *instance();
//! Constructor
CGuiApplication(const QString &applicationName = executable(), const QPixmap &icon = BlackMisc::CIcons::swift48());
//! Destructor
virtual ~CGuiApplication();
//! CMD line arguments
void addWindowStateOption();
//! CMD line arguments
void addWindowModeOption();
//! Window state
Qt::WindowState getWindowState() const;
//! Window mode (window flags)
CEnableForFramelessWindow::WindowMode getWindowMode() const;
//! Init the main application window based on information in this application
void initMainApplicationWindow(QWidget *mainWindow) const;
//! \copydoc BlackCore::CApplication::errorMessage
virtual void errorMessage(const QString &errorMessage) const override;
//! Set icon
//! \note Pixmap requires a valid QApplication, so it cannot be passed as constructor parameter
static void setWindowIcon(const QPixmap &icon);
//! Exit application, perform graceful shutdown and exit
static void exit(int retcode = 0);
protected:
//! \name print messages generated during parsing
//! @{
virtual void helpMessage() override;
virtual void versionMessage() const override;
//! @}
//! Handle paring of special GUI cmd arguments
virtual bool parsingHookIn() override;
private:
QPixmap m_windowIcon;
QCommandLineOption m_cmdWindowStateMinimized { "empty" }; //!< window state (minimized)
QCommandLineOption m_cmdWindowMode { "empty" }; //!< window mode (flags: frameless ...)
};
} // ns
//! Single instance of GUI application object
extern BLACKGUI_EXPORT BlackGui::CGuiApplication *sGui;
#endif // guard

View File

@@ -69,39 +69,6 @@ namespace BlackGui
return (mw && mw->isFrameless());
}
void CGuiUtility::initSwiftGuiApplication(QApplication &a, const QString &applicationName, const QPixmap &icon)
{
CCoreFacade::registerMetadata(); // register metadata
CCookieManager::instance(); // init cookie manager if ever needed
CLogHandler::instance()->install(); // make sure we have a log handler!
QApplication::setApplicationName(applicationName);
QApplication::setApplicationVersion(CProject::version());
QApplication::setWindowIcon(icon);
// Logging
QString executableName = QFileInfo(QCoreApplication::applicationFilePath()).completeBaseName();
QString category("swift." + executableName);
// 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
static const QString logPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/org.swift-project/logs";
CFileLogger *fileLogger = new CFileLogger(executableName, logPath, &a);
fileLogger->changeLogPattern(CLogPattern().withSeverityAtOrAbove(CStatusMessage::SeverityDebug));
// GUI icon
a.installTranslator(&translator);
}
bool CGuiUtility::lenientTitleComparison(const QString &title, const QString &comparison)
{
if (title == comparison) { return true; }
@@ -274,34 +241,4 @@ namespace BlackGui
// then finally
delete layout;
}
void CGuiUtility::commandLineErrorMessage(const QString &errorMessage, const QCommandLineParser &parser)
{
# ifdef Q_OS_WIN
QMessageBox::warning(nullptr, 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(nullptr, 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::information(nullptr, QGuiApplication::applicationDisplayName(), "<html><head/><body><pre>" + parser.helpText() + "</pre></body></html>");
# else
parser.showHelp(); // terminates
Q_UNREACHABLE();
# endif
}
} // ns

View File

@@ -48,18 +48,6 @@ 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());
//! Leninet / relaxed
static bool lenientTitleComparison(const QString &title, const QString &comparison);