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