1 #include "mainwindow.h"
4 #include "apitracecall.h"
5 #include "apicalldelegate.h"
6 #include "apitracemodel.h"
7 #include "apitracefilter.h"
9 #include "settingsdialog.h"
10 #include "ui_retracerdialog.h"
11 #include "vertexdatainterpreter.h"
13 #include <qjson/parser.h>
17 #include <QDesktopServices>
19 #include <QFileDialog>
21 #include <QMessageBox>
22 #include <QProgressBar>
29 MainWindow::MainWindow()
33 m_jsonParser(new QJson::Parser())
36 m_ui.stateTreeWidget->sortByColumn(0, Qt::AscendingOrder);
38 m_trace = new ApiTrace();
39 connect(m_trace, SIGNAL(startedLoadingTrace()),
40 this, SLOT(startedLoadingTrace()));
41 connect(m_trace, SIGNAL(finishedLoadingTrace()),
42 this, SLOT(finishedLoadingTrace()));
44 m_retracer = new Retracer(this);
45 connect(m_retracer, SIGNAL(finished(const QByteArray&)),
46 this, SLOT(replayFinished(const QByteArray&)));
47 connect(m_retracer, SIGNAL(error(const QString&)),
48 this, SLOT(replayError(const QString&)));
50 m_vdataInterpreter = new VertexDataInterpreter(this);
51 m_vdataInterpreter->setListWidget(m_ui.vertexDataListWidget);
52 m_vdataInterpreter->setStride(
53 m_ui.vertexStrideSB->value());
54 m_vdataInterpreter->setComponents(
55 m_ui.vertexComponentsSB->value());
56 m_vdataInterpreter->setStartingOffset(
57 m_ui.startingOffsetSB->value());
58 m_vdataInterpreter->setTypeFromString(
59 m_ui.vertexTypeCB->currentText());
61 connect(m_ui.vertexInterpretButton, SIGNAL(clicked()),
62 m_vdataInterpreter, SLOT(interpretData()));
63 connect(m_ui.vertexTypeCB, SIGNAL(currentIndexChanged(const QString&)),
64 m_vdataInterpreter, SLOT(setTypeFromString(const QString&)));
65 connect(m_ui.vertexStrideSB, SIGNAL(valueChanged(int)),
66 m_vdataInterpreter, SLOT(setStride(int)));
67 connect(m_ui.vertexComponentsSB, SIGNAL(valueChanged(int)),
68 m_vdataInterpreter, SLOT(setComponents(int)));
69 connect(m_ui.startingOffsetSB, SIGNAL(valueChanged(int)),
70 m_vdataInterpreter, SLOT(setStartingOffset(int)));
72 m_model = new ApiTraceModel();
73 m_model->setApiTrace(m_trace);
74 m_proxyModel = new ApiTraceFilter();
75 m_proxyModel->setSourceModel(m_model);
76 m_ui.callView->setModel(m_proxyModel);
77 m_ui.callView->setItemDelegate(new ApiCallDelegate);
78 m_ui.callView->resizeColumnToContents(0);
79 m_ui.callView->header()->swapSections(0, 1);
80 m_ui.callView->setColumnWidth(1, 42);
82 QToolBar *toolBar = addToolBar(tr("Navigation"));
83 m_filterEdit = new QLineEdit(toolBar);
84 toolBar->addWidget(m_filterEdit);
86 m_progressBar = new QProgressBar();
87 m_progressBar->setRange(0, 0);
88 statusBar()->addPermanentWidget(m_progressBar);
89 m_progressBar->hide();
91 m_ui.detailsDock->hide();
92 m_ui.vertexDataDock->hide();
93 m_ui.stateDock->hide();
94 setDockOptions(dockOptions() | QMainWindow::ForceTabbedDocks);
96 tabifyDockWidget(m_ui.stateDock, m_ui.vertexDataDock);
98 connect(m_ui.actionOpen, SIGNAL(triggered()),
99 this, SLOT(openTrace()));
100 connect(m_ui.actionQuit, SIGNAL(triggered()),
101 this, SLOT(close()));
103 connect(m_ui.actionReplay, SIGNAL(triggered()),
104 this, SLOT(replayStart()));
105 connect(m_ui.actionStop, SIGNAL(triggered()),
106 this, SLOT(replayStop()));
107 connect(m_ui.actionLookupState, SIGNAL(triggered()),
108 this, SLOT(lookupState()));
109 connect(m_ui.actionOptions, SIGNAL(triggered()),
110 this, SLOT(showSettings()));
112 connect(m_ui.callView, SIGNAL(activated(const QModelIndex &)),
113 this, SLOT(callItemSelected(const QModelIndex &)));
114 connect(m_filterEdit, SIGNAL(returnPressed()),
115 this, SLOT(filterTrace()));
117 m_ui.detailsWebView->page()->setLinkDelegationPolicy(
118 QWebPage::DelegateExternalLinks);
119 connect(m_ui.detailsWebView, SIGNAL(linkClicked(const QUrl&)),
120 this, SLOT(openHelp(const QUrl&)));
123 void MainWindow::openTrace()
126 QFileDialog::getOpenFileName(
130 tr("Trace Files (*.trace)"));
132 qDebug()<< "File name : " <<fileName;
134 newTraceFile(fileName);
137 void MainWindow::loadTrace(const QString &fileName)
139 if (!QFile::exists(fileName)) {
140 QMessageBox::warning(this, tr("File Missing"),
141 tr("File '%1' doesn't exist.").arg(fileName));
144 qDebug()<< "Loading : " <<fileName;
146 m_progressBar->setValue(0);
147 newTraceFile(fileName);
150 void MainWindow::callItemSelected(const QModelIndex &index)
152 ApiTraceEvent *event =
153 index.data(ApiTraceModel::EventRole).value<ApiTraceEvent*>();
155 if (event && event->type() == ApiTraceEvent::Call) {
156 ApiTraceCall *call = static_cast<ApiTraceCall*>(event);
157 m_ui.detailsWebView->setHtml(call->toHtml());
158 m_ui.detailsDock->show();
159 if (call->hasBinaryData()) {
161 call->argValues[call->binaryDataIndex()].toByteArray();
162 m_vdataInterpreter->setData(data);
164 for (int i = 0; i < call->argNames.count(); ++i) {
165 QString name = call->argNames[i];
166 if (name == QLatin1String("stride")) {
167 int stride = call->argValues[i].toInt();
168 m_ui.vertexStrideSB->setValue(stride);
169 } else if (name == QLatin1String("size")) {
170 int components = call->argValues[i].toInt();
171 m_ui.vertexComponentsSB->setValue(components);
172 } else if (name == QLatin1String("type")) {
173 QString val = call->argValues[i].toString();
174 int textIndex = m_ui.vertexTypeCB->findText(val);
176 m_ui.vertexTypeCB->setCurrentIndex(textIndex);
180 m_ui.vertexDataDock->setVisible(call->hasBinaryData());
181 m_selectedEvent = call;
183 if (event && event->type() == ApiTraceEvent::Frame) {
184 m_selectedEvent = static_cast<ApiTraceFrame*>(event);
187 m_ui.detailsDock->hide();
188 m_ui.vertexDataDock->hide();
190 if (m_selectedEvent && !m_selectedEvent->state().isEmpty()) {
193 m_ui.stateDock->hide();
196 void MainWindow::filterTrace()
198 m_proxyModel->setFilterString(m_filterEdit->text());
201 void MainWindow::replayStart()
204 Ui_RetracerDialog dlgUi;
207 dlgUi.doubleBufferingCB->setChecked(
208 m_retracer->isDoubleBuffered());
209 dlgUi.benchmarkCB->setChecked(
210 m_retracer->isBenchmarking());
212 if (dlg.exec() == QDialog::Accepted) {
213 m_retracer->setDoubleBuffered(
214 dlgUi.doubleBufferingCB->isChecked());
215 m_retracer->setBenchmarking(
216 dlgUi.benchmarkCB->isChecked());
221 void MainWindow::replayStop()
223 m_retracer->terminate();
224 m_ui.actionStop->setEnabled(false);
225 m_ui.actionReplay->setEnabled(true);
226 m_ui.actionLookupState->setEnabled(true);
229 void MainWindow::newTraceFile(const QString &fileName)
231 m_traceFileName = fileName;
232 m_trace->setFileName(fileName);
234 if (m_traceFileName.isEmpty()) {
235 m_ui.actionReplay->setEnabled(false);
236 m_ui.actionLookupState->setEnabled(false);
237 setWindowTitle(tr("QApiTrace"));
239 QFileInfo info(fileName);
240 m_ui.actionReplay->setEnabled(true);
241 m_ui.actionLookupState->setEnabled(true);
243 tr("QApiTrace - %1").arg(info.fileName()));
247 void MainWindow::replayFinished(const QByteArray &output)
249 m_ui.actionStop->setEnabled(false);
250 m_ui.actionReplay->setEnabled(true);
251 m_ui.actionLookupState->setEnabled(true);
253 if (m_retracer->captureState()) {
255 QVariantMap parsedJson = m_jsonParser->parse(output, &ok).toMap();
256 parseState(parsedJson[QLatin1String("parameters")].toMap());
257 } else if (output.length() < 80) {
258 statusBar()->showMessage(output);
263 void MainWindow::replayError(const QString &message)
265 m_ui.actionStop->setEnabled(false);
266 m_ui.actionReplay->setEnabled(true);
267 m_ui.actionLookupState->setEnabled(true);
270 QMessageBox::warning(
271 this, tr("Replay Failed"), message);
274 void MainWindow::startedLoadingTrace()
277 m_progressBar->show();
278 QFileInfo info(m_trace->fileName());
279 statusBar()->showMessage(
280 tr("Loading %1...").arg(info.fileName()));
283 void MainWindow::finishedLoadingTrace()
285 m_progressBar->hide();
289 QFileInfo info(m_trace->fileName());
290 statusBar()->showMessage(
291 tr("Loaded %1").arg(info.fileName()), 3000);
294 void MainWindow::replayTrace(bool dumpState)
296 if (m_traceFileName.isEmpty())
299 m_retracer->setFileName(m_traceFileName);
300 m_retracer->setCaptureState(dumpState);
301 if (m_retracer->captureState() && m_selectedEvent) {
303 if (m_selectedEvent->type() == ApiTraceEvent::Call) {
304 index = static_cast<ApiTraceCall*>(m_selectedEvent)->index;
305 } else if (m_selectedEvent->type() == ApiTraceEvent::Frame) {
306 ApiTraceFrame *frame = static_cast<ApiTraceFrame*>(m_selectedEvent);
307 if (frame->calls.isEmpty()) {
308 //XXX i guess we could still get the current state
309 qDebug()<<"tried to get a state for an empty frame";
312 index = frame->calls.first()->index;
314 qDebug()<<"Unknown event type";
317 m_retracer->setCaptureAtCallNumber(index);
321 m_ui.actionStop->setEnabled(true);
324 void MainWindow::lookupState()
326 if (!m_selectedEvent) {
327 QMessageBox::warning(
328 this, tr("Unknown Event"),
329 tr("To inspect the state select an event in the event list."));
332 m_stateEvent = m_selectedEvent;
336 MainWindow::~MainWindow()
341 void MainWindow::parseState(const QVariantMap ¶ms)
343 QVariantMap::const_iterator itr;
345 m_stateEvent->setState(params);
346 m_model->stateSetOnEvent(m_stateEvent);
347 if (m_selectedEvent == m_stateEvent) {
350 m_ui.stateDock->hide();
355 variantToString(const QVariant &var, QString &str)
357 if (var.type() == QVariant::List) {
358 QVariantList lst = var.toList();
359 str += QLatin1String("[");
360 for (int i = 0; i < lst.count(); ++i) {
361 QVariant val = lst[i];
362 variantToString(val, str);
363 if (i < lst.count() - 1)
364 str += QLatin1String(", ");
366 str += QLatin1String("]");
367 } else if (var.type() == QVariant::Map) {
368 Q_ASSERT(!"unsupported state type");
369 } else if (var.type() == QVariant::Hash) {
370 Q_ASSERT(!"unsupported state type");
372 str += var.toString();
376 void MainWindow::fillStateForFrame()
378 QVariantMap::const_iterator itr;
381 if (!m_selectedEvent || m_selectedEvent->state().isEmpty())
384 m_ui.stateTreeWidget->clear();
385 params = m_selectedEvent->state();
386 QList<QTreeWidgetItem *> items;
387 for (itr = params.constBegin(); itr != params.constEnd(); ++itr) {
388 QString key = itr.key();
391 variantToString(itr.value(), val);
392 //qDebug()<<"key = "<<key;
393 //qDebug()<<"val = "<<val;
397 items.append(new QTreeWidgetItem((QTreeWidget*)0, lst));
399 m_ui.stateTreeWidget->insertTopLevelItems(0, items);
400 m_ui.stateDock->show();
403 void MainWindow::showSettings()
405 SettingsDialog dialog;
406 dialog.setFilterOptions(m_proxyModel->filterOptions());
408 if (dialog.exec() == QDialog::Accepted) {
409 m_proxyModel->setFilterOptions(dialog.filterOptions());
413 void MainWindow::openHelp(const QUrl &url)
415 QDesktopServices::openUrl(url);
418 #include "mainwindow.moc"