Skip to content

Commit

Permalink
Removed QgsIOException. From now if QgsProject's read() and write() f…
Browse files Browse the repository at this point in the history
…unctions return false, it's possible to find out the error message using error() function.

git-svn-id: http://svn.osgeo.org/qgis/trunk@12349 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed Dec 7, 2009
1 parent 0da6f38 commit 90eac5e
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 106 deletions.
14 changes: 14 additions & 0 deletions python/core/qgsproject.sip
Expand Up @@ -231,6 +231,20 @@ public:
@note added in 1.3 */
QString readPath( QString filename ) const;

/** Return error message from previous read/write
@note added in 1.4 */
QString error() const;

protected:

/** Set error message from read/write operation
@note added in 1.4 */
void setError( QString errorMessage );

/** Clear error message
@note added in 1.4 */
void clearError();

signals:

//! emitted when project is being read
Expand Down
106 changes: 30 additions & 76 deletions src/app/qgisapp.cpp
Expand Up @@ -3370,6 +3370,9 @@ void QgisApp::fileOpen()
{
if ( ! QgsProject::instance()->read() )
{
QMessageBox::critical( this,
tr( "QGIS Project Read Error" ),
QgsProject::instance()->error() );
mMapCanvas->freeze( false );
mMapCanvas->refresh();
return;
Expand All @@ -3389,16 +3392,6 @@ void QgisApp::fileOpen()
// Tell the legend to update the ordering
mMapLegend->readProject( e.document() );
}
catch ( std::exception & e )
{
QMessageBox::critical( this,
tr( "QGIS Project Read Error" ),
QString::fromLocal8Bit( e.what() ) );
QgsDebugMsg( "BAD QgsMapLayer::LayerType FOUND" );
mMapCanvas->freeze( false );
mMapCanvas->refresh();
return;
}

setTitleBarText_( *this );
emit projectRead(); // let plug-ins know that we've read in a new
Expand Down Expand Up @@ -3436,6 +3429,12 @@ bool QgisApp::addProject( QString projectFile )
{
if ( ! QgsProject::instance()->read( projectFile ) )
{
QMessageBox::critical( this,
tr( "Unable to open project" ),
QgsProject::instance()->error() );

QApplication::restoreOverrideCursor();

mMapCanvas->freeze( false );
mMapCanvas->refresh();
return false;
Expand Down Expand Up @@ -3468,20 +3467,6 @@ bool QgisApp::addProject( QString projectFile )
// Continue after last catch statement

}
catch ( std::exception & e )
{
QgsDebugMsg( "BAD QgsMapLayer::LayerType FOUND" );

QMessageBox::critical( this,
tr( "Unable to open project" ),
QString::fromLocal8Bit( e.what() ) );

QApplication::restoreOverrideCursor();

mMapCanvas->freeze( false );
mMapCanvas->refresh();
return false;
}

// Continue, now with layers found (hopefully)

Expand Down Expand Up @@ -3569,33 +3554,23 @@ bool QgisApp::fileSave()
QgsProject::instance()->setFileName( fullPath.filePath() );
}

try
if ( QgsProject::instance()->write() )
{
if ( QgsProject::instance()->write() )
{
setTitleBarText_( *this ); // update title bar
statusBar()->showMessage( tr( "Saved project to: %1" ).arg( QgsProject::instance()->fileName() ) );
setTitleBarText_( *this ); // update title bar
statusBar()->showMessage( tr( "Saved project to: %1" ).arg( QgsProject::instance()->fileName() ) );

if ( isNewProject )
{
// add this to the list of recently used project files
QSettings settings;
saveRecentProjectPath( fullPath.filePath(), settings );
}
}
else
if ( isNewProject )
{
QMessageBox::critical( this,
tr( "Unable to save project" ),
tr( "Unable to save project to %1" ).arg( QgsProject::instance()->fileName() ) );
return false;
// add this to the list of recently used project files
QSettings settings;
saveRecentProjectPath( fullPath.filePath(), settings );
}
}
catch ( std::exception & e )
else
{
QMessageBox::critical( this,
tr( "Unable to save project %1" ).arg( QgsProject::instance()->fileName() ),
QString::fromLocal8Bit( e.what() ) );
QgsProject::instance()->error() );
return false;
}
return true;
Expand Down Expand Up @@ -3628,29 +3603,20 @@ void QgisApp::fileSaveAs()
saveFilePath = myFI.filePath() + ".qgs";
}

try
{
QgsProject::instance()->setFileName( saveFilePath );
QgsProject::instance()->setFileName( saveFilePath );

if ( QgsProject::instance()->write() )
{
setTitleBarText_( *this ); // update title bar
statusBar()->showMessage( tr( "Saved project to: %1" ).arg( QgsProject::instance()->fileName() ) );
// add this to the list of recently used project files
saveRecentProjectPath( saveFilePath, settings );
}
else
{
QMessageBox::critical( this,
tr( "Unable to save project" ),
tr( "Unable to save project to %1" ).arg( QgsProject::instance()->fileName() ) );
}
if ( QgsProject::instance()->write() )
{
setTitleBarText_( *this ); // update title bar
statusBar()->showMessage( tr( "Saved project to: %1" ).arg( QgsProject::instance()->fileName() ) );
// add this to the list of recently used project files
saveRecentProjectPath( saveFilePath, settings );
}
catch ( std::exception & e )
else
{
QMessageBox::critical( 0x0,
QMessageBox::critical( this,
tr( "Unable to save project %1" ).arg( QgsProject::instance()->fileName() ),
QString::fromLocal8Bit( e.what() ),
QgsProject::instance()->error(),
QMessageBox::Ok,
Qt::NoButton );
}
Expand Down Expand Up @@ -3693,20 +3659,8 @@ void QgisApp::openProject( const QString & fileName )
// possibly save any pending work before opening a different project
if ( saveDirty() )
{
try
{
if ( ! addProject( fileName ) )
{
QgsDebugMsg( "unable to load project " + fileName );
}
}
catch ( QgsIOException & io_exception )
{
Q_UNUSED( io_exception );
QMessageBox::critical( this,
tr( "QGIS: Unable to load project" ),
tr( "Unable to load project %1" ).arg( fileName ) );
}
// error handling and reporting is in addProject() function
addProject( fileName );
}
return ;
}
Expand Down
20 changes: 0 additions & 20 deletions src/core/qgsexception.h
Expand Up @@ -56,26 +56,6 @@ class CORE_EXPORT QgsException : public std::exception
}; // class QgsException


/** for Qgis I/O related exceptions
@note usually thrown for opening file's that don't exist, and the like.
*/
class QgsIOException : public QgsException
{
public:

QgsIOException( std::string const & what )
: QgsException( what )
{}

QgsIOException( QString const & what )
: QgsException( what )
{}

}; // class QgsIOException



/** for files missing from layers while reading project files
Expand Down
42 changes: 33 additions & 9 deletions src/core/qgsproject.cpp
Expand Up @@ -747,6 +747,8 @@ bool QgsProject::read( QFileInfo const &file )
*/
bool QgsProject::read()
{
clearError();

std::auto_ptr< QDomDocument > doc =
std::auto_ptr < QDomDocument > ( new QDomDocument( "qgis" ) );

Expand All @@ -755,7 +757,8 @@ bool QgsProject::read()
imp_->file.close(); // even though we got an error, let's make
// sure it's closed anyway

throw QgsIOException( tr( "Unable to open %1" ).arg( imp_->file.fileName() ) );
setError( tr( "Unable to open %1" ).arg( imp_->file.fileName() ) );
return false;
}

// location of problem associated with errorMsg
Expand All @@ -776,7 +779,8 @@ bool QgsProject::read()

imp_->file.close();

throw QgsException( tr( "%1 for file %2" ).arg( errorString ).arg( imp_->file.fileName() ) );
setError( tr( "%1 for file %2" ).arg( errorString ).arg( imp_->file.fileName() ) );
return false;
}

imp_->file.close();
Expand Down Expand Up @@ -922,6 +926,8 @@ bool QgsProject::write( QFileInfo const &file )

bool QgsProject::write()
{
clearError();

// if we have problems creating or otherwise writing to the project file,
// let's find out up front before we go through all the hand-waving
// necessary to create all the Dom objects
Expand All @@ -930,16 +936,18 @@ bool QgsProject::write()
imp_->file.close(); // even though we got an error, let's make
// sure it's closed anyway

throw QgsIOException( tr( "Unable to save to file %1" ).arg( imp_->file.fileName() ) );
setError( tr( "Unable to save to file %1" ).arg( imp_->file.fileName() ) );
return false;
}
QFileInfo myFileInfo( imp_->file );
if ( !myFileInfo.isWritable() )
{
// even though we got an error, let's make
// sure it's closed anyway
imp_->file.close();
throw QgsIOException( tr( "%1 is not writeable. Please adjust permissions (if possible) and try again." )
.arg( imp_->file.fileName() ) );
setError( tr( "%1 is not writeable. Please adjust permissions (if possible) and try again." )
.arg( imp_->file.fileName() ) );
return false;
}

QDomImplementation DomImplementation;
Expand Down Expand Up @@ -1023,10 +1031,11 @@ bool QgsProject::write()
//
if ( projectFileStream.pos() == -1 || imp_->file.error() != QFile::NoError )
{
throw QgsIOException( tr( "Unable to save to file %1. Your project "
"may be corrupted on disk. Try clearing some space on the volume and "
"check file permissions before pressing save again." )
.arg( imp_->file.fileName() ) );
setError( tr( "Unable to save to file %1. Your project "
"may be corrupted on disk. Try clearing some space on the volume and "
"check file permissions before pressing save again." )
.arg( imp_->file.fileName() ) );
return false;
}

dirty( false ); // reset to pristine state
Expand Down Expand Up @@ -1456,3 +1465,18 @@ QString QgsProject::writePath( QString src ) const

return srcElems.join( "/" );
}

void QgsProject::setError( QString errorMessage )
{
mErrorMessage = errorMessage;
}

QString QgsProject::error() const
{
return mErrorMessage;
}

void QgsProject::clearError()
{
setError( QString() );
}
16 changes: 16 additions & 0 deletions src/core/qgsproject.h
Expand Up @@ -264,6 +264,20 @@ class CORE_EXPORT QgsProject : public QObject
@note added in 1.3 */
QString readPath( QString filename ) const;

/** Return error message from previous read/write
@note added in 1.4 */
QString error() const;

protected:

/** Set error message from read/write operation
@note added in 1.4 */
void setError( QString errorMessage );

/** Clear error message
@note added in 1.4 */
void clearError();

signals:

//! emitted when project is being read
Expand Down Expand Up @@ -295,6 +309,8 @@ class CORE_EXPORT QgsProject : public QObject

std::pair< bool, std::list<QDomNode> > _getMapLayers( QDomDocument const &doc );

QString mErrorMessage;

}; // QgsProject

#endif
2 changes: 1 addition & 1 deletion src/plugins/north_arrow/plugin.cpp
Expand Up @@ -325,7 +325,7 @@ bool QgsNorthArrowPlugin::calculateNorthDirection()
p1 = transform.transform( p1 );
p2 = transform.transform( p2 );
}
catch ( QgsException &e )
catch ( QgsCsException &e )
{
Q_UNUSED( e );
// just give up
Expand Down

0 comments on commit 90eac5e

Please sign in to comment.