]> git.cworth.org Git - apitrace/blobdiff - gui/mainwindow.cpp
Plugging some memory leaks
[apitrace] / gui / mainwindow.cpp
index cd63962d068f704e57984efa77002ccfc0024348..1b4de12b6f35c2c7094334b266e73af1d8e22ea4 100644 (file)
 #include "vertexdatainterpreter.h"
 
 #include <QAction>
+#include <QApplication>
 #include <QDebug>
 #include <QDesktopServices>
+#include <QDesktopWidget>
 #include <QDir>
 #include <QFileDialog>
 #include <QLineEdit>
 #include <QMessageBox>
 #include <QProgressBar>
-#include <QShortcut>
 #include <QToolBar>
 #include <QUrl>
 #include <QVBoxLayout>
@@ -154,14 +155,14 @@ void MainWindow::replayStart()
 
     dlgUi.doubleBufferingCB->setChecked(
         m_retracer->isDoubleBuffered());
-    dlgUi.benchmarkCB->setChecked(
-        m_retracer->isBenchmarking());
+    dlgUi.errorCheckCB->setChecked(
+        !m_retracer->isBenchmarking());
 
     if (dlg.exec() == QDialog::Accepted) {
         m_retracer->setDoubleBuffered(
             dlgUi.doubleBufferingCB->isChecked());
         m_retracer->setBenchmarking(
-            dlgUi.benchmarkCB->isChecked());
+            !dlgUi.errorCheckCB->isChecked());
         replayTrace(false);
     }
 }
@@ -202,6 +203,11 @@ void MainWindow::replayFinished(const QString &output)
         statusBar()->showMessage(output);
     }
     m_stateEvent = 0;
+    m_ui.actionShowErrorsDock->setEnabled(m_trace->hasErrors());
+    m_ui.errorsDock->setVisible(m_trace->hasErrors());
+    if (!m_trace->hasErrors())
+        m_ui.errorsTreeWidget->clear();
+
     statusBar()->showMessage(
         tr("Replaying finished!"), 2000);
 }
@@ -300,6 +306,11 @@ void MainWindow::lookupState()
 
 MainWindow::~MainWindow()
 {
+    delete m_trace;
+    m_trace = 0;
+
+    delete m_proxyModel;
+    delete m_model;
 }
 
 static void
@@ -556,10 +567,26 @@ void MainWindow::showSelectedSurface()
         return;
 
     QVariant var = item->data(0, Qt::UserRole);
-    m_imageViewer->setImage(var.value<QImage>());
-    m_imageViewer->show();
-    m_imageViewer->raise();
-    m_imageViewer->activateWindow();
+    QImage img = var.value<QImage>();
+    ImageViewer *viewer = new ImageViewer(this);
+
+    QString title;
+    if (currentCall()) {
+        title = tr("QApiTrace - Surface at %1 (%2)")
+                .arg(currentCall()->name())
+                .arg(currentCall()->index());
+    } else {
+        title = tr("QApiTrace - Surface Viewer");
+    }
+    viewer->setWindowTitle(title);
+    viewer->setAttribute(Qt::WA_DeleteOnClose, true);
+    viewer->setImage(img);
+    QRect screenRect = QApplication::desktop()->availableGeometry();
+    viewer->resize(qMin(int(0.75 * screenRect.width()), img.width()) + 40,
+                   qMin(int(0.75 * screenRect.height()), img.height()) + 40);
+    viewer->show();
+    viewer->raise();
+    viewer->activateWindow();
 }
 
 void MainWindow::initObjects()
@@ -585,8 +612,6 @@ void MainWindow::initObjects()
     m_vdataInterpreter->setTypeFromString(
         m_ui.vertexTypeCB->currentText());
 
-    m_imageViewer = new ImageViewer(this);
-
     m_model = new ApiTraceModel();
     m_model->setApiTrace(m_trace);
     m_proxyModel = new ApiTraceFilter();
@@ -606,11 +631,13 @@ void MainWindow::initObjects()
     m_argsEditor = new ArgumentsEditor(this);
 
     m_ui.detailsDock->hide();
+    m_ui.errorsDock->hide();
     m_ui.vertexDataDock->hide();
     m_ui.stateDock->hide();
     setDockOptions(dockOptions() | QMainWindow::ForceTabbedDocks);
 
     tabifyDockWidget(m_ui.stateDock, m_ui.vertexDataDock);
+    tabifyDockWidget(m_ui.detailsDock, m_ui.errorsDock);
 
     m_ui.surfacesTreeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
 
@@ -626,11 +653,6 @@ void MainWindow::initObjects()
     m_searchWidget->hide();
 
     m_traceProcess = new TraceProcess(this);
-
-    new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_G),
-                  this, SLOT(slotGoTo()));
-    new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_F),
-                  this, SLOT(slotSearch()));
 }
 
 void MainWindow::initConnections()
@@ -643,6 +665,8 @@ void MainWindow::initConnections()
             this, SLOT(slotStartedSaving()));
     connect(m_trace, SIGNAL(saved()),
             this, SLOT(slotSaved()));
+    connect(m_trace, SIGNAL(changed(ApiTraceCall*)),
+            this, SLOT(slotTraceChanged(ApiTraceCall*)));
 
     connect(m_retracer, SIGNAL(finished(const QString&)),
             this, SLOT(replayFinished(const QString&)));
@@ -650,6 +674,8 @@ void MainWindow::initConnections()
             this, SLOT(replayError(const QString&)));
     connect(m_retracer, SIGNAL(foundState(const ApiTraceState&)),
             this, SLOT(replayStateFound(const ApiTraceState&)));
+    connect(m_retracer, SIGNAL(retraceErrors(const QList<RetraceError>&)),
+            this, SLOT(slotRetraceErrors(const QList<RetraceError>&)));
 
     connect(m_ui.vertexInterpretButton, SIGNAL(clicked()),
             m_vdataInterpreter, SLOT(interpretData()));
@@ -670,6 +696,15 @@ void MainWindow::initConnections()
     connect(m_ui.actionQuit, SIGNAL(triggered()),
             this, SLOT(close()));
 
+    connect(m_ui.actionFind, SIGNAL(triggered()),
+            this, SLOT(slotSearch()));
+    connect(m_ui.actionGo, SIGNAL(triggered()),
+            this, SLOT(slotGoTo()));
+    connect(m_ui.actionGoFrameStart, SIGNAL(triggered()),
+            this, SLOT(slotGoFrameStart()));
+    connect(m_ui.actionGoFrameEnd, SIGNAL(triggered()),
+            this, SLOT(slotGoFrameEnd()));
+
     connect(m_ui.actionReplay, SIGNAL(triggered()),
             this, SLOT(replayStart()));
     connect(m_ui.actionStop, SIGNAL(triggered()),
@@ -711,6 +746,13 @@ void MainWindow::initConnections()
             SLOT(createdTrace(const QString&)));
     connect(m_traceProcess, SIGNAL(error(const QString&)),
             SLOT(traceError(const QString&)));
+
+    connect(m_ui.errorsDock, SIGNAL(visibilityChanged(bool)),
+            m_ui.actionShowErrorsDock, SLOT(setChecked(bool)));
+    connect(m_ui.actionShowErrorsDock, SIGNAL(triggered(bool)),
+            m_ui.errorsDock, SLOT(setVisible(bool)));
+    connect(m_ui.errorsTreeWidget, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)),
+            this, SLOT(slotErrorSelected(QTreeWidgetItem*)));
 }
 
 void MainWindow::replayStateFound(const ApiTraceState &state)
@@ -758,7 +800,8 @@ void MainWindow::slotSearch()
     m_searchWidget->show();
 }
 
-void MainWindow::slotSearchNext(const QString &str, Qt::CaseSensitivity sensitivity)
+void MainWindow::slotSearchNext(const QString &str,
+                                Qt::CaseSensitivity sensitivity)
 {
     QModelIndex index = m_ui.callView->currentIndex();
     ApiTraceEvent *event = 0;
@@ -793,12 +836,12 @@ void MainWindow::slotSearchNext(const QString &str, Qt::CaseSensitivity sensitiv
 
     for (int i = callNum + 1; i < calls.count(); ++i) {
         ApiTraceCall *testCall = calls[i];
-        QString txt = testCall->filterText();
-        if (txt.contains(str, sensitivity)) {
-            QModelIndex index = m_proxyModel->indexForCall(testCall);
-            /* if it's not valid it means that the proxy model has already
-             * filtered it out */
-            if (index.isValid()) {
+        QModelIndex index = m_proxyModel->indexForCall(testCall);
+        /* if it's not valid it means that the proxy model has already
+         * filtered it out */
+        if (index.isValid()) {
+            QString txt = testCall->filterText();
+            if (txt.contains(str, sensitivity)) {
                 m_ui.callView->setCurrentIndex(index);
                 m_searchWidget->setFound(true);
                 return;
@@ -808,7 +851,8 @@ void MainWindow::slotSearchNext(const QString &str, Qt::CaseSensitivity sensitiv
     m_searchWidget->setFound(false);
 }
 
-void MainWindow::slotSearchPrev(const QString &str, Qt::CaseSensitivity sensitivity)
+void MainWindow::slotSearchPrev(const QString &str,
+                                Qt::CaseSensitivity sensitivity)
 {
     QModelIndex index = m_ui.callView->currentIndex();
     ApiTraceEvent *event = 0;
@@ -843,12 +887,12 @@ void MainWindow::slotSearchPrev(const QString &str, Qt::CaseSensitivity sensitiv
 
     for (int i = callNum - 1; i >= 0; --i) {
         ApiTraceCall *testCall = calls[i];
-        QString txt = testCall->filterText();
-        if (txt.contains(str, sensitivity)) {
-            QModelIndex index = m_proxyModel->indexForCall(testCall);
-            /* if it's not valid it means that the proxy model has already
-             * filtered it out */
-            if (index.isValid()) {
+        QModelIndex index = m_proxyModel->indexForCall(testCall);
+        /* if it's not valid it means that the proxy model has already
+         * filtered it out */
+        if (index.isValid()) {
+            QString txt = testCall->filterText();
+            if (txt.contains(str, sensitivity)) {
                 m_ui.callView->setCurrentIndex(index);
                 m_searchWidget->setFound(true);
                 return;
@@ -934,4 +978,112 @@ void MainWindow::slotSaved()
     m_progressBar->hide();
 }
 
+void MainWindow::slotGoFrameStart()
+{
+    ApiTraceFrame *frame = currentFrame();
+    if (!frame || frame->calls.isEmpty()) {
+        return;
+    }
+
+    QList<ApiTraceCall*>::const_iterator itr;
+
+    itr = frame->calls.constBegin();
+    while (itr != frame->calls.constEnd()) {
+        ApiTraceCall *call = *itr;
+        QModelIndex idx = m_proxyModel->indexForCall(call);
+        if (idx.isValid()) {
+            m_ui.callView->setCurrentIndex(idx);
+            break;
+        }
+        ++itr;
+    }
+}
+
+void MainWindow::slotGoFrameEnd()
+{
+    ApiTraceFrame *frame = currentFrame();
+    if (!frame || frame->calls.isEmpty()) {
+        return;
+    }
+    QList<ApiTraceCall*>::const_iterator itr;
+
+    itr = frame->calls.constEnd();
+    do {
+        --itr;
+        ApiTraceCall *call = *itr;
+        QModelIndex idx = m_proxyModel->indexForCall(call);
+        if (idx.isValid()) {
+            m_ui.callView->setCurrentIndex(idx);
+            break;
+        }
+    } while (itr != frame->calls.constBegin());
+}
+
+ApiTraceFrame * MainWindow::currentFrame() const
+{
+    if (m_selectedEvent) {
+        if (m_selectedEvent->type() == ApiTraceEvent::Frame) {
+            return static_cast<ApiTraceFrame*>(m_selectedEvent);
+        } else {
+            Q_ASSERT(m_selectedEvent->type() == ApiTraceEvent::Call);
+            ApiTraceCall *call = static_cast<ApiTraceCall*>(m_selectedEvent);
+            return call->parentFrame();
+        }
+    }
+    return NULL;
+}
+
+void MainWindow::slotTraceChanged(ApiTraceCall *call)
+{
+    Q_ASSERT(call);
+    if (call == m_selectedEvent) {
+        m_ui.detailsWebView->setHtml(call->toHtml());
+    }
+}
+
+void MainWindow::slotRetraceErrors(const QList<RetraceError> &errors)
+{
+    m_ui.errorsTreeWidget->clear();
+
+    foreach(RetraceError error, errors) {
+        ApiTraceCall *call = m_trace->callWithIndex(error.callIndex);
+        if (!call)
+            continue;
+        call->setError(error.message);
+
+        QTreeWidgetItem *item =
+            new QTreeWidgetItem(m_ui.errorsTreeWidget);
+        item->setData(0, Qt::DisplayRole, error.callIndex);
+        item->setData(0, Qt::UserRole, QVariant::fromValue(call));
+        QString type = error.type;
+        type[0] = type[0].toUpper();
+        item->setData(1, Qt::DisplayRole, type);
+        item->setData(2, Qt::DisplayRole, error.message);
+    }
+}
+
+void MainWindow::slotErrorSelected(QTreeWidgetItem *current)
+{
+    if (current) {
+        ApiTraceCall *call =
+            current->data(0, Qt::UserRole).value<ApiTraceCall*>();
+        Q_ASSERT(call);
+        QModelIndex index = m_proxyModel->indexForCall(call);
+        if (index.isValid()) {
+            m_ui.callView->setCurrentIndex(index);
+        } else {
+            statusBar()->showMessage(tr("Call has been filtered out."));
+        }
+    }
+}
+
+ApiTraceCall * MainWindow::currentCall() const
+{
+    if (m_selectedEvent &&
+        m_selectedEvent->type() == ApiTraceEvent::Call) {
+        return static_cast<ApiTraceCall*>(m_selectedEvent);
+    }
+    return NULL;
+}
+
 #include "mainwindow.moc"