diff --git a/CHANGELOG.md b/CHANGELOG.md index c560986..cd52a22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 0.1.10 + + * Improve search results window and highlight in calendar when there are matches. + # 0.1.9 * More styling/toolbar fixes to support toggled-on styles, exclusive styles (e.g it should not be diff --git a/bouquin/main_window.py b/bouquin/main_window.py index 1428384..693456b 100644 --- a/bouquin/main_window.py +++ b/bouquin/main_window.py @@ -17,11 +17,12 @@ from PySide6.QtCore import ( ) from PySide6.QtGui import ( QAction, + QBrush, + QColor, QCursor, QDesktopServices, QFont, QGuiApplication, - QTextCharFormat, QTextListFormat, ) from PySide6.QtWidgets import ( @@ -132,15 +133,15 @@ class MainWindow(QMainWindow): self.search = Search(self.db) self.search.openDateRequested.connect(self._load_selected_date) + self.search.resultDatesChanged.connect(self._on_search_dates_changed) # Lock the calendar to the left panel at the top to stop it stretching # when the main window is resized. left_panel = QWidget() left_layout = QVBoxLayout(left_panel) left_layout.setContentsMargins(8, 8, 8, 8) - left_layout.addWidget(self.calendar, alignment=Qt.AlignTop) - left_layout.addWidget(self.search, alignment=Qt.AlignBottom) - left_layout.addStretch(1) + left_layout.addWidget(self.calendar) + left_layout.addWidget(self.search) left_panel.setFixedWidth(self.calendar.sizeHint().width() + 16) # This is the note-taking editor @@ -313,22 +314,44 @@ class MainWindow(QMainWindow): if self._try_connect(): return True + def _on_search_dates_changed(self, date_strs: list[str]): + dates = set() + for ds in date_strs or []: + qd = QDate.fromString(ds, "yyyy-MM-dd") + if qd.isValid(): + dates.add(qd) + self._apply_search_highlights(dates) + + def _apply_search_highlights(self, dates: set): + yellow = QBrush(QColor("#fff9c4")) + old = getattr(self, "_search_highlighted_dates", set()) + + for d in old - dates: # clear removed + fmt = self.calendar.dateTextFormat(d) + fmt.setBackground(Qt.transparent) + self.calendar.setDateTextFormat(d, fmt) + + for d in dates: # apply new/current + fmt = self.calendar.dateTextFormat(d) + fmt.setBackground(yellow) + self.calendar.setDateTextFormat(d, fmt) + + self._search_highlighted_dates = dates + def _refresh_calendar_marks(self): - """ - Sets a bold marker on the day to indicate that text exists - for that day. - """ - fmt_bold = QTextCharFormat() - fmt_bold.setFontWeight(QFont.Weight.Bold) - # Clear previous marks + """Make days with entries bold, but keep any search highlight backgrounds.""" for d in getattr(self, "_marked_dates", set()): - self.calendar.setDateTextFormat(d, QTextCharFormat()) + fmt = self.calendar.dateTextFormat(d) + fmt.setFontWeight(QFont.Weight.Normal) # remove bold only + self.calendar.setDateTextFormat(d, fmt) self._marked_dates = set() try: for date_iso in self.db.dates_with_content(): qd = QDate.fromString(date_iso, "yyyy-MM-dd") if qd.isValid(): - self.calendar.setDateTextFormat(qd, fmt_bold) + fmt = self.calendar.dateTextFormat(qd) + fmt.setFontWeight(QFont.Weight.Bold) # add bold only + self.calendar.setDateTextFormat(qd, fmt) self._marked_dates.add(qd) except Exception: pass diff --git a/bouquin/search.py b/bouquin/search.py index 8cd2fd5..27c7e17 100644 --- a/bouquin/search.py +++ b/bouquin/search.py @@ -6,10 +6,12 @@ from typing import Iterable, Tuple from PySide6.QtCore import Qt, Signal from PySide6.QtGui import QFont, QTextCharFormat, QTextCursor, QTextDocument from PySide6.QtWidgets import ( + QFrame, QLabel, QLineEdit, QListWidget, QListWidgetItem, + QSizePolicy, QHBoxLayout, QVBoxLayout, QWidget, @@ -23,6 +25,7 @@ class Search(QWidget): """Encapsulates the search UI + logic and emits a signal when a result is chosen.""" openDateRequested = Signal(str) + resultDatesChanged = Signal(list) def __init__(self, db, parent: QWidget | None = None): super().__init__(parent) @@ -30,17 +33,21 @@ class Search(QWidget): self.search = QLineEdit() self.search.setPlaceholderText("Search for notes here") + self.search.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Fixed) self.search.textChanged.connect(self._search) self.results = QListWidget() self.results.setUniformItemSizes(False) self.results.setSelectionMode(self.results.SelectionMode.SingleSelection) + self.results.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding) self.results.itemClicked.connect(self._open_selected) self.results.hide() + self.results.setMinimumHeight(250) lay = QVBoxLayout(self) lay.setContentsMargins(0, 0, 0, 0) lay.setSpacing(6) + lay.setAlignment(Qt.AlignTop) lay.addWidget(self.search) lay.addWidget(self.results) @@ -58,6 +65,7 @@ class Search(QWidget): if not q: self.results.clear() self.results.hide() + self.resultDatesChanged.emit([]) # clear highlights return try: @@ -73,8 +81,10 @@ class Search(QWidget): rows = list(rows) if not rows: self.results.hide() + self.resultDatesChanged.emit([]) # clear highlights return + self.resultDatesChanged.emit(sorted({d for d, _ in rows})) self.results.show() for date_str, content in rows: @@ -90,12 +100,13 @@ class Search(QWidget): outer.setSpacing(2) # Date label (plain text) - date_lbl = QLabel(date_str) - date_lbl.setTextFormat(Qt.TextFormat.PlainText) + date_lbl = QLabel() + date_lbl.setTextFormat(Qt.TextFormat.RichText) + date_lbl.setText(f"{date_str}:") date_f = date_lbl.font() - date_f.setPointSizeF(date_f.pointSizeF() - 1) + date_f.setPointSizeF(date_f.pointSizeF() + 1) date_lbl.setFont(date_f) - date_lbl.setStyleSheet("color:#666;") + date_lbl.setStyleSheet("color:#000;") outer.addWidget(date_lbl) # Preview row with optional ellipses @@ -127,6 +138,11 @@ class Search(QWidget): outer.addWidget(row) + line = QFrame() + line.setFrameShape(QFrame.HLine) + line.setFrameShadow(QFrame.Sunken) + outer.addWidget(line) + # ---- Add to list ---- item = QListWidgetItem() item.setData(Qt.ItemDataRole.UserRole, date_str)