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; delete mySplash;
QString plib = PLUGINS; QString plib = PLUGINS;
std::cout << "Plugins are installed in " << plib << std::endl; std::cout << "Plugins are installed in " << plib << std::endl;
// set the dirty flag to false -- no changes yet
projectIsDirty = false;
} }
QgisApp::~QgisApp() QgisApp::~QgisApp()
@ -352,6 +354,7 @@ void QgisApp::addLayer()
lyr->setRenderer(renderer); lyr->setRenderer(renderer);
renderer->initializeSymbology(lyr); renderer->initializeSymbology(lyr);
mapCanvas->addLayer(lyr); mapCanvas->addLayer(lyr);
projectIsDirty = true;
} else { } else {
QString msg = *it + " "; QString msg = *it + " ";
msg += tr("is not a valid or recognized data source"); msg += tr("is not a valid or recognized data source");
@ -447,6 +450,7 @@ QgisApp::addRasterLayer()
{ {
// add it to the mapcanvas collection // add it to the mapcanvas collection
mapCanvas->addLayer( layer ); mapCanvas->addLayer( layer );
projectIsDirty = true;
} else } else
{ {
QString msg( *it + " is not a valid or recognized raster data source" ); QString msg( *it + " is not a valid or recognized raster data source" );
@ -543,6 +547,7 @@ void QgisApp::addDatabaseLayer()
renderer->initializeSymbology(lyr); renderer->initializeSymbology(lyr);
// add it to the mapcanvas collection // add it to the mapcanvas collection
mapCanvas->addLayer(lyr); mapCanvas->addLayer(lyr);
projectIsDirty = true;
}else{ }else{
std::cerr << *it << " is an invalid layer - not loaded" << std::endl; std::cerr << *it << " is an invalid layer - not loaded" << std::endl;
QMessageBox::critical(this, tr("Invalid Layer"), QMessageBox::critical(this, tr("Invalid Layer"),
@ -579,24 +584,34 @@ void QgisApp::fileExit()
} }
void QgisApp::fileNew(){ void QgisApp::fileNew(){
mapCanvas->removeAll(); int answer= saveDirty();
setCaption(tr("Quantum GIS -- Untitled"));
mapCanvas->clear(); if(answer != QMessageBox::Cancel){
mapLegend->update(); mapCanvas->removeAll();
fullPath = ""; setCaption(tr("Quantum GIS -- Untitled"));
mapCanvas->clear();
mapLegend->update();
fullPath = "";
projectIsDirty = false;
}
} }
void QgisApp::fileOpen(){ void QgisApp::fileOpen(){
mapCanvas->freeze(true); int answer= saveDirty();
QgsProjectIo *pio = new QgsProjectIo(mapCanvas, QgsProjectIo::OPEN, this);
if(answer != QMessageBox::Cancel){
if(pio->read()){ mapCanvas->freeze(true);
setCaption(tr("Quantum GIS --") +" " + pio->baseName()); QgsProjectIo *pio = new QgsProjectIo(mapCanvas, QgsProjectIo::OPEN, this);
fullPath = pio->fullPathName();
} if(pio->read()){
delete pio; setCaption(tr("Quantum GIS --") +" " + pio->baseName());
fullPath = pio->fullPathName();
mapLegend->update(); }
mapCanvas->freeze(false); delete pio;
mapLegend->update();
mapCanvas->freeze(false);
projectIsDirty = false;
}
} }
void QgisApp::fileSave(){ void QgisApp::fileSave(){
QgsProjectIo *pio = new QgsProjectIo(mapCanvas, QgsProjectIo::SAVE); QgsProjectIo *pio = new QgsProjectIo(mapCanvas, QgsProjectIo::SAVE);
@ -606,6 +621,7 @@ void QgisApp::fileSave(){
statusBar()->message(tr("Saved map to:") +" " + pio->fullPathName()); statusBar()->message(tr("Saved map to:") +" " + pio->fullPathName());
} }
delete pio; delete pio;
projectIsDirty = false;
} }
void QgisApp::fileSaveAs(){ void QgisApp::fileSaveAs(){
@ -615,6 +631,7 @@ void QgisApp::fileSaveAs(){
statusBar()->message(tr("Saved map to:") + " " + pio->fullPathName()); statusBar()->message(tr("Saved map to:") + " " + pio->fullPathName());
} }
delete pio; delete pio;
projectIsDirty = false;
} }
void QgisApp::exportMapServer(){ void QgisApp::exportMapServer(){
@ -1401,6 +1418,7 @@ void QgisApp::addVectorLayer(QString vectorLayerPath, QString baseName, QString
renderer->initializeSymbology(lyr); renderer->initializeSymbology(lyr);
// add it to the mapcanvas collection // add it to the mapcanvas collection
mapCanvas->addLayer(lyr); mapCanvas->addLayer(lyr);
projectIsDirty = true;
//qWarning("incrementing iterator"); //qWarning("incrementing iterator");
/*! \todo Need legend scrollview and legenditem classes */ /*! \todo Need legend scrollview and legenditem classes */
mapLegend->update(); mapLegend->update();
@ -1416,3 +1434,19 @@ void QgisApp::addVectorLayer(QString vectorLayerPath, QString baseName, QString
mapCanvas->render2(); mapCanvas->render2();
QApplication::restoreOverrideCursor(); 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(); void about();
//! activates the selection tool //! activates the selection tool
void select(); void select();
//! check to see if file is dirty and if so, prompt the user th save it
int saveDirty();
private slots: private slots:
//! Slot to show the map coordinate position of the mouse cursor //! Slot to show the map coordinate position of the mouse cursor
void showMouseCoordinate(QgsPoint &); void showMouseCoordinate(QgsPoint &);
@ -167,6 +168,10 @@ class QgisApp:public QgisAppBase
QString versionMessage; QString versionMessage;
friend class QgisIface; friend class QgisIface;
QgsProviderRegistry *providerRegistry; 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 #endif

View File

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

View File

@ -99,6 +99,8 @@ class QgsMapCanvas:public QWidget
void removeAll(); void removeAll();
//! Flag the canvas as dirty and needed a refresh //! Flag the canvas as dirty and needed a refresh
void setDirty(bool _dirty); 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 //! Declare the legend class as a friend of the map canvas
friend class QgsLegend; friend class QgsLegend;
public slots: public slots:

View File

@ -12,7 +12,7 @@
* (at your option) any later version. * * (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 <iostream>
#include <fstream> #include <fstream>
#include <qfiledialog.h> #include <qfiledialog.h>
@ -481,10 +481,10 @@ if(action == SAVE && fullPath.isEmpty()){
} }
switch(action){ switch(action){
case OPEN: 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; break;
case SAVEAS: 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; break;
} }
return fullPath; return fullPath;