From 143add5d1b64c11df9a98270e89c1c99205ae8c7 Mon Sep 17 00:00:00 2001 From: wmayer Date: Wed, 4 Nov 2015 22:04:48 +0100 Subject: [PATCH] + move GuiApplication class into its own source files --- src/Gui/Application.cpp | 98 +---------------------------- src/Gui/CMakeLists.txt | 3 + src/Gui/GuiApplication.cpp | 126 +++++++++++++++++++++++++++++++++++++ src/Gui/GuiApplication.h | 56 +++++++++++++++++ 4 files changed, 186 insertions(+), 97 deletions(-) create mode 100644 src/Gui/GuiApplication.cpp create mode 100644 src/Gui/GuiApplication.h diff --git a/src/Gui/Application.cpp b/src/Gui/Application.cpp index 772d41d45d..fb6f881dc8 100644 --- a/src/Gui/Application.cpp +++ b/src/Gui/Application.cpp @@ -63,7 +63,7 @@ #include "Application.h" #include "AutoSaver.h" -#include "GuiApplicationNativeEventAware.h" +#include "GuiApplication.h" #include "MainWindow.h" #include "Document.h" #include "View.h" @@ -1536,102 +1536,6 @@ void Application::initTypes(void) Gui::PythonWorkbench ::init(); } -namespace Gui { -/** Override QCoreApplication::notify() to fetch exceptions in Qt widgets - * properly that are not handled in the event handler or slot. - */ -class GUIApplication : public GUIApplicationNativeEventAware -{ - int systemExit; -public: - GUIApplication(int & argc, char ** argv, int exitcode) - : GUIApplicationNativeEventAware(argc, argv), systemExit(exitcode) - { - } - - /** - * Make forwarding events exception-safe and get more detailed information - * where an unhandled exception comes from. - */ - bool notify (QObject * receiver, QEvent * event) - { - if (!receiver && event) { - Base::Console().Log("GUIApplication::notify: Unexpected null receiver, event type: %d\n", - (int)event->type()); - } - try { - if (event->type() == Spaceball::ButtonEvent::ButtonEventType || - event->type() == Spaceball::MotionEvent::MotionEventType) - return processSpaceballEvent(receiver, event); - else - return QApplication::notify(receiver, event); - } - catch (const Base::SystemExitException&) { - qApp->exit(systemExit); - return true; - } - catch (const Base::Exception& e) { - Base::Console().Error("Unhandled Base::Exception caught in GUIApplication::notify.\n" - "The error message is: %s\n", e.what()); - } - catch (const std::exception& e) { - Base::Console().Error("Unhandled std::exception caught in GUIApplication::notify.\n" - "The error message is: %s\n", e.what()); - } - catch (...) { - Base::Console().Error("Unhandled unknown exception caught in GUIApplication::notify.\n"); - } - - // Print some more information to the log file (if active) to ease bug fixing - if (receiver && event) { - try { - std::stringstream dump; - dump << "The event type " << (int)event->type() << " was sent to " - << receiver->metaObject()->className() << "\n"; - dump << "Object tree:\n"; - if (receiver->isWidgetType()) { - QWidget* w = qobject_cast(receiver); - while (w) { - dump << "\t"; - dump << w->metaObject()->className(); - QString name = w->objectName(); - if (!name.isEmpty()) - dump << " (" << (const char*)name.toUtf8() << ")"; - w = w->parentWidget(); - if (w) - dump << " is child of\n"; - } - std::string str = dump.str(); - Base::Console().Log("%s",str.c_str()); - } - } - catch (...) { - Base::Console().Log("Invalid recipient and/or event in GUIApplication::notify\n"); - } - } - - return true; - } - void commitData(QSessionManager &manager) - { - if (manager.allowsInteraction()) { - if (!Gui::getMainWindow()->close()) { - // cancel the shutdown - manager.release(); - manager.cancel(); - } - } - else { - // no user interaction allowed, thus close all documents and - // the main window - App::GetApplication().closeAllDocuments(); - Gui::getMainWindow()->close(); - } - - } -}; -} - void Application::runApplication(void) { // A new QApplication diff --git a/src/Gui/CMakeLists.txt b/src/Gui/CMakeLists.txt index 5bc7ba0564..ea5638607a 100644 --- a/src/Gui/CMakeLists.txt +++ b/src/Gui/CMakeLists.txt @@ -210,6 +210,7 @@ set(Gui_MOC_HDRS Flag.h GraphicsViewZoom.h GraphvizView.h + GuiApplication.h GuiApplicationNativeEventAware.h HelpView.h InputVector.h @@ -978,6 +979,7 @@ SET(FreeCADGui_CPP_SRCS ExpressionBinding.cpp GraphicsViewZoom.cpp ExpressionCompleter.cpp + GuiApplication.cpp GuiApplicationNativeEventAware.cpp GuiConsole.cpp Macro.cpp @@ -1001,6 +1003,7 @@ SET(FreeCADGui_SRCS ExpressionCompleter.h FreeCADGuiInit.py GraphicsViewZoom.h + GuiApplication.h GuiApplicationNativeEventAware.h GuiConsole.h InventorAll.h diff --git a/src/Gui/GuiApplication.cpp b/src/Gui/GuiApplication.cpp new file mode 100644 index 0000000000..2ab1ea4088 --- /dev/null +++ b/src/Gui/GuiApplication.cpp @@ -0,0 +1,126 @@ +/*************************************************************************** + * Copyright (c) 2015 Werner Mayer * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307, USA * + * * + ***************************************************************************/ + + +#include "PreCompiled.h" + +#ifndef _PreComp_ +# include +# include +# include +#endif + +#include "GuiApplication.h" +#include "SpaceballEvent.h" +#include "MainWindow.h" + +#include +#include +#include + +#include + +using namespace Gui; + +GUIApplication::GUIApplication(int & argc, char ** argv, int exitcode) + : GUIApplicationNativeEventAware(argc, argv), systemExit(exitcode) +{ +} + +bool GUIApplication::notify (QObject * receiver, QEvent * event) +{ + if (!receiver && event) { + Base::Console().Log("GUIApplication::notify: Unexpected null receiver, event type: %d\n", + (int)event->type()); + } + try { + if (event->type() == Spaceball::ButtonEvent::ButtonEventType || + event->type() == Spaceball::MotionEvent::MotionEventType) + return processSpaceballEvent(receiver, event); + else + return QApplication::notify(receiver, event); + } + catch (const Base::SystemExitException&) { + qApp->exit(systemExit); + return true; + } + catch (const Base::Exception& e) { + Base::Console().Error("Unhandled Base::Exception caught in GUIApplication::notify.\n" + "The error message is: %s\n", e.what()); + } + catch (const std::exception& e) { + Base::Console().Error("Unhandled std::exception caught in GUIApplication::notify.\n" + "The error message is: %s\n", e.what()); + } + catch (...) { + Base::Console().Error("Unhandled unknown exception caught in GUIApplication::notify.\n"); + } + + // Print some more information to the log file (if active) to ease bug fixing + if (receiver && event) { + try { + std::stringstream dump; + dump << "The event type " << (int)event->type() << " was sent to " + << receiver->metaObject()->className() << "\n"; + dump << "Object tree:\n"; + if (receiver->isWidgetType()) { + QWidget* w = qobject_cast(receiver); + while (w) { + dump << "\t"; + dump << w->metaObject()->className(); + QString name = w->objectName(); + if (!name.isEmpty()) + dump << " (" << (const char*)name.toUtf8() << ")"; + w = w->parentWidget(); + if (w) + dump << " is child of\n"; + } + std::string str = dump.str(); + Base::Console().Log("%s",str.c_str()); + } + } + catch (...) { + Base::Console().Log("Invalid recipient and/or event in GUIApplication::notify\n"); + } + } + + return true; +} + +void GUIApplication::commitData(QSessionManager &manager) +{ + if (manager.allowsInteraction()) { + if (!Gui::getMainWindow()->close()) { + // cancel the shutdown + manager.release(); + manager.cancel(); + } + } + else { + // no user interaction allowed, thus close all documents and + // the main window + App::GetApplication().closeAllDocuments(); + Gui::getMainWindow()->close(); + } +} + +#include "moc_GuiApplication.cpp" diff --git a/src/Gui/GuiApplication.h b/src/Gui/GuiApplication.h new file mode 100644 index 0000000000..29bb91445d --- /dev/null +++ b/src/Gui/GuiApplication.h @@ -0,0 +1,56 @@ +/*************************************************************************** + * Copyright (c) 2015 Werner Mayer * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307, USA * + * * + ***************************************************************************/ + + +#ifndef GUI_APPLICATION_H +#define GUI_APPLICATION_H + +#include "GuiApplicationNativeEventAware.h" + +class QSessionManager; + +namespace Gui +{ +/** Override QCoreApplication::notify() to fetch exceptions in Qt widgets + * properly that are not handled in the event handler or slot. + */ +class GUIApplication : public GUIApplicationNativeEventAware +{ + Q_OBJECT + +public: + GUIApplication(int & argc, char ** argv, int exitcode); + + /** + * Make forwarding events exception-safe and get more detailed information + * where an unhandled exception comes from. + */ + bool notify (QObject * receiver, QEvent * event); + void commitData(QSessionManager &manager); + +private: + int systemExit; +}; + +} + +#endif // GUI_APPLICATION_H