Skip to content

Instantly share code, notes, and snippets.

@abidibo
Last active December 29, 2015 07:39
Show Gist options
  • Save abidibo/7638039 to your computer and use it in GitHub Desktop.
Save abidibo/7638039 to your computer and use it in GitHub Desktop.
awrgui
#! /usr/bin/python
from gi.repository import Gtk, Gdk, GObject
from agtk import MainWindow
"""
@brief AWR Graphic User Interface
"""
class AWRGUI:
"""
@brief Constructor
@param AWR app the main application instance
"""
def __init__(self, app):
self._app = app
self._active_radio = None
self._style = 'light';
# main window
self._win = MainWindow('main_window', 'AWR', self._app.kill_proc)
self._win.set_resizable(False)
# main container
self.create_container()
# track and controllers
self.create_controlbar()
# notebook
self.create_notebook()
# footer
self.create_footer()
# style
self.style_provider = Gtk.CssProvider()
self.style_provider.load_from_path('./css/style-%s.css' % self._style)
screen = Gdk.Screen.get_default()
styleContext = Gtk.StyleContext()
styleContext.add_provider_for_screen(screen, self.style_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER)
self._win.show_all()
"""
Main window widget getter
"""
def get_win(self):
return self._win
"""
@brief Creates the external container
"""
def create_container(self):
self._container = Gtk.Box(name='main_container', spacing=5, orientation=Gtk.Orientation.VERTICAL, homogeneous=False, margin=10)
self._win.add(self._container)
"""
@brief Creates the controller top bar
@description displays current track and stop, play, pause controllers
"""
def create_controlbar(self):
controlbar_box = Gtk.Box(spacing=5)
controlbar_box.get_style_context().add_class("ctrlbar");
self._container.pack_start(controlbar_box, False, False, 0)
# stop button
self._stop_button = Gtk.Button(stock=Gtk.STOCK_MEDIA_STOP)
self._stop_button.connect('clicked', self._app.stop_stream)
controlbar_box.pack_start(self._stop_button, False, False, 0)
# playpause button
self._playpause_button = Gtk.Button(stock=Gtk.STOCK_MEDIA_PAUSE)
self._playpause_button.connect('clicked', self._app.playpause_stream)
controlbar_box.pack_start(self._playpause_button, False, False, 0)
# onair label
onair_label = Gtk.Label('on air')
controlbar_box.pack_start(onair_label, False, False, 0)
# track label (changes dynamically)
self._track_label = Gtk.Label('--')
self._track_label.get_style_context().add_class("evidence");
controlbar_box.pack_start(self._track_label, False, False, 0)
self.update()
"""
@brief Updates the controllers
"""
def update(self, track_title=None):
status = self._app.get_status()
self.update_stop_button(status)
self.update_playpause_button(status)
self.update_track_label(status, track_title)
if status == 'stopped':
self.unset_active_radio()
"""
@brief Updates the stop button
"""
def update_stop_button(self, status):
if status == 'stopped' or status == 'init':
self._stop_button.set_sensitive(False)
self._stop_button.get_style_context().add_class("button-disabled");
else:
self._stop_button.set_sensitive(True)
self._stop_button.get_style_context().remove_class("button-disabled");
"""
@brief Updates the play and pause buttons
"""
def update_playpause_button(self, status):
if status == 'stopped' or status == 'init':
self._playpause_button.set_sensitive(False)
self._playpause_button.set_label(Gtk.STOCK_MEDIA_PAUSE)
self._playpause_button.get_style_context().add_class("button-disabled");
elif status == 'playing':
self._playpause_button.set_sensitive(True)
self._playpause_button.set_label(Gtk.STOCK_MEDIA_PAUSE)
self._playpause_button.get_style_context().remove_class("button-disabled");
else:
self._playpause_button.set_label(Gtk.STOCK_MEDIA_PLAY)
def update_track_label(self, status, track_title):
if status == 'stopped':
self.update_track('--')
elif track_title:
self.update_track(track_title)
"""
@brief Creates the genres notebook
"""
def create_notebook(self):
json_data = open('./conf/radios.json')
data = json.load(json_data)
notebook = Gtk.Notebook()
self._container.pack_start(notebook, False, False, 0)
for genre in data['genres']:
genre_label = Gtk.Label(genre['name']);
genre_table = self.construct_genre_page(genre);
notebook.append_page(genre_table, genre_label);
"""
@brief Creates a genre page
"""
def construct_genre_page(self, genre):
num_radios = len(genre['radios'])
grid = Gtk.Grid(row_spacing=10, margin=10)
i = 0
for radio in genre['radios']:
img_button = Gtk.Button(image=Gtk.Image.new_from_file(radio['img']))
img_button.get_style_context().add_class("button-img");
img_button.set_vexpand(False)
img_button.connect('clicked', self._app.stream_radio, radio)
label = Gtk.Label(use_markup=True, xalign=0, margin_left=5)
label.set_line_wrap(True)
label.set_markup('<b>%s</b>\n%s' % (radio['name'], radio['description']))
grid.attach(img_button, 0, i, 1, 1)
grid.attach_next_to(label, img_button, Gtk.PositionType.RIGHT, 1, 1)
i = i + 1
return grid
"""
Creates the application footer
"""
def create_footer(self):
abidibo_container = Gtk.EventBox()
abidibo = Gtk.Image.new_from_file('abidibo.png')
abidibo.set_property('xalign', 1)
abidibo_container.add(abidibo)
abidibo_container.connect('button_press_event', self.toggle_style)
self._container.pack_start(abidibo_container, False, False, 0)
"""
Sets the active radio widget
"""
def set_active_radio(self, widget):
self.unset_active_radio()
self._active_radio = widget
self._active_radio.get_style_context().add_class("button-selected");
"""
Unsets the active radio widget
"""
def unset_active_radio(self):
if self._active_radio:
self._active_radio.get_style_context().remove_class("button-selected");
self._active_radio = None
"""
@brief Updates the current track
"""
def update_track(self, title):
self._track_label.set_markup(title)
"""
@brief Toggles between light and dark styles
"""
def toggle_style(self, widget, event):
self._style = 'dark' if self._style == 'light' else 'light'
self.style_provider.load_from_path('./css/style-%s.css' % self._style)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment