Commit d148cfc0 authored by Mario Hlawitschka's avatar Mario Hlawitschka
Browse files

[CHANGE] added initial session handling for closing requests by the OS

parent 625a9651
#include <QtGui/QtGui>
#include "WMainWindow.h"
#include "WApplication.h"
WApplication::WApplication( int argc, char** argv, bool GUIenabled )
: QApplication( argc, argv, GUIenabled ),
mainWindow( 0 )
{
}
void WApplication::setMainWindow( WMainWindow* window )
{
mainWindow = window;
}
void WApplication::commitData( QSessionManager& manager )
{
if ( manager.allowsInteraction() )
{
int ret = QMessageBox::warning(
mainWindow,
tr( "OpenWalnut" ),
tr( "Save changes?" ),
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel );
switch ( ret )
{
case QMessageBox::Save:
manager.release();
mainWindow->projectSaveAll();
break;
case QMessageBox::Discard:
break;
case QMessageBox::Cancel:
default:
manager.cancel();
}
}
else
{
// no interaction allowed, what should we do?
}
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WAPPLICATION_H
#define WAPPLICATION_H
#include <QtGui/QApplication>
class WMainWindow;
/**
* Overloaded base class for our application that has initial handling of
* session data
*/
class WApplication : public QApplication
{
public:
/** default constructor, see QApplication */
WApplication( int argc, char** argv, bool GUIenabled = true );
/** stores the main window used as parent for dialogs */
void setMainWindow( WMainWindow* window );
/** manage save dialogs when the session manager asks us to
* take care of our data */
virtual void commitData( QSessionManager& manager );
protected:
WMainWindow* mainWindow;
};
#endif // WAPPLICATION_H
......@@ -566,7 +566,7 @@ WQtNetworkEditor* WMainWindow::getNetworkEditor()
return m_networkEditor;
}
void WMainWindow::projectSave( const std::vector< boost::shared_ptr< WProjectFileIO > >& writer )
bool WMainWindow::projectSave( const std::vector< boost::shared_ptr< WProjectFileIO > >& writer )
{
QFileDialog fd;
fd.setWindowTitle( "Save Project as" );
......@@ -582,13 +582,18 @@ void WMainWindow::projectSave( const std::vector< boost::shared_ptr< WProjectFil
{
fileNames = fd.selectedFiles();
}
else
{
return false; // the user canceled, no files, so nothing saved
}
bool success = true;
QStringList::const_iterator constIterator;
for( constIterator = fileNames.constBegin(); constIterator != fileNames.constEnd(); ++constIterator )
{
std::string filename = ( *constIterator ).toStdString();
// append owp if not existent
if( filename.rfind( ".owp" ) == std::string::npos )
// append owp if suffix is not present, yet
if( filename.rfind( ".owp" ) != filename.size() - 4 )
{
filename += ".owp";
}
......@@ -615,36 +620,38 @@ void WMainWindow::projectSave( const std::vector< boost::shared_ptr< WProjectFil
QString message = "<b>Problem while saving project file.</b><br/><br/><b>File: </b>" + ( *constIterator ) +
"<br/><b>Message: </b>" + QString::fromStdString( e.what() );
QMessageBox::critical( this, title, message );
success = false;
}
}
return success;
}
void WMainWindow::projectSaveAll()
bool WMainWindow::projectSaveAll()
{
std::vector< boost::shared_ptr< WProjectFileIO > > w;
// an empty list equals "all"
projectSave( w );
return projectSave( w );
}
void WMainWindow::projectSaveCameraOnly()
bool WMainWindow::projectSaveCameraOnly()
{
std::vector< boost::shared_ptr< WProjectFileIO > > w;
w.push_back( WProjectFile::getCameraWriter() );
projectSave( w );
return projectSave( w );
}
void WMainWindow::projectSaveROIOnly()
bool WMainWindow::projectSaveROIOnly()
{
std::vector< boost::shared_ptr< WProjectFileIO > > w;
w.push_back( WProjectFile::getROIWriter() );
projectSave( w );
return projectSave( w );
}
void WMainWindow::projectSaveModuleOnly()
bool WMainWindow::projectSaveModuleOnly()
{
std::vector< boost::shared_ptr< WProjectFileIO > > w;
w.push_back( WProjectFile::getModuleWriter() );
projectSave( w );
return projectSave( w );
}
void WMainWindow::projectLoad()
......
......@@ -209,7 +209,7 @@ protected:
*
* \param writer the list of writers to use.
*/
virtual void projectSave( const std::vector< boost::shared_ptr< WProjectFileIO > >& writer );
virtual bool projectSave( const std::vector< boost::shared_ptr< WProjectFileIO > >& writer );
public slots:
/**
......@@ -280,22 +280,22 @@ public slots:
/**
* Gets called whenever the user presses the project save button.
*/
void projectSaveAll();
bool projectSaveAll();
/**
* Gets called by the save menu to only save the camera settings
*/
void projectSaveCameraOnly();
bool projectSaveCameraOnly();
/**
* Gets called by the save menu to only save the ROI settings
*/
void projectSaveROIOnly();
bool projectSaveROIOnly();
/**
* Gets called by the save menu to only save the Module settings
*/
void projectSaveModuleOnly();
bool projectSaveModuleOnly();
/**
* Is able to handle updates in the log-level setting.
......
......@@ -40,6 +40,7 @@
#include <QtCore/QSettings>
#include "WMainWindow.h" // this has to be included before any other includes
#include "WApplication.h"
#include "core/common/WConditionOneShot.h"
#include "core/common/WIOTools.h"
#include "core/common/WPathHelper.h"
......@@ -142,7 +143,7 @@ int WQt4Gui::run()
m_loggerConnection = WLogger::getLogger()->subscribeSignal( WLogger::AddLog, boost::bind( &WQt4Gui::slotAddLog, this, _1 ) );
// make qapp instance before using the applicationDirPath() function
QApplication appl( m_argc, m_argv, true );
WApplication appl( m_argc, m_argv, true );
// the call path of the application, this uses QApplication which needs to be instantiated.
boost::filesystem::path walnutBin = boost::filesystem::path( QApplication::applicationDirPath().toStdString() );
......@@ -211,6 +212,7 @@ int WQt4Gui::run()
m_mainWindow = new WMainWindow();
m_mainWindow->setupGUI();
m_mainWindow->show();
appl.setMainWindow( m_mainWindow );
// connect out loader signal with kernel
#ifdef _WIN32
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment