Skip to content

Commit a85618f

Browse files
author
wonder
committed
Removed QgsIOException. From now if QgsProject's read() and write() functions return false, it's possible to find out the error message using error() function.
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@12349 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 3a7d217 commit a85618f

File tree

6 files changed

+94
-106
lines changed

6 files changed

+94
-106
lines changed

python/core/qgsproject.sip

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,20 @@ public:
231231
@note added in 1.3 */
232232
QString readPath( QString filename ) const;
233233

234+
/** Return error message from previous read/write
235+
@note added in 1.4 */
236+
QString error() const;
237+
238+
protected:
239+
240+
/** Set error message from read/write operation
241+
@note added in 1.4 */
242+
void setError( QString errorMessage );
243+
244+
/** Clear error message
245+
@note added in 1.4 */
246+
void clearError();
247+
234248
signals:
235249

236250
//! emitted when project is being read

src/app/qgisapp.cpp

Lines changed: 30 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3370,6 +3370,9 @@ void QgisApp::fileOpen()
33703370
{
33713371
if ( ! QgsProject::instance()->read() )
33723372
{
3373+
QMessageBox::critical( this,
3374+
tr( "QGIS Project Read Error" ),
3375+
QgsProject::instance()->error() );
33733376
mMapCanvas->freeze( false );
33743377
mMapCanvas->refresh();
33753378
return;
@@ -3389,16 +3392,6 @@ void QgisApp::fileOpen()
33893392
// Tell the legend to update the ordering
33903393
mMapLegend->readProject( e.document() );
33913394
}
3392-
catch ( std::exception & e )
3393-
{
3394-
QMessageBox::critical( this,
3395-
tr( "QGIS Project Read Error" ),
3396-
QString::fromLocal8Bit( e.what() ) );
3397-
QgsDebugMsg( "BAD QgsMapLayer::LayerType FOUND" );
3398-
mMapCanvas->freeze( false );
3399-
mMapCanvas->refresh();
3400-
return;
3401-
}
34023395

34033396
setTitleBarText_( *this );
34043397
emit projectRead(); // let plug-ins know that we've read in a new
@@ -3436,6 +3429,12 @@ bool QgisApp::addProject( QString projectFile )
34363429
{
34373430
if ( ! QgsProject::instance()->read( projectFile ) )
34383431
{
3432+
QMessageBox::critical( this,
3433+
tr( "Unable to open project" ),
3434+
QgsProject::instance()->error() );
3435+
3436+
QApplication::restoreOverrideCursor();
3437+
34393438
mMapCanvas->freeze( false );
34403439
mMapCanvas->refresh();
34413440
return false;
@@ -3468,20 +3467,6 @@ bool QgisApp::addProject( QString projectFile )
34683467
// Continue after last catch statement
34693468

34703469
}
3471-
catch ( std::exception & e )
3472-
{
3473-
QgsDebugMsg( "BAD QgsMapLayer::LayerType FOUND" );
3474-
3475-
QMessageBox::critical( this,
3476-
tr( "Unable to open project" ),
3477-
QString::fromLocal8Bit( e.what() ) );
3478-
3479-
QApplication::restoreOverrideCursor();
3480-
3481-
mMapCanvas->freeze( false );
3482-
mMapCanvas->refresh();
3483-
return false;
3484-
}
34853470

34863471
// Continue, now with layers found (hopefully)
34873472

@@ -3569,33 +3554,23 @@ bool QgisApp::fileSave()
35693554
QgsProject::instance()->setFileName( fullPath.filePath() );
35703555
}
35713556

3572-
try
3557+
if ( QgsProject::instance()->write() )
35733558
{
3574-
if ( QgsProject::instance()->write() )
3575-
{
3576-
setTitleBarText_( *this ); // update title bar
3577-
statusBar()->showMessage( tr( "Saved project to: %1" ).arg( QgsProject::instance()->fileName() ) );
3559+
setTitleBarText_( *this ); // update title bar
3560+
statusBar()->showMessage( tr( "Saved project to: %1" ).arg( QgsProject::instance()->fileName() ) );
35783561

3579-
if ( isNewProject )
3580-
{
3581-
// add this to the list of recently used project files
3582-
QSettings settings;
3583-
saveRecentProjectPath( fullPath.filePath(), settings );
3584-
}
3585-
}
3586-
else
3562+
if ( isNewProject )
35873563
{
3588-
QMessageBox::critical( this,
3589-
tr( "Unable to save project" ),
3590-
tr( "Unable to save project to %1" ).arg( QgsProject::instance()->fileName() ) );
3591-
return false;
3564+
// add this to the list of recently used project files
3565+
QSettings settings;
3566+
saveRecentProjectPath( fullPath.filePath(), settings );
35923567
}
35933568
}
3594-
catch ( std::exception & e )
3569+
else
35953570
{
35963571
QMessageBox::critical( this,
35973572
tr( "Unable to save project %1" ).arg( QgsProject::instance()->fileName() ),
3598-
QString::fromLocal8Bit( e.what() ) );
3573+
QgsProject::instance()->error() );
35993574
return false;
36003575
}
36013576
return true;
@@ -3628,29 +3603,20 @@ void QgisApp::fileSaveAs()
36283603
saveFilePath = myFI.filePath() + ".qgs";
36293604
}
36303605

3631-
try
3632-
{
3633-
QgsProject::instance()->setFileName( saveFilePath );
3606+
QgsProject::instance()->setFileName( saveFilePath );
36343607

3635-
if ( QgsProject::instance()->write() )
3636-
{
3637-
setTitleBarText_( *this ); // update title bar
3638-
statusBar()->showMessage( tr( "Saved project to: %1" ).arg( QgsProject::instance()->fileName() ) );
3639-
// add this to the list of recently used project files
3640-
saveRecentProjectPath( saveFilePath, settings );
3641-
}
3642-
else
3643-
{
3644-
QMessageBox::critical( this,
3645-
tr( "Unable to save project" ),
3646-
tr( "Unable to save project to %1" ).arg( QgsProject::instance()->fileName() ) );
3647-
}
3608+
if ( QgsProject::instance()->write() )
3609+
{
3610+
setTitleBarText_( *this ); // update title bar
3611+
statusBar()->showMessage( tr( "Saved project to: %1" ).arg( QgsProject::instance()->fileName() ) );
3612+
// add this to the list of recently used project files
3613+
saveRecentProjectPath( saveFilePath, settings );
36483614
}
3649-
catch ( std::exception & e )
3615+
else
36503616
{
3651-
QMessageBox::critical( 0x0,
3617+
QMessageBox::critical( this,
36523618
tr( "Unable to save project %1" ).arg( QgsProject::instance()->fileName() ),
3653-
QString::fromLocal8Bit( e.what() ),
3619+
QgsProject::instance()->error(),
36543620
QMessageBox::Ok,
36553621
Qt::NoButton );
36563622
}
@@ -3693,20 +3659,8 @@ void QgisApp::openProject( const QString & fileName )
36933659
// possibly save any pending work before opening a different project
36943660
if ( saveDirty() )
36953661
{
3696-
try
3697-
{
3698-
if ( ! addProject( fileName ) )
3699-
{
3700-
QgsDebugMsg( "unable to load project " + fileName );
3701-
}
3702-
}
3703-
catch ( QgsIOException & io_exception )
3704-
{
3705-
Q_UNUSED( io_exception );
3706-
QMessageBox::critical( this,
3707-
tr( "QGIS: Unable to load project" ),
3708-
tr( "Unable to load project %1" ).arg( fileName ) );
3709-
}
3662+
// error handling and reporting is in addProject() function
3663+
addProject( fileName );
37103664
}
37113665
return ;
37123666
}

src/core/qgsexception.h

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,26 +56,6 @@ class CORE_EXPORT QgsException : public std::exception
5656
}; // class QgsException
5757

5858

59-
/** for Qgis I/O related exceptions
60-
61-
@note usually thrown for opening file's that don't exist, and the like.
62-
63-
*/
64-
class QgsIOException : public QgsException
65-
{
66-
public:
67-
68-
QgsIOException( std::string const & what )
69-
: QgsException( what )
70-
{}
71-
72-
QgsIOException( QString const & what )
73-
: QgsException( what )
74-
{}
75-
76-
}; // class QgsIOException
77-
78-
7959

8060
/** for files missing from layers while reading project files
8161

src/core/qgsproject.cpp

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,8 @@ bool QgsProject::read( QFileInfo const &file )
747747
*/
748748
bool QgsProject::read()
749749
{
750+
clearError();
751+
750752
std::auto_ptr< QDomDocument > doc =
751753
std::auto_ptr < QDomDocument > ( new QDomDocument( "qgis" ) );
752754

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

758-
throw QgsIOException( tr( "Unable to open %1" ).arg( imp_->file.fileName() ) );
760+
setError( tr( "Unable to open %1" ).arg( imp_->file.fileName() ) );
761+
return false;
759762
}
760763

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

777780
imp_->file.close();
778781

779-
throw QgsException( tr( "%1 for file %2" ).arg( errorString ).arg( imp_->file.fileName() ) );
782+
setError( tr( "%1 for file %2" ).arg( errorString ).arg( imp_->file.fileName() ) );
783+
return false;
780784
}
781785

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

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

933-
throw QgsIOException( tr( "Unable to save to file %1" ).arg( imp_->file.fileName() ) );
939+
setError( tr( "Unable to save to file %1" ).arg( imp_->file.fileName() ) );
940+
return false;
934941
}
935942
QFileInfo myFileInfo( imp_->file );
936943
if ( !myFileInfo.isWritable() )
937944
{
938945
// even though we got an error, let's make
939946
// sure it's closed anyway
940947
imp_->file.close();
941-
throw QgsIOException( tr( "%1 is not writeable. Please adjust permissions (if possible) and try again." )
942-
.arg( imp_->file.fileName() ) );
948+
setError( tr( "%1 is not writeable. Please adjust permissions (if possible) and try again." )
949+
.arg( imp_->file.fileName() ) );
950+
return false;
943951
}
944952

945953
QDomImplementation DomImplementation;
@@ -1023,10 +1031,11 @@ bool QgsProject::write()
10231031
//
10241032
if ( projectFileStream.pos() == -1 || imp_->file.error() != QFile::NoError )
10251033
{
1026-
throw QgsIOException( tr( "Unable to save to file %1. Your project "
1027-
"may be corrupted on disk. Try clearing some space on the volume and "
1028-
"check file permissions before pressing save again." )
1029-
.arg( imp_->file.fileName() ) );
1034+
setError( tr( "Unable to save to file %1. Your project "
1035+
"may be corrupted on disk. Try clearing some space on the volume and "
1036+
"check file permissions before pressing save again." )
1037+
.arg( imp_->file.fileName() ) );
1038+
return false;
10301039
}
10311040

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

14571466
return srcElems.join( "/" );
14581467
}
1468+
1469+
void QgsProject::setError( QString errorMessage )
1470+
{
1471+
mErrorMessage = errorMessage;
1472+
}
1473+
1474+
QString QgsProject::error() const
1475+
{
1476+
return mErrorMessage;
1477+
}
1478+
1479+
void QgsProject::clearError()
1480+
{
1481+
setError( QString() );
1482+
}

src/core/qgsproject.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,20 @@ class CORE_EXPORT QgsProject : public QObject
264264
@note added in 1.3 */
265265
QString readPath( QString filename ) const;
266266

267+
/** Return error message from previous read/write
268+
@note added in 1.4 */
269+
QString error() const;
270+
271+
protected:
272+
273+
/** Set error message from read/write operation
274+
@note added in 1.4 */
275+
void setError( QString errorMessage );
276+
277+
/** Clear error message
278+
@note added in 1.4 */
279+
void clearError();
280+
267281
signals:
268282

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

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

312+
QString mErrorMessage;
313+
298314
}; // QgsProject
299315

300316
#endif

src/plugins/north_arrow/plugin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ bool QgsNorthArrowPlugin::calculateNorthDirection()
325325
p1 = transform.transform( p1 );
326326
p2 = transform.transform( p2 );
327327
}
328-
catch ( QgsException &e )
328+
catch ( QgsCsException &e )
329329
{
330330
Q_UNUSED( e );
331331
// just give up

0 commit comments

Comments
 (0)