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>
17 #include <QFileDialog>
19 #include <QMessageBox>
20 #include <QProgressBar>
25 MainWindow::MainWindow()
29 m_jsonParser(new QJson::Parser())
32 m_ui.stateTreeWidget->sortByColumn(0, Qt::AscendingOrder);
34 m_trace = new ApiTrace();
35 connect(m_trace, SIGNAL(startedLoadingTrace()),
36 this, SLOT(startedLoadingTrace()));
37 connect(m_trace, SIGNAL(finishedLoadingTrace()),
38 this, SLOT(finishedLoadingTrace()));
40 m_retracer = new Retracer(this);
41 connect(m_retracer, SIGNAL(finished(const QByteArray&)),
42 this, SLOT(replayFinished(const QByteArray&)));
43 connect(m_retracer, SIGNAL(error(const QString&)),
44 this, SLOT(replayError(const QString&)));
46 m_vdataInterpreter = new VertexDataInterpreter(this);
47 m_vdataInterpreter->setListWidget(m_ui.vertexDataListWidget);
48 m_vdataInterpreter->setStride(
49 m_ui.vertexStrideSB->value());
50 m_vdataInterpreter->setComponents(
51 m_ui.vertexComponentsSB->value());
52 m_vdataInterpreter->setTypeFromString(
53 m_ui.vertexTypeCB->currentText());
55 connect(m_ui.vertexInterpretButton, SIGNAL(clicked()),
56 m_vdataInterpreter, SLOT(interpretData()));
57 connect(m_ui.vertexTypeCB, SIGNAL(activated(const QString&)),
58 m_vdataInterpreter, SLOT(setTypeFromString(const QString&)));
59 connect(m_ui.vertexStrideSB, SIGNAL(valueChanged(int)),
60 m_vdataInterpreter, SLOT(setStride(int)));
61 connect(m_ui.vertexComponentsSB, SIGNAL(valueChanged(int)),
62 m_vdataInterpreter, SLOT(setComponents(int)));
64 m_model = new ApiTraceModel();
65 m_model->setApiTrace(m_trace);
66 m_proxyModel = new ApiTraceFilter();
67 m_proxyModel->setSourceModel(m_model);
68 m_ui.callView->setModel(m_proxyModel);
69 m_ui.callView->setItemDelegate(new ApiCallDelegate);
70 m_ui.callView->resizeColumnToContents(0);
71 m_ui.callView->header()->swapSections(0, 1);
72 m_ui.callView->setColumnWidth(1, 42);
74 QToolBar *toolBar = addToolBar(tr("Navigation"));
75 m_filterEdit = new QLineEdit(toolBar);
76 toolBar->addWidget(m_filterEdit);
78 m_progressBar = new QProgressBar();
79 m_progressBar->setRange(0, 0);
80 statusBar()->addPermanentWidget(m_progressBar);
81 m_progressBar->hide();
83 m_ui.detailsDock->hide();
84 m_ui.vertexDataDock->hide();
85 m_ui.stateDock->hide();
86 setDockOptions(dockOptions() | QMainWindow::ForceTabbedDocks);
88 tabifyDockWidget(m_ui.stateDock, m_ui.vertexDataDock);
90 connect(m_ui.actionOpen, SIGNAL(triggered()),
91 this, SLOT(openTrace()));
92 connect(m_ui.actionQuit, SIGNAL(triggered()),
95 connect(m_ui.actionReplay, SIGNAL(triggered()),
96 this, SLOT(replayStart()));
97 connect(m_ui.actionStop, SIGNAL(triggered()),
98 this, SLOT(replayStop()));
99 connect(m_ui.actionLookupState, SIGNAL(triggered()),
100 this, SLOT(lookupState()));
101 connect(m_ui.actionOptions, SIGNAL(triggered()),
102 this, SLOT(showSettings()));
104 connect(m_ui.callView, SIGNAL(activated(const QModelIndex &)),
105 this, SLOT(callItemSelected(const QModelIndex &)));
106 connect(m_filterEdit, SIGNAL(returnPressed()),
107 this, SLOT(filterTrace()));
110 void MainWindow::openTrace()
113 QFileDialog::getOpenFileName(
117 tr("Trace Files (*.trace)"));
119 qDebug()<< "File name : " <<fileName;
121 newTraceFile(fileName);
124 void MainWindow::loadTrace(const QString &fileName)
126 if (!QFile::exists(fileName)) {
127 QMessageBox::warning(this, tr("File Missing"),
128 tr("File '%1' doesn't exist.").arg(fileName));
131 qDebug()<< "Loading : " <<fileName;
133 m_progressBar->setValue(0);
134 newTraceFile(fileName);
137 void MainWindow::callItemSelected(const QModelIndex &index)
139 ApiTraceEvent *event =
140 index.data(ApiTraceModel::EventRole).value<ApiTraceEvent*>();
142 if (event && event->type() == ApiTraceEvent::Call) {
143 ApiTraceCall *call = static_cast<ApiTraceCall*>(event);
144 m_ui.detailsWebView->setHtml(call->toHtml());
145 m_ui.detailsDock->show();
146 if (call->hasBinaryData()) {
148 call->argValues[call->binaryDataIndex()].toByteArray();
149 m_vdataInterpreter->setData(data);
151 for (int i = 0; i < call->argNames.count(); ++i) {
152 QString name = call->argNames[i];
153 if (name == QLatin1String("stride")) {
154 int stride = call->argValues[i].toInt();
155 m_ui.vertexStrideSB->setValue(stride);
156 } else if (name == QLatin1String("size")) {
157 int components = call->argValues[i].toInt();
158 m_ui.vertexComponentsSB->setValue(components);
159 } else if (name == QLatin1String("type")) {
160 QString val = call->argValues[i].toString();
161 int textIndex = m_ui.vertexTypeCB->findText(val);
163 m_ui.vertexTypeCB->setCurrentIndex(textIndex);
167 m_ui.vertexDataDock->setVisible(call->hasBinaryData());
168 m_selectedEvent = call;
170 if (event && event->type() == ApiTraceEvent::Frame) {
171 m_selectedEvent = static_cast<ApiTraceFrame*>(event);
174 m_ui.detailsDock->hide();
175 m_ui.vertexDataDock->hide();
177 if (m_selectedEvent && !m_selectedEvent->state().isEmpty()) {
180 m_ui.stateDock->hide();
183 void MainWindow::filterTrace()
185 m_proxyModel->setFilterString(m_filterEdit->text());
188 void MainWindow::replayStart()
193 void MainWindow::replayStop()
195 m_retracer->terminate();
196 m_ui.actionStop->setEnabled(false);
197 m_ui.actionReplay->setEnabled(true);
198 m_ui.actionLookupState->setEnabled(true);
201 void MainWindow::newTraceFile(const QString &fileName)
203 m_traceFileName = fileName;
204 m_trace->setFileName(fileName);
206 if (m_traceFileName.isEmpty()) {
207 m_ui.actionReplay->setEnabled(false);
208 m_ui.actionLookupState->setEnabled(false);
209 setWindowTitle(tr("QApiTrace"));
211 QFileInfo info(fileName);
212 m_ui.actionReplay->setEnabled(true);
213 m_ui.actionLookupState->setEnabled(true);
215 tr("QApiTrace - %1").arg(info.fileName()));
219 void MainWindow::replayFinished(const QByteArray &output)
221 m_ui.actionStop->setEnabled(false);
222 m_ui.actionReplay->setEnabled(true);
223 m_ui.actionLookupState->setEnabled(true);
225 if (m_retracer->captureState()) {
227 QVariantMap parsedJson = m_jsonParser->parse(output, &ok).toMap();
228 parseState(parsedJson[QLatin1String("parameters")].toMap());
229 } else if (output.length() < 80) {
230 statusBar()->showMessage(output);
235 void MainWindow::replayError(const QString &message)
237 m_ui.actionStop->setEnabled(false);
238 m_ui.actionReplay->setEnabled(true);
239 m_ui.actionLookupState->setEnabled(true);
242 QMessageBox::warning(
243 this, tr("Replay Failed"), message);
246 void MainWindow::startedLoadingTrace()
249 m_progressBar->show();
250 QFileInfo info(m_trace->fileName());
251 statusBar()->showMessage(
252 tr("Loading %1...").arg(info.fileName()));
255 void MainWindow::finishedLoadingTrace()
257 m_progressBar->hide();
261 QFileInfo info(m_trace->fileName());
262 statusBar()->showMessage(
263 tr("Loaded %1").arg(info.fileName()), 3000);
266 void MainWindow::replayTrace(bool dumpState)
268 if (m_traceFileName.isEmpty())
271 m_retracer->setFileName(m_traceFileName);
272 m_retracer->setCaptureState(dumpState);
273 if (m_retracer->captureState() && m_selectedEvent) {
275 if (m_selectedEvent->type() == ApiTraceEvent::Call) {
276 index = static_cast<ApiTraceCall*>(m_selectedEvent)->index;
277 } else if (m_selectedEvent->type() == ApiTraceEvent::Frame) {
278 ApiTraceFrame *frame = static_cast<ApiTraceFrame*>(m_selectedEvent);
279 if (frame->calls.isEmpty()) {
280 //XXX i guess we could still get the current state
281 qDebug()<<"tried to get a state for an empty frame";
284 index = frame->calls.first()->index;
286 qDebug()<<"Unknown event type";
289 m_retracer->setCaptureAtCallNumber(index);
293 m_ui.actionStop->setEnabled(true);
296 void MainWindow::lookupState()
298 if (!m_selectedEvent) {
299 QMessageBox::warning(
300 this, tr("Unknown Event"),
301 tr("To inspect the state select an event in the event list."));
304 m_stateEvent = m_selectedEvent;
308 MainWindow::~MainWindow()
313 void MainWindow::parseState(const QVariantMap ¶ms)
315 QVariantMap::const_iterator itr;
317 m_stateEvent->setState(params);
318 m_model->stateSetOnEvent(m_stateEvent);
319 if (m_selectedEvent == m_stateEvent) {
322 m_ui.stateDock->hide();
327 variantToString(const QVariant &var, QString &str)
329 if (var.type() == QVariant::List) {
330 QVariantList lst = var.toList();
331 str += QLatin1String("[");
332 for (int i = 0; i < lst.count(); ++i) {
333 QVariant val = lst[i];
334 variantToString(val, str);
335 if (i < lst.count() - 1)
336 str += QLatin1String(", ");
338 str += QLatin1String("]");
339 } else if (var.type() == QVariant::Map) {
340 Q_ASSERT(!"unsupported state type");
341 } else if (var.type() == QVariant::Hash) {
342 Q_ASSERT(!"unsupported state type");
344 str += var.toString();
348 void MainWindow::fillStateForFrame()
350 QVariantMap::const_iterator itr;
353 if (!m_selectedEvent || m_selectedEvent->state().isEmpty())
356 m_ui.stateTreeWidget->clear();
357 params = m_selectedEvent->state();
358 QList<QTreeWidgetItem *> items;
359 for (itr = params.constBegin(); itr != params.constEnd(); ++itr) {
360 QString key = itr.key();
363 variantToString(itr.value(), val);
364 //qDebug()<<"key = "<<key;
365 //qDebug()<<"val = "<<val;
369 items.append(new QTreeWidgetItem((QTreeWidget*)0, lst));
371 m_ui.stateTreeWidget->insertTopLevelItems(0, items);
372 m_ui.stateDock->show();
375 void MainWindow::showSettings()
377 SettingsDialog dialog;
378 dialog.setFilterOptions(m_proxyModel->filterOptions());
380 if (dialog.exec() == QDialog::Accepted) {
381 m_proxyModel->setFilterOptions(dialog.filterOptions());
385 #include "mainwindow.moc"