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 "vertexdatainterpreter.h"
12 #include <qjson/parser.h>
16 #include <QDesktopServices>
18 #include <QFileDialog>
20 #include <QMessageBox>
21 #include <QProgressBar>
28 MainWindow::MainWindow()
32 m_jsonParser(new QJson::Parser())
35 m_ui.stateTreeWidget->sortByColumn(0, Qt::AscendingOrder);
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()));
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&)));
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());
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)));
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);
77 QToolBar *toolBar = addToolBar(tr("Navigation"));
78 m_filterEdit = new QLineEdit(toolBar);
79 toolBar->addWidget(m_filterEdit);
81 m_progressBar = new QProgressBar();
82 m_progressBar->setRange(0, 0);
83 statusBar()->addPermanentWidget(m_progressBar);
84 m_progressBar->hide();
86 m_ui.detailsDock->hide();
87 m_ui.vertexDataDock->hide();
88 m_ui.stateDock->hide();
89 setDockOptions(dockOptions() | QMainWindow::ForceTabbedDocks);
91 tabifyDockWidget(m_ui.stateDock, m_ui.vertexDataDock);
93 connect(m_ui.actionOpen, SIGNAL(triggered()),
94 this, SLOT(openTrace()));
95 connect(m_ui.actionQuit, SIGNAL(triggered()),
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()));
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()));
112 m_ui.detailsWebView->page()->setLinkDelegationPolicy(
113 QWebPage::DelegateExternalLinks);
114 connect(m_ui.detailsWebView, SIGNAL(linkClicked(const QUrl&)),
115 this, SLOT(openHelp(const QUrl&)));
118 void MainWindow::openTrace()
121 QFileDialog::getOpenFileName(
125 tr("Trace Files (*.trace)"));
127 qDebug()<< "File name : " <<fileName;
129 newTraceFile(fileName);
132 void MainWindow::loadTrace(const QString &fileName)
134 if (!QFile::exists(fileName)) {
135 QMessageBox::warning(this, tr("File Missing"),
136 tr("File '%1' doesn't exist.").arg(fileName));
139 qDebug()<< "Loading : " <<fileName;
141 m_progressBar->setValue(0);
142 newTraceFile(fileName);
145 void MainWindow::callItemSelected(const QModelIndex &index)
147 ApiTraceEvent *event =
148 index.data(ApiTraceModel::EventRole).value<ApiTraceEvent*>();
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()) {
156 call->argValues[call->binaryDataIndex()].toByteArray();
157 m_vdataInterpreter->setData(data);
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);
171 m_ui.vertexTypeCB->setCurrentIndex(textIndex);
175 m_ui.vertexDataDock->setVisible(call->hasBinaryData());
176 m_selectedEvent = call;
178 if (event && event->type() == ApiTraceEvent::Frame) {
179 m_selectedEvent = static_cast<ApiTraceFrame*>(event);
182 m_ui.detailsDock->hide();
183 m_ui.vertexDataDock->hide();
185 if (m_selectedEvent && !m_selectedEvent->state().isEmpty()) {
188 m_ui.stateDock->hide();
191 void MainWindow::filterTrace()
193 m_proxyModel->setFilterString(m_filterEdit->text());
196 void MainWindow::replayStart()
201 void MainWindow::replayStop()
203 m_retracer->terminate();
204 m_ui.actionStop->setEnabled(false);
205 m_ui.actionReplay->setEnabled(true);
206 m_ui.actionLookupState->setEnabled(true);
209 void MainWindow::newTraceFile(const QString &fileName)
211 m_traceFileName = fileName;
212 m_trace->setFileName(fileName);
214 if (m_traceFileName.isEmpty()) {
215 m_ui.actionReplay->setEnabled(false);
216 m_ui.actionLookupState->setEnabled(false);
217 setWindowTitle(tr("QApiTrace"));
219 QFileInfo info(fileName);
220 m_ui.actionReplay->setEnabled(true);
221 m_ui.actionLookupState->setEnabled(true);
223 tr("QApiTrace - %1").arg(info.fileName()));
227 void MainWindow::replayFinished(const QByteArray &output)
229 m_ui.actionStop->setEnabled(false);
230 m_ui.actionReplay->setEnabled(true);
231 m_ui.actionLookupState->setEnabled(true);
233 if (m_retracer->captureState()) {
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);
243 void MainWindow::replayError(const QString &message)
245 m_ui.actionStop->setEnabled(false);
246 m_ui.actionReplay->setEnabled(true);
247 m_ui.actionLookupState->setEnabled(true);
250 QMessageBox::warning(
251 this, tr("Replay Failed"), message);
254 void MainWindow::startedLoadingTrace()
257 m_progressBar->show();
258 QFileInfo info(m_trace->fileName());
259 statusBar()->showMessage(
260 tr("Loading %1...").arg(info.fileName()));
263 void MainWindow::finishedLoadingTrace()
265 m_progressBar->hide();
269 QFileInfo info(m_trace->fileName());
270 statusBar()->showMessage(
271 tr("Loaded %1").arg(info.fileName()), 3000);
274 void MainWindow::replayTrace(bool dumpState)
276 if (m_traceFileName.isEmpty())
279 m_retracer->setFileName(m_traceFileName);
280 m_retracer->setCaptureState(dumpState);
281 if (m_retracer->captureState() && m_selectedEvent) {
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";
292 index = frame->calls.first()->index;
294 qDebug()<<"Unknown event type";
297 m_retracer->setCaptureAtCallNumber(index);
301 m_ui.actionStop->setEnabled(true);
304 void MainWindow::lookupState()
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."));
312 m_stateEvent = m_selectedEvent;
316 MainWindow::~MainWindow()
321 void MainWindow::parseState(const QVariantMap ¶ms)
323 QVariantMap::const_iterator itr;
325 m_stateEvent->setState(params);
326 m_model->stateSetOnEvent(m_stateEvent);
327 if (m_selectedEvent == m_stateEvent) {
330 m_ui.stateDock->hide();
335 variantToString(const QVariant &var, QString &str)
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(", ");
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");
352 str += var.toString();
356 void MainWindow::fillStateForFrame()
358 QVariantMap::const_iterator itr;
361 if (!m_selectedEvent || m_selectedEvent->state().isEmpty())
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();
371 variantToString(itr.value(), val);
372 //qDebug()<<"key = "<<key;
373 //qDebug()<<"val = "<<val;
377 items.append(new QTreeWidgetItem((QTreeWidget*)0, lst));
379 m_ui.stateTreeWidget->insertTopLevelItems(0, items);
380 m_ui.stateDock->show();
383 void MainWindow::showSettings()
385 SettingsDialog dialog;
386 dialog.setFilterOptions(m_proxyModel->filterOptions());
388 if (dialog.exec() == QDialog::Accepted) {
389 m_proxyModel->setFilterOptions(dialog.filterOptions());
393 void MainWindow::openHelp(const QUrl &url)
395 QDesktopServices::openUrl(url);
398 #include "mainwindow.moc"