Skip to content

Commit

Permalink
ticket:4164 Subclass QApplication to handle QFileOpenEvent for OSX.
Browse files Browse the repository at this point in the history
  • Loading branch information
adeas31 committed Feb 16, 2017
1 parent e814eb1 commit 36812d4
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 105 deletions.
161 changes: 161 additions & 0 deletions OMEdit/OMEditGUI/OMEditApplication.cpp
@@ -0,0 +1,161 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE
* OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3, ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
/*
* @author Adeel Asghar <adeel.asghar@liu.se>
*/

#include "OMEditApplication.h"
#include "Util/Utilities.h"
#include "Util/Helper.h"
#include "MainWindow.h"
#include "Modeling/LibraryTreeWidget.h"

#include <QMessageBox>

OMEditApplication::OMEditApplication(int &argc, char **argv)
: QApplication(argc, argv)
{
// set the stylesheet
setStyleSheet("file:///:/Resources/css/stylesheet.qss");
#if !(QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
QTextCodec::setCodecForTr(QTextCodec::codecForName(Helper::utf8.toLatin1().data()));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName(Helper::utf8.toLatin1().data()));
#endif
#ifndef WIN32
QTextCodec::setCodecForLocale(QTextCodec::codecForName(Helper::utf8.toLatin1().data()));
#endif
setAttribute(Qt::AA_DontShowIconsInMenus, false);
// Localization
//*a.severin/ add localization
const char *omhome = getenv("OPENMODELICAHOME");
#ifdef WIN32
if (!omhome) {
QMessageBox::critical(0, QString(Helper::applicationName).append(" - ").append(Helper::error),
GUIMessages::getMessage(GUIMessages::OPENMODELICAHOME_NOT_FOUND), Helper::ok);
quit();
exit(1);
}
#else /* unix */
omhome = omhome ? omhome : CONFIG_DEFAULT_OPENMODELICAHOME;
#endif
QSettings *pSettings = Utilities::getApplicationSettings();
QLocale settingsLocale = QLocale(pSettings->value("language").toString());
settingsLocale = settingsLocale.name() == "C" ? pSettings->value("language").toLocale() : settingsLocale;
QString locale = settingsLocale.name().isEmpty() ? QLocale::system().name() : settingsLocale.name();
/* set the default locale of the application so that QSpinBox etc show values according to the locale. */
QLocale::setDefault(settingsLocale);

QString translationDirectory = omhome + QString("/share/omedit/nls");
// install Qt's default translations
QTranslator qtTranslator;
#ifdef Q_OS_WIN
qtTranslator.load("qt_" + locale, translationDirectory);
#else
qtTranslator.load("qt_" + locale, QLibraryInfo::location(QLibraryInfo::TranslationsPath));
#endif
installTranslator(&qtTranslator);
// install application translations
QTranslator translator;
translator.load("OMEdit_" + locale, translationDirectory);
installTranslator(&translator);
// Splash Screen
QPixmap pixmap(":/Resources/icons/omedit_splashscreen.png");
SplashScreen *pSplashScreen = SplashScreen::instance();
pSplashScreen->setPixmap(pixmap);
pSplashScreen->show();
Helper::initHelperVariables();
/* Force C-style doubles */
setlocale(LC_NUMERIC, "C");
// if user has requested to open the file by passing it in argument then,
bool debug = false;
QString fileName = "";
QStringList fileNames;
if (arguments().size() > 1) {
for (int i = 1; i < arguments().size(); i++) {
if (strncmp(arguments().at(i).toStdString().c_str(), "--Debug=",8) == 0) {
QString debugArg = arguments().at(i);
debugArg.remove("--Debug=");
if (0 == strcmp("true", debugArg.toStdString().c_str())) {
debug = true;
} else {
debug = false;
}
} else {
fileName = arguments().at(i);
if (!fileName.isEmpty()) {
// if path is relative make it absolute
QFileInfo file (fileName);
QString absoluteFileName = fileName;
if (file.isRelative()) {
absoluteFileName = QString("%1/%2").arg(QDir::currentPath()).arg(fileName);
}
absoluteFileName = absoluteFileName.replace("\\", "/");
if (QFile::exists(absoluteFileName)) {
fileNames << absoluteFileName;
} else {
printf("Invalid command line argument: %s %s\n", fileName.toStdString().c_str(), absoluteFileName.toStdString().c_str());
}
}
}
}
}
// MainWindow Initialization
MainWindow *pMainwindow = MainWindow::instance(debug);
pMainwindow->setUpMainWindow();
if (pMainwindow->getExitApplicationStatus()) { // if there is some issue in running the application.
quit();
exit(1);
}
// open the files passed as command line arguments
foreach (QString fileName, fileNames) {
pMainwindow->getLibraryWidget()->openFile(fileName);
}
// finally show the main window
pMainwindow->show();
// hide the splash screen
pSplashScreen->finish(pMainwindow);
}

bool OMEditApplication::event(QEvent *pEvent)
{
switch (pEvent->type()) {
case QEvent::FileOpen: {
QFileOpenEvent *pFileOpenEvent = static_cast<QFileOpenEvent*>(pEvent);
if (pFileOpenEvent) {
MainWindow::instance()->getLibraryWidget()->openFile(pFileOpenEvent->file());
return true;
}
break;
}
default:
break;
}
return QApplication::event(pEvent);
}
47 changes: 47 additions & 0 deletions OMEdit/OMEditGUI/OMEditApplication.h
@@ -0,0 +1,47 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE
* OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3, ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
/*
* @author Adeel Asghar <adeel.asghar@liu.se>
*/

#ifndef OMEDITAPPLICATION_H
#define OMEDITAPPLICATION_H

#include <QApplication>

class OMEditApplication : public QApplication
{
public:
OMEditApplication(int& argc, char**argv);
protected:
virtual bool event(QEvent *pEvent);
};

#endif // OMEDITAPPLICATION_H
6 changes: 4 additions & 2 deletions OMEdit/OMEditGUI/OMEditGUI.pro
Expand Up @@ -184,7 +184,8 @@ SOURCES += main.cpp \
Git/RevertCommitsDialog.cpp \
Git/CleanDialog.cpp \
Traceability/TraceabilityPushDialog.cpp \
Traceability/TraceabilityQueryDialog.cpp
Traceability/TraceabilityQueryDialog.cpp \
OMEditApplication.cpp

HEADERS += Util/Helper.h \
Util/Utilities.h \
Expand Down Expand Up @@ -258,7 +259,8 @@ HEADERS += Util/Helper.h \
Git/RevertCommitsDialog.h \
Git/CleanDialog.h \
Traceability/TraceabilityPushDialog.h \
Traceability/TraceabilityQueryDialog.h
Traceability/TraceabilityQueryDialog.h \
OMEditApplication.h

CONFIG(osg) {

Expand Down
105 changes: 2 additions & 103 deletions OMEdit/OMEditGUI/main.cpp
Expand Up @@ -57,10 +57,8 @@

#include <locale.h>

#include "MainWindow.h"
#include "Util/Helper.h"
#include "OMEditApplication.h"
#include "CrashReport/CrashReportDialog.h"
#include "Modeling/LibraryTreeWidget.h"
#include "meta/meta_modelica.h"

#ifndef WIN32
Expand Down Expand Up @@ -175,105 +173,6 @@ int main(int argc, char *argv[])
}
}
Q_INIT_RESOURCE(resource_omedit);
QApplication a(argc, argv);
// set the stylesheet
a.setStyleSheet("file:///:/Resources/css/stylesheet.qss");
#if !(QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
QTextCodec::setCodecForTr(QTextCodec::codecForName(Helper::utf8.toLatin1().data()));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName(Helper::utf8.toLatin1().data()));
#endif
#ifndef WIN32
QTextCodec::setCodecForLocale(QTextCodec::codecForName(Helper::utf8.toLatin1().data()));
#endif
a.setAttribute(Qt::AA_DontShowIconsInMenus, false);
// Localization
//*a.severin/ add localization
const char *omhome = getenv("OPENMODELICAHOME");
#ifdef WIN32
if (!omhome) {
QMessageBox::critical(0, QString(Helper::applicationName).append(" - ").append(Helper::error),
GUIMessages::getMessage(GUIMessages::OPENMODELICAHOME_NOT_FOUND), Helper::ok);
a.quit();
exit(1);
}
#else /* unix */
omhome = omhome ? omhome : CONFIG_DEFAULT_OPENMODELICAHOME;
#endif
QSettings *pSettings = Utilities::getApplicationSettings();
QLocale settingsLocale = QLocale(pSettings->value("language").toString());
settingsLocale = settingsLocale.name() == "C" ? pSettings->value("language").toLocale() : settingsLocale;
QString locale = settingsLocale.name().isEmpty() ? QLocale::system().name() : settingsLocale.name();
/* set the default locale of the application so that QSpinBox etc show values according to the locale. */
QLocale::setDefault(settingsLocale);

QString translationDirectory = omhome + QString("/share/omedit/nls");
// install Qt's default translations
QTranslator qtTranslator;
#ifdef Q_OS_WIN
qtTranslator.load("qt_" + locale, translationDirectory);
#else
qtTranslator.load("qt_" + locale, QLibraryInfo::location(QLibraryInfo::TranslationsPath));
#endif
a.installTranslator(&qtTranslator);
// install application translations
QTranslator translator;
translator.load("OMEdit_" + locale, translationDirectory);
a.installTranslator(&translator);
// Splash Screen
QPixmap pixmap(":/Resources/icons/omedit_splashscreen.png");
SplashScreen *pSplashScreen = SplashScreen::instance();
pSplashScreen->setPixmap(pixmap);
pSplashScreen->show();
Helper::initHelperVariables();
/* Force C-style doubles */
setlocale(LC_NUMERIC, "C");
// if user has requested to open the file by passing it in argument then,
bool debug = false;
QString fileName = "";
QStringList fileNames;
if (a.arguments().size() > 1) {
for (int i = 1; i < a.arguments().size(); i++) {
if (strncmp(a.arguments().at(i).toStdString().c_str(), "--Debug=",8) == 0) {
QString debugArg = a.arguments().at(i);
debugArg.remove("--Debug=");
if (0 == strcmp("true", debugArg.toStdString().c_str())) {
debug = true;
} else {
debug = false;
}
} else {
fileName = a.arguments().at(i);
if (!fileName.isEmpty()) {
// if path is relative make it absolute
QFileInfo file (fileName);
QString absoluteFileName = fileName;
if (file.isRelative()) {
absoluteFileName = QString("%1/%2").arg(QDir::currentPath()).arg(fileName);
}
absoluteFileName = absoluteFileName.replace("\\", "/");
if (QFile::exists(absoluteFileName)) {
fileNames << absoluteFileName;
} else {
printf("Invalid command line argument: %s %s\n", fileName.toStdString().c_str(), absoluteFileName.toStdString().c_str());
}
}
}
}
}
// MainWindow Initialization
MainWindow *pMainwindow = MainWindow::instance(debug);
pMainwindow->setUpMainWindow();
if (pMainwindow->getExitApplicationStatus()) { // if there is some issue in running the application.
a.quit();
exit(1);
}
// open the files passed as command line arguments
foreach (QString fileName, fileNames) {
pMainwindow->getLibraryWidget()->openFile(fileName);
}
// finally show the main window
pMainwindow->show();
// hide the splash screen
pSplashScreen->finish(pMainwindow);
OMEditApplication a(argc, argv);
return a.exec();
}

0 comments on commit 36812d4

Please sign in to comment.