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