-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgstreamerlogwidget.cpp
229 lines (204 loc) · 8.03 KB
/
gstreamerlogwidget.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
#include "gstreamerlogwidget.h"
#include "ui_gstreamerlogwidget.h"
#include "gstreamerlogmodel.h"
#include "customfilterproxymodel.h"
#include <QtCore/QDir>
#include <QtCore/QProcess>
#include <QtCore/QSettings>
#include <QtCore/QTimer>
#include <QtGui/QShortcut>
namespace {
QString searchFile(const QDir &directory, const QString &fileName)
{
if (directory.exists(fileName))
return directory.absoluteFilePath(fileName);
QStringList directoryNames = directory.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
// 各エントリを走査
for (const QString & directoryName : directoryNames) {
QDir dir(directory.absoluteFilePath(directoryName));
auto result = searchFile(dir, fileName);
if (!result.isEmpty())
return result;
}
return QString();
}
}
class GStreamerLogWidget::Private : public Ui::GStreamerLogWidget
{
public:
Private(const QString &fileName, ::GStreamerLogWidget *parent);
~Private();
private:
void open(const QString &fileName, int line) const;
private:
::GStreamerLogWidget *q;
QString fileName;
struct SearchResults {
QString text;
QModelIndex index;
} searchResults;
public:
GStreamerLogModel model;
CustomFilterProxyModel proxyModel;
bool busy = false;
QSettings settings;
};
GStreamerLogWidget::Private::Private(const QString &fileName, ::GStreamerLogWidget *parent)
: q(parent)
, fileName(fileName)
, model(fileName)
{
settings.beginGroup(q->metaObject()->className());
setupUi(q);
tableView->setModel(&proxyModel);
connect(tableView, &GStreamerLogView::jumpToLog, [this](int line) {
open(this->fileName, line);
});
connect(tableView, &GStreamerLogView::jumpToSource, [this](const QString &source, int line) {
QSettings settings;
settings.beginGroup("Preferences");
if (!settings.contains(QStringLiteral("gstreamerSourceDirectory"))) {
emit q->openPreferences(QStringLiteral("gstreamerSourceDirectory"));
}
if (settings.contains(QStringLiteral("gstreamerSourceDirectory"))) {
const auto directory = settings.value(QStringLiteral("gstreamerSourceDirectory")).toString();
const auto path = searchFile(QDir(directory), source);
if (path.isEmpty()) {
emit q->errorOccurred(tr("Source file \"%1\" not found under \"%2\"").arg(source).arg(directory));
} else {
open(path, line);
}
}
});
connect(tableView, &GStreamerLogView::activated, [this](const QString &text) {
auto filterText = filter->text();
if (filterText.isEmpty()) {
filterText = text;
} else if (!filterText.contains(text.trimmed())) {
if (!filterText.endsWith(QLatin1Char(' '))) {
filterText += QLatin1Char(' ');
}
filterText += text;
} else {
return;
}
filter->setText(filterText);
filter->setFocus();
QTimer::singleShot(100, filter, &QLineEdit::returnPressed);
});
timestampView->setBuddy(tableView);
proxyModel.setSourceModel(&model);
connect(&proxyModel, &CustomFilterProxyModel::layoutAboutToBeChanged, [this]() {
q->setBusy(true);
});
connect(&proxyModel, &CustomFilterProxyModel::layoutChanged, [this]() {
q->filteredCountChanged(proxyModel.rowCount());
QTimer::singleShot(10, [this]() {
q->setBusy(false);
});
});
connect(&proxyModel, &CustomFilterProxyModel::progressChanged, q, &::GStreamerLogWidget::progressChanged);
splitter->restoreState(settings.value(QStringLiteral("splitterState")).toByteArray());
auto shortcut = new QShortcut(QKeySequence(tr("Ctrl+L", "Filter")), q);
connect(shortcut, &QShortcut::activated, [this]() {
filter->setFocus();
});
connect(filter, &QLineEdit::returnPressed, [this]() {
const int headerHeight = tableView->horizontalHeader()->height();
const auto firstIndex = tableView->indexAt(QPoint(0, 0));
const auto lastIndex = tableView->indexAt(QPoint(0, tableView->contentsRect().height()));
auto currentIndex = tableView->currentIndex();
if (currentIndex.row() < firstIndex.row() || lastIndex.row() < currentIndex.row()) {
currentIndex = currentIndex.siblingAtRow((firstIndex.row() + lastIndex.row()) / 2);
}
const auto timestamp = Timestamp::fromString(currentIndex.siblingAtColumn(GStreamerLogModel::TimestampColumn).data().toString());
proxyModel.setFilter(filter->text());
const auto indices = proxyModel.match(QModelIndex(), Qt::DisplayRole, QVariant::fromValue(timestamp), 1, Qt::MatchStartsWith); // abuse the flag for nearest timestamp match
if (!indices.isEmpty()) {
const auto index = indices.first().siblingAtColumn(currentIndex.column());
tableView->setCurrentIndex(index);
QTimer::singleShot(10, [this]() {
tableView->scrollTo(tableView->currentIndex().siblingAtColumn(GStreamerLogModel::TimestampColumn), QTableView::PositionAtCenter);
});
}
});
shortcut = new QShortcut(QKeySequence(tr("Ctrl+F", "Find")), q);
connect(shortcut, &QShortcut::activated, [this]() {
find->setFocus();
});
connect(find, &LineEdit::activated, [this](Qt::KeyboardModifiers modifiers) {
const auto text = find->text();
searchResults.text = text;
auto start = searchResults.index;
if (!start.isValid() || start != tableView->currentIndex())
start = tableView->currentIndex();
if (!start.isValid())
start = proxyModel.index(0, 0, QModelIndex());
q->setBusy(true);
Qt::MatchFlags flags = Qt::MatchContains | Qt::MatchWrap;
if (modifiers & Qt::ShiftModifier)
flags |= Qt::MatchRecursive; // abuse recursive flag for backwards search
const auto indices = proxyModel.match(start, Qt::DisplayRole, text, 1, flags);
q->setBusy(false);
searchResults.index = indices.isEmpty() ? QModelIndex() : indices.first();
if (!searchResults.index.isValid())
return;
tableView->setCurrentIndex(searchResults.index);
tableView->selectionModel()->select(searchResults.index, QItemSelectionModel::ClearAndSelect);
tableView->scrollTo(searchResults.index);
});
}
GStreamerLogWidget::Private::~Private()
{
settings.setValue(QStringLiteral("splitterState"), splitter->saveState());
}
void GStreamerLogWidget::Private::open(const QString &fileName, int line) const
{
QSettings settings;
settings.beginGroup("Preferences");
if (settings.contains(QStringLiteral("externalTextEditor"))) {
const auto method = settings.value(QStringLiteral("externalTextEditor")).toString();
if (!method.isEmpty()) {
QString commandline = method;
commandline.replace(QStringLiteral("%f"), fileName);
commandline.replace(QStringLiteral("%l"), QString::number(line));
auto list = QProcess::splitCommand(commandline);
QProcess::startDetached(list.takeFirst(), list);
} else {
#ifdef Q_OS_WIN
QProcess::startDetached(QStringLiteral("start"), {fileName});
#elif defined(Q_OS_MAC)
QProcess::startDetached(QStringLiteral("open"), {fileName});
#elif defined(Q_OS_LINUX)
QProcess::startDetached(QStringLiteral("xdg-open"), {fileName});
#endif
}
}
}
GStreamerLogWidget::GStreamerLogWidget(const QString &fileName, QWidget *parent)
: QWidget(parent)
, d(new Private(fileName, this))
{}
GStreamerLogWidget::~GStreamerLogWidget() = default;
bool GStreamerLogWidget::isBusy() const
{
return d->busy;
}
void GStreamerLogWidget::setBusy(bool busy)
{
if (d->busy == busy) return;
d->busy = busy;
emit busyChanged(busy);
}
int GStreamerLogWidget::count() const
{
return d->model.rowCount();
}
int GStreamerLogWidget::filteredCount() const
{
return d->proxyModel.rowCount();
}
void GStreamerLogWidget::reload()
{
d->model.reload();
}