]> git.cworth.org Git - apitrace/blob - gui/mainwindow.cpp
Best effort to show (and compare) nested state parameters.
[apitrace] / gui / mainwindow.cpp
1 #include "mainwindow.h"
2
3 #include "apitrace.h"
4 #include "apitracecall.h"
5 #include "apicalldelegate.h"
6 #include "apitracemodel.h"
7 #include "apitracefilter.h"
8 #include "imageviewer.h"
9 #include "jumpwidget.h"
10 #include "retracer.h"
11 #include "searchwidget.h"
12 #include "settingsdialog.h"
13 #include "shaderssourcewidget.h"
14 #include "tracedialog.h"
15 #include "traceprocess.h"
16 #include "ui_retracerdialog.h"
17 #include "vertexdatainterpreter.h"
18
19 #include <QAction>
20 #include <QDebug>
21 #include <QDesktopServices>
22 #include <QDir>
23 #include <QFileDialog>
24 #include <QLineEdit>
25 #include <QMessageBox>
26 #include <QProgressBar>
27 #include <QShortcut>
28 #include <QToolBar>
29 #include <QUrl>
30 #include <QVBoxLayout>
31 #include <QWebPage>
32 #include <QWebView>
33
34
35 MainWindow::MainWindow()
36     : QMainWindow(),
37       m_selectedEvent(0),
38       m_stateEvent(0)
39 {
40     m_ui.setupUi(this);
41     initObjects();
42     initConnections();
43 }
44
45 void MainWindow::createTrace()
46 {
47     TraceDialog dialog;
48
49     if (!m_traceProcess->canTrace()) {
50         QMessageBox::warning(
51             this,
52             tr("Unsupported"),
53             tr("Current configuration doesn't support tracing."));
54         return;
55     }
56
57     if (dialog.exec() == QDialog::Accepted) {
58         qDebug()<< "App : " <<dialog.applicationPath();
59         qDebug()<< "  Arguments: "<<dialog.arguments();
60         m_traceProcess->setExecutablePath(dialog.applicationPath());
61         m_traceProcess->setArguments(dialog.arguments());
62         m_traceProcess->start();
63     }
64 }
65
66 void MainWindow::openTrace()
67 {
68     QString fileName =
69         QFileDialog::getOpenFileName(
70             this,
71             tr("Open Trace"),
72             QDir::homePath(),
73             tr("Trace Files (*.trace)"));
74
75     qDebug()<< "File name : " <<fileName;
76
77     newTraceFile(fileName);
78 }
79
80 void MainWindow::loadTrace(const QString &fileName)
81 {
82     if (!QFile::exists(fileName)) {
83         QMessageBox::warning(this, tr("File Missing"),
84                              tr("File '%1' doesn't exist.").arg(fileName));
85         return;
86     }
87     qDebug()<< "Loading  : " <<fileName;
88
89     m_progressBar->setValue(0);
90     newTraceFile(fileName);
91 }
92
93 void MainWindow::callItemSelected(const QModelIndex &index)
94 {
95     ApiTraceEvent *event =
96         index.data(ApiTraceModel::EventRole).value<ApiTraceEvent*>();
97
98     if (event && event->type() == ApiTraceEvent::Call) {
99         ApiTraceCall *call = static_cast<ApiTraceCall*>(event);
100         m_ui.detailsWebView->setHtml(call->toHtml());
101         m_ui.detailsDock->show();
102         if (call->hasBinaryData()) {
103             QByteArray data =
104                 call->argValues[call->binaryDataIndex()].toByteArray();
105             m_vdataInterpreter->setData(data);
106
107             for (int i = 0; i < call->argNames.count(); ++i) {
108                 QString name = call->argNames[i];
109                 if (name == QLatin1String("stride")) {
110                     int stride = call->argValues[i].toInt();
111                     m_ui.vertexStrideSB->setValue(stride);
112                 } else if (name == QLatin1String("size")) {
113                     int components = call->argValues[i].toInt();
114                     m_ui.vertexComponentsSB->setValue(components);
115                 } else if (name == QLatin1String("type")) {
116                     QString val = call->argValues[i].toString();
117                     int textIndex = m_ui.vertexTypeCB->findText(val);
118                     if (textIndex >= 0)
119                         m_ui.vertexTypeCB->setCurrentIndex(textIndex);
120                 }
121             }
122         }
123         m_ui.vertexDataDock->setVisible(call->hasBinaryData());
124         m_selectedEvent = call;
125     } else {
126         if (event && event->type() == ApiTraceEvent::Frame) {
127             m_selectedEvent = static_cast<ApiTraceFrame*>(event);
128         } else
129             m_selectedEvent = 0;
130         m_ui.detailsDock->hide();
131         m_ui.vertexDataDock->hide();
132     }
133     if (m_selectedEvent && !m_selectedEvent->state().isEmpty()) {
134         fillStateForFrame();
135     } else
136         m_ui.stateDock->hide();
137 }
138
139 void MainWindow::replayStart()
140 {
141     QDialog dlg;
142     Ui_RetracerDialog dlgUi;
143     dlgUi.setupUi(&dlg);
144
145     dlgUi.doubleBufferingCB->setChecked(
146         m_retracer->isDoubleBuffered());
147     dlgUi.benchmarkCB->setChecked(
148         m_retracer->isBenchmarking());
149
150     if (dlg.exec() == QDialog::Accepted) {
151         m_retracer->setDoubleBuffered(
152             dlgUi.doubleBufferingCB->isChecked());
153         m_retracer->setBenchmarking(
154             dlgUi.benchmarkCB->isChecked());
155         replayTrace(false);
156     }
157 }
158
159 void MainWindow::replayStop()
160 {
161     m_retracer->quit();
162     m_ui.actionStop->setEnabled(false);
163     m_ui.actionReplay->setEnabled(true);
164     m_ui.actionLookupState->setEnabled(true);
165 }
166
167 void MainWindow::newTraceFile(const QString &fileName)
168 {
169     m_traceFileName = fileName;
170     m_trace->setFileName(fileName);
171
172     if (m_traceFileName.isEmpty()) {
173         m_ui.actionReplay->setEnabled(false);
174         m_ui.actionLookupState->setEnabled(false);
175         setWindowTitle(tr("QApiTrace"));
176     } else {
177         QFileInfo info(fileName);
178         m_ui.actionReplay->setEnabled(true);
179         m_ui.actionLookupState->setEnabled(true);
180         setWindowTitle(
181             tr("QApiTrace - %1").arg(info.fileName()));
182     }
183 }
184
185 void MainWindow::replayFinished(const QString &output)
186 {
187     m_ui.actionStop->setEnabled(false);
188     m_ui.actionReplay->setEnabled(true);
189     m_ui.actionLookupState->setEnabled(true);
190
191     m_progressBar->hide();
192     if (output.length() < 80) {
193         statusBar()->showMessage(output);
194     }
195     m_stateEvent = 0;
196     statusBar()->showMessage(
197         tr("Replaying finished!"), 2000);
198 }
199
200 void MainWindow::replayError(const QString &message)
201 {
202     m_ui.actionStop->setEnabled(false);
203     m_ui.actionReplay->setEnabled(true);
204     m_ui.actionLookupState->setEnabled(true);
205     m_stateEvent = 0;
206
207     m_progressBar->hide();
208     statusBar()->showMessage(
209         tr("Replaying unsuccessful."), 2000);
210     QMessageBox::warning(
211         this, tr("Replay Failed"), message);
212 }
213
214 void MainWindow::startedLoadingTrace()
215 {
216     Q_ASSERT(m_trace);
217     m_progressBar->show();
218     QFileInfo info(m_trace->fileName());
219     statusBar()->showMessage(
220         tr("Loading %1...").arg(info.fileName()));
221 }
222
223 void MainWindow::finishedLoadingTrace()
224 {
225     m_progressBar->hide();
226     if (!m_trace) {
227         return;
228     }
229     QFileInfo info(m_trace->fileName());
230     statusBar()->showMessage(
231         tr("Loaded %1").arg(info.fileName()), 3000);
232 }
233
234 void MainWindow::replayTrace(bool dumpState)
235 {
236     if (m_traceFileName.isEmpty())
237         return;
238
239     m_retracer->setFileName(m_traceFileName);
240     m_retracer->setCaptureState(dumpState);
241     if (m_retracer->captureState() && m_selectedEvent) {
242         int index = 0;
243         if (m_selectedEvent->type() == ApiTraceEvent::Call) {
244             index = static_cast<ApiTraceCall*>(m_selectedEvent)->index;
245         } else if (m_selectedEvent->type() == ApiTraceEvent::Frame) {
246             ApiTraceFrame *frame =
247                 static_cast<ApiTraceFrame*>(m_selectedEvent);
248             if (frame->calls.isEmpty()) {
249                 //XXX i guess we could still get the current state
250                 qDebug()<<"tried to get a state for an empty frame";
251                 return;
252             }
253             index = frame->calls.first()->index;
254         } else {
255             qDebug()<<"Unknown event type";
256             return;
257         }
258         m_retracer->setCaptureAtCallNumber(index);
259     }
260     m_retracer->start();
261
262     m_ui.actionStop->setEnabled(true);
263     m_progressBar->show();
264     if (dumpState)
265         statusBar()->showMessage(
266             tr("Looking up the state..."));
267     else
268         statusBar()->showMessage(
269             tr("Replaying the trace file..."));
270 }
271
272 void MainWindow::lookupState()
273 {
274     if (!m_selectedEvent) {
275         QMessageBox::warning(
276             this, tr("Unknown Event"),
277             tr("To inspect the state select an event in the event list."));
278         return;
279     }
280     m_stateEvent = m_selectedEvent;
281     replayTrace(true);
282 }
283
284 MainWindow::~MainWindow()
285 {
286 }
287
288 static void
289 variantToString(const QVariant &var, QString &str)
290 {
291     if (var.type() == QVariant::List) {
292         QVariantList lst = var.toList();
293         str += QLatin1String("[");
294         for (int i = 0; i < lst.count(); ++i) {
295             QVariant val = lst[i];
296             variantToString(val, str);
297             if (i < lst.count() - 1)
298                 str += QLatin1String(", ");
299         }
300         str += QLatin1String("]");
301     } else if (var.type() == QVariant::Map) {
302         Q_ASSERT(!"unsupported state type");
303     } else if (var.type() == QVariant::Hash) {
304         Q_ASSERT(!"unsupported state type");
305     } else {
306         str += var.toString();
307     }
308 }
309
310 static QTreeWidgetItem *
311 variantToItem(const QString &key, const QVariant &var, const QVariant &defaultVar);
312
313 static void
314 variantMapToItems(const QVariantMap &map, const QVariantMap &defaultMap, QList<QTreeWidgetItem *> &items)
315 {
316     QVariantMap::const_iterator itr;
317     for (itr = map.constBegin(); itr != map.constEnd(); ++itr) {
318         QString key = itr.key();
319         QVariant var = itr.value();
320         QVariant defaultVar = defaultMap[key];
321
322         QTreeWidgetItem *item = variantToItem(key, var, defaultVar);
323         if (item) {
324             items.append(item);
325         }
326     }
327 }
328
329 static void
330 variantListToItems(const QVariantList &lst, const QVariantList &defaultLst, QList<QTreeWidgetItem *> &items)
331 {
332     for (int i = 0; i < lst.count(); ++i) {
333         QString key = QString::number(i);
334         QVariant var = lst[i];
335         QVariant defaultVar;
336         
337         if (i < defaultLst.count()) {
338             defaultVar = defaultLst[i];
339         }
340
341         QTreeWidgetItem *item = variantToItem(key, var, defaultVar);
342         if (item) {
343             items.append(item);
344         }
345     }
346 }
347
348 static bool
349 isVariantDeep(const QVariant &var)
350 {
351     if (var.type() == QVariant::List) {
352         QVariantList lst = var.toList();
353         for (int i = 0; i < lst.count(); ++i) {
354             if (isVariantDeep(lst[i])) {
355                 return true;
356             }
357         }
358         return false;
359     } else if (var.type() == QVariant::Map) {
360         return true;
361     } else if (var.type() == QVariant::Hash) {
362         return true;
363     } else {
364         return false;
365     }
366 }
367
368 static QTreeWidgetItem *
369 variantToItem(const QString &key, const QVariant &var, const QVariant &defaultVar)
370 {
371     if (var == defaultVar) {
372         return NULL;
373     }
374
375     QString val;
376
377     bool deep = isVariantDeep(var);
378     if (!deep) {
379         variantToString(var, val);
380     }
381
382     //qDebug()<<"key = "<<key;
383     //qDebug()<<"val = "<<val;
384     QStringList lst;
385     lst += key;
386     lst += val;
387
388     QTreeWidgetItem *item = new QTreeWidgetItem((QTreeWidgetItem *)0, lst);
389
390     if (deep) {
391         QList<QTreeWidgetItem *> children;
392         if (var.type() == QVariant::Map) {
393             QVariantMap map = var.toMap();
394             QVariantMap defaultMap = defaultVar.toMap();
395             variantMapToItems(map, defaultMap, children);
396         }
397         if (var.type() == QVariant::List) {
398             QVariantList lst = var.toList();
399             QVariantList defaultLst = defaultVar.toList();
400             variantListToItems(lst, defaultLst, children);
401         }
402         item->addChildren(children);
403     }
404
405     return item;
406 }
407
408 void MainWindow::fillStateForFrame()
409 {
410     QVariantMap params;
411
412     if (!m_selectedEvent || m_selectedEvent->state().isEmpty())
413         return;
414
415     bool nonDefaults = m_ui.nonDefaultsCB->isChecked();
416     QVariantMap defaultParams;
417     if (nonDefaults) {
418         ApiTraceState defaultState = m_trace->defaultState();
419         defaultParams = defaultState.parameters();
420     }
421
422     const ApiTraceState &state = m_selectedEvent->state();
423     m_ui.stateTreeWidget->clear();
424     params = state.parameters();
425     QList<QTreeWidgetItem *> items;
426     variantMapToItems(params, defaultParams, items);
427     m_ui.stateTreeWidget->insertTopLevelItems(0, items);
428
429     QMap<QString, QString> shaderSources = state.shaderSources();
430     if (shaderSources.isEmpty()) {
431         m_sourcesWidget->setShaders(shaderSources);
432     } else {
433         m_sourcesWidget->setShaders(shaderSources);
434     }
435
436     const QList<ApiTexture> &textures =
437         state.textures();
438     const QList<ApiFramebuffer> &fbos =
439         state.framebuffers();
440
441     m_ui.surfacesTreeWidget->clear();
442     if (textures.isEmpty() && fbos.isEmpty()) {
443         m_ui.surfacesTab->setDisabled(false);
444     } else {
445         m_ui.surfacesTreeWidget->setIconSize(QSize(64, 64));
446         if (!textures.isEmpty()) {
447             QTreeWidgetItem *textureItem =
448                 new QTreeWidgetItem(m_ui.surfacesTreeWidget);
449             textureItem->setText(0, tr("Textures"));
450             if (textures.count() <= 6)
451                 textureItem->setExpanded(true);
452
453             for (int i = 0; i < textures.count(); ++i) {
454                 const ApiTexture &texture =
455                     textures[i];
456                 QIcon icon(QPixmap::fromImage(texture.thumb()));
457                 QTreeWidgetItem *item = new QTreeWidgetItem(textureItem);
458                 item->setIcon(0, icon);
459                 int width = texture.size().width();
460                 int height = texture.size().height();
461                 QString descr =
462                     QString::fromLatin1("%1, %2 x %3")
463                     .arg(texture.target())
464                     .arg(width)
465                     .arg(height);
466                 item->setText(1, descr);
467
468                 item->setData(0, Qt::UserRole,
469                               texture.image());
470             }
471         }
472         if (!fbos.isEmpty()) {
473             QTreeWidgetItem *fboItem =
474                 new QTreeWidgetItem(m_ui.surfacesTreeWidget);
475             fboItem->setText(0, tr("Framebuffers"));
476             if (fbos.count() <= 6)
477                 fboItem->setExpanded(true);
478
479             for (int i = 0; i < fbos.count(); ++i) {
480                 const ApiFramebuffer &fbo =
481                     fbos[i];
482                 QIcon icon(QPixmap::fromImage(fbo.thumb()));
483                 QTreeWidgetItem *item = new QTreeWidgetItem(fboItem);
484                 item->setIcon(0, icon);
485                 int width = fbo.size().width();
486                 int height = fbo.size().height();
487                 QString descr =
488                     QString::fromLatin1("%1, %2 x %3")
489                     .arg(fbo.type())
490                     .arg(width)
491                     .arg(height);
492                 item->setText(1, descr);
493
494                 item->setData(0, Qt::UserRole,
495                               fbo.image());
496             }
497         }
498         m_ui.surfacesTab->setEnabled(true);
499     }
500     m_ui.stateDock->show();
501 }
502
503 void MainWindow::showSettings()
504 {
505     SettingsDialog dialog;
506     dialog.setFilterModel(m_proxyModel);
507
508     dialog.exec();
509 }
510
511 void MainWindow::openHelp(const QUrl &url)
512 {
513     QDesktopServices::openUrl(url);
514 }
515
516 void MainWindow::showSurfacesMenu(const QPoint &pos)
517 {
518     QTreeWidget *tree = m_ui.surfacesTreeWidget;
519     QTreeWidgetItem *item = tree->itemAt(pos);
520     if (!item)
521         return;
522
523     QMenu menu(tr("Surfaces"), this);
524     //add needed actions
525     QAction *act = menu.addAction(tr("View Image"));
526     act->setStatusTip(tr("View the currently selected surface"));
527     connect(act, SIGNAL(triggered()),
528             SLOT(showSelectedSurface()));
529
530     menu.exec(tree->viewport()->mapToGlobal(pos));
531 }
532
533 void MainWindow::showSelectedSurface()
534 {
535     QTreeWidgetItem *item =
536         m_ui.surfacesTreeWidget->currentItem();
537
538     if (!item)
539         return;
540
541     QVariant var = item->data(0, Qt::UserRole);
542     m_imageViewer->setImage(var.value<QImage>());
543     m_imageViewer->show();
544     m_imageViewer->raise();
545     m_imageViewer->activateWindow();
546 }
547
548 void MainWindow::initObjects()
549 {
550     m_ui.stateTreeWidget->sortByColumn(0, Qt::AscendingOrder);
551
552     m_sourcesWidget = new ShadersSourceWidget(m_ui.shadersTab);
553     QVBoxLayout *layout = new QVBoxLayout;
554     layout->addWidget(m_sourcesWidget);
555     m_ui.shadersTab->setLayout(layout);
556
557     m_trace = new ApiTrace();
558     m_retracer = new Retracer(this);
559
560     m_vdataInterpreter = new VertexDataInterpreter(this);
561     m_vdataInterpreter->setListWidget(m_ui.vertexDataListWidget);
562     m_vdataInterpreter->setStride(
563         m_ui.vertexStrideSB->value());
564     m_vdataInterpreter->setComponents(
565         m_ui.vertexComponentsSB->value());
566     m_vdataInterpreter->setStartingOffset(
567         m_ui.startingOffsetSB->value());
568     m_vdataInterpreter->setTypeFromString(
569         m_ui.vertexTypeCB->currentText());
570
571     m_imageViewer = new ImageViewer(this);
572
573     m_model = new ApiTraceModel();
574     m_model->setApiTrace(m_trace);
575     m_proxyModel = new ApiTraceFilter();
576     m_proxyModel->setSourceModel(m_model);
577     m_ui.callView->setModel(m_proxyModel);
578     m_ui.callView->setItemDelegate(new ApiCallDelegate);
579     m_ui.callView->resizeColumnToContents(0);
580     m_ui.callView->header()->swapSections(0, 1);
581     m_ui.callView->setColumnWidth(1, 42);
582
583     m_progressBar = new QProgressBar();
584     m_progressBar->setRange(0, 0);
585     statusBar()->addPermanentWidget(m_progressBar);
586     m_progressBar->hide();
587
588     m_ui.detailsDock->hide();
589     m_ui.vertexDataDock->hide();
590     m_ui.stateDock->hide();
591     setDockOptions(dockOptions() | QMainWindow::ForceTabbedDocks);
592
593     tabifyDockWidget(m_ui.stateDock, m_ui.vertexDataDock);
594
595     m_ui.surfacesTreeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
596
597     m_ui.detailsWebView->page()->setLinkDelegationPolicy(
598         QWebPage::DelegateExternalLinks);
599
600     m_jumpWidget = new JumpWidget(this);
601     m_ui.centralLayout->addWidget(m_jumpWidget);
602     m_jumpWidget->hide();
603
604     m_searchWidget = new SearchWidget(this);
605     m_ui.centralLayout->addWidget(m_searchWidget);
606     m_searchWidget->hide();
607
608     m_traceProcess = new TraceProcess(this);
609
610     new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_G),
611                   this, SLOT(slotGoTo()));
612     new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_F),
613                   this, SLOT(slotSearch()));
614 }
615
616 void MainWindow::initConnections()
617 {
618     connect(m_trace, SIGNAL(startedLoadingTrace()),
619             this, SLOT(startedLoadingTrace()));
620     connect(m_trace, SIGNAL(finishedLoadingTrace()),
621             this, SLOT(finishedLoadingTrace()));
622
623     connect(m_retracer, SIGNAL(finished(const QString&)),
624             this, SLOT(replayFinished(const QString&)));
625     connect(m_retracer, SIGNAL(error(const QString&)),
626             this, SLOT(replayError(const QString&)));
627     connect(m_retracer, SIGNAL(foundState(const ApiTraceState&)),
628             this, SLOT(replayStateFound(const ApiTraceState&)));
629
630     connect(m_ui.vertexInterpretButton, SIGNAL(clicked()),
631             m_vdataInterpreter, SLOT(interpretData()));
632     connect(m_ui.vertexTypeCB, SIGNAL(currentIndexChanged(const QString&)),
633             m_vdataInterpreter, SLOT(setTypeFromString(const QString&)));
634     connect(m_ui.vertexStrideSB, SIGNAL(valueChanged(int)),
635             m_vdataInterpreter, SLOT(setStride(int)));
636     connect(m_ui.vertexComponentsSB, SIGNAL(valueChanged(int)),
637             m_vdataInterpreter, SLOT(setComponents(int)));
638     connect(m_ui.startingOffsetSB, SIGNAL(valueChanged(int)),
639             m_vdataInterpreter, SLOT(setStartingOffset(int)));
640
641
642     connect(m_ui.actionNew, SIGNAL(triggered()),
643             this, SLOT(createTrace()));
644     connect(m_ui.actionOpen, SIGNAL(triggered()),
645             this, SLOT(openTrace()));
646     connect(m_ui.actionQuit, SIGNAL(triggered()),
647             this, SLOT(close()));
648
649     connect(m_ui.actionReplay, SIGNAL(triggered()),
650             this, SLOT(replayStart()));
651     connect(m_ui.actionStop, SIGNAL(triggered()),
652             this, SLOT(replayStop()));
653     connect(m_ui.actionLookupState, SIGNAL(triggered()),
654             this, SLOT(lookupState()));
655     connect(m_ui.actionOptions, SIGNAL(triggered()),
656             this, SLOT(showSettings()));
657
658     connect(m_ui.callView, SIGNAL(activated(const QModelIndex &)),
659             this, SLOT(callItemSelected(const QModelIndex &)));
660
661     connect(m_ui.surfacesTreeWidget,
662             SIGNAL(customContextMenuRequested(const QPoint &)),
663             SLOT(showSurfacesMenu(const QPoint &)));
664     connect(m_ui.surfacesTreeWidget,
665             SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)),
666             SLOT(showSelectedSurface()));
667
668     connect(m_ui.detailsWebView, SIGNAL(linkClicked(const QUrl&)),
669             this, SLOT(openHelp(const QUrl&)));
670
671     connect(m_ui.nonDefaultsCB, SIGNAL(toggled(bool)),
672             this, SLOT(fillState(bool)));
673
674     connect(m_jumpWidget, SIGNAL(jumpTo(int)),
675             SLOT(slotJumpTo(int)));
676
677     connect(m_searchWidget,
678             SIGNAL(searchNext(const QString&, Qt::CaseSensitivity)),
679             SLOT(slotSearchNext(const QString&, Qt::CaseSensitivity)));
680     connect(m_searchWidget,
681             SIGNAL(searchPrev(const QString&, Qt::CaseSensitivity)),
682             SLOT(slotSearchPrev(const QString&, Qt::CaseSensitivity)));
683
684     connect(m_traceProcess, SIGNAL(tracedFile(const QString&)),
685             SLOT(createdTrace(const QString&)));
686     connect(m_traceProcess, SIGNAL(error(const QString&)),
687             SLOT(traceError(const QString&)));
688 }
689
690 void MainWindow::replayStateFound(const ApiTraceState &state)
691 {
692     m_stateEvent->setState(state);
693     m_model->stateSetOnEvent(m_stateEvent);
694     if (m_selectedEvent == m_stateEvent) {
695         fillStateForFrame();
696     } else {
697         m_ui.stateDock->hide();
698     }
699 }
700
701 void MainWindow::slotGoTo()
702 {
703     m_searchWidget->hide();
704     m_jumpWidget->show();
705 }
706
707 void MainWindow::slotJumpTo(int callNum)
708 {
709     QModelIndex index = m_proxyModel->callIndex(callNum);
710     if (index.isValid()) {
711         m_ui.callView->setCurrentIndex(index);
712     }
713 }
714
715 void MainWindow::createdTrace(const QString &path)
716 {
717     qDebug()<<"Done tracing "<<path;
718     newTraceFile(path);
719 }
720
721 void MainWindow::traceError(const QString &msg)
722 {
723     QMessageBox::warning(
724             this,
725             tr("Tracing Error"),
726             msg);
727 }
728
729 void MainWindow::slotSearch()
730 {
731     m_jumpWidget->hide();
732     m_searchWidget->show();
733 }
734
735 void MainWindow::slotSearchNext(const QString &str, Qt::CaseSensitivity sensitivity)
736 {
737     QModelIndex index = m_ui.callView->currentIndex();
738     ApiTraceEvent *event = 0;
739
740
741     if (!index.isValid()) {
742         index = m_proxyModel->index(0, 0, QModelIndex());
743         if (!index.isValid()) {
744             qDebug()<<"no currently valid index";
745             m_searchWidget->setFound(false);
746             return;
747         }
748     }
749
750     event = index.data(ApiTraceModel::EventRole).value<ApiTraceEvent*>();
751     ApiTraceCall *call = 0;
752
753     if (event->type() == ApiTraceCall::Call)
754         call = static_cast<ApiTraceCall*>(event);
755     else {
756         Q_ASSERT(event->type() == ApiTraceCall::Frame);
757         ApiTraceFrame *frame = static_cast<ApiTraceFrame*>(event);
758         call = frame->calls.value(0);
759     }
760
761     if (!call) {
762         m_searchWidget->setFound(false);
763         return;
764     }
765     const QList<ApiTraceCall*> &calls = m_trace->calls();
766     int callNum = calls.indexOf(call);
767
768     for (int i = callNum + 1; i < calls.count(); ++i) {
769         ApiTraceCall *testCall = calls[i];
770         QString txt = testCall->filterText();
771         if (txt.contains(str, sensitivity)) {
772             QModelIndex index = m_proxyModel->indexForCall(testCall);
773             /* if it's not valid it means that the proxy model has already
774              * filtered it out */
775             if (index.isValid()) {
776                 m_ui.callView->setCurrentIndex(index);
777                 m_searchWidget->setFound(true);
778                 return;
779             }
780         }
781     }
782     m_searchWidget->setFound(false);
783 }
784
785 void MainWindow::slotSearchPrev(const QString &str, Qt::CaseSensitivity sensitivity)
786 {
787     QModelIndex index = m_ui.callView->currentIndex();
788     ApiTraceEvent *event = 0;
789
790
791     if (!index.isValid()) {
792         index = m_proxyModel->index(0, 0, QModelIndex());
793         if (!index.isValid()) {
794             qDebug()<<"no currently valid index";
795             m_searchWidget->setFound(false);
796             return;
797         }
798     }
799
800     event = index.data(ApiTraceModel::EventRole).value<ApiTraceEvent*>();
801     ApiTraceCall *call = 0;
802
803     if (event->type() == ApiTraceCall::Call)
804         call = static_cast<ApiTraceCall*>(event);
805     else {
806         Q_ASSERT(event->type() == ApiTraceCall::Frame);
807         ApiTraceFrame *frame = static_cast<ApiTraceFrame*>(event);
808         call = frame->calls.value(0);
809     }
810
811     if (!call) {
812         m_searchWidget->setFound(false);
813         return;
814     }
815     const QList<ApiTraceCall*> &calls = m_trace->calls();
816     int callNum = calls.indexOf(call);
817
818     for (int i = callNum - 1; i >= 0; --i) {
819         ApiTraceCall *testCall = calls[i];
820         QString txt = testCall->filterText();
821         if (txt.contains(str, sensitivity)) {
822             QModelIndex index = m_proxyModel->indexForCall(testCall);
823             /* if it's not valid it means that the proxy model has already
824              * filtered it out */
825             if (index.isValid()) {
826                 m_ui.callView->setCurrentIndex(index);
827                 m_searchWidget->setFound(true);
828                 return;
829             }
830         }
831     }
832     m_searchWidget->setFound(false);
833 }
834
835 void MainWindow::fillState(bool nonDefaults)
836 {
837     if (nonDefaults) {
838         ApiTraceState defaultState = m_trace->defaultState();
839         if (defaultState.isEmpty()) {
840             m_ui.nonDefaultsCB->blockSignals(true);
841             m_ui.nonDefaultsCB->setChecked(false);
842             m_ui.nonDefaultsCB->blockSignals(false);
843             int ret = QMessageBox::question(
844                 this, tr("Empty Default State"),
845                 tr("The applcation needs to figure out the "
846                    "default state for the current trace. "
847                    "This only has to be done once and "
848                    "afterwards you will be able to enable "
849                    "displaying of non default state for all calls."
850                    "\nDo you want to lookup the default state now?"),
851                 QMessageBox::Yes | QMessageBox::No);
852             if (ret != QMessageBox::Yes)
853                 return;
854             ApiTraceFrame *firstFrame =
855                 m_trace->frameAt(0);
856             ApiTraceEvent *oldSelected = m_selectedEvent;
857             if (!firstFrame)
858                 return;
859             m_selectedEvent = firstFrame;
860             lookupState();
861             m_selectedEvent = oldSelected;
862         }
863     }
864     fillStateForFrame();
865 }
866
867 #include "mainwindow.moc"