logic for tracking state of the project/map canvas so user can be prompted to save prior to exiting, closing, opening, or creating a new project file

git-svn-id: http://svn.osgeo.org/qgis/trunk@621 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
gsherman 2004-01-28 05:55:27 +00:00
parent df14f2695e
commit 4a546ca8a6
5 changed files with 71 additions and 20 deletions

View File

@ -284,6 +284,8 @@ QgisApp::QgisApp(QWidget * parent, const char *name, WFlags fl):QgisAppBase(pare
delete mySplash;
QString plib = PLUGINS;
std::cout << "Plugins are installed in " << plib << std::endl;
// set the dirty flag to false -- no changes yet
projectIsDirty = false;
}
QgisApp::~QgisApp()
@ -352,6 +354,7 @@ void QgisApp::addLayer()
lyr->setRenderer(renderer);
renderer->initializeSymbology(lyr);
mapCanvas->addLayer(lyr);
projectIsDirty = true;
} else {
QString msg = *it + " ";
msg += tr("is not a valid or recognized data source");
@ -447,6 +450,7 @@ QgisApp::addRasterLayer()
{
// add it to the mapcanvas collection
mapCanvas->addLayer( layer );
projectIsDirty = true;
} else
{
QString msg( *it + " is not a valid or recognized raster data source" );
@ -543,6 +547,7 @@ void QgisApp::addDatabaseLayer()
renderer->initializeSymbology(lyr);
// add it to the mapcanvas collection
mapCanvas->addLayer(lyr);
projectIsDirty = true;
}else{
std::cerr << *it << " is an invalid layer - not loaded" << std::endl;
QMessageBox::critical(this, tr("Invalid Layer"),
@ -579,24 +584,34 @@ void QgisApp::fileExit()
}
void QgisApp::fileNew(){
mapCanvas->removeAll();
setCaption(tr("Quantum GIS -- Untitled"));
mapCanvas->clear();
mapLegend->update();
fullPath = "";
int answer= saveDirty();
if(answer != QMessageBox::Cancel){
mapCanvas->removeAll();
setCaption(tr("Quantum GIS -- Untitled"));
mapCanvas->clear();
mapLegend->update();
fullPath = "";
projectIsDirty = false;
}
}
void QgisApp::fileOpen(){
mapCanvas->freeze(true);
QgsProjectIo *pio = new QgsProjectIo(mapCanvas, QgsProjectIo::OPEN, this);
if(pio->read()){
setCaption(tr("Quantum GIS --") +" " + pio->baseName());
fullPath = pio->fullPathName();
}
delete pio;
mapLegend->update();
mapCanvas->freeze(false);
int answer= saveDirty();
if(answer != QMessageBox::Cancel){
mapCanvas->freeze(true);
QgsProjectIo *pio = new QgsProjectIo(mapCanvas, QgsProjectIo::OPEN, this);
if(pio->read()){
setCaption(tr("Quantum GIS --") +" " + pio->baseName());
fullPath = pio->fullPathName();
}
delete pio;
mapLegend->update();
mapCanvas->freeze(false);
projectIsDirty = false;
}
}
void QgisApp::fileSave(){
QgsProjectIo *pio = new QgsProjectIo(mapCanvas, QgsProjectIo::SAVE);
@ -606,6 +621,7 @@ void QgisApp::fileSave(){
statusBar()->message(tr("Saved map to:") +" " + pio->fullPathName());
}
delete pio;
projectIsDirty = false;
}
void QgisApp::fileSaveAs(){
@ -615,6 +631,7 @@ void QgisApp::fileSaveAs(){
statusBar()->message(tr("Saved map to:") + " " + pio->fullPathName());
}
delete pio;
projectIsDirty = false;
}
void QgisApp::exportMapServer(){
@ -1401,6 +1418,7 @@ void QgisApp::addVectorLayer(QString vectorLayerPath, QString baseName, QString
renderer->initializeSymbology(lyr);
// add it to the mapcanvas collection
mapCanvas->addLayer(lyr);
projectIsDirty = true;
//qWarning("incrementing iterator");
/*! \todo Need legend scrollview and legenditem classes */
mapLegend->update();
@ -1416,3 +1434,19 @@ void QgisApp::addVectorLayer(QString vectorLayerPath, QString baseName, QString
mapCanvas->render2();
QApplication::restoreOverrideCursor();
}
int QgisApp::saveDirty(){
int answer = 0;
mapCanvas->freeze(true);
if((projectIsDirty || mapCanvas->isDirty()) && mapCanvas->layerCount() > 0){
// prompt user to save
answer = QMessageBox::information(this, "Save?","Do you want to save the current project?",
QMessageBox::Yes | QMessageBox::Default,
QMessageBox::No,
QMessageBox::Cancel | QMessageBox::Escape);
if(answer == QMessageBox::Yes){
fileSave();
}
}
mapCanvas->freeze(false);
return answer;
}

View File

@ -93,7 +93,8 @@ class QgisApp:public QgisAppBase
void about();
//! activates the selection tool
void select();
//! check to see if file is dirty and if so, prompt the user th save it
int saveDirty();
private slots:
//! Slot to show the map coordinate position of the mouse cursor
void showMouseCoordinate(QgsPoint &);
@ -167,6 +168,10 @@ class QgisApp:public QgisAppBase
QString versionMessage;
friend class QgisIface;
QgsProviderRegistry *providerRegistry;
/** Flag to track whether the user should be prompted to save the project
* before opening/creating a new one or exiting the application
*/
bool projectIsDirty;
};
#endif

View File

@ -72,6 +72,9 @@ QgsLegend * QgsMapCanvas::getLegend(){
void QgsMapCanvas::setDirty(bool _dirty){
dirty = _dirty;
}
bool QgsMapCanvas::isDirty(){
return dirty;
}
void QgsMapCanvas::addLayer(QgsMapLayerInterface * lyr){
// add a maplayer interface to a layer type defined in a plugin
@ -312,6 +315,7 @@ void QgsMapCanvas::zoomFullExtent()
currentExtent = fullExtent;
clear();
render2();
dirty = true;
}
void QgsMapCanvas::zoomPreviousExtent()
@ -322,6 +326,7 @@ void QgsMapCanvas::zoomPreviousExtent()
previousExtent = tempRect;
clear();
render2();
dirty = true;
}
}
@ -365,6 +370,7 @@ void QgsMapCanvas::zoomToSelected()
return;
}
}
dirty = true;
}
void QgsMapCanvas::mousePressEvent(QMouseEvent * e)
@ -413,6 +419,7 @@ void QgsMapCanvas::mouseReleaseEvent(QMouseEvent * e)
currentExtent.normalize();
clear();
render2();
dirty = true;
break;
case QGis::ZoomOut:
{
@ -454,6 +461,7 @@ void QgsMapCanvas::mouseReleaseEvent(QMouseEvent * e)
std::cout << "Center of currentExtent after scaling is " << currentExtent.center() << std::endl;
clear();
render2();
dirty = true;
}
break;
@ -485,6 +493,7 @@ void QgsMapCanvas::mouseReleaseEvent(QMouseEvent * e)
}
clear();
render2();
dirty = true;
}
break;
@ -700,6 +709,7 @@ void QgsMapCanvas::remove(QString key)
layers = newLayers;
delete l;
zOrder.remove(key);
dirty = true;
}
void QgsMapCanvas::removeAll(){
layers.clear();

View File

@ -99,6 +99,8 @@ class QgsMapCanvas:public QWidget
void removeAll();
//! Flag the canvas as dirty and needed a refresh
void setDirty(bool _dirty);
//! Return the state of the canvas (dirty or not)
bool isDirty();
//! Declare the legend class as a friend of the map canvas
friend class QgsLegend;
public slots:

View File

@ -12,7 +12,7 @@
* (at your option) any later version. *
* *
***************************************************************************/
/* qgsprojectio.cpp,v 1.20 2004/01/27 07:56:13 mhugent Exp */
/* qgsprojectio.cpp,v 1.21 2004/01/28 05:55:27 gsherman Exp */
#include <iostream>
#include <fstream>
#include <qfiledialog.h>
@ -481,10 +481,10 @@ if(action == SAVE && fullPath.isEmpty()){
}
switch(action){
case OPEN:
fullPath = QFileDialog::getOpenFileName("./", QObject::tr("QGis files (*.qgs)"), 0, 0, QObject::tr("Choose a file to open") );
fullPath = QFileDialog::getOpenFileName("./", QObject::tr("QGis files (*.qgs)"), 0, 0, QObject::tr("Choose a QGIS project file to open") );
break;
case SAVEAS:
fullPath = QFileDialog::getSaveFileName("./", QObject::tr("QGis files (*.qgs)"), 0, 0, QObject::tr("Choose a filename to save") );
fullPath = QFileDialog::getSaveFileName("./", QObject::tr("QGis files (*.qgs)"), 0, 0, QObject::tr("Choose a filename to save") );
break;
}
return fullPath;