~facundo/encuentro/trunk

« back to all changes in this revision

Viewing changes to encuentro/ui/preferences.py

  • Committer: Facundo Batista
  • Date: 2013-03-27 02:39:36 UTC
  • mfrom: (151.1.22 qtui)
  • mto: (151.2.2 trunk)
  • mto: This revision was merged to the branch mainline in revision 153.
  • Revision ID: facundo@taniquetil.com.ar-20130327023936-leainarptqpt7v7c
We do Qt now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf8 -*-
 
2
# FIXME: header y eso
 
3
 
 
4
import os
 
5
import pickle
 
6
import sys
 
7
import logging
 
8
 
 
9
from PyQt4.QtGui import (
 
10
    QCheckBox,
 
11
    QDialog,
 
12
    QDialogButtonBox,
 
13
    QGridLayout,
 
14
    QHBoxLayout,
 
15
    QLabel,
 
16
    QLineEdit,
 
17
    QTabWidget,
 
18
    QVBoxLayout,
 
19
    QWidget,
 
20
)
 
21
from PyQt4.QtCore import Qt
 
22
 
 
23
from encuentro import platform
 
24
 
 
25
logger = logging.getLogger('encuentro.preferences')
 
26
 
 
27
URL_CONECTATE = (
 
28
    "http://registro.educ.ar/cuentas/registro/index?servicio=conectate"
 
29
)
 
30
 
 
31
 
 
32
 
 
33
class GeneralPreferences(QWidget):
 
34
    """The general preferences input."""
 
35
    def __init__(self, config_data):
 
36
        super(GeneralPreferences, self).__init__()
 
37
        grid = QGridLayout(self)
 
38
        grid.setSpacing(20)
 
39
 
 
40
        l = QLabel(
 
41
                u"<b>Ingresá el directorio donde descargar los videos...</b>")
 
42
        l.setTextFormat(Qt.RichText)
 
43
        grid.addWidget(l, 0, 0, 1, 2)
 
44
 
 
45
        grid.addWidget(QLabel(u"Descargar en:"), 1, 0, 2, 1)
 
46
        prv = config_data.get('downloaddir', '')
 
47
        self.downloaddir_entry = QLineEdit(prv)
 
48
        grid.addWidget(self.downloaddir_entry, 1, 1, 2, 2)
 
49
        # FIXME: the text entry is too separated of the "descarga en" subtitle,
 
50
        # it should automatically use more of the width
 
51
 
 
52
        self.autoreload_checkbox = QCheckBox(
 
53
                u"Recargar automáticamente la lista de episodios al iniciar")
 
54
        prv = config_data.get('autorefresh', False)
 
55
        self.autoreload_checkbox.setChecked(prv)
 
56
        grid.addWidget(self.autoreload_checkbox, 2, 0, 3, 2)
 
57
 
 
58
        self.shownotifs_checkbox = QCheckBox(
 
59
                u"Mostrar una notificación cuando termina cada descarga")
 
60
        prv = config_data.get('notification', True)
 
61
        self.shownotifs_checkbox.setChecked(prv)
 
62
        grid.addWidget(self.shownotifs_checkbox, 3, 0, 4, 2)
 
63
 
 
64
    def get_config(self):
 
65
        """Return the config for this tab."""
 
66
        d = {}
 
67
        d['downloaddir'] = self.downloaddir_entry.text()
 
68
        d['autorefresh'] = self.autoreload_checkbox.isChecked()
 
69
        d['notification'] = self.shownotifs_checkbox.isChecked()
 
70
        return d
 
71
 
 
72
 
 
73
class ConectatePreferences(QWidget):
 
74
    """The preferences for Conectate backend."""
 
75
    def __init__(self, config_data):
 
76
        super(ConectatePreferences, self).__init__()
 
77
        grid = QGridLayout(self)
 
78
        grid.setSpacing(20)
 
79
 
 
80
        l = QLabel(u"<b>Ingresá tus datos del portal Conectate:</b>")
 
81
        l.setTextFormat(Qt.RichText)
 
82
        grid.addWidget(l, 0, 0, 1, 2)
 
83
 
 
84
        grid.addWidget(QLabel(u"Usuario:"), 1, 0, 2, 1)
 
85
        prv = config_data.get('user', '')
 
86
        self.user_entry = QLineEdit(prv)
 
87
        grid.addWidget(self.user_entry, 1, 1, 2, 2)
 
88
        # FIXME: the text entry is too separated of the "descarga en" subtitle,
 
89
        # it should automatically use more of the width
 
90
 
 
91
        grid.addWidget(QLabel(u"Contraseña:"), 2, 0, 3, 1)
 
92
        prv = config_data.get('password', '')
 
93
        self.password_entry = QLineEdit(prv)
 
94
        grid.addWidget(self.password_entry, 2, 1, 3, 2)
 
95
        # FIXME: the text entry is too separated of the "descarga en" subtitle,
 
96
        # it should automatically use more of the width
 
97
 
 
98
        l = QLabel(u'Si no tenés estos datos, <a href="%s">registrate aquí'
 
99
                   u'</a>' % (URL_CONECTATE,))
 
100
        # FIXME: make this link to automaticall open the browser with it
 
101
        l.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
 
102
        l.setTextFormat(Qt.RichText)
 
103
        l.setOpenExternalLinks(True)
 
104
        grid.addWidget(l, 3, 0, 4, 3)
 
105
 
 
106
    def get_config(self):
 
107
        """Return the config for this tab."""
 
108
        d = {}
 
109
        d['user'] = self.user_entry.text()
 
110
        d['password'] = self.password_entry.text()
 
111
        return d
 
112
 
 
113
 
 
114
class PreferencesDialog(QDialog):
 
115
    """The dialog for preferences."""
 
116
    def __init__(self):
 
117
        super(PreferencesDialog, self).__init__()
 
118
        vbox = QVBoxLayout(self)
 
119
        self._config_file = os.path.join(platform.config_dir, 'encuentro.conf')
 
120
        if os.path.exists(self._config_file):
 
121
            with open(self._config_file, 'rb') as fh:
 
122
                self.config_data = pickle.load(fh)
 
123
        else:
 
124
            self.config_data = {}
 
125
 
 
126
        tabbed = QTabWidget()
 
127
        self.gp = GeneralPreferences(self.config_data)
 
128
        tabbed.addTab(self.gp, u"General")
 
129
        self.cp = ConectatePreferences(self.config_data)
 
130
        tabbed.addTab(self.cp, u"Conectate")
 
131
        vbox.addWidget(tabbed)
 
132
 
 
133
        bbox = QDialogButtonBox(QDialogButtonBox.Ok)
 
134
        bbox.accepted.connect(self.accept)
 
135
        vbox.addWidget(bbox)
 
136
 
 
137
    def save_config(self):
 
138
        """Save all config."""
 
139
        # get it from tabs
 
140
        new_cfg = {}
 
141
        new_cfg.update(self.gp.get_config())
 
142
        new_cfg.update(self.cp.get_config())
 
143
 
 
144
        # save it!
 
145
        logger.info("Updating preferences config: %s", new_cfg)
 
146
        with open(self._config_file, 'wb') as fh:
 
147
            pickle.dump(new_cfg, fh)
 
148
 
 
149
 
 
150
if __name__ == '__main__':
 
151
 
 
152
    project_basedir = os.path.abspath(os.path.dirname(os.path.dirname(
 
153
                                            os.path.realpath(sys.argv[0]))))
 
154
    sys.path.insert(0, project_basedir)
 
155
 
 
156
    from PyQt4.QtGui import QApplication
 
157
    app = QApplication(sys.argv)
 
158
 
 
159
    frame = PreferencesDialog()
 
160
    frame.show()
 
161
    frame.exec_()
 
162
    frame.save_config()