Source

noor / noorgtk.py

#!/usr/bin/env python
import ConfigParser
import sys

import gtk
import pango

import noor.uihelpers


def write_aya(text, number=None, sajda=None, translation=None, juz_start=None):
    result = []
    if juz_start is not None:
        result.append('<span foreground="black" background="green"><big>%s'
                      '</big></span>\n' % str(juz_start).ljust(2))
    result.append('<big>%s</big>' % text)
    # handling aya sign
    if number is not None:
        sign = str(number)
        if sajda == 'minor':
            sign = '<span foreground="red">*%s*</span>' % sign
        if sajda == 'major':
            sign = '<span foreground="red">**%s**</span>' % sign
        result.append('<span foreground="blue">[%s]</span>\n' % sign)
    else:
        result.append('\n')
    if translation is not None:
        result.append('<small>%s</small>\n' % translation)
    result.append('\n')
    return ''.join(result)


class NoorDialog(object):

    def __init__(self, config, title, text):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_title(title)
        self.window.set_border_width(1)
        self.window.set_size_request(800, 600)
        self.window.connect('destroy', self._quit)

        content = gtk.Label()
        content.set_line_wrap(True)
        content.modify_font(_get_font_desc(config))
        content.set_markup(text)

        self.sura_window = gtk.ScrolledWindow()
        self.sura_window.add_with_viewport(content)
        self.sura_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
        self.sura_window.connect('key_press_event', self._key_pressed)
        #self.window.vbox.pack_start(self.sura_window, True, True, 0)
        self.window.add(self.sura_window)
        self.adjustment = self.sura_window.get_vscrollbar().get_adjustment()

    def _key_pressed(self, widget, event, data=None):
        if event.type == gtk.gdk.KEY_PRESS:
            if 0 <= event.keyval < 256:
                key = chr(event.keyval)
            else:
                return
            if event.state & gtk.gdk.CONTROL_MASK:
                if key == 'v':
                    self._next_page()
                if key == 'n':
                    self._next_line()
                if key == 'p':
                    self._prev_line()
                if key == 'a':
                    self._start()
                if key == 'e':
                    self._end()
            elif event.state & gtk.gdk.MOD1_MASK:
                if key == 'v':
                    self._prev_page()
            elif event.state & gtk.gdk.SHIFT_MASK:
                if key == ' ':
                    self._prev_page()
            else:
                if key == 'j':
                    self._next_line()
                if key == 'k':
                    self._prev_line()
                if key == ' ':
                    self._next_page()
                if key == 'q':
                    self._quit()

    def _adjust_page(self, increment):
        new_value = self.adjustment.value + increment
        newvalue = max(self.adjustment.lower,
                       min(new_value, self.adjustment.upper
                           - self.adjustment.page_size))
        self.adjustment.set_value(newvalue)

    def _prev_line(self):
        self._adjust_page(-self.adjustment.step_increment)

    def _next_line(self):
        self._adjust_page(self.adjustment.step_increment)

    def _prev_page(self):
        self._adjust_page(-self.adjustment.page_increment)

    def _next_page(self):
        self._adjust_page(self.adjustment.page_increment)

    def _start(self):
        self._adjust_page(self.adjustment.lower - self.adjustment.value)

    def _end(self):
        self._adjust_page(self.adjustment.upper - self.adjustment.value
                          - self.adjustment.page_size)

    def _quit(self, *args):
        gtk.main_quit()

    def run(self):
        self.window.show_all()
        gtk.main()


class SurasDialog(object):

    def __init__(self, config, quran):
        self.dialog = gtk.Dialog()
        self.dialog.set_title('Noor - Choose Sura')
        self.dialog.set_border_width(1)
        self.dialog.set_size_request(400, 200)
        self.dialog.connect('destroy', self._quit)

        l = gtk.Label()
        l.set_markup('Choose a sura to display:')
        self.combobox = gtk.combo_box_new_text()
        self.combobox.modify_font(_get_font_desc(config))
        for index, sura in enumerate(quran.suras):
            self.combobox.append_text('%d - %s' % (index + 1, sura.name))
        self.combobox.set_active(0)

        button = gtk.Button()
        button.connect('clicked', self._selected)
        button_label = gtk.Label()
        button_label.set_markup('Choose')
        button.add(button_label)

        self.dialog.vbox.pack_start(l, True, True, 0)
        self.dialog.vbox.pack_start(self.combobox, True, True, 0)
        self.dialog.vbox.pack_start(button, True, True, 0)
        self.selected = False

    def _quit(self, *args):
        gtk.main_quit()

    def _selected(self, *args):
        self.selected = True
        self._quit()

    def run(self):
        self.dialog.show_all()
        gtk.main()

    def get_sura(self):
        if self.selected:
            return self.combobox.get_active() + 1


def _get_font_desc(config):
    try:
        font = config.get('gtk', 'font')
    except ConfigParser.Error:
        font = 'nazli 19'
    return pango.FontDescription(font)


def ask_sura(config, quran):
    dialog = SurasDialog(config, quran)
    dialog.run()
    return dialog.get_sura()

def show(sura=None):
    quran = noor.uihelpers.get_quran()
    config = noor.uihelpers.get_config()
    if sura is None:
        sura = ask_sura(config, quran)
        if sura is None:
            return
    trans = noor.uihelpers.get_translation(config)
    title = 'Noor - ' + str(sura) + '. ' + quran.suras[sura - 1].name
    NoorDialog(config, title, text(quran, trans, write_aya, sura)).run()

def text(quran, trans, write_aya, sura):
    return noor.uihelpers.write_sura(quran, sura, write_aya, trans)

if __name__ == '__main__':
    if len(sys.argv) == 1:
        show()
    else:
        if not sys.argv[1].isdigit():
            print 'Usage: %s [sura_number]' % sys.argv[0]
        else:
            show(int(sys.argv[1]))