mvdDatasetModel.cxx 7.65 KB
Newer Older
1
2
3
4
5
6
7
/*=========================================================================

  Program:   Monteverdi2
  Language:  C++


  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
8
  See Copyright.txt for details.
9

10
11
12
  Monteverdi2 is distributed under the CeCILL licence version 2. See
  Licence_CeCILL_V2-en.txt or
  http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt for more details.
13

14
15
16
  This software is distributed WITHOUT ANY WARRANTY; without even
  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  PURPOSE.  See the above copyright notices for more information.
17
18
19
20
21

=========================================================================*/

#include "mvdDatasetModel.h"

22
23
24
25

/*****************************************************************************/
/* INCLUDE SECTION                                                           */

26
//
27
28
// Qt includes (sorted by alphabetic order)
//// Must be included before system/custom includes.
29
30

//
31
// System includes (sorted by alphabetic order)
32
33
34
35
36
37
38
39
40

//
// ITK includes (sorted by alphabetic order)

//
// OTB includes (sorted by alphabetic order)

//
// Monteverdi includes (sorted by alphabetic order)
41
#include "mvdDatasetDescriptor.h"
42
#include "mvdI18nApplication.h"
43
#include "mvdQuicklookModel.h"
44
#include "mvdSystemError.h"
45
#include "mvdVectorImageModel.h"
46
47
48
49
50
51
52
53
54
55
56

namespace mvd
{
/*
  TRANSLATOR mvd::DatasetModel

  Necessary for lupdate to be aware of C++ namespaces.

  Context comment for translator.
*/

57
58
59
60
61
62
63
64
65
66
67
68
69

/*****************************************************************************/
/* CONSTANTS                                                                 */

const char* DatasetModel::DESCRIPTOR_FILENAME = "descriptor.xml";

/*****************************************************************************/
/* STATIC IMPLEMENTATION SECTION                                             */


/*****************************************************************************/
/* CLASS IMPLEMENTATION SECTION                                              */

70
71
72
/*******************************************************************************/
DatasetModel
::DatasetModel( QObject* parent ) :
73
74
75
76
77
  AbstractModel( parent ),
  m_Descriptor( NULL ),
  m_Path(),
  m_Name(),
  m_Directory()
78
79
80
81
82
83
84
85
86
{
}

/*******************************************************************************/
DatasetModel
::~DatasetModel()
{
}

87
/*******************************************************************************/
88
#if 0
89
90
91
92
bool
DatasetModel
::SetContent( const QString& path, const QString& name )
{
93
  return true;
94
}
95
#endif
96

97
98
99
/*******************************************************************************/
void
DatasetModel
100
101
102
103
104
105
106
107
108
::ImportImage( const QString& filename , int width, int height )
{
  LoadImage( filename, true, width, height );
}

/*******************************************************************************/
void
DatasetModel
::LoadImage( const QString& filename, bool foo, int width, int height )
109
110
{
  // 1. Instanciate local image model.
111
  VectorImageModel* vectorImageModel = new VectorImageModel( this );
112
113
114
115

  // 2. Safely load data from file.
  try
    {
116
    //
117
    // 2.1. Information.
118
    vectorImageModel->SetFilename( filename, width, height );
119

120
    //
121
    // 2.2. Generate cached data.
Stéphane Albert's avatar
STYLE    
Stéphane Albert committed
122
123
    // TODO: generate image-model cached data (quicklook,
    // histogram-list etc.)
124
    vectorImageModel->BuildModel();
125
126
    assert( vectorImageModel->GetQuicklookModel()!=NULL );

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
    // If first time, import image into descriptor.
    if( foo )
      {
      //
      // 2.3: Add image to Dataset descriptor file.
      m_Descriptor->InsertImageModel(
	GetImageModels().size(),
	vectorImageModel->GetFilename(),
	&vectorImageModel->GetSettings(),
	vectorImageModel->GetQuicklookModel()->GetFilename()
      );

      //
      // 2.4: Force writing descriptor with newly imported image.
      WriteDescriptor();
      }
143
144
145
146
147
148
149
150
151
152
153
154
    }
  catch( std::exception& exc )
    {
    // Release local memory allocation.
    delete vectorImageModel;
    vectorImageModel = NULL;

    // Forward exception to upper level (GUI).
    throw;
    }
}

155
156
157
/*******************************************************************************/
void
DatasetModel
158
159
::virtual_BuildModel( void* context )
{
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
  // Get build-context.
  assert( context!=NULL );
  BuildContext* buildContext = static_cast< BuildContext* >( context );

  // Create directory structure, if needed.
  bool isEmpty = I18nApplication::MakeDirTree(
    buildContext->m_Path,
    buildContext->m_Name
  );

  // Access working directory.
  QDir workingDir( buildContext->m_Path );
  if( !workingDir.cd( buildContext->m_Name ) )
    throw SystemError(
      ToStdString(
	QString( "('%1')" ).arg( workingDir.filePath( buildContext->m_Name ) ) )
    );    

  // Remember access to directory structure.
  m_Path = buildContext->m_Path;
  m_Name = buildContext->m_Name;
  m_Directory = workingDir;

183
184
185
186
187
188
  // Initialize content.
  if( isEmpty )
    {
    // Create in-memory descriptor.
    assert( m_Descriptor==NULL );
    m_Descriptor = newChildModel< DatasetDescriptor >();
189

190
191
    // Force writing in-memory descriptor into disk in order to have a
    // consistent empty dataset directory structure.
192
    WriteDescriptor();
193
194
195
196
197
198
199
200
201
202
    }
  else
    {
    // Create context with filename.
    QFileInfo finfo( m_Directory, DatasetModel::DESCRIPTOR_FILENAME );
    DatasetDescriptor::BuildContext context( finfo.filePath() );

    // Relink to on-disk descriptor.
    assert( m_Descriptor==NULL );
    m_Descriptor = newChildModel< DatasetDescriptor >( &context );
203

204
    // Load image-models from descriptor.
Mickael Savinaud's avatar
Mickael Savinaud committed
205
    ParseDescriptor(buildContext);
206
    }
207
208
209
210
211
}

/*******************************************************************************/
void
DatasetModel
Mickael Savinaud's avatar
Mickael Savinaud committed
212
::ParseDescriptor(BuildContext* bContext )
213
{
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
  for( QDomElement imageElt( m_Descriptor->FirstImageElement() );
       !imageElt.isNull();
       imageElt = DatasetDescriptor::NextImageSiblingElement( imageElt ) )
    {
    int id = -1;
    QString filename;
    QString quicklook;
    VectorImageModel::Settings settings;

    DatasetDescriptor::GetImageModel(
      imageElt,
      id, filename, &settings, quicklook
    );

    qDebug()
      << "Input image:"
      << "\nid: " << id
      << "\nfilename: " << filename
      << "\nquicklook: " << quicklook;

    // TODO: 1) Re-use quicklook filename.
    // TODO: 2) Assign rendering-settings.
    // TODO: 3) Remove WxH for screen best-fit during loading of model!
Mickael Savinaud's avatar
Mickael Savinaud committed
237
    LoadImage( filename, false, bContext->m_Width, bContext->m_Height );
238
    }
239
240
}

241
242
243
244
245
246
247
/*******************************************************************************/
void
DatasetModel
::WriteDescriptor() const
{
  QFileInfo finfo( m_Directory, DatasetModel::DESCRIPTOR_FILENAME );

248
  assert( m_Descriptor!=NULL );
249
250
251
  m_Descriptor->Write( finfo.filePath() );
}

252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*******************************************************************************/
bool
DatasetModel
::IsModified() const
{
  ConstAbstractImageModelList aimList( GetImageModels() );

  // If, at least one image-model is modified, then this dataset-model
  // is considered modified.
  for( ConstAbstractImageModelList::const_iterator it( aimList.begin() );
       it!=aimList.end();
       ++it )
    {
    if( ( *it )->IsModified() )
      return true;
    }

  // Otherwise, this dataset-model is not modified.
  return false;
}

273
274
275
276
277
278
279
280
/*******************************************************************************/
void
DatasetModel
::Save() const
{
  WriteDescriptor();
}

281
282
283
284
285
286
287
/*******************************************************************************/
/* SLOTS                                                                       */
/*******************************************************************************/

/*******************************************************************************/

} // end namespace 'mvd'