Skip to content
149 changes: 69 additions & 80 deletions wpgtk/gui/color_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
from ..data import themer
from . import util as gui_util

from .color_picker import ColorDialog
from gi import require_version

require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, GdkPixbuf # noqa: E402
require_version("Gtk", "4.0")
from gi.repository import Gtk, Gdk # noqa: E402

# TODO: remove current_walls call, use simple list
# TODO: use simple text combo
Expand All @@ -27,36 +26,35 @@ class ColorGrid(Gtk.Grid):
def __init__(self, parent):
Gtk.Grid.__init__(self)
self.parent = parent
self.set_border_width(PAD)
self.set_column_homogeneous(1)
gui_util.set_uniform_margins(self, PAD)
self.set_row_spacing(PAD)
self.set_column_spacing(PAD)

self.colorgrid = Gtk.Grid()
self.colorgrid.set_border_width(PAD)
self.colorgrid.set_column_homogeneous(1)
self.colorgrid.set_row_spacing(PAD)
self.colorgrid.set_column_spacing(PAD)

self.sat_add = Gtk.Button("+")
self.sat_add = Gtk.Button.new_with_label("+")
self.sat_add.set_sensitive(False)

self.sat_red = Gtk.Button("-")
self.sat_red = Gtk.Button.new_with_label("-")
self.sat_red.set_sensitive(False)

self.sat_add.connect("pressed", self.hls_change, "sat", "add")
self.sat_red.connect("pressed", self.hls_change, "sat", "red")
self.sat_lbl = Gtk.Label("Saturation:")
self.sat_add.connect("clicked", self.hls_change, "sat", "add")
self.sat_red.connect("clicked", self.hls_change, "sat", "red")
self.sat_lbl = Gtk.Label(label="Saturation:")

self.light_add = Gtk.Button("+")
self.light_add = Gtk.Button(label="+")
self.light_add.set_sensitive(False)

self.light_red = Gtk.Button("-")
self.light_red = Gtk.Button(label="-")
self.light_red.set_sensitive(False)

self.light_add.connect("pressed", self.hls_change, "light", "add")
self.light_red.connect("pressed", self.hls_change, "light", "red")
self.light_lbl = Gtk.Label("Brightness:")
self.light_add.connect("clicked", self.hls_change, "light", "add")
self.light_red.connect("clicked", self.hls_change, "light", "red")
self.light_lbl = Gtk.Label(label="Brightness:")

self.sat_light_grid = Gtk.Grid()
self.sat_light_grid.set_column_homogeneous(1)
Expand All @@ -74,48 +72,44 @@ def __init__(self, parent):
self.combo_grid.set_row_spacing(PAD)

self.color_list = ["000000"] * 16
self.button_list = [Gtk.Button("000000") for x in range(16)]
self.button_list = [Gtk.Button(label="000000") for x in range(16)]
self.selected_file = ""
for button in self.button_list:
button.connect("pressed", self.on_color_click)
button.connect("clicked", self.on_color_click)
button.set_sensitive(False)

cont = 0
for y in range(0, 8, 2):
for x in range(0, 4):
label = Gtk.Label(cont)
label = Gtk.Label(label=cont)
self.colorgrid.attach(label, x, y, 1, 1)
self.colorgrid.attach(self.button_list[cont], x, y + 1, 1, 1)
cont += 1

sample_name = os.path.join(SAMPLE_DIR, ".no_sample.sample.png")
self.sample = Gtk.Image()
self.sample = Gtk.Picture.new_for_filename(sample_name)

pixbuf_sample = gui_util.get_sample_pixbuf(sample_name)
if pixbuf_sample is not None:
self.sample.set_from_pixbuf(self.pixbuf_sample)

self.shuffle_button = Gtk.Button("Shuffle colors")
self.shuffle_button.connect("pressed", self.on_shuffle_click)
self.shuffle_button = Gtk.Button(label="Shuffle colors")
self.shuffle_button.connect("clicked", self.on_shuffle_click)
self.shuffle_button.set_sensitive(False)

self.import_button = Gtk.Button("import")
self.import_button = Gtk.Button(label="Import")
self.import_button.set_sensitive(False)
self.import_button.connect("pressed", self.on_import_click)
self.import_button.connect("clicked", self.on_import_click)

self.ok_button = Gtk.Button("Save")
self.ok_button.connect("pressed", self.on_ok_click)
self.ok_button = Gtk.Button(label="Save")
self.ok_button.connect("clicked", self.on_ok_click)
self.ok_button.set_sensitive(False)

self.auto_button = Gtk.Button("Auto-adjust")
self.auto_button.connect("pressed", self.on_auto_click)
self.auto_button = Gtk.Button(label="Auto-adjust")
self.auto_button.connect("clicked", self.on_auto_click)
self.auto_button.set_sensitive(False)

self.reset_button = Gtk.Button("Reset")
self.reset_button = Gtk.Button(label="Reset")
self.reset_button.set_sensitive(False)
self.reset_button.connect("pressed", self.on_reset_click)
self.reset_button.connect("clicked", self.on_reset_click)

self.done_lbl = Gtk.Label("")
self.done_lbl = Gtk.Label(label="")

option_list = Gtk.ListStore(str)
for elem in list(files.get_file_list()):
Expand Down Expand Up @@ -153,15 +147,15 @@ def __init__(self, parent):

def render_buttons(self):
for x, button in enumerate(self.button_list):
gcolor = Gdk.color_parse(self.color_list[x])
if util.get_hls_val(self.color_list[x], "light") < 99:
fgcolor = Gdk.color_parse("#FFFFFF")
fgcolor = "#FFFFFF"
else:
fgcolor = Gdk.color_parse("#000000")
fgcolor = "#000000"
button.set_label(self.color_list[x])
button.set_sensitive(True)
button.modify_bg(Gtk.StateType.NORMAL, gcolor)
button.modify_fg(Gtk.StateType.NORMAL, fgcolor)
gui_util.set_widget_colors(
button, background=self.color_list[x], foreground=fgcolor
)

def render_theme(self):
sample_path = files.get_sample_path(self.selected_file)
Expand All @@ -172,13 +166,11 @@ def render_theme(self):
self.color_list = themer.set_fallback_theme(self.selected_file)
self.render_buttons()

pixbuf_sample = gui_util.get_sample_pixbuf(sample_path)
if pixbuf_sample is None:
if not os.path.isfile(sample_path):
sample.create_sample(self.color_list, sample_path)
pixbuf_sample = gui_util.get_sample_pixbuf(sample_path)

self.sample.set_from_pixbuf(pixbuf_sample)
self.parent.sample.set_from_pixbuf(pixbuf_sample)
self.sample.set_filename(sample_path)
self.parent.sample.set_filename(sample_path)

def hls_change(self, widget, *gparam):
if gparam[0] == "sat":
Expand All @@ -197,10 +189,7 @@ def hls_change(self, widget, *gparam):
def render_sample(self):
sample.create_sample(self.color_list)
sample_path = os.path.join(SAMPLE_DIR, ".tmp.sample.png")
self.pixbuf_sample = GdkPixbuf.Pixbuf.new_from_file_at_size(
sample_path, width=500, height=300
)
self.sample.set_from_pixbuf(self.pixbuf_sample)
self.sample.set_filename(sample_path)

def on_ok_click(self, widget):
color.write_colors(self.selected_file, self.color_list)
Expand All @@ -214,10 +203,7 @@ def on_ok_click(self, widget):

self.done_lbl.set_text("Changes saved")
sample_path = files.get_sample_path(self.selected_file)
self.parent.pixbuf_sample = GdkPixbuf.Pixbuf.new_from_file_at_size(
sample_path, width=500, height=300
)
self.parent.sample.set_from_pixbuf(self.pixbuf_sample)
self.parent.sample.set_filename(sample_path)

def on_auto_click(self, widget):
self.color_list = color.auto_adjust(self.color_list)
Expand All @@ -229,29 +215,24 @@ def on_reset_click(self, widget):
self.render_theme()

def on_import_click(self, widget):
fcd = Gtk.FileChooserDialog(
"Select a colorscheme",
self.parent,
Gtk.FileChooserAction.OPEN,
(
Gtk.STOCK_CANCEL,
Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN,
Gtk.ResponseType.OK,
),
)
fcd = Gtk.FileDialog()

filter = Gtk.FileFilter()
filter.set_name("JSON colorscheme")
filter.add_mime_type("application/json")
fcd.add_filter(filter)
response = fcd.run()

if response == Gtk.ResponseType.OK:
self.color_list = color.get_color_list(fcd.get_filename(), True)
fcd.set_default_filter(filter)
fcd.set_title("Select a colorscheme")

fcd.open(parent=self.parent, callback=self.on_import_finish)

def on_import_finish(self, dialog, result):
filename = dialog.open_finish(result)

if filename:
self.color_list = color.get_color_list(filename, True)
self.render_buttons()
self.render_sample()
fcd.destroy()

def on_shuffle_click(self, widget):
self.color_list = color.shuffle_colors(self.color_list)
Expand All @@ -260,32 +241,40 @@ def on_shuffle_click(self, widget):

def on_color_click(self, widget):
self.done_lbl.set_text("")
self.active_color_button = widget
gcolor = Gdk.RGBA()
gcolor.parse(widget.get_label())
dialog = ColorDialog(self.parent, self.selected_file, gcolor)
response = dialog.run()
dialog = Gtk.ColorDialog()
dialog.set_with_alpha(False)
dialog.set_title("Choose a Color")
dialog.choose_rgba(
parent=self.parent, initial_color=gcolor, callback=self.on_color_selected
)

if response == Gtk.ResponseType.OK:
r, g, b, _ = dialog.colorchooser.get_rgba()
def on_color_selected(self, dialog, result):
rgba = dialog.choose_rgba_finish(result)

if rgba:
r, g, b, _ = rgba
rgb = list(map(lambda x: round(x * 100 * 2.55), [r, g, b]))
hex_color = pywal.util.rgb_to_hex(rgb)
widget.set_label(hex_color)
# widget.set_label(hex_color)

gcolor = Gdk.color_parse(hex_color)
if util.get_hls_val(hex_color, "light") < 100:
fgcolor = Gdk.color_parse("#FFFFFF")
fgcolor = "#FFFFFF"
else:
fgcolor = Gdk.color_parse("#000000")
fgcolor = "#000000"

widget.set_sensitive(True)
widget.modify_bg(Gtk.StateType.NORMAL, gcolor)
widget.modify_fg(Gtk.StateType.NORMAL, fgcolor)
self.active_color_button.set_sensitive(True)
self.active_color_button.set_label(hex_color)
gui_util.set_widget_colors(
self.active_color_button, background=hex_color, foreground=fgcolor
)

for i, c in enumerate(self.button_list):
if c.get_label() != self.color_list[i]:
self.color_list[i] = c.get_label()
self.render_sample()
dialog.destroy()

def combo_box_change(self, widget):
self.done_lbl.set_text("")
Expand Down
70 changes: 0 additions & 70 deletions wpgtk/gui/color_picker.py

This file was deleted.

21 changes: 15 additions & 6 deletions wpgtk/gui/keyword_dialog.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
from gi import require_version
require_version("Gtk", "3.0")

require_version("Gtk", "4.0")
from gi.repository import Gtk # noqa: E402


class KeywordDialog(Gtk.Dialog):

def __init__(self, parent):
Gtk.Dialog.__init__(self, "Name you keyword/value set", parent, 0,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OK, Gtk.ResponseType.OK))
Gtk.Dialog.__init__(
self,
"Name your keyword/value set",
parent,
0,
(
Gtk.STOCK_CANCEL,
Gtk.ResponseType.CANCEL,
Gtk.STOCK_OK,
Gtk.ResponseType.OK,
),
)

self.set_default_size(150, 100)
self.name_text_input = Gtk.Entry()
Expand All @@ -24,6 +33,6 @@ def __init__(self, parent):

def get_section_name(self):
if len(self.name_text_input.get_text()) <= 0:
raise Exception('Empty name not allowed')
raise Exception("Empty name not allowed")

return self.name_text_input.get_text()
Loading