~umang/indicator-stickynotes/trunk

« back to all changes in this revision

Viewing changes to stickynotes/gui.py

  • Committer: Umang Varma
  • Date: 2015-06-04 17:29:32 UTC
  • Revision ID: git-v1:fec51493cdd3e317640597f70eeb492411886cdd
Always raise window on creation (solves new note not appearing)

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
        self.populate_menu()
47
47
 
48
48
        # Load CSS template and initialize Gtk.CssProvider
49
 
        with open(os.path.join(self.path, "style.css"), encoding="utf-8") \
50
 
                as css_file:
 
49
        with open(os.path.join(self.path, "style.css")) as css_file:
51
50
            self.css_template = Template(css_file.read())
52
51
        self.css = Gtk.CssProvider()
53
52
 
64
63
        # Get necessary objects
65
64
        self.winMain.set_name("main-window")
66
65
        widgets = ["txtNote", "bAdd", "imgAdd", "imgResizeR", "eResizeR",
67
 
                "bLock", "imgLock", "imgUnlock", "imgClose", "imgDropdown",
68
 
                "bClose", "confirmDelete", "movebox1", "movebox2"]
 
66
                "bLock", "imgLock", "imgUnlock", "bClose", "confirmDelete"]
69
67
        for w in widgets:
70
68
            setattr(self, w, self.builder.get_object(w))
71
69
        self.style_contexts = [self.winMain.get_style_context(),
119
117
 
120
118
    # workaround which is based on deleting a sticky note and re-initializing
121
119
    # it. 
122
 
    def show(self, widget=None, event=None, reload_from_backend=False):
 
120
    def show(self, widget=None, event=None):
123
121
        """Shows the stickynotes window"""
124
122
 
125
 
        # don't overwrite settings if loading from backend
126
 
        if not reload_from_backend:
127
 
            # store sticky note's settings
128
 
            self.update_note()
129
 
        else:
130
 
            # Categories may have changed in backend
131
 
            self.populate_menu()
 
123
        # store sticky note's settings
 
124
        self.update_note()
132
125
 
133
126
        # destroy its main window
134
127
        self.winMain.destroy()
176
169
 
177
170
    def update_style(self):
178
171
        """Updates the style using CSS template"""
179
 
        self.update_button_color()
180
172
        css_string = self.css_template.substitute(**self.css_data())\
181
173
                .encode("ascii", "replace")
182
174
        self.css.load_from_data(css_string)
184
176
            context.add_provider(self.css,
185
177
                    Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
186
178
 
187
 
    def update_button_color(self):
188
 
        """Switches between regular and dark icons appropriately"""
189
 
        lightness = sum(colorsys.hsv_to_rgb(*self.note.cat_prop("bgcolor_hsv")))
190
 
        suffix = "-dark" if lightness <= 1 else ""
191
 
        iconfiles = {"imgAdd":"add", "imgClose":"close", "imgDropdown":"menu",
192
 
                "imgLock":"lock", "imgUnlock":"unlock", "imgResizeR":"resizer"}
193
 
        for img, filename in iconfiles.items():
194
 
            getattr(self, img).set_from_file("Icons/" + filename + suffix +
195
 
                    ".png")
196
 
 
197
179
    def css_data(self):
198
180
        """Returns data to substitute into the CSS template"""
199
181
        data = {}
201
183
        rgb_to_hex = lambda x: "#" + "".join(["{:02x}".format(int(255*a))
202
184
            for a in x])
203
185
        hsv_to_hex = lambda x: rgb_to_hex(colorsys.hsv_to_rgb(*x))
204
 
        bgcolor_hsv = self.note.cat_prop("bgcolor_hsv")
205
 
        data["bgcolor_hex"] = hsv_to_hex(
206
 
                self.note.cat_prop("bgcolor_hsv"))
 
186
        bg_end_hsv = self.note.cat_prop("bgcolor_hsv")
 
187
        shadow_amount = self.note.cat_prop("shadow")/100.0
 
188
        # bg_start_hsv is computed by "lightening" bg_end_hsv. 
 
189
        bg_start_hsv = [bg_end_hsv[0], bg_end_hsv[1],
 
190
                bg_end_hsv[2] + shadow_amount]
 
191
        if bg_start_hsv[2] > 1:
 
192
            bg_start_hsv[1] -= bg_start_hsv[2] - 1
 
193
            bg_start_hsv[2] = 1
 
194
        if bg_start_hsv[1] < 0:
 
195
            bg_start_hsv[1] = 0
 
196
        data.update({"bg_start": hsv_to_hex(bg_start_hsv), "bg_end":
 
197
                hsv_to_hex(bg_end_hsv)})
207
198
        data["text_color"] = rgb_to_hex(self.note.cat_prop("textcolor"))
208
199
        return data
209
200
 
257
248
        return False
258
249
 
259
250
    def delete(self, *args):
260
 
        winConfirm = Gtk.MessageDialog(self.winMain, None,
261
 
                Gtk.MessageType.QUESTION, Gtk.ButtonsType.NONE,
262
 
                _("Are you sure you want to delete this note?"))
263
 
        winConfirm.add_buttons(Gtk.STOCK_CANCEL, Gtk.ResponseType.REJECT,
264
 
                Gtk.STOCK_DELETE, Gtk.ResponseType.ACCEPT)
265
 
        confirm = winConfirm.run()
266
 
        winConfirm.destroy()
267
 
        if confirm == Gtk.ResponseType.ACCEPT:
 
251
        confirm = self.confirmDelete.run()
 
252
        self.confirmDelete.hide()
 
253
        if confirm == 1:
268
254
            self.note.delete()
269
255
            self.winMain.destroy()
270
256
            return False
321
307
        self.path = os.path.abspath(os.path.join(os.path.dirname(__file__),
322
308
            '..'))
323
309
        self.builder.add_objects_from_file(os.path.join(self.path,
324
 
            "SettingsCategory.glade"), ["catExpander"])
 
310
            "SettingsCategory.glade"), ["catExpander", "confirmDelete", "adjShadow"])
325
311
        self.builder.connect_signals(self)
326
312
        widgets = ["catExpander", "lExp", "cbBG", "cbText", "eName",
327
 
                "confirmDelete", "fbFont"]
 
313
                "confirmDelete", "fbFont", "scShadow"]
328
314
        for w in widgets:
329
315
            setattr(self, w, self.builder.get_object(w))
330
316
        name = self.noteset.categories[cat].get("name", _("New Category"))
344
330
                    .get_font(Gtk.StateFlags.NORMAL).to_string()
345
331
                #why.is.this.so.long?
346
332
        self.fbFont.set_font(fontname)
 
333
        self.scShadow.set_value(
 
334
                self.noteset.get_category_property(cat, "shadow"))
347
335
 
348
336
    def refresh_title(self, *args):
349
337
        """Updates the title of the category"""
355
343
 
356
344
    def delete_cat(self, *args):
357
345
        """Delete a category"""
358
 
        winConfirm = Gtk.MessageDialog(self.settingsdialog.wSettings, None,
359
 
                Gtk.MessageType.QUESTION, Gtk.ButtonsType.NONE,
360
 
                _("Are you sure you want to delete this category?"))
361
 
        winConfirm.add_buttons(Gtk.STOCK_CANCEL, Gtk.ResponseType.REJECT,
362
 
                Gtk.STOCK_DELETE, Gtk.ResponseType.ACCEPT)
363
 
        confirm = winConfirm.run()
364
 
        winConfirm.destroy()
365
 
        if confirm == Gtk.ResponseType.ACCEPT:
 
346
        confirm = self.confirmDelete.run()
 
347
        self.confirmDelete.hide()
 
348
        if confirm == 1:
366
349
            self.settingsdialog.delete_category(self.cat)
367
350
 
368
351
    def make_default(self, *args):
376
359
    def eName_changed(self, *args):
377
360
        """Update a category name"""
378
361
        self.noteset.categories[self.cat]["name"] = self.eName.get_text()
379
 
        self.refresh_title()
 
362
        self.lExp.set_text(self.eName.get_text())
380
363
        for note in self.noteset.notes:
381
364
            note.gui.populate_menu()
382
365
 
393
376
        self.noteset.categories[self.cat]["bgcolor_hsv"] = hsv
394
377
        for note in self.noteset.notes:
395
378
            note.gui.update_style()
396
 
        # Remind some widgets that they are transparent, etc.
 
379
        # Remind GtkSourceView's that they are transparent, etc.
397
380
        load_global_css()
398
381
 
399
382
    def update_textcolor(self, *args):
415
398
        for note in self.noteset.notes:
416
399
            note.gui.update_font()
417
400
 
 
401
    def update_shadow(self, *args):
 
402
        """Action to update the amount of shadow on the notes"""
 
403
        self.noteset.categories[self.cat]["shadow"] = self.scShadow.get_value()
 
404
        for note in self.noteset.notes:
 
405
            note.gui.update_style()
 
406
        # Remind GtkSourceView's that they are transparent, etc.
 
407
        load_global_css()
 
408
 
418
409
class SettingsDialog:
419
410
    """Manages the GUI of the settings dialog"""
420
411
    def __init__(self, noteset):