Commit bff83e56 authored by Stéphane Albert's avatar Stéphane Albert

ENH: Added I18nMainWindow::ImportImage() method + factorized code with...

ENH: Added I18nMainWindow::ImportImage() method + factorized code with ::ImportDataset(); Added safe template QObjectCast() method (which throws std::bad_cast exception); Linked import image to Mv2 main-window.
parent 67c68e4c
......@@ -49,6 +49,7 @@
// #include "Core/mvdDatabaseModel.h"
// #include "Core/mvdDatasetModel.h"
#include "Core/mvdQuicklookModel.h"
#include "Core/mvdStackedLayerModel.h"
#include "Core/mvdVectorImageModel.h"
//
#include "Gui/mvdApplicationsToolBox.h"
......@@ -951,6 +952,22 @@ void
MainWindow
::ImportImage( const QString& filename, bool forceCreate )
{
VectorImageModel * imageModel = ImportImage( filename, -1, -1 );
assert( imageModel );
assert( Application::Instance() );
assert(
Application::Instance()->GetModel()==
Application::Instance()->GetModel< StackedLayerModel >()
);
StackedLayerModel* stackedLayerModel =
Application::Instance()->GetModel< StackedLayerModel >();
assert( stackedLayerModel!=NULL );
stackedLayerModel->Add( imageModel );
/*
DatasetModel* datasetModel =
ImportImage(
......
......@@ -137,7 +137,7 @@ protected:
*/
void ImportImage( const QString& filename, bool forceCreate );
using I18nMainWindow::ImportDataset;
using I18nMainWindow::ImportImage;
//
// QMainWindow overrides.
......
......@@ -187,9 +187,9 @@ AbstractModel
/*****************************************************************************/
template< typename T >
T*
T *
AbstractModel
::newChildModel( void* context )
::newChildModel( void * context )
{
T* model = new T( this );
......
......@@ -190,6 +190,46 @@ I18nCoreApplication
return hash;
}
/*****************************************************************************/
VectorImageModel *
I18nCoreApplication
::LoadImageModel( const QString & filename,
int width,
int height,
QObject * parent )
{
try
{
VectorImageModel::EnsureValidImage( filename );
}
catch( ... )
{
throw;
}
VectorImageModel * imageModel = NULL;
try
{
AbstractImageModel::BuildContext context( filename );
imageModel = new VectorImageModel( parent );
imageModel->SetFilename( filename, width, height );
imageModel->BuildModel( &context );
}
catch( ... )
{
delete imageModel;
imageModel = NULL;
throw;
}
return imageModel;
}
/*****************************************************************************/
DatasetModel*
I18nCoreApplication
......
......@@ -66,7 +66,7 @@ namespace mvd
// Internal classes pre-declaration.
class AbstractModel;
class DatasetModel;
class VectorImageModel;
/*****************************************************************************/
/* CLASS DEFINITION SECTION */
......@@ -285,6 +285,15 @@ public:
QString& name,
const QString& imageFilename );
/**
*/
static
VectorImageModel *
LoadImageModel( const QString & filename,
int width,
int height,
QObject * parent =NULL );
/**
* \brief Load a DatasetModel from disk.
*
......
......@@ -40,6 +40,7 @@
// Monteverdi includes (sorted by alphabetic order)
#include "mvdDatasetModel.h"
#include "mvdI18nCoreApplication.h"
#include "mvdVectorImageModel.h"
namespace mvd
{
......@@ -75,12 +76,28 @@ ImageImporter
QObject* parent ) :
AbstractWorker( parent ),
m_Filename( filename ),
m_ModelType( DATASET ),
m_Width( width ),
m_Height( height ),
m_IsForceCreateEnabled( isForceCreateEnabled )
{
}
/*******************************************************************************/
ImageImporter
::ImageImporter( const QString& filename,
int width,
int height,
QObject* parent ) :
AbstractWorker( parent ),
m_Filename( filename ),
m_ModelType( IMAGE ),
m_Width( width ),
m_Height( height ),
m_IsForceCreateEnabled( false )
{
}
/*******************************************************************************/
ImageImporter
::~ImageImporter()
......@@ -106,11 +123,29 @@ ImageImporter
emit ProgressRangeChanged( 0, 0 );
// Load dataset-model.
return
I18nCoreApplication::LoadDatasetModel(
m_Filename, m_Width, m_Height, m_IsForceCreateEnabled
);
// Load model.
switch( m_ModelType )
{
case DATASET:
return
I18nCoreApplication::LoadDatasetModel(
m_Filename, m_Width, m_Height, m_IsForceCreateEnabled
);
break;
case IMAGE:
return
I18nCoreApplication::LoadImageModel(
m_Filename, m_Width, m_Height
);
break;
default:
assert( false && "Unhandled ImageImporter::ModelType value." );
break;
}
return NULL;
}
/*******************************************************************************/
......@@ -118,9 +153,28 @@ QString
ImageImporter
::virtual_GetFirstProgressText() const
{
return
tr( "Importing image '%1' as dataset into cache directory..." )
.arg( QFileInfo( m_Filename ).fileName() );
switch( m_ModelType )
{
case DATASET:
return
tr( "Importing image '%1' as dataset into cache directory..." )
.arg( QFileInfo( m_Filename ).fileName()
);
break;
case IMAGE:
return
tr( "Loading image '%1'..." )
.arg( QFileInfo( m_Filename ).fileName()
);
break;
default:
assert( false && "Unhandled ImageImporter::ModelType value." );
break;
}
return QString();
}
/*******************************************************************************/
......
......@@ -84,7 +84,7 @@ class Monteverdi2_EXPORT ImageImporter :
public:
/**
* \brief Constructor.
* \brief Constructor (dataset-model).
*
* \param filename Filename of image to import.
* \param width Width of the best-fit size or -1 if none.
......@@ -97,6 +97,19 @@ public:
int height =-1,
QObject* parent =NULL );
/**
* \brief Constructor (image-model).
*
* \param filename Filename of image to import.
* \param width Width of the best-fit size or -1 if none.
* \param height Height of the best-fit size or -1 if none.
* \param isForceCreateEnabled true to force creation of dataset.
*/
ImageImporter( const QString& filename,
int width =-1,
int height =-1,
QObject* parent =NULL );
/**
* \brief Destructor.
*/
......@@ -140,6 +153,15 @@ protected:
/*-[ PRIVATE SECTION ]-----------------------------------------------------*/
//
// Private types.
private:
enum ModelType
{
DATASET,
IMAGE,
};
//
// Private methods.
private:
......@@ -158,6 +180,9 @@ private:
/**
*/
QString m_Filename;
/**
*/
ModelType m_ModelType;
/**
*/
int m_Width;
......
......@@ -36,6 +36,7 @@
//
// System includes (sorted by alphabetic order)
#include <exception>
//
// ITK includes (sorted by alphabetic order)
......@@ -313,6 +314,25 @@ typedef QPair< QString, QString > StringPairType;
*/
typedef QList< StringPairType > StringPairListType;
/*******************************************************************************/
template< typename T >
inline
T
QObjectCast( QObject * object, const QString & string = QString() )
{
T model = qobject_cast< T >( object );
if( model!=object )
{
if( !string.isNull() )
qWarning() << string;
throw std::bad_cast();
}
return model;
}
/*******************************************************************************/
} // end namespace 'mvd'
......
......@@ -39,6 +39,7 @@
#include "Core/mvdDatabaseConnection.h"
#include "Core/mvdDatasetModel.h"
#include "Core/mvdImageImporter.h"
#include "Core/mvdVectorImageModel.h"
#include "Gui/mvdAboutDialog.h"
#include "Gui/mvdI18nApplication.h"
#include "Gui/mvdTaskProgressDialog.h"
......@@ -166,17 +167,52 @@ I18nMainWindow
int height,
bool forceCreate )
{
//
// Background task.
return
QObjectCast< DatasetModel * >(
Import(
// New dataset-importer worker.
// It will be auto-deleted by background-task.
new ImageImporter(
filename,
forceCreate,
width, height
)
),
"QObject is not a DatasetModel."
);
}
// New image-importer worker.
// It will be auto-deleted by background-task.
ImageImporter* importer =
new ImageImporter(
filename,
forceCreate,
width, height
/*****************************************************************************/
VectorImageModel *
I18nMainWindow
::ImportImage( const QString& filename,
int width,
int height )
{
return
QObjectCast< VectorImageModel * >(
Import(
// New dataset-importer worker.
// It will be auto-deleted by background-task.
new ImageImporter(
filename,
width, height
)
),
"QObject is not a VectorImageModel."
);
}
/*****************************************************************************/
QObject *
I18nMainWindow
::Import( ImageImporter * importer )
{
assert( importer );
//
// Background task.
// New background-task running worker.
// Will be self auto-deleted when worker has finished.
......@@ -208,16 +244,16 @@ I18nMainWindow
// MANTIS-921 (then, process result).
if( button!=QDialog::Accepted )
{
assert( progress.GetObject< DatasetModel >()==NULL );
assert( progress.GetObject()==NULL );
return NULL;
}
// qDebug() << "datasetModel:" << progress.GetObject< DatasetModel >();
assert( progress.GetObject< DatasetModel >()!=NULL );
assert( progress.GetObject()!=NULL );
return progress.GetObject< DatasetModel >();
return progress.GetObject();
}
/*****************************************************************************/
......
......@@ -62,6 +62,8 @@ namespace mvd
// Internal class pre-declaration.
class AbstractModel;
class DatasetModel;
class ImageImporter;
class VectorImageModel;
/*****************************************************************************/
/* CLASS DEFINITION SECTION */
......@@ -266,7 +268,13 @@ protected:
int width,
int height,
bool forceCreate );
/**
*/
VectorImageModel *
ImportImage( const QString & filename,
int width,
int height );
//
// QMainWindow overrides.
......@@ -301,6 +309,9 @@ private:
//
// Private methods.
private:
/**
*/
QObject * Import( ImageImporter * );
/**
*/
......@@ -314,6 +325,9 @@ private:
*/
inline QMessageBox::StandardButton ConfirmSaveQuit( bool canBeCancelled );
/**
*/
//
// Private attributes.
private:
......
......@@ -101,6 +101,10 @@ public:
*/
int Exec();
/**
*/
inline QObject * GetObject() const;
/**
*/
template< typename T >
......@@ -174,6 +178,15 @@ private slots:
namespace mvd
{
/*****************************************************************************/
inline
QObject *
TaskProgressDialog
::GetObject() const
{
return m_Object;
}
/*****************************************************************************/
template< typename T >
inline
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment