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->setStartingOffset(
56 m_ui.startingOffsetSB->value());
57 m_vdataInterpreter->setTypeFromString(
58 m_ui.vertexTypeCB->currentText());
60 connect(m_ui.vertexInterpretButton, SIGNAL(clicked()),
61 m_vdataInterpreter, SLOT(interpretData()));
62 connect(m_ui.vertexTypeCB, SIGNAL(currentIndexChanged(const QString&)),
63 m_vdataInterpreter, SLOT(setTypeFromString(const QString&)));
64 connect(m_ui.vertexStrideSB, SIGNAL(valueChanged(int)),
65 m_vdataInterpreter, SLOT(setStride(int)));
66 connect(m_ui.vertexComponentsSB, SIGNAL(valueChanged(int)),
67 m_vdataInterpreter, SLOT(setComponents(int)));
68 connect(m_ui.startingOffsetSB, SIGNAL(valueChanged(int)),
69 m_vdataInterpreter, SLOT(setStartingOffset(int)));
71 m_model = new ApiTraceModel();
72 m_model->setApiTrace(m_trace);
73 m_proxyModel = new ApiTraceFilter();
74 m_proxyModel->setSourceModel(m_model);
75 m_ui.callView->setModel(m_proxyModel);
76 m_ui.callView->setItemDelegate(new ApiCallDelegate);
77 m_ui.callView->resizeColumnToContents(0);
78 m_ui.callView->header()->swapSections(0, 1);
79 m_ui.callView->setColumnWidth(1, 42);
81 QToolBar *toolBar = addToolBar(tr("Navigation"));
82 m_filterEdit = new QLineEdit(toolBar);
83 toolBar->addWidget(m_filterEdit);
85 m_progressBar = new QProgressBar();
86 m_progressBar->setRange(0, 0);
87 statusBar()->addPermanentWidget(m_progressBar);
88 m_progressBar->hide();
90 m_ui.detailsDock->hide();
91 m_ui.vertexDataDock->hide();
92 m_ui.stateDock->hide();
93 setDockOptions(dockOptions() | QMainWindow::ForceTabbedDocks);
95 tabifyDockWidget(m_ui.stateDock, m_ui.vertexDataDock);
97 connect(m_ui.actionOpen, SIGNAL(triggered()),
98 this, SLOT(openTrace()));
99 connect(m_ui.actionQuit, SIGNAL(triggered()),
100 this, SLOT(close()));
102 connect(m_ui.actionReplay, SIGNAL(triggered()),
103 this, SLOT(replayStart()));
104 connect(m_ui.actionStop, SIGNAL(triggered()),
105 this, SLOT(replayStop()));
106 connect(m_ui.actionLookupState, SIGNAL(triggered()),
107 this, SLOT(lookupState()));
108 connect(m_ui.actionOptions, SIGNAL(triggered()),
109 this, SLOT(showSettings()));
111 connect(m_ui.callView, SIGNAL(activated(const QModelIndex &)),
112 this, SLOT(callItemSelected(const QModelIndex &)));
113 connect(m_filterEdit, SIGNAL(returnPressed()),
114 this, SLOT(filterTrace()));
116 m_ui.detailsWebView->page()->setLinkDelegationPolicy(
117 QWebPage::DelegateExternalLinks);
118 connect(m_ui.detailsWebView, SIGNAL(linkClicked(const QUrl&)),
119 this, SLOT(openHelp(const QUrl&)));
122 void MainWindow::openTrace()
125 QFileDialog::getOpenFileName(
129 tr("Trace Files (*.trace)"));
131 qDebug()<< "File name : " <<fileName;
133 newTraceFile(fileName);
136 void MainWindow::loadTrace(const QString &fileName)
138 if (!QFile::exists(fileName)) {
139 QMessageBox::warning(this, tr("File Missing"),
140 tr("File '%1' doesn't exist.").arg(fileName));
143 qDebug()<< "Loading : " <<fileName;
145 m_progressBar->setValue(0);
146 newTraceFile(fileName);
149 void MainWindow::callItemSelected(const QModelIndex &index)
151 ApiTraceEvent *event =
152 index.data(ApiTraceModel::EventRole).value<ApiTraceEvent*>();
154 if (event && event->type() == ApiTraceEvent::Call) {
155 ApiTraceCall *call = static_cast<ApiTraceCall*>(event);
156 m_ui.detailsWebView->setHtml(call->toHtml());
157 m_ui.detailsDock->show();
158 if (call->hasBinaryData()) {
160 call->argValues[call->binaryDataIndex()].toByteArray();
161 m_vdataInterpreter->setData(data);
163 for (int i = 0; i < call->argNames.count(); ++i) {
164 QString name = call->argNames[i];
165 if (name == QLatin1String("stride")) {
166 int stride = call->argValues[i].toInt();
167 m_ui.vertexStrideSB->setValue(stride);
168 } else if (name == QLatin1String("size")) {
169 int components = call->argValues[i].toInt();
170 m_ui.vertexComponentsSB->setValue(components);
171 } else if (name == QLatin1String("type")) {
172 QString val = call->argValues[i].toString();
173 int textIndex = m_ui.vertexTypeCB->findText(val);
175 m_ui.vertexTypeCB->setCurrentIndex(textIndex);
179 m_ui.vertexDataDock->setVisible(call->hasBinaryData());
180 m_selectedEvent = call;
182 if (event && event->type() == ApiTraceEvent::Frame) {
183 m_selectedEvent = static_cast<ApiTraceFrame*>(event);
186 m_ui.detailsDock->hide();
187 m_ui.vertexDataDock->hide();
189 if (m_selectedEvent && !m_selectedEvent->state().isEmpty()) {
192 m_ui.stateDock->hide();
195 void MainWindow::filterTrace()
197 m_proxyModel->setFilterString(m_filterEdit->text());
200 void MainWindow::replayStart()
205 void MainWindow::replayStop()
207 m_retracer->terminate();
208 m_ui.actionStop->setEnabled(false);
209 m_ui.actionReplay->setEnabled(true);
210 m_ui.actionLookupState->setEnabled(true);
213 void MainWindow::newTraceFile(const QString &fileName)
215 m_traceFileName = fileName;
216 m_trace->setFileName(fileName);
218 if (m_traceFileName.isEmpty()) {
219 m_ui.actionReplay->setEnabled(false);
220 m_ui.actionLookupState->setEnabled(false);
221 setWindowTitle(tr("QApiTrace"));
223 QFileInfo info(fileName);
224 m_ui.actionReplay->setEnabled(true);
225 m_ui.actionLookupState->setEnabled(true);
227 tr("QApiTrace - %1").arg(info.fileName()));
231 void MainWindow::replayFinished(const QByteArray &output)
233 m_ui.actionStop->setEnabled(false);
234 m_ui.actionReplay->setEnabled(true);
235 m_ui.actionLookupState->setEnabled(true);
237 if (m_retracer->captureState()) {
239 QVariantMap parsedJson = m_jsonParser->parse(output, &ok).toMap();
240 parseState(parsedJson[QLatin1String("parameters")].toMap());
241 } else if (output.length() < 80) {
242 statusBar()->showMessage(output);
247 void MainWindow::replayError(const QString &message)
249 m_ui.actionStop->setEnabled(false);
250 m_ui.actionReplay->setEnabled(true);
251 m_ui.actionLookupState->setEnabled(true);
254 QMessageBox::warning(
255 this, tr("Replay Failed"), message);
258 void MainWindow::startedLoadingTrace()
261 m_progressBar->show();
262 QFileInfo info(m_trace->fileName());
263 statusBar()->showMessage(
264 tr("Loading %1...").arg(info.fileName()));
267 void MainWindow::finishedLoadingTrace()
269 m_progressBar->hide();
273 QFileInfo info(m_trace->fileName());
274 statusBar()->showMessage(
275 tr("Loaded %1").arg(info.fileName()), 3000);
278 void MainWindow::replayTrace(bool dumpState)
280 if (m_traceFileName.isEmpty())
283 m_retracer->setFileName(m_traceFileName);
284 m_retracer->setCaptureState(dumpState);
285 if (m_retracer->captureState() && m_selectedEvent) {
287 if (m_selectedEvent->type() == ApiTraceEvent::Call) {
288 index = static_cast<ApiTraceCall*>(m_selectedEvent)->index;
289 } else if (m_selectedEvent->type() == ApiTraceEvent::Frame) {
290 ApiTraceFrame *frame = static_cast<ApiTraceFrame*>(m_selectedEvent);
291 if (frame->calls.isEmpty()) {
292 //XXX i guess we could still get the current state
293 qDebug()<<"tried to get a state for an empty frame";
296 index = frame->calls.first()->index;
298 qDebug()<<"Unknown event type";
301 m_retracer->setCaptureAtCallNumber(index);
305 m_ui.actionStop->setEnabled(true);
308 void MainWindow::lookupState()
310 if (!m_selectedEvent) {
311 QMessageBox::warning(
312 this, tr("Unknown Event"),
313 tr("To inspect the state select an event in the event list."));
316 m_stateEvent = m_selectedEvent;
320 MainWindow::~MainWindow()
325 void MainWindow::parseState(const QVariantMap ¶ms)
327 QVariantMap::const_iterator itr;
329 m_stateEvent->setState(params);
330 m_model->stateSetOnEvent(m_stateEvent);
331 if (m_selectedEvent == m_stateEvent) {
334 m_ui.stateDock->hide();
339 variantToString(const QVariant &var, QString &str)
341 if (var.type() == QVariant::List) {
342 QVariantList lst = var.toList();
343 str += QLatin1String("[");
344 for (int i = 0; i < lst.count(); ++i) {
345 QVariant val = lst[i];
346 variantToString(val, str);
347 if (i < lst.count() - 1)
348 str += QLatin1String(", ");
350 str += QLatin1String("]");
351 } else if (var.type() == QVariant::Map) {
352 Q_ASSERT(!"unsupported state type");
353 } else if (var.type() == QVariant::Hash) {
354 Q_ASSERT(!"unsupported state type");
356 str += var.toString();
360 void MainWindow::fillStateForFrame()
362 QVariantMap::const_iterator itr;
365 if (!m_selectedEvent || m_selectedEvent->state().isEmpty())
368 m_ui.stateTreeWidget->clear();
369 params = m_selectedEvent->state();
370 QList<QTreeWidgetItem *> items;
371 for (itr = params.constBegin(); itr != params.constEnd(); ++itr) {
372 QString key = itr.key();
375 variantToString(itr.value(), val);
376 //qDebug()<<"key = "<<key;
377 //qDebug()<<"val = "<<val;
381 items.append(new QTreeWidgetItem((QTreeWidget*)0, lst));
383 m_ui.stateTreeWidget->insertTopLevelItems(0, items);
384 m_ui.stateDock->show();
387 void MainWindow::showSettings()
389 SettingsDialog dialog;
390 dialog.setFilterOptions(m_proxyModel->filterOptions());
392 if (dialog.exec() == QDialog::Accepted) {
393 m_proxyModel->setFilterOptions(dialog.filterOptions());
397 void MainWindow::openHelp(const QUrl &url)
399 QDesktopServices::openUrl(url);
402 #include "mainwindow.moc"