]> git.cworth.org Git - apitrace/blob - gui/mainwindow.cpp
Allow setting of the filter options.
[apitrace] / gui / mainwindow.cpp
1 #include "mainwindow.h"
2
3 #include "apitrace.h"
4 #include "apitracecall.h"
5 #include "apicalldelegate.h"
6 #include "apitracemodel.h"
7 #include "apitracefilter.h"
8 #include "settingsdialog.h"
9
10 #include <qjson/parser.h>
11
12 #include <QAction>
13 #include <QDebug>
14 #include <QDir>
15 #include <QFileDialog>
16 #include <QLineEdit>
17 #include <QMessageBox>
18 #include <QProcess>
19 #include <QProgressBar>
20 #include <QToolBar>
21 #include <QWebView>
22
23
24 MainWindow::MainWindow()
25     : QMainWindow(),
26       m_replayProcess(0),
27       m_selectedEvent(0),
28       m_stateEvent(0),
29       m_findingState(false),
30       m_jsonParser(new QJson::Parser())
31 {
32     m_ui.setupUi(this);
33     m_ui.stateTreeWidget->sortByColumn(0, Qt::AscendingOrder);
34
35     m_trace = new ApiTrace();
36     connect(m_trace, SIGNAL(startedLoadingTrace()),
37             this, SLOT(startedLoadingTrace()));
38     connect(m_trace, SIGNAL(finishedLoadingTrace()),
39             this, SLOT(finishedLoadingTrace()));
40
41     m_model = new ApiTraceModel();
42     m_model->setApiTrace(m_trace);
43     m_proxyModel = new ApiTraceFilter();
44     m_proxyModel->setSourceModel(m_model);
45     m_ui.callView->setModel(m_proxyModel);
46     m_ui.callView->setItemDelegate(new ApiCallDelegate);
47     m_ui.callView->resizeColumnToContents(0);
48     m_ui.callView->header()->swapSections(0, 1);
49     m_ui.callView->setColumnWidth(1, 42);
50
51     QToolBar *toolBar = addToolBar(tr("Navigation"));
52     m_filterEdit = new QLineEdit(toolBar);
53     toolBar->addWidget(m_filterEdit);
54
55     m_progressBar = new QProgressBar();
56     m_progressBar->setRange(0, 0);
57     statusBar()->addPermanentWidget(m_progressBar);
58     m_progressBar->hide();
59
60     m_ui.detailsDock->hide();
61     m_ui.stateDock->hide();
62
63     connect(m_ui.actionOpen, SIGNAL(triggered()),
64             this, SLOT(openTrace()));
65     connect(m_ui.actionQuit, SIGNAL(triggered()),
66             this, SLOT(close()));
67
68     connect(m_ui.actionReplay, SIGNAL(triggered()),
69             this, SLOT(replayStart()));
70     connect(m_ui.actionStop, SIGNAL(triggered()),
71             this, SLOT(replayStop()));
72     connect(m_ui.actionLookupState, SIGNAL(triggered()),
73             this, SLOT(lookupState()));
74        connect(m_ui.actionOptions, SIGNAL(triggered()),
75             this, SLOT(showSettings()));
76
77     connect(m_ui.callView, SIGNAL(activated(const QModelIndex &)),
78             this, SLOT(callItemSelected(const QModelIndex &)));
79     connect(m_filterEdit, SIGNAL(returnPressed()),
80             this, SLOT(filterTrace()));
81 }
82
83 void MainWindow::openTrace()
84 {
85     QString fileName =
86         QFileDialog::getOpenFileName(
87             this,
88             tr("Open Trace"),
89             QDir::homePath(),
90             tr("Trace Files (*.trace)"));
91
92     qDebug()<< "File name : " <<fileName;
93
94     newTraceFile(fileName);
95 }
96
97 void MainWindow::loadTrace(const QString &fileName)
98 {
99     if (!QFile::exists(fileName)) {
100         QMessageBox::warning(this, tr("File Missing"),
101                              tr("File '%1' doesn't exist.").arg(fileName));
102         return;
103     }
104     qDebug()<< "Loading  : " <<fileName;
105
106     m_progressBar->setValue(0);
107     newTraceFile(fileName);
108 }
109
110 void MainWindow::callItemSelected(const QModelIndex &index)
111 {
112     ApiTraceEvent *event =
113         index.data(ApiTraceModel::EventRole).value<ApiTraceEvent*>();
114
115     if (event && event->type() == ApiTraceEvent::Call) {
116         ApiTraceCall *call = static_cast<ApiTraceCall*>(event);
117         m_ui.detailsWebView->setHtml(call->toHtml());
118         m_ui.detailsDock->show();
119         m_selectedEvent = call;
120     } else {
121         if (event && event->type() == ApiTraceEvent::Frame) {
122             m_selectedEvent = static_cast<ApiTraceFrame*>(event);
123         } else
124             m_selectedEvent = 0;
125         m_ui.detailsDock->hide();
126     }
127     if (m_selectedEvent && !m_selectedEvent->state().isEmpty()) {
128         fillStateForFrame();
129     } else
130         m_ui.stateDock->hide();
131 }
132
133 void MainWindow::filterTrace()
134 {
135     m_proxyModel->setFilterString(m_filterEdit->text());
136 }
137
138 void MainWindow::replayStart()
139 {
140     replayTrace(false);
141 }
142
143 void MainWindow::replayStop()
144 {
145     if (m_replayProcess) {
146         m_replayProcess->kill();
147
148         m_ui.actionStop->setEnabled(false);
149         m_ui.actionReplay->setEnabled(true);
150         m_ui.actionLookupState->setEnabled(true);
151     }
152 }
153
154 void MainWindow::newTraceFile(const QString &fileName)
155 {
156     m_traceFileName = fileName;
157     m_trace->setFileName(fileName);
158
159     if (m_traceFileName.isEmpty()) {
160         m_ui.actionReplay->setEnabled(false);
161         m_ui.actionLookupState->setEnabled(false);
162     } else {
163         m_ui.actionReplay->setEnabled(true);
164         m_ui.actionLookupState->setEnabled(true);
165     }
166 }
167
168 void MainWindow::replayFinished()
169 {
170     m_ui.actionStop->setEnabled(false);
171     m_ui.actionReplay->setEnabled(true);
172     m_ui.actionLookupState->setEnabled(true);
173
174     QByteArray output = m_replayProcess->readAllStandardOutput();
175
176 #if 0
177     qDebug()<<"Process finished = ";
178     qDebug()<<"\terr = "<<m_replayProcess->readAllStandardError();
179     qDebug()<<"\tout = "<<output;
180 #endif
181
182     if (m_findingState) {
183         bool ok = false;
184         QVariantMap parsedJson = m_jsonParser->parse(output, &ok).toMap();
185         parseState(parsedJson[QLatin1String("parameters")].toMap());
186     } else if (output.length() < 80) {
187         statusBar()->showMessage(output);
188     }
189     m_stateEvent = 0;
190     m_findingState = false;
191 }
192
193 void MainWindow::replayError(QProcess::ProcessError err)
194 {
195     m_ui.actionStop->setEnabled(false);
196     m_ui.actionReplay->setEnabled(true);
197     m_ui.actionLookupState->setEnabled(true);
198     m_findingState = false;
199     m_stateEvent = 0;
200
201     qDebug()<<"Process error = "<<err;
202     qDebug()<<"\terr = "<<m_replayProcess->readAllStandardError();
203     qDebug()<<"\tout = "<<m_replayProcess->readAllStandardOutput();
204     QMessageBox::warning(
205         this, tr("Replay Failed"),
206         tr("Couldn't execute the replay file '%1'").arg(m_traceFileName));
207 }
208
209 void MainWindow::startedLoadingTrace()
210 {
211     Q_ASSERT(m_trace);
212     m_progressBar->show();
213     QFileInfo info(m_trace->fileName());
214     statusBar()->showMessage(
215         tr("Loading %1...").arg(info.fileName()));
216 }
217
218 void MainWindow::finishedLoadingTrace()
219 {
220     m_progressBar->hide();
221     if (!m_trace) {
222         return;
223     }
224     QFileInfo info(m_trace->fileName());
225     statusBar()->showMessage(
226         tr("Loaded %1").arg(info.fileName()), 3000);
227 }
228
229 void MainWindow::replayTrace(bool dumpState)
230 {
231     if (!m_replayProcess) {
232 #ifdef Q_OS_WIN
233         QString format = QLatin1String("%1;");
234 #else
235         QString format = QLatin1String("%1:");
236 #endif
237         QString buildPath = format.arg(BUILD_DIR);
238         QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
239         env.insert("PATH", buildPath + env.value("PATH"));
240
241         qputenv("PATH", env.value("PATH").toLatin1());
242
243         m_replayProcess = new QProcess(this);
244         m_replayProcess->setProcessEnvironment(env);
245
246         connect(m_replayProcess, SIGNAL(finished(int, QProcess::ExitStatus)),
247                 this, SLOT(replayFinished()));
248         connect(m_replayProcess, SIGNAL(error(QProcess::ProcessError)),
249                 this, SLOT(replayError(QProcess::ProcessError)));
250     }
251
252     if (m_traceFileName.isEmpty())
253         return;
254
255     QStringList arguments;
256     if (dumpState &&
257         m_selectedEvent) {
258         int index = 0;
259         if (m_selectedEvent->type() == ApiTraceEvent::Call) {
260             index = static_cast<ApiTraceCall*>(m_selectedEvent)->index;
261         } else if (m_selectedEvent->type() == ApiTraceEvent::Frame) {
262             ApiTraceFrame *frame = static_cast<ApiTraceFrame*>(m_selectedEvent);
263             if (frame->calls.isEmpty()) {
264                 //XXX i guess we could still get the current state
265                 qDebug()<<"tried to get a state for an empty frame";
266                 return;
267             }
268             index = frame->calls.first()->index;
269         } else {
270             qDebug()<<"Unknown event type";
271             return;
272         }
273         arguments << QLatin1String("-D");
274         arguments << QString::number(index);
275     }
276     arguments << m_traceFileName;
277
278     m_replayProcess->start(QLatin1String("glretrace"),
279                            arguments);
280
281     m_ui.actionStop->setEnabled(true);
282 }
283
284 void MainWindow::lookupState()
285 {
286     if (!m_selectedEvent) {
287         QMessageBox::warning(
288             this, tr("Unknown Event"),
289             tr("To inspect the state select an event in the event list."));
290         return;
291     }
292     m_stateEvent = m_selectedEvent;
293     m_findingState = true;
294     replayTrace(true);
295 }
296
297 MainWindow::~MainWindow()
298 {
299     delete m_jsonParser;
300 }
301
302 void MainWindow::parseState(const QVariantMap &params)
303 {
304     QVariantMap::const_iterator itr;
305
306     m_stateEvent->setState(params);
307     m_model->stateSetOnEvent(m_stateEvent);
308     if (m_selectedEvent == m_stateEvent) {
309         fillStateForFrame();
310     } else {
311         m_ui.stateDock->hide();
312     }
313 }
314
315 static void
316 variantToString(const QVariant &var, QString &str)
317 {
318     if (var.type() == QVariant::List) {
319         QVariantList lst = var.toList();
320         str += QLatin1String("[");
321         for (int i = 0; i < lst.count(); ++i) {
322             QVariant val = lst[i];
323             variantToString(val, str);
324             if (i < lst.count() - 1)
325                 str += QLatin1String(", ");
326         }
327         str += QLatin1String("]");
328     } else if (var.type() == QVariant::Map) {
329         Q_ASSERT(!"unsupported state type");
330     } else if (var.type() == QVariant::Hash) {
331         Q_ASSERT(!"unsupported state type");
332     } else {
333         str += var.toString();
334     }
335 }
336
337 void MainWindow::fillStateForFrame()
338 {
339     QVariantMap::const_iterator itr;
340     QVariantMap params;
341
342     if (!m_selectedEvent || m_selectedEvent->state().isEmpty())
343         return;
344
345     m_ui.stateTreeWidget->clear();
346     params = m_selectedEvent->state();
347     QList<QTreeWidgetItem *> items;
348     for (itr = params.constBegin(); itr != params.constEnd(); ++itr) {
349         QString key = itr.key();
350         QString val;
351
352         variantToString(itr.value(), val);
353         //qDebug()<<"key = "<<key;
354         //qDebug()<<"val = "<<val;
355         QStringList lst;
356         lst += key;
357         lst += val;
358         items.append(new QTreeWidgetItem((QTreeWidget*)0, lst));
359     }
360     m_ui.stateTreeWidget->insertTopLevelItems(0, items);
361     m_ui.stateDock->show();
362 }
363
364 void MainWindow::showSettings()
365 {
366     SettingsDialog dialog;
367     dialog.setFilterOptions(m_proxyModel->filterOptions());
368
369     if (dialog.exec() == QDialog::Accepted) {
370         m_proxyModel->setFilterOptions(dialog.filterOptions());
371     }
372 }
373
374 #include "mainwindow.moc"