Commit dfe8e6ef authored by Colomban Wendling's avatar Colomban Wendling

Initial import

parents
#!/usr/bin/env python
#
# Copyright (C) 2013 Colomban Wendling <ban@herbesfolles.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of the author nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
from gi.repository import Gtk, Gdk, Gio
class RGBA(Gdk.RGBA):
def __init__(self, arg=None):
Gdk.RGBA.__init__(self)
if isinstance(arg, str):
self.parse(arg)
elif isinstance(arg, Gdk.RGBA):
self.red = arg.red
self.green = arg.green
self.blue = arg.blue
self.alpha = arg.alpha
def to_string(self, alpha=False):
if alpha:
return '#%.2x%.2x%.2x%.2x' % (self.red * 0xff,
self.green * 0xff,
self.blue * 0xff,
self.alpha * 0xff)
else:
return '#%.2x%.2x%.2x' % (self.red * 0xff,
self.green * 0xff,
self.blue * 0xff)
class AppWindow(Gtk.Window):
ROW_KEY = 0
ROW_COLOR = 1
def __init__(self):
Gtk.Window.__init__(self)
self.set_default_size(400, 300)
self.set_title("GTK3 Theme Pref")
self.set_icon_name("gtk-preferences")
builder = Gtk.Builder()
builder.add_from_file("window.ui")
uimanager = builder.get_object('uimanager1')
self.add_accel_group(uimanager.get_accel_group())
vbox = builder.get_object("box")
self.add(vbox)
quit = builder.get_object('quit')
quit.connect('activate', lambda s: self.destroy())
add = builder.get_object('add')
add.connect('activate', self.on_add_activate)
remove = builder.get_object('remove')
remove.connect('activate', self.on_remove_activate)
self.store = builder.get_object('liststore1')
self.view = builder.get_object('treeview1')
self.view.connect('row-activated', self.on_row_activated)
def on_key_cell_edited(cell, path, text, data=None):
self.store.set(self.store.get_iter(path), self.ROW_KEY, text)
cell = builder.get_object('cellrenderertext1')
cell.connect('edited', on_key_cell_edited)
self.settings = Gio.Settings.new('org.gnome.desktop.interface')
self.load()
def on_model_changed(model, path, iter=None, data=None):
self.update_theme()
self.store.connect('row-changed', on_model_changed)
self.store.connect('row-deleted', on_model_changed)
self.store.connect('row-inserted', on_model_changed)
def on_row_activated(self, view, path, column, data=None):
iter = self.store.get_iter(path)
dialog = Gtk.ColorChooserDialog()
dialog.set_transient_for(self)
dialog.set_rgba(RGBA(self.store.get(iter, self.ROW_COLOR)[0]))
answer = dialog.run()
if answer == Gtk.ResponseType.ACCEPT or \
answer == Gtk.ResponseType.APPLY or \
answer == Gtk.ResponseType.OK or \
answer == Gtk.ResponseType.YES:
try:
rgba = RGBA(dialog.get_rgba())
except:
# Apparently old GI was broken...
rgba = RGBA()
dialog.get_rgba(rgba)
self.store.set(iter, self.ROW_COLOR, rgba.to_string())
dialog.destroy()
def on_add_activate(self, action, data=None):
iter = self.store.append()
self.store.set(iter, self.ROW_KEY, 'key', self.ROW_COLOR, '#000')
self.view.set_cursor(self.store.get_path(iter), None, True)
def on_remove_activate(self, action, data=None):
sel = self.view.get_selection()
model, iter = sel.get_selected()
if iter is not None:
model.remove(iter)
def load(self):
scheme = self.settings.get_string('gtk-color-scheme').strip()
n = 0
for entry in scheme.split(';'):
entry = entry.strip()
if len(entry) < 1:
continue
k, v = entry.split(':')
iter = self.store.append()
self.store.set(iter, self.ROW_KEY, k, self.ROW_COLOR, v)
n += 1
if n == 0:
for k, v in [('base_color', '#ffffff'),
('bg_color', '#ededed'),
('tooltip_bg_color', '#343434'),
('selected_bg_color', '#4a90d9'),
('text_color', '#2e3436'),
('fg_color', '#2e3436'),
('tooltip_fg_color', '#ffffff'),
('selected_fg_color', '#ffffff')]:
iter = self.store.append()
self.store.set(iter, self.ROW_KEY, k, self.ROW_COLOR, v)
def update_theme(self):
values = []
iter = self.store.get_iter_first()
while iter is not None:
key, color = self.store.get(iter, self.ROW_KEY, self.ROW_COLOR)
values.append('%s:%s' % (key, color))
iter = self.store.iter_next(iter)
value = ';'.join(values)
#print(value)
scheme = self.settings.set_string('gtk-color-scheme', value)
class App(Gtk.Application):
def __init__(self):
Gtk.Application.__init__(self, application_id="apps.gtk_theme_prefs")
def do_activate(self):
window = AppWindow()
window.show_all()
self.add_window(window)
if __name__ == '__main__':
import sys
app = App()
app.run(sys.argv)
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.0 -->
<object class="GtkUIManager" id="uimanager1">
<child>
<object class="GtkActionGroup" id="actiongroup1">
<child>
<object class="GtkAction" id="quit">
<property name="label" translatable="yes">_Quit</property>
<property name="tooltip" translatable="yes">Quit</property>
<property name="stock_id">gtk-quit</property>
</object>
<accelerator key="q" modifiers="GDK_CONTROL_MASK"/>
</child>
<child>
<object class="GtkAction" id="add">
<property name="label" translatable="yes">_Add</property>
<property name="tooltip" translatable="yes">Add a new key</property>
<property name="stock_id">gtk-add</property>
</object>
<accelerator key="n" modifiers="GDK_CONTROL_MASK"/>
</child>
<child>
<object class="GtkAction" id="remove">
<property name="label" translatable="yes">_Remove</property>
<property name="tooltip" translatable="yes">Remove the selected key</property>
<property name="stock_id">gtk-delete</property>
</object>
<accelerator key="Delete"/>
</child>
<child>
<object class="GtkAction" id="undo">
<property name="label" translatable="yes">_Undo</property>
<property name="tooltip" translatable="yes">Undo the last action</property>
<property name="stock_id">gtk-undo</property>
</object>
<accelerator key="z" modifiers="GDK_CONTROL_MASK"/>
</child>
<child>
<object class="GtkAction" id="redo">
<property name="label" translatable="yes">_Redo</property>
<property name="tooltip" translatable="yes">Redo the last undone action</property>
<property name="stock_id">gtk-redo</property>
</object>
<accelerator key="z" modifiers="GDK_SHIFT_MASK | GDK_CONTROL_MASK"/>
</child>
</object>
</child>
<ui>
<toolbar name="toolbar1">
<toolitem action="add"/>
<toolitem action="remove"/>
<separator/>
<toolitem action="undo"/>
<toolitem action="redo"/>
<separator expand="true"/>
<toolitem action="quit"/>
</toolbar>
</ui>
</object>
<object class="GtkListStore" id="liststore1">
<columns>
<!-- column-name key -->
<column type="gchararray"/>
<!-- column-name color -->
<column type="gchararray"/>
</columns>
</object>
<object class="GtkVBox" id="box">
<property name="can_focus">False</property>
<property name="border_width">12</property>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkToolbar" id="toolbar1" constructor="uimanager1"/>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkScrolledWindow" id="scrolledwindow1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="shadow_type">in</property>
<child>
<object class="GtkTreeView" id="treeview1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="model">liststore1</property>
<child internal-child="selection">
<object class="GtkTreeSelection" id="treeview-selection1"/>
</child>
<child>
<object class="GtkTreeViewColumn" id="treeviewcolumn1">
<property name="title" translatable="yes">Key</property>
<child>
<object class="GtkCellRendererText" id="cellrenderertext1">
<property name="editable">True</property>
</object>
<attributes>
<attribute name="text">0</attribute>
</attributes>
</child>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="treeviewcolumn2">
<property name="title" translatable="yes">Color</property>
<child>
<object class="GtkCellRendererText" id="cellrenderertext2"/>
<attributes>
<attribute name="cell-background">1</attribute>
</attributes>
</child>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
</object>
</interface>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment