Verified Commit 008c756d authored by tallero's avatar tallero

page:add value for existing property

parent 2a15b4fa
......@@ -62,11 +62,12 @@ class Entity(Stack):
entry = Template.Child("entry")
label = Template.Child("label")
value_eventbox = Template.Child("value_eventbox")
unit = Template.Child("unit")
#TODO:implement editing
def __init__(self, snak=None, qualifier=False, css=None, **kwargs):
def __init__(self, snak=None, new=False, qualifier=False, css=None, **kwargs):
Stack.__init__(self, **kwargs)
context = self.entry.get_style_context()
......@@ -79,77 +80,14 @@ class Entity(Stack):
self.unit.props.valign = 1
try:
if snak['snaktype'] == 'novalue':
self.label.set_text("No value")
if snak['snaktype'] == 'value':
dv = snak['datavalue']
dt = snak['datatype']
if dt == 'wikibase-item' or dt == 'wikibase-property':
if dv['type'] == 'wikibase-entityid':
entity_type = dv['value']['entity-type']
numeric_id = dv['value']['numeric-id']
if entity_type == 'item':
URI = 'Q' + str(numeric_id)
if entity_type == 'property':
URI = 'P' + str(numeric_id)
print("Entity: light downloading", URI)
download_light(URI, self.load_entity)
if dt == 'url':
url = dv['value']
#label = "".join(["<a href='", url, "'>", url.split('/')[2], '</a>'])
label = url.split('/')[2]
self.data = {"url":url}
self.set_text(label, url)
context = self.label.get_style_context()
set_style(context, '/ml/prevete/Daty/gtk/entity.css', 'url', True)
#self.label.props.use_markup = True
if dt == 'quantity':
unit = dv['value']['unit']
if unit.startswith('http'):
unit = dv['value']['unit'].split('/')[-1]
download_light(unit, self.on_download_unit)
amount = dv['value']['amount']
ub = dv['value']['upperBound']
lb = dv['value']['lowerBound']
if float(amount) > 0:
amount = str(round(float(amount)))
if ub and lb:
amount = amount + " ± " + str(round(float(ub) - float(amount)))
if ub and not lb:
amount = amount + " + " + str(round(float(ub) - float(amount)))
if not ub and lb:
amount = amount + " - " + str(round(float(amount) - float(lb)))
self.label.set_text(amount)
if dt == 'string':
self.set_text(dv['value'], "Text")
if dt == 'monolingualtext':
#TODO: better implement monolingual text
self.set_text(dv['value']['text'], dv['value']['language'])
if dt == 'commonsMedia':
self.set_text(dv['value'], "Picture")
if dt == 'external-id':
self.set_text(dv['value'], "External ID")
if dt == 'geo-shape':
#TODO: implement geo-shape
#print('geo-shape')
pass
if dt == 'globe-coordinate':
#TODO: implement globe-coordinate
#print('globe-coordinate')
pass
if dt == 'tabular-data':
#TODO: implement tabular-data
#print('tabular-data')
pass
if dt == 'time':
#TODO: implement time point
#print('time')
pass
del snak
self.read(snak)
except Exception as err:
raise err
self.new = new
if self.new:
print("new entity")
self.entry_focus_out_connection = self.entry.connect("focus-out-event",
self.entry_focus_out_event_cb)
self.entry_focus_in_connection = self.entry.connect("focus-in-event",
......@@ -157,12 +95,80 @@ class Entity(Stack):
#self.entry.connect("search-changed", self.entry_search_changed_cb)
def read(self, snak):
if snak['snaktype'] == 'novalue':
self.label.set_text("No value")
if snak['snaktype'] == 'value':
dv = snak['datavalue']
dt = snak['datatype']
if dt == 'wikibase-item' or dt == 'wikibase-property':
if dv['type'] == 'wikibase-entityid':
entity_type = dv['value']['entity-type']
numeric_id = dv['value']['numeric-id']
if entity_type == 'item':
URI = 'Q' + str(numeric_id)
if entity_type == 'property':
URI = 'P' + str(numeric_id)
print("Entity: light downloading", URI)
download_light(URI, self.load_entity)
if dt == 'url':
url = dv['value']
#label = "".join(["<a href='", url, "'>", url.split('/')[2], '</a>'])
label = url.split('/')[2]
self.data = {"url":url}
self.set_text(label, url)
context = self.label.get_style_context()
set_style(context, '/ml/prevete/Daty/gtk/entity.css', 'url', True)
#self.label.props.use_markup = True
if dt == 'quantity':
unit = dv['value']['unit']
if unit.startswith('http'):
unit = dv['value']['unit'].split('/')[-1]
download_light(unit, self.on_download_unit)
amount = dv['value']['amount']
ub = dv['value']['upperBound']
lb = dv['value']['lowerBound']
if float(amount) > 0:
amount = str(round(float(amount)))
if ub and lb:
amount = amount + " ± " + str(round(float(ub) - float(amount)))
if ub and not lb:
amount = amount + " + " + str(round(float(ub) - float(amount)))
if not ub and lb:
amount = amount + " - " + str(round(float(amount) - float(lb)))
self.label.set_text(amount)
if dt == 'string':
self.set_text(dv['value'], "Text")
if dt == 'monolingualtext':
#TODO: better implement monolingual text
self.set_text(dv['value']['text'], dv['value']['language'])
if dt == 'commonsMedia':
self.set_text(dv['value'], "Picture")
if dt == 'external-id':
self.set_text(dv['value'], "External ID")
if dt == 'geo-shape':
#TODO: implement geo-shape
#print('geo-shape')
pass
if dt == 'globe-coordinate':
#TODO: implement globe-coordinate
#print('globe-coordinate')
pass
if dt == 'tabular-data':
#TODO: implement tabular-data
#print('tabular-data')
pass
if dt == 'time':
#TODO: implement time point
#print('time')
pass
del snak
def set_text(self, label, description):
set_text(self.label, label, description)
set_text(self.entry, label, description)
def on_download_unit(self, URI, unit, error):
if error:
print(error)
......
......@@ -27,11 +27,11 @@ from copy import deepcopy as cp
from gi import require_version
require_version('Gdk', '3.0')
require_version('Gtk', '3.0')
from gi.repository.Gdk import Event, EventKey, KEY_Escape
from gi.repository.Gdk import Event, EventButton, EventKey, KEY_Escape
from gi.repository.GObject import SignalFlags as sf
from gi.repository.GObject import TYPE_NONE, TYPE_STRING, TYPE_PYOBJECT
from gi.repository.GLib import idle_add, PRIORITY_LOW
from gi.repository.Gtk import Frame, Label, ScrolledWindow, Template
from gi.repository.Gtk import Frame, Label, ScrolledWindow, Template, Viewport
from pprint import pprint
from threading import Thread
......@@ -72,7 +72,7 @@ class Page(ScrolledWindow):
references_toggled = 1
def __init__(self, entity, *args, **kwargs):
def __init__(self, entity, *args, new=False, **kwargs):
ScrolledWindow.__init__(self, *args, **kwargs)
claims = entity['claims']
......@@ -107,17 +107,51 @@ class Page(ScrolledWindow):
self.references_toggled -= 1
frame.set_min_content_height(frame.height*min(self.references_toggled, 3))
@Template.Callback()
def statement_add_clicked_cb(self, widget):
print("deve comparire una barra di ricerca")
def load_property(self, URI, prop, error, i):
try:
if error:
print(error)
prop = Property(prop)
self.statements.attach(prop, 0, i, 1, 1)
prop.connect('value-new', self.property_value_new_clicked_cb)
prop.position, prop.size = (0,i), (1,1)
self.statements.attach(prop, *prop.position, *prop.size)
return None
except Exception as e:
print(URI)
raise e
def property_value_new_clicked_cb(self, property, original_position):
get_top = lambda x: self.statements.child_get_property(x, 'top-attach')
property_top_value = get_top(property)
for x in self.statements.get_children():
if get_top(x) == property_top_value:
while type(x) in (Frame, ScrolledWindow, Viewport):
x = x.get_children()[0]
values = x
while not type(x) == Value:
x = x.get_children()[0]
value = x
print(value.entity)
print(values.get_children()[0].get_children())
claim = {'mainsnak':{'snaktype':'value',
'datavalue':{'type':'wikibase-entityid',
'value':{'entity-type':'item',
'numeric-id':1}},
'datatype':'wikibase-item'}}
value = Value(claim=claim, new=True)
values.add(value)
values.show_all()
event = EventButton()
value.entity.value_eventbox.do_button_press_event(self, event)
value.entity.set_visible_child_name('entry')
value.entity.entry.grab_focus()
def load_value_async(self, claim, values):
def do_call():
error = None
......
......@@ -25,6 +25,9 @@
from gi import require_version
require_version('Gtk', '3.0')
from gi.repository.Gtk import CssProvider, StyleContext, STYLE_PROVIDER_PRIORITY_APPLICATION, Button, IconTheme, Template
from gi.repository.GObject import SignalFlags as sf
from gi.repository.GObject import TYPE_NONE, TYPE_STRING, TYPE_PYOBJECT
from .util import set_style
#from .wikidata import Wikidata
......@@ -33,6 +36,10 @@ from .util import set_style
class Property(Button):
__gtype_name__ = "Property"
__gsignals__ = {'value-new':(sf.RUN_LAST,
TYPE_NONE,
(TYPE_PYOBJECT,))}
property_label = Template.Child("property_label")
popover = Template.Child("popover")
description = Template.Child("description")
......@@ -60,6 +67,12 @@ class Property(Button):
self.property_label.set_text(label)
self.property_label.set_tooltip_text(tooltip)
@Template.Callback()
def value_new_clicked_cb(self, widget):
print("value-new emitted")
self.emit('value-new', self.position)
self.popover.popdown()
@Template.Callback()
def clicked_cb(self, widget):
print("hi")
......
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<!-- Generated with glade 3.22.0 -->
<interface>
<requires lib="gtk+" version="3.22"/>
<!-- interface-css-provider-path entity.css -->
......@@ -34,7 +34,7 @@
</packing>
</child>
<child>
<object class="GtkEventBox">
<object class="GtkEventBox" id="value_eventbox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="vexpand">True</property>
......
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<!-- Generated with glade 3.22.0 -->
<interface>
<requires lib="gtk+" version="3.22"/>
<requires lib="libhandy" version="0.0"/>
......@@ -121,10 +121,11 @@
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkButton" id="add_statement">
<object class="GtkButton" id="statement_add">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="statement_add_clicked_cb" swapped="no"/>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
......
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<!-- Generated with glade 3.22.0 -->
<interface>
<requires lib="gtk+" version="3.22"/>
<!-- interface-css-provider-path property.css -->
......@@ -75,6 +75,7 @@
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="text" translatable="yes">Add value</property>
<signal name="clicked" handler="value_new_clicked_cb" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
......
......@@ -80,16 +80,17 @@ class Value(Grid):
qualifier_new = Template.Child("qualifier_new")
reference_new = Template.Child("reference_new")
def __init__(self, claim, *args, **kwargs):
def __init__(self, claim, *args, new=False, **kwargs):
Grid.__init__(self, *args, **kwargs)
self.qualifier_pos = {}
self.qualifier_row = 0
self.reference_row = 0
self.references_expanded = False
self.entity = Entity(claim['mainsnak'])
self.entity = Entity(claim['mainsnak'], new=new)
self.entity.connect("entity-editing", self.entity_editing_cb)
self.entity.connect("entity-leaving", self.entity_leaving_cb)
self.entity.connect("object-selected", self.object_selected_cb, claim)
......
......@@ -64,6 +64,9 @@ class Values(ListBox):
row.add(widget)
super(Values, self).add(row)
def add_new():
pass
def references_toggled_cb(self, widget, child):
for i, row in enumerate(self.get_children()):
if hasattr(row, 'child') and row.child == child:
......
......@@ -68,7 +68,7 @@
],
"build-options" : {
"env" : {
"GTK_DEBUG" : "interactive"
"GTK_DEBUG" : "touchscreen"
}
}
}
<object class="GtkFrame">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="valign">center</property>
<property name="margin_bottom">18</property>
<property name="row_spacing">6</property> >
<property name="column_spacing">12</property>
<property name="hexpand">True</property>
<property name="label_xalign">0</property>
<property name="shadow_type">out</property>
<child>
<object class="GtkLabel" id="username_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="tooltip_markup" translatable="yes">_("Wikimedia username description")</property>
<property name="halign">end</property>
<property name="label" translatable="yes">Username</property>
<property name="justify">right</property>
<attributes>
<attribute name="font-desc" value="Cantarell 10"/>
</attributes>
<style>
<class name="dim-label"/>
</style>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkListBox" id="credentials">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="selection_mode">none</property>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
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