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

@@ -8,111 +8,43 @@
*/
#include "swiftcore.h"
#include "blackcore/corefacade.h"
#include "blackgui/guiapplication.h"
#include "blackgui/stylesheetutility.h"
#include "blackgui/guiutility.h"
#include "blackcore/contextapplication.h"
#include "blackcore/contextapplicationimpl.h"
#include "blackmisc/dbusserver.h"
#include "blackmisc/icons.h"
#include "blackmisc/worker.h"
#include "blackmisc/network/networkutils.h"
#include "blackmisc/blackmiscfreefunctions.h"
#include "blackmisc/project.h"
#include "blackmisc/loghandler.h"
#include "blackmisc/filelogger.h"
#include "blackgui/guiutility.h"
#include "blackgui/stylesheetutility.h"
#include <QApplication>
#include <QMessageBox>
using namespace BlackMisc;
using namespace BlackCore;
using namespace BlackGui;
enum CommandLineParseResult
{
CommandLineOk,
CommandLineError,
CommandLineVersionRequested,
CommandLineHelpRequested
};
CommandLineParseResult parseCommandLine(QCommandLineParser &parser, CSwiftCore::SetupInfo *setup, QString *errorMessage)
{
parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
parser.addOption({{"d", "dbus"}, QCoreApplication::translate("main", "DBus options: session, system, p2p."), "dbus"});
parser.addOption({{"m", "minimized"}, QCoreApplication::translate("main", "Start minimized in system tray.")});
parser.addOption({{"r", "start"}, QCoreApplication::translate("main", "Start the server.")});
parser.addOption({{"c", "coreaudio"}, QCoreApplication::translate("main", "Audio in core.")});
QCommandLineOption helpOption = parser.addHelpOption();
QCommandLineOption versionOption = parser.addVersionOption();
if (!parser.parse(QCoreApplication::arguments()))
{
*errorMessage = parser.errorText();
return CommandLineError;
}
// help/version
if (parser.isSet(helpOption)) { return CommandLineHelpRequested; }
if (parser.isSet(versionOption)) { return CommandLineVersionRequested; }
setup->m_dbusAddress = CDBusServer::sessionBusAddress(); // default
if (parser.isSet("dbus"))
{
QString v(parser.value("dbus").trimmed().toLower());
if (v.startsWith("p2p") || v.contains("peer") || v.contains("tcp:") || v.contains("host"))
{
setup->m_dbusAddress = CDBusServer::p2pAddress();
}
else if (v.contains("sys"))
{
setup->m_dbusAddress = CDBusServer::systemBusAddress(); // default
}
}
if (parser.isSet("minimized")) { setup->m_minimzed = true; }
if (parser.isSet("start")) { setup->m_start = true; }
if (parser.isSet("coreaudio")) { setup->m_coreAudio = true; }
return CommandLineOk;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
const QString appName("swift core");
a.setApplicationVersion(CProject::version());
a.setApplicationName(appName);
QCommandLineParser parser;
parser.setApplicationDescription(appName);
CGuiApplication a(argc, argv, "swift core");
a.setWindowIcon(CIcons::swiftNova24());
a.addWindowStateOption();
a.addDBusAddressOption();
a.addParserOption({{"r", "start"}, QCoreApplication::translate("main", "Start the server.")});
a.addParserOption({{"c", "coreaudio"}, QCoreApplication::translate("main", "Audio in core.")});
a.parse();
CSwiftCore::SetupInfo setup;
QString errorMessage;
switch (parseCommandLine(parser, &setup, &errorMessage))
{
case CommandLineOk:
break;
case CommandLineError:
CGuiUtility::commandLineErrorMessage(errorMessage, parser);
return 1;
case CommandLineVersionRequested:
CGuiUtility::commandLineVersionRequested();
return 0;
case CommandLineHelpRequested:
CGuiUtility::commandLineHelpRequested(parser);
return 0;
}
const QString dBusAdress(a.getCmdDBusAddressValue());
a.useContexts(a.isParserOptionSet("coreaudio") ?
CCoreFacadeConfig::forCoreAllLocalInDBus(dBusAdress) :
CCoreFacadeConfig::forCoreAllLocalInDBusNoAudio(dBusAdress));
if (!QSystemTrayIcon::isSystemTrayAvailable())
{
QMessageBox::critical(0, QObject::tr("Systray"), QObject::tr("I couldn't detect any system tray on this system."));
return 1;
a.parserErrorMessage("I could not detect any system tray on this system.");
return EXIT_FAILURE;
}
CGuiUtility::initSwiftGuiApplication(a, appName, CIcons::swiftNova24());
CSwiftCore w(setup);
if (!setup.m_minimzed) { w.show(); }
CSwiftCore w;
if (a.getWindowState() != Qt::WindowMinimized) { w.show(); }
return a.exec();
}

View File

@@ -13,6 +13,7 @@
#include "blackmisc/loghandler.h"
#include "blackmisc/project.h"
#include "blackmisc/dbusserver.h"
#include "blackgui/guiapplication.h"
#include "blackgui/components/logcomponent.h"
#include "blackgui/components/enableforruntime.h"
#include "blackgui/stylesheetutility.h"
@@ -25,25 +26,23 @@ using namespace BlackCore;
using namespace BlackGui;
using namespace BlackGui::Components;
CSwiftCore::CSwiftCore(const SetupInfo &info, QWidget *parent) :
CSwiftCore::CSwiftCore(QWidget *parent) :
CSystemTrayWindow(CIcons::swiftNova24(), parent),
CIdentifiable(this),
ui(new Ui::CSwiftCore)
{
ui->setupUi(this);
const QString name(QCoreApplication::instance()->applicationName() + " " + CProject::version());
sGui->initMainApplicationWindow(this);
const QString name(sGui->getApplicationNameAndVersion());
setSystemTrayMode(MinimizeToTray | QuitOnClose);
setSystemTrayToolTip(name);
setWindowTitle(name);
setWindowIcon(CIcons::swiftNova24());
setWindowIconText(name);
initLogDisplay();
initSlots();
initStyleSheet();
initDBusMode(info);
initDBusMode();
if (info.m_start) { startCore(info); }
if (sGui->isParserOptionSet("start")) { startCore(sGui->getCmdDBusAddressValue()); }
}
void CSwiftCore::initStyleSheet()
@@ -58,20 +57,21 @@ void CSwiftCore::initStyleSheet()
this->setStyleSheet(s);
}
void CSwiftCore::initDBusMode(const CSwiftCore::SetupInfo &setup)
void CSwiftCore::initDBusMode()
{
if (setup.m_dbusAddress.startsWith(CDBusServer::sessionBusAddress()))
const QString dBusAddress(sGui->getCmdDBusAddressValue());
if (dBusAddress.startsWith(CDBusServer::sessionBusAddress()))
{
this->ui->rb_SessionBus->setChecked(true);
}
else if (setup.m_dbusAddress.startsWith(CDBusServer::systemBusAddress()))
else if (dBusAddress.startsWith(CDBusServer::systemBusAddress()))
{
this->ui->rb_SystemBus->setChecked(true);
}
else
{
this->ui->rb_P2PBus->setChecked(true);
this->ui->le_P2PAddress->setText(setup.m_dbusAddress);
this->ui->le_P2PAddress->setText(dBusAddress);
}
}
@@ -80,9 +80,7 @@ CSwiftCore::~CSwiftCore()
void CSwiftCore::ps_startCorePressed()
{
SetupInfo setup;
setup.m_dbusAddress = getDBusAddress();
startCore(setup);
startCore(getDBusAddress());
}
void CSwiftCore::ps_stopCorePressed()
@@ -131,10 +129,10 @@ void CSwiftCore::initLogDisplay()
logHandler->subscribe(this, &CSwiftCore::ps_appendLogMessage);
}
void CSwiftCore::startCore(const SetupInfo &setup)
void CSwiftCore::startCore(const QString &dBusAdress)
{
if (getRuntime()) { return; }
if (setup.m_dbusAddress.isEmpty()) { return; }
if (dBusAdress.isEmpty()) { return; }
ui->pb_StartCore->setEnabled(false);
ui->pb_StopCore->setEnabled(true);
@@ -142,9 +140,9 @@ void CSwiftCore::startCore(const SetupInfo &setup)
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
// context
this->createRuntime(setup.m_coreAudio ?
CCoreFacadeConfig::forCoreAllLocalInDBus(setup.m_dbusAddress) :
CCoreFacadeConfig::forCoreAllLocalInDBusNoAudio(setup.m_dbusAddress),
this->createRuntime(sGui->isParserOptionSet("coreaudio") ?
CCoreFacadeConfig::forCoreAllLocalInDBus(dBusAdress) :
CCoreFacadeConfig::forCoreAllLocalInDBusNoAudio(dBusAdress),
this);
CEnableForRuntime::setRuntimeForComponents(this->getRuntime(), this);
connect(ui->le_CommandLineInput, &CCommandInput::commandEntered, getRuntime(), &CCoreFacade::parseCommandLine);

View File

@@ -31,41 +31,34 @@ class CSwiftCore :
Q_OBJECT
public:
//! SwiftCore setup information
struct SetupInfo
{
SetupInfo() {}
bool m_minimzed = false; //!< Start minimized to tray
bool m_start = false; //!< Start server when core is started
bool m_coreAudio = false; //!< Audio in core
QString m_dbusAddress; //!< DBus address (session, system, p2p)
};
//! Constructor
CSwiftCore(const SetupInfo &info, QWidget *parent = nullptr);
CSwiftCore(QWidget *parent = nullptr);
//! Destructor
~CSwiftCore();
private slots:
// PushButton slots
//! \name PushButton slots
//! @[
void ps_startCorePressed();
void ps_stopCorePressed();
void ps_appendLogMessage(const BlackMisc::CStatusMessage &message);
void ps_p2pModeToggled(bool checked);
//! }@
//! Style sheet has changed
virtual void ps_onStyleSheetsChanged();
private:
//! \name Init
//! @[
void initSlots();
void initLogDisplay();
void initStyleSheet();
void initDBusMode(const SetupInfo &setup);
void initDBusMode();
//! }@
void startCore(const SetupInfo &setup);
void startCore(const QString &dBusAdress);
void stopCore();
QString getDBusAddress() const;