mvdApplication.cxx 6.34 KB
Newer Older
1
2
/*=========================================================================

3
  Program:   Monteverdi2
4
5
6
  Language:  C++


7
8
  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
  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

=========================================================================*/
#include "mvdApplication.h"

21
22
23
24

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

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

//
30
// System includes (sorted by alphabetic order)
31
32
33
34
35
36

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

//
// Monteverdi includes (sorted by alphabetic order)
37
#include "mvdAbstractModel.h"
38
#include "mvdDatasetModel.h"
39
#include "mvdVectorImageModel.h"
40
41
42
43
44

// Application icon on Linux platform
#if ! defined(Q_WS_WIN) && ! defined(Q_WS_MAC)
#  include "monteverdi2-48x48.xpm"
#endif
45
46
47
48
49

//
// Class implementation.
namespace mvd
{
50
51
52
53
54
55
56
/*
  TRANSLATOR mvd::Application

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

  Context comment for translator.
*/
57

58
59
60
61
62
63
64
65
/*****************************************************************************/
/* CONSTANTS                                                                 */

const char* Application::DATASET_EXT = ".ds";

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

66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*****************************************************************************/
void
Application
::DatasetPathName( QString& path,
		   QString& name,
		   const QString& imageFilename )
{
  // '/tmp/archive.tar.gz'
  QFileInfo fileInfo( imageFilename );

#if 0
  // Dataset is stored into image-file path.
  // E.g. '/tmp'
  path = fileInfo.path();
#else
  // Dataset is stored into application cache-directory.
  // E.g. '$HOME/<CACHE_DIR>'
  path = Application::Instance()->GetCacheDir().path();
#endif

  // '[_tmp_]archive.tar.gz.<SUFFIX>'
  name =
#if 1
Stéphane Albert's avatar
COMP    
Stéphane Albert committed
89
    fileInfo.canonicalPath().replace( QRegExp( "[/\\\\:]+" ), "_") +
90
91
92
93
94
95
    "_" +
#endif
    fileInfo.fileName() +
    Application::DATASET_EXT;
}

96
97
98
99
100
101
/*****************************************************************************/
DatasetModel*
Application::LoadDatasetModel( const QString& imageFilename,
			       int width,
			       int height )
{
102
  // New model.
103
104
  DatasetModel* model = new DatasetModel();

105
  // Retrive path and name.
106
107
108
109
110
111
112
  QString path;
  QString name;

  Application::DatasetPathName( path, name, imageFilename );
  qDebug() << "Dataset path: " << path;
  qDebug() << "Dataset name: " << name;

113
114
115
  // Setup QObject
  model->setObjectName( QDir( path ).filePath( name ) );

116
117
  try
    {
118
119
    // try if the filename is valid
    VectorImageModel::EnsureValidImage(imageFilename);
120
    // Build model (relink to cached data).
Mickael Savinaud's avatar
Mickael Savinaud committed
121
    DatasetModel::BuildContext context( path, name, width, height );
122
123
124
125
    model->BuildModel( &context );

    // Load image if DatasetModel is empty.
    if( !model->HasSelectedImageModel() )
126
127
128
129
130
131
132
133
134
135
136
137
138
139
      {
      // Import image from filename given (w; h) size to choose
      // best-fit resolution.
      model->ImportImage( imageFilename, width, height );
      }
    }

  catch( std::exception& exc )
    {
    delete model;
    model = NULL;

    throw;
    }
140
 
141
142
143
144
145
146
  return model;
}

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

147
/*******************************************************************************/
148
149
Application
::Application( int& argc, char** argv ) :
150
151
  I18nApplication( argc, argv ),
  m_Model( NULL )
152
{
153
154
  InitializeCore();

155
156
157
158
  QObject::connect(
    this, SIGNAL( aboutToQuit() ),
    this, SLOT( OnAboutToQuit() )
    );
159
160

  InitializeModel();
161
162
}

163
/*******************************************************************************/
164
165
166
167
168
Application
::~Application()
{
}

169
170
171
172
173
/*******************************************************************************/
void
Application
::SetModel( AbstractModel* model )
{
174
  emit AboutToChangeSelectedModel( model );
175

176
177
  delete m_Model;

178
  m_Model = model;
179
180
181

  if( model!=NULL )
    m_Model->setParent( this );
182

183
  emit SelectedModelChanged( m_Model );
184
185
}

186
187
/*******************************************************************************/
void
188
189
Application
::InitializeCore()
190
{
191
192
  setObjectName( "Application" );

193
194
  //
  // Setup application tags.
195
  //
196
197
198
199
200
201
  QCoreApplication::setApplicationName(
    PROJECT_NAME
  );
  QCoreApplication::setApplicationVersion(
    Monteverdi2_VERSION_STRING
  );
202
203
204

  //
  // Setup organization tags.
205
  //
206
  QCoreApplication::setOrganizationName(
207
    "OrfeoToolBox");
208
209
210
  QCoreApplication::setOrganizationDomain(
    "orfeo-toolbox.org"
  );
211

212
213
#ifndef Q_WS_MAC
    setWindowIcon(QIcon(QLatin1String(":/images/application_icon")));
214
#endif
Julien Malik's avatar
Julien Malik committed
215

216
217
}

218
219
220
221
222
223
224
/*******************************************************************************/
void
Application
::InitializeModel()
{
}

225
226
227
/*******************************************************************************/
bool Application::HasSettingsFile()
{
228
  // The settings file should contain the cacheDir key to be valid
229
230
  QSettings settings;
  settings.sync();
231
  return ( (settings.status()==QSettings::NoError) && settings.contains("cacheDir"));
232
233
234
235
236
237
}

/*******************************************************************************/
void Application::ReadCacheDirFromSettings()
{
  QSettings settings;
238
239
  QString cacheDirSetting = settings.value("cacheDir").toString();
  m_CacheDir.setPath(cacheDirSetting);
240
241
242
243
244
}

void Application::WriteCacheDirIntoSettings()
{
  QSettings settings;
245
  settings.setValue("cacheDir", m_CacheDir.path());
246
247
248
}


249
250
251
/*******************************************************************************/
/* SLOTS                                                                       */
/*******************************************************************************/
252
253
254
255
void
Application
::OnAboutToQuit()
{
256
  qDebug() << tr( "mvd::Application::OnAboutToQuit()." );
257
258
259
}

} // end namespace 'mvd'