Skip to content

Commit

Permalink
ui(content-browser): general improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
Guts committed Apr 30, 2024
1 parent 12b585f commit da1a20b
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 137 deletions.
81 changes: 50 additions & 31 deletions qtribu/gui/dlg_contents.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#! python3 # noqa: E265

"""
QDialog to browse Geotribu contents.
"""

from pathlib import Path
from typing import Callable, Dict, List
from typing import Callable, Dict, List, Optional

from qgis.core import QgsApplication
from qgis.PyQt import QtCore, QtWidgets, uic
Expand All @@ -20,11 +26,13 @@
ICON_GEORDP = QIcon(str(DIR_PLUGIN_ROOT.joinpath("resources/images/geordp.svg")))
MARKER_VALUE = "---"

# -- CLASSES --


class GeotribuContentsDialog(QDialog):
contents: Dict[int, List[RssItem]] = {}

def __init__(self, parent: QWidget = None):
def __init__(self, parent: Optional[QWidget] = None):
"""
QDialog for geotribu contents
Expand All @@ -43,47 +51,46 @@ def __init__(self, parent: QWidget = None):
# buttons actions
self.form_article = None
self.form_rdp_news = None
self.submit_article_button.clicked.connect(self.submit_article)
self.submit_article_button.setIcon(
self.btn_submit_article.clicked.connect(self.submit_article)
self.btn_submit_article.setIcon(
QgsApplication.getThemeIcon("mActionEditTable.svg")
)
self.submit_news_button.clicked.connect(self.submit_news)
self.submit_news_button.setIcon(
QgsApplication.getThemeIcon("mActionAllEdits.svg")
)
self.donate_button.clicked.connect(self.donate)
self.donate_button.setIcon(
self.btn_submit_news.clicked.connect(self.submit_news)
self.btn_submit_news.setIcon(QgsApplication.getThemeIcon("mActionAllEdits.svg"))
self.btn_donate.clicked.connect(self.donate)
self.btn_donate.setIcon(
QgsApplication.getThemeIcon("mIconCertificateTrusted.svg")
)

# search actions
self.search_line_edit.textChanged.connect(self.on_search_text_changed)
self.lne_search.textChanged.connect(self.on_search_text_changed)

# authors combobox
self.authors_combobox.addItem(MARKER_VALUE)
self.cbb_authors.addItem(MARKER_VALUE)
for author in self.json_feed_client.authors():
self.authors_combobox.addItem(author)
self.authors_combobox.currentTextChanged.connect(self.on_author_changed)
self.cbb_authors.addItem(author)
self.cbb_authors.currentTextChanged.connect(self.on_author_changed)

# categories combobox
self.categories_combobox.addItem(MARKER_VALUE)
self.cbb_tags.addItem(MARKER_VALUE)
for cat in self.json_feed_client.categories():
self.categories_combobox.addItem(cat)
self.categories_combobox.currentTextChanged.connect(self.on_category_changed)
if cat not in ("article", "revue de presse"):
self.cbb_tags.addItem(cat)
self.cbb_tags.currentTextChanged.connect(self.on_category_changed)

# tree widget initialization
self.contents_tree_widget.setHeaderLabels(
self.tree_contents.setHeaderLabels(
[
self.tr("Date"),
self.tr("Title"),
self.tr("Author(s)"),
self.tr("Categories"),
]
)
self.contents_tree_widget.itemClicked.connect(self.on_tree_view_item_click)
self.tree_contents.itemClicked.connect(self.on_tree_view_item_click)

self.refresh_list(lambda: self.search_line_edit.text())
self.contents_tree_widget.expandAll()
self.refresh_list(lambda: self.lne_search.text())
self.tree_contents.expandAll()

def submit_article(self) -> None:
"""
Expand Down Expand Up @@ -159,7 +166,7 @@ def refresh_list(self, query_action: Callable[[], str]) -> None:
}

# clean treeview items
self.contents_tree_widget.clear()
self.tree_contents.clear()

# populate treewidget
items = []
Expand All @@ -171,8 +178,16 @@ def refresh_list(self, query_action: Callable[[], str]) -> None:
child = self._build_tree_widget_item_from_content(content)
item.addChild(child)
items.append(item)
self.contents_tree_widget.insertTopLevelItems(0, items)
self.contents_tree_widget.expandAll()
self.tree_contents.insertTopLevelItems(0, items)
self.tree_contents.expandAll()
self.tableTunning()

def tableTunning(self):
"""Prettify table aspect"""
# fit to content
self.tree_contents.resizeColumnToContents(0)
self.tree_contents.resizeColumnToContents(1)
self.tree_contents.resizeColumnToContents(2)

@QtCore.pyqtSlot(QtWidgets.QTreeWidgetItem, int)
def on_tree_view_item_click(self, item: QTreeWidgetItem, column: int):
Expand All @@ -194,7 +209,7 @@ def on_search_text_changed(self) -> None:
Should get search
"""
# do nothing if text is too small
current = self.search_line_edit.text()
current = self.lne_search.text()
if current == "":
self.refresh_list(lambda: current)
return
Expand All @@ -209,9 +224,9 @@ def on_author_changed(self, value: str) -> None:
:param value: text value of the selected author
:type value: str
"""
self.search_line_edit.setText("")
self.lne_search.setText("")
if value == MARKER_VALUE:
self.refresh_list(lambda: self.search_line_edit.text())
self.refresh_list(lambda: self.lne_search.text())
return
self.refresh_list(lambda: value)

Expand All @@ -222,9 +237,9 @@ def on_category_changed(self, value: str) -> None:
:param value: text value of the selected category
:type value: str
"""
self.search_line_edit.setText("")
self.lne_search.setText("")
if value == MARKER_VALUE:
self.refresh_list(lambda: self.search_line_edit.text())
self.refresh_list(lambda: self.lne_search.text())
return
self.refresh_list(lambda: value)

Expand All @@ -236,12 +251,16 @@ def _build_tree_widget_item_from_content(content: RssItem) -> QTreeWidgetItem:
:param content: content to generate item for
:type content: RssItem
"""
tags = ", ".join(
[t for t in content.categories if t not in ("article", "revue de presse")]
)

item = QTreeWidgetItem(
[
content.date_pub.strftime("%d %B"),
content.title,
",".join(content.author),
",".join(content.categories),
", ".join(content.author),
tags,
content.url,
]
)
Expand Down
Loading

0 comments on commit da1a20b

Please sign in to comment.