-
Notifications
You must be signed in to change notification settings - Fork 297
/
ModelicaClassDialog.cpp
2478 lines (2408 loc) · 119 KB
/
ModelicaClassDialog.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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* 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.
*
*/
/*
* @author Adeel Asghar <adeel.asghar@liu.se>
*/
#include <limits>
#include "Modeling/ModelicaClassDialog.h"
#include "MainWindow.h"
#include "Options/OptionsDialog.h"
#include "MessagesWidget.h"
#include "Util/StringHandler.h"
#include "Modeling/ModelWidgetContainer.h"
#include "Commands.h"
#include "Modeling/ItemDelegate.h"
#include <QApplication>
#include <QMessageBox>
#include <QCompleter>
#include <QHeaderView>
LibraryBrowseDialog::LibraryBrowseDialog(QString title, QLineEdit *pLineEdit, LibraryWidget *pLibraryWidget)
: QDialog(0)
{
setAttribute(Qt::WA_DeleteOnClose);
setWindowTitle(QString("%1 - %2").arg(Helper::applicationName, title));
resize(250, 500);
mpLineEdit = pLineEdit;
mpLibraryWidget = pLibraryWidget;
mpTreeSearchFilters = new TreeSearchFilters(this);
mpTreeSearchFilters->getFilterTextBox()->setPlaceholderText(Helper::filterClasses);
connect(mpTreeSearchFilters->getFilterTextBox(), SIGNAL(returnPressed()), SLOT(searchClasses()));
connect(mpTreeSearchFilters->getFilterTextBox(), SIGNAL(textEdited(QString)), SLOT(searchClasses()));
connect(mpTreeSearchFilters->getCaseSensitiveCheckBox(), SIGNAL(toggled(bool)), SLOT(searchClasses()));
connect(mpTreeSearchFilters->getSyntaxComboBox(), SIGNAL(currentIndexChanged(int)), SLOT(searchClasses()));
// create the tree
mpLibraryTreeProxyModel = new LibraryTreeProxyModel(mpLibraryWidget, true);
mpLibraryTreeProxyModel->setDynamicSortFilter(true);
mpLibraryTreeProxyModel->setSourceModel(mpLibraryWidget->getLibraryTreeModel());
mpLibraryTreeView = new QTreeView;
mpLibraryTreeView->setItemDelegate(new ItemDelegate(mpLibraryTreeView));
mpLibraryTreeView->setTextElideMode(Qt::ElideMiddle);
mpLibraryTreeView->setIndentation(Helper::treeIndentation);
mpLibraryTreeView->setDragEnabled(true);
int libraryIconSize = OptionsDialog::instance()->getGeneralSettingsPage()->getLibraryIconSizeSpinBox()->value();
mpLibraryTreeView->setIconSize(QSize(libraryIconSize, libraryIconSize));
mpLibraryTreeView->setContextMenuPolicy(Qt::CustomContextMenu);
mpLibraryTreeView->setExpandsOnDoubleClick(false);
mpLibraryTreeView->setModel(mpLibraryTreeProxyModel);
connect(mpTreeSearchFilters->getExpandAllButton(), SIGNAL(clicked()), mpLibraryTreeView, SLOT(expandAll()));
connect(mpTreeSearchFilters->getCollapseAllButton(), SIGNAL(clicked()), mpLibraryTreeView, SLOT(collapseAll()));
connect(mpLibraryTreeView, SIGNAL(doubleClicked(QModelIndex)), SLOT(useModelicaClass()));
// try to automatically select if user has something in the text box.
if (!mpLineEdit->text().isEmpty()) {
findAndSelectLibraryTreeItem(QRegExp(mpLineEdit->text()));
}
// Create the buttons
mpOkButton = new QPushButton(Helper::ok);
mpOkButton->setAutoDefault(true);
connect(mpOkButton, SIGNAL(clicked()), SLOT(useModelicaClass()));
mpCancelButton = new QPushButton(Helper::cancel);
mpCancelButton->setAutoDefault(false);
connect(mpCancelButton, SIGNAL(clicked()), SLOT(reject()));
// create buttons box
mpButtonBox = new QDialogButtonBox(Qt::Horizontal);
mpButtonBox->addButton(mpOkButton, QDialogButtonBox::ActionRole);
mpButtonBox->addButton(mpCancelButton, QDialogButtonBox::ActionRole);
// Create a layout
QGridLayout *pMainLayout = new QGridLayout;
pMainLayout->setAlignment(Qt::AlignLeft | Qt::AlignTop);
pMainLayout->addWidget(mpTreeSearchFilters, 0, 0);
pMainLayout->addWidget(mpLibraryTreeView, 1, 0);
pMainLayout->addWidget(mpButtonBox, 2, 0, 1, 1, Qt::AlignRight);
setLayout(pMainLayout);
}
/*!
* \brief LibraryBrowseDialog::findAndSelectLibraryTreeItem
* Finds the LibraryTreeItem and selects it.
* \param regExp
*/
void LibraryBrowseDialog::findAndSelectLibraryTreeItem(const QRegExp ®Exp)
{
QModelIndex proxyIndex = mpLibraryTreeProxyModel->index(0, 0);
if (proxyIndex.isValid()) {
QModelIndex modelIndex = mpLibraryTreeProxyModel->mapToSource(proxyIndex);
LibraryTreeItem *pLibraryTreeItem = mpLibraryWidget->getLibraryTreeModel()->findLibraryTreeItem(regExp, static_cast<LibraryTreeItem*>(modelIndex.internalPointer()));
if (pLibraryTreeItem) {
modelIndex = mpLibraryWidget->getLibraryTreeModel()->libraryTreeItemIndex(pLibraryTreeItem);
proxyIndex = mpLibraryTreeProxyModel->mapFromSource(modelIndex);
mpLibraryTreeView->selectionModel()->select(proxyIndex, QItemSelectionModel::Select);
while (proxyIndex.parent().isValid()) {
proxyIndex = proxyIndex.parent();
mpLibraryTreeView->expand(proxyIndex);
}
}
}
}
/*!
* \brief LibraryBrowseDialog::searchClasses
* Searches the classes.
*/
void LibraryBrowseDialog::searchClasses()
{
mpLibraryTreeView->selectionModel()->clearSelection();
QString searchText = mpTreeSearchFilters->getFilterTextBox()->text();
QRegExp::PatternSyntax syntax = QRegExp::PatternSyntax(mpTreeSearchFilters->getSyntaxComboBox()->itemData(mpTreeSearchFilters->getSyntaxComboBox()->currentIndex()).toInt());
Qt::CaseSensitivity caseSensitivity = mpTreeSearchFilters->getCaseSensitiveCheckBox()->isChecked() ? Qt::CaseSensitive: Qt::CaseInsensitive;
QRegExp regExp(searchText, caseSensitivity, syntax);
mpLibraryTreeProxyModel->setFilterRegExp(regExp);
// if we have really searched something
if (!searchText.isEmpty()) {
findAndSelectLibraryTreeItem(regExp);
}
}
/*!
* \brief LibraryBrowseDialog::useModelicaClass
* Uses the selected Modelica class.
*/
void LibraryBrowseDialog::useModelicaClass()
{
const QModelIndexList modelIndexes = mpLibraryTreeView->selectionModel()->selectedIndexes();
if (!modelIndexes.isEmpty()) {
QModelIndex index = modelIndexes.at(0);
index = mpLibraryTreeProxyModel->mapToSource(index);
LibraryTreeItem *pLibraryTreeItem = static_cast<LibraryTreeItem*>(index.internalPointer());
mpLineEdit->setText(pLibraryTreeItem->getNameStructure());
}
accept();
}
/*!
* \class ModelicaClassDialog
* \brief Creates a dialog to allow users to create new Modelica class restriction.
*/
/*!
* \brief ModelicaClassDialog::ModelicaClassDialog
* \param pParent
*/
ModelicaClassDialog::ModelicaClassDialog(QWidget *pParent)
: QDialog(pParent)
{
setAttribute(Qt::WA_DeleteOnClose);
setWindowTitle(QString("%1 - %2").arg(Helper::applicationName).arg(Helper::createNewModelicaClass));
setMinimumWidth(400);
// Create the name label and text box
mpNameLabel = new Label(Helper::name);
mpNameTextBox = new QLineEdit;
// Create the specialization label and combo box
mpSpecializationLabel = new Label(tr("Specialization:"));
mpSpecializationComboBox = new QComboBox;
mpSpecializationComboBox->addItem(StringHandler::getModelicaClassType(StringHandler::Model));
mpSpecializationComboBox->addItem(StringHandler::getModelicaClassType(StringHandler::Class));
mpSpecializationComboBox->addItem(StringHandler::getModelicaClassType(StringHandler::Connector));
mpSpecializationComboBox->addItem(StringHandler::getModelicaClassType(StringHandler::ExpandableConnector));
mpSpecializationComboBox->addItem(StringHandler::getModelicaClassType(StringHandler::Record));
mpSpecializationComboBox->addItem(StringHandler::getModelicaClassType(StringHandler::Block));
mpSpecializationComboBox->addItem(StringHandler::getModelicaClassType(StringHandler::Function));
mpSpecializationComboBox->addItem(StringHandler::getModelicaClassType(StringHandler::Package));
mpSpecializationComboBox->addItem(StringHandler::getModelicaClassType(StringHandler::Type));
mpSpecializationComboBox->addItem(StringHandler::getModelicaClassType(StringHandler::Operator));
mpSpecializationComboBox->addItem(StringHandler::getModelicaClassType(StringHandler::OperatorRecord));
mpSpecializationComboBox->addItem(StringHandler::getModelicaClassType(StringHandler::OperatorFunction));
/* Don't add optimization restriction for now.
mpSpecializationComboBox->addItem(StringHandler::getModelicaClassType(StringHandler::OPTIMIZATION));
*/
connect(mpSpecializationComboBox, SIGNAL(currentIndexChanged(QString)), SLOT(showHideSaveContentsInOneFileCheckBox(QString)));
// create the extends the label and text box
mpExtendsClassLabel = new Label(tr("Extends (optional):"));
mpExtendsClassTextBox = new QLineEdit;
mpExtendsClassBrowseButton = new QPushButton(Helper::browse);
mpExtendsClassBrowseButton->setAutoDefault(false);
connect(mpExtendsClassBrowseButton, SIGNAL(clicked()), SLOT(browseExtendsClass()));
// Create the parent package label, text box, browse button
mpParentClassLabel = new Label(tr("Insert in class (optional):"));
mpParentClassTextBox = new QLineEdit;
mpParentClassBrowseButton = new QPushButton(Helper::browse);
mpParentClassBrowseButton->setAutoDefault(false);
connect(mpParentClassBrowseButton, SIGNAL(clicked()), SLOT(browseParentClass()));
// create partial checkbox
mpPartialCheckBox = new QCheckBox(tr("Partial"));
// create encapsulated checkbox
mpEncapsulatedCheckBox = new QCheckBox(tr("Encapsulated"));
// create state checkbox
mpStateCheckBox = new QCheckBox(tr("State"));
// create save contents of package in one file checkbox
mpSaveContentsInOneFileCheckBox = new QCheckBox(Helper::saveContentsInOneFile);
mpSaveContentsInOneFileCheckBox->setChecked(true);
mpSaveContentsInOneFileCheckBox->setVisible(false);
// Create the buttons
mpOkButton = new QPushButton(Helper::ok);
mpOkButton->setAutoDefault(true);
connect(mpOkButton, SIGNAL(clicked()), SLOT(createModelicaClass()));
mpCancelButton = new QPushButton(Helper::cancel);
mpCancelButton->setAutoDefault(false);
connect(mpCancelButton, SIGNAL(clicked()), SLOT(reject()));
// create buttons box
mpButtonBox = new QDialogButtonBox(Qt::Horizontal);
mpButtonBox->addButton(mpOkButton, QDialogButtonBox::ActionRole);
mpButtonBox->addButton(mpCancelButton, QDialogButtonBox::ActionRole);
// Create a layout
QGridLayout *pMainLayout = new QGridLayout;
pMainLayout->setAlignment(Qt::AlignLeft | Qt::AlignTop);
pMainLayout->addWidget(mpNameLabel, 0, 0);
pMainLayout->addWidget(mpNameTextBox, 0, 1, 1, 2);
pMainLayout->addWidget(mpSpecializationLabel, 1, 0);
pMainLayout->addWidget(mpSpecializationComboBox, 1, 1, 1, 2);
pMainLayout->addWidget(mpExtendsClassLabel, 2, 0);
pMainLayout->addWidget(mpExtendsClassTextBox, 2, 1);
pMainLayout->addWidget(mpExtendsClassBrowseButton, 2, 2);
pMainLayout->addWidget(mpParentClassLabel, 3, 0);
pMainLayout->addWidget(mpParentClassTextBox, 3, 1);
pMainLayout->addWidget(mpParentClassBrowseButton, 3, 2);
pMainLayout->addWidget(mpPartialCheckBox, 4, 0);
pMainLayout->addWidget(mpSaveContentsInOneFileCheckBox, 4, 1, 1, 2);
pMainLayout->addWidget(mpEncapsulatedCheckBox, 5, 0, 1, 3);
pMainLayout->addWidget(mpStateCheckBox, 6, 0, 1, 3);
pMainLayout->addWidget(mpButtonBox, 7, 0, 1, 3, Qt::AlignRight);
setLayout(pMainLayout);
}
/*!
* \brief ModelicaClassDialog::showHideSaveContentsInOneFileCheckBox
* Show/Hide save contents in one file checkbox.
* \param text
*/
void ModelicaClassDialog::showHideSaveContentsInOneFileCheckBox(QString text)
{
if (text.toLower().compare("package") == 0) {
mpSaveContentsInOneFileCheckBox->setVisible(true);
} else {
mpSaveContentsInOneFileCheckBox->setVisible(false);
mpSaveContentsInOneFileCheckBox->setChecked(true);
}
}
void ModelicaClassDialog::browseExtendsClass()
{
LibraryBrowseDialog *pLibraryBrowseDialog = new LibraryBrowseDialog(tr("Select Extends Class"), mpExtendsClassTextBox,
MainWindow::instance()->getLibraryWidget());
pLibraryBrowseDialog->exec();
}
void ModelicaClassDialog::browseParentClass()
{
LibraryBrowseDialog *pLibraryBrowseDialog = new LibraryBrowseDialog(tr("Select Parent Class"), mpParentClassTextBox,
MainWindow::instance()->getLibraryWidget());
pLibraryBrowseDialog->exec();
}
/*!
Creates a new Modelica class restriction.\n
Slot activated when mpOkButton clicked signal is raised.
*/
void ModelicaClassDialog::createModelicaClass()
{
if (mpNameTextBox->text().isEmpty()) {
QMessageBox::critical(this, QString(Helper::applicationName).append(" - ").append(Helper::error), GUIMessages::getMessage(
GUIMessages::ENTER_NAME).arg(mpSpecializationComboBox->currentText()), Helper::ok);
return;
}
/* if extends class doesn't exist. */
LibraryTreeModel *pLibraryTreeModel = MainWindow::instance()->getLibraryWidget()->getLibraryTreeModel();
LibraryTreeItem *pExtendsLibraryTreeItem = 0;
if (!mpExtendsClassTextBox->text().isEmpty()) {
pExtendsLibraryTreeItem = pLibraryTreeModel->findLibraryTreeItem(mpExtendsClassTextBox->text());
if (!pExtendsLibraryTreeItem) {
QMessageBox::critical(this, QString(Helper::applicationName).append(" - ").append(Helper::error),
GUIMessages::getMessage(GUIMessages::EXTENDS_CLASS_NOT_FOUND).arg(mpExtendsClassTextBox->text()), Helper::ok);
return;
}
}
/* if insert in class doesn't exist. */
LibraryTreeItem *pParentLibraryTreeItem = pLibraryTreeModel->getRootLibraryTreeItem();
if (!mpParentClassTextBox->text().isEmpty()) {
pParentLibraryTreeItem = pLibraryTreeModel->findLibraryTreeItem(mpParentClassTextBox->text());
if (!pParentLibraryTreeItem) {
QMessageBox::critical(this, QString(Helper::applicationName).append(" - ").append(Helper::error),
GUIMessages::getMessage(GUIMessages::INSERT_IN_CLASS_NOT_FOUND).arg(mpParentClassTextBox->text()), Helper::ok);
return;
}
}
/* if insert in class is system library. */
if (pParentLibraryTreeItem && pParentLibraryTreeItem->isSystemLibrary()) {
QMessageBox::critical(this, QString(Helper::applicationName).append(" - ").append(Helper::error),
GUIMessages::getMessage(GUIMessages::INSERT_IN_SYSTEM_LIBRARY_NOT_ALLOWED)
.arg(mpParentClassTextBox->text()), Helper::ok);
return;
}
QString model, parentPackage;
if (mpParentClassTextBox->text().isEmpty()) {
model = mpNameTextBox->text().trimmed();
parentPackage = "Global Scope";
} else {
model = QString(mpParentClassTextBox->text().trimmed()).append(".").append(mpNameTextBox->text().trimmed());
parentPackage = QString("Package '").append(mpParentClassTextBox->text().trimmed()).append("'");
}
// Check whether model exists or not.
if (MainWindow::instance()->getOMCProxy()->existClass(model) || pLibraryTreeModel->findLibraryTreeItemOneLevel(model)) {
QMessageBox::critical(this, QString(Helper::applicationName).append(" - ").append(Helper::error), GUIMessages::getMessage(
GUIMessages::MODEL_ALREADY_EXISTS).arg(mpSpecializationComboBox->currentText()).arg(model)
.arg(parentPackage), Helper::ok);
return;
}
// create the model.
QString modelicaClass = mpEncapsulatedCheckBox->isChecked() ? "encapsulated " : "";
modelicaClass.append(mpPartialCheckBox->isChecked() ? "partial " : "");
modelicaClass.append(mpSpecializationComboBox->currentText().toLower());
if (mpParentClassTextBox->text().isEmpty()) {
if (!MainWindow::instance()->getOMCProxy()->createClass(modelicaClass, mpNameTextBox->text().trimmed(), pExtendsLibraryTreeItem)) {
QMessageBox::critical(this, QString(Helper::applicationName).append(" - ").append(Helper::error), GUIMessages::getMessage(
GUIMessages::ERROR_OCCURRED).arg(MainWindow::instance()->getOMCProxy()->getErrorString()).append("\n\n").
append(GUIMessages::getMessage(GUIMessages::NO_OPENMODELICA_KEYWORDS)), Helper::ok);
return;
}
} else {
if (!MainWindow::instance()->getOMCProxy()->createSubClass(modelicaClass, mpNameTextBox->text().trimmed(), pParentLibraryTreeItem, pExtendsLibraryTreeItem)) {
QMessageBox::critical(this, QString(Helper::applicationName).append(" - ").append(Helper::error), GUIMessages::getMessage(
GUIMessages::ERROR_OCCURRED).arg(MainWindow::instance()->getOMCProxy()->getErrorString()).append("\n\n").
append(GUIMessages::getMessage(GUIMessages::NO_OPENMODELICA_KEYWORDS)), Helper::ok);
return;
}
}
// if state
if (mpStateCheckBox->isChecked()) {
QString nameStructure;
if (pParentLibraryTreeItem->getNameStructure().isEmpty()) {
nameStructure = mpNameTextBox->text().trimmed();
} else {
nameStructure = pParentLibraryTreeItem->getNameStructure() + "." + mpNameTextBox->text().trimmed();
}
MainWindow::instance()->getOMCProxy()->addClassAnnotation(nameStructure, "annotate=Icon(graphics={Text(extent={{-100,100},{100,-100}},textString=\"%name\")})");
MainWindow::instance()->getOMCProxy()->addClassAnnotation(nameStructure, "annotate=__Dymola_state(true)");
MainWindow::instance()->getOMCProxy()->addClassAnnotation(nameStructure, "annotate=singleInstance(true)");
}
// open the new tab in central widget and add the model to library tree.
LibraryTreeItem *pLibraryTreeItem;
pLibraryTreeItem = pLibraryTreeModel->createLibraryTreeItem(mpNameTextBox->text().trimmed(), pParentLibraryTreeItem, false, false, true);
if (pParentLibraryTreeItem != pLibraryTreeModel->getRootLibraryTreeItem() && pParentLibraryTreeItem->getSaveContentsType() == LibraryTreeItem::SaveInOneFile) {
pLibraryTreeItem->setSaveContentsType(LibraryTreeItem::SaveInOneFile);
} else if (mpSaveContentsInOneFileCheckBox->isChecked()) {
pLibraryTreeItem->setSaveContentsType(LibraryTreeItem::SaveInOneFile);
} else {
pLibraryTreeItem->setSaveContentsType(LibraryTreeItem::SaveFolderStructure);
}
pLibraryTreeModel->checkIfAnyNonExistingClassLoaded();
pLibraryTreeItem->setExpanded(true);
// show the ModelWidget
pLibraryTreeModel->showModelWidget(pLibraryTreeItem, true);
if (pLibraryTreeItem->getModelWidget()) {
pLibraryTreeItem->getModelWidget()->getIconGraphicsView()->addClassAnnotation(false);
pLibraryTreeItem->getModelWidget()->getDiagramGraphicsView()->addClassAnnotation(false);
pLibraryTreeItem->getModelWidget()->updateModelText();
}
accept();
}
/*!
* \class OpenModelicaFile
* \brief Creates a dialog to allow users to open Modelica file(s).
*/
/*!
* \brief OpenModelicaFile::OpenModelicaFile
* \param pParent
*/
OpenModelicaFile::OpenModelicaFile(QWidget *pParent)
: QDialog(pParent)
{
setAttribute(Qt::WA_DeleteOnClose);
setWindowTitle(QString(Helper::applicationName).append(" - ").append(Helper::openConvertModelicaFiles));
setMinimumWidth(400);
setModal(true);
// create the File Label, textbox and browse button.
mpFileLabel = new Label(Helper::fileLabel);
mpFileTextBox = new QLineEdit;
mpFileBrowseButton = new QPushButton(Helper::browse);
mpFileBrowseButton->setAutoDefault(false);
connect(mpFileBrowseButton, SIGNAL(clicked()), SLOT(browseForFile()));
// create the encoding label, combobox and encoding note.
mpEncodingLabel = new Label(Helper::encoding);
mpEncodingComboBox = new QComboBox;
StringHandler::fillEncodingComboBox(mpEncodingComboBox);
mpConvertAllFilesCheckBox = new QCheckBox(tr("Convert all files within the selected directory and sub-directories"));
// Create the buttons
/* Open with selected encoding button */
mpOpenWithEncodingButton = new QPushButton(tr("Open with selected encoding"));
mpOpenWithEncodingButton->setStyleSheet("QPushButton{padding: 5px 15px 5px 15px;}");
mpOpenWithEncodingButton->setAutoDefault(true);
connect(mpOpenWithEncodingButton, SIGNAL(clicked()), SLOT(openModelicaFiles()));
/* Open and convert to utf-8 button */
mpOpenAndConvertToUTF8Button = new QPushButton(tr("Open and convert to UTF-8"));
mpOpenAndConvertToUTF8Button->setStyleSheet("QPushButton{padding: 5px 15px 5px 15px;}");
connect(mpOpenAndConvertToUTF8Button, SIGNAL(clicked()), SLOT(convertModelicaFiles()));
/* cancel button */
mpCancelButton = new QPushButton(Helper::cancel);
connect(mpCancelButton, SIGNAL(clicked()), SLOT(reject()));
// create buttons box
mpButtonBox = new QDialogButtonBox(Qt::Horizontal);
mpButtonBox->addButton(mpOpenWithEncodingButton, QDialogButtonBox::ActionRole);
mpButtonBox->addButton(mpOpenAndConvertToUTF8Button, QDialogButtonBox::ActionRole);
mpButtonBox->addButton(mpCancelButton, QDialogButtonBox::ActionRole);
// Create a layout
QGridLayout *pMainLayout = new QGridLayout;
pMainLayout->setAlignment(Qt::AlignLeft | Qt::AlignTop);
pMainLayout->addWidget(mpFileLabel, 0, 0);
pMainLayout->addWidget(mpFileTextBox, 0, 1);
pMainLayout->addWidget(mpFileBrowseButton, 0, 2);
pMainLayout->addWidget(mpEncodingLabel, 1, 0);
pMainLayout->addWidget(mpEncodingComboBox, 1, 1, 1, 2);
pMainLayout->addWidget(mpConvertAllFilesCheckBox, 2, 0, 1, 3);
pMainLayout->addWidget(mpButtonBox, 3, 0, 1, 3, Qt::AlignRight);
setLayout(pMainLayout);
}
/*!
Looks for the files and directories for conversion to UTF-8.
\param filesAndDirectories - the list of files and directories that should be converted.
\param path - the directory path where files and directories are located.
*/
void OpenModelicaFile::convertModelicaFiles(QStringList filesAndDirectories, QString path, QTextCodec *pCodec)
{
foreach (QString fileOrDirectory, filesAndDirectories)
{
QFileInfo fileOrDirectoryInfo(QString(path).append("/").append(fileOrDirectory));
if (fileOrDirectoryInfo.isDir())
{
QDir directory(QString(path).append("/").append(fileOrDirectory));
QStringList nameFilter("*.mo");
QStringList filesAndDirectories = directory.entryList(nameFilter, QDir::AllDirs | QDir::Files | QDir::NoSymLinks |
QDir::NoDotAndDotDot | QDir::Writable | QDir::CaseSensitive);
convertModelicaFiles(filesAndDirectories, directory.absolutePath(), pCodec);
}
else
{
convertModelicaFile(QString(path).append("/").append(fileOrDirectory), pCodec);
}
}
}
/*!
Converts the file to UTF-8 encoding.
\param fileName - the full name of the file to convert.
*/
void OpenModelicaFile::convertModelicaFile(QString fileName, QTextCodec *pCodec)
{
QFile file(fileName);
file.open(QIODevice::ReadOnly);
QString fileData(pCodec->toUnicode(file.readAll()));
file.close();
file.open(QIODevice::WriteOnly | QIODevice::Truncate);
QTextStream out(&file);
out.setCodec(Helper::utf8.toUtf8().constData());
out.setGenerateByteOrderMark(false);
out << fileData;
file.close();
}
/*!
Asks the user to select the Modelica file(s) they want to open.\n
Slot activated when mpFileBrowseButton clicked signal is raised.
*/
void OpenModelicaFile::browseForFile()
{
QStringList fileNames;
mFileNames.clear();
fileNames = StringHandler::getOpenFileNames(this, QString(Helper::applicationName).append(" - ").append(Helper::chooseFiles),
NULL, Helper::omFileTypes, NULL);
foreach (QString fileName, fileNames)
{
mFileNames.append(fileName.replace("\\", "/"));
}
mpFileTextBox->setText(mFileNames.join(";"));
}
/*!
Opens the selected Modelica file(s).\n
Slot activated when mpOpenButton clicked signal is raised.
*/
void OpenModelicaFile::openModelicaFiles(bool convertedToUTF8)
{
int progressValue = 0;
MainWindow::instance()->getProgressBar()->setRange(0, mFileNames.size());
MainWindow::instance()->showProgressBar();
foreach (QString fileName, mFileNames)
{
MainWindow::instance()->getStatusBar()->showMessage(QString(Helper::loading).append(": ").append(fileName));
MainWindow::instance()->getProgressBar()->setValue(++progressValue);
// if file doesn't exists
if (!QFile::exists(fileName))
{
QMessageBox *pMessageBox = new QMessageBox(MainWindow::instance());
pMessageBox->setWindowTitle(QString(Helper::applicationName).append(" - ").append(Helper::error));
pMessageBox->setIcon(QMessageBox::Critical);
pMessageBox->setAttribute(Qt::WA_DeleteOnClose);
pMessageBox->setText(QString(GUIMessages::getMessage(GUIMessages::UNABLE_TO_LOAD_FILE).arg(fileName)));
pMessageBox->setInformativeText(QString(GUIMessages::getMessage(GUIMessages::FILE_NOT_FOUND).arg(fileName)));
pMessageBox->setStandardButtons(QMessageBox::Ok);
pMessageBox->exec();
}
else
{
if (convertedToUTF8)
MainWindow::instance()->getLibraryWidget()->openFile(fileName, Helper::utf8, false);
else
MainWindow::instance()->getLibraryWidget()->openFile(fileName, mpEncodingComboBox->itemData(mpEncodingComboBox->currentIndex()).toString(), false);
}
}
MainWindow::instance()->getStatusBar()->clearMessage();
MainWindow::instance()->hideProgressBar();
accept();
}
/*!
Converts the selected Modelica file(s).\n
Slot activated when mpOpenAndConvertToUTF8Button clicked signal is raised.
*/
void OpenModelicaFile::convertModelicaFiles()
{
QTextCodec *pCodec = QTextCodec::codecForName(mpEncodingComboBox->itemData(mpEncodingComboBox->currentIndex()).toString().toUtf8().constData());
if (pCodec != NULL)
{
MainWindow::instance()->getStatusBar()->showMessage(tr("Converting files to UTF-8"));
QApplication::setOverrideCursor(Qt::WaitCursor);
foreach (QString fileName, mFileNames)
{
if (QFile::exists(fileName))
{
if (mpConvertAllFilesCheckBox->isChecked())
{
QFileInfo fileInfo(fileName);
QDir directory = fileInfo.absoluteDir();
QStringList nameFilter("*.mo");
QStringList filesAndDirectories = directory.entryList(nameFilter, QDir::AllDirs | QDir::Files | QDir::NoSymLinks |
QDir::NoDotAndDotDot | QDir::Writable | QDir::CaseSensitive);
convertModelicaFiles(filesAndDirectories, directory.absolutePath(), pCodec);
}
else
{
convertModelicaFile(fileName, pCodec);
}
}
}
}
MainWindow::instance()->getStatusBar()->clearMessage();
QApplication::restoreOverrideCursor();
openModelicaFiles(true);
}
/*!
* \class SaveAsClassDialog
* \brief Creates a dialog to allow users to save as the Modelica class.
*/
/*!
* \brief SaveAsClassDialog::SaveAsClassDialog
* \param pModelWidget
* \param pParent
*/
SaveAsClassDialog::SaveAsClassDialog(ModelWidget *pModelWidget, QWidget *pParent)
: QDialog(pParent)
{
setAttribute(Qt::WA_DeleteOnClose);
setWindowTitle(QString(Helper::applicationName).append(" - ").append(tr("Save As Modelica Class")));
setMinimumWidth(400);
mpModelWidget = pModelWidget;
setModal(true);
// Create the name label and text box
mpNameLabel = new Label(Helper::name);
mpNameTextBox = new QLineEdit(pModelWidget->getLibraryTreeItem()->getName());
// Create the parent package label, text box, browse button
mpParentPackageLabel = new Label(tr("Insert in class (optional):"));
mpParentClassComboBox = new QComboBox;
mpParentClassComboBox->setEditable(true);
/* Since the default QCompleter for QComboBox is case insenstive. */
QCompleter *pParentClassComboBoxCompleter = mpParentClassComboBox->completer();
pParentClassComboBoxCompleter->setCaseSensitivity(Qt::CaseSensitive);
mpParentClassComboBox->setCompleter(pParentClassComboBoxCompleter);
mpParentClassComboBox->addItem("");
int currentIndex = mpParentClassComboBox->findText(pModelWidget->getLibraryTreeItem()->parent()->getNameStructure(), Qt::MatchExactly);
if (currentIndex > -1)
mpParentClassComboBox->setCurrentIndex(currentIndex);
connect(mpParentClassComboBox, SIGNAL(editTextChanged(QString)), SLOT(showHideSaveContentsInOneFileCheckBox(QString)));
// create save contents of package in one file checkbox
mpSaveContentsInOneFileCheckBox = new QCheckBox(Helper::saveContentsInOneFile);
mpSaveContentsInOneFileCheckBox->setChecked(true);
if (pModelWidget->getLibraryTreeItem()->getRestriction() == StringHandler::Package && mpParentClassComboBox->currentText().isEmpty())
mpSaveContentsInOneFileCheckBox->setVisible(true);
else
mpSaveContentsInOneFileCheckBox->setVisible(false);
// Create the buttons
mpOkButton = new QPushButton(Helper::ok);
mpOkButton->setAutoDefault(true);
connect(mpOkButton, SIGNAL(clicked()), SLOT(saveAsModelicaClass()));
mpCancelButton = new QPushButton(Helper::cancel);
mpCancelButton->setAutoDefault(false);
connect(mpCancelButton, SIGNAL(clicked()), SLOT(reject()));
// create buttons box
mpButtonBox = new QDialogButtonBox(Qt::Horizontal);
mpButtonBox->addButton(mpOkButton, QDialogButtonBox::ActionRole);
mpButtonBox->addButton(mpCancelButton, QDialogButtonBox::ActionRole);
// Create a layout
QGridLayout *pMainLayout = new QGridLayout;
pMainLayout->setAlignment(Qt::AlignLeft | Qt::AlignTop);
pMainLayout->addWidget(mpNameLabel, 0, 0);
pMainLayout->addWidget(mpNameTextBox, 0, 1);
pMainLayout->addWidget(mpParentPackageLabel, 1, 0);
pMainLayout->addWidget(mpParentClassComboBox, 1, 1);
pMainLayout->addWidget(mpSaveContentsInOneFileCheckBox, 2, 0);
pMainLayout->addWidget(mpButtonBox, 2, 1, Qt::AlignRight);
setLayout(pMainLayout);
}
/*!
Creates a new Modelica class restriction.\n
Slot activated when mpOkButton clicked signal is raised.
*/
void SaveAsClassDialog::saveAsModelicaClass()
{
QString type = StringHandler::getModelicaClassType(mpModelWidget->getLibraryTreeItem()->getRestriction());
if (mpNameTextBox->text().isEmpty()) {
QMessageBox::critical(this, QString(Helper::applicationName).append(" - ").append(Helper::error), GUIMessages::getMessage(
GUIMessages::ENTER_NAME).arg(type), Helper::ok);
return;
}
/* if insert in class doesn't exist. */
if (!mpParentClassComboBox->currentText().isEmpty()) {
if (!MainWindow::instance()->getOMCProxy()->existClass(mpParentClassComboBox->currentText())) {
QMessageBox::critical(this, QString(Helper::applicationName).append(" - ").append(Helper::error), GUIMessages::getMessage(
GUIMessages::INSERT_IN_CLASS_NOT_FOUND).arg(mpParentClassComboBox->currentText()), Helper::ok);
return;
}
}
/* if insert in class is system library. */
LibraryTreeModel *pLibraryTreeModel = MainWindow::instance()->getLibraryWidget()->getLibraryTreeModel();
LibraryTreeItem *pParentLibraryTreeItem = pLibraryTreeModel->findLibraryTreeItem(mpParentClassComboBox->currentText());
if (pParentLibraryTreeItem) {
if (pParentLibraryTreeItem->isSystemLibrary()) {
QMessageBox::critical(this, QString(Helper::applicationName).append(" - ").append(Helper::error), GUIMessages::getMessage(
GUIMessages::INSERT_IN_SYSTEM_LIBRARY_NOT_ALLOWED).arg(mpParentClassComboBox->currentText()), Helper::ok);
return;
}
}
QString model, parentPackage;
if (mpParentClassComboBox->currentText().isEmpty())
{
model = mpNameTextBox->text();
parentPackage = "Global Scope";
}
else
{
model = QString(mpParentClassComboBox->currentText()).append(".").append(mpNameTextBox->text());
parentPackage = QString("Package '").append(mpParentClassComboBox->currentText()).append("'");
}
// Check whether model exists or not.
if (MainWindow::instance()->getOMCProxy()->existClass(model) || MainWindow::instance()->getLibraryWidget()->getLibraryTreeModel()->findLibraryTreeItemOneLevel(model)) {
QMessageBox::critical(this, QString(Helper::applicationName).append(" - ").append(Helper::error), GUIMessages::getMessage(
GUIMessages::MODEL_ALREADY_EXISTS).arg(type).arg(model).arg(parentPackage), Helper::ok);
return;
}
// duplicate the model.
QString sourceModelText = MainWindow::instance()->getOMCProxy()->list(mpModelWidget->getLibraryTreeItem()->getNameStructure());
QString duplicateModelText = sourceModelText;
/* remove the starting and ending text strings of the model. */
duplicateModelText.remove(0, QString(type.toLower()).append(" ").append(mpModelWidget->getLibraryTreeItem()->getName()).length());
QString endString = QString("end ").append(mpModelWidget->getLibraryTreeItem()->getName()).append(";");
duplicateModelText.remove(duplicateModelText.lastIndexOf(endString), endString.length());
/* add the starting and ending text strings. */
duplicateModelText.prepend(QString(type.toLower()).append(" ").append(mpNameTextBox->text()));
duplicateModelText.append(QString("end ").append(mpNameTextBox->text()).append(";"));
if (!mpParentClassComboBox->currentText().isEmpty())
{
duplicateModelText.prepend(QString("within ").append(mpParentClassComboBox->currentText()).append(";"));
}
MainWindow::instance()->getOMCProxy()->sendCommand(duplicateModelText);
if (MainWindow::instance()->getOMCProxy()->getResult().toLower().contains("error"))
{
QMessageBox::critical(this, QString(Helper::applicationName).append(" - ").append(Helper::error), GUIMessages::getMessage(
GUIMessages::ERROR_OCCURRED).arg(MainWindow::instance()->getOMCProxy()->getResult()).append("\n\n").
append(GUIMessages::getMessage(GUIMessages::NO_OPENMODELICA_KEYWORDS)), Helper::ok);
return;
}
//open the new tab in central widget and add the model to library tree.
LibraryTreeItem *pLibraryTreeItem;
if (pParentLibraryTreeItem) {
pLibraryTreeItem = pLibraryTreeModel->createLibraryTreeItem(mpNameTextBox->text(), pParentLibraryTreeItem, false, false, true);
} else {
pLibraryTreeItem = pLibraryTreeModel->createLibraryTreeItem(mpNameTextBox->text(), pLibraryTreeModel->getRootLibraryTreeItem(), false, false, true);
}
pLibraryTreeItem->setSaveContentsType(mpSaveContentsInOneFileCheckBox->isChecked() ? LibraryTreeItem::SaveInOneFile : LibraryTreeItem::SaveFolderStructure);
pLibraryTreeModel->checkIfAnyNonExistingClassLoaded();
// show the ModelWidget
pLibraryTreeModel->showModelWidget(pLibraryTreeItem);
accept();
}
void SaveAsClassDialog::showHideSaveContentsInOneFileCheckBox(QString text)
{
if (text.isEmpty() && mpModelWidget->getLibraryTreeItem()->getRestriction() == StringHandler::Package)
mpSaveContentsInOneFileCheckBox->setVisible(true);
else
mpSaveContentsInOneFileCheckBox->setVisible(false);
}
/*!
* \class DuplicateClassDialog
* \brief Creates a dialog to allow users to duplicate the Modelica class.
*/
/*!
* \brief DuplicateClassDialog::DuplicateClassDialog
* \param pLibraryTreeItem
* \param pParent
*/
DuplicateClassDialog::DuplicateClassDialog(LibraryTreeItem *pLibraryTreeItem, QWidget *pParent)
: QDialog(pParent), mpLibraryTreeItem(pLibraryTreeItem)
{
setMinimumWidth(400);
setAttribute(Qt::WA_DeleteOnClose);
setWindowTitle(QString("%1 - %2 %3").arg(Helper::applicationName, Helper::duplicate, mpLibraryTreeItem->getNameStructure()));
mpNameLabel = new Label(Helper::name);
mpNameTextBox = new QLineEdit(mpLibraryTreeItem->getName());
mpNameTextBox->selectAll();
mpPathLabel = new Label(Helper::path);
mpPathTextBox = new QLineEdit(mpLibraryTreeItem->isTopLevel() ? "" : mpLibraryTreeItem->parent()->getNameStructure());
mpPathBrowseButton = new QPushButton(Helper::browse);
mpPathBrowseButton->setAutoDefault(false);
connect(mpPathBrowseButton, SIGNAL(clicked()), SLOT(browsePath()));
// Create the buttons
mpOkButton = new QPushButton(Helper::ok);
mpOkButton->setAutoDefault(true);
connect(mpOkButton, SIGNAL(clicked()), SLOT(duplicateClass()));
mpCancelButton = new QPushButton(Helper::cancel);
mpCancelButton->setAutoDefault(false);
connect(mpCancelButton, SIGNAL(clicked()), SLOT(reject()));
// create buttons box
mpButtonBox = new QDialogButtonBox(Qt::Horizontal);
mpButtonBox->addButton(mpOkButton, QDialogButtonBox::ActionRole);
mpButtonBox->addButton(mpCancelButton, QDialogButtonBox::ActionRole);
// Create a layout
QGridLayout *pMainLayout = new QGridLayout;
pMainLayout->setAlignment(Qt::AlignLeft | Qt::AlignTop);
pMainLayout->addWidget(mpNameLabel, 0, 0);
pMainLayout->addWidget(mpNameTextBox, 0, 1, 1, 2);
pMainLayout->addWidget(mpPathLabel, 1, 0);
pMainLayout->addWidget(mpPathTextBox, 1, 1);
pMainLayout->addWidget(mpPathBrowseButton, 1, 2);
pMainLayout->addWidget(new Label(tr("* Note: This operation can take sometime to finish depending on the size of your library.")), 2, 0, 1, 3, Qt::AlignLeft);
pMainLayout->addWidget(mpButtonBox, 3, 0, 1, 3, Qt::AlignRight);
setLayout(pMainLayout);
}
/*!
* \brief DuplicateClassDialog::selectFileType
* Shows a QDialog which asks for a file type for a class.
* \param pLibraryTreeItem - The source LibraryTreeItem.
* \param pParentLibraryTreeItem - The parent LibraryTreeItem where the class will be duplicated.
* \return
*/
DuplicateClassDialog::FileType DuplicateClassDialog::selectFileType(LibraryTreeItem *pLibraryTreeItem,
LibraryTreeItem *pParentLibraryTreeItem)
{
// if the destination package is saved in one file then we always save in one file
if (pLibraryTreeItem->getRestriction() == StringHandler::Package
&& pParentLibraryTreeItem
&& !pParentLibraryTreeItem->isRootItem()
&& pParentLibraryTreeItem->getSaveContentsType() == LibraryTreeItem::SaveInOneFile) {
return OneFile;
} else if (pLibraryTreeItem->getRestriction() == StringHandler::Package) {
// select dialog
QDialog *pSelectFileTypeDialog = new QDialog;
pSelectFileTypeDialog->setAttribute(Qt::WA_DeleteOnClose);
pSelectFileTypeDialog->setWindowTitle(QString("%1 - %2").arg(Helper::applicationName, Helper::question));
// icon
int iconSize = this->style()->pixelMetric(QStyle::PM_MessageBoxIconSize, 0, this);
QIcon tmpIcon = this->style()->standardIcon(QStyle::SP_MessageBoxQuestion, 0, this);
Label *pPixmapLabel = new Label;
pPixmapLabel->setPixmap(tmpIcon.pixmap(iconSize, iconSize));
// text
Label *pTextLabel = new Label(tr("Select file type for %1").arg(pLibraryTreeItem->getNameStructure()));
// buttons
QSignalMapper signalMapper;
// Keep structure button
QPushButton *pKeepStructureButton = new QPushButton(tr("Keep Structure"));
pKeepStructureButton->setToolTip(tr("Keeps the same file type structure for the package and its contents recursively."));
pKeepStructureButton->setAutoDefault(true);
connect(pKeepStructureButton, SIGNAL(clicked()), &signalMapper, SLOT(map()));
// One File button
QPushButton *pOneFileButton = new QPushButton(tr("One File"));
pOneFileButton->setToolTip(tr("Stores the package and all its contents in one file."));
pOneFileButton->setAutoDefault(false);
connect(pOneFileButton, SIGNAL(clicked()), pSelectFileTypeDialog, SLOT(reject()));
// Directory button
QPushButton *pDirectoryButton = new QPushButton(tr("Directory"));
pDirectoryButton->setToolTip(tr("Creates a directory for the package."));
pDirectoryButton->setAutoDefault(false);
connect(pDirectoryButton, SIGNAL(clicked()), pSelectFileTypeDialog, SLOT(accept()));
// Directories button
QPushButton *pDirectoriesForAllButton = new QPushButton(tr("Directories For All"));
pDirectoriesForAllButton->setToolTip(tr("Creates the directories for the package and its contents recursively."));
pDirectoriesForAllButton->setAutoDefault(false);
connect(pDirectoriesForAllButton, SIGNAL(clicked()), &signalMapper, SLOT(map()));
// set signal mapping
signalMapper.setMapping(pDirectoriesForAllButton, 2);
signalMapper.setMapping(pKeepStructureButton, 3);
connect(&signalMapper, SIGNAL(mapped(int)), pSelectFileTypeDialog, SLOT(done(int)));
// layout the buttons
QDialogButtonBox *pButtonBox = new QDialogButtonBox;
pButtonBox->addButton(pKeepStructureButton, QDialogButtonBox::ActionRole);
pButtonBox->addButton(pOneFileButton, QDialogButtonBox::ActionRole);
pButtonBox->addButton(pDirectoryButton, QDialogButtonBox::ActionRole);
pButtonBox->addButton(pDirectoriesForAllButton, QDialogButtonBox::ActionRole);
// horizontal layout
QHBoxLayout *pHorizontalLayout = new QHBoxLayout;
pHorizontalLayout->addWidget(pPixmapLabel, 0, Qt::AlignTop);
pHorizontalLayout->addWidget(pTextLabel, 0, Qt::AlignTop);
// main layout
QGridLayout *pMainLayout = new QGridLayout;
pMainLayout->addLayout(pHorizontalLayout, 0, 0, Qt::AlignTop | Qt::AlignLeft);
pMainLayout->addWidget(pButtonBox, 1, 0, Qt::AlignRight | Qt::AlignBottom);
pSelectFileTypeDialog->setLayout(pMainLayout);
int answer = pSelectFileTypeDialog->exec();
switch (answer) {
case 1:
return Directory;
case 2:
return Directories;
case 3:
return KeepStructure;
case 0:
default:
return OneFile;
}
} else {
return OneFile;
}
}
/*!
* \brief DuplicateClassDialog::setSaveContentsTypeAsFolderStructure
* Set LibraryTreeItem::SaveFolderStructure for all nested packages.
* \param pLibraryTreeItem
*/
void DuplicateClassDialog::setSaveContentsTypeAsFolderStructure(LibraryTreeItem *pLibraryTreeItem)
{
for (int i = 0; i < pLibraryTreeItem->childrenSize(); i++) {
LibraryTreeItem *pChildLibraryTreeItem = pLibraryTreeItem->child(i);
if (pChildLibraryTreeItem->getRestriction() == StringHandler::Package) {
pChildLibraryTreeItem->setSaveContentsType(LibraryTreeItem::SaveFolderStructure);
pChildLibraryTreeItem->setClassText(QString("within %1;\npackage %2\nend %2;").arg(pLibraryTreeItem->getNameStructure(),
pChildLibraryTreeItem->getName()));
}
setSaveContentsTypeAsFolderStructure(pChildLibraryTreeItem);
}
}
/*!
* \brief DuplicateClassDialog::duplicateClassHelper
* Duplicates the classes recursively. Performs the diff to preserve formatting.
* \param pDestinationLibraryTreeItem
* \param pSourceLibraryTreeItem
* \param fileType
*/
void DuplicateClassDialog::duplicateClassHelper(LibraryTreeItem *pDestinationLibraryTreeItem, LibraryTreeItem *pSourceLibraryTreeItem,
FileType fileType)
{
QString classText;
if (pDestinationLibraryTreeItem->parent()->getSaveContentsType() == LibraryTreeItem::SaveInOneFile
&& pSourceLibraryTreeItem->getSaveContentsType() == LibraryTreeItem::SaveFolderStructure && fileType == OneFile) {
classText = MainWindow::instance()->getOMCProxy()->listFile(pDestinationLibraryTreeItem->getNameStructure(), false);
} else if (!pDestinationLibraryTreeItem->parent()->isRootItem()
&& pDestinationLibraryTreeItem->parent()->getSaveContentsType() == LibraryTreeItem::SaveInOneFile) {
classText = MainWindow::instance()->getOMCProxy()->list(pDestinationLibraryTreeItem->getNameStructure());
} else if (fileType == KeepStructure) {
classText = MainWindow::instance()->getOMCProxy()->listFile(pDestinationLibraryTreeItem->getNameStructure(),
pSourceLibraryTreeItem->getSaveContentsType() == LibraryTreeItem::SaveInOneFile);
} else {
classText = MainWindow::instance()->getOMCProxy()->listFile(pDestinationLibraryTreeItem->getNameStructure(), fileType == OneFile);
}
QString beforeClassText = pSourceLibraryTreeItem->getClassText(MainWindow::instance()->getLibraryWidget()->getLibraryTreeModel());
beforeClassText = StringHandler::removeLeadingSpaces(beforeClassText);
/* Ticket #3912
* In order to preserve the formatting we use OMCProxy::diffModelicaFileListings()
*/
classText = MainWindow::instance()->getOMCProxy()->diffModelicaFileListings(beforeClassText, classText);
// set the class text
pDestinationLibraryTreeItem->setClassText(classText);
if (pDestinationLibraryTreeItem->parent()->getSaveContentsType() == LibraryTreeItem::SaveInOneFile
&& pSourceLibraryTreeItem->getSaveContentsType() == LibraryTreeItem::SaveFolderStructure && fileType == OneFile) {
/* Case 8, 10
* If the save type is save in one file and source package is saved as folder structure
* then we need to flat the package to one file and then insert it inside the package if within is specified.
*/
classText = StringHandler::removeLine(classText, QString("within %1;").arg(pDestinationLibraryTreeItem->parent()->getNameStructure()));
// remove the whitespaces around the string if any
classText = classText.trimmed();
QString childClassText;
folderToOneFilePackage(pDestinationLibraryTreeItem, pSourceLibraryTreeItem, &childClassText);
// insert the class in package
classText = StringHandler::insertClassAtPosition(classText, childClassText, 1, 2);
// set the class text
pDestinationLibraryTreeItem->setClassText(classText);
if (!pDestinationLibraryTreeItem->parent()->isRootItem()) {
insertClassInOneFilePackage(pDestinationLibraryTreeItem);
}
} else if (!pDestinationLibraryTreeItem->parent()->isRootItem()
&& pDestinationLibraryTreeItem->parent()->getSaveContentsType() == LibraryTreeItem::SaveInOneFile) {
/* Case 2
* If the save type is save in one file then we need to insert the new class inside the package.
*/
insertClassInOneFilePackage(pDestinationLibraryTreeItem);
}
if (fileType == Directory || fileType == Directories
|| (fileType == KeepStructure && pSourceLibraryTreeItem->getSaveContentsType() == LibraryTreeItem::SaveFolderStructure)) {
for (int i = 0 ; i < pDestinationLibraryTreeItem->childrenSize() ; i++) {
LibraryTreeItem *pDestinationChildLibraryTreeItem = pDestinationLibraryTreeItem->childAt(i);
LibraryTreeItem *pSourceChildLibraryTreeItem = pSourceLibraryTreeItem->childAt(i);
// save file type
FileType saveFileType = fileType;
if (fileType == Directory) {
fileType = selectFileType(pDestinationChildLibraryTreeItem);
}
if (fileType == OneFile || pDestinationChildLibraryTreeItem->getRestriction() != StringHandler::Package) {
pDestinationChildLibraryTreeItem->setSaveContentsType(LibraryTreeItem::SaveInOneFile);
} else if (fileType == KeepStructure) {
pDestinationChildLibraryTreeItem->setSaveContentsType(pSourceChildLibraryTreeItem->getSaveContentsType());
} else {
pDestinationChildLibraryTreeItem->setSaveContentsType(LibraryTreeItem::SaveFolderStructure);
}
duplicateClassHelper(pDestinationChildLibraryTreeItem, pSourceChildLibraryTreeItem, fileType);
// restore file type so the next iteration can use the correct file type.
fileType = saveFileType;
}
}
}
/*!
* \brief DuplicateClassDialog::syncDuplicatedModelWithOMC
* Send the duplicated model text to OMC to sync the line numbers.
* \param pLibraryTreeItem
*/
void DuplicateClassDialog::syncDuplicatedModelWithOMC(LibraryTreeItem *pLibraryTreeItem)
{
/* Ticket #3793
* We need to call loadString with the new text of the class so that the getClassInformation returns the correct line number information.
* Otherwise we have the problems like the one reported in Ticket #3793.
*/
QString filename = pLibraryTreeItem->getNameStructure();
LibraryTreeModel *pLibraryTreeModel = MainWindow::instance()->getLibraryWidget()->getLibraryTreeModel();
if (!pLibraryTreeItem->parent()->isRootItem() && pLibraryTreeItem->parent()->getSaveContentsType() == LibraryTreeItem::SaveInOneFile) {
LibraryTreeItem *pParentLibraryTreeItem = pLibraryTreeModel->getContainingFileParentLibraryTreeItem(pLibraryTreeItem);
filename = pParentLibraryTreeItem->isFilePathValid() ? pParentLibraryTreeItem->getFileName() : pParentLibraryTreeItem->getNameStructure();
pLibraryTreeItem = pParentLibraryTreeItem;
}
QString classText = pLibraryTreeItem->getClassText(pLibraryTreeModel);
// load the string in omc so that OMC and OMEdit have same line numbers.
MainWindow::instance()->getOMCProxy()->loadString(classText, filename, Helper::utf8, false, false);
if (pLibraryTreeItem->getSaveContentsType() == LibraryTreeItem::SaveInOneFile) {
pLibraryTreeModel->updateChildLibraryTreeItemClassText(pLibraryTreeItem, classText, pLibraryTreeItem->getFileName());
} else {
for (int i = 0 ; i < pLibraryTreeItem->childrenSize() ; i++) {
syncDuplicatedModelWithOMC(pLibraryTreeItem->childAt(i));
}
}
pLibraryTreeItem->updateClassInformation();
}