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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
|
# -*- coding: UTF-8 -*-
# Copyright 2013 Facundo Batista
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
# For further info, check https://launchpad.net/encuentro
"""Central panels in the main window, the content part of all the interface."""
import logging
import operator
from PyQt4.QtGui import (
QAbstractItemView,
QAbstractTextDocumentLayout,
QApplication,
QColor,
QHBoxLayout,
QLabel,
QMenu,
QPalette,
QPixmap,
QPushButton,
QStyle,
QStyleOptionViewItemV4,
QStyledItemDelegate,
QTextDocument,
QTextEdit,
QTreeWidgetItem,
QVBoxLayout,
QWidget,
)
from PyQt4.QtCore import Qt, QSize
from encuentro import data, image
from encuentro.config import config, signal
from encuentro.data import Status
from encuentro.ui import remembering
from encuentro.ui.throbber import Throbber
logger = logging.getLogger("encuentro.centralpanel")
class DownloadsWidget(remembering.RememberingTreeWidget):
"""The downloads queue."""
def __init__(self, episodes_widget):
self.episodes_widget = episodes_widget
super(DownloadsWidget, self).__init__('downloads')
signal.register(self.save_state)
_headers = (u"Descargando...", u"Estado")
self.setColumnCount(len(_headers))
self.setHeaderLabels(_headers)
self.queue = []
self.current = -1
self.downloading = False
# connect the signals
self.clicked.connect(self.on_signal_clicked)
def on_signal_clicked(self, _):
"""The view was clicked."""
item = self.currentItem()
self.episodes_widget.show(item.episode_id)
def append(self, episode):
"""Append an episode to the downloads list."""
# add to the list in the GUI
item = QTreeWidgetItem((episode.title, u"Encolado"))
item.episode_id = episode.episode_id
self.queue.append((episode, item))
self.addTopLevelItem(item)
self.setCurrentItem(item)
# fix episode state
episode.state = Status.downloading
def prepare(self):
"""Set up everything for next download."""
self.downloading = True
self.current += 1
episode, _ = self.queue[self.current]
return episode
def start(self):
"""Download started."""
episode, item = self.queue[self.current]
item.setText(1, u"Comenzando")
episode.state = Status.downloading
def progress(self, progress):
"""Advance the progress indicator."""
_, item = self.queue[self.current]
item.setText(1, u"Descargando: %s" % progress)
def end(self, error=None):
"""Mark episode as downloaded."""
episode, item = self.queue[self.current]
if error is None:
# downloaded OK
gui_msg = u"Terminado ok"
end_state = Status.downloaded
else:
# something bad happened
gui_msg = unicode(error)
end_state = Status.none
item.setText(1, gui_msg)
item.setDisabled(True)
episode.state = end_state
self.episodes_widget.set_color(episode)
self.downloading = False
def cancel(self):
"""The download is being cancelled."""
_, item = self.queue[self.current]
item.setText(1, u"Cancelado")
def pending(self):
"""Return the pending downloads quantity (including current)."""
# remaining after current one
q = len(self.queue) - self.current - 1
# if we're still downloading current one, add it to the count
if self.downloading:
q += 1
return q
def save_state(self):
"""Save state for pending downloads."""
p = self.pending()
if p > 0:
config[config.SYSTEM]['pending_ids'] = [
e.episode_id for e, _ in self.queue[-p:]]
def load_pending(self):
"""Queue the pending downloads."""
loaded_pending_ids = config[config.SYSTEM].get('pending_ids', [])
for episode_id in loaded_pending_ids:
main_window = self.episodes_widget.main_window
try:
episode = main_window.programs_data[episode_id]
except KeyError:
logger.debug("Tried to load pending %r, didn't find it",
episode_id)
else:
main_window.queue_download(episode)
class HTMLDelegate(QStyledItemDelegate):
"""Custom delegate so the QTreeWidget can do HTML.
This is an adaptation of a post here:
http://stackoverflow.com/questions/10924175/how-do-i-use-a-
qstyleditemdelegate-to-paint-only-the-background-without-coverin
We only need to do background highlighting, so probably this will be
trimmed as much as possible for performance reasons.
Also, we only do HTML for one column, the rest is delegated to parent.
"""
def __init__(self, parent, html_column):
self._html_column = html_column
QStyledItemDelegate.__init__(self, parent)
def paint(self, painter, option, index):
"""Render the delegate for the item."""
if index.column() != self._html_column:
return QStyledItemDelegate.paint(self, painter, option, index)
options = QStyleOptionViewItemV4(option)
self.initStyleOption(options, index)
if options.widget is None:
style = QApplication.style()
else:
style = options.widget.style()
doc = QTextDocument()
doc.setHtml(options.text)
options.text = ""
style.drawControl(QStyle.CE_ItemViewItem, options, painter)
ctx = QAbstractTextDocumentLayout.PaintContext()
textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options)
painter.save()
painter.translate(textRect.topLeft())
painter.setClipRect(textRect.translated(-textRect.topLeft()))
doc.documentLayout().draw(painter, ctx)
painter.restore()
def sizeHint(self, option, index):
"""Calculate the needed size."""
options = QStyleOptionViewItemV4(option)
self.initStyleOption(options, index)
doc = QTextDocument()
doc.setHtml(options.text)
doc.setTextWidth(options.rect.width())
return QSize(doc.idealWidth(), doc.size().height())
class EpisodesWidget(remembering.RememberingTreeWidget):
"""The list of episodes info."""
_row_getter = operator.attrgetter('channel', 'section',
'title', 'duration')
_title_column = 2
def __init__(self, main_window, episode_info):
self.main_window = main_window
self.episode_info = episode_info
super(EpisodesWidget, self).__init__('episodes')
self.setMinimumSize(600, 300)
self.setItemDelegate(HTMLDelegate(self, self._title_column))
_headers = (u"Canal", u"Sección", u"Título", u"Duración [min]")
self.setColumnCount(len(_headers))
self.setHeaderLabels(_headers)
header = self.header()
header.setStretchLastSection(False)
header.setResizeMode(2, header.Stretch)
episodes = list(self.main_window.programs_data.values())
self._item_map = {}
for e in episodes:
item = QTreeWidgetItem([unicode(v) for v in self._row_getter(e)])
item.episode_id = e.episode_id
item.setTextAlignment(3, Qt.AlignRight)
self._item_map[e.episode_id] = item
self.addTopLevelItem(item)
self.set_color(e)
# enable sorting
self.setSortingEnabled(True)
self.setSelectionMode(QAbstractItemView.ExtendedSelection)
# connect the signals
self.clicked.connect(self.on_signal_clicked)
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.on_right_button)
self.itemDoubleClicked.connect(self.on_double_click)
def show(self, episode_id):
"""Show the row for the requested episode."""
item = self._item_map[episode_id]
self.setCurrentItem(item)
self._adjust_gui(episode_id)
def on_signal_clicked(self, _):
"""The view was clicked."""
item = self.currentItem()
self._adjust_gui(item.episode_id)
def on_double_click(self, item, col):
"""Double click."""
episode = self.main_window.programs_data[item.episode_id]
logger.debug("Double click in %s", episode)
if episode.state == Status.downloaded:
self.main_window.play_episode(episode)
elif episode.state == Status.none:
if self.main_window.have_config():
self.main_window.queue_download(episode)
else:
logger.debug("Not starting download because no config.")
t = (u"No se puede arrancar una descarga porque la "
u"configuración está incompleta.")
self.main_window.show_message(u'Falta configuración', t)
def _adjust_gui(self, episode_id):
"""Adjust the rest of the GUI for this episode."""
episode = self.main_window.programs_data[episode_id]
self.episode_info.update(episode)
self.main_window.check_download_play_buttons()
def on_right_button(self, point):
"""Right button was pressed, build a menu."""
item = self.currentItem()
self._adjust_gui(item.episode_id)
episode = self.main_window.programs_data[item.episode_id]
menu = QMenu()
mw = self.main_window
act_play = menu.addAction(u"&Reproducir",
lambda: mw.play_episode(episode))
act_cancel = menu.addAction(u"&Cancelar descarga",
lambda: mw.cancel_download(episode))
act_download = menu.addAction(u"&Descargar",
lambda: mw.queue_download(episode))
# set menu options according status
state = episode.state
if state == Status.downloaded:
act_play.setEnabled(True)
act_cancel.setEnabled(False)
act_download.setEnabled(False)
elif state == Status.downloading or state == Status.waiting:
act_play.setEnabled(False)
act_cancel.setEnabled(True)
act_download.setEnabled(False)
elif state == Status.none:
act_play.setEnabled(False)
act_cancel.setEnabled(False)
if self.main_window.have_config():
act_download.setEnabled(True)
else:
act_download.setEnabled(False)
menu.exec_(self.viewport().mapToGlobal(point))
def update_episode(self, episode):
"""Update episode with new info"""
item = self._item_map[episode.episode_id]
for i, v in enumerate(self._row_getter(episode)):
item.setText(i, unicode(v))
def add_episode(self, episode):
"""Update episode with new info"""
item = QTreeWidgetItem([unicode(v) for v in self._row_getter(episode)])
item.episode_id = episode.episode_id
item.setTextAlignment(3, Qt.AlignRight)
self._item_map[episode.episode_id] = item
self.set_color(episode)
self.addTopLevelItem(item)
def set_color(self, episode):
"""Set the background color for an episode (if needed)."""
if episode.state == Status.downloaded:
color = QColor("light green")
else:
palette = self.palette()
color = palette.color(QPalette.AlternateBase)
item = self._item_map[episode.episode_id]
for i in xrange(item.columnCount()):
item.setBackgroundColor(i, color)
def set_filter(self, text, only_downloaded=False):
"""Apply a filter to the episodes list."""
for episode_id, item in self._item_map.iteritems():
episode = self.main_window.programs_data[episode_id]
params = episode.filter_params(text, only_downloaded)
if params is None:
item.setHidden(True)
else:
item.setHidden(False)
pos1, pos2 = params
if pos1 is None:
# no highlighting
item.setText(self._title_column, episode.title)
else:
# filtering by text, so highlight
t = episode.title
ht = u''.join((t[:pos1],
'<span style="background-color:yellow">',
t[pos1:pos2], '</span>', t[pos2:]))
item.setText(self._title_column, ht)
# clear the selection to get consistent behaviour (because otherwise
# something will keep selected when widening the filter, or nothing
# will be selected when the filter gets more restrict)
self.clearSelection()
# now nothing is selected, so clear the episode info
self.episode_info.clear()
class EpisodeInfo(QWidget):
"""Show the episode at the right."""
def __init__(self, main_window):
self.main_window = main_window
super(EpisodeInfo, self).__init__()
self.current_episode = None
layout = QVBoxLayout(self)
# a throbber, that we don't initially show
self.throbber = Throbber()
layout.addWidget(self.throbber)
self.throbber.hide()
# the image and its getter
self.image_episode = QLabel()
self.image_episode.hide()
layout.addWidget(self.image_episode, alignment=Qt.AlignCenter)
self.get_image = image.ImageGetter(self.image_episode_loaded).get_image
# text area
self.text_edit = QTextEdit(
u"Seleccionar un programa para ver aquí la info.")
self.text_edit.setReadOnly(True)
layout.addWidget(self.text_edit)
# the button
self.button = QPushButton()
self.button.connected = False
self.button.hide()
layout.addWidget(self.button)
def image_episode_loaded(self, episode_id, image_path):
"""An image has arrived, show it only if the path is correct."""
# only set the image if the user still have the same episode selected
if self.current_episode != episode_id:
return
# load the image and show it
pixmap = QPixmap(image_path)
self.image_episode.setPixmap(pixmap)
self.image_episode.show()
# hide the throbber
self.throbber.hide()
def clear(self):
"""Clear the episode info panel."""
self.throbber.hide()
self.image_episode.hide()
msg = u"Seleccionar un programa para ver aquí la info."
self.text_edit.setText(msg)
self.button.hide()
def update(self, episode):
"""Update all the episode info."""
self.current_episode = episode.episode_id
# image
if episode.image_url is not None:
# this must be before the get_image call, as it may call
# immediately to image_episode_loaded (showing the image and
# hiding the throber)
self.image_episode.hide()
self.throbber.show()
# now do call the get_image
self.get_image(episode.episode_id,
episode.image_url.encode('utf-8'))
# all description
msg = "<center><h3>%s</h3></center><br/><br/>%s" % (
episode.title, episode.description)
self.text_edit.setHtml(msg)
# action button
self.button.show()
if episode.state == data.Status.downloaded:
label = "Reproducir"
func = self.main_window.play_episode
enable = True
elif (episode.state == data.Status.downloading or
episode.state == data.Status.waiting):
label = u"Cancelar descarga"
func = self.main_window.cancel_download
enable = True
else:
label = u"Descargar"
func = self.main_window.download_episode
enable = bool(self.main_window.have_config())
def _exec(func, episode):
"""Execute a function on the episode and update its info."""
func(episode)
self.update(episode)
# set button text, disconnect if should, and connect new func
self.button.setEnabled(enable)
self.button.setText(label)
if self.button.connected:
self.button.clicked.disconnect()
self.button.connected = True
self.button.clicked.connect(lambda: _exec(func, episode))
class BigPanel(QWidget):
"""The big panel for the main interface with user."""
def __init__(self, main_window):
super(BigPanel, self).__init__()
self.main_window = main_window
layout = QHBoxLayout(self)
# get this before, as it be used when creating other sutff
episode_info = EpisodeInfo(main_window)
self.episodes = EpisodesWidget(main_window, episode_info)
# split on the right
right_split = remembering.RememberingSplitter(Qt.Vertical, 'right')
right_split.addWidget(episode_info)
self.downloads_widget = DownloadsWidget(self.episodes)
right_split.addWidget(self.downloads_widget)
# main split
main_split = remembering.RememberingSplitter(Qt.Horizontal, 'main')
main_split.addWidget(self.episodes)
main_split.addWidget(right_split)
layout.addWidget(main_split)
|