-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
307 lines (271 loc) · 10.2 KB
/
mainwindow.cpp
File metadata and controls
307 lines (271 loc) · 10.2 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
// Copyright (C) 2025 Pedro López-Cabanillas
// SPDX-License-Identifier: GPL-3.0-or-later
#include "mainwindow.h"
#include <QActionGroup>
#include <QApplication>
#include <QFile>
#include <QFileDialog>
#include <QFileInfo>
#include <QLocale>
#include <QMenu>
#include <QMenuBar>
#include <QMessageBox>
#include <QMimeData>
#include <QScreen>
#include <QSettings>
#include <QStatusBar>
#include <algorithm>
#include "QHexView/model/buffer/qmemorybuffer.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow{parent}
, m_treeview{new QTreeView(this)}
, m_hexview{new QHexView(this)}
{
m_treeview->setModel(m_treemodel);
m_hexview->setDocument(m_hexdoc);
m_hexview->setReadOnly(true);
m_splitter = new QSplitter(this);
m_splitter->addWidget(m_treeview);
m_splitter->addWidget(m_hexview);
m_splitter->setSizes({333, 666});
setCentralWidget(m_splitter);
statusBar()->setSizeGripEnabled(true);
createActions();
createMenus();
setWindowIcon(QIcon(":/RiffTree.png"));
setMinimumSize(666, 666);
const auto screenSize = screen()->availableSize();
resize({screenSize.width() / 2, screenSize.height() * 2 / 3});
connect(m_treeview, &QTreeView::clicked, this, &MainWindow::treeItemClicked);
updateWindowTitle();
readSettings();
}
void MainWindow::openFile(const QString fileName)
{
QFile file(fileName);
bool Ok = file.open(QIODevice::ReadOnly);
if (Ok) {
// QFile::map doesn't allow options like MAP_HUGETLB, MAP_PRIVATE or MAP_LOCKED
// but it is more portable between different operating systems than mmap().
// Previously, we tried to use MAP_HUGETLB with mmap() syscall but it is only
// valid for anonymous memory.
uint8_t *buffer = file.map(0, file.size());
if (buffer == nullptr) {
QMessageBox::warning(this, qApp->applicationName(), file.errorString());
file.close();
return;
}
delete m_treemodel;
m_treemodel = new TreeModel(this);
m_treeview->setModel(m_treemodel);
m_treeview->setSelectionMode(QAbstractItemView::SingleSelection);
m_treeview->setColumnWidth(0, 100);
m_treeview->setColumnWidth(1, 66);
m_treeview->setColumnWidth(2, 66);
m_hexview->setDocument(nullptr);
if (m_treemodel->loadData(buffer)) {
m_hexdoc = QHexDocument::fromMemory<QMemoryBuffer>(reinterpret_cast<char *>(buffer),
file.size());
m_hexview->setDocument(m_hexdoc);
m_treeview->expandAll();
m_treeview->resizeColumnToContents(0);
m_openFileName = QFileInfo(fileName).fileName();
updateWindowTitle();
} else {
QMessageBox::warning(this,
qApp->applicationName(),
tr("%1 is not a valid RIFF file").arg(fileName));
}
file.unmap(buffer);
file.close();
}
}
void MainWindow::open()
{
QString selectedFilter;
QString fileName = QFileDialog::getOpenFileName(
this,
tr("Open File"),
m_openFileName,
tr("All Files (*);;Riff Files (*.dls *.sf2 *.sf3 *.avi *.wav "
"*.rmi *.cdr *.ani *.pal *.webp)"),
&selectedFilter,
QFileDialog::ReadOnly);
if (!fileName.isEmpty()) {
openFile(fileName);
}
}
void MainWindow::updateWindowTitle()
{
setWindowTitle(tr("%1 [%2]").arg(qApp->applicationName()).arg(m_openFileName));
}
void MainWindow::retranslate()
{
QLocale locale(m_currentLang);
if (qtTranslator.load(locale, "qt", "_", ":/")) {
QCoreApplication::installTranslator(&qtTranslator);
}
if (appTranslator.load(locale, qApp->applicationName(), "_", ":/")) {
QCoreApplication::installTranslator(&appTranslator);
}
// retranslate the menus
fileMenu->setTitle(tr("&File"));
editMenu->setTitle(tr("&Edit"));
helpMenu->setTitle(tr("&Help"));
languageMenu->setTitle(tr("&Language"));
openAct->setText(tr("&Open..."));
openAct->setStatusTip(tr("Open an existing file"));
exitAct->setText(tr("E&xit"));
exitAct->setStatusTip(tr("Exit the application"));
aboutAct->setText(tr("&About"));
aboutAct->setStatusTip(tr("Show the application's About box"));
aboutQtAct->setText(tr("About &Qt"));
aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
findAct->setText(tr("Find..."));
findAct->setStatusTip(tr("Show the Find dialog"));
}
void MainWindow::readSettings()
{
QSettings settings;
QByteArray geometry = settings.value("geometry").toByteArray();
if (!geometry.isEmpty()) {
restoreGeometry(geometry);
}
auto lang = settings.value("language").toString();
if (!lang.isEmpty()) {
m_currentLang = lang;
}
auto lngActions = languageMenu->actions();
auto it = std::find_if(lngActions.begin(), lngActions.end(), [=](QAction *action) {
return action->data().toString() == m_currentLang;
});
if (it != lngActions.end()) {
(*it)->setChecked(true);
}
retranslate();
}
void MainWindow::changeLanguage(QAction *action)
{
if (action) {
m_currentLang = action->data().toString();
retranslate();
QMessageBox::information(this,
tr("Program Translation"),
tr("Please restart the program\n"
"to complete the translation."));
}
}
void MainWindow::about()
{
QMessageBox::about(this,
tr("About RiffTree"),
tr("RiffTree is a RIFF file structure viewer\n"
"with hex content view"));
}
void MainWindow::treeItemClicked(const QModelIndex &index)
{
// QString title = m_treemodel->data(index.sibling(index.row(), 0), Qt::DisplayRole).toString();
qint64 offs = m_treemodel->data(index.sibling(index.row(), 1), Qt::DisplayRole).toLongLong();
qint64 size = m_treemodel->data(index.sibling(index.row(), 2), Qt::DisplayRole).toLongLong()
+ 2 * sizeof(uint32_t);
// qDebug() << Q_FUNC_INFO << title << "offset:" << offs << "size:" << size;
// m_hexview->clearMetadata();
// m_hexview->hexCursor()->move(offs);
// m_hexview->setMetadata(offs, offs + size, Qt::black, Qt::yellow, title);
m_hexview->hexCursor()->clearSelection();
m_hexview->hexCursor()->move(offs);
m_hexview->hexCursor()->select(offs);
m_hexview->hexCursor()->selectSize(size);
m_hexview->update();
}
void MainWindow::createActions()
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QIcon openIcon = QIcon::fromTheme("document-open");
#else
QIcon openIcon = QIcon::fromTheme(QIcon::ThemeIcon::DocumentOpen);
#endif
openAct = new QAction(openIcon, tr("&Open..."), this);
openAct->setShortcuts(QKeySequence::Open);
openAct->setStatusTip(tr("Open an existing file"));
connect(openAct, &QAction::triggered, this, &MainWindow::open);
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QIcon exitIcon = QIcon::fromTheme("application-exit");
#else
QIcon exitIcon = QIcon::fromTheme(QIcon::ThemeIcon::ApplicationExit);
#endif
exitAct = new QAction(exitIcon, tr("E&xit"), this);
exitAct->setShortcuts(QKeySequence::Quit);
exitAct->setStatusTip(tr("Exit the application"));
connect(exitAct, &QAction::triggered, this, &QWidget::close);
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QIcon helpIcon = QIcon::fromTheme("help-about");
#else
QIcon helpIcon = QIcon::fromTheme(QIcon::ThemeIcon::HelpAbout);
#endif
aboutAct = new QAction(helpIcon, tr("&About"), this);
aboutAct->setStatusTip(tr("Show the application's About box"));
connect(aboutAct, &QAction::triggered, this, &MainWindow::about);
QIcon aboutQtIcon = qApp->style()->standardIcon(QStyle::SP_TitleBarMenuButton);
aboutQtAct = new QAction(aboutQtIcon, tr("About &Qt"), this);
aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
connect(aboutQtAct, &QAction::triggered, qApp, &QApplication::aboutQt);
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QIcon findIcon = QIcon::fromTheme("edit-find");
#else
QIcon findIcon = QIcon::fromTheme(QIcon::ThemeIcon::EditFind);
#endif
findAct = new QAction(findIcon, tr("Find..."), this);
findAct->setStatusTip(tr("Show the Find dialog"));
connect(findAct, &QAction::triggered, m_hexview, &QHexView::showFind);
}
void MainWindow::createMenus()
{
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(openAct);
fileMenu->addSeparator();
fileMenu->addAction(exitAct);
editMenu = menuBar()->addMenu(tr("&Edit"));
editMenu->addAction(findAct);
helpMenu = menuBar()->addMenu(tr("&Help"));
languageMenu = helpMenu->addMenu(tr("&Language"));
auto languageGroup = new QActionGroup(this);
languageGroup->setExclusive(true);
auto enAct = languageGroup->addAction(
QStringLiteral(u"English")); // Native name, do not translate
enAct->setData(QLatin1String("en_US"));
enAct->setCheckable(true);
auto esAct = languageGroup->addAction(
QStringLiteral(u"español")); // Native name, do not translate
esAct->setData(QLatin1String("es_ES"));
esAct->setCheckable(true);
connect(languageGroup, &QActionGroup::triggered, this, &MainWindow::changeLanguage);
languageMenu->addActions(languageGroup->actions());
helpMenu->addSeparator();
helpMenu->addAction(aboutAct);
helpMenu->addAction(aboutQtAct);
}
void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasUrls()) {
event->acceptProposedAction();
}
}
void MainWindow::dropEvent(QDropEvent *event)
{
QStringList types{"dls", "sf2", "sf3", "avi", "wav", "rmi", "cdr", "ani", "pal", "webp"};
foreach (const QUrl &url, event->mimeData()->urls()) {
QString fname = url.toLocalFile();
QFileInfo info(fname);
if (info.exists() && types.contains(info.suffix().trimmed(), Qt::CaseInsensitive)) {
openFile(fname);
}
}
}
void MainWindow::closeEvent(QCloseEvent *event)
{
QSettings settings;
settings.setValue("geometry", saveGeometry());
settings.setValue("language", m_currentLang);
QMainWindow::closeEvent(event);
}