Verified Commit a1b07c1e authored by tallero's avatar tallero

Open: selection mode completed

parent 4e56fa5c
......@@ -32,10 +32,11 @@ from gi.repository.Gtk import Application, Builder
from sys import argv
#from .aboutdaty import AboutDaty
name = "ml.prevete.Daty"
class Daty(Application):
def __init__(self, *args, new_session=True, entities=[], **kwargs):
super().__init__(*args, application_id="ml.prevete.Daty",
super().__init__(*args, application_id=name,
flags=ApplicationFlags.HANDLES_COMMAND_LINE,
**kwargs)
self.entities=entities
......
......@@ -33,7 +33,9 @@ class EntitySelectable(CheckButton):
description = Template.Child("description")
URI = Template.Child("URI")
def __init__(self, entity, *args, widget=True, selected=None, open_button=None):
def __init__(self, entity, *args,
widget=True, selected=None, open_button=None,
select_entities=None):
"""Search result widget in 'open new entity' dialog
Args:
......@@ -47,7 +49,6 @@ class EntitySelectable(CheckButton):
if widget:
self.label.set_text(entity['Label'])
self.description.set_text(entity['Description'])
#self.URI.set_text('(' + entity['URI'] + ')')
self.URI.set_text(entity['URI'])
else:
self.label.destroy()
......@@ -59,6 +60,7 @@ class EntitySelectable(CheckButton):
self.set_active(True)
args = [self.toggled_cb, selected]
if open_button: args = args + [open_button]
if select_entities: args = args + [select_entities]
self.connect("toggled", *args)
self.show_all()
......@@ -74,12 +76,21 @@ class EntitySelectable(CheckButton):
selected.add(self.entity)
try:
args[0].set_sensitive(True)
except:
pass
args[1].set_label(" ".join(["Selected",
str(len(selected)),
"entities"]))
except Exception as e:
raise e
#pass
else:
selected.remove(self.entity)
if not selected:
try:
try:
if not selected:
args[0].set_sensitive(False)
except:
pass
args[1].set_label("Click on entities to select them")
else:
args[1].set_label(" ".join(["Selected",
str(len(selected)),
"entities"]))
except:
pass
......@@ -33,11 +33,11 @@ from threading import Thread
from .entityselectable import EntitySelectable
from .overlayedlistboxrow import OverlayedListBoxRow
from .roundedbutton import RoundedButton
from .sidebarentity import SidebarEntity
from .triplet import Triplet
from .wikidata import Wikidata
from .util import EntitySet
from .util import EntitySet, search
name = 'ml.prevete.Daty'
......@@ -47,23 +47,26 @@ class Open(Window):
wikidata = Wikidata()
#properties = Template.Child("properties")
back = Template.Child("back")
content_stack = Template.Child("content_stack")
bottom_bar = Template.Child("bottom_bar")
header_bar = Template.Child("header_bar")
cancel = Template.Child("cancel")
constraint_box = Template.Child("constraint_box")
constraint_button_box = Template.Child("constraint_button_box")
constraint_listbox = Template.Child("constraint_listbox")
constraint_search = Template.Child("constraint_search")
open_session = Template.Child("open_session")
page = Template.Child("page")
label_listbox = Template.Child("label_listbox")
open_button = Template.Child("open_button")
results = Template.Child("results")
results_listbox = Template.Child("results_listbox")
select = Template.Child("select")
select_entities = Template.Child("select_entities")
select_menu = Template.Child("select_menu")
search_box = Template.Child("search_box")
search_entry = Template.Child("search_entry")
subtitle = Template.Child("subtitle")
title = Template.Child("title")
titlebar = Template.Child("titlebar")
def __init__(self, load, *args, new_session=True, quit_cb=None, verbose=False):
Window.__init__(self, *args)
......@@ -76,37 +79,34 @@ class Open(Window):
self.verbose = verbose
self.new_session = new_session
self.load = load
self.results_listbox.selected = EntitySet()
self.entities = self.results_listbox.selected
self.hb_title = self.header_bar.get_title()
self.hb_subtitle = self.header_bar.get_subtitle()
if quit_cb:
self.quit_cb = quit_cb
self.connect("delete-event", self.on_quit)
#self.show_all()
self.show()
constraint = ListBoxRow()
self.constraint_listbox.show_all()
self.label_listbox.selected = EntitySet()
if new_session:
self.header_bar.set_show_close_button(True)
#self.set_search_placeholder(True)
else:
self.header_bar.set_show_close_button(False)
self.back.set_visible(True)
self.entities = self.label_listbox.selected
self.open_button.connect('clicked', self.open_button_clicked_cb, load)
def on_quit(self, widget, event):
self.quit_cb()
def set_search_placeholder(self, value, search_stack="label_search"):
def set_search_placeholder(self, value):
self.title.set_visible(value)
self.subtitle.set_visible(value)
self.open_button.set_visible(not value)
self.select.set_visible(not value)
#self.open_button.set_visible(not value)
self.results.set_visible(not value)
child = self.page.get_child_at(0,0)
child.set_property("vexpand", value)
......@@ -114,10 +114,8 @@ class Open(Window):
self.page.child_set_property(child, "width", 2)
if not self.constraint_box.get_visible():
self.constraint_button_box.props.valign = 1
else:
self.page.child_set_property(child, "width", 1)
#child.set_property("vexpand", False)
if not self.constraint_box.get_visible():
self.constraint_button_box.props.valign = 0
......@@ -133,7 +131,7 @@ class Open(Window):
#self.set_search_placeholder(True)
@Template.Callback()
def placeholder_add_constraint_clicked_cb(self, widget):
def add_constraint_clicked_cb(self, widget):
self.constraint_box.set_visible(True)
row = OverlayedListBoxRow(RoundedButton(), Triplet())
self.constraint_listbox.add(row)
......@@ -150,42 +148,85 @@ class Open(Window):
return None
self.search_entry.set_text("")
def on_search_done(self, results, error):
self.label_listbox.foreach(self.label_listbox.remove)
def on_search_done(self, results):
self.results_listbox.foreach(self.results_listbox.remove)
#TODO: Implement selection mode and make single selection the default
for r in results:
entity = EntitySelectable(r,
selected=self.label_listbox.selected,
open_button=self.open_button)
if self.titlebar.get_selection_mode():
entity = EntitySelectable(r,
selected=self.entities,
open_button=self.open_button)
else:
entity = SidebarEntity(r, close=False)
row = ListBoxRow()
row.child = entity
row.add(entity)
self.label_listbox.add(row)
self.label_listbox.show_all()
self.results_listbox.add(row)
self.results_listbox.show_all()
self.set_search_placeholder(False)
def search(self, query):
@Template.Callback()
def search_entry_search_changed_cb(self, entry):
query = entry.get_text()
if query:
query = cp(query)
wikidata = cp(self.wikidata)
f = lambda : wikidata.search(query)
def do_call():
results, error = None, None
try:
results = f()
except Exception as err:
error = err
idle_add(lambda: self.on_search_done(results, error))
thread = Thread(target = do_call)
thread.start()
search(entry.get_text(), self.on_search_done)
else:
self.set_search_placeholder(True)
@Template.Callback()
def search_entry_search_changed_cb(self, entry):
self.search(entry.get_text())
def results_listbox_row_activated_cb(self, widget, row):
if self.titlebar.get_selection_mode():
toggle = row.child #row.get_children()[0]
toggle.set_active(False) if toggle.get_active() else toggle.set_active(True)
else:
self.entities.add(row.child.entity)
self.load(self.entities)
self.destroy()
def set_selection_mode(self, value):
self.titlebar.set_selection_mode(value)
self.select.set_visible(not value)
self.cancel.set_visible(value)
self.bottom_bar.set_visible(value)
if value:
self.header_bar.set_custom_title(self.select_entities)
else:
self.header_bar.set_custom_title(None)
self.results_listbox.foreach(self.set_row_selection, value)
def set_row_selection(self, row, value):
entity = row.child.entity
if value:
entity = EntitySelectable(entity,
selected=self.entities,
open_button=self.open_button,
select_entities=self.select_entities)
else:
entity = SidebarEntity(entity, description=True, close=False)
row.child.destroy()
row.add(entity)
row.child = entity
@Template.Callback()
def select_clicked_cb(self, widget):
if not self.titlebar.get_selection_mode():
self.set_selection_mode(True)
else:
self.set_selection_mode(False)
@Template.Callback()
def select_entities_clicked_cb(self, widget):
self.select_menu.set_visible(True)
@Template.Callback()
def select_all_clicked_cb(self, widget):
self.results_listbox.foreach(self.select_row, True)
def select_row(self, row, value):
row.child.set_active(value)
@Template.Callback()
def label_listbox_row_activated_cb(self, widget, row):
toggle = row.get_children()[0]
toggle.set_active(False) if toggle.get_active() else toggle.set_active(True)
def deselect_all_clicked_cb(self, widget):
self.results_listbox.foreach(self.select_row, False)
This diff is collapsed.
......@@ -73,6 +73,7 @@
</child>
<child>
<object class="GtkLabel" id="description">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="label" translatable="yes">Description</property>
......
......@@ -256,8 +256,8 @@ def search(query, callback, *cb_args, wikidata=None, **kwargs):
try:
results = wikidata.search(query)
except Exception as err:
error = err
idle_add(lambda: callback(results, error, *cb_args, **kwargs))
raise err
idle_add(lambda: callback(results, *cb_args, **kwargs))
thread = Thread(target = do_call)
thread.start()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment