Skip to content

Commit 696666a

Browse files
Fix Search (#78)
* fix previous * Proper highlighting * Calling selection with focus
1 parent 1e67a6b commit 696666a

3 files changed

Lines changed: 23 additions & 12 deletions

File tree

src/petab_gui/controllers/table_controllers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ def cleanse_highlighted_cells(self):
369369
self.model.dataChanged.emit(top_left, bottom_right,
370370
[Qt.ForegroundRole])
371371

372-
def focus_match(self, match):
372+
def focus_match(self, match, with_focus: bool = False):
373373
"""Focus and select the given match in the table."""
374374
if match is None:
375375
self.view.table_view.clearSelection()
@@ -382,11 +382,12 @@ def focus_match(self, match):
382382
if not proxy_index.isValid():
383383
return
384384

385-
self.view.table_view.setCurrentIndex(proxy_index)
386385
self.view.table_view.setCurrentIndex(proxy_index)
387386
self.view.table_view.scrollTo(
388387
proxy_index, QAbstractItemView.EnsureVisible
389388
)
389+
if with_focus:
390+
self.view.table_view.setFocus()
390391

391392
def replace_text(self, row, col, replace_text, search_text, case_sensitive, regex):
392393
"""Replace the text in the given cell and update highlights."""

src/petab_gui/models/pandas_table_model.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import pandas as pd
22
from PySide6.QtCore import (Qt, QAbstractTableModel, QModelIndex, Signal,
33
QSortFilterProxyModel, QMimeData)
4-
from PySide6.QtGui import QColor, QBrush
5-
4+
from PySide6.QtGui import QColor, QBrush, QPalette
5+
from PySide6.QtWidgets import QApplication
66
from ..C import COLUMNS
77
from ..utils import validate_value, create_empty_dataframe, is_invalid, \
88
get_selected
@@ -61,7 +61,7 @@ def data(self, index, role=Qt.DisplayRole):
6161
elif role == Qt.ForegroundRole:
6262
# Return yellow text if this cell is a match
6363
if (row, column) in self.highlighted_cells:
64-
return QBrush(QColor(255, 255, 0)) # Yellow color
64+
return QApplication.palette().color(QPalette.HighlightedText)
6565
return QBrush(QColor(0, 0, 0)) # Default black text
6666
return None
6767

@@ -444,6 +444,8 @@ def determine_background_color(self, row, column):
444444
"""
445445
if (row, column) == (self._data_frame.shape[0], 0):
446446
return QColor(144, 238, 144, 150)
447+
if (row, column) in self.highlighted_cells:
448+
return QApplication.palette().color(QPalette.Highlight)
447449
if (row, column) in self._invalid_cells:
448450
return QColor(255, 100, 100, 150)
449451
if row % 2 == 0:
@@ -538,6 +540,11 @@ def data(self, index, role=Qt.DisplayRole):
538540
return str(value)
539541
elif role == Qt.BackgroundRole:
540542
return self.determine_background_color(row, column)
543+
elif role == Qt.ForegroundRole:
544+
# Return yellow text if this cell is a match
545+
if (row, column) in self.highlighted_cells:
546+
return QApplication.palette().color(QPalette.HighlightedText)
547+
return QBrush(QColor(0, 0, 0)) # Default black text
541548
return None
542549

543550
def headerData(self, section, orientation, role=Qt.DisplayRole):

src/petab_gui/utils.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -831,16 +831,19 @@ def find_next(self):
831831
controller.focus_match(None)
832832
self.current_match_ind = (self.current_match_ind + 1) % len(self.matches)
833833
row, col, controller = self.matches[self.current_match_ind]
834-
controller.focus_match((row, col))
834+
controller.focus_match((row, col), with_focus=True)
835835
self.update_result_label()
836836

837837
def find_previous(self):
838838
"""Move to the previous match."""
839839
if not self.matches:
840840
return
841-
842-
self.current_match_ind = (self.current_match_ind - 1) % len(self.matches)
843-
self.controller.observable_controller.focus_match(self.matches[self.current_match_ind])
841+
__, _, controller = self.matches[self.current_match_ind]
842+
controller.focus_match(None)
843+
self.current_match_ind = (self.current_match_ind - 1) % len(
844+
self.matches)
845+
row, col, controller = self.matches[self.current_match_ind]
846+
controller.focus_match((row, col), with_focus=True)
844847
self.update_result_label()
845848

846849
def update_result_label(self):
@@ -874,7 +877,7 @@ def replace_current_match(self):
874877
self.matches.pop(self.current_match_ind)
875878
self.update_result_label()
876879
match = self.matches[self.current_match_ind] if self.matches else None
877-
self.focus_match(match)
880+
self.focus_match(match, with_focus=True)
878881

879882
def replace_all(self):
880883
"""Replace all matches with the given text."""
@@ -901,12 +904,12 @@ def replace_all(self):
901904
controller.cleanse_highlighted_cells()
902905
self.run_find()
903906

904-
def focus_match(self, match):
907+
def focus_match(self, match, with_focus: bool = False):
905908
"""Focus the match in the correct table."""
906909
if not match:
907910
return
908911
row, col, controller = match
909-
controller.focus_match((row, col))
912+
controller.focus_match((row, col), with_focus)
910913

911914
def show_filter_menu(self):
912915
"""Show the filter selection dropdown below the filter button."""

0 commit comments

Comments
 (0)