better displaying of results

This commit is contained in:
Jonas Weinz 2020-10-23 18:39:49 +02:00
parent 505b2c67a9
commit 5651c51bde
2 changed files with 72 additions and 37 deletions

View File

@ -1,6 +1,8 @@
import requests
import json
import datetime as dt
class MediathekViewWebAnswer(object):
def __init__(self, answer_json: str):
@ -68,6 +70,11 @@ class MediathekViewWebAnswer(object):
def get_description(self):
return self._description
def get_timestamp(self) -> dt.datetime:
if self._timestamp is None:
return None
return dt.datetime.utcfromtimestamp(int(self._timestamp))
class MediathekViewWebRequest(object):

View File

@ -1,14 +1,14 @@
from gi.repository import Gtk, GLib
from . import mediathek_request as mr
from . import tools
from gi.repository import Gtk, GLib, Gdk
import gi
gi.require_version("Gtk", "3.0")
from . import tools
from . import mediathek_request as mr
class SearchWidget(Gtk.Box):
def __init__(self, parent: 'main_window.MainApp') -> None:
Gtk.Box.__init__(self, orientation = Gtk.Orientation.VERTICAL)
Gtk.Box.__init__(self, orientation=Gtk.Orientation.VERTICAL)
self._search_bar = Gtk.SearchBar()
self._search_entry = Gtk.SearchEntry()
@ -23,10 +23,31 @@ class SearchWidget(Gtk.Box):
self._main_window = parent
self._current_search_cards = []
self._search_card_container = Gtk.Box(orientation = Gtk.Orientation.VERTICAL)
self._search_card_container = Gtk.FlowBox()
self._search_card_container.set_homogeneous(True)
self._search_card_container.set_vexpand(False)
self._search_card_container.set_valign(Gtk.Align.START)
self.pack_end(self._search_card_container, False, True, 10)
self._scrolled_window = Gtk.ScrolledWindow()
self._scrolled_window.add(self._search_card_container)
self._scrolled_window.set_vexpand(False)
self._card_bg_color = None
self.pack_start(self._scrolled_window, True, True, 10)
def get_card_bg_color(self):
if self._card_bg_color is None:
color = self._main_window.get_style_context().get_background_color(Gtk.StateType.NORMAL)
new_red = int(color.red * 0.95 * 2**16)
new_green = int(color.red * 0.95 * 2**16)
new_blue = int(color.red * 0.95 * 2**16)
self._card_bg_color = Gdk.Color(new_red, new_green, new_blue)
return self._card_bg_color
def dialog_response(self, widget, response_id):
# if the button clicked gives response OK (-5)
if response_id == Gtk.ResponseType.OK:
@ -47,22 +68,24 @@ class SearchWidget(Gtk.Box):
messagedialog.connect("response", self.dialog_response)
# show the messagedialog
messagedialog.show()
def create_result_card(self, answer:mr.MediathekViewWebAnswer) -> Gtk.Box:
def create_result_card(self, answer: mr.MediathekViewWebAnswer) -> Gtk.Box:
url = answer.get_best_vid_url()
title = answer.get_title()
description = answer.get_description()
time = answer.get_timestamp()
time_str = "" if time is None else time.strftime('%Y-%m-%d %H:%M:%S')
if len(description) > 50:
description = description[:50] + "..."
play_button = tools.new_button_with_icon("media-playback-start")
def on_click(_ = None):
def on_click(_=None):
if url is not None:
self._main_window.start_player(url)
play_button.connect("clicked", on_click)
title_label = Gtk.Label()
@ -73,33 +96,42 @@ class SearchWidget(Gtk.Box):
description_label.set_markup(description)
description_label.set_justify(Gtk.Justification.LEFT)
description_label.set_xalign(0)
time_label = Gtk.Label()
time_label.set_markup(time_str)
time_label.set_justify(Gtk.Justification.LEFT)
time_label.set_xalign(0)
text_box = Gtk.Box(orientation = Gtk.Orientation.VERTICAL)
text_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
text_box.pack_start(title_label, True, False, 0)
text_box.pack_start(description_label, True, False, 0)
text_box.pack_start(title_label, False, False, 0)
text_box.pack_start(description_label, False, False, 0)
text_box.pack_start(time_label, False, False, 0)
card_content = Gtk.Box(orientation = Gtk.Orientation.HORIZONTAL)
card_content = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
card_content.pack_start(play_button, False, False, 10)
card_content.pack_start(text_box, True, True, 0)
card_content.pack_start(play_button, False, False, 0)
card = Gtk.Box(orientation = Gtk.Orientation.VERTICAL)
card.pack_start(card_content, True, True, 0)
card.pack_end(Gtk.Separator(orientation = Gtk.Orientation.HORIZONTAL), False, False, 10)
#card.pack_end(Gtk.Separator(orientation = Gtk.Orientation.HORIZONTAL), False, False, 10)
#card_event_box = Gtk.EventBox()
#card_event_box.add(card_content)
#card_event_box.modify_bg(Gtk.StateType.NORMAL, self.get_card_bg_color())
card_content.set_vexpand(False)
return card_content
return card
def clean_results(self):
for result in self._current_search_cards:
self._search_card_container.remove(result)
# result.destroy()
self._search_card_container.remove(result.get_parent())
self._current_search_cards = []
def on_search(self, _) -> None:
self.clean_results()
search_text = self._search_entry.get_text()
req = mr.MediathekViewWebRequest(query=search_text)
@ -109,21 +141,17 @@ class SearchWidget(Gtk.Box):
if answers is None:
self.display_warning("error while performing the search request")
return
if len(answers) == 0:
self.display_warning("no results found")
return
self.clean_results()
for answer in answers:
#print(answer)
# print(answer)
card = self.create_result_card(answer)
self._current_search_cards.append(card)
self._search_card_container.pack_start(card, False, False, 0)
self._search_card_container.insert(card, -1)
self._search_card_container.show_all()
self.show_all()