1 #include "mainwindow.h"
4 #include "apitracecall.h"
5 #include "apicalldelegate.h"
6 #include "apitracemodel.h"
7 #include "apitracefilter.h"
9 #include <qjson/parser.h>
14 #include <QFileDialog>
16 #include <QMessageBox>
18 #include <QProgressBar>
23 MainWindow::MainWindow()
28 m_findingState(false),
29 m_jsonParser(new QJson::Parser())
33 m_trace = new ApiTrace();
34 connect(m_trace, SIGNAL(startedLoadingTrace()),
35 this, SLOT(startedLoadingTrace()));
36 connect(m_trace, SIGNAL(finishedLoadingTrace()),
37 this, SLOT(finishedLoadingTrace()));
39 m_model = new ApiTraceModel();
40 m_model->setApiTrace(m_trace);
41 m_proxyModel = new ApiTraceFilter();
42 m_proxyModel->setSourceModel(m_model);
43 m_ui.callView->setModel(m_proxyModel);
44 m_ui.callView->setItemDelegate(new ApiCallDelegate);
45 for (int column = 0; column < m_model->columnCount(); ++column)
46 m_ui.callView->resizeColumnToContents(column);
48 QToolBar *toolBar = addToolBar(tr("Navigation"));
49 m_filterEdit = new QLineEdit(toolBar);
50 toolBar->addWidget(m_filterEdit);
52 m_progressBar = new QProgressBar();
53 m_progressBar->setRange(0, 0);
54 statusBar()->addPermanentWidget(m_progressBar);
55 m_progressBar->hide();
57 m_ui.detailsDock->hide();
58 m_ui.stateDock->hide();
60 connect(m_ui.actionOpen, SIGNAL(triggered()),
61 this, SLOT(openTrace()));
62 connect(m_ui.actionQuit, SIGNAL(triggered()),
65 connect(m_ui.actionReplay, SIGNAL(triggered()),
66 this, SLOT(replayStart()));
67 connect(m_ui.actionStop, SIGNAL(triggered()),
68 this, SLOT(replayStop()));
69 connect(m_ui.actionLookupState, SIGNAL(triggered()),
70 this, SLOT(lookupState()));
72 connect(m_ui.callView, SIGNAL(activated(const QModelIndex &)),
73 this, SLOT(callItemSelected(const QModelIndex &)));
74 connect(m_filterEdit, SIGNAL(returnPressed()),
75 this, SLOT(filterTrace()));
78 void MainWindow::openTrace()
81 QFileDialog::getOpenFileName(
85 tr("Trace Files (*.trace)"));
87 qDebug()<< "File name : " <<fileName;
89 newTraceFile(fileName);
92 void MainWindow::loadTrace(const QString &fileName)
94 if (!QFile::exists(fileName)) {
95 QMessageBox::warning(this, tr("File Missing"),
96 tr("File '%1' doesn't exist.").arg(fileName));
99 qDebug()<< "Loading : " <<fileName;
101 m_progressBar->setValue(0);
102 newTraceFile(fileName);
105 void MainWindow::callItemSelected(const QModelIndex &index)
107 ApiTraceCall *call = index.data().value<ApiTraceCall*>();
109 m_ui.detailsWebView->setHtml(call->toHtml());
110 m_ui.detailsDock->show();
111 m_selectedEvent = call;
113 m_selectedEvent = index.data().value<ApiTraceFrame*>();
114 m_ui.detailsDock->hide();
116 if (m_selectedEvent && !m_selectedEvent->state().isEmpty()) {
119 m_ui.stateDock->hide();
122 void MainWindow::filterTrace()
124 m_proxyModel->setFilterString(m_filterEdit->text());
127 void MainWindow::replayStart()
132 void MainWindow::replayStop()
134 if (m_replayProcess) {
135 m_replayProcess->kill();
137 m_ui.actionStop->setEnabled(false);
138 m_ui.actionReplay->setEnabled(true);
139 m_ui.actionLookupState->setEnabled(true);
143 void MainWindow::newTraceFile(const QString &fileName)
145 m_traceFileName = fileName;
146 m_trace->setFileName(fileName);
148 if (m_traceFileName.isEmpty()) {
149 m_ui.actionReplay->setEnabled(false);
150 m_ui.actionLookupState->setEnabled(false);
152 m_ui.actionReplay->setEnabled(true);
153 m_ui.actionLookupState->setEnabled(true);
157 void MainWindow::replayFinished()
159 m_ui.actionStop->setEnabled(false);
160 m_ui.actionReplay->setEnabled(true);
161 m_ui.actionLookupState->setEnabled(true);
163 QByteArray output = m_replayProcess->readAllStandardOutput();
166 qDebug()<<"Process finished = ";
167 qDebug()<<"\terr = "<<m_replayProcess->readAllStandardError();
168 qDebug()<<"\tout = "<<output;
171 if (m_findingState) {
173 QVariantMap parsedJson = m_jsonParser->parse(output, &ok).toMap();
174 parseState(parsedJson[QLatin1String("parameters")].toMap());
175 } else if (output.length() < 80) {
176 statusBar()->showMessage(output);
179 m_findingState = false;
182 void MainWindow::replayError(QProcess::ProcessError err)
184 m_ui.actionStop->setEnabled(false);
185 m_ui.actionReplay->setEnabled(true);
186 m_ui.actionLookupState->setEnabled(true);
187 m_findingState = false;
190 qDebug()<<"Process error = "<<err;
191 qDebug()<<"\terr = "<<m_replayProcess->readAllStandardError();
192 qDebug()<<"\tout = "<<m_replayProcess->readAllStandardOutput();
193 QMessageBox::warning(
194 this, tr("Replay Failed"),
195 tr("Couldn't execute the replay file '%1'").arg(m_traceFileName));
198 void MainWindow::startedLoadingTrace()
201 m_progressBar->show();
202 QFileInfo info(m_trace->fileName());
203 statusBar()->showMessage(
204 tr("Loading %1...").arg(info.fileName()));
207 void MainWindow::finishedLoadingTrace()
209 m_progressBar->hide();
213 QFileInfo info(m_trace->fileName());
214 statusBar()->showMessage(
215 tr("Loaded %1").arg(info.fileName()), 3000);
218 void MainWindow::replayTrace(bool dumpState)
220 if (!m_replayProcess) {
222 QString format = QLatin1String("%1;");
224 QString format = QLatin1String("%1:");
226 QString buildPath = format.arg(BUILD_DIR);
227 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
228 env.insert("PATH", buildPath + env.value("PATH"));
230 qputenv("PATH", env.value("PATH").toLatin1());
232 m_replayProcess = new QProcess(this);
233 m_replayProcess->setProcessEnvironment(env);
235 connect(m_replayProcess, SIGNAL(finished(int, QProcess::ExitStatus)),
236 this, SLOT(replayFinished()));
237 connect(m_replayProcess, SIGNAL(error(QProcess::ProcessError)),
238 this, SLOT(replayError(QProcess::ProcessError)));
241 if (m_traceFileName.isEmpty())
244 QStringList arguments;
248 if (m_selectedEvent->type() == ApiTraceEvent::Call) {
249 index = static_cast<ApiTraceCall*>(m_selectedEvent)->index;
250 } else if (m_selectedEvent->type() == ApiTraceEvent::Frame) {
251 ApiTraceFrame *frame = static_cast<ApiTraceFrame*>(m_selectedEvent);
252 if (frame->calls.isEmpty()) {
253 //XXX i guess we could still get the current state
254 qDebug()<<"tried to get a state for an empty frame";
257 index = frame->calls.first()->index;
259 qDebug()<<"Unknown event type";
262 arguments << QLatin1String("-D");
263 arguments << QString::number(index);
265 arguments << m_traceFileName;
267 m_replayProcess->start(QLatin1String("glretrace"),
270 m_ui.actionStop->setEnabled(true);
273 void MainWindow::lookupState()
275 if (!m_selectedEvent) {
276 QMessageBox::warning(
277 this, tr("Unknown Event"),
278 tr("To inspect the state select an event in the event list."));
281 m_stateEvent = m_selectedEvent;
282 m_findingState = true;
286 MainWindow::~MainWindow()
291 void MainWindow::parseState(const QVariantMap ¶ms)
293 QVariantMap::const_iterator itr;
295 m_stateEvent->setState(params);
296 if (m_selectedEvent == m_stateEvent) {
299 m_ui.stateDock->hide();
304 variantToString(const QVariant &var, QString &str)
306 if (var.type() == QVariant::List) {
307 QVariantList lst = var.toList();
308 str += QLatin1String("[");
309 for (int i = 0; i < lst.count(); ++i) {
310 QVariant val = lst[i];
311 variantToString(val, str);
312 if (i < lst.count() - 1)
313 str += QLatin1String(", ");
315 str += QLatin1String("]");
316 } else if (var.type() == QVariant::Map) {
317 Q_ASSERT(!"unsupported state type");
318 } else if (var.type() == QVariant::Hash) {
319 Q_ASSERT(!"unsupported state type");
321 str += var.toString();
325 void MainWindow::fillStateForFrame()
327 QVariantMap::const_iterator itr;
330 if (!m_selectedEvent || m_selectedEvent->state().isEmpty())
333 m_ui.stateTreeWidget->clear();
334 params = m_selectedEvent->state();
335 QList<QTreeWidgetItem *> items;
336 for (itr = params.constBegin(); itr != params.constEnd(); ++itr) {
337 QString key = itr.key();
340 variantToString(itr.value(), val);
341 //qDebug()<<"key = "<<key;
342 //qDebug()<<"val = "<<val;
346 items.append(new QTreeWidgetItem((QTreeWidget*)0, lst));
348 m_ui.stateTreeWidget->insertTopLevelItems(0, items);
349 m_ui.stateDock->show();
352 #include "mainwindow.moc"