-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
213 lines (185 loc) · 6.96 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "gstreamerlogwidget.h"
#include "preferences.h"
#include <QtCore/QSettings>
#include <QtCore/QScopeGuard>
#include <QtGui/QDesktopServices>
#include <QtWidgets/QMessageBox>
#include <QtWidgets/QFileDialog>
#include <QtWidgets/QProgressBar>
class MainWindow::Private : public Ui::MainWindow
{
public:
Private(::MainWindow *parent);
~Private();
private:
void tabCountChanged(int index);
void openFile(const QString &fileName);
private:
::MainWindow *q;
QSettings settings;
};
MainWindow::Private::Private(::MainWindow *parent)
: q(parent)
{
setupUi(q);
statusbar->addPermanentWidget(progressBar);
statusbar->addPermanentWidget(counts);
progressBar->setVisible(false);
connect(readme, &QTextBrowser::anchorClicked, [](const QUrl &url) {
QDesktopServices::openUrl(url);
});
connect(tabWidget, &QTabWidget::currentChanged, [this](int index) {
tabCountChanged(index);
});
tabWidget->clear();
connect(tabWidget, &QTabWidget::tabCloseRequested, [this](int index) {
tabWidget->widget(index)->deleteLater();
tabWidget->removeTab(index);
});
connect(tabWidget, &QTabWidget::currentChanged, [this](int index) {
QString text;
if (index >= 0) {
auto widget = tabWidget->widget(index);
auto tableView = qobject_cast<GStreamerLogWidget *>(widget);
if (tableView)
text = QStringLiteral("%1/%2").arg(tableView->filteredCount()).arg(tableView->count());
}
counts->setText(text);
});
settings.beginGroup(q->metaObject()->className());
q->restoreGeometry(settings.value(QStringLiteral("geometry")).toByteArray());
q->restoreState(settings.value(QStringLiteral("state")).toByteArray());
connect(open, &QAction::triggered, [this]() {
const auto caption = tr("Open GStreamer log file");
QFileDialog dialog(q, caption);
dialog.setFileMode(QFileDialog::ExistingFiles);
QStringList nameFilters = {
tr("GStreamer log files (*.log *.txt)"),
tr("All Files (*)"),
};
dialog.setNameFilters(nameFilters);
dialog.setAcceptMode(QFileDialog::AcceptOpen);
if (dialog.exec() != QDialog::Accepted)
return;
openFile(dialog.selectedFiles().first());
});
const auto recentFiles = settings.value(QStringLiteral("recentFiles")).toStringList();
openRecent->setEnabled(!recentFiles.isEmpty());
QStringList recentFilesExists;
for (const auto &file : recentFiles) {
if (QFile::exists(file)) {
const auto action = openRecent->addAction(file);
connect(action, &QAction::triggered, [this, file]() {
openFile(file);
});
if (qEnvironmentVariableIsSet("GLV_OPEN_FIRST")) {
static bool first = true;
if (first) {
first= false;
openFile(file);
}
}
recentFilesExists.append(file);
}
}
settings.setValue(QStringLiteral("recentFiles"), recentFilesExists);
connect(reload, &QAction::triggered, [this]() {
static_cast<GStreamerLogWidget *>(tabWidget->currentWidget())->reload();
});
connect(preferences, &QAction::triggered, [this]() {
Preferences dialog(q);
dialog.exec();
});
connect(close, &QAction::triggered, [this]() {
tabWidget->currentWidget()->deleteLater();
tabWidget->removeTab(tabWidget->currentIndex());
});
connect(quit, &QAction::triggered, q, &QMainWindow::close);
connect(aboutApp, &QAction::triggered, [this]() {
QString title = tr("About %1").arg(q->windowTitle());
QString description = tr("%1 is a tool to view log from GStreamer(e.g. GST_DEBUG=4)").arg(q->windowTitle());
QMessageBox::about(q, title, description);
});
connect(aboutQt, &QAction::triggered, [this]() {
QMessageBox::aboutQt(q);
});
}
MainWindow::Private::~Private()
{
settings.setValue(QStringLiteral("geometry"), q->saveGeometry());
settings.setValue(QStringLiteral("state"), q->saveState());
}
void MainWindow::Private::tabCountChanged(int index) {
const bool empty = index < 0;
readme->setVisible(empty);
reload->setEnabled(!empty);
close->setEnabled(!empty);
tabWidget->setVisible(!empty);
}
void MainWindow::Private::openFile(const QString &fileName) {
auto recentFiles = settings.value(QStringLiteral("recentFiles")).toStringList();
if (recentFiles.contains(fileName))
recentFiles.removeAll(fileName);
auto cleanup = qScopeGuard([&] {
while (recentFiles.count() > 10)
recentFiles.removeLast();
settings.setValue(QStringLiteral("recentFiles"), recentFiles);
});
for (int i = 0; i < tabWidget->count(); ++i) {
auto tabToolTip = tabWidget->tabToolTip(i);
if (tabToolTip == fileName) {
tabWidget->setCurrentIndex(i);
recentFiles.prepend(fileName);
return;
}
}
QFileInfo fileInfo(fileName);
if (!fileInfo.exists()) {
statusbar->showMessage(tr("File does not exist: %1").arg(fileName), 10000);
return;
}
QGuiApplication::setOverrideCursor(Qt::BusyCursor);
auto tableView = new GStreamerLogWidget(fileName);
QGuiApplication::restoreOverrideCursor();
connect(tableView, &GStreamerLogWidget::busyChanged, [this](bool busy) {
progressBar->setVisible(busy);
progressBar->setMaximum(100);
if (busy)
QGuiApplication::setOverrideCursor(Qt::BusyCursor);
else
QGuiApplication::restoreOverrideCursor();
});
connect(tableView, &GStreamerLogWidget::progressChanged, [this](int progress) {
if (progress == 100) {
progressBar->setMaximum(0);
} else {
progressBar->setMaximum(100);
progressBar->setValue(progress);
}
QGuiApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
});
connect(tableView, &GStreamerLogWidget::openPreferences, [this](const QString &focus) {
Preferences dialog(q);
dialog.setCurrentField(focus);
dialog.exec();
});
connect(tableView, &GStreamerLogWidget::errorOccurred, [this](const QString &message) {
statusbar->showMessage(message, 10000);
});
connect(tableView, &GStreamerLogWidget::filteredCountChanged, [tableView, this](int count) {
counts->setText(QStringLiteral("%1/%2").arg(count).arg(tableView->count()));
});
counts->setText(QStringLiteral("%1/%2").arg(tableView->filteredCount()).arg(tableView->count()));
int index = tabWidget->addTab(tableView, fileInfo.fileName());
tabWidget->setCurrentIndex(index);
tabWidget->setTabToolTip(index, fileName);
recentFiles.prepend(fileName);
};
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, d(new Private(this))
{
}
MainWindow::~MainWindow() = default;