Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/activity_review/src/View.py
blob: f9fc923b79ba5e8a20853443853c7298227e85d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'''
Created on 22 Oct 2012

@author: cgueret
'''
from gi.repository import Gtk, Gdk, GObject
from semanticxo import graphstore
from Model import ReviewModel

class Main(object):
	def __init__(self):
		# Load the interface
		builder = Gtk.Builder()
		builder.add_from_file("review_ui.glade")
		
		# Create the internal variables used to store the list of talks
		self._talks = {}
		self._talks_notebook = builder.get_object("talks_notebook")
		
		# Get the main widget
		self._widget = builder.get_object("main_box")
	
		# Load the talks
		self.reload_talks()

		# The timeout to reload the talks
		self._timeout_id = GObject.timeout_add(5000, self.reload_talks, None)
		
	def get_widget(self):
		return self._widget

	def on_add_talk(self, data=None):
		model = ReviewModel()
		builder = Gtk.Builder()
		builder.add_from_file("review_ui.glade")
		dialog = builder.get_object("add_talk_dialog")
		response = dialog.run()
		if response == 1:
			title = builder.get_object("new_talk_title").get_text()
			model.add_new_talk(title)
			self.reload_talks()
		dialog.destroy()

	def reload_talks(self, data=None):
		model = ReviewModel()
		talks_list = model.get_talks_list()
		for talk_id in talks_list:
			if talk_id not in self._talks:
				self._talks[talk_id] = TalkPanel(talk_id)
				talk_resource = graphstore.get_instance().get_resource(talk_id)
				title_slug = talk_resource.get("title")[0]
				if len(title_slug) > 8:
					title_slug = title_slug[:4] + "..."
				self._talks_notebook.append_page(self._talks[talk_id].get_widget(), Gtk.Label(title_slug))
				self._talks_notebook.show_all()
		return True
	
class TalkPanel(object):
	def __init__(self, identifier):
		# Keep a pointer to the identifier of the talk
		self._identifier = identifier
		
		# Load the interface
		builder = Gtk.Builder()
		builder.add_from_file("review_ui.glade")
		
		# List of comments
		self._comments = set()
		self._comments_store = builder.get_object("questions_list_store")
		
		# Load the panel and keep a reference
		self._widget = builder.get_object("talk_panel")

		# Create an instance of the data model interface
		model = ReviewModel()
		
		# Set the title of the talk
		talk_resource = graphstore.get_instance().get_resource(identifier)
		builder.get_object("talk_title").set_text(talk_resource.get("title")[0])
		
		# Set the mood
		mood = model.get_mood_for_talk(talk=identifier)
		if mood == 'happy':
			radio = builder.get_object("radiobutton1").set_active(True)
		if mood == 'surprised':
			radio = builder.get_object("radiobutton2").set_active(True)
		if mood == 'perplex':
			radio = builder.get_object("radiobutton3").set_active(True)
		if mood == 'bored':
			radio = builder.get_object("radiobutton4").set_active(True)

		# Load the comments
		self.update_comments(model)

		# Connect the mood bar buttons to the call back
		for mood_id in [1, 2, 3, 4]:
			radio = builder.get_object("radiobutton%d" % mood_id)
			radio.connect("toggled", self.on_mood_button_toggled, mood_id)
		
		# Connect the question send button to the call back
		button = builder.get_object("new_question_button")
		text_entry = builder.get_object("new_question")
		button.connect("clicked", self.on_comment_button_clicked, text_entry)
		text_entry.connect("key-press-event", self.on_entry_key_pressed)
		
	def get_widget(self):
		return self._widget

	def on_mood_button_toggled(self, button, name):
		'''
		Update the status of the mood bar
		'''
		if button.get_active():
			moods = ['happy', 'surprised', 'perplex', 'bored']
			mood = moods[name - 1]
			model = ReviewModel()
			model.set_mood_for_talk(talk=self._identifier, mood=mood)
			
	def on_comment_button_clicked(self, button, entry):
		'''
		Post a new comment
		'''
		model = ReviewModel()
		model.add_new_comment_for_talk(talk=self._identifier, comment=entry.get_text())
		entry.set_text("")
		self.update_comments(model)
	
	def on_entry_key_pressed(self, entry, event):
		'''
		If the key pressed is enter, post the comment
		'''
		if event.keyval == Gdk.KEY_Return:
			model = ReviewModel()
			model.add_new_comment_for_talk(talk=self._identifier, comment=entry.get_text())
			entry.set_text("")
			self.update_comments(model)
		
	def update_comments(self, model):
		for (comment_id, message, date, author) in model.get_comments_for_talk(talk=self._identifier):
			if comment_id not in self._comments:
				self._comments_store.append([message, author.split('/')[-1], date])
				self._comments.add(comment_id)