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