diff mbox series

[06/11] kernel-shark-qt: Update search iterator when marker is changed

Message ID 20181121151356.16901-8-ykaradzhov@vmware.com (mailing list archive)
State Accepted
Headers show
Series Small modifications and bug fixes toward KS 1.0 | expand

Commit Message

Yordan Karadzhov Nov. 21, 2018, 3:14 p.m. UTC
When the Dual marker changes it active state (between A and B) or
its selected row, the iterator over the list of search matching entries
has to be updated such that it points to the selected entry. If the
selected entry do not belong to the matching list, the iterator will
point to the first matching entry after the selected one.

Signed-off-by: Yordan Karadzhov <ykaradzhov@vmware.com>
---
 kernel-shark-qt/src/KsTraceViewer.cpp | 114 +++++++++++++++++---------
 kernel-shark-qt/src/KsTraceViewer.hpp |   4 +
 2 files changed, 80 insertions(+), 38 deletions(-)
diff mbox series

Patch

diff --git a/kernel-shark-qt/src/KsTraceViewer.cpp b/kernel-shark-qt/src/KsTraceViewer.cpp
index 3df4a5d..1f96234 100644
--- a/kernel-shark-qt/src/KsTraceViewer.cpp
+++ b/kernel-shark-qt/src/KsTraceViewer.cpp
@@ -350,10 +350,21 @@  void KsTraceViewer::_next()
 	}
 
 	if (!_matchList.empty()) { // Items have been found.
-		++_it; // Move the iterator.
-		if (_it == _matchList.end() ) {
-			// This is the last item of the list. Go back to the beginning.
-			_it = _matchList.begin();
+		int row = _getSelectedDataRow();
+		/*
+		 * The iterator can only be at the selected row or if the
+		 * selected row is not a match at the first matching row after
+		 * the selected one.
+		 */
+		if (*_it == row) {
+			++_it; // Move the iterator.
+			if (_it == _matchList.end() ) {
+				/*
+				 * This is the last item of the list.
+				 * Go back to the beginning.
+				 */
+				_it = _matchList.begin();
+			}
 		}
 
 		// Select the row of the item.
@@ -407,14 +418,17 @@  void KsTraceViewer::_searchStop()
 
 void KsTraceViewer::_clicked(const QModelIndex& i)
 {
-	if (_graphFollows) {
-		/*
-		 * Use the index of the proxy model to retrieve the value
-		 * of the row number in the base model.
-		 */
-		size_t row = _proxyModel.mapRowFromSource(i.row());
+	/*
+	 * Use the index of the proxy model to retrieve the value
+	 * of the row number in the base model.
+	 */
+	size_t row = _proxyModel.mapRowFromSource(i.row());
+
+	_setSearchIterator(row);
+	_updateSearchCount();
+
+	if (_graphFollows)
 		emit select(row); // Send a signal to the Graph widget.
-	}
 }
 
 /** Make a given row of the table visible. */
@@ -454,6 +468,8 @@  void KsTraceViewer::deselect()
 /** Switch the Dual marker. */
 void KsTraceViewer::markSwitch()
 {
+	int row;
+
 	/* The state of the Dual marker has changed. Get the new active marker. */
 	DualMarkerState state = _mState->getState();
 
@@ -495,6 +511,12 @@  void KsTraceViewer::markSwitch()
 	} else {
 		_view.clearSelection();
 	}
+
+	row = _getSelectedDataRow();
+	if (row >= 0) {
+		_setSearchIterator(row);
+		_updateSearchCount();
+	}
 }
 
 /**
@@ -529,12 +551,9 @@  void KsTraceViewer::resizeEvent(QResizeEvent* event)
 void KsTraceViewer::keyReleaseEvent(QKeyEvent *event)
 {
 	if (event->key() == Qt::Key_Up || event->key() == Qt::Key_Down) {
-		QItemSelectionModel *sm = _view.selectionModel();
-		if (sm->hasSelection()) {
-			/* Only one row at the time can be selected. */
-			int row = sm->selectedRows()[0].row();
+		int row = _getSelectedDataRow();
+		if (row >= 0)
 			emit select(row); // Send a signal to the Graph widget.
-		}
 
 		return;
 	}
@@ -564,7 +583,7 @@  size_t KsTraceViewer::_searchItems(int column,
 				   const QString &searchText,
 				   condition_func cond)
 {
-	int count;
+	int count, dataRow;
 
 	_searchProgBar.show();
 	_pbAction->setVisible(true);
@@ -588,28 +607,10 @@  size_t KsTraceViewer::_searchItems(int column,
 	if (count == 0) // No items have been found. Do nothing.
 		return 0;
 
-	QItemSelectionModel *sm = _view.selectionModel();
-	if (sm->hasSelection()) {
-		/* Only one row at the time can be selected. */
-		int row = sm->selectedRows()[0].row();
-
+	dataRow = _getSelectedDataRow();
+	if (dataRow >= 0) {
 		_view.clearSelection();
-		_it = _matchList.begin();
-		/*
-		 * Move the iterator to the first element of the match list
-		 * after the selected one.
-		 */
-		while (*_it <= row) {
-			++_it;  // Move the iterator.
-			if (_it == _matchList.end()) {
-				/*
-				 * This is the last item of the list. Go back
-				 * to the beginning.
-				 */
-				_it = _matchList.begin();
-				break;
-			}
-		}
+		_setSearchIterator(dataRow);
 	} else {
 		/* Move the iterator to the beginning of the match list. */
 		_view.clearSelection();
@@ -621,6 +622,29 @@  size_t KsTraceViewer::_searchItems(int column,
 	return count;
 }
 
+void KsTraceViewer::_setSearchIterator(int row)
+{
+	if (_matchList.isEmpty())
+		return;
+
+	/*
+	 * Move the iterator to the first element of the match list
+	 * after the selected one.
+	 */
+	_it = _matchList.begin();
+	while (*_it < row) {
+		++_it;  // Move the iterator.
+		if (_it == _matchList.end()) {
+			/*
+			 * This is the last item of the list. Go back
+			 * to the beginning.
+			 */
+			_it = _matchList.begin();
+			break;
+		}
+	}
+}
+
 void KsTraceViewer::_searchItemsMapReduce(int column,
 					  const QString &searchText,
 					  condition_func cond)
@@ -668,3 +692,17 @@  void KsTraceViewer::_searchItemsMapReduce(int column,
 	for (auto &m: maps)
 		lamSearchReduce(_matchList, m.get());
 }
+
+int KsTraceViewer::_getSelectedDataRow()
+{
+	QItemSelectionModel *sm = _view.selectionModel();
+	int dataRow = -1;
+
+	if (sm->hasSelection()) {
+		/* Only one row at the time can be selected. */
+		QModelIndex  i = sm->selectedRows()[0];
+		dataRow = _proxyModel.mapRowFromSource(i.row());
+	}
+
+	return dataRow;
+}
diff --git a/kernel-shark-qt/src/KsTraceViewer.hpp b/kernel-shark-qt/src/KsTraceViewer.hpp
index 19371de..50c9115 100644
--- a/kernel-shark-qt/src/KsTraceViewer.hpp
+++ b/kernel-shark-qt/src/KsTraceViewer.hpp
@@ -140,6 +140,10 @@  private:
 
 	void _onCustomContextMenu(const QPoint &);
 
+	void _setSearchIterator(int row);
+
+	int _getSelectedDataRow();
+
 private slots:
 
 	void _searchEdit(int);