-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.cpp
601 lines (444 loc) · 13.8 KB
/
MainWindow.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
// MainWindow.cpp
// Implements the MainWindow class representing the main app window
#include "Globals.h"
#include "MainWindow.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QFile>
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QString>
#include "ui_MainWindow.h"
#include "MassifParser.h"
#include "Project.h"
#include "Snapshot.h"
#include "DlgSnapshotDetails.h"
#include "FormatNumber.h"
#include "SnapshotDiff.h"
#include "DlgSnapshotDiffs.h"
#include "CodeLocationStatsModel.h"
#include "HistoryModel.h"
#include "HistoryModelHierarchyDelegate.h"
#include "ProjectLoader.h"
#include "ProjectSaver.h"
#include "LiveCaptureSettings.h"
#include "DlgLiveCaptureSettings.h"
#include "VgdbComm.h"
#include "LiveCapture.h"
#include "DlgLiveCapture.h"
#include "SnapshotModel.h"
MainWindow::MainWindow(QWidget * a_Parent):
Super(a_Parent),
m_UI(new Ui::MainWindow)
{
m_UI->setupUi(this);
// Add a context menu to twSnapshots:
m_UI->tvSnapshots->addAction(m_UI->actCtxDiffSelected);
m_UI->tvSnapshots->addAction(m_UI->actCtxDiffAll);
// Create a new empty project:
setProject(std::make_shared<Project>());
m_UI->tvSnapshots->sortByColumn(0, Qt::AscendingOrder); // Qt seems to default to Descending on Win8
// Set up the History view delegates:
m_UI->tvHistory->setItemDelegateForColumn(5, new HistoryModelHierarchyDelegate(this));
// m_UI->tvHistory->setItemDelegateForColumn(6, new HistoryModelPositionDelegate(this));
// Connect the UI signals / slots:
connect(m_UI->actProjectNew, SIGNAL(triggered()), this, SLOT(newProject()));
connect(m_UI->actProjectOpen, SIGNAL(triggered()), this, SLOT(loadProject()));
connect(m_UI->actProjectSave, SIGNAL(triggered()), this, SLOT(saveProject()));
connect(m_UI->actProjectSaveAs, SIGNAL(triggered()), this, SLOT(saveProjectAs()));
connect(m_UI->actSnapshotsAdd, SIGNAL(triggered()), this, SLOT(addSnapshotsFromFile()));
connect(m_UI->actSnapshotsLiveCapture, SIGNAL(triggered()), this, SLOT(snapshotsLiveCapture()));
connect(m_UI->tvSnapshots, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(tvItemDblClicked(const QModelIndex &)));
connect(m_UI->actCtxDiffSelected, SIGNAL(triggered()), this, SLOT(diffSelected()));
connect(m_UI->actCtxDiffAll, SIGNAL(triggered()), this, SLOT(diffAll()));
}
MainWindow::~MainWindow()
{
delete m_UI;
}
void MainWindow::addSnapshotsFromFile(void)
{
// Get the filename(s):
auto fileNames = QFileDialog::getOpenFileNames(this);
for (const auto & fileName: fileNames)
{
addSnapshotsFromFile(fileName);
}
}
void MainWindow::addSnapshotsFromFile(const QString & a_FileName)
{
// Open the file:
QFile file(a_FileName);
if (!file.open(QIODevice::ReadOnly))
{
QMessageBox::warning(this,
tr("File error"),
tr("Failed to open\n%1").arg(a_FileName)
);
return;
}
// Parse the file:
// TODO: Move this to a background thread (?)
MassifParser parser(m_Project);
connect(&parser, SIGNAL(parseError(quint32, const char *, const char *)), this, SLOT(parseError(quint32, const char *, const char *)));
connect(&parser, SIGNAL(newSnapshotParsed(SnapshotPtr)), this, SLOT(newSnapshotParsed(SnapshotPtr)));
connect(&parser, SIGNAL(parsedCommand(const char *)), this, SLOT(parsedCommand(const char *)));
connect(&parser, SIGNAL(parsedTimeUnit(const char *)), this, SLOT(parsedTimeUnit(const char *)));
parser.parse(file);
}
void MainWindow::snapshotsLiveCapture()
{
// Check that vgdb is available:
if (!VgdbComm::checkAvailability())
{
QMessageBox::warning(this,
tr("Live Capture not available"),
tr("Cannot start live capture, because Valgrind's \"vgdb\" program is not available.")
);
return;
}
// Query all the running valgrind instances:
auto runningInstances = VgdbComm::listRunningInstances();
// Ask for the settings:
LiveCaptureSettings settings;
QFileInfo projFile(m_Project->getFileName());
settings.m_SnapshotFolder = projFile.absolutePath();
settings.m_SnapshotFileNameFormat = projFile.completeBaseName() + QString::fromUtf8("-%1.massif");
DlgLiveCaptureSettings dlgSettings;
if (!dlgSettings.show(settings, runningInstances))
{
return;
}
// If we are to save the project and it hasn't been saved yet, ask for filename:
if (settings.m_ShouldSaveProject)
{
if (m_Project->getFileName().isEmpty())
{
if (!saveProjectAs())
{
return;
}
}
}
// Start live capture:
LiveCapture capture(m_Project, settings);
DlgLiveCapture dlgCapture;
dlgCapture.show(capture, settings);
}
void MainWindow::parseError(quint32 a_LineNum, const char * a_Msg, const char * a_Line)
{
QMessageBox::warning(this,
tr("Parse error"),
tr("Error while parsing line %1:\n\n%2\n\nThe offending line is:\n%3")
.arg(a_LineNum)
.arg(a_Msg)
.arg(a_Line)
);
}
void MainWindow::newSnapshotParsed(SnapshotPtr a_Snapshot)
{
assert(m_Project != nullptr);
// If the project already contains a snapshot with this timestamp, skip:
if (m_Project->hasSnapshotForTimestamp(a_Snapshot->getTimestamp()))
{
return;
}
// Add snapshot to project:
m_Project->addSnapshot(a_Snapshot);
}
void MainWindow::parsedCommand(const char * a_Command)
{
assert(m_Project != nullptr);
if (!m_Project->checkAndSetCommand(a_Command))
{
reinterpret_cast<MassifParser *>(QObject::sender())->abortParsing();
QMessageBox::warning(this,
tr("Bad commandline"),
tr("The commandline in the specified file is different, it is likely not created from the same Massif run.\n\n" \
"This project uses data from commandline \"%1\", but the file specified the commandline \"%2\"")
.arg(m_Project->getCommand().c_str())
.arg(a_Command)
);
}
}
void MainWindow::parsedTimeUnit(const char * a_TimeUnit)
{
assert(m_Project != nullptr);
if (!m_Project->checkAndSetTimeUnit(a_TimeUnit))
{
reinterpret_cast<MassifParser *>(QObject::sender())->abortParsing();
QMessageBox::warning(this,
tr("Bad time unit"),
tr("The time unit in the specified file is different, it is likely not created from the same Massif run.\n\n" \
"This project uses time units \"%1\", but the file specified the time unit \"%2\"")
.arg(m_Project->getTimeUnit().c_str())
.arg(a_TimeUnit)
);
}
}
void MainWindow::tvItemDblClicked(const QModelIndex & a_Item)
{
auto snapshot = m_ProjectSnapshotsModel->getItemSnapshot(a_Item);
if (snapshot != nullptr)
{
viewSnapshotDetails(snapshot);
}
}
void MainWindow::tvItemSelChanged(const QItemSelection & a_Selected, const QItemSelection & a_Deselected)
{
Q_UNUSED(a_Selected);
Q_UNUSED(a_Deselected);
m_UI->actCtxDiffSelected->setEnabled(m_UI->tvSnapshots->selectionModel()->selectedRows().size() > 1);
}
void MainWindow::viewSnapshotDetails(SnapshotPtr a_Snapshot)
{
auto dlg = new DlgSnapshotDetails;
dlg->show(a_Snapshot);
}
void MainWindow::diffSelected()
{
// Collect all selected snapshots:
SnapshotPtrs snapshots;
for (const auto row: m_UI->tvSnapshots->selectionModel()->selectedRows())
{
auto snapshot = m_ProjectSnapshotsModel->getItemSnapshot(row);
assert(snapshot != nullptr);
snapshots.push_back(snapshot);
}
showDiffsForSnapshots(snapshots);
}
void MainWindow::diffAll()
{
showDiffsForSnapshots(m_Project->getSnapshots());
}
void MainWindow::newProject()
{
// Ask the user whether to save current project (if appropriate):
if (!prepareCurrentProjectForUnload())
{
return;
}
// Create and set a new project:
setProject(std::make_shared<Project>());
}
void MainWindow::loadProject()
{
auto fileName = QFileDialog::getOpenFileName(
nullptr, // Parent widget
tr("Open a project file"), // Title
QString(), // Initial folder
tr("VisualMassifDiff project file (*.vmdp)") // Filter
);
if (fileName.isEmpty())
{
return;
}
loadProject(fileName);
}
void MainWindow::loadProject(const QString & a_FileName)
{
// Ask the user whether to save current project (if appropriate):
if (!prepareCurrentProjectForUnload())
{
return;
}
// Load the project from the file:
QFile f(a_FileName);
if (!f.open(QFile::ReadOnly))
{
QMessageBox::warning(this,
tr("File error"),
tr("Failed to open project file\n%1").arg(a_FileName)
);
return;
}
ProjectPtr project;
try
{
project = ProjectLoader::loadProject(f);
}
catch (const std::exception & exc)
{
QMessageBox::warning(this,
tr("File error"),
tr("Failed to load project from file\n%1\n\n%2").arg(a_FileName).arg(QString::fromUtf8(exc.what()))
);
return;
}
if (project == nullptr)
{
QMessageBox::warning(this,
tr("File error"),
tr("Failed to load project from file\n%1").arg(a_FileName)
);
return;
}
// Mark the project as non-dirty:
project->setSaved(a_FileName);
// Replace the currently loaded project:
setProject(project);
}
bool MainWindow::saveProject()
{
if (m_Project->getFileName().isEmpty())
{
return saveProjectAs();
}
return saveProject(m_Project->getFileName());
}
bool MainWindow::saveProject(const QString & a_FileName)
{
// Open the file for writing:
QFile f(a_FileName);
if (!f.open(QFile::WriteOnly))
{
QMessageBox::warning(
this,
tr("VisualMassifDiff: Failed to save project"),
tr("Failed to write to file %1").arg(a_FileName)
);
return false;
}
// Save the project:
try
{
ProjectSaver::saveProject(*m_Project, f);
m_Project->setSaved(a_FileName);
}
catch (const std::exception & exc)
{
QMessageBox::warning(
this,
tr("VisualMassifDiff: Failed to save project"),
tr("Failed to save project to file %1: %2").arg(a_FileName).arg(exc.what())
);
return false;
}
return true;
}
bool MainWindow::saveProjectAs()
{
auto fileName = QFileDialog::getSaveFileName(
nullptr, // Parent widget
tr("Save the project file"), // Title
QString(), // Initial folder
tr("VisualMassifDiff project file (*.vmdp)") // Filter
);
if (fileName.isEmpty())
{
return false;
}
return saveProject(fileName);
}
bool MainWindow::openUnknownFile(const QString & a_FileName)
{
// Check whether the file is a project file:
QFile f(a_FileName);
if (!f.open(QFile::ReadOnly))
{
return false;
}
bool isProject = ProjectLoader::isProjectFile(f);
f.close();
// If the file is a project, load it:
if (isProject)
{
loadProject(a_FileName);
return true;
}
// It is not a project, assume it's a snapshot file:
addSnapshotsFromFile(a_FileName);
return true;
}
void MainWindow::showDiffsForSnapshots(const SnapshotPtrs & a_Snapshots)
{
// Sort the snapshots by their timestamp:
SnapshotPtrs snapshots(a_Snapshots);
snapshots.sort([](SnapshotPtr a_First, SnapshotPtr a_Second)
{
assert(a_First != nullptr);
assert(a_Second != nullptr);
return (a_First->getTimestamp() < a_Second->getTimestamp());
}
);
// Create the diffs:
SnapshotDiffPtrs diffs;
SnapshotPtr prevSnapshot;
for (auto s: snapshots)
{
if (prevSnapshot != nullptr)
{
auto diff = std::make_shared<SnapshotDiff>(prevSnapshot, s);
diffs.push_back(diff);
}
prevSnapshot = s;
}
// Show the diffs:
auto dlg = new DlgSnapshotDiffs(this);
dlg->show(std::move(diffs));
}
bool MainWindow::prepareCurrentProjectForUnload()
{
// If not changed, doesn't need any confirmation
if (!m_Project->hasChangedSinceSave())
{
return true;
}
// Ask the user what to do:
auto answer = QMessageBox::question(
this, // Parent widget
tr("VisualMassifDiff: Save project?"), // Title
tr("Current project has not been saved, would you like to save it now?"), // Text
QMessageBox::StandardButtons(QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel), // Buttons
QMessageBox::Yes // Default button
);
switch (answer)
{
case QMessageBox::No:
{
// User wants to throw away the current project's changes
return true;
}
case QMessageBox::Yes:
{
return saveProject();
}
case QMessageBox::Cancel:
default:
{
// User wants to abort any operation and keep the current project as is, unsaved:
return false;
}
}
}
void MainWindow::setProject(ProjectPtr a_Project)
{
// Replace the ProjectSnapshots model:
auto projectSnapshotsModel = std::make_shared<SnapshotModel>(a_Project);
m_UI->tvSnapshots->setModel(projectSnapshotsModel.get());
m_ProjectSnapshotsModel = projectSnapshotsModel;
auto selectionModel = m_UI->tvSnapshots->selectionModel();
connect(
selectionModel, SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
this, SLOT(tvItemSelChanged(const QItemSelection &, const QItemSelection &))
);
// Replace the CodeLocationStats model:
auto codeLocationStatsModel = std::make_shared<CodeLocationStatsModel>(a_Project);
auto codeLocationStatsSortModel = std::make_shared<QSortFilterProxyModel>();
codeLocationStatsSortModel->setSourceModel(codeLocationStatsModel.get());
codeLocationStatsSortModel->setSortRole(CodeLocationStatsModel::SortRole);
m_UI->tvCodeLocations->setModel(codeLocationStatsSortModel.get());
m_CodeLocationStatsModel = codeLocationStatsModel;
m_CodeLocationStatsSortModel = codeLocationStatsSortModel;
// Replace the History model:
auto historyModel = std::make_shared<HistoryModel>(a_Project);
m_UI->tvHistory->setModel(historyModel.get());
m_UI->grHistory->setProject(
a_Project,
reinterpret_cast<HistoryModel *>(historyModel.get()),
m_UI->tvHistory->selectionModel()
);
m_HistoryModel = historyModel;
// Replace the current project (and free the old one):
m_Project = a_Project;
}