From 08e8597ec87ed52f590aa99904ddfbe793c95129 Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Thu, 15 May 2014 17:43:10 +0200 Subject: [PATCH] Subclass QApplication and move most code from main() there Create a new subclass of the QApplication class and move most of the code in the main function to the constructor of the new class. This makes the code a bit more consistent in terms of object orientedness, makes it a little simpler and allows extending the new Application class in the future. --- src/Application.cpp | 93 +++++++++++++++++++++++++++++++++++++++++++++ src/Application.h | 22 +++++++++++ src/main.cpp | 91 ++------------------------------------------ src/src.pro | 6 ++- 4 files changed, 122 insertions(+), 90 deletions(-) create mode 100644 src/Application.cpp create mode 100644 src/Application.h diff --git a/src/Application.cpp b/src/Application.cpp new file mode 100644 index 00000000..f07cebb3 --- /dev/null +++ b/src/Application.cpp @@ -0,0 +1,93 @@ +#include +#include +#include +#include +#include +#include + +#include "Application.h" +#include "MainWindow.h" + +Application::Application(int& argc, char** argv) : + QApplication(argc, argv) +{ + // Set organisation and application names + setOrganizationName("sqlitebrowser"); + setApplicationName("SQLite Database Browser"); + + // Set character encoding to UTF8 + QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8")); +#if QT_VERSION < 0x050000 + QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8")); + QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8")); +#endif + + // Enable translation + QTranslator translator; + translator.load("qt_" + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath)); + installTranslator(&translator); + QTranslator apptranslator; + apptranslator.load("translations/tr_" + QLocale::system().name()); + installTranslator(&apptranslator); + + // Parse command line + QString fileToOpen; + QStringList sqlToExecute; + m_dontShowMainWindow = false; + for(int i=1;i= arguments().size()) + qWarning() << qPrintable(tr("The -s/--sql option requires an argument")); + else if(!QFile::exists(arguments().at(i))) + qWarning() << qPrintable(tr("The file %1 does not exist").arg(arguments().at(i))); + else + sqlToExecute.append(arguments().at(i)); + } else if(arguments().at(i) == "-q" || arguments().at(i) == "--quit") { + m_dontShowMainWindow = true; + } else { + // Other: Check if it's a valid file name + if(QFile::exists(arguments().at(i))) + fileToOpen = arguments().at(i); + else + qWarning() << qPrintable(tr("Invalid option/non-existant file: %1").arg(arguments().at(i))); + } + } + + // Show main window + m_mainWindow = new MainWindow(); + m_mainWindow->show(); + connect(this, SIGNAL(lastWindowClosed()), this, SLOT(quit())); + + // Open database if one was specified + if(fileToOpen.size()) + { + if(m_mainWindow->fileOpen(fileToOpen)) + { + // If database could be opened run the SQL scripts + foreach(const QString& f, sqlToExecute) + { + QFile file(f); + if(file.open(QIODevice::ReadOnly)) + { + m_mainWindow->getDb()->executeMultiSQL(file.readAll(), false, true); + file.close(); + } + } + if(!sqlToExecute.isEmpty()) + m_mainWindow->browseRefresh(); + } + } +} diff --git a/src/Application.h b/src/Application.h new file mode 100644 index 00000000..19c04abb --- /dev/null +++ b/src/Application.h @@ -0,0 +1,22 @@ +#ifndef __APPLICATION_H__ +#define __APPLICATION_H__ + +#include + +class MainWindow; + +class Application : public QApplication +{ + Q_OBJECT + +public: + explicit Application(int& argc, char** argv); + + bool dontShowMainWindow() { return m_dontShowMainWindow; } + +private: + bool m_dontShowMainWindow; + MainWindow* m_mainWindow; +}; + +#endif diff --git a/src/main.cpp b/src/main.cpp index 1af4177a..2329a3da 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,11 +1,4 @@ -#include "MainWindow.h" - -#include -#include -#include -#include -#include -#include +#include "Application.h" #if 0 and defined(Q_WS_MAC) #include @@ -75,93 +68,15 @@ static pascal OSErr odocHandler(const AppleEvent* inEvent, AppleEvent* int main( int argc, char ** argv ) { - QApplication a( argc, argv ); - a.setOrganizationName("sqlitebrowser"); - a.setApplicationName("SQLite Database Browser"); + Application a(argc, argv); - // Set character encoding to UTF8 - QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8")); -#if QT_VERSION < 0x050000 - QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8")); - QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8")); -#endif - - // Enable translation - QTranslator translator; - translator.load("qt_" + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath)); - a.installTranslator(&translator); - QTranslator apptranslator; - apptranslator.load("translations/tr_" + QLocale::system().name()); - a.installTranslator(&apptranslator); - - // Parse command line - QString fileToOpen; - QStringList sqlToExecute; - bool quitApplication = false; - for(int i=1;i= a.arguments().size()) - qWarning() << qPrintable(QObject::tr("The -s/--sql option requires an argument")); - else if(!QFile::exists(a.arguments().at(i))) - qWarning() << qPrintable(QObject::tr("The file %1 does not exist").arg(a.arguments().at(i))); - else - sqlToExecute.append(a.arguments().at(i)); - } else if(a.arguments().at(i) == "-q" || a.arguments().at(i) == "--quit") { - quitApplication = true; - } else { - // Other: Check if it's a valid file name - if(QFile::exists(a.arguments().at(i))) - fileToOpen = a.arguments().at(i); - else - qWarning() << qPrintable(QObject::tr("Invalid option/non-existant file: %1").arg(a.arguments().at(i))); - } - } - - MainWindow w; #if defined(Q_WS_MAC) // AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, // NewAEEventHandlerUPP(odocHandler),reinterpret_cast(&w),false); #endif // Q_WS_MAC - // Show main window - w.show(); - a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit())); - - // Open database if one was specified - if(fileToOpen.size()) - { - if(w.fileOpen(fileToOpen)) - { - // If database could be opened run the SQL scripts - foreach(const QString& f, sqlToExecute) - { - QFile file(f); - if(file.open(QIODevice::ReadOnly)) - { - w.getDb()->executeMultiSQL(file.readAll(), false, true); - file.close(); - } - } - if(!sqlToExecute.isEmpty()) - w.browseRefresh(); - } - } - // Quit application now if user doesn't want to see the UI - if(quitApplication) + if(a.dontShowMainWindow()) return 0; // Run application diff --git a/src/src.pro b/src/src.pro index 4190b977..63a9ae24 100644 --- a/src/src.pro +++ b/src/src.pro @@ -40,7 +40,8 @@ HEADERS += \ gen_version.h \ SqlExecutionArea.h \ VacuumDialog.h \ - DbStructureModel.h + DbStructureModel.h \ + Application.h SOURCES += \ sqlitedb.cpp \ @@ -62,7 +63,8 @@ SOURCES += \ FilterTableHeader.cpp \ SqlExecutionArea.cpp \ VacuumDialog.cpp \ - DbStructureModel.cpp + DbStructureModel.cpp \ + Application.cpp RESOURCES += icons/icons.qrc