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