~umang/indicator-stickynotes/trunk

157 by Umang Varma
Updated copyright year and translations, bumped version
1
# Copyright © 2012-2018 Umang Varma <umang.me@gmail.com>
22 by Umang Varma
Move backend and gui to sticknotes package
2
# 
3
# This file is part of indicator-stickynotes.
4
# 
5
# indicator-stickynotes is free software: you can redistribute it and/or
6
# modify it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation, either version 3 of the License, or (at your
8
# option) any later version.
9
# 
10
# indicator-stickynotes is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13
# more details.
14
# 
15
# You should have received a copy of the GNU General Public License along with
16
# indicator-stickynotes.  If not, see <http://www.gnu.org/licenses/>.
17
18
from datetime import datetime
45 by Umang Varma
style.css is now a template, not the final code
19
from string import Template
148 by Umang Varma
gi.require_version for future-proofing
20
import gi
21
gi.require_version("Gtk", "3.0")
22
gi.require_version("GtkSource", "3.0")
84 by Umang Varma
Ability to change fonts
23
from gi.repository import Gtk, Gdk, Gio, GObject, GtkSource, Pango
40 by Umang Varma
Added keyboard shortcuts, tooltips for buttons
24
from locale import gettext as _
26 by Umang Varma
Made python code pwd agnostic (hopefully)
25
import os.path
46 by Umang Varma
Notes can now change color.
26
import colorsys
75 by Umang Varma
Add and delete category
27
import uuid
46 by Umang Varma
Notes can now change color.
28
29
def load_global_css():
68 by Umang Varma
Documented some methods.
30
    """Adds a provider for the global CSS"""
46 by Umang Varma
Notes can now change color.
31
    global_css = Gtk.CssProvider()
32
    global_css.load_from_path(os.path.join(os.path.dirname(__file__), "..",
33
        "style_global.css"))
34
    Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(),
35
            global_css, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
22 by Umang Varma
Move backend and gui to sticknotes package
36
45 by Umang Varma
style.css is now a template, not the final code
37
class StickyNote:
68 by Umang Varma
Documented some methods.
38
    """Manages the GUI of an individual stickynote"""
22 by Umang Varma
Move backend and gui to sticknotes package
39
    def __init__(self, note):
68 by Umang Varma
Documented some methods.
40
        """Initializes the stickynotes window"""
26 by Umang Varma
Made python code pwd agnostic (hopefully)
41
        self.path = os.path.abspath(os.path.join(os.path.dirname(__file__),
42
            '..'))
22 by Umang Varma
Move backend and gui to sticknotes package
43
        self.note = note
46 by Umang Varma
Notes can now change color.
44
        self.noteset = note.noteset
22 by Umang Varma
Move backend and gui to sticknotes package
45
        self.locked = self.note.properties.get("locked", False)
107 by Umang Varma
Patch for show desktop bug (submitted by Bram Kuijper)
46
47
        # Create menu
48
        self.menu = Gtk.Menu()
49
        self.populate_menu()
50
51
        # Load CSS template and initialize Gtk.CssProvider
125 by Umang Varma
Force utf-8 encoding while opening css file.
52
        with open(os.path.join(self.path, "style.css"), encoding="utf-8") \
53
                as css_file:
107 by Umang Varma
Patch for show desktop bug (submitted by Bram Kuijper)
54
            self.css_template = Template(css_file.read())
55
        self.css = Gtk.CssProvider()
56
57
        self.build_note()
58
        
59
    def build_note(self):
22 by Umang Varma
Move backend and gui to sticknotes package
60
        self.builder = Gtk.Builder()
61
        GObject.type_register(GtkSource.View)
26 by Umang Varma
Made python code pwd agnostic (hopefully)
62
        self.builder.add_from_file(os.path.join(self.path,
152 by Umang Varma
.glade -> .ui in code
63
            "StickyNotes.ui"))
22 by Umang Varma
Move backend and gui to sticknotes package
64
        self.builder.connect_signals(self)
107 by Umang Varma
Patch for show desktop bug (submitted by Bram Kuijper)
65
        self.winMain = self.builder.get_object("MainWindow")
66
22 by Umang Varma
Move backend and gui to sticknotes package
67
        # Get necessary objects
93 by Umang Varma
Simply getting widgets
68
        widgets = ["txtNote", "bAdd", "imgAdd", "imgResizeR", "eResizeR",
124 by Umang Varma
Switch to -dark icons if using dark background
69
                "bLock", "imgLock", "imgUnlock", "imgClose", "imgDropdown",
70
                "bClose", "confirmDelete", "movebox1", "movebox2"]
93 by Umang Varma
Simply getting widgets
71
        for w in widgets:
72
            setattr(self, w, self.builder.get_object(w))
51 by Umang Varma
Added text color editing support
73
        self.style_contexts = [self.winMain.get_style_context(),
74
                self.txtNote.get_style_context()]
46 by Umang Varma
Notes can now change color.
75
        # Update window-specific style. Global styles are loaded initially!
45 by Umang Varma
style.css is now a template, not the final code
76
        self.update_style()
84 by Umang Varma
Ability to change fonts
77
        self.update_font()
45 by Umang Varma
style.css is now a template, not the final code
78
        # Ensure buttons are displayed with images
22 by Umang Varma
Move backend and gui to sticknotes package
79
        settings = Gtk.Settings.get_default()
80
        settings.props.gtk_button_images = True
81
        # Set text buffer
82
        self.bbody = GtkSource.Buffer()
83
        self.bbody.begin_not_undoable_action()
84
        self.bbody.set_text(self.note.body)
85
        self.bbody.set_highlight_matching_brackets(False)
86
        self.bbody.end_not_undoable_action()
87
        self.txtNote.set_buffer(self.bbody)
88
        # Make resize work
89
        self.winMain.add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
90
        self.eResizeR.add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
91
        # Move Window
92
        self.winMain.move(*self.note.properties.get("position", (10,10)))
93
        self.winMain.resize(*self.note.properties.get("size", (200,150)))
105 by Umang Varma
Showing after resizing seems to be more consistent
94
        # Show the window
110 by Umang Varma
skip pager hint, so don't show in Unity Window Spread
95
        self.winMain.set_skip_pager_hint(True)
107 by Umang Varma
Patch for show desktop bug (submitted by Bram Kuijper)
96
        self.winMain.show_all()
22 by Umang Varma
Move backend and gui to sticknotes package
97
        # Mouse over
98
        self.eResizeR.get_window().set_cursor(Gdk.Cursor.new_for_display(
99
                    self.eResizeR.get_window().get_display(),
100
                    Gdk.CursorType.BOTTOM_RIGHT_CORNER))
101
        # Set locked state
102
        self.set_locked_state(self.locked)
103
116 by Umang Varma
Always raise window on creation (solves new note not appearing)
104
        # call set_keep_above just to have the note appearing
105
        # above everything else.
106
        # without it, it still won't appear above a window
107
        # in which a cursor is active
108
        self.winMain.set_keep_above(True)
109
110
        # immediately undo the set keep above after the window
111
        # is shown, so that windows won't stay up if we switch to
112
        # a different window
113
        self.winMain.set_keep_above(False)
114
115
107 by Umang Varma
Patch for show desktop bug (submitted by Bram Kuijper)
116
    # (re-)show the sticky note after it has been hidden getting a sticky note
117
    # to show itself was problematic after a "show desktop" command in unity.
118
    # (see bug lp:1105948).  Reappearance of dialog is problematic for any
152 by Umang Varma
.glade -> .ui in code
119
    # dialog which has the skip_taskbar_hint=True property in StickyNotes.ui
107 by Umang Varma
Patch for show desktop bug (submitted by Bram Kuijper)
120
    # (property necessary to prevent sticky note from showing on the taskbar)
121
122
    # workaround which is based on deleting a sticky note and re-initializing
123
    # it. 
128 by Umang Varma
Implement import/export feature
124
    def show(self, widget=None, event=None, reload_from_backend=False):
68 by Umang Varma
Documented some methods.
125
        """Shows the stickynotes window"""
107 by Umang Varma
Patch for show desktop bug (submitted by Bram Kuijper)
126
128 by Umang Varma
Implement import/export feature
127
        # don't overwrite settings if loading from backend
128
        if not reload_from_backend:
129
            # store sticky note's settings
130
            self.update_note()
131
        else:
132
            # Categories may have changed in backend
133
            self.populate_menu()
107 by Umang Varma
Patch for show desktop bug (submitted by Bram Kuijper)
134
135
        # destroy its main window
136
        self.winMain.destroy()
137
138
        # reinitialize that window
139
        self.build_note()
140
22 by Umang Varma
Move backend and gui to sticknotes package
141
    def hide(self, *args):
68 by Umang Varma
Documented some methods.
142
        """Hides the stickynotes window"""
22 by Umang Varma
Move backend and gui to sticknotes package
143
        self.winMain.hide()
144
145
    def update_note(self):
68 by Umang Varma
Documented some methods.
146
        """Update the underlying note object"""
22 by Umang Varma
Move backend and gui to sticknotes package
147
        self.note.update(self.bbody.get_text(self.bbody.get_start_iter(),
148
            self.bbody.get_end_iter(), True))
149
150
    def move(self, widget, event):
68 by Umang Varma
Documented some methods.
151
        """Action to begin moving (by dragging) the window"""
22 by Umang Varma
Move backend and gui to sticknotes package
152
        self.winMain.begin_move_drag(event.button, event.x_root,
153
                event.y_root, event.get_time())
154
        return False
155
156
    def resize(self, widget, event, *args):
68 by Umang Varma
Documented some methods.
157
        """Action to begin resizing (by dragging) the window"""
22 by Umang Varma
Move backend and gui to sticknotes package
158
        self.winMain.begin_resize_drag(Gdk.WindowEdge.SOUTH_EAST,
159
                event.button, event.x_root, event.y_root, event.get_time())
160
        return True
161
162
    def properties(self):
68 by Umang Varma
Documented some methods.
163
        """Get properties of the current note"""
22 by Umang Varma
Move backend and gui to sticknotes package
164
        prop = {"position":self.winMain.get_position(),
165
                "size":self.winMain.get_size(), "locked":self.locked}
166
        if not self.winMain.get_visible():
167
            prop["position"] = self.note.properties.get("position", (10, 10))
58 by Umang Varma
Remember hidden state on startup
168
            prop["size"] = self.note.properties.get("size", (200, 150))
22 by Umang Varma
Move backend and gui to sticknotes package
169
        return prop
170
84 by Umang Varma
Ability to change fonts
171
    def update_font(self):
172
        """Updates the font"""
86 by Umang Varma
Unset font before setting a new font
173
        # Unset any previously set font
174
        self.txtNote.override_font(None)
84 by Umang Varma
Ability to change fonts
175
        font = Pango.FontDescription.from_string(
176
                self.note.cat_prop("font"))
177
        self.txtNote.override_font(font)
178
45 by Umang Varma
style.css is now a template, not the final code
179
    def update_style(self):
180
        """Updates the style using CSS template"""
124 by Umang Varma
Switch to -dark icons if using dark background
181
        self.update_button_color()
45 by Umang Varma
style.css is now a template, not the final code
182
        css_string = self.css_template.substitute(**self.css_data())\
183
                .encode("ascii", "replace")
184
        self.css.load_from_data(css_string)
51 by Umang Varma
Added text color editing support
185
        for context in self.style_contexts:
186
            context.add_provider(self.css,
187
                    Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
45 by Umang Varma
style.css is now a template, not the final code
188
124 by Umang Varma
Switch to -dark icons if using dark background
189
    def update_button_color(self):
190
        """Switches between regular and dark icons appropriately"""
133 by Umang Varma
Tweaked "dark"/"light" color thresholds
191
        h,s,v = self.note.cat_prop("bgcolor_hsv")
192
        # an arbitrary quadratic found by trial and error
193
        thresh_sat = 1.05 - 1.7*((v-1)**2)
194
        suffix = "-dark" if s >= thresh_sat else ""
124 by Umang Varma
Switch to -dark icons if using dark background
195
        iconfiles = {"imgAdd":"add", "imgClose":"close", "imgDropdown":"menu",
196
                "imgLock":"lock", "imgUnlock":"unlock", "imgResizeR":"resizer"}
197
        for img, filename in iconfiles.items():
137 by Umang Varma
Relative path issues with icons
198
            getattr(self, img).set_from_file(
199
                    os.path.join(os.path.dirname(__file__), "..","Icons/" +
200
                    filename + suffix + ".png"))
124 by Umang Varma
Switch to -dark icons if using dark background
201
45 by Umang Varma
style.css is now a template, not the final code
202
    def css_data(self):
203
        """Returns data to substitute into the CSS template"""
46 by Umang Varma
Notes can now change color.
204
        data = {}
51 by Umang Varma
Added text color editing support
205
        # Converts to RGB hex. All RGB/HSV values are scaled to a max of 1
206
        rgb_to_hex = lambda x: "#" + "".join(["{:02x}".format(int(255*a))
207
            for a in x])
208
        hsv_to_hex = lambda x: rgb_to_hex(colorsys.hsv_to_rgb(*x))
121 by Umang Varma
Removed shadow feature
209
        bgcolor_hsv = self.note.cat_prop("bgcolor_hsv")
210
        data["bgcolor_hex"] = hsv_to_hex(
211
                self.note.cat_prop("bgcolor_hsv"))
69 by Umang Varma
Back-end category capability
212
        data["text_color"] = rgb_to_hex(self.note.cat_prop("textcolor"))
46 by Umang Varma
Notes can now change color.
213
        return data
45 by Umang Varma
style.css is now a template, not the final code
214
73 by Umang Varma
Added drop down menu to chose categories
215
    def populate_menu(self):
216
        """(Re)populates the note's menu items appropriately"""
217
        def _delete_menu_item(item, *args):
218
            self.menu.remove(item)
219
        self.menu.foreach(_delete_menu_item, None)
220
83 by Umang Varma
Added always on top feature
221
        aot = Gtk.CheckMenuItem.new_with_label(_("Always on top"))
222
        aot.connect("toggled", self.malways_on_top_toggled)
223
        self.menu.append(aot)
224
        aot.show()
225
96 by Umang Varma
Add settings option to menus
226
        mset = Gtk.MenuItem(_("Settings"))
227
        mset.connect("activate", self.noteset.indicator.show_settings)
228
        self.menu.append(mset)
229
        mset.show()
230
83 by Umang Varma
Added always on top feature
231
        sep = Gtk.SeparatorMenuItem()
232
        self.menu.append(sep)
233
        sep.show()
234
73 by Umang Varma
Added drop down menu to chose categories
235
        catgroup = []
236
        mcats = Gtk.RadioMenuItem.new_with_label(catgroup,
83 by Umang Varma
Added always on top feature
237
                _("Categories:"))
73 by Umang Varma
Added drop down menu to chose categories
238
        self.menu.append(mcats)
239
        mcats.set_sensitive(False)
240
        catgroup = mcats.get_group()
241
        mcats.show()
242
243
        for cid, cdata in self.noteset.categories.items():
244
            mitem = Gtk.RadioMenuItem.new_with_label(catgroup,
245
                    cdata.get("name", _("New Category")))
246
            catgroup = mitem.get_group()
247
            if cid == self.note.category:
248
                mitem.set_active(True)
249
            mitem.connect("activate", self.set_category, cid)
250
            self.menu.append(mitem)
251
            mitem.show()
252
83 by Umang Varma
Added always on top feature
253
    def malways_on_top_toggled(self, widget, *args):
254
        self.winMain.set_keep_above(widget.get_active())
255
22 by Umang Varma
Move backend and gui to sticknotes package
256
    def save(self, *args):
257
        self.note.noteset.save()
258
        return False
259
260
    def add(self, *args):
160 by Justin Engel
Add new note below the current note.
261
        new_note = self.note.noteset.new()
262
161 by Justin Engel
Add new note with same category
263
        # Set the new note to the current category
264
        new_note.gui.set_category(None, self.note.category)
265
        new_note.gui.populate_menu()  # Fix Category Menu Selected indicator
266
160 by Justin Engel
Add new note below the current note.
267
        # Set the new note position below this note
268
        w, h = self.note.properties.get("position", (10, 10))
269
        h += self.winMain.get_allocation().height + 10
270
        new_note.gui.winMain.move(w, h)
271
22 by Umang Varma
Move backend and gui to sticknotes package
272
        return False
273
274
    def delete(self, *args):
132 by Umang Varma
Generate Gtk.MessageDialog from code instead of Glade
275
        winConfirm = Gtk.MessageDialog(self.winMain, None,
276
                Gtk.MessageType.QUESTION, Gtk.ButtonsType.NONE,
277
                _("Are you sure you want to delete this note?"))
278
        winConfirm.add_buttons(Gtk.STOCK_CANCEL, Gtk.ResponseType.REJECT,
279
                Gtk.STOCK_DELETE, Gtk.ResponseType.ACCEPT)
280
        confirm = winConfirm.run()
281
        winConfirm.destroy()
282
        if confirm == Gtk.ResponseType.ACCEPT:
22 by Umang Varma
Move backend and gui to sticknotes package
283
            self.note.delete()
109 by Umang Varma
Safe to destroy (not hide) deleted notes
284
            self.winMain.destroy()
22 by Umang Varma
Move backend and gui to sticknotes package
285
            return False
286
        else:
287
            return True
288
73 by Umang Varma
Added drop down menu to chose categories
289
    def popup_menu(self, button, *args):
290
        """Pops up the note's menu"""
291
        self.menu.popup(None, None, None, None, Gdk.BUTTON_PRIMARY, 
292
                Gtk.get_current_event_time())
293
294
    def set_category(self, widget, cat):
295
        """Set the note's category"""
296
        if not cat in self.noteset.categories:
297
            raise KeyError("No such category")
298
        self.note.category = cat
299
        self.update_style()
84 by Umang Varma
Ability to change fonts
300
        self.update_font()
73 by Umang Varma
Added drop down menu to chose categories
301
22 by Umang Varma
Move backend and gui to sticknotes package
302
    def set_locked_state(self, locked):
68 by Umang Varma
Documented some methods.
303
        """Change the locked state of the stickynote"""
22 by Umang Varma
Move backend and gui to sticknotes package
304
        self.locked = locked
305
        self.txtNote.set_editable(not self.locked)
306
        self.txtNote.set_cursor_visible(not self.locked)
307
        self.bLock.set_image({True:self.imgLock,
308
            False:self.imgUnlock}[self.locked])
40 by Umang Varma
Added keyboard shortcuts, tooltips for buttons
309
        self.bLock.set_tooltip_text({True: _("Unlock"),
310
            False: _("Lock")}[self.locked])
22 by Umang Varma
Move backend and gui to sticknotes package
311
57 by Umang Varma
Made toggle button into a regular button
312
    def lock_clicked(self, *args):
68 by Umang Varma
Documented some methods.
313
        """Toggle the locked state of the note"""
57 by Umang Varma
Made toggle button into a regular button
314
        self.set_locked_state(not self.locked)
22 by Umang Varma
Move backend and gui to sticknotes package
315
316
    def focus_out(self, *args):
317
        self.save(*args)
318
41 by Umang Varma
Added about dialog
319
def show_about_dialog():
320
    glade_file = os.path.abspath(os.path.join(os.path.dirname(__file__),
152 by Umang Varma
.glade -> .ui in code
321
            '..', "GlobalDialogs.ui"))
41 by Umang Varma
Added about dialog
322
    builder = Gtk.Builder()
323
    builder.add_from_file(glade_file)
324
    winAbout = builder.get_object("AboutWindow")
325
    ret =  winAbout.run()
326
    winAbout.destroy()
327
    return ret
47 by Umang Varma
Added settings dialog, which controls bgcolor
328
71 by Umang Varma
Ability to modify categories through settings
329
class SettingsCategory:
330
    """Widgets that handle properties of a category"""
76 by Umang Varma
Implemented default categories
331
    def __init__(self, settingsdialog, cat):
332
        self.settingsdialog = settingsdialog
333
        self.noteset = settingsdialog.noteset
71 by Umang Varma
Ability to modify categories through settings
334
        self.cat = cat
335
        self.builder = Gtk.Builder()
336
        self.path = os.path.abspath(os.path.join(os.path.dirname(__file__),
337
            '..'))
338
        self.builder.add_objects_from_file(os.path.join(self.path,
152 by Umang Varma
.glade -> .ui in code
339
            "SettingsCategory.ui"), ["catExpander"])
71 by Umang Varma
Ability to modify categories through settings
340
        self.builder.connect_signals(self)
75 by Umang Varma
Add and delete category
341
        widgets = ["catExpander", "lExp", "cbBG", "cbText", "eName",
121 by Umang Varma
Removed shadow feature
342
                "confirmDelete", "fbFont"]
71 by Umang Varma
Ability to modify categories through settings
343
        for w in widgets:
344
            setattr(self, w, self.builder.get_object(w))
73 by Umang Varma
Added drop down menu to chose categories
345
        name = self.noteset.categories[cat].get("name", _("New Category"))
71 by Umang Varma
Ability to modify categories through settings
346
        self.eName.set_text(name)
76 by Umang Varma
Implemented default categories
347
        self.refresh_title()
71 by Umang Varma
Ability to modify categories through settings
348
        self.cbBG.set_rgba(Gdk.RGBA(*colorsys.hsv_to_rgb(
78 by Umang Varma
s/bgcolor/bgcolor_hsv and text color fix
349
            *self.noteset.get_category_property(cat, "bgcolor_hsv")),
71 by Umang Varma
Ability to modify categories through settings
350
            alpha=1))
78 by Umang Varma
s/bgcolor/bgcolor_hsv and text color fix
351
        self.cbText.set_rgba(Gdk.RGBA(
352
            *self.noteset.get_category_property(cat, "textcolor"),
71 by Umang Varma
Ability to modify categories through settings
353
            alpha=1))
84 by Umang Varma
Ability to change fonts
354
        fontname = self.noteset.get_category_property(cat, "font")
355
        if not fontname:
356
            # Get the system default font, if none is set
357
            fontname = \
358
                self.settingsdialog.wSettings.get_style_context()\
359
                    .get_font(Gtk.StateFlags.NORMAL).to_string()
360
                #why.is.this.so.long?
361
        self.fbFont.set_font(fontname)
71 by Umang Varma
Ability to modify categories through settings
362
76 by Umang Varma
Implemented default categories
363
    def refresh_title(self, *args):
364
        """Updates the title of the category"""
365
        name = self.noteset.categories[self.cat].get("name",
366
                _("New Category"))
367
        if self.noteset.properties.get("default_cat", "") == self.cat:
368
            name += " (" + _("Default Category") + ")"
369
        self.lExp.set_text(name)
370
75 by Umang Varma
Add and delete category
371
    def delete_cat(self, *args):
372
        """Delete a category"""
132 by Umang Varma
Generate Gtk.MessageDialog from code instead of Glade
373
        winConfirm = Gtk.MessageDialog(self.settingsdialog.wSettings, None,
374
                Gtk.MessageType.QUESTION, Gtk.ButtonsType.NONE,
375
                _("Are you sure you want to delete this category?"))
376
        winConfirm.add_buttons(Gtk.STOCK_CANCEL, Gtk.ResponseType.REJECT,
377
                Gtk.STOCK_DELETE, Gtk.ResponseType.ACCEPT)
378
        confirm = winConfirm.run()
379
        winConfirm.destroy()
380
        if confirm == Gtk.ResponseType.ACCEPT:
76 by Umang Varma
Implemented default categories
381
            self.settingsdialog.delete_category(self.cat)
382
383
    def make_default(self, *args):
384
        """Make this the default category"""
385
        self.noteset.properties["default_cat"] = self.cat
386
        self.settingsdialog.refresh_category_titles()
387
        for note in self.noteset.notes:
388
            note.gui.update_style()
84 by Umang Varma
Ability to change fonts
389
            note.gui.update_font()
75 by Umang Varma
Add and delete category
390
71 by Umang Varma
Ability to modify categories through settings
391
    def eName_changed(self, *args):
75 by Umang Varma
Add and delete category
392
        """Update a category name"""
71 by Umang Varma
Ability to modify categories through settings
393
        self.noteset.categories[self.cat]["name"] = self.eName.get_text()
120 by Umang Varma
Default category should be indicated even when title changing
394
        self.refresh_title()
73 by Umang Varma
Added drop down menu to chose categories
395
        for note in self.noteset.notes:
396
            note.gui.populate_menu()
71 by Umang Varma
Ability to modify categories through settings
397
398
    def update_bg(self, *args):
399
        """Action to update the background color"""
400
        try:
401
            rgba = self.cbBG.get_rgba()
402
        except TypeError:
403
            rgba = Gdk.RGBA()
404
            self.cbBG.get_rgba(rgba)
405
            # Some versions of GObjectIntrospection are affected by
406
            # https://bugzilla.gnome.org/show_bug.cgi?id=687633 
407
        hsv = colorsys.rgb_to_hsv(rgba.red, rgba.green, rgba.blue)
78 by Umang Varma
s/bgcolor/bgcolor_hsv and text color fix
408
        self.noteset.categories[self.cat]["bgcolor_hsv"] = hsv
71 by Umang Varma
Ability to modify categories through settings
409
        for note in self.noteset.notes:
410
            note.gui.update_style()
121 by Umang Varma
Removed shadow feature
411
        # Remind some widgets that they are transparent, etc.
71 by Umang Varma
Ability to modify categories through settings
412
        load_global_css()
413
414
    def update_textcolor(self, *args):
415
        """Action to update the text color"""
416
        try:
417
            rgba = self.cbText.get_rgba()
418
        except TypeError:
419
            rgba = Gdk.RGBA()
420
            self.cbText.get_rgba(rgba)
421
        self.noteset.categories[self.cat]["textcolor"] = \
422
                [rgba.red, rgba.green, rgba.blue]
423
        for note in self.noteset.notes:
424
            note.gui.update_style()
425
84 by Umang Varma
Ability to change fonts
426
    def update_font(self, *args):
427
        """Action to update the font size"""
428
        self.noteset.categories[self.cat]["font"] = \
429
            self.fbFont.get_font_name()
430
        for note in self.noteset.notes:
431
            note.gui.update_font()
432
47 by Umang Varma
Added settings dialog, which controls bgcolor
433
class SettingsDialog:
68 by Umang Varma
Documented some methods.
434
    """Manages the GUI of the settings dialog"""
47 by Umang Varma
Added settings dialog, which controls bgcolor
435
    def __init__(self, noteset):
436
        self.noteset = noteset
71 by Umang Varma
Ability to modify categories through settings
437
        self.categories = {}
47 by Umang Varma
Added settings dialog, which controls bgcolor
438
        self.path = os.path.abspath(os.path.join(os.path.dirname(__file__),
439
            '..'))
152 by Umang Varma
.glade -> .ui in code
440
        glade_file = (os.path.join(self.path, "GlobalDialogs.ui"))
47 by Umang Varma
Added settings dialog, which controls bgcolor
441
        self.builder = Gtk.Builder()
442
        self.builder.add_from_file(glade_file)
443
        self.builder.connect_signals(self)
71 by Umang Varma
Ability to modify categories through settings
444
        widgets = ["wSettings", "boxCategories"]
47 by Umang Varma
Added settings dialog, which controls bgcolor
445
        for w in widgets:
446
            setattr(self, w, self.builder.get_object(w))
71 by Umang Varma
Ability to modify categories through settings
447
        for c in self.noteset.categories:
448
            self.add_category_widgets(c)
47 by Umang Varma
Added settings dialog, which controls bgcolor
449
        ret =  self.wSettings.run()
450
        self.wSettings.destroy()
451
71 by Umang Varma
Ability to modify categories through settings
452
    def add_category_widgets(self, cat):
75 by Umang Varma
Add and delete category
453
        """Add the widgets for a category"""
76 by Umang Varma
Implemented default categories
454
        self.categories[cat] = SettingsCategory(self, cat)
71 by Umang Varma
Ability to modify categories through settings
455
        self.boxCategories.pack_start(self.categories[cat].catExpander,
456
                False, False, 0)
75 by Umang Varma
Add and delete category
457
458
    def new_category(self, *args):
459
        """Make a new category"""
460
        cid = str(uuid.uuid4())
461
        self.noteset.categories[cid] = {}
462
        self.add_category_widgets(cid)
76 by Umang Varma
Implemented default categories
463
464
    def delete_category(self, cat):
465
        """Delete a category"""
466
        del self.noteset.categories[cat]
467
        self.categories[cat].catExpander.destroy()
468
        del self.categories[cat]
469
        for note in self.noteset.notes:
470
            note.gui.populate_menu()
471
            note.gui.update_style()
84 by Umang Varma
Ability to change fonts
472
            note.gui.update_font()
76 by Umang Varma
Implemented default categories
473
474
    def refresh_category_titles(self):
475
        for cid, catsettings in self.categories.items():
476
            catsettings.refresh_title()