-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
528 lines (452 loc) · 16.8 KB
/
mainwindow.cpp
File metadata and controls
528 lines (452 loc) · 16.8 KB
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
#include <QFileDialog>
#include <QMessageBox>
#include <QApplication>
#include <QStatusBar>
#include <QLabel>
#include <QAction>
#include <QMenuBar>
#include <QToolBar>
#include <QColorDialog>
#include <QFontDialog>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QSplitter>
#include <QDockWidget>
#include "mainwindow.h"
#include "ui_mainwindow.h"
/*****************************************************************************/
/* Public methods */
/*****************************************************************************/
MainWindow::MainWindow() :
_ui(new Ui::MainWindow),
_parserDockWidget(0)
{
_ui->setupUi(this);
setAcceptDrops( true );
init();
setCurrentFile("");
}
MainWindow::~MainWindow()
{
delete _ui;
}
/*****************************************************************************/
/* Protected methods */
/*****************************************************************************/
void MainWindow::closeEvent(QCloseEvent *)
{
writeSettings();
}
void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasUrls()) {
event->accept();
}
}
void MainWindow::dropEvent(QDropEvent *event)
{
if (event->mimeData()->hasUrls())
{
QList<QUrl> urls = event->mimeData()->urls();
QString filePath = urls.at(0).toLocalFile();
loadFile(filePath);
event->accept();
}
}
/*****************************************************************************/
/* Private Slots */
/*****************************************************************************/
void MainWindow::about()
{
QMessageBox::about(this, tr("About"),
tr("<b>Transport Stream Analyzer</b> analyzes the content"
"of MPEG-2 transport stream used in video content delivery.<br/><br/>"
"<i>Copyright © 2018-2020 kohnech.</i><br/><br/>"
"This library is free software; you can redistribute it<br/>"
"and/or modify it under the terms of the GNU Lesser General<br/>"
"Public License as published by the Free Software Foundation;<br/>"
"either version 2.1 of the License, or (at your option) any <br/>"
"later version.<br/><br/>"
"This library is distributed in the hope that it will be <br/>"
"useful, but WITHOUT ANY WARRANTY; without even the implied <br/>"
"warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR <br/>"
"PURPOSE. See the GNU Lesser General Public License for more <br/>"
"details.<br/><br/>"
"You should have received a copy of the GNU Lesser General <br/>"
"Public License along with this library; if not, write to the <br/>"
"Free Software Foundation, Inc., 51 Franklin Street, Fifth <br/>"
"Floor, Boston, MA 02110-1301 USA"));
}
void MainWindow::dataChanged()
{
setWindowModified(_hexEdit->isModified());
}
void MainWindow::open()
{
QString fileName = QFileDialog::getOpenFileName(this);
if (!fileName.isEmpty()) {
loadFile(fileName);
}
}
void MainWindow::optionsAccepted()
{
writeSettings();
readSettings();
}
void MainWindow::findNext()
{
_searchDialog->findNext();
}
bool MainWindow::save()
{
if (isUntitled) {
return saveAs();
}
else {
return saveFile(curFile);
}
}
bool MainWindow::saveAs()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),
curFile);
if (fileName.isEmpty())
return false;
return saveFile(fileName);
}
void MainWindow::saveSelectionToReadableFile()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("Save To Readable File"));
if (!fileName.isEmpty())
{
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
QMessageBox::warning(this, tr("QHexEdit"),
tr("Cannot write file %1:\n%2.")
.arg(fileName)
.arg(file.errorString()));
return;
}
QApplication::setOverrideCursor(Qt::WaitCursor);
file.write(_hexEdit->selectionToReadableString().toLatin1());
QApplication::restoreOverrideCursor();
statusBar()->showMessage(tr("File saved"), 2000);
}
}
void MainWindow::saveToReadableFile()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("Save To Readable File"));
if (!fileName.isEmpty())
{
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
QMessageBox::warning(this, tr("QHexEdit"),
tr("Cannot write file %1:\n%2.")
.arg(fileName)
.arg(file.errorString()));
return;
}
QApplication::setOverrideCursor(Qt::WaitCursor);
file.write(_hexEdit->toReadableString().toLatin1());
QApplication::restoreOverrideCursor();
statusBar()->showMessage(tr("File saved"), 2000);
}
}
void MainWindow::setAddress(qint64 address)
{
lbAddress->setText(QString("%1").arg(address, 1, 16));
}
void MainWindow::setOverwriteMode(bool mode)
{
QSettings settings;
settings.setValue("OverwriteMode", mode);
if (mode)
lbOverwriteMode->setText(tr("Overwrite"));
else
lbOverwriteMode->setText(tr("Insert"));
}
void MainWindow::setSize(qint64 size)
{
lbSize->setText(QString("%1").arg(size));
}
void MainWindow::showOptionsDialog()
{
_optionsDialog->show();
}
void MainWindow::showSearchDialog()
{
_searchDialog->show();
}
void MainWindow::showParserDialog()
{
_parserDialog->show();
}
void MainWindow::showParserWindow()
{
if (_parserDockWidget != 0)
{
if (_parserDockWidget->isVisible())
{
return; // If already visible we don't need create a new widget
}
}
// Dock widget use parser dialog
_parserDockWidget = new QDockWidget(tr("Parser"));
_parserDockWidget->setWidget(_parserDialog);
_parserDockWidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
addDockWidget(Qt::RightDockWidgetArea, _parserDockWidget);
}
/*****************************************************************************/
/* Private Methods */
/*****************************************************************************/
void MainWindow::init()
{
setAttribute(Qt::WA_DeleteOnClose);
_optionsDialog = new OptionsDialog(this);
connect(_optionsDialog, SIGNAL(accepted()), this, SLOT(optionsAccepted()));
isUntitled = true;
_hexEdit = new QHexEdit;
_hexEdit->setFixedWidth(800); // Give space left to parser dialog...
// Connect events to parser
_parserDialog = new ParserDialog(_hexEdit, this);
connect(this,SIGNAL(newFileLoaded()),_parserDialog,SLOT(on_parseButton_clicked()));
// Dock widget use parser dialog
showParserWindow();
// Used before to create horizontal space
QSplitter* mainSplitter = new QSplitter(Qt::Horizontal);
mainSplitter->addWidget(_hexEdit);
mainSplitter->setStretchFactor(1, 2);
setCentralWidget(mainSplitter);
setWindowTitle("Transportstream Analyzer");
//setCentralWidget(_hexEdit);
connect(_hexEdit, SIGNAL(overwriteModeChanged(bool)), this, SLOT(setOverwriteMode(bool)));
connect(_hexEdit, SIGNAL(dataChanged()), this, SLOT(dataChanged()));
_searchDialog = new SearchDialog(_hexEdit, this);
createActions();
createMenus();
createToolBars();
createStatusBar();
readSettings();
setUnifiedTitleAndToolBarOnMac(true);
}
void MainWindow::createActions()
{
// Open
openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
openAct->setShortcuts(QKeySequence::Open);
openAct->setStatusTip(tr("Open an existing file"));
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
// Save
saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this);
saveAct->setShortcuts(QKeySequence::Save);
saveAct->setStatusTip(tr("Save the document to disk"));
connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
saveAsAct = new QAction(tr("Save &As..."), this);
saveAsAct->setShortcuts(QKeySequence::SaveAs);
saveAsAct->setStatusTip(tr("Save the document under a new name"));
connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
saveReadable = new QAction(tr("Save &Readable..."), this);
saveReadable->setStatusTip(tr("Save document in readable form"));
connect(saveReadable, SIGNAL(triggered()), this, SLOT(saveToReadableFile()));
const QIcon exitIcon = QIcon::fromTheme("application-exit");
exitAct = new QAction(exitIcon, tr("E&xit"), this);
exitAct->setShortcuts(QKeySequence::Quit);
exitAct->setStatusTip(tr("Exit the application"));
connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
// ----Edit menu----
// Undo
undoAct = new QAction(QIcon(":/images/undo.png"), tr("&Undo"), this);
undoAct->setShortcuts(QKeySequence::Undo);
connect(undoAct, SIGNAL(triggered()), _hexEdit, SLOT(undo()));
// Redo
redoAct = new QAction(QIcon(":/images/redo.png"), tr("&Redo"), this);
redoAct->setShortcuts(QKeySequence::Redo);
connect(redoAct, SIGNAL(triggered()), _hexEdit, SLOT(redo()));
saveSelectionReadable = new QAction(tr("&Save Selection Readable..."), this);
saveSelectionReadable->setStatusTip(tr("Save selection in readable form"));
connect(saveSelectionReadable, SIGNAL(triggered()), this, SLOT(saveSelectionToReadableFile()));
aboutAct = new QAction(tr("&About"), this);
aboutAct->setStatusTip(tr("Show the application's About box"));
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
aboutQtAct = new QAction(tr("About &Qt"), this);
aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
// Find
findAct = new QAction(QIcon(":/images/find.png"), tr("&Find/Replace"), this);
findAct->setShortcuts(QKeySequence::Find);
findAct->setStatusTip(tr("Show the Dialog for finding and replacing"));
connect(findAct, SIGNAL(triggered()), this, SLOT(showSearchDialog()));
findNextAct = new QAction(tr("Find &next"), this);
findNextAct->setShortcuts(QKeySequence::FindNext);
findNextAct->setStatusTip(tr("Find next occurrence of the searched pattern"));
connect(findNextAct, SIGNAL(triggered()), this, SLOT(findNext()));
// Options
optionsAct = new QAction(tr("&Options"), this);
optionsAct->setStatusTip(tr("Show the Dialog to select applications options"));
connect(optionsAct, SIGNAL(triggered()), this, SLOT(showOptionsDialog()));
// ----View menu----
showParserWinAct = new QAction(tr("&Show Parser Window"), this);
showParserWinAct->setStatusTip(tr("Shows the transport stream parser dialog."));
connect(showParserWinAct, SIGNAL(triggered()), this, SLOT(showParserWindow()));
// Parser
_parserAct = new QAction(QIcon(":/images/parser_icon.png"), tr("&Transport stream"), this);
_parserAct->setStatusTip(tr("Show the Dialog to parse the input transport stream"));
connect(_parserAct, SIGNAL(triggered()), this, SLOT(showParserDialog()));
}
void MainWindow::createMenus()
{
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(openAct);
fileMenu->addAction(saveAct);
fileMenu->addAction(saveAsAct);
fileMenu->addAction(saveReadable);
fileMenu->addSeparator();
fileMenu->addAction(exitAct);
editMenu = menuBar()->addMenu(tr("&Edit"));
editMenu->addAction(undoAct);
editMenu->addAction(redoAct);
editMenu->addAction(saveSelectionReadable);
editMenu->addSeparator();
editMenu->addAction(findAct);
editMenu->addAction(findNextAct);
editMenu->addSeparator();
editMenu->addAction(optionsAct);
editMenu->addSeparator();
editMenu->addAction(_parserAct);
viewMenu = menuBar()->addMenu(tr("&View"));
viewMenu->addAction(showParserWinAct);
helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(aboutAct);
helpMenu->addAction(aboutQtAct);
}
void MainWindow::createStatusBar()
{
// Address Label
lbAddressName = new QLabel();
lbAddressName->setText(tr("Address:"));
statusBar()->addPermanentWidget(lbAddressName);
lbAddress = new QLabel();
lbAddress->setFrameShape(QFrame::Panel);
lbAddress->setFrameShadow(QFrame::Sunken);
lbAddress->setMinimumWidth(70);
statusBar()->addPermanentWidget(lbAddress);
connect(_hexEdit, SIGNAL(currentAddressChanged(qint64)), this, SLOT(setAddress(qint64)));
// Size Label
lbSizeName = new QLabel();
lbSizeName->setText(tr("Size:"));
statusBar()->addPermanentWidget(lbSizeName);
lbSize = new QLabel();
lbSize->setFrameShape(QFrame::Panel);
lbSize->setFrameShadow(QFrame::Sunken);
lbSize->setMinimumWidth(70);
statusBar()->addPermanentWidget(lbSize);
connect(_hexEdit, SIGNAL(currentSizeChanged(qint64)), this, SLOT(setSize(qint64)));
// Overwrite Mode Label
lbOverwriteModeName = new QLabel();
lbOverwriteModeName->setText(tr("Mode:"));
statusBar()->addPermanentWidget(lbOverwriteModeName);
lbOverwriteMode = new QLabel();
lbOverwriteMode->setFrameShape(QFrame::Panel);
lbOverwriteMode->setFrameShadow(QFrame::Sunken);
lbOverwriteMode->setMinimumWidth(70);
statusBar()->addPermanentWidget(lbOverwriteMode);
setOverwriteMode(_hexEdit->overwriteMode());
statusBar()->showMessage(tr("Ready"), 2000);
}
void MainWindow::createToolBars()
{
fileToolBar = addToolBar(tr("File"));
fileToolBar->addAction(openAct);
fileToolBar->addAction(saveAct);
editToolBar = addToolBar(tr("Edit"));
editToolBar->addAction(undoAct);
editToolBar->addAction(redoAct);
editToolBar->addAction(findAct);
_parserToolBar = addToolBar(tr("Parser"));
_parserToolBar->addAction(_parserAct);
}
void MainWindow::loadFile(const QString &fileName)
{
file.setFileName(fileName);
if (!_hexEdit->setData(file)) {
QMessageBox::warning(this, tr("QHexEdit"),
tr("Cannot read file %1:\n%2.")
.arg(fileName)
.arg(file.errorString()));
return;
}
setCurrentFile(fileName);
statusBar()->showMessage(tr("File loaded"), 2000);
emitNewFileLoaded();
}
void MainWindow::readSettings()
{
QSettings settings;
QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
QSize size = settings.value("size", QSize(610, 460)).toSize();
move(pos);
resize(size);
_hexEdit->setAddressArea(settings.value("AddressArea").toBool());
_hexEdit->setAsciiArea(settings.value("AsciiArea").toBool());
_hexEdit->setHighlighting(settings.value("Highlighting").toBool());
_hexEdit->setOverwriteMode(settings.value("OverwriteMode").toBool());
_hexEdit->setReadOnly(settings.value("ReadOnly").toBool());
_hexEdit->setHighlightingColor(settings.value("HighlightingColor").value<QColor>());
_hexEdit->setAddressAreaColor(settings.value("AddressAreaColor").value<QColor>());
_hexEdit->setSelectionColor(settings.value("SelectionColor").value<QColor>());
_hexEdit->setFont(settings.value("WidgetFont").value<QFont>());
_hexEdit->setAddressWidth(settings.value("AddressAreaWidth").toInt());
_hexEdit->setBytesPerLine(settings.value("BytesPerLine").toInt());
}
bool MainWindow::saveFile(const QString &fileName)
{
QString tmpFileName = fileName + ".~tmp";
QApplication::setOverrideCursor(Qt::WaitCursor);
QFile file(tmpFileName);
bool ok = _hexEdit->write(file);
if (QFile::exists(fileName))
ok = QFile::remove(fileName);
if (ok)
{
file.setFileName(tmpFileName);
ok = file.copy(fileName);
if (ok)
ok = QFile::remove(tmpFileName);
}
QApplication::restoreOverrideCursor();
if (!ok) {
QMessageBox::warning(this, tr("QHexEdit"),
tr("Cannot write file %1.")
.arg(fileName));
return false;
}
setCurrentFile(fileName);
statusBar()->showMessage(tr("File saved"), 2000);
return true;
}
void MainWindow::setCurrentFile(const QString &fileName)
{
curFile = QFileInfo(fileName).canonicalFilePath();
isUntitled = fileName.isEmpty();
setWindowModified(false);
if (fileName.isEmpty())
setWindowFilePath("TsAnalyzer");
else
setWindowFilePath(curFile + " - TsAnalyzer");
_fileName = curFile.mid(curFile.lastIndexOf("/") + 1);
_parserDialog->setFileName(_fileName);
}
QString MainWindow::strippedName(const QString &fullFileName)
{
return QFileInfo(fullFileName).fileName();
}
void MainWindow::writeSettings()
{
QSettings settings;
settings.setValue("pos", pos());
settings.setValue("size", size());
}
void MainWindow::emitNewFileLoaded()
{
emit newFileLoaded();
}