From f54c4fc1b52ece07d54b86caa09a4616b98605e7 Mon Sep 17 00:00:00 2001 From: Zack Rusin Date: Tue, 12 Apr 2011 23:39:13 -0400 Subject: [PATCH] Highly optimize searching and fix a crash. --- gui/apitracefilter.cpp | 7 +++++++ gui/apitracefilter.h | 3 +++ gui/apitracemodel.cpp | 7 +++++-- gui/apitracemodel.h | 1 + gui/mainwindow.cpp | 34 ++++++++++++++++++++++++++++------ gui/searchwidget.cpp | 11 ++++++++--- gui/searchwidget.h | 1 + 7 files changed, 53 insertions(+), 11 deletions(-) diff --git a/gui/apitracefilter.cpp b/gui/apitracefilter.cpp index 7580bba..50b854e 100644 --- a/gui/apitracefilter.cpp +++ b/gui/apitracefilter.cpp @@ -96,4 +96,11 @@ QModelIndex ApiTraceFilter::callIndex(int callIdx) const return mapFromSource(index); } +QModelIndex ApiTraceFilter::indexForCall(ApiTraceCall *call) const +{ + ApiTraceModel *model = static_cast(sourceModel()); + QModelIndex index = model->indexForCall(call); + return mapFromSource(index); +} + #include "apitracefilter.moc" diff --git a/gui/apitracefilter.h b/gui/apitracefilter.h index 2bd8557..7ceabe6 100644 --- a/gui/apitracefilter.h +++ b/gui/apitracefilter.h @@ -3,6 +3,8 @@ #include +class ApiTraceCall; + class ApiTraceFilter : public QSortFilterProxyModel { Q_OBJECT @@ -24,6 +26,7 @@ public: void setFilterString(const QString &text); QModelIndex callIndex(int callNum) const; + QModelIndex indexForCall(ApiTraceCall *call) const; protected: bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const; diff --git a/gui/apitracemodel.cpp b/gui/apitracemodel.cpp index 2b4b6bb..d3f3e6a 100644 --- a/gui/apitracemodel.cpp +++ b/gui/apitracemodel.cpp @@ -256,9 +256,12 @@ void ApiTraceModel::stateSetOnEvent(ApiTraceEvent *event) QModelIndex ApiTraceModel::callIndex(int callNum) const { ApiTraceCall *call = m_trace->callWithIndex(callNum); + return indexForCall(call); +} +QModelIndex ApiTraceModel::indexForCall(ApiTraceCall *call) const +{ if (!call) { - qDebug()<<"couldn't find call at "<calls.indexOf(call); if (row < 0) { - qDebug() << "Couldn't find call num "<index<<" inside parent!"; return QModelIndex(); } return createIndex(row, 0, call); diff --git a/gui/apitracemodel.h b/gui/apitracemodel.h index 345cd13..7a0394b 100644 --- a/gui/apitracemodel.h +++ b/gui/apitracemodel.h @@ -26,6 +26,7 @@ public: void stateSetOnEvent(ApiTraceEvent *event); QModelIndex callIndex(int callNum) const; + QModelIndex indexForCall(ApiTraceCall *call) const; public: /* QAbstractItemModel { */ diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 35c287a..ff51d95 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -650,8 +650,19 @@ void MainWindow::slotSearch() void MainWindow::slotSearchNext(const QString &str, Qt::CaseSensitivity sensitivity) { QModelIndex index = m_ui.callView->currentIndex(); - ApiTraceEvent *event = - index.data(ApiTraceModel::EventRole).value(); + ApiTraceEvent *event = 0; + + + if (!index.isValid()) { + index = m_proxyModel->index(0, 0, QModelIndex()); + if (!index.isValid()) { + qDebug()<<"no currently valid index"; + m_searchWidget->setFound(false); + return; + } + } + + event = index.data(ApiTraceModel::EventRole).value(); ApiTraceCall *call = 0; if (event->type() == ApiTraceCall::Call) @@ -673,7 +684,7 @@ void MainWindow::slotSearchNext(const QString &str, Qt::CaseSensitivity sensitiv ApiTraceCall *testCall = calls[i]; QString txt = testCall->filterText(); if (txt.contains(str, sensitivity)) { - QModelIndex index = m_proxyModel->callIndex(testCall->index); + 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()) { @@ -689,8 +700,19 @@ void MainWindow::slotSearchNext(const QString &str, Qt::CaseSensitivity sensitiv void MainWindow::slotSearchPrev(const QString &str, Qt::CaseSensitivity sensitivity) { QModelIndex index = m_ui.callView->currentIndex(); - ApiTraceEvent *event = - index.data(ApiTraceModel::EventRole).value(); + ApiTraceEvent *event = 0; + + + if (!index.isValid()) { + index = m_proxyModel->index(0, 0, QModelIndex()); + if (!index.isValid()) { + qDebug()<<"no currently valid index"; + m_searchWidget->setFound(false); + return; + } + } + + event = index.data(ApiTraceModel::EventRole).value(); ApiTraceCall *call = 0; if (event->type() == ApiTraceCall::Call) @@ -712,7 +734,7 @@ void MainWindow::slotSearchPrev(const QString &str, Qt::CaseSensitivity sensitiv ApiTraceCall *testCall = calls[i]; QString txt = testCall->filterText(); if (txt.contains(str, sensitivity)) { - QModelIndex index = m_proxyModel->callIndex(testCall->index); + 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()) { diff --git a/gui/searchwidget.cpp b/gui/searchwidget.cpp index 6f9bc94..45762b3 100644 --- a/gui/searchwidget.cpp +++ b/gui/searchwidget.cpp @@ -44,9 +44,6 @@ void SearchWidget::slotCancel() void SearchWidget::showEvent(QShowEvent *event) { - m_ui.lineEdit->selectAll(); - m_ui.lineEdit->setFocus(Qt::ShortcutFocusReason); - m_ui.lineEdit->setPalette(m_origPalette); return QWidget::showEvent(event); } @@ -77,4 +74,12 @@ void SearchWidget::setFound(bool found) m_ui.notFoundLabel->setVisible(!found); } +void SearchWidget::show() +{ + QWidget::show(); + m_ui.lineEdit->selectAll(); + m_ui.lineEdit->setFocus(Qt::ShortcutFocusReason); + m_ui.lineEdit->setPalette(m_origPalette); +} + #include "searchwidget.moc" diff --git a/gui/searchwidget.h b/gui/searchwidget.h index d105303..f59b4a0 100644 --- a/gui/searchwidget.h +++ b/gui/searchwidget.h @@ -14,6 +14,7 @@ public: SearchWidget(QWidget *parent=0); void setFound(bool f); + void show(); signals: void searchNext(const QString &str, Qt::CaseSensitivity cs = Qt::CaseInsensitive); void searchPrev(const QString &str, Qt::CaseSensitivity cs = Qt::CaseInsensitive); -- 2.7.4