Skip to content

Commit

Permalink
Create a new module for the custom widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
RMPR committed Jan 16, 2021
1 parent 543a474 commit 00a58ca
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 75 deletions.
77 changes: 2 additions & 75 deletions atbswp/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

import settings

from custom_widgets import SliderDialog, CountdownDialog

import wx
import wx.adv

Expand Down Expand Up @@ -520,78 +522,3 @@ def action(event):
url = "https://youtu.be/L0jjSgX5FYk"
wx.LaunchDefaultBrowser(url, flags=0)

"""
Because of cyclic import the two following custom widgets will be stored here
to be consumed by the elements of the package
"""

class SliderDialog(wx.Dialog):
"""Wrap a slider in a dialog and get the value"""

def __init__(self, *args, **kwargs):
"""Initialize the widget with the custom attributes."""
self._current_value = kwargs.pop("default_value", 1)
self.min_value = kwargs.pop("min_value", 1)
self.max_value = kwargs.pop("max_value", 999)
super(SliderDialog, self).__init__(*args, **kwargs)
self._value = 1
self.init_ui()
self.slider.Bind(wx.EVT_KEY_UP, self.on_esc_press)
self.Bind(wx.EVT_CLOSE, self.on_close)

def init_ui(self):
"""Initialize the UI elements"""
pnl = wx.Panel(self)
sizer = wx.BoxSizer(orient=wx.VERTICAL)
self.slider = wx.Slider(parent=pnl, id=wx.ID_ANY, value=self._current_value,
minValue=self.min_value, maxValue=self.max_value,
name="Choose a number", size=self.GetSize(),
style=wx.SL_VALUE_LABEL | wx.SL_AUTOTICKS)
sizer.Add(self.slider)
pnl.SetSizer(sizer)
sizer.Fit(self)

def on_esc_press(self, event):
"""Close the dialog if the user presses ESC"""
if event.KeyCode == wx.WXK_ESCAPE:
self.Close()
event.Skip()

def on_close(self, event):
"""Triggered when the widget is closed."""
self._value = self.slider.Value
self.Destroy()

@property
def value(self):
"""Getter."""
return self._value

@value.setter
def value(self, value):
"""Setter."""
self._value = value


class CountdownDialog(wx.Dialog):
"""Display a dialog with a countdown"""

def __init__(self, *args, **kwargs):
self._countdown = kwargs.pop("countdown", 0)
super(CountdownDialog, self).__init__(*args, **kwargs)
self.init_ui()

def init_ui(self):
self.pnl = wx.Panel(self)
self.sizer = wx.BoxSizer(orient=wx.HORIZONTAL)
message = wx.StaticText(self, label=f"The recording will start in {self._countdown} seconds")
self.sizer.Add(message)
self.pnl.SetSizer(self.sizer)
self.sizer.Fit(self)

def update_ui(self):
self._countdown -= 1
self.sizer.Clear(True)
message = wx.StaticText(self, label=f"The recording will start in {self._countdown} seconds")
self.sizer.Add(message)
return self._countdown
91 changes: 91 additions & 0 deletions atbswp/custom_widgets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
"""Custom Widgets created to modify the settings."""

# atbswp: Record mouse and keyboard actions and reproduce them identically at will
#
# Copyright (C) 2019 Mairo Rufus <github@rmpr.xyz>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import wx

class SliderDialog(wx.Dialog):
"""Wrap a slider in a dialog and get the value"""

def __init__(self, *args, **kwargs):
"""Initialize the widget with the custom attributes."""
self._current_value = kwargs.pop("default_value", 1)
self.min_value = kwargs.pop("min_value", 1)
self.max_value = kwargs.pop("max_value", 999)
super(SliderDialog, self).__init__(*args, **kwargs)
self._value = 1
self.init_ui()
self.slider.Bind(wx.EVT_KEY_UP, self.on_esc_press)
self.Bind(wx.EVT_CLOSE, self.on_close)

def init_ui(self):
"""Initialize the UI elements"""
pnl = wx.Panel(self)
sizer = wx.BoxSizer(orient=wx.VERTICAL)
self.slider = wx.Slider(parent=pnl, id=wx.ID_ANY, value=self._current_value,
minValue=self.min_value, maxValue=self.max_value,
name="Choose a number", size=self.GetSize(),
style=wx.SL_VALUE_LABEL | wx.SL_AUTOTICKS)
sizer.Add(self.slider)
pnl.SetSizer(sizer)
sizer.Fit(self)

def on_esc_press(self, event):
"""Close the dialog if the user presses ESC"""
if event.KeyCode == wx.WXK_ESCAPE:
self.Close()
event.Skip()

def on_close(self, event):
"""Triggered when the widget is closed."""
self._value = self.slider.Value
self.Destroy()

@property
def value(self):
"""Getter."""
return self._value

@value.setter
def value(self, value):
"""Setter."""
self._value = value


class CountdownDialog(wx.Dialog):
"""Display a dialog with a countdown"""

def __init__(self, *args, **kwargs):
self._countdown = kwargs.pop("countdown", 0)
super(CountdownDialog, self).__init__(*args, **kwargs)
self.init_ui()

def init_ui(self):
self.pnl = wx.Panel(self)
self.sizer = wx.BoxSizer(orient=wx.HORIZONTAL)
message = wx.StaticText(self, label=f"The recording will start in {self._countdown} seconds")
self.sizer.Add(message)
self.pnl.SetSizer(self.sizer)
self.sizer.Fit(self)

def update_ui(self):
self._countdown -= 1
self.sizer.Clear(True)
message = wx.StaticText(self, label=f"The recording will start in {self._countdown} seconds")
self.sizer.Add(message)
return self._countdown

0 comments on commit 00a58ca

Please sign in to comment.