/
pmrworkspaceswindowsynchronizedialog.cpp
1071 lines (769 loc) · 40.4 KB
/
pmrworkspaceswindowsynchronizedialog.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
/*******************************************************************************
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/>.
*******************************************************************************/
//==============================================================================
// PMR Workspaces window synchronise dialog
//==============================================================================
#include "borderedwidget.h"
#include "cellmlfilemanager.h"
#include "corecliutils.h"
#include "coreguiutils.h"
#include "file.h"
#include "i18ninterface.h"
#include "pmrworkspace.h"
#include "pmrworkspaceswindowsynchronizedialog.h"
#include "splitterwidget.h"
#include "toolbarwidget.h"
#include "webviewerwidget.h"
//==============================================================================
#include <QCheckBox>
#include <QApplication>
#include <QDesktopWidget>
#include <QDialogButtonBox>
#include <QDir>
#include <QKeyEvent>
#include <QLabel>
#include <QListView>
#include <QPushButton>
#include <QSettings>
#include <QSortFilterProxyModel>
#include <QStandardItemModel>
#include <QTextEdit>
#include <QTimer>
#include <QVBoxLayout>
#include <QWebElement>
#include <QWebFrame>
#include <QWebView>
//==============================================================================
#include "diff_match_patch.h"
//==============================================================================
#include "xdiff.h"
//==============================================================================
namespace OpenCOR {
namespace PMRWorkspacesWindow {
//==============================================================================
PmrWorkspacesWindowSynchronizeDialogItem::PmrWorkspacesWindowSynchronizeDialogItem(PMRSupport::PmrWorkspaceFileNode *pFileNode) :
QStandardItem(pFileNode->path()),
mFileNode(pFileNode)
{
}
//==============================================================================
PMRSupport::PmrWorkspaceFileNode * PmrWorkspacesWindowSynchronizeDialogItem::fileNode() const
{
// Return our file node
return mFileNode;
}
//==============================================================================
static const auto SettingsCellmlTextFormatSupport = QStringLiteral("CellmlTextFormatSupport");
static const auto SettingsHorizontalSplitterSizes = QStringLiteral("HorizontalSplitterSizes");
static const auto SettingsVerticalSplitterSizes = QStringLiteral("VerticalSplitterSizes");
//==============================================================================
PmrWorkspacesWindowSynchronizeDialog::PmrWorkspacesWindowSynchronizeDialog(const QString &pSettingsGroup,
PMRSupport::PmrWorkspace *pWorkspace,
QTimer *pTimer,
QWidget *pParent) :
Core::Dialog(pParent),
mSettingsGroup(pSettingsGroup),
mWorkspace(pWorkspace),
mSha1s(QMap<QString, QString>()),
mDiffHtmls(QMap<QString, QString>()),
mCellmlDiffHtmls(QMap<QString, QString>()),
mPreviouslySelectedIndexes(QModelIndexList()),
mInvalidCellmlCode(QStringList()),
mNeedUpdateDiffInformation(false)
{
// Set both our object name and title
setObjectName("PmrWorkspacesWindowSynchronizeDialog");
setWindowTitle(tr("Synchronise With PMR"));
// Create and set our vertical layout
QVBoxLayout *layout = new QVBoxLayout(this);
setLayout(layout);
// Create both our horizontal and vertical splitters
mHorizontalSplitter = new Core::SplitterWidget(this);
mVerticalSplitter = new Core::SplitterWidget(Qt::Vertical, mHorizontalSplitter);
// Create our message-related widget, populate it, and add it to our
// vertical splitter
QWidget *messageWidget = new QWidget(mVerticalSplitter);
QVBoxLayout *messageLayout = new QVBoxLayout(messageWidget);
int margin;
messageLayout->getContentsMargins(0, 0, 0, &margin);
int halfMargin = margin >> 1;
messageLayout->setContentsMargins(0, 0, halfMargin, halfMargin);
messageWidget->setLayout(messageLayout);
QLabel *messageLabel = new QLabel(tr("Message:"), messageWidget);
QFont newFont = messageLabel->font();
newFont.setBold(true);
messageLabel->setFont(newFont);
mMessageValue = new QTextEdit(messageWidget);
messageLayout->addWidget(messageLabel);
messageLayout->addWidget(mMessageValue);
mVerticalSplitter->addWidget(messageWidget);
// Create our changes-related widget, populate it, and add it to our
// vertical splitter
QWidget *changesWidget = new QWidget(mVerticalSplitter);
QVBoxLayout *changesLayout = new QVBoxLayout(changesWidget);
changesLayout->setContentsMargins(0, halfMargin, halfMargin, 0);
changesWidget->setLayout(changesLayout);
QLabel *changesLabel = new QLabel(changesWidget);
changesLabel->setFont(newFont);
mChangesValue = new QListView(changesWidget);
#ifdef Q_OS_MAC
mChangesValue->setAttribute(Qt::WA_MacShowFocusRect, false);
#endif
mChangesValue->setSelectionMode(QAbstractItemView::ExtendedSelection);
mSelectAllChangesCheckBox = new QCheckBox(tr("Select all the changes"), changesWidget);
mSelectAllChangesCheckBox->setTristate(true);
changesLayout->addWidget(changesLabel);
changesLayout->addWidget(mChangesValue);
changesLayout->addWidget(mSelectAllChangesCheckBox);
mVerticalSplitter->addWidget(changesWidget);
// Customise our vertical splitter and add it to our horizontal splitter
mVerticalSplitter->setCollapsible(0, false);
mVerticalSplitter->setCollapsible(1, false);
mVerticalSplitter->setStretchFactor(1, 1);
mHorizontalSplitter->addWidget(mVerticalSplitter);
// Create our Web viewer and add it to our horizontal splitter
QWidget *webViewerWidget = new QWidget(mVerticalSplitter);
QVBoxLayout *webViewerLayout = new QVBoxLayout(webViewerWidget);
webViewerLayout->setContentsMargins(halfMargin, 0, 0, 0);
webViewerWidget->setLayout(webViewerLayout);
Core::ToolBarWidget *webViewerToolBarWidget = new Core::ToolBarWidget();
QLabel *webViewerLabel = new QLabel(tr("Changes:"), webViewerWidget);
QWidget *webViewerSpacer = new QWidget(webViewerToolBarWidget);
QAction *webViewerNormalSizeAction = Core::newAction(QIcon(":/oxygen/actions/zoom-original.png"), webViewerToolBarWidget);
QAction *webViewerZoomInAction = Core::newAction(QIcon(":/oxygen/actions/zoom-in.png"), webViewerToolBarWidget);
QAction *webViewerZoomOutAction = Core::newAction(QIcon(":/oxygen/actions/zoom-out.png"), webViewerToolBarWidget);
mWebViewerCellmlTextFormatAction = Core::newAction(QIcon(":/CellMLSupport/logo.png"), webViewerToolBarWidget);
webViewerLabel->setAlignment(Qt::AlignBottom);
webViewerLabel->setFont(newFont);
webViewerSpacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
I18nInterface::retranslateAction(mWebViewerCellmlTextFormatAction, tr("CellML Text Format"),
tr("Try to use the CellML Text format whenever possible"));
I18nInterface::retranslateAction(webViewerNormalSizeAction, tr("Normal Size"),
tr("Reset the size of the changes"));
I18nInterface::retranslateAction(webViewerZoomInAction, tr("Zoom In"),
tr("Zoom in the changes"));
I18nInterface::retranslateAction(webViewerZoomOutAction, tr("Zoom Out"),
tr("Zoom out the changes"));
mWebViewerCellmlTextFormatAction->setCheckable(true);
webViewerToolBarWidget->addWidget(webViewerLabel);
webViewerToolBarWidget->addWidget(webViewerSpacer);
webViewerToolBarWidget->addAction(mWebViewerCellmlTextFormatAction);
webViewerToolBarWidget->addSeparator();
webViewerToolBarWidget->addAction(webViewerNormalSizeAction);
webViewerToolBarWidget->addSeparator();
webViewerToolBarWidget->addAction(webViewerZoomInAction);
webViewerToolBarWidget->addAction(webViewerZoomOutAction);
mWebViewer = new WebViewerWidget::WebViewerWidget(mHorizontalSplitter);
mWebViewer->webView()->setContextMenuPolicy(Qt::CustomContextMenu);
mWebViewer->setOverrideCursor(true);
webViewerLayout->addWidget(webViewerToolBarWidget);
webViewerLayout->addWidget(new Core::BorderedWidget(mWebViewer,
true, true, true, true));
mHorizontalSplitter->addWidget(webViewerWidget);
// Now, we can customise our horizontal splitter and add it to our layout
mHorizontalSplitter->setCollapsible(0, false);
mHorizontalSplitter->setCollapsible(1, false);
mHorizontalSplitter->setStretchFactor(1, 1);
layout->addWidget(mHorizontalSplitter);
// Retrieve our user's settings
QSettings settings;
settings.beginGroup(mSettingsGroup);
settings.beginGroup(objectName());
mWebViewer->loadSettings(&settings);
mWebViewerCellmlTextFormatAction->setChecked(settings.value(SettingsCellmlTextFormatSupport, true).toBool());
mHorizontalSplitter->setSizes(qVariantListToIntList(settings.value(SettingsHorizontalSplitterSizes).toList()));
mVerticalSplitter->setSizes(qVariantListToIntList(settings.value(SettingsVerticalSplitterSizes,
QVariantList() << 222 << 555).toList()));
settings.endGroup();
settings.endGroup();
// Add some dialog buttons
mButtonBox = new QDialogButtonBox(this);
mButtonBox->setStandardButtons(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
layout->addWidget(mButtonBox);
// Populate ourselves with the list of files that have changed
mModel = new QStandardItemModel(this);
mProxyModel = new QSortFilterProxyModel(this);
mProxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
mProxyModel->setSourceModel(mModel);
mChangesValue->setModel(mProxyModel);
populateModel(pWorkspace->rootFileNode());
mProxyModel->sort(0);
changesLabel->setText((mModel->rowCount() == 1)?
tr("1 change:"):
tr("%1 changes:").arg(mModel->rowCount()));
mSelectAllChangesCheckBox->setVisible(mModel->rowCount() != 1);
// Connect some signals
connect(pTimer, SIGNAL(timeout()),
this, SLOT(refreshChanges()));
connect(mMessageValue, SIGNAL(textChanged()),
this, SLOT(updateOkButton()));
connect(mModel, SIGNAL(itemChanged(QStandardItem *)),
this, SLOT(updateSelectAllChangesCheckBox(QStandardItem *)));
connect(mSelectAllChangesCheckBox, SIGNAL(toggled(bool)),
this, SLOT(updateOkButton()));
connect(mSelectAllChangesCheckBox, SIGNAL(clicked(bool)),
this, SLOT(selectAllChangesCheckBoxClicked()));
connect(mButtonBox, SIGNAL(accepted()),
this, SLOT(acceptSynchronization()));
connect(mButtonBox, SIGNAL(rejected()),
this, SLOT(reject()));
connect(mChangesValue->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
this, SLOT(updateDiffInformation()));
connect(mWebViewerCellmlTextFormatAction, SIGNAL(toggled(bool)),
this, SLOT(updateDiffInformation()));
connect(webViewerNormalSizeAction, SIGNAL(triggered(bool)),
mWebViewer, SLOT(resetZoom()));
connect(webViewerZoomInAction, SIGNAL(triggered(bool)),
mWebViewer, SLOT(zoomIn()));
connect(webViewerZoomOutAction, SIGNAL(triggered(bool)),
mWebViewer, SLOT(zoomOut()));
// Retrieve our diff template
Core::readFileContentsFromFile(":/PMRWorkspacesWindow/diff.html", mDiffTemplate);
// Select our first changes
mChangesValue->setCurrentIndex(mProxyModel->index(0, 0));
// Initialise (update) the checked state of our Select All check box
updateSelectAllChangesCheckBox();
// Set our minimum size and adjust our initial size
// Note: the size adjustment is just so that it looks 'better' the very
// first time round. From there, it will indeed be up to the user to
// decide about the (position and) size of the dialog...
QSize minimumSize = Core::minimumWidgetSize(this);
setMinimumSize(minimumSize);
if (!hasPositionAndSize()) {
QSize size = minimumSize;
size.setWidth(qMax(size.width(), int(0.85*size.height())));
resize(size);
}
}
//==============================================================================
PmrWorkspacesWindowSynchronizeDialog::~PmrWorkspacesWindowSynchronizeDialog()
{
// Keep track of our user's settings
QSettings settings;
settings.beginGroup(mSettingsGroup);
settings.beginGroup(objectName());
mWebViewer->saveSettings(&settings);
settings.setValue(SettingsCellmlTextFormatSupport,
mWebViewerCellmlTextFormatAction->isChecked());
settings.setValue(SettingsHorizontalSplitterSizes,
qIntListToVariantList(mHorizontalSplitter->sizes()));
settings.setValue(SettingsVerticalSplitterSizes,
qIntListToVariantList(mVerticalSplitter->sizes()));
settings.endGroup();
settings.endGroup();
}
//==============================================================================
void PmrWorkspacesWindowSynchronizeDialog::keyPressEvent(QKeyEvent *pEvent)
{
// Check whether we are trying to quick synchronise
if ( !(pEvent->modifiers() & Qt::ShiftModifier)
&& (pEvent->modifiers() & Qt::ControlModifier)
&& !(pEvent->modifiers() & Qt::AltModifier)
&& !(pEvent->modifiers() & Qt::MetaModifier)
&& (pEvent->key() == Qt::Key_Return)) {
// Pretend that we clicked on the Ok button, if possible
if (mButtonBox->button(QDialogButtonBox::Ok)->isEnabled())
acceptSynchronization();
pEvent->accept();
} else {
// Default handling of the event
Core::Dialog::keyPressEvent(pEvent);
}
}
//==============================================================================
PmrWorkspacesWindowSynchronizeDialogItems PmrWorkspacesWindowSynchronizeDialog::populateModel(PMRSupport::PmrWorkspaceFileNode *pFileNode)
{
// List all the files that have changed
PmrWorkspacesWindowSynchronizeDialogItems res = PmrWorkspacesWindowSynchronizeDialogItems();
foreach (PMRSupport::PmrWorkspaceFileNode *fileNode, pFileNode->children()) {
if (fileNode->hasChildren()) {
// This is a folder, so populate ourselves with its children
res << populateModel(fileNode);
} else {
// This is a file, so check whether it has changes
QChar status = fileNode->status().second;
if ((status != '\0') && (status != ' ')) {
// This is a changed file, so check whether we already know
// about it and, if so, whether its SHA-1 is still the same and
// if that's not the case then reset a few things
PmrWorkspacesWindowSynchronizeDialogItem *fileItem = 0;
QString fileName = fileNode->path();
QString sha1 = Core::File::sha1(fileName);
for (int i = 0, iMax = mModel->invisibleRootItem()->rowCount(); i < iMax; ++i) {
PmrWorkspacesWindowSynchronizeDialogItem *item = static_cast<PmrWorkspacesWindowSynchronizeDialogItem *>(mModel->invisibleRootItem()->child(i));
if (item->fileNode() == fileNode) {
fileItem = item;
if (sha1.compare(mSha1s.value(fileName))) {
mSha1s.insert(fileName, sha1);
mDiffHtmls.remove(fileName);
mCellmlDiffHtmls.remove(fileName);
if (mChangesValue->selectionModel()->isSelected(mProxyModel->mapFromSource(item->index())))
mNeedUpdateDiffInformation = true;
}
break;
}
}
// Create a new item, if needed
if (!fileItem) {
fileItem = new PmrWorkspacesWindowSynchronizeDialogItem(fileNode);
fileItem->setCheckable(true);
fileItem->setCheckState(Qt::Checked);
fileItem->setEditable(false);
fileItem->setToolTip(fileName);
mModel->appendRow(fileItem);
mSha1s.insert(fileName, sha1);
}
res << fileItem;
}
}
}
return res;
}
//==============================================================================
QString PmrWorkspacesWindowSynchronizeDialog::message() const
{
// Return the commit message
return mMessageValue->toPlainText().trimmed();
}
//==============================================================================
QStringList PmrWorkspacesWindowSynchronizeDialog::fileNames() const
{
// Return our file names
QStringList res = QStringList();
for (int i = 0, iMax = mModel->invisibleRootItem()->rowCount(); i < iMax; ++i) {
QStandardItem *fileItem = mModel->invisibleRootItem()->child(i);
if (fileItem->checkState() == Qt::Checked)
res << mModel->invisibleRootItem()->child(i)->text();
}
return res;
}
//==============================================================================
void PmrWorkspacesWindowSynchronizeDialog::refreshChanges()
{
// Keep track of our existing items
PmrWorkspacesWindowSynchronizeDialogItems oldItems = PmrWorkspacesWindowSynchronizeDialogItems();
for (int i = 0, iMax = mModel->invisibleRootItem()->rowCount(); i < iMax; ++i)
oldItems << static_cast<PmrWorkspacesWindowSynchronizeDialogItem *>(mModel->invisibleRootItem()->child(i));
// Update our model by (re)populating it
// Note: we don't need to refresh the status of our workspace since it has
// been done in PmrWorkspacesWindowWidget::refreshWorkspace()...
PmrWorkspacesWindowSynchronizeDialogItems newItems = populateModel(mWorkspace->rootFileNode());
// Delete old unused items
foreach (PmrWorkspacesWindowSynchronizeDialogItem *oldItem, oldItems) {
if (!newItems.contains(oldItem)) {
mDiffHtmls.remove(oldItem->text());
mCellmlDiffHtmls.remove(oldItem->text());
mModel->invisibleRootItem()->removeRow(oldItem->row());
}
}
// Check whether we still have at least one item selected and, if not,
// select the 'new' first one
if (mChangesValue->selectionModel()->selectedIndexes().isEmpty())
mChangesValue->setCurrentIndex(mProxyModel->index(0, 0));
// Update our diff information, if needed
if (mNeedUpdateDiffInformation) {
mNeedUpdateDiffInformation = false;
updateDiffInformation();
}
}
//==============================================================================
void PmrWorkspacesWindowSynchronizeDialog::updateSelectAllChangesCheckBox(QStandardItem *pItem)
{
// Un/check the selected items, if any
// Note: we temporally disable the handling of the itemChanged() signal
// since we 'manually' set everything ourselves...
if (pItem) {
disconnect(mModel, SIGNAL(itemChanged(QStandardItem *)),
this, SLOT(updateSelectAllChangesCheckBox(QStandardItem *)));
foreach (const QModelIndex &fileIndex, mChangesValue->selectionModel()->selectedIndexes())
mModel->itemFromIndex(mProxyModel->mapToSource(fileIndex))->setCheckState(pItem->checkState());
connect(mModel, SIGNAL(itemChanged(QStandardItem *)),
this, SLOT(updateSelectAllChangesCheckBox(QStandardItem *)));
}
// Update the checked state of our Select All check box
int nbOfCheckedFiles = 0;
for (int i = 0, iMax = mModel->invisibleRootItem()->rowCount(); i < iMax; ++i)
nbOfCheckedFiles += mModel->invisibleRootItem()->child(i)->checkState() == Qt::Checked;
mSelectAllChangesCheckBox->setCheckState(nbOfCheckedFiles?
(nbOfCheckedFiles == mModel->rowCount())?
Qt::Checked:
Qt::PartiallyChecked:
Qt::Unchecked);
}
//==============================================================================
void PmrWorkspacesWindowSynchronizeDialog::selectAllChangesCheckBoxClicked()
{
// If our checked state is partially checked, then we want to make it fully
// so
if (mSelectAllChangesCheckBox->checkState() == Qt::PartiallyChecked)
mSelectAllChangesCheckBox->setCheckState(Qt::Checked);
// Un/select all the files
Qt::CheckState checkState = mSelectAllChangesCheckBox->isChecked()?Qt::Checked:Qt::Unchecked;
for (int i = 0, iMax = mModel->invisibleRootItem()->rowCount(); i < iMax; ++i)
mModel->invisibleRootItem()->child(i)->setCheckState(checkState);
}
//==============================================================================
void PmrWorkspacesWindowSynchronizeDialog::updateOkButton()
{
// Update the state of our Ok button
mButtonBox->button(QDialogButtonBox::Ok)->setEnabled( !message().isEmpty()
&& (mSelectAllChangesCheckBox->checkState() != Qt::Unchecked));
}
//==============================================================================
void PmrWorkspacesWindowSynchronizeDialog::acceptSynchronization()
{
// Confirm that we accept the synchronisation
done(QMessageBox::Ok);
}
//==============================================================================
bool PmrWorkspacesWindowSynchronizeDialog::cellmlText(const QString &pFileName,
QString &pCellmlText)
{
// Try to generate the CellML Text version of the given CellML file
// Note: we want to run the CLI version of OpenCOR, so on Windows this means
// that we need to replace the program's file extension from ".exe" to
// ".com"...
#ifdef Q_OS_WIN
static const QRegularExpression ExeExtensionRegEx = QRegularExpression("\\.exe$");
#endif
QString program = qApp->applicationFilePath();
#ifdef Q_OS_WIN
program.replace(ExeExtensionRegEx, ".com");
#endif
if (!Core::exec(program,
QStringList() << "-c"
<< "CellMLTextView::import"
<< pFileName,
pCellmlText)) {
return true;
} else {
return false;
}
}
//==============================================================================
int xdiffCallback(void *data, mmbuffer_t *pBuffer, int pBufferSize)
{
// Add the given buffer to the given data
for (int i = 0; i < pBufferSize; ++i)
*static_cast<QString *>(data) += QString(pBuffer[i].ptr).left(pBuffer[i].size);
return 0;
}
//==============================================================================
static const auto Row = QStringLiteral(" <tr class=\"%1\">\n"
" <td class=\"linenumber shrink rightborder\">\n"
" <code>%2</code>\n"
" </td>\n"
" <td class=\"linenumber shrink rightborder\">\n"
" <code>%3</code>\n"
" </td>\n"
" <td class=\"tag shrink\">\n"
" <code>%4</code>\n"
" </td>\n"
" <td class=\"expand\">\n"
" <code>%5</code>\n"
" </td>\n"
" </tr>\n");
//==============================================================================
PmrWorkspacesWindowSynchronizeDialog::DifferenceData PmrWorkspacesWindowSynchronizeDialog::differenceData(const QString &pOperation,
const QString &pRemoveLineNumber,
const QString &pAddLineNumber,
const QChar &pTag,
const QString &pDifference)
{
PmrWorkspacesWindowSynchronizeDialog::DifferenceData res = PmrWorkspacesWindowSynchronizeDialog::DifferenceData();
res.operation = pOperation;
res.removeLineNumber = pRemoveLineNumber;
res.addLineNumber = pAddLineNumber;
res.tag = pTag;
res.difference = pDifference;
return res;
}
//==============================================================================
QString PmrWorkspacesWindowSynchronizeDialog::diffHtml(DifferencesData &pDifferencesData)
{
// Make sure that we have some differences data
if (pDifferencesData.isEmpty()) {
return QString();
} else {
// Highlight the differences within our differences data' difference
// field
QString oldString = QString();
QString newString = QString();
foreach (const DifferenceData &differenceData, pDifferencesData) {
if (differenceData.tag == '+')
newString += differenceData.difference+"\n";
else
oldString += differenceData.difference+"\n";
}
typedef diff_match_patch<std::wstring> DiffMatchPatch;
DiffMatchPatch diffMatchPatch;
DiffMatchPatch::Diffs diffs = diffMatchPatch.diff_main(oldString.toStdWString(), newString.toStdWString());
diffMatchPatch.diff_cleanupEfficiency(diffs);
QString oldDiffString = QString();
QString newDiffString = QString();
for (DiffMatchPatch::Diffs::const_iterator diffIterator = diffs.begin(), endDiffIterator = diffs.end();
diffIterator != endDiffIterator; ++diffIterator) {
QString text = QString::fromStdWString((*diffIterator).text).toHtmlEscaped()
.replace(' ', " ");
switch ((*diffIterator).operation) {
case DiffMatchPatch::EQUAL:
oldDiffString += text;
newDiffString += text;
break;
case DiffMatchPatch::INSERT:
newDiffString += oldString.isEmpty()?
text:
text.contains('\n')?
text.endsWith('\n')?
QString("<span class=\"add\">%1</span>\n").arg(text.remove('\n')):
QString("<span class=\"add\">%1</span>").arg(text.replace('\n', "</span>\n<span class=\"add\">")):
QString("<span class=\"add\">%1</span>").arg(text);
break;
case DiffMatchPatch::DELETE:
oldDiffString += newString.isEmpty()?
text:
text.contains('\n')?
text.endsWith('\n')?
QString("<span class=\"remove\">%1</span>\n").arg(text.remove('\n')):
QString("<span class=\"remove\">%1</span>").arg(text.replace('\n', "</span>\n<span class=\"remove\">")):
QString("<span class=\"remove\">%1</span>").arg(text);
break;
}
}
// Generate the HTML code for any differences data that we may have been
// given
QString html = QString();
QStringList oldDiffStrings = oldDiffString.split('\n');
QStringList newDiffStrings = newDiffString.split('\n');
int addLineNumber = -1;
int removeLineNumber = -1;
foreach (const DifferenceData &differenceData, pDifferencesData) {
html += Row.arg(differenceData.operation,
differenceData.removeLineNumber,
differenceData.addLineNumber,
differenceData.tag,
(differenceData.tag == '+')?
newDiffStrings[++addLineNumber]:
oldDiffStrings[++removeLineNumber]);
}
pDifferencesData.clear();
return html;
}
}
//==============================================================================
QString PmrWorkspacesWindowSynchronizeDialog::diffHtml(const QString &pOld,
const QString &pNew)
{
// Retrieve the UNIX-like differences between the given old and new strings
mmfile_t oldBlock;
mmfile_t newBlock;
QByteArray oldByteArray = pOld.toUtf8();
QByteArray newByteArray = pNew.toUtf8();
xdl_init_mmfile(&oldBlock, oldByteArray.size(), XDL_MMF_ATOMIC);
xdl_init_mmfile(&newBlock, newByteArray.size(), XDL_MMF_ATOMIC);
memcpy(xdl_mmfile_writeallocate(&oldBlock, oldByteArray.size()),
oldByteArray.constData(), oldByteArray.size());
memcpy(xdl_mmfile_writeallocate(&newBlock, newByteArray.size()),
newByteArray.constData(), newByteArray.size());
xpparam_t parameters;
xdemitconf_t context;
QString differences = QString();
xdemitcb_t callback;
parameters.flags = 0;
context.ctxlen = 3;
callback.priv = &differences;
callback.outf = xdiffCallback;
xdl_diff(&oldBlock, &newBlock, ¶meters, &context, &callback);
xdl_free_mmfile(&oldBlock);
xdl_free_mmfile(&newBlock);
// Generate the HTML code for those differences
static const QRegularExpression BeforeAddLineNumberRegEx = QRegularExpression("[^\\+]*\\+");
static const QRegularExpression BeforeAddNumberOfLinesRegEx = QRegularExpression("[^,]*,");
static const QRegularExpression BeforeRemoveLineNumberRegEx = QRegularExpression("[^-]*-");
static const QRegularExpression AfterLineNumberRegEx = QRegularExpression(",.*");
static const QRegularExpression AfterNumberOfLinesRegEx = QRegularExpression(" .*");
QString html = QString();
QStringList differencesList = differences.split('\n');
int differenceNumber = 0;
int differenceMaxNumber = differencesList.count()-1;
int addLineNumber = 0;
int addMaxLineNumber = 0;
int removeLineNumber = 0;
DifferencesData differencesData = DifferencesData();
foreach (const QString &difference, differencesList) {
++differenceNumber;
if (difference.startsWith("@@") && difference.endsWith("@@")) {
addLineNumber = QString(difference).remove(BeforeAddLineNumberRegEx).remove(AfterLineNumberRegEx).toInt()-1;
addMaxLineNumber = addLineNumber+QString(difference).remove(BeforeAddNumberOfLinesRegEx).remove(AfterNumberOfLinesRegEx).toInt();
removeLineNumber = QString(difference).remove(BeforeRemoveLineNumberRegEx).remove(AfterLineNumberRegEx).toInt()-1;
html += Row.arg("header", "...", "...", QString(), difference);
} else {
QString diff = difference;
QChar tag = diff[0];
diff.remove(0, 1);
if (tag == '+') {
++addLineNumber;
differencesData << differenceData((differenceNumber == differenceMaxNumber)?"last add":"add",
QString(), QString::number(addLineNumber),
'+', diff);
} else if (tag == '-') {
++removeLineNumber;
differencesData << differenceData((differenceNumber == differenceMaxNumber)?"last remove":"remove",
QString::number(removeLineNumber), QString(),
'-', diff);
} else if (addLineNumber != addMaxLineNumber) {
// Output any differences data that we may have
html += diffHtml(differencesData);
// Output the current line
++addLineNumber;
++removeLineNumber;
html += Row.arg((differenceNumber == differenceMaxNumber)?"last default":"default",
QString::number(removeLineNumber), QString::number(addLineNumber),
QString(), diff.toHtmlEscaped()
.replace(' ', " "));
}
}
}
// Output any differences data that may be left
html += diffHtml(differencesData);
return html;
}
//==============================================================================
QString PmrWorkspacesWindowSynchronizeDialog::diffHtml(const QString &pFileName)
{
// Temporarily save the contents of the head version of the given file
QString oldFileName = Core::temporaryFileName();
QByteArray oldFileContents = mWorkspace->headFileContents(QDir(mWorkspace->path()).relativeFilePath(pFileName));
Core::writeFileContentsToFile(oldFileName, oldFileContents);
// Retrieve the contents of the working version of the given file
QByteArray newFileContents;
Core::readFileContentsFromFile(pFileName, newFileContents);
// Check whether both the head and working versions of the given file are
// text files
// Note: to retrieve the contents of a binary file may result in an empty
// string, so if we both the old and new contents is empty it means
// that we are dealing with a binary file...
QString res = QString();
bool oldFileEmpty = oldFileContents.isEmpty();
bool newFileEmpty = newFileContents.isEmpty();
if ( !(oldFileEmpty && newFileEmpty)
&& (oldFileEmpty || Core::isTextFile(oldFileName))
&& (newFileEmpty || Core::isTextFile(pFileName))) {
// Both versions of the given file are not text files, so check whether
// they are also CellML files
if ( mWebViewerCellmlTextFormatAction->isChecked()
&& !mInvalidCellmlCode.contains(oldFileContents)
&& !mInvalidCellmlCode.contains(newFileContents)
&& (oldFileEmpty || CellMLSupport::CellmlFileManager::instance()->isCellmlFile(oldFileName))
&& (newFileEmpty || CellMLSupport::CellmlFileManager::instance()->isCellmlFile(pFileName))) {
// We are dealing with a CellML file, so generate the CellML Text
// version of the file, this for both its head and working versions,
// and if successful then diff them
QString oldCellmlTextContents = QString();
QString newCellmlTextContents = QString();
if ( (oldFileEmpty || cellmlText(oldFileName, oldCellmlTextContents))
&& (newFileEmpty || cellmlText(pFileName, newCellmlTextContents))) {
res = diffHtml(oldCellmlTextContents, newCellmlTextContents);
mCellmlDiffHtmls.insert(pFileName, res);
} else {
// The conversion failed, so keep track of that fact (so as not
// to try to convert everytime this file gets selected)
if (!oldFileEmpty)
mInvalidCellmlCode << oldFileContents;
if (!newFileEmpty)
mInvalidCellmlCode << newFileContents;
}
}
// At this stage, if we haven't got any diff information then it means
// that we are either dealing with a text file, that we don't want to
// use the CellML Text format or that we couldn't convert both the head
// and working versions of the file to CellML Text format, in which case
// we diff their raw contents
if (res.isEmpty()) {
res = diffHtml(oldFileContents, newFileContents);
mDiffHtmls.insert(pFileName, res);
}
} else {
// We are dealing with a binary file
static const QString BinaryFile = " <tr class=\"binaryfile\">\n"
" <td colspan=4>\n"
" <code>%1</code>\n"
" </td>\n"
" </tr>\n";
res = BinaryFile.arg("["+tr("Binary File")+"]");
mDiffHtmls.insert(pFileName, res);
}
QFile::remove(oldFileName);
return res;
}
//==============================================================================
void PmrWorkspacesWindowSynchronizeDialog::updateDiffInformation()
{
// If there are no selected indexes then select the indexes that were
// previously selected otherwise retrieve the file name of the selected
// indexes and see whether we have already computed their differences, and
// then show those differences
QModelIndexList indexes = mChangesValue->selectionModel()->selectedIndexes();
if (indexes.isEmpty()) {
// No selected indexes, so select the previously selected indexes