Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add current_location_label #21

Draft
wants to merge 3 commits into
base: milestone-1
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion PyMca5/PyMcaGui/io/QTiledCatalogSelectorDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,21 @@ def create_layout(self) -> None:
self.rows_per_page_selector = QComboBox()

self.current_location_label = QLabel()
self.first_page = ClickableQLabel("<<")
self.previous_page = ClickableQLabel("<")
self.next_page = ClickableQLabel(">")
self.last_page = ClickableQLabel(">>")
self.navigation_widget = QWidget()

# Navigation layout
navigation_layout = QHBoxLayout()
navigation_layout.addWidget(self.rows_per_page_label)
navigation_layout.addWidget(self.rows_per_page_selector)
navigation_layout.addWidget(self.current_location_label)
navigation_layout.addWidget(self.first_page)
navigation_layout.addWidget(self.previous_page)
navigation_layout.addWidget(self.next_page)
navigation_layout.addWidget(self.last_page)
self.navigation_widget.setLayout(navigation_layout)

# Current path layout
Expand Down Expand Up @@ -218,6 +222,15 @@ def reset_rows_per_page(self) -> None:
)
self.rows_per_page_selector.setCurrentIndex(self.model._rows_per_page_index)

def _set_current_location_label(self):
starting_index = self.model._current_page * self.model.rows_per_page + 1
ending_index = min(
self.model.rows_per_page * (self.model._current_page + 1),
len(self.model.get_current_node()),
)
current_location_text = f"{starting_index}-{ending_index} of {len(self.model.get_current_node())}"
self.current_location_label.setText(current_location_text)

def populate_table(self):
original_state = {}
# TODO: may need if condition if we implement a disconnect button
Expand All @@ -235,7 +248,7 @@ def populate_table(self):
self.catalog_table.setItem(0, 0, self.catalog_breadcrumbs)

# Then add new rows
rows_per_page = self.model._rows_per_page_options[self.model._rows_per_page_index]
rows_per_page = self.model.rows_per_page
for _ in range(rows_per_page):
last_row_position = self.catalog_table.rowCount()
self.catalog_table.insertRow(last_row_position)
Expand Down Expand Up @@ -337,8 +350,12 @@ def on_client_connection_error(error_msg: str):
@self.model.table_changed.connect
def on_table_changed(node_path_parts: Tuple[str]):
_logger.debug(f"on_table_changed(): {node_path_parts = }")
if self.model.client is None:
# TODO: handle disconnecting from tiled client later
return
self.populate_table()
self._rebuild_current_path_layout()
self._set_current_location_label()

@self.model.url_validation_error.connect
def on_url_validation_error(error_msg: str):
Expand All @@ -356,8 +373,11 @@ def connect_model_slots(self) -> None:
self.url_entry.textEdited.connect(model.on_url_text_edited)
self.url_entry.editingFinished.connect(model.on_url_editing_finished)
self.connect_button.clicked.connect(model.on_connect_clicked)
self.first_page.clicked.connect(model.on_first_page_clicked)
self.next_page.clicked.connect(model.on_next_page_clicked)
self.previous_page.clicked.connect(model.on_prev_page_clicked)
self.last_page.clicked.connect(model.on_last_page_clicked)
self.rows_per_page_selector.currentIndexChanged.connect(self.model.on_rows_per_page_changed)

def connect_self_signals(self):
# TODO find another way to do this?
Expand Down
23 changes: 22 additions & 1 deletion PyMca5/PyMcaGui/io/TiledCatalogSelector.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import functools
from math import ceil
import logging
from collections import defaultdict
from datetime import date, datetime
Expand Down Expand Up @@ -117,6 +118,10 @@ def client(self):
def client(self, _):
"""Do not directly replace the root Tiled client."""
raise NotImplementedError("Call connect_client() instead")

@property
def rows_per_page(self):
return self._rows_per_page_options[self._rows_per_page_index]

def connect_client(self) -> None:
"""Connect the model's Tiled client to the Tiled server at URL.
Expand Down Expand Up @@ -198,19 +203,35 @@ def on_item_selected(self, child_node_path):
else:
self.load_button_enabled = False

def on_rows_per_page_changed(self, index):
self._rows_per_page_index = index
self._current_page = 0
self.table_changed.emit(self.node_path_parts)

def on_first_page_clicked(self):
self._current_page = 0
self.table_changed.emit(self.node_path_parts)

def on_prev_page_clicked(self):
if self._current_page != 0:
self._current_page -= 1
self.table_changed.emit(self.node_path_parts)

def on_next_page_clicked(self):
rows_per_page = self._rows_per_page_options[self._rows_per_page_index]
rows_per_page = self.rows_per_page
if (
self._current_page * rows_per_page
) + rows_per_page < len(self.get_current_node()):
self._current_page += 1
self.table_changed.emit(self.node_path_parts)

def on_last_page_clicked(self):
# TODO: math.ceil gives the wrong answer for really large numbers
# Solution 4 in this answer: https://stackoverflow.com/a/54585138
# Do we worry about this for our use case?
self._current_page = ceil(len(self.get_current_node()) / self.rows_per_page) - 1
self.table_changed.emit(self.node_path_parts)
Comment on lines +228 to +233
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that I see these are integers and used for tracking pages, my 2c is that the integer division/modulo is clearest.


def get_current_node(self) -> BaseClient:
"""Fetch a Tiled client corresponding to the current node path."""
return self.get_node(self.node_path_parts)
Expand Down