/
cellapplication.cpp
567 lines (502 loc) · 17.2 KB
/
cellapplication.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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2010, Linköpings University,
* Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THIS OSMC PUBLIC
* LICENSE (OSMC-PL). ANY USE, REPRODUCTION OR DISTRIBUTION OF
* THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THE OSMC
* PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköpings University, either from the above address,
* from the URL: http://www.ida.liu.se/projects/OpenModelica
* and in the OpenModelica distribution.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
* OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
* For more information about the Qt-library visit TrollTech's webpage
* regarding the Qt licence: http://www.trolltech.com/products/qt/licensing.html
*/
#define RUN_DRMODELICA_CONVERTION false
//IAEX Headers
#include "cellapplication.h"
#include "celldocument.h"
#include "commandcenter.h"
#include "cellcommandcenter.h"
#include "cursorcommands.h"
#include "notebook.h"
#include "omcinteractiveenvironment.h"
#include "updategroupcellvisitor.h"
#include "commandcompletion.h"
#include "highlighterthread.h"
#include "stylesheet.h"
#include "inputcell.h"
#include "notebookcommands.h"
#include <cstdlib>
namespace IAEX
{
/*!
* \class CellApplication
* \author Ingemar Axelsson and Anders Fernström
* \date 2006-04-10 (update)
*
* \brief Implements the application interface. This class is the
* main controller of the program.
*
* This class has the responsibility to open new windows, open new
* documents and handle commands. Commands are sent to a
* commandCenter object where they are executed and stored (they
* should be stored).
*
* 2005-10-25 AF, Added a check to see if OMC is running, if not -
* try to start OMC.
* 2005-12-16 AF, Added code that create an instance of
* CommandCompletion, so all commands are loaded from the beginning.
* 2005-12-17 AF, Added code that create instance of stylesheet, so
* the styles are loaded from the beginning.
* 2006-01-09 AF, added a new highlight thread with the
* 'openmodelicahighlighter' as the highlighter that should be used.
* 2006-02-09 AF, code for starting omc have been moved to the
* omc interactive environment.
* 2006-02-13 AF, create temp dir
* 2006-02-27 AF, use environment variable to find DrModelica
* 2006-03-24 AF, first look for DrModelica.onb, and then for
* DrModelica.nb
* 2006-04-10 AF, use environment variable to find xml files
* 2006-04-10 AF, Open file that is sent to main
*/
// ******************************************************
CellApplication::CellApplication( int &argc, char *argv[] )
: QObject()
{
app_ = new QApplication(argc, argv);
mainWindow = new QMainWindow();
QDir dir;
// when last window closed, the applicaiton should quit also
QObject::connect(app_, SIGNAL(lastWindowClosed()), app_, SLOT(quit()));
//Create a commandCenter.
cmdCenter_ = new CellCommandCenter(this);
/* Force C-style doubles */
setlocale(LC_NUMERIC, "C");
// 2006-04-10 AF, use environment variable to find xml files
QString openmodelica = OmcInteractiveEnvironment::OpenModelicaHome();
if(!QDir().exists(openmodelica))
{
QMessageBox::critical( 0, "OpenModelica Error", "The environment variable OPENMODELICAHOME="+openmodelica+" is not a valid directory" );
exit(1);
}
// Avoid cluttering the whole disk with omc temp-files
OmcInteractiveEnvironment *env = OmcInteractiveEnvironment::getInstance();
env->evalExpression("setCommandLineOptions(\"+d=shortOutput\")");
QString cmdLine = env->getResult();
cout << "Set shortOutput flag: " << cmdLine.toStdString() << std::endl;
QString tmpDir = OmcInteractiveEnvironment::TmpPath();
if (!QDir().exists(tmpDir)) QDir().mkdir(tmpDir);
tmpDir = QDir(tmpDir).canonicalPath();
cout << "Temp.Dir " << tmpDir.toStdString() << std::endl;
QString cdCmd = "cd(\"" + tmpDir + "\")";
env->evalExpression(cdCmd);
QString cdRes = env->getResult();
cdRes.remove("\"");
if (0 != tmpDir.compare(cdRes)) {
QMessageBox::critical( 0, "OpenModelica Error", "Could not create or cd to temp-dir\nCommand:\n "+tmpDir+"\nReturned:\n "+cdRes);
exit(1);
}
// 2005-12-17 AF, Create instance (load styles) of stylesheet
// 2006-04-10 AF, use environment variable to find stylesheet.xml
Stylesheet *sheet;
try
{
QString stylesheetfile;
if( openmodelica.endsWith("/") || openmodelica.endsWith( "\\") )
stylesheetfile = openmodelica + "share/omnotebook/stylesheet.xml";
else
stylesheetfile = openmodelica + "/share/omnotebook/stylesheet.xml";
sheet = Stylesheet::instance( stylesheetfile );
}
catch( exception &e )
{
QMessageBox::warning( 0, "Error", e.what(), "OK" );
exit(-1);
}
// 2005-12-16 AF, Create instance of CommandCompletion.
// 2006-04-10 AF, use environment variable to find commands.xml
try
{
QString commandfile;
if( openmodelica.endsWith("/") || openmodelica.endsWith( "\\") )
commandfile = openmodelica + "share/omnotebook/commands.xml";
else
commandfile = openmodelica + "/share/omnotebook/commands.xml";
CommandCompletion::instance( commandfile );
}
catch( exception &e )
{
QString msg = e.what();
msg += "\nCould not create command completion class, exiting OMNotebook";
QMessageBox::warning( 0, "Error", msg, "OK" );
std::exit(-1);
}
// 2006-03-21 AF, code for converting DrModelica from mathematica
// fullform to OMNotebook (.onb)
if( RUN_DRMODELICA_CONVERTION )
{
convertDrModelica();
}
else
{
// 2006-01-09 AF, create a new highlight thread with the
// 'openmodelicahighlighter' as the highlighter that should be
// used.
// 2006-04-10 AF, use environment variable to find modelicacolors.xml
try
{
Stylesheet *sheet = Stylesheet::instance( "stylesheet.xml" );
CellStyle style = sheet->getStyle( "Input" );
style.textCharFormat()->setBackground( QBrush( QColor( 200, 200, 255 ) ));
QString modelicacolorsfile;
if( openmodelica.endsWith("/") || openmodelica.endsWith( "\\") )
modelicacolorsfile = openmodelica + "share/omnotebook/modelicacolors.xml";
else
modelicacolorsfile = openmodelica + "/share/omnotebook/modelicacolors.xml";
OpenModelicaHighlighter *highlighter = new OpenModelicaHighlighter( modelicacolorsfile, *style.textCharFormat() );
HighlighterThread *thread = HighlighterThread::instance( highlighter );
//thread->start( QThread::LowPriority );
}
catch( exception &e )
{
QString msg = e.what();
msg += "\nCould not create highlighter thread, exiting OMNotebook";
QMessageBox::warning( 0, "Error", msg, "OK" );
std::exit(-1);
}
// second arg is a file that should be opened.
if( argc > 1 )
{
QString fileToOpen( argv[1] );
if( dir.exists( fileToOpen ) && ( fileToOpen.endsWith( ".onb" ) || fileToOpen.endsWith( ".onbz" ) || fileToOpen.endsWith( ".nb" )))
{
open( fileToOpen );
}
else
{
cout << "File not found: " << fileToOpen.toStdString() << endl;
open(QString::null);
}
}
else
{
// 2006-02-27 AF, use environment variable to find DrModelica
// 2006-03-24 AF, First try to find DrModelica.onb, then .nb
QString drmodelica = OmcInteractiveEnvironment::OpenModelicaHome() + "/share/omnotebook/drmodelica/DrModelica.onb";
//QString drmodelica = OmcInteractiveEnvironment::OpenModelicaHome() + "/share/omnotebook/drmodelica/QuickTour/HelloWorld.onb";
if( dir.exists( drmodelica ))
open(drmodelica);
else if( dir.exists( "DrModelica/DrModelica.onb" ))
open( "DrModelica/DrModelica.onb" );
else
{
cout << "Unable to find (1): " << drmodelica.toStdString() << endl;
cout << "Unable to find (2): DrModelica/DrModelica.onb" << endl;
// NB
drmodelica = OmcInteractiveEnvironment::OpenModelicaHome() + "/share/omnotebook/drmodelica/DrModelica.onb";
if( dir.exists( drmodelica ))
open(drmodelica);
else if( dir.exists( "DrModelica/DrModelica.nb" ))
open( "DrModelica/DrModelica.nb" );
else
{
cout << "Unable to find (3): " << drmodelica.toStdString() << endl;
cout << "Unable to find (4): DrModelica/DrModelica.nb" << endl;
open(QString::null);
}
}
}
}
}
/*!
* \author Anders Fernström and Ingemar Axelsson
* \date 2006-05-03 (update)
*
* \brief Class destructor
*
* 2005-11-24 AF, Added code that quited OMC, if it was still running.
* 2005-12-19 AF, Added code that stopped the highlighter thread,
* if it is running
* 2006-01-16 AF, Go Through remove list and remove all temporary
* files.
* 2006-02-09 AF, moved code for quiting omc to notebook windows
* closeEvent handler
* 2006-02-13 AF, remove temp dir
* 2006-05-03 AF, delete notebook socket
*/
CellApplication::~CellApplication()
{
// 2005-12-19 AF, stop highlighter thread
HighlighterThread *thread = HighlighterThread::instance();
thread->exit();
// 2006-02-09 AF, moved code for quiting omc to the notebook windos
// 2006-01-16 AF, remove temporary files
QDir dir;
for( int i = 0; i < removeList_.size(); i++ )
{
if( !dir.remove( removeList_.at(i) ))
{
QString msg = "Could not remove temporary image " + removeList_.at(i) + " from harddrive.";
QMessageBox::warning( 0, "Warning", msg, "OK" );
}
}
}
/*!
* \author Ingemar Axelsson
*/
CommandCenter *CellApplication::commandCenter()
{
return cmdCenter_;
}
/*!
* \author Ingemar Axelsson
*/
void CellApplication::setCommandCenter(CommandCenter *c)
{
cmdCenter_ = c;
cmdCenter_->setApplication(this);
}
/*!
* \author Anders Fernström and Ingemar Axelsson
* \date 2006-01-12 (update)
*
* 2006-01-12 AF, added so any images in the cell are copied.
* 2006-02-13 AF, removed code for copy image
*
* \todo Create a pasteboard class as a Singleton that should be
* used instead of having a singleton inside the application class.
* Other things to do is to use the systemwide pasteboard instead.
* (Ingemar Axelsson)
*/
void CellApplication::addToPasteboard(Cell *c)
{
pasteboard_.push_back(c);
}
/*!
* \author Ingemar Axelsson
*
* This is used to clear the pasteboard. This is an ugly solution.
*/
void CellApplication::clearPasteboard()
{
pasteboard_.clear();
}
/*!
* \author Ingemar Axelsson
*
* \brief returns a vector with all content of the pasteboard.
*/
vector<Cell*> CellApplication::pasteboard()
{
return pasteboard_;
}
/*!
* \author Ingemar Axelsson
*/
int CellApplication::exec()
{
return app_->exec();
}
/*!
* \author Ingemar Axelsson
*/
void CellApplication::add(Document *d)
{
documents_.push_back(d);
}
/*!
* \author Ingemar Axelsson
*/
void CellApplication::add(DocumentView *d)
{
views_.push_back(d);
}
/*!
* \author Ingemar Axelsson and Anders Fernström
* \date 2006-05-03 (update)
*
* \brief Open an file, and display the content of hte file
*
* 2005-09-22 AF, added the filename to the NotebookWindow() call
* 2005-10-11 AF, Porting, added resize call, so all cells get the
* correct size. Ugly way!
* 2005-11-30 AF, added code to launch the visitor that applies
* hide() and show() to groupcells.
* 2005-12-01 AF, added a try-catch statment around the function
* 2006-01-17 AF, added code that set the change variable in a
* document to false.
* 2006-01-31 AF, open windows minimized, then show normal when
* all operations are done on the window.
* 2006-05-03 AF, during open, stop highlighter
*/
void CellApplication::open( const QString filename, int readmode )
{
// 2005-12-01 AF, Added try-catch
try
{
//2006-05-03 AF, during open, stop highlighter
HighlighterThread *thread = HighlighterThread::instance();
thread->setStop( true );
//1. Create a new document.
Document *d = new CellDocument( this, filename, readmode );
add(d);
//2. Create a new View.
// 2005-09-22 AF: Added 'filename' in NotebookWindow() call
DocumentView *v = new NotebookWindow(d, filename);
add(v);
// 2006-01-31 AF, Open window minimized insted of normal
//v->showMinimized();
// 2005-10-11 AF, Porting, added resize so all cells get the
// correct size. Ugly way!
//v->resize( 810, 610 ); //not working with Qt 4.3
// 2006-01-17 AF, when the document have been opened, set the
// changed variable to false.
v->document()->setChanged( false );
// 2006-01-31 AF, show window again
v->show();
v->resize( 801, 600 ); //fjass
// 2005-11-30 AF, apply hide() and show() to closed groupcells
// childs in the documentview
UpdateGroupcellVisitor visitor;
v->document()->runVisitor( visitor );
// 2006-05-03 AF, done, start highlighter again
thread->setStop( false );
}
catch( exception &e )
{
throw e;
}
}
/*!
* \author Anders Fernström
* \date 2006-01-16
*
* \brief Add filename to a list of temporary files that should
* be deleted when the applicaiton quits.
*/
void CellApplication::removeTempFiles(QString filename)
{
removeList_.append( filename );
}
/*!
* \author Anders Fernström
* \date 2006-01-27
*
* \brief returns list of all current doucment views
*/
vector<DocumentView *> CellApplication::documentViewList()
{
return views_;
}
/*!
* \author Anders Fernström
* \date 2006-01-27
*
* \brief remove document view from internal list, also remove
* document
*/
void CellApplication::removeDocumentView( DocumentView *view )
{
vector<Document *>::iterator d_iter = documents_.begin();
while( d_iter != documents_.end() )
{
if( (*d_iter) == view->document() )
{
documents_.erase( d_iter );
break;
}
else
++d_iter;
}
vector<DocumentView *>::iterator dv_iter = views_.begin();
while( dv_iter != views_.end() )
{
if( (*dv_iter) == view )
{
views_.erase( dv_iter );
break;
}
else
++dv_iter;
}
}
/*!
* \author Anders Fernström
* \date 2006-03-21
*
* \brief convert DrModelica documentation into OMNotebook format
* (.onb).
*
* NO A WORKING FUNCTION
* -Temporary function
* -The function is not called anywhere.
* -The function asume that DrModelia is located in 'C:\OpenModelica132\DrModelicaConv'
* -Save documents to 'C:\OpenModelica132\DrModelicaConv'
* -remove all .nb file
*/
void CellApplication::convertDrModelica()
{
cout << "CONVERTING DRMODELICA" << endl;
cout << "---------------------" << endl << endl;
// load from
QString path = "C:/OpenModelica132/DrModelicaConv";
QDir dir( path );
dir.setSorting( QDir::Name );
if( dir.exists() )
{
// get dirs
dir.setFilter( QDir::Dirs | QDir::NoDotAndDotDot);
QStringList dirList = dir.entryList();
dirList.prepend( "" );
for( int i = 0; i < dirList.size(); ++i )
{
// get file names
QDir fileDir( dir.absolutePath() + "/" + dirList.at(i) );
fileDir.setSorting( QDir::Name );
fileDir.setFilter( QDir::Files );
//fileDir.setNameFilters( QStringList(".nb") );
QStringList fileList = fileDir.entryList();
// loop through all files
for( int j = 0; j < fileList.size(); ++j )
{
cout << "Loading: " << fileDir.absolutePath().toStdString() +
string( "/" ) + fileList.at(j).toStdString() << endl;
Document *d = new CellDocument( this, fileDir.absolutePath() +
QString( "/" ) + fileList.at(j), READMODE_CONVERTING_ONB );
// save file
QString filename = fileList.at(j);
filename.replace( ".nb", ".onb" );
cout << "Saving: " << dir.absolutePath().toStdString() +
string( "/" ) + dirList.at(i).toStdString() + string( "/" ) +
filename.toStdString() << endl;
SaveDocumentCommand command( d, dir.absolutePath() +
QString( "/" ) + dirList.at(i) + QString( "/" ) + filename );
this->commandCenter()->executeCommand( &command );
cout << "DONE!" << endl << endl;
// delete file
delete d;
fileDir.remove( fileList.at(j) );
}
}
}
cout << "CONVERTION DONE !!!" << endl;
}
}