+ move GuiApplication class into its own source files

This commit is contained in:
wmayer 2015-11-04 22:04:48 +01:00
parent e5f805a773
commit 899199daf5
4 changed files with 186 additions and 97 deletions

View File

@ -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<QWidget*>(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

View File

@ -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

126
src/Gui/GuiApplication.cpp Normal file
View File

@ -0,0 +1,126 @@
/***************************************************************************
* Copyright (c) 2015 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* 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 <sstream>
# include <stdexcept>
# include <QSessionManager>
#endif
#include "GuiApplication.h"
#include "SpaceballEvent.h"
#include "MainWindow.h"
#include <Base/Console.h>
#include <Base/Exception.h>
#include <Base/Interpreter.h>
#include <App/Application.h>
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<QWidget*>(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"

56
src/Gui/GuiApplication.h Normal file
View File

@ -0,0 +1,56 @@
/***************************************************************************
* Copyright (c) 2015 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* 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