/
pmrworkspaceswindowwidget.cpp
1506 lines (1114 loc) · 60.7 KB
/
pmrworkspaceswindowwidget.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 widget
//==============================================================================
#include "corecliutils.h"
#include "coreguiutils.h"
#include "i18ninterface.h"
#include "pmrsupportpreferenceswidget.h"
#include "pmrwebservice.h"
#include "pmrworkspacemanager.h"
#include "pmrworkspaceswindowsynchronizedialog.h"
#include "pmrworkspaceswindowwidget.h"
#include "pmrworkspaceswindowwindow.h"
#include "preferencesinterface.h"
#include "treeviewwidget.h"
#include "usermessagewidget.h"
//==============================================================================
#include "ui_pmrworkspaceswindowwindow.h"
//==============================================================================
#include <QClipboard>
#include <QDesktopServices>
#include <QDir>
#include <QFileIconProvider>
#include <QKeyEvent>
#include <QMainWindow>
#include <QMenu>
#include <QSettings>
#include <QTimer>
#include <QUrl>
//==============================================================================
#include "git2/remote.h"
#include "git2/repository.h"
//==============================================================================
namespace OpenCOR {
namespace PMRWorkspacesWindow {
//==============================================================================
PmrWorkspacesWindowItem::PmrWorkspacesWindowItem(const Type &pType,
Core::TreeViewWidget *pTreeViewWidget,
PmrWorkspacesWindowProxyModel *pTreeViewProxyModel,
PMRSupport::PmrWorkspace *pWorkspace,
PMRSupport::PmrWorkspaceFileNode *pFileNode,
const QIcon &pIcon,
const QString &pText,
const QIcon &pCollapsedIcon,
const QIcon &pExpandedIcon) :
QStandardItem(pIcon, pText)
{
// Some initialisations
mType = pType;
mTreeViewWidget = pTreeViewWidget;
mTreeViewProxyModel = pTreeViewProxyModel;
mWorkspace = pWorkspace;
mFileNode = pFileNode;
mCollapsedIcon = pCollapsedIcon;
mExpandedIcon = pExpandedIcon;
// Customise ourselves
setToolTip(text());
}
//==============================================================================
PmrWorkspacesWindowItem::PmrWorkspacesWindowItem(const Type &pType,
Core::TreeViewWidget *pTreeViewWidget,
PmrWorkspacesWindowProxyModel *pTreeViewProxyModel,
PMRSupport::PmrWorkspace *pWorkspace,
const QIcon &pCollapsedIcon,
const QIcon &pExpandedIcon) :
PmrWorkspacesWindowItem(pType, pTreeViewWidget, pTreeViewProxyModel,
pWorkspace, 0, pCollapsedIcon, pWorkspace->name(),
pCollapsedIcon, pExpandedIcon)
{
}
//==============================================================================
PmrWorkspacesWindowItem::PmrWorkspacesWindowItem(const Type &pType,
Core::TreeViewWidget *pTreeViewWidget,
PmrWorkspacesWindowProxyModel *pTreeViewProxyModel,
PMRSupport::PmrWorkspace *pWorkspace,
PMRSupport::PmrWorkspaceFileNode *pFileNode,
const QIcon &pCollapsedIcon,
const QIcon &pExpandedIcon) :
PmrWorkspacesWindowItem(pType, pTreeViewWidget, pTreeViewProxyModel,
pWorkspace, pFileNode, pCollapsedIcon,
pFileNode->name(), pCollapsedIcon, pExpandedIcon)
{
}
//==============================================================================
PmrWorkspacesWindowItem::PmrWorkspacesWindowItem(const Type &pType,
Core::TreeViewWidget *pTreeViewWidget,
PmrWorkspacesWindowProxyModel *pTreeViewProxyModel,
PMRSupport::PmrWorkspace *pWorkspace,
PMRSupport::PmrWorkspaceFileNode *pFileNode,
const QIcon &pIcon) :
PmrWorkspacesWindowItem(pType, pTreeViewWidget, pTreeViewProxyModel,
pWorkspace, pFileNode, pIcon, pFileNode->name(),
QIcon(), QIcon())
{
}
//==============================================================================
int PmrWorkspacesWindowItem::type() const
{
// Return our type
return mType;
}
//==============================================================================
PMRSupport::PmrWorkspace * PmrWorkspacesWindowItem::workspace() const
{
// Return our workspace
return mWorkspace;
}
//==============================================================================
PMRSupport::PmrWorkspaceFileNode * PmrWorkspacesWindowItem::fileNode() const
{
// Return our file node
return mFileNode;
}
//==============================================================================
QIcon PmrWorkspacesWindowItem::collapsedIcon() const
{
// Return our collapsed icon
return mCollapsedIcon;
}
//==============================================================================
void PmrWorkspacesWindowItem::setCollapsedIcon(const QIcon &pCollapsedIcon)
{
// Set our collapsed icon
mCollapsedIcon = pCollapsedIcon;
setIcon(mTreeViewWidget->isExpanded(mTreeViewProxyModel->mapFromSource(index()))?mExpandedIcon:mCollapsedIcon);
}
//==============================================================================
QIcon PmrWorkspacesWindowItem::expandedIcon() const
{
// Return our expanded icon
return mExpandedIcon;
}
//==============================================================================
void PmrWorkspacesWindowItem::setExpandedIcon(const QIcon &pExpandedIcon)
{
// Set our expanded icon
mExpandedIcon = pExpandedIcon;
setIcon(mTreeViewWidget->isExpanded(mTreeViewProxyModel->mapFromSource(index()))?mExpandedIcon:mCollapsedIcon);
}
//==============================================================================
PmrWorkspacesWindowProxyModel::PmrWorkspacesWindowProxyModel(QStandardItemModel *pModel,
QObject *pParent) :
QSortFilterProxyModel(pParent),
mModel(pModel)
{
}
//==============================================================================
bool PmrWorkspacesWindowProxyModel::lessThan(const QModelIndex &pSourceLeft,
const QModelIndex &pSourceRight) const
{
// Determine and return whether the left source should be before the right
// source
int leftType = static_cast<PmrWorkspacesWindowItem *>(mModel->itemFromIndex(pSourceLeft))->type();
int rightType = static_cast<PmrWorkspacesWindowItem *>(mModel->itemFromIndex(pSourceRight))->type();
if ( (leftType != PmrWorkspacesWindowItem::File)
&& (rightType == PmrWorkspacesWindowItem::File)) {
return true;
} else if ( (leftType == PmrWorkspacesWindowItem::File)
&& (rightType != PmrWorkspacesWindowItem::File)) {
return false;
} else {
return QSortFilterProxyModel::lessThan(pSourceLeft, pSourceRight);
}
}
//==============================================================================
PmrWorkspacesWindowWidget::PmrWorkspacesWindowWidget(const QString &pPmrUrl,
PMRSupport::PmrWebService *pPmrWebService,
PmrWorkspacesWindowWindow *pParent) :
Core::TreeViewWidget(pParent),
mSettingsGroup(QString()),
mPmrWebService(pPmrWebService)
{
// Initialise our internals by 'resetting' them
reset(pPmrUrl);
// Customise ourselves
mTreeViewModel = new QStandardItemModel(this);
mTreeViewProxyModel = new PmrWorkspacesWindowProxyModel(mTreeViewModel, this);
mTreeViewProxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
mTreeViewProxyModel->setSourceModel(mTreeViewModel);
setContextMenuPolicy(Qt::CustomContextMenu);
setEditTriggers(QAbstractItemView::NoEditTriggers);
setHeaderHidden(true);
setModel(mTreeViewProxyModel);
setSelectionMode(QAbstractItemView::ExtendedSelection);
connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
this, SLOT(showCustomContextMenu()));
connect(this, SIGNAL(doubleClicked(const QModelIndex &)),
this, SLOT(itemDoubleClicked()));
connect(this, SIGNAL(expanded(const QModelIndex &)),
this, SLOT(resizeTreeViewToContents()));
connect(this, SIGNAL(expanded(const QModelIndex &)),
this, SLOT(itemExpanded(const QModelIndex &)));
connect(this, SIGNAL(collapsed(const QModelIndex &)),
this, SLOT(resizeTreeViewToContents()));
connect(this, SIGNAL(collapsed(const QModelIndex &)),
this, SLOT(itemCollapsed(const QModelIndex &)));
// Create and set ourselves a layout
QVBoxLayout *layout = new QVBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
setLayout(layout);
// Create and customise our user menssage
mUserMessageWidget = new Core::UserMessageWidget(this);
mUserMessageWidget->setScale(0.85);
layout->addWidget(mUserMessageWidget);
// Create our various non-owned workspace icons
static const QIcon UnstagedIcon = QIcon(":/PMRWorkspacesWindow/wQ.png");
static const QIcon ConflictIcon = QIcon(":/PMRWorkspacesWindow/wE.png");
mCollapsedWorkspaceIcon = QApplication::style()->standardIcon(QStyle::SP_DirClosedIcon);
mExpandedWorkspaceIcon = QApplication::style()->standardIcon(QStyle::SP_DirOpenIcon);
int folderIconSize = mCollapsedWorkspaceIcon.availableSizes().first().width();
int overlayIconSize = 0.57*folderIconSize;
mUnstagedCollapsedWorkspaceIcon = Core::overlayedIcon(mCollapsedWorkspaceIcon, UnstagedIcon,
folderIconSize, folderIconSize,
0, 0, overlayIconSize, overlayIconSize);
mUnstagedExpandedWorkspaceIcon = Core::overlayedIcon(mExpandedWorkspaceIcon, UnstagedIcon,
folderIconSize, folderIconSize,
0, 0, overlayIconSize, overlayIconSize);
mConflictCollapsedWorkspaceIcon = Core::overlayedIcon(mCollapsedWorkspaceIcon, ConflictIcon,
folderIconSize, folderIconSize,
0, 0, overlayIconSize, overlayIconSize);
mConflictExpandedWorkspaceIcon = Core::overlayedIcon(mExpandedWorkspaceIcon, ConflictIcon,
folderIconSize, folderIconSize,
0, 0, overlayIconSize, overlayIconSize);
// Create our various owned workspace icons
static const QIcon FavoriteIcon = QIcon(":/oxygen/places/favorites.png");
int overlayIconPos = folderIconSize-overlayIconSize;
mCollapsedOwnedWorkspaceIcon = Core::overlayedIcon(mCollapsedWorkspaceIcon, FavoriteIcon,
folderIconSize, folderIconSize,
overlayIconPos, overlayIconPos,
overlayIconSize, overlayIconSize);
mExpandedOwnedWorkspaceIcon = Core::overlayedIcon(mExpandedWorkspaceIcon, FavoriteIcon,
folderIconSize, folderIconSize,
overlayIconPos, overlayIconPos,
overlayIconSize, overlayIconSize);
mUnstagedCollapsedOwnedWorkspaceIcon = Core::overlayedIcon(mCollapsedOwnedWorkspaceIcon, UnstagedIcon,
folderIconSize, folderIconSize,
0, 0, overlayIconSize, overlayIconSize);
mUnstagedExpandedOwnedWorkspaceIcon = Core::overlayedIcon(mExpandedOwnedWorkspaceIcon, UnstagedIcon,
folderIconSize, folderIconSize,
0, 0, overlayIconSize, overlayIconSize);
mConflictCollapsedOwnedWorkspaceIcon = Core::overlayedIcon(mCollapsedOwnedWorkspaceIcon, ConflictIcon,
folderIconSize, folderIconSize,
0, 0, overlayIconSize, overlayIconSize);
mConflictExpandedOwnedWorkspaceIcon = Core::overlayedIcon(mExpandedOwnedWorkspaceIcon, ConflictIcon,
folderIconSize, folderIconSize,
0, 0, overlayIconSize, overlayIconSize);
// Create our various file icons
mFileIcon = QFileIconProvider().icon(QFileIconProvider::File);
int fileIconSize = mFileIcon.availableSizes().first().width();
overlayIconSize = 0.57*fileIconSize;
mWaFileIcon = Core::overlayedIcon(mFileIcon, QIcon(":/PMRWorkspacesWindow/wA.png"),
fileIconSize, fileIconSize,
0, 0, overlayIconSize, overlayIconSize);
mWcFileIcon = Core::overlayedIcon(mFileIcon, QIcon(":/PMRWorkspacesWindow/wC.png"),
fileIconSize, fileIconSize,
0, 0, overlayIconSize, overlayIconSize);
mWdFileIcon = Core::overlayedIcon(mFileIcon, QIcon(":/PMRWorkspacesWindow/wD.png"),
fileIconSize, fileIconSize,
0, 0, overlayIconSize, overlayIconSize);
mWeFileIcon = Core::overlayedIcon(mFileIcon, QIcon(":/PMRWorkspacesWindow/wE.png"),
fileIconSize, fileIconSize,
0, 0, overlayIconSize, overlayIconSize);
mWmFileIcon = Core::overlayedIcon(mFileIcon, QIcon(":/PMRWorkspacesWindow/wM.png"),
fileIconSize, fileIconSize,
0, 0, overlayIconSize, overlayIconSize);
mWqFileIcon = Core::overlayedIcon(mFileIcon, QIcon(":/PMRWorkspacesWindow/wQ.png"),
fileIconSize, fileIconSize,
0, 0, overlayIconSize, overlayIconSize);
mWrFileIcon = Core::overlayedIcon(mFileIcon, QIcon(":/PMRWorkspacesWindow/wR.png"),
fileIconSize, fileIconSize,
0, 0, overlayIconSize, overlayIconSize);
mWtFileIcon = Core::overlayedIcon(mFileIcon, QIcon(":/PMRWorkspacesWindow/wT.png"),
fileIconSize, fileIconSize,
0, 0, overlayIconSize, overlayIconSize);
// Connection to handle a response from our workspace manager
PMRSupport::PmrWorkspaceManager *workspaceManager = PMRSupport::PmrWorkspaceManager::instance();
connect(workspaceManager, SIGNAL(workspaceCloned(PMRSupport::PmrWorkspace *)),
this, SLOT(workspaceCloned(PMRSupport::PmrWorkspace *)));
connect(workspaceManager, SIGNAL(workspaceUncloned(PMRSupport::PmrWorkspace *)),
this, SLOT(workspaceUncloned(PMRSupport::PmrWorkspace *)));
connect(workspaceManager, SIGNAL(workspaceSynchronized(PMRSupport::PmrWorkspace *)),
this, SLOT(workspaceSynchronized(PMRSupport::PmrWorkspace *)));
// Create and start a timer for refreshing our workspaces
mTimer = new QTimer(this);
connect(mTimer, SIGNAL(timeout()),
this, SLOT(refreshWorkspaces()));
mTimer->start(1000);
// Create and populate our context menu
mContextMenu = new QMenu(this);
mParentNewAction = pParent->gui()->actionNew;
mParentReloadAction = pParent->gui()->actionReload;
mNewWorkspaceAction = Core::newAction(mParentNewAction->icon(),
this);
mViewWorkspaceInPmrAction = Core::newAction(QIcon(":/oxygen/categories/applications-internet.png"),
this);
mViewWorkspaceOncomputerAction = Core::newAction(QIcon(":/oxygen/devices/computer.png"),
this);
mCopyWorkspaceUrlAction = Core::newAction(QIcon(":/oxygen/actions/edit-copy.png"),
this);
mCopyWorkspacePathAction = Core::newAction(QIcon(":/oxygen/actions/edit-copy.png"),
this);
mMakeLocalWorkspaceCopyAction = Core::newAction(Core::overlayedIcon(mCollapsedWorkspaceIcon,
QIcon(":/PMRWorkspacesWindow/pull.png"),
folderIconSize, folderIconSize,
overlayIconPos, overlayIconPos,
overlayIconSize, overlayIconSize),
this);
mSynchronizeWorkspaceAction = Core::newAction(QIcon(":/PMRWorkspacesWindow/synchronize.png"),
this);
mReloadWorkspacesAction = Core::newAction(mParentReloadAction->icon(),
this);
mAboutWorkspaceAction = Core::newAction(QIcon(":/oxygen/actions/help-about.png"),
this);
connect(mNewWorkspaceAction, SIGNAL(triggered(bool)),
mParentNewAction, SIGNAL(triggered(bool)));
connect(mViewWorkspaceInPmrAction, SIGNAL(triggered(bool)),
this, SLOT(viewWorkspaceInPmr()));
connect(mViewWorkspaceOncomputerAction, SIGNAL(triggered(bool)),
this, SLOT(viewWorkspaceOncomputer()));
connect(mCopyWorkspaceUrlAction, SIGNAL(triggered(bool)),
this, SLOT(copyWorkspaceUrl()));
connect(mCopyWorkspacePathAction, SIGNAL(triggered(bool)),
this, SLOT(copyWorkspacePath()));
connect(mMakeLocalWorkspaceCopyAction, SIGNAL(triggered(bool)),
this, SLOT(makeLocalWorkspaceCopy()));
connect(mSynchronizeWorkspaceAction, SIGNAL(triggered(bool)),
this, SLOT(synchronizeWorkspace()));
connect(mReloadWorkspacesAction, SIGNAL(triggered(bool)),
mParentReloadAction, SIGNAL(triggered(bool)));
connect(mAboutWorkspaceAction, SIGNAL(triggered(bool)),
this, SLOT(aboutWorkspace()));
mContextMenu->addAction(mNewWorkspaceAction);
mContextMenu->addSeparator();
mContextMenu->addAction(mViewWorkspaceInPmrAction);
mContextMenu->addAction(mViewWorkspaceOncomputerAction);
mContextMenu->addSeparator();
mContextMenu->addAction(mCopyWorkspaceUrlAction);
mContextMenu->addAction(mCopyWorkspacePathAction);
mContextMenu->addSeparator();
mContextMenu->addAction(mMakeLocalWorkspaceCopyAction);
mContextMenu->addSeparator();
mContextMenu->addAction(mSynchronizeWorkspaceAction);
mContextMenu->addSeparator();
mContextMenu->addAction(mReloadWorkspacesAction);
mContextMenu->addSeparator();
mContextMenu->addAction(mAboutWorkspaceAction);
}
//==============================================================================
PmrWorkspacesWindowWidget::~PmrWorkspacesWindowWidget()
{
// Delete some internal objects
delete mTimer;
}
//==============================================================================
void PmrWorkspacesWindowWidget::retranslateUi()
{
// Retranslate our actions
// Note: some actions are translated prior to showing the context menu since
// they can be used for one or several items...
I18nInterface::retranslateAction(mNewWorkspaceAction, tr("New Workspace..."),
mParentNewAction->statusTip());
I18nInterface::retranslateAction(mCopyWorkspaceUrlAction, tr("Copy Workspace URL"),
tr("Copy the current workspace URL to the clipboard"));
I18nInterface::retranslateAction(mCopyWorkspacePathAction, tr("Copy Workspace Path"),
tr("Copy the current workspace path to the clipboard"));
I18nInterface::retranslateAction(mMakeLocalWorkspaceCopyAction, tr("Make Local Workspace Copy..."),
tr("Make a local copy of the current workspace"));
I18nInterface::retranslateAction(mSynchronizeWorkspaceAction, tr("Synchronise Workspace With PMR..."),
tr("Synchronise the curent workspace with PMR"));
I18nInterface::retranslateAction(mReloadWorkspacesAction, tr("Reload Workspaces"),
mParentReloadAction->statusTip());
I18nInterface::retranslateAction(mAboutWorkspaceAction, tr("About Workspace"),
tr("Some information about the current workspace"));
// Retranslate the rest of our GUI by updating it, if we have been
// initialised
if (mInitialized)
updateGui();
}
//==============================================================================
static const auto SettingsClonedWorkspaceFolders = QStringLiteral("ClonedWorkspaceFolders/%1");
//==============================================================================
void PmrWorkspacesWindowWidget::loadSettings(QSettings *pSettings)
{
// Keep track of our settings' group
mSettingsGroup = pSettings->group();
// Retrieve and keep track of some information about the previously cloned
// workspace folders
// Note: for the key, we use the PMR URL's host since the URL itself
// contains a "://" and this messes things up with QSettings...
foreach (const QString &clonedWorkspaceFolder,
pSettings->value(SettingsClonedWorkspaceFolders.arg(QUrl(mPmrUrl).host())).toStringList()) {
// Retrieve the URL (i.e. remote.origin.url) of the cloned workspace
// folder
QString clonedWorkspaceUrl = QString();
git_repository *gitRepository = 0;
if (!git_repository_open(&gitRepository,
clonedWorkspaceFolder.toUtf8().constData())) {
git_strarray remotes;
if (!git_remote_list(&remotes, gitRepository)) {
for (size_t i = 0; i < remotes.count; ++i) {
char *name = remotes.strings[i];
if (!strcmp(name, "origin")) {
git_remote *remote = 0;
if (!git_remote_lookup(&remote, gitRepository, name)) {
const char *remoteUrl = git_remote_url(remote);
if (remoteUrl)
clonedWorkspaceUrl = QString(remoteUrl);
}
}
}
}
git_repository_free(gitRepository);
}
// Keep track of the URL of the cloned workspace folder, if there is one
// and it's not already tracked
if (!clonedWorkspaceUrl.isEmpty()) {
if (mWorkspaceUrlFoldersOwned.contains(clonedWorkspaceUrl)) {
duplicateCloneMessage(clonedWorkspaceUrl, mWorkspaceUrlFoldersOwned.value(clonedWorkspaceUrl).first, clonedWorkspaceFolder);
} else {
mClonedWorkspaceFolderUrls.insert(clonedWorkspaceFolder, clonedWorkspaceUrl);
mWorkspaceUrlFoldersOwned.insert(clonedWorkspaceUrl, QPair<QString, bool>(clonedWorkspaceFolder, false));
}
}
}
}
//==============================================================================
void PmrWorkspacesWindowWidget::saveSettings(QSettings *pSettings) const
{
// Keep track of the names of folders containing cloned workspaces
// Note: for the key, we use the PMR URL's host since the URL itself
// contains a "://" and this messes things up with QSettings...
pSettings->setValue(SettingsClonedWorkspaceFolders.arg(QUrl(mPmrUrl).host()),
QVariant(mClonedWorkspaceFolderUrls.keys()));
}
//==============================================================================
void PmrWorkspacesWindowWidget::keyPressEvent(QKeyEvent *pEvent)
{
// Default handling of the event
Core::TreeViewWidget::keyPressEvent(pEvent);
// Retrieve all the files that are currently selected
// Note: if there is a folder among the selected items, then ignore
// everything...
QStringList fileNames = QStringList();
QModelIndexList items = selectionModel()->selectedIndexes();
for (int i = 0, iMax = items.count(); i < iMax; ++i) {
PmrWorkspacesWindowItem *item = static_cast<PmrWorkspacesWindowItem *>(mTreeViewModel->itemFromIndex(mTreeViewProxyModel->mapToSource(items[i])));
if (item->type() == PmrWorkspacesWindowItem::File) {
fileNames << item->fileNode()->path();
} else {
fileNames = QStringList();
break;
}
}
// Let people know about a key having been pressed with the view of opening
// one or several files
if ( fileNames.count()
&& ((pEvent->key() == Qt::Key_Enter) || (pEvent->key() == Qt::Key_Return))) {
// There are some files that are selected and we want to open them, so
// let people know about it
emit openFilesRequested(fileNames);
}
}
//==============================================================================
void PmrWorkspacesWindowWidget::update(const QString &pPmrUrl)
{
// Save our settings using the 'old' PMR URL, reset ourselves and load our
// 'new' settings
QSettings settings;
settings.beginGroup(mSettingsGroup);
saveSettings(&settings);
reset(pPmrUrl);
loadSettings(&settings);
settings.endGroup();
}
//==============================================================================
void PmrWorkspacesWindowWidget::reset(const QString &pPmrUrl)
{
// Reset our internals
mPmrUrl = pPmrUrl;
mClonedWorkspaceFolderUrls = QMap<QString, QString>();
mWorkspaceUrlFoldersOwned = QMap<QString, QPair<QString, bool>>();
mInitialized = false;
mErrorMessage = QString();
mAuthenticated = false;
}
//==============================================================================
void PmrWorkspacesWindowWidget::updateGui(const bool &pForceUserMessageVisibility)
{
// Determine the message to be displayed, if any
if (!mAuthenticated) {
mUserMessageWidget->setIconMessage(":/oxygen/actions/help-hint.png",
tr("Authenticate yourself..."),
tr("Click on the top-right button."));
} else if (mErrorMessage.isEmpty()) {
if (!PMRSupport::PmrWorkspaceManager::instance()->count()) {
mUserMessageWidget->setIconMessage(":/oxygen/actions/help-about.png",
tr("No workspaces were found..."));
} else {
mUserMessageWidget->resetMessage();
}
} else {
mUserMessageWidget->setIconMessage(":/oxygen/emblems/emblem-important.png",
Core::formatMessage(mErrorMessage, false, true));
}
// Show/hide our user message widget
mUserMessageWidget->setVisible( pForceUserMessageVisibility
|| !mUserMessageWidget->text().isEmpty());
}
//==============================================================================
void PmrWorkspacesWindowWidget::initialize(const PMRSupport::PmrWorkspaces &pWorkspaces,
const QString &pErrorMessage,
const bool &pAuthenticated)
{
// Initialise / keep track of some properties
PMRSupport::PmrWorkspaceManager *workspaceManager = PMRSupport::PmrWorkspaceManager::instance();
workspaceManager->clearWorkspaces();
mErrorMessage = pErrorMessage;
mAuthenticated = pAuthenticated;
if (pErrorMessage.isEmpty() && pAuthenticated) {
// Reconcile the URLs of my-workspaces (on PMR) with those from our
// workspace folders (in doing so, folders/URLs that don't correspond to
// an actual PMR workspace are pruned from the relevant maps)
// First, clear the owned flag from the list of URLs with workspace
// folders
QMutableMapIterator<QString, QPair<QString, bool>> urlsIterator(mWorkspaceUrlFoldersOwned);
while (urlsIterator.hasNext()) {
urlsIterator.next();
urlsIterator.setValue(QPair<QString, bool>(urlsIterator.value().first, false));
}
foreach (PMRSupport::PmrWorkspace *workspace, pWorkspaces) {
// Remember our workspace, so we can find it by URL
QString url = workspace->url();
workspaceManager->addWorkspace(workspace);
// Check if we know about its folder and, if so, flag it as ours
if (mWorkspaceUrlFoldersOwned.contains(url)) {
QString path = mWorkspaceUrlFoldersOwned.value(url).first;
mWorkspaceUrlFoldersOwned.insert(url, QPair<QString, bool>(path, true));
workspace->open(path);
}
}
// Now check the workspace folders that aren't from my-workspaces (on
// PMR)
urlsIterator.toFront();
while (urlsIterator.hasNext()) {
urlsIterator.next();
if (!urlsIterator.value().second) {
QString url = urlsIterator.key();
PMRSupport::PmrWorkspace *workspace = mPmrWebService->workspace(url);
if (workspace) {
// The workspace is known, so ask our workspace manager to
// track it, and then open it
workspaceManager->addWorkspace(workspace);
workspace->open(urlsIterator.value().first);
} else {
// The workspace is not known, so forget about it
mClonedWorkspaceFolderUrls.remove(urlsIterator.value().first);
urlsIterator.remove();
}
}
}
}
// Populate our tree view widget with our different workspaces
mTreeViewModel->clear();
foreach (PMRSupport::PmrWorkspace *workspace, workspaceManager->workspaces())
addWorkspace(workspace);
updateGui( (pWorkspaces == PMRSupport::PmrWorkspaces())
&& pErrorMessage.isEmpty() && !pAuthenticated);
mInitialized = true;
}
//==============================================================================
void PmrWorkspacesWindowWidget::resizeTreeViewToContents()
{
// Resize our tree view widget so that all of its contents is visible
resizeColumnToContents(0);
}
//==============================================================================
PmrWorkspacesWindowItem * PmrWorkspacesWindowWidget::currentItem() const
{
// Return our current item
PmrWorkspacesWindowItem *res = static_cast<PmrWorkspacesWindowItem *>(mTreeViewModel->itemFromIndex(mTreeViewProxyModel->mapToSource(currentIndex())));
if (!res) {
// There is no current item, so return the one under our mouse pointer
res = static_cast<PmrWorkspacesWindowItem *>(mTreeViewModel->itemFromIndex(mTreeViewProxyModel->mapToSource(indexAt(mapFromGlobal(QCursor::pos())))));
}
return res;
}
//==============================================================================
PmrWorkspacesWindowItem * PmrWorkspacesWindowWidget::workspaceItem(PMRSupport::PmrWorkspace *pWorkspace) const
{
// Return the item, if any, corresponding to the given workspace
for (int i = 0, iMax = mTreeViewModel->invisibleRootItem()->rowCount(); i < iMax; ++i) {
PmrWorkspacesWindowItem *item = static_cast<PmrWorkspacesWindowItem *>(mTreeViewModel->invisibleRootItem()->child(i));
if (item->workspace() == pWorkspace)
return item;
}
return 0;
}
//==============================================================================
void PmrWorkspacesWindowWidget::retrieveWorkspaceIcons(PMRSupport::PmrWorkspace *pWorkspace,
QIcon &pCollapsedIcon,
QIcon &pExpandedIcon)
{
// Retrieve the icons to use for the given workspace
PMRSupport::PmrWorkspace::WorkspaceStatus workspaceStatus = pWorkspace->gitWorkspaceStatus();
if (workspaceStatus & PMRSupport::PmrWorkspace::StatusConflict) {
pCollapsedIcon = pWorkspace->isOwned()?mConflictCollapsedOwnedWorkspaceIcon:mConflictCollapsedWorkspaceIcon;
pExpandedIcon = pWorkspace->isOwned()?mConflictExpandedOwnedWorkspaceIcon:mConflictExpandedWorkspaceIcon;
} else if (workspaceStatus & PMRSupport::PmrWorkspace::StatusUnstaged) {
pCollapsedIcon = pWorkspace->isOwned()?mUnstagedCollapsedOwnedWorkspaceIcon:mUnstagedCollapsedWorkspaceIcon;
pExpandedIcon = pWorkspace->isOwned()?mUnstagedExpandedOwnedWorkspaceIcon:mUnstagedExpandedWorkspaceIcon;
} else {
pCollapsedIcon = pWorkspace->isOwned()?mCollapsedOwnedWorkspaceIcon:mCollapsedWorkspaceIcon;
pExpandedIcon = pWorkspace->isOwned()?mExpandedOwnedWorkspaceIcon:mExpandedWorkspaceIcon;
}
}
//==============================================================================
PmrWorkspacesWindowItems PmrWorkspacesWindowWidget::retrieveItems(PmrWorkspacesWindowItem *pItem) const
{
// Return the child items of the given item
PmrWorkspacesWindowItems res = PmrWorkspacesWindowItems();
for (int i = 0, iMax = pItem->rowCount(); i < iMax; ++i) {
PmrWorkspacesWindowItem *item = static_cast<PmrWorkspacesWindowItem *>(pItem->child(i));
res << item;
if (item->hasChildren())
res << retrieveItems(item);
}
return res;
}
//==============================================================================
void PmrWorkspacesWindowWidget::deleteItems(PmrWorkspacesWindowItem *pItem,
PmrWorkspacesWindowItems &pItems)
{
// Recursively delete the given items, unless there is none left
if (pItems.isEmpty())
return;
if (pItem->hasChildren()) {
// Note: we delete the child items in reverse order since deletion is
// done using their row number...
for (int i = pItem->rowCount()-1; i >= 0; --i)
deleteItems(static_cast<PmrWorkspacesWindowItem *>(pItem->child(i)), pItems);
// Remove the folder item, if it is now empty, or workspace, if it's not
// owned and the folder where it was cloned has been deleted
if (!pItem->hasChildren()) {
pItems.removeOne(pItem);
if (pItem->parent())
pItem->parent()->removeRow(pItem->row());
else if (!pItem->workspace()->isOwned())
mTreeViewModel->invisibleRootItem()->removeRow(pItem->row());
}
} else if (pItems.contains(pItem)) {
pItems.removeOne(pItem);
pItem->parent()->removeRow(pItem->row());
}
}
//==============================================================================
void PmrWorkspacesWindowWidget::addWorkspace(PMRSupport::PmrWorkspace *pWorkspace)
{
// Add the given workspace to our tree view widget
QIcon collapsedIcon;
QIcon expandedIcon;
retrieveWorkspaceIcons(pWorkspace, collapsedIcon, expandedIcon);
PmrWorkspacesWindowItem *item = new PmrWorkspacesWindowItem(pWorkspace->isOwned()?
PmrWorkspacesWindowItem::OwnedWorkspace:
PmrWorkspacesWindowItem::Workspace,
this,
mTreeViewProxyModel,
pWorkspace,
collapsedIcon, expandedIcon);
mTreeViewModel->invisibleRootItem()->appendRow(item);
populateWorkspace(pWorkspace, item, pWorkspace->rootFileNode());
// Make sure that everything is properly sorted and that all of the contents
// of our tree view widget is visible
sortAndResizeTreeViewToContents();
}
//==============================================================================
PmrWorkspacesWindowItems PmrWorkspacesWindowWidget::populateWorkspace(PMRSupport::PmrWorkspace *pWorkspace,
PmrWorkspacesWindowItem *pFolderItem,
PMRSupport::PmrWorkspaceFileNode *pFileNode,
bool &pIsUnstaged,
bool &pHasConflicts)
{
// Populate the given folder item with its children, which are referenced in
// the given file node
PmrWorkspacesWindowItems res = PmrWorkspacesWindowItems();
pIsUnstaged = false;
pHasConflicts = false;
foreach(PMRSupport::PmrWorkspaceFileNode *fileNode, pFileNode->children()) {
// Check whether we already know about the file node
PmrWorkspacesWindowItem *newItem = 0;
for (int i = 0, iMax = pFolderItem->rowCount(); i < iMax; ++i) {
PmrWorkspacesWindowItem *item = static_cast<PmrWorkspacesWindowItem *>(pFolderItem->child(i));
if (item->fileNode() == fileNode) {
newItem = item;
break;
}
}
// Add a new item for the file node or use the one that already exists
// for it
if (fileNode->hasChildren()) {
PmrWorkspacesWindowItem *folderItem = newItem?
newItem:
new PmrWorkspacesWindowItem(PmrWorkspacesWindowItem::Folder,
this,
mTreeViewProxyModel,
pWorkspace,
fileNode,
mCollapsedWorkspaceIcon,
mExpandedWorkspaceIcon);
if (!newItem)
pFolderItem->appendRow(folderItem);
bool isUnstaged;
bool hasConflicts;
res << folderItem
<< populateWorkspace(pWorkspace, folderItem, fileNode,
isUnstaged, hasConflicts);
folderItem->setCollapsedIcon(hasConflicts?
mConflictCollapsedWorkspaceIcon:
isUnstaged?
mUnstagedCollapsedWorkspaceIcon:
mCollapsedWorkspaceIcon);
folderItem->setExpandedIcon(hasConflicts?
mConflictExpandedWorkspaceIcon:
isUnstaged?
mUnstagedExpandedWorkspaceIcon:
mExpandedWorkspaceIcon);
} else {
// We are dealing with a file, so retrieve its status and use the
// corresponding icon for it, if needed
// Note: it may happen (e.g. when deleting a folder) that the Git
// status is not up to date, hence we need to check for the
// I and W values not to be '\0' (which would be the case for
// a folder that doesn't contain any files anymore)...
PMRSupport::CharPair status = fileNode->status();
if ((status.first == '\0') && (status.second == '\0'))
continue;
QIcon icon = mFileIcon;