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 "shaderssourcewidget.h"
11 #include "ui_retracerdialog.h"
12 #include "vertexdatainterpreter.h"
14 #include <qjson/parser.h>
18 #include <QDesktopServices>
20 #include <QFileDialog>
22 #include <QMessageBox>
23 #include <QProgressBar>
26 #include <QVBoxLayout>
31 MainWindow::MainWindow()
35 m_jsonParser(new QJson::Parser())
38 m_ui.stateTreeWidget->sortByColumn(0, Qt::AscendingOrder);
40 m_sourcesWidget = new ShadersSourceWidget(m_ui.shadersTab);
41 QVBoxLayout *layout = new QVBoxLayout;
42 layout->addWidget(m_sourcesWidget);
43 m_ui.shadersTab->setLayout(layout);
45 m_trace = new ApiTrace();
46 connect(m_trace, SIGNAL(startedLoadingTrace()),
47 this, SLOT(startedLoadingTrace()));
48 connect(m_trace, SIGNAL(finishedLoadingTrace()),
49 this, SLOT(finishedLoadingTrace()));
51 m_retracer = new Retracer(this);
52 connect(m_retracer, SIGNAL(finished(const QByteArray&)),
53 this, SLOT(replayFinished(const QByteArray&)));
54 connect(m_retracer, SIGNAL(error(const QString&)),
55 this, SLOT(replayError(const QString&)));
57 m_vdataInterpreter = new VertexDataInterpreter(this);
58 m_vdataInterpreter->setListWidget(m_ui.vertexDataListWidget);
59 m_vdataInterpreter->setStride(
60 m_ui.vertexStrideSB->value());
61 m_vdataInterpreter->setComponents(
62 m_ui.vertexComponentsSB->value());
63 m_vdataInterpreter->setStartingOffset(
64 m_ui.startingOffsetSB->value());
65 m_vdataInterpreter->setTypeFromString(
66 m_ui.vertexTypeCB->currentText());
68 connect(m_ui.vertexInterpretButton, SIGNAL(clicked()),
69 m_vdataInterpreter, SLOT(interpretData()));
70 connect(m_ui.vertexTypeCB, SIGNAL(currentIndexChanged(const QString&)),
71 m_vdataInterpreter, SLOT(setTypeFromString(const QString&)));
72 connect(m_ui.vertexStrideSB, SIGNAL(valueChanged(int)),
73 m_vdataInterpreter, SLOT(setStride(int)));
74 connect(m_ui.vertexComponentsSB, SIGNAL(valueChanged(int)),
75 m_vdataInterpreter, SLOT(setComponents(int)));
76 connect(m_ui.startingOffsetSB, SIGNAL(valueChanged(int)),
77 m_vdataInterpreter, SLOT(setStartingOffset(int)));
79 m_model = new ApiTraceModel();
80 m_model->setApiTrace(m_trace);
81 m_proxyModel = new ApiTraceFilter();
82 m_proxyModel->setSourceModel(m_model);
83 m_ui.callView->setModel(m_proxyModel);
84 m_ui.callView->setItemDelegate(new ApiCallDelegate);
85 m_ui.callView->resizeColumnToContents(0);
86 m_ui.callView->header()->swapSections(0, 1);
87 m_ui.callView->setColumnWidth(1, 42);
89 QToolBar *toolBar = addToolBar(tr("Navigation"));
90 m_filterEdit = new QLineEdit(toolBar);
91 toolBar->addWidget(m_filterEdit);
93 m_progressBar = new QProgressBar();
94 m_progressBar->setRange(0, 0);
95 statusBar()->addPermanentWidget(m_progressBar);
96 m_progressBar->hide();
98 m_ui.detailsDock->hide();
99 m_ui.vertexDataDock->hide();
100 m_ui.stateDock->hide();
101 setDockOptions(dockOptions() | QMainWindow::ForceTabbedDocks);
103 tabifyDockWidget(m_ui.stateDock, m_ui.vertexDataDock);
105 connect(m_ui.actionOpen, SIGNAL(triggered()),
106 this, SLOT(openTrace()));
107 connect(m_ui.actionQuit, SIGNAL(triggered()),
108 this, SLOT(close()));
110 connect(m_ui.actionReplay, SIGNAL(triggered()),
111 this, SLOT(replayStart()));
112 connect(m_ui.actionStop, SIGNAL(triggered()),
113 this, SLOT(replayStop()));
114 connect(m_ui.actionLookupState, SIGNAL(triggered()),
115 this, SLOT(lookupState()));
116 connect(m_ui.actionOptions, SIGNAL(triggered()),
117 this, SLOT(showSettings()));
119 connect(m_ui.callView, SIGNAL(activated(const QModelIndex &)),
120 this, SLOT(callItemSelected(const QModelIndex &)));
121 connect(m_filterEdit, SIGNAL(returnPressed()),
122 this, SLOT(filterTrace()));
124 m_ui.detailsWebView->page()->setLinkDelegationPolicy(
125 QWebPage::DelegateExternalLinks);
126 connect(m_ui.detailsWebView, SIGNAL(linkClicked(const QUrl&)),
127 this, SLOT(openHelp(const QUrl&)));
130 void MainWindow::openTrace()
133 QFileDialog::getOpenFileName(
137 tr("Trace Files (*.trace)"));
139 qDebug()<< "File name : " <<fileName;
141 newTraceFile(fileName);
144 void MainWindow::loadTrace(const QString &fileName)
146 if (!QFile::exists(fileName)) {
147 QMessageBox::warning(this, tr("File Missing"),
148 tr("File '%1' doesn't exist.").arg(fileName));
151 qDebug()<< "Loading : " <<fileName;
153 m_progressBar->setValue(0);
154 newTraceFile(fileName);
157 void MainWindow::callItemSelected(const QModelIndex &index)
159 ApiTraceEvent *event =
160 index.data(ApiTraceModel::EventRole).value<ApiTraceEvent*>();
162 if (event && event->type() == ApiTraceEvent::Call) {
163 ApiTraceCall *call = static_cast<ApiTraceCall*>(event);
164 m_ui.detailsWebView->setHtml(call->toHtml());
165 m_ui.detailsDock->show();
166 if (call->hasBinaryData()) {
168 call->argValues[call->binaryDataIndex()].toByteArray();
169 m_vdataInterpreter->setData(data);
171 for (int i = 0; i < call->argNames.count(); ++i) {
172 QString name = call->argNames[i];
173 if (name == QLatin1String("stride")) {
174 int stride = call->argValues[i].toInt();
175 m_ui.vertexStrideSB->setValue(stride);
176 } else if (name == QLatin1String("size")) {
177 int components = call->argValues[i].toInt();
178 m_ui.vertexComponentsSB->setValue(components);
179 } else if (name == QLatin1String("type")) {
180 QString val = call->argValues[i].toString();
181 int textIndex = m_ui.vertexTypeCB->findText(val);
183 m_ui.vertexTypeCB->setCurrentIndex(textIndex);
187 m_ui.vertexDataDock->setVisible(call->hasBinaryData());
188 m_selectedEvent = call;
190 if (event && event->type() == ApiTraceEvent::Frame) {
191 m_selectedEvent = static_cast<ApiTraceFrame*>(event);
194 m_ui.detailsDock->hide();
195 m_ui.vertexDataDock->hide();
197 if (m_selectedEvent && !m_selectedEvent->state().isEmpty()) {
200 m_ui.stateDock->hide();
203 void MainWindow::filterTrace()
205 m_proxyModel->setFilterString(m_filterEdit->text());
208 void MainWindow::replayStart()
211 Ui_RetracerDialog dlgUi;
214 dlgUi.doubleBufferingCB->setChecked(
215 m_retracer->isDoubleBuffered());
216 dlgUi.benchmarkCB->setChecked(
217 m_retracer->isBenchmarking());
219 if (dlg.exec() == QDialog::Accepted) {
220 m_retracer->setDoubleBuffered(
221 dlgUi.doubleBufferingCB->isChecked());
222 m_retracer->setBenchmarking(
223 dlgUi.benchmarkCB->isChecked());
228 void MainWindow::replayStop()
230 m_retracer->terminate();
231 m_ui.actionStop->setEnabled(false);
232 m_ui.actionReplay->setEnabled(true);
233 m_ui.actionLookupState->setEnabled(true);
236 void MainWindow::newTraceFile(const QString &fileName)
238 m_traceFileName = fileName;
239 m_trace->setFileName(fileName);
241 if (m_traceFileName.isEmpty()) {
242 m_ui.actionReplay->setEnabled(false);
243 m_ui.actionLookupState->setEnabled(false);
244 setWindowTitle(tr("QApiTrace"));
246 QFileInfo info(fileName);
247 m_ui.actionReplay->setEnabled(true);
248 m_ui.actionLookupState->setEnabled(true);
250 tr("QApiTrace - %1").arg(info.fileName()));
254 void MainWindow::replayFinished(const QByteArray &output)
256 m_ui.actionStop->setEnabled(false);
257 m_ui.actionReplay->setEnabled(true);
258 m_ui.actionLookupState->setEnabled(true);
260 if (m_retracer->captureState()) {
262 QVariantMap parsedJson = m_jsonParser->parse(output, &ok).toMap();
263 parseState(parsedJson);
264 } else if (output.length() < 80) {
265 statusBar()->showMessage(output);
270 void MainWindow::replayError(const QString &message)
272 m_ui.actionStop->setEnabled(false);
273 m_ui.actionReplay->setEnabled(true);
274 m_ui.actionLookupState->setEnabled(true);
277 QMessageBox::warning(
278 this, tr("Replay Failed"), message);
281 void MainWindow::startedLoadingTrace()
284 m_progressBar->show();
285 QFileInfo info(m_trace->fileName());
286 statusBar()->showMessage(
287 tr("Loading %1...").arg(info.fileName()));
290 void MainWindow::finishedLoadingTrace()
292 m_progressBar->hide();
296 QFileInfo info(m_trace->fileName());
297 statusBar()->showMessage(
298 tr("Loaded %1").arg(info.fileName()), 3000);
301 void MainWindow::replayTrace(bool dumpState)
303 if (m_traceFileName.isEmpty())
306 m_retracer->setFileName(m_traceFileName);
307 m_retracer->setCaptureState(dumpState);
308 if (m_retracer->captureState() && m_selectedEvent) {
310 if (m_selectedEvent->type() == ApiTraceEvent::Call) {
311 index = static_cast<ApiTraceCall*>(m_selectedEvent)->index;
312 } else if (m_selectedEvent->type() == ApiTraceEvent::Frame) {
313 ApiTraceFrame *frame = static_cast<ApiTraceFrame*>(m_selectedEvent);
314 if (frame->calls.isEmpty()) {
315 //XXX i guess we could still get the current state
316 qDebug()<<"tried to get a state for an empty frame";
319 index = frame->calls.first()->index;
321 qDebug()<<"Unknown event type";
324 m_retracer->setCaptureAtCallNumber(index);
328 m_ui.actionStop->setEnabled(true);
331 void MainWindow::lookupState()
333 if (!m_selectedEvent) {
334 QMessageBox::warning(
335 this, tr("Unknown Event"),
336 tr("To inspect the state select an event in the event list."));
339 m_stateEvent = m_selectedEvent;
343 MainWindow::~MainWindow()
348 void MainWindow::parseState(const QVariantMap &parsedJson)
350 m_stateEvent->setState(ApiTraceState(parsedJson));
351 m_model->stateSetOnEvent(m_stateEvent);
352 if (m_selectedEvent == m_stateEvent) {
355 m_ui.stateDock->hide();
360 variantToString(const QVariant &var, QString &str)
362 if (var.type() == QVariant::List) {
363 QVariantList lst = var.toList();
364 str += QLatin1String("[");
365 for (int i = 0; i < lst.count(); ++i) {
366 QVariant val = lst[i];
367 variantToString(val, str);
368 if (i < lst.count() - 1)
369 str += QLatin1String(", ");
371 str += QLatin1String("]");
372 } else if (var.type() == QVariant::Map) {
373 Q_ASSERT(!"unsupported state type");
374 } else if (var.type() == QVariant::Hash) {
375 Q_ASSERT(!"unsupported state type");
377 str += var.toString();
381 void MainWindow::fillStateForFrame()
383 QVariantMap::const_iterator itr;
386 if (!m_selectedEvent || m_selectedEvent->state().isEmpty())
389 const ApiTraceState &state = m_selectedEvent->state();
390 m_ui.stateTreeWidget->clear();
391 params = state.parameters();
392 QList<QTreeWidgetItem *> items;
393 for (itr = params.constBegin(); itr != params.constEnd(); ++itr) {
394 QString key = itr.key();
397 variantToString(itr.value(), val);
398 //qDebug()<<"key = "<<key;
399 //qDebug()<<"val = "<<val;
403 items.append(new QTreeWidgetItem((QTreeWidget*)0, lst));
405 m_ui.stateTreeWidget->insertTopLevelItems(0, items);
407 QStringList shaderSources = state.shaderSources();
408 if (shaderSources.isEmpty()) {
409 m_sourcesWidget->setShaders(shaderSources);
411 m_sourcesWidget->setShaders(shaderSources);
414 m_ui.surfacesTab->setEnabled(false);
415 m_ui.stateDock->show();
418 void MainWindow::showSettings()
420 SettingsDialog dialog;
421 dialog.setFilterOptions(m_proxyModel->filterOptions());
423 if (dialog.exec() == QDialog::Accepted) {
424 m_proxyModel->setFilterOptions(dialog.filterOptions());
428 void MainWindow::openHelp(const QUrl &url)
430 QDesktopServices::openUrl(url);
433 #include "mainwindow.moc"