1 #include "mainwindow.h"
4 #include "apitracecall.h"
5 #include "apicalldelegate.h"
6 #include "apitracemodel.h"
7 #include "apitracefilter.h"
8 #include "imageviewer.h"
10 #include "settingsdialog.h"
11 #include "shaderssourcewidget.h"
12 #include "ui_retracerdialog.h"
13 #include "vertexdatainterpreter.h"
17 #include <QDesktopServices>
19 #include <QFileDialog>
21 #include <QMessageBox>
22 #include <QProgressBar>
25 #include <QVBoxLayout>
30 MainWindow::MainWindow()
40 void MainWindow::openTrace()
43 QFileDialog::getOpenFileName(
47 tr("Trace Files (*.trace)"));
49 qDebug()<< "File name : " <<fileName;
51 newTraceFile(fileName);
54 void MainWindow::loadTrace(const QString &fileName)
56 if (!QFile::exists(fileName)) {
57 QMessageBox::warning(this, tr("File Missing"),
58 tr("File '%1' doesn't exist.").arg(fileName));
61 qDebug()<< "Loading : " <<fileName;
63 m_progressBar->setValue(0);
64 newTraceFile(fileName);
67 void MainWindow::callItemSelected(const QModelIndex &index)
69 ApiTraceEvent *event =
70 index.data(ApiTraceModel::EventRole).value<ApiTraceEvent*>();
72 if (event && event->type() == ApiTraceEvent::Call) {
73 ApiTraceCall *call = static_cast<ApiTraceCall*>(event);
74 m_ui.detailsWebView->setHtml(call->toHtml());
75 m_ui.detailsDock->show();
76 if (call->hasBinaryData()) {
78 call->argValues[call->binaryDataIndex()].toByteArray();
79 m_vdataInterpreter->setData(data);
81 for (int i = 0; i < call->argNames.count(); ++i) {
82 QString name = call->argNames[i];
83 if (name == QLatin1String("stride")) {
84 int stride = call->argValues[i].toInt();
85 m_ui.vertexStrideSB->setValue(stride);
86 } else if (name == QLatin1String("size")) {
87 int components = call->argValues[i].toInt();
88 m_ui.vertexComponentsSB->setValue(components);
89 } else if (name == QLatin1String("type")) {
90 QString val = call->argValues[i].toString();
91 int textIndex = m_ui.vertexTypeCB->findText(val);
93 m_ui.vertexTypeCB->setCurrentIndex(textIndex);
97 m_ui.vertexDataDock->setVisible(call->hasBinaryData());
98 m_selectedEvent = call;
100 if (event && event->type() == ApiTraceEvent::Frame) {
101 m_selectedEvent = static_cast<ApiTraceFrame*>(event);
104 m_ui.detailsDock->hide();
105 m_ui.vertexDataDock->hide();
107 if (m_selectedEvent && !m_selectedEvent->state().isEmpty()) {
110 m_ui.stateDock->hide();
113 void MainWindow::filterTrace()
115 m_proxyModel->setFilterString(m_filterEdit->text());
118 void MainWindow::replayStart()
121 Ui_RetracerDialog dlgUi;
124 dlgUi.doubleBufferingCB->setChecked(
125 m_retracer->isDoubleBuffered());
126 dlgUi.benchmarkCB->setChecked(
127 m_retracer->isBenchmarking());
129 if (dlg.exec() == QDialog::Accepted) {
130 m_retracer->setDoubleBuffered(
131 dlgUi.doubleBufferingCB->isChecked());
132 m_retracer->setBenchmarking(
133 dlgUi.benchmarkCB->isChecked());
138 void MainWindow::replayStop()
141 m_ui.actionStop->setEnabled(false);
142 m_ui.actionReplay->setEnabled(true);
143 m_ui.actionLookupState->setEnabled(true);
146 void MainWindow::newTraceFile(const QString &fileName)
148 m_traceFileName = fileName;
149 m_trace->setFileName(fileName);
151 if (m_traceFileName.isEmpty()) {
152 m_ui.actionReplay->setEnabled(false);
153 m_ui.actionLookupState->setEnabled(false);
154 setWindowTitle(tr("QApiTrace"));
156 QFileInfo info(fileName);
157 m_ui.actionReplay->setEnabled(true);
158 m_ui.actionLookupState->setEnabled(true);
160 tr("QApiTrace - %1").arg(info.fileName()));
164 void MainWindow::replayFinished(const QString &output)
166 m_ui.actionStop->setEnabled(false);
167 m_ui.actionReplay->setEnabled(true);
168 m_ui.actionLookupState->setEnabled(true);
170 m_progressBar->hide();
171 if (output.length() < 80) {
172 statusBar()->showMessage(output);
175 statusBar()->showMessage(
176 tr("Replaying finished!"), 2000);
179 void MainWindow::replayError(const QString &message)
181 m_ui.actionStop->setEnabled(false);
182 m_ui.actionReplay->setEnabled(true);
183 m_ui.actionLookupState->setEnabled(true);
186 m_progressBar->hide();
187 statusBar()->showMessage(
188 tr("Replaying unsuccessful."), 2000);
189 QMessageBox::warning(
190 this, tr("Replay Failed"), message);
193 void MainWindow::startedLoadingTrace()
196 m_progressBar->show();
197 QFileInfo info(m_trace->fileName());
198 statusBar()->showMessage(
199 tr("Loading %1...").arg(info.fileName()));
202 void MainWindow::finishedLoadingTrace()
204 m_progressBar->hide();
208 QFileInfo info(m_trace->fileName());
209 statusBar()->showMessage(
210 tr("Loaded %1").arg(info.fileName()), 3000);
213 void MainWindow::replayTrace(bool dumpState)
215 if (m_traceFileName.isEmpty())
218 m_retracer->setFileName(m_traceFileName);
219 m_retracer->setCaptureState(dumpState);
220 if (m_retracer->captureState() && m_selectedEvent) {
222 if (m_selectedEvent->type() == ApiTraceEvent::Call) {
223 index = static_cast<ApiTraceCall*>(m_selectedEvent)->index;
224 } else if (m_selectedEvent->type() == ApiTraceEvent::Frame) {
225 ApiTraceFrame *frame =
226 static_cast<ApiTraceFrame*>(m_selectedEvent);
227 if (frame->calls.isEmpty()) {
228 //XXX i guess we could still get the current state
229 qDebug()<<"tried to get a state for an empty frame";
232 index = frame->calls.first()->index;
234 qDebug()<<"Unknown event type";
237 m_retracer->setCaptureAtCallNumber(index);
241 m_ui.actionStop->setEnabled(true);
242 m_progressBar->show();
244 statusBar()->showMessage(
245 tr("Looking up the state..."));
247 statusBar()->showMessage(
248 tr("Replaying the trace file..."));
251 void MainWindow::lookupState()
253 if (!m_selectedEvent) {
254 QMessageBox::warning(
255 this, tr("Unknown Event"),
256 tr("To inspect the state select an event in the event list."));
259 m_stateEvent = m_selectedEvent;
263 MainWindow::~MainWindow()
268 variantToString(const QVariant &var, QString &str)
270 if (var.type() == QVariant::List) {
271 QVariantList lst = var.toList();
272 str += QLatin1String("[");
273 for (int i = 0; i < lst.count(); ++i) {
274 QVariant val = lst[i];
275 variantToString(val, str);
276 if (i < lst.count() - 1)
277 str += QLatin1String(", ");
279 str += QLatin1String("]");
280 } else if (var.type() == QVariant::Map) {
281 Q_ASSERT(!"unsupported state type");
282 } else if (var.type() == QVariant::Hash) {
283 Q_ASSERT(!"unsupported state type");
285 str += var.toString();
289 void MainWindow::fillStateForFrame()
291 QVariantMap::const_iterator itr;
294 if (!m_selectedEvent || m_selectedEvent->state().isEmpty())
297 const ApiTraceState &state = m_selectedEvent->state();
298 m_ui.stateTreeWidget->clear();
299 params = state.parameters();
300 QList<QTreeWidgetItem *> items;
301 for (itr = params.constBegin(); itr != params.constEnd(); ++itr) {
302 QString key = itr.key();
305 variantToString(itr.value(), val);
306 //qDebug()<<"key = "<<key;
307 //qDebug()<<"val = "<<val;
311 items.append(new QTreeWidgetItem((QTreeWidget*)0, lst));
313 m_ui.stateTreeWidget->insertTopLevelItems(0, items);
315 QStringList shaderSources = state.shaderSources();
316 if (shaderSources.isEmpty()) {
317 m_sourcesWidget->setShaders(shaderSources);
319 m_sourcesWidget->setShaders(shaderSources);
322 const QList<ApiTexture> &textures =
325 m_ui.surfacesTreeWidget->clear();
326 if (textures.isEmpty()) {
327 m_ui.surfacesTab->setDisabled(false);
329 QTreeWidgetItem *textureItem =
330 new QTreeWidgetItem(m_ui.surfacesTreeWidget);
331 m_ui.surfacesTreeWidget->setIconSize(QSize(64, 64));
332 textureItem->setText(0, tr("Textures"));
333 for (int i = 0; i < textures.count(); ++i) {
334 const ApiTexture &texture =
336 QIcon icon(QPixmap::fromImage(texture.thumb()));
337 QTreeWidgetItem *item = new QTreeWidgetItem(textureItem);
338 item->setIcon(0, icon);
339 int width = texture.size().width();
340 int height = texture.size().height();
342 QString::fromLatin1("%1, %2 x %3")
343 .arg(texture.target())
346 item->setText(1, descr);
348 item->setData(0, Qt::UserRole,
351 m_ui.surfacesTab->setEnabled(true);
353 m_ui.stateDock->show();
356 void MainWindow::showSettings()
358 SettingsDialog dialog;
359 dialog.setFilterOptions(m_proxyModel->filterOptions());
361 if (dialog.exec() == QDialog::Accepted) {
362 m_proxyModel->setFilterOptions(dialog.filterOptions());
366 void MainWindow::openHelp(const QUrl &url)
368 QDesktopServices::openUrl(url);
371 void MainWindow::showSurfacesMenu(const QPoint &pos)
373 QTreeWidget *tree = m_ui.surfacesTreeWidget;
374 QTreeWidgetItem *item = tree->itemAt(pos);
378 QMenu menu(tr("Surfaces"), this);
380 QAction *act = menu.addAction(tr("View Image"));
381 act->setStatusTip(tr("View the currently selected surface"));
382 connect(act, SIGNAL(triggered()),
383 SLOT(showSelectedSurface()));
385 menu.exec(tree->viewport()->mapToGlobal(pos));
388 void MainWindow::showSelectedSurface()
390 QTreeWidgetItem *item =
391 m_ui.surfacesTreeWidget->currentItem();
396 QVariant var = item->data(0, Qt::UserRole);
397 m_imageViewer->setImage(var.value<QImage>());
398 m_imageViewer->show();
399 m_imageViewer->raise();
400 m_imageViewer->activateWindow();
403 void MainWindow::initObjects()
405 m_ui.stateTreeWidget->sortByColumn(0, Qt::AscendingOrder);
407 m_sourcesWidget = new ShadersSourceWidget(m_ui.shadersTab);
408 QVBoxLayout *layout = new QVBoxLayout;
409 layout->addWidget(m_sourcesWidget);
410 m_ui.shadersTab->setLayout(layout);
412 m_trace = new ApiTrace();
413 m_retracer = new Retracer(this);
415 m_vdataInterpreter = new VertexDataInterpreter(this);
416 m_vdataInterpreter->setListWidget(m_ui.vertexDataListWidget);
417 m_vdataInterpreter->setStride(
418 m_ui.vertexStrideSB->value());
419 m_vdataInterpreter->setComponents(
420 m_ui.vertexComponentsSB->value());
421 m_vdataInterpreter->setStartingOffset(
422 m_ui.startingOffsetSB->value());
423 m_vdataInterpreter->setTypeFromString(
424 m_ui.vertexTypeCB->currentText());
426 m_imageViewer = new ImageViewer(this);
428 m_model = new ApiTraceModel();
429 m_model->setApiTrace(m_trace);
430 m_proxyModel = new ApiTraceFilter();
431 m_proxyModel->setSourceModel(m_model);
432 m_ui.callView->setModel(m_proxyModel);
433 m_ui.callView->setItemDelegate(new ApiCallDelegate);
434 m_ui.callView->resizeColumnToContents(0);
435 m_ui.callView->header()->swapSections(0, 1);
436 m_ui.callView->setColumnWidth(1, 42);
438 QToolBar *toolBar = addToolBar(tr("Navigation"));
439 m_filterEdit = new QLineEdit(toolBar);
440 toolBar->addWidget(m_filterEdit);
442 m_progressBar = new QProgressBar();
443 m_progressBar->setRange(0, 0);
444 statusBar()->addPermanentWidget(m_progressBar);
445 m_progressBar->hide();
447 m_ui.detailsDock->hide();
448 m_ui.vertexDataDock->hide();
449 m_ui.stateDock->hide();
450 setDockOptions(dockOptions() | QMainWindow::ForceTabbedDocks);
452 tabifyDockWidget(m_ui.stateDock, m_ui.vertexDataDock);
454 m_ui.surfacesTreeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
456 m_ui.detailsWebView->page()->setLinkDelegationPolicy(
457 QWebPage::DelegateExternalLinks);
460 void MainWindow::initConnections()
462 connect(m_trace, SIGNAL(startedLoadingTrace()),
463 this, SLOT(startedLoadingTrace()));
464 connect(m_trace, SIGNAL(finishedLoadingTrace()),
465 this, SLOT(finishedLoadingTrace()));
467 connect(m_retracer, SIGNAL(finished(const QString&)),
468 this, SLOT(replayFinished(const QString&)));
469 connect(m_retracer, SIGNAL(error(const QString&)),
470 this, SLOT(replayError(const QString&)));
471 connect(m_retracer, SIGNAL(foundState(const ApiTraceState&)),
472 this, SLOT(replayStateFound(const ApiTraceState&)));
474 connect(m_ui.vertexInterpretButton, SIGNAL(clicked()),
475 m_vdataInterpreter, SLOT(interpretData()));
476 connect(m_ui.vertexTypeCB, SIGNAL(currentIndexChanged(const QString&)),
477 m_vdataInterpreter, SLOT(setTypeFromString(const QString&)));
478 connect(m_ui.vertexStrideSB, SIGNAL(valueChanged(int)),
479 m_vdataInterpreter, SLOT(setStride(int)));
480 connect(m_ui.vertexComponentsSB, SIGNAL(valueChanged(int)),
481 m_vdataInterpreter, SLOT(setComponents(int)));
482 connect(m_ui.startingOffsetSB, SIGNAL(valueChanged(int)),
483 m_vdataInterpreter, SLOT(setStartingOffset(int)));
486 connect(m_ui.actionOpen, SIGNAL(triggered()),
487 this, SLOT(openTrace()));
488 connect(m_ui.actionQuit, SIGNAL(triggered()),
489 this, SLOT(close()));
491 connect(m_ui.actionReplay, SIGNAL(triggered()),
492 this, SLOT(replayStart()));
493 connect(m_ui.actionStop, SIGNAL(triggered()),
494 this, SLOT(replayStop()));
495 connect(m_ui.actionLookupState, SIGNAL(triggered()),
496 this, SLOT(lookupState()));
497 connect(m_ui.actionOptions, SIGNAL(triggered()),
498 this, SLOT(showSettings()));
500 connect(m_ui.callView, SIGNAL(activated(const QModelIndex &)),
501 this, SLOT(callItemSelected(const QModelIndex &)));
502 connect(m_filterEdit, SIGNAL(returnPressed()),
503 this, SLOT(filterTrace()));
505 connect(m_ui.surfacesTreeWidget,
506 SIGNAL(customContextMenuRequested(const QPoint &)),
507 SLOT(showSurfacesMenu(const QPoint &)));
508 connect(m_ui.surfacesTreeWidget,
509 SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)),
510 SLOT(showSelectedSurface()));
512 connect(m_ui.detailsWebView, SIGNAL(linkClicked(const QUrl&)),
513 this, SLOT(openHelp(const QUrl&)));
516 void MainWindow::replayStateFound(const ApiTraceState &state)
518 m_stateEvent->setState(state);
519 m_model->stateSetOnEvent(m_stateEvent);
520 if (m_selectedEvent == m_stateEvent) {
523 m_ui.stateDock->hide();
527 #include "mainwindow.moc"