/
cellmltoolsplugin.cpp
491 lines (352 loc) · 17.9 KB
/
cellmltoolsplugin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*******************************************************************************
Copyright (C) The University of Auckland
OpenCOR 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 3 of the License, or
(at your option) any later version.
OpenCOR is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
//==============================================================================
// CellML tools plugin
//==============================================================================
#include "cellmlfilemanager.h"
#include "cellmlinterface.h"
#include "cellmltoolsplugin.h"
#include "corecliutils.h"
#include "coreguiutils.h"
#include "filemanager.h"
//==============================================================================
#include <QApplication>
#include <QFile>
#include <QMainWindow>
#include <QMenu>
//==============================================================================
#include <iostream>
//==============================================================================
namespace OpenCOR {
namespace CellMLTools {
//==============================================================================
PLUGININFO_FUNC CellMLToolsPluginInfo()
{
Descriptions descriptions;
descriptions.insert("en", QString::fromUtf8("a plugin to access various <a href=\"http://www.cellml.org/\">CellML</a>-related tools."));
descriptions.insert("fr", QString::fromUtf8("une extension pour accéder divers outils en rapport avec <a href=\"http://www.cellml.org/\">CellML</a>."));
return new PluginInfo(PluginInfo::Tools, true, true,
QStringList() << "CellMLSupport",
descriptions);
}
//==============================================================================
CellMLToolsPlugin::CellMLToolsPlugin() :
mFileName(QString())
{
}
//==============================================================================
// CLI interface
//==============================================================================
int CellMLToolsPlugin::executeCommand(const QString &pCommand,
const QStringList &pArguments)
{
// Run the given CLI command
if (!pCommand.compare("help")) {
// Display the commands that we support
runHelpCommand();
return 0;
} else if (!pCommand.compare("export")) {
// Export a file from one format to another
return runExportCommand(pArguments);
} else {
// Not a CLI command that we support
runHelpCommand();
return -1;
}
}
//==============================================================================
// GUI interface
//==============================================================================
void CellMLToolsPlugin::updateGui(Plugin *pViewPlugin, const QString &pFileName)
{
Q_UNUSED(pViewPlugin);
// Enable/disable and show/hide our tools in case we are dealing with a
// CellML-based view plugin
CellMLSupport::CellmlFile *cellmlFile = CellMLSupport::CellmlFileManager::instance()->cellmlFile(pFileName);
CellMLSupport::CellmlFile::Version cellmlVersion = cellmlFile?cellmlFile->version():CellMLSupport::CellmlFile::Unknown;
mExportToCellml10Action->setEnabled( cellmlFile && cellmlFile->model()
&& (cellmlVersion != CellMLSupport::CellmlFile::Unknown)
&& (cellmlVersion != CellMLSupport::CellmlFile::Cellml_1_0));
mExportToUserDefinedFormatAction->setEnabled(cellmlFile && cellmlFile->model());
// Keep track of the file name
mFileName = pFileName;
}
//==============================================================================
Gui::Menus CellMLToolsPlugin::guiMenus() const
{
// We don't handle this interface...
return Gui::Menus();
}
//==============================================================================
Gui::MenuActions CellMLToolsPlugin::guiMenuActions() const
{
// Return our menu actions
return Gui::MenuActions() << Gui::MenuAction(Gui::MenuAction::Tools, mCellmlFileExportToMenu->menuAction())
<< Gui::MenuAction(Gui::MenuAction::Tools);
}
//==============================================================================
// I18n interface
//==============================================================================
void CellMLToolsPlugin::retranslateUi()
{
// Retranslate our different Tools actions
retranslateMenu(mCellmlFileExportToMenu, tr("CellML File Export To"));
retranslateAction(mExportToCellml10Action, tr("CellML 1.0..."),
tr("Export the CellML file to CellML 1.0"));
retranslateAction(mExportToUserDefinedFormatAction, tr("User-Defined Format..."),
tr("Export the CellML file to some user-defined format"));
}
//==============================================================================
// Plugin interface
//==============================================================================
bool CellMLToolsPlugin::definesPluginInterfaces()
{
// We don't handle this interface...
return false;
}
//==============================================================================
bool CellMLToolsPlugin::pluginInterfacesOk(const QString &pFileName,
QObject *pInstance)
{
Q_UNUSED(pFileName);
Q_UNUSED(pInstance);
// We don't handle this interface...
return false;
}
//==============================================================================
void CellMLToolsPlugin::initializePlugin()
{
// What we are doing below requires to be in GUI mode, so leave if we are
// not in that mode
if (!Core::mainWindow())
return;
// Create our Tools | Export To menu
mCellmlFileExportToMenu = Core::newMenu("CellmlFileExportTo", Core::mainWindow());
// Create our different Tools | Export To actions, and add them to our
// Tools | Export To menu
mExportToCellml10Action = Core::newAction(Core::mainWindow());
mExportToUserDefinedFormatAction = Core::newAction(Core::mainWindow());
mCellmlFileExportToMenu->addAction(mExportToCellml10Action);
mCellmlFileExportToMenu->addSeparator();
mCellmlFileExportToMenu->addAction(mExportToUserDefinedFormatAction);
// Some connections to handle our different Tools | Export To actions
connect(mExportToCellml10Action, SIGNAL(triggered(bool)),
this, SLOT(exportToCellml10()));
connect(mExportToUserDefinedFormatAction, SIGNAL(triggered(bool)),
this, SLOT(exportToUserDefinedFormat()));
}
//==============================================================================
void CellMLToolsPlugin::finalizePlugin()
{
// We don't handle this interface...
}
//==============================================================================
void CellMLToolsPlugin::pluginsInitialized(const Plugins &pLoadedPlugins)
{
Q_UNUSED(pLoadedPlugins);
// We don't handle this interface...
}
//==============================================================================
void CellMLToolsPlugin::loadSettings(QSettings *pSettings)
{
Q_UNUSED(pSettings);
// We don't handle this interface...
}
//==============================================================================
void CellMLToolsPlugin::saveSettings(QSettings *pSettings) const
{
Q_UNUSED(pSettings);
// We don't handle this interface...
}
//==============================================================================
void CellMLToolsPlugin::handleUrl(const QUrl &pUrl)
{
Q_UNUSED(pUrl);
// We don't handle this interface...
}
//==============================================================================
// Plugin specific
//==============================================================================
void CellMLToolsPlugin::runHelpCommand()
{
// Output the commands we support
std::cout << "Commands supported by the CellMLTools plugin:" << std::endl;
std::cout << " * Display the commands supported by the CellMLTools plugin:" << std::endl;
std::cout << " help" << std::endl;
std::cout << " * Export <file> to a <predefined_format> or a <user_defined_format_file>:" << std::endl;
std::cout << " export <file> <predefined_format>|<user_defined_format_file>" << std::endl;
std::cout << " <predefined_format> can take one of the following values:" << std::endl;
std::cout << " cellml_1_0: to export a CellML 1.1 file to CellML 1.0" << std::endl;
}
//==============================================================================
int CellMLToolsPlugin::runExportCommand(const QStringList &pArguments)
{
// Export an existing file to the console using a given format as the
// destination format
// Make sure that we have the correct number of arguments
if (pArguments.count() != 2) {
runHelpCommand();
return -1;
}
// Check whether we are dealing with a local or a remote file
QString errorMessage = QString();
bool isLocalFile;
QString fileNameOrUrl;
Core::checkFileNameOrUrl(pArguments[0], isLocalFile, fileNameOrUrl);
QString fileName = fileNameOrUrl;
if (!isLocalFile) {
// We are dealing with a remote file, so try to get a local copy of it
QString fileContents;
if (Core::readFileContentsFromUrl(fileNameOrUrl, fileContents, &errorMessage)) {
// We were able to retrieve the contents of the remote file, so save
// it locally to a 'temporary' file
fileName = Core::temporaryFileName();
if (!Core::writeFileContentsToFile(fileName, fileContents))
errorMessage = "The file could not be saved locally.";
} else {
errorMessage = QString("The file could not be opened (%1).").arg(Core::formatMessage(errorMessage));
}
}
// At this stage, we should have a real file (be it originally local or
// remote), so carry on with the export
if (errorMessage.isEmpty()) {
// Before actually doing the export, we need to make sure that the file
// exists, that it is a valid CellML file, that it can be managed and
// that it can be loaded
if (!QFile::exists(fileName)) {
errorMessage = "The file could not be found.";
} else if (!CellMLSupport::CellmlFileManager::instance()->isCellmlFile(fileName)) {
errorMessage = "The file is not a CellML file.";
} else {
Core::FileManager *fileManagerInstance = Core::FileManager::instance();
if (fileManagerInstance->manage(fileName,
isLocalFile?
Core::File::Local:
Core::File::Remote,
isLocalFile?
QString():
fileNameOrUrl) != Core::FileManager::Added) {
errorMessage = "The file could not be managed.";
} else {
CellMLSupport::CellmlFile *cellmlFile = new CellMLSupport::CellmlFile(fileName);
if (!cellmlFile->load()) {
errorMessage = "The file could not be loaded.";
} else {
// At this stage, everything is fine with the file, so now
// we need to check the type of export the user wants
QString predefinedFormatOrUserDefinedFormatFileName = pArguments[1];
bool wantExportToUserDefinedFormat = predefinedFormatOrUserDefinedFormatFileName.compare("cellml_1_0");
// If we want to export to CellML 1.0, then we need to make
// sure that the file is not already in that format
if ( wantExportToUserDefinedFormat
&& !QFile::exists(predefinedFormatOrUserDefinedFormatFileName)) {
errorMessage = "The user-defined format file could not be found.";
} else if ( !wantExportToUserDefinedFormat
&& (cellmlFile->version() == CellMLSupport::CellmlFile::Cellml_1_0)) {
errorMessage = "The file is already a CellML 1.0 file.";
} else {
// Everything seems to be fine, so attempt the export
// itself
if ( ( wantExportToUserDefinedFormat && !cellmlFile->exportTo(QString(), predefinedFormatOrUserDefinedFormatFileName))
|| (!wantExportToUserDefinedFormat && !cellmlFile->exportTo(QString(), CellMLSupport::CellmlFile::Cellml_1_0))) {
errorMessage = "The file could not be exported";
if (cellmlFile->issues().count()) {
errorMessage += " ("+cellmlFile->issues().first().message()+")";
// Note: if there are 'cellmlFile->issues()',
// then there can be only one of them
// following a CellML export...
}
errorMessage += '.';
}
}
}
// We are done (whether the export was successful or not), so
// delete our CellML file object and unmanage our input file
delete cellmlFile;
fileManagerInstance->unmanage(fileName);
}
}
}
// Delete the temporary file, if any, i.e. we are dealing with a remote file
// and it has a temporay file associated with it
if (!isLocalFile && QFile::exists(fileName))
QFile::remove(fileName);
// Let the user know if something went wrong at some point and then leave
if (errorMessage.isEmpty()) {
return 0;
} else {
std::cout << errorMessage.toStdString() << std::endl;
return -1;
}
}
//==============================================================================
void CellMLToolsPlugin::exportToCellml10()
{
// Export the current file to CellML 1.0 by asking for the name of the file
// that will contain the export
QStringList cellmlFilters = Core::filters(FileTypeInterfaces() << CellMLSupport::fileTypeInterface());
QString firstCellmlFilter = cellmlFilters.first();
QString fileName = Core::getSaveFileName(tr("Export CellML File To CellML 1.0"),
Core::newFileName(mFileName, tr("Exported"), false),
cellmlFilters, &firstCellmlFilter);
if (fileName.isEmpty())
return;
// Now that we have a file name, we can do the export itself
CellMLSupport::CellmlFile *cellmlFile = CellMLSupport::CellmlFileManager::instance()->cellmlFile(mFileName);
if (!cellmlFile->exportTo(fileName, CellMLSupport::CellmlFile::Cellml_1_0, true)) {
QString errorMessage = QString();
if (cellmlFile->issues().count()) {
errorMessage = " ("+cellmlFile->issues().first().message()+")";
// Note: if there are 'cellmlFile->issues()', then there can be only
// one of them following a CellML export...
}
Core::warningMessageBox(tr("Export CellML File To CellML 1.0"),
tr("<strong>%1</strong> could not be exported to <strong>CellML 1.0</strong>%2.").arg(fileName, errorMessage));
}
}
//==============================================================================
void CellMLToolsPlugin::exportToUserDefinedFormat()
{
// Ask for the name of the file that contains the user-defined format
QString xmlFilter = tr("User-Defined Format File")+" (*.xml)";
QString userDefinedFormatFileName = Core::getOpenFileName(tr("Select User-Defined Format File"),
QStringList() << xmlFilter,
&xmlFilter);
if (userDefinedFormatFileName.isEmpty())
return;
// Ask for the name of the file that will contain the export
QString fileName = Core::getSaveFileName(tr("Export CellML File To User-Defined Format"),
Core::newFileName(mFileName, tr("Exported"), false));
if (fileName.isEmpty())
return;
// Now that we have both a user-defined format file and output file, we can
// do the export itself
CellMLSupport::CellmlFile *cellmlFile = CellMLSupport::CellmlFileManager::instance()->cellmlFile(mFileName);
if (!cellmlFile->exportTo(fileName, userDefinedFormatFileName, true)) {
QString errorMessage = QString();
if (cellmlFile->issues().count()) {
errorMessage = " ("+cellmlFile->issues().first().message()+")";
// Note: if there are 'cellmlFile->issues()', then there can be only
// one of them following a CellML export...
}
Core::warningMessageBox(tr("Export CellML File To User-Defined Format"),
tr("<strong>%1</strong> could not be exported to the user-defined format described in <strong>%2</strong>%3.").arg(fileName, userDefinedFormatFileName, errorMessage));
}
}
//==============================================================================
} // namespace CellMLSupport
} // namespace OpenCOR
//==============================================================================
// End of file
//==============================================================================