Skip to content
Permalink
Browse files

QgsError for better error propagation to user, started using for rasters

  • Loading branch information
blazek committed Oct 24, 2012
1 parent b78456f commit e210a57101d264995bd19abe9c2f0521036f8da5
@@ -559,9 +559,26 @@ IF (GIT_MARKER)
COMMAND ${GITCOMMAND} log -n1 --pretty=%h OUTPUT_VARIABLE REVISION
)
STRING(STRIP "${REVISION}" REVISION)
# Get GIT remote and branch
EXECUTE_PROCESS(
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND ${GITCOMMAND} name-rev --name-only HEAD OUTPUT_VARIABLE GIT_LOCAL_BRANCH
)
STRING(STRIP "${GIT_LOCAL_BRANCH}" GIT_LOCAL_BRANCH)
EXECUTE_PROCESS(
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND ${GITCOMMAND} config branch.${GIT_LOCAL_BRANCH}.remote OUTPUT_VARIABLE GIT_REMOTE
)
STRING(STRIP "${GIT_REMOTE}" GIT_REMOTE)
EXECUTE_PROCESS(
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND ${GITCOMMAND} config remote.${GIT_REMOTE}.url OUTPUT_VARIABLE GIT_REMOTE_URL
)
STRING(STRIP "${GIT_REMOTE_URL}" GIT_REMOTE_URL)
ADD_CUSTOM_COMMAND(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/qgsversion.h
COMMAND echo \\\#define QGSVERSION \\\"${REVISION}\\\" >${CMAKE_CURRENT_BINARY_DIR}/qgsversion.h
COMMAND echo \\\#define QGS_GIT_REMOTE_URL \\\"${GIT_REMOTE_URL}\\\" >>${CMAKE_CURRENT_BINARY_DIR}/qgsversion.h
MAIN_DEPENDENCY ${GIT_MARKER}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
@@ -99,31 +99,6 @@ class QgsRasterDataProvider : QgsDataProvider, QgsRasterInterface
/* It makes no sense to set input on provider */
bool setInput( QgsRasterInterface* input );

/**
* Add the list of WMS layer names to be rendered by this server
*/
virtual void addLayers( const QStringList & layers,
const QStringList & styles = QStringList() ) = 0;

//! get raster image encodings supported by (e.g.) the WMS Server, expressed as MIME types
virtual QStringList supportedImageEncodings() = 0;

/**
* Get the image encoding (as a MIME type) used in the transfer from (e.g.) the WMS server
*/
virtual QString imageEncoding() const = 0;

/**
* Set the image encoding (as a MIME type) used in the transfer from (e.g.) the WMS server
*/
virtual void setImageEncoding( const QString & mimeType ) = 0;

/**
* Set the image projection (in WMS CRS format) used in the transfer from (e.g.) the WMS server
*/
virtual void setImageCrs( const QString & crs ) = 0;


// TODO: Document this better.
/** \brief Renders the layer as an image
*/
@@ -147,10 +147,6 @@ class QgsRasterLayer : QgsMapLayer
/** [ data provider interface ] Set the data provider */
void setDataProvider( const QString & provider );

static QLibrary* loadProviderLibrary( QString theProviderKey );
static QgsRasterDataProvider* loadProvider( QString theProviderKey, QString theDataSource = 0 );


/** \brief Accessor for blue band name mapping */
QString blueBandName() const;

@@ -120,6 +120,8 @@
#include "qgsdecorationscalebar.h"
#include "qgsdecorationgrid.h"
#include "qgsencodingfiledialog.h"
#include "qgserror.h"
#include "qgserrordialog.h"
#include "qgsexception.h"
#include "qgsfeature.h"
#include "qgsformannotationitem.h"
@@ -7294,6 +7296,14 @@ QgsRasterLayer* QgisApp::addRasterLayer( QString const & rasterFile, QString con
QgsRasterLayer *layer =
new QgsRasterLayer( rasterFile, baseName ); // fi.completeBaseName());

if ( !layer->isValid() )
{
//QMessageBox::critical( this, tr( "Invalid Layer" ), layer->error().message() );
QgsErrorDialog::show( layer->error(), tr( "Invalid Layer" ) );
delete layer;
return NULL;
}

bool ok = false;

if ( shouldAskUserForGDALSublayers( layer ) )
@@ -7377,16 +7387,15 @@ QgsRasterLayer* QgisApp::addRasterLayer(

QgsDebugMsg( "Constructed new layer." );

if ( layer && layer->isValid() )
if ( layer->isValid() )
{
addRasterLayer( layer );

statusBar()->showMessage( mMapCanvas->extent().toString( 2 ) );
}
else
{
QMessageBox::critical( this, tr( "Layer is not valid" ),
tr( "The layer is not a valid layer and can not be added to the map" ) );
QgsErrorDialog::show( layer->error(), tr( "Invalid Layer" ) );
}

// update UI
@@ -7454,25 +7463,37 @@ bool QgisApp::addRasterLayers( QStringList const &theFileNameQStringList, bool g
// create the layer
QgsRasterLayer *layer = new QgsRasterLayer( *myIterator, myBaseNameQString );

if ( shouldAskUserForGDALSublayers( layer ) )
if ( !layer->isValid() )
{
askUserForGDALSublayers( layer );

// The first layer loaded is not useful in that case. The user can select it in
// the list if he wants to load it.
delete layer;
returnValue = false;
if ( guiWarning )
{
QgsErrorDialog::show( layer->error(), tr( "Invalid Layer" ) );
delete layer;
}
}
else
{
addRasterLayer( layer );

//only allow one copy of a ai grid file to be loaded at a
//time to prevent the user selecting all adfs in 1 dir which
//actually represent 1 coverate,
if ( shouldAskUserForGDALSublayers( layer ) )
{
askUserForGDALSublayers( layer );

if ( myBaseNameQString.toLower().endsWith( ".adf" ) )
// The first layer loaded is not useful in that case. The user can select it in
// the list if he wants to load it.
delete layer;
}
else
{
break;
addRasterLayer( layer );

//only allow one copy of a ai grid file to be loaded at a
//time to prevent the user selecting all adfs in 1 dir which
//actually represent 1 coverate,

if ( myBaseNameQString.toLower().endsWith( ".adf" ) )
{
break;
}
}
}
} // valid raster filename
@@ -63,6 +63,7 @@ SET(QGIS_CORE_SRCS
qgsdbfilterproxymodel.cpp
qgsdiagramrendererv2.cpp
qgsdistancearea.cpp
qgserror.cpp
qgsexpression.cpp
qgsfeature.cpp
qgsfield.cpp
@@ -350,6 +351,7 @@ SET(QGIS_CORE_HDRS
qgsdataitem.h
qgsdistancearea.h
qgscsexception.h
qgserror.h
qgsexception.h
qgsexpression.h
qgsfeature.h
@@ -22,6 +22,7 @@
#include <QStringList>

//#include "qgsdataitem.h"
#include "qgserror.h"

class QgsRectangle;
class QgsCoordinateReferenceSystem;
@@ -290,6 +291,12 @@ class CORE_EXPORT QgsDataProvider : public QObject
/** Current time stamp of data source */
virtual QDateTime dataTimestamp() const { return QDateTime(); }

/** Get current status error. This error describes some principal problem
* for which provider cannot work and thus is not valid. It is not last error
* after accessing data by block(), identify() etc.
*/
virtual QgsError error() const { return mError; }

signals:

/**
@@ -318,6 +325,16 @@ class CORE_EXPORT QgsDataProvider : public QObject
* Timestamp of data in the moment when the data were loaded by provider.
*/
QDateTime mTimestamp;

/** \brief Error */
QgsError mError;

/** Add error message */
void appendError( const QgsErrorMessage & theMessage ) { mError.append( theMessage );}

/** Set error message */
void setError( const QgsError & theError ) { mError = theError;}

private:

/**
@@ -0,0 +1,111 @@
/***************************************************************************
qgserror.cpp - Error container
-------------------
begin : October 2012
copyright : (C) 2012 Radim Blazek
email : radim dot blazek at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgis.h"
#include "qgsversion.h"
#include "qgsconfig.h"
#include "qgserror.h"
#include "qgslogger.h"

#include <QRegExp>

QgsErrorMessage::QgsErrorMessage ( const QString & theMessage, const QString & theTag, const QString & theFile, const QString & theFunction, int theLine )
: mMessage(theMessage)
, mTag(theTag)
, mFile(theFile)
, mFunction(theFunction)
, mLine(theLine)
, mFormat(Text)
{
}

QgsError::QgsError ( const QString & theMessage, const QString & theTag )
{
append ( theMessage, theTag );
}

void QgsError::append ( const QString & theMessage, const QString & theTag )
{
mMessageList.append ( QgsErrorMessage ( theMessage, theTag ) );
}

void QgsError::append ( const QgsErrorMessage & theMessage )
{
mMessageList.append ( theMessage );
}

QString QgsError::message ( QgsErrorMessage::Format theFormat ) const
{
QString str;
int sPrefixLength = strlen( CMAKE_SOURCE_DIR ) + 1;

QString srcUrl;
#if defined(QGISDEBUG) && defined(QGS_GIT_REMOTE_URL)
// TODO: verify if we are not ahead to origin (remote hash does not exist)
// and there are no local not commited changes
QString hash = QString(QGis::QGIS_DEV_VERSION);
QString remote = QString(QGS_GIT_REMOTE_URL);
QgsDebugMsg ("remote = " + remote );
if ( !hash.isEmpty () && !remote.isEmpty() && remote.contains ( "github.com" ) )
{
QString path = remote.remove( QRegExp(".*github.com[:/]" )).remove(".git");
srcUrl = "https://github.com/" + path + "/blob/" + hash;
}
#endif

foreach ( QgsErrorMessage m, mMessageList )
{
#ifdef QGISDEBUG
QString file;
#ifndef _MSC_VER
file = m.file().mid(sPrefixLength);
#else
file = m.file();
#endif
#endif

if ( theFormat == QgsErrorMessage::Text )
{
str += m.tag() + " " + m.message();
#ifdef QGISDEBUG
str += QString( "\nat %1 : %2 : %3" ).arg( file ).arg( m.line() ).arg( m.function() );
#endif
}
else // QgsErrorMessage::Html
{
str += "<p><b>" + m.tag() + ":</b> " + m.message();
#ifdef QGISDEBUG
QString location = QString( "%1 : %2 : %3" ).arg( file ).arg( m.line() ).arg( m.function() );
if ( !srcUrl.isEmpty() )
{
QString url = QString("%1/%2#L%3").arg(srcUrl).arg(file).arg( m.line() );
str += QString( "<br>(<a href='%1'>%2</a>)" ).arg(url).arg( location );
}
else
{
str += QString( "<br>(%1)" ).arg( location );
}
#endif
}
}
return str;
}

QString QgsError::summary ( ) const
{
// The first message in chain is usually the real error given by backend/server
return mMessageList.first().message();
}

0 comments on commit e210a57

Please sign in to comment.
You can’t perform that action at this time.