Skip to content

Commit

Permalink
Bump the version to 0.1.8 with some brake changes on progress bar imp…
Browse files Browse the repository at this point in the history
…lementation
  • Loading branch information
SoulRaven committed Jun 24, 2024
1 parent ab8cd6c commit 7c64ac7
Show file tree
Hide file tree
Showing 14 changed files with 258 additions and 126 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

### @TODO:
- improve the code
- change the interfaces and reimplement the callback method


## Version 0.1.8
- BRAKE: the mode how the ProgressBar is reimplemented will brake the old code. Use the example from README.md
- reword on the progress bar and threads. For now seams to work OK
- small fixes for documentation and added some images

## Version 0.1.7
- refactored all the code
Expand Down
79 changes: 48 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,6 @@ $ python setup.py install

Simple dialog:

```python
from src.sgzenity import calendar

result = calendar(title="Awesome Calendar", text="Your birthday ?")
print(result)
```
This code show a calendar dialog :

![dialog_01](docs/img/screen_01.png)

And display the result :

```bash
$ python test.py
$ (year=2017, month=6, day=4)
```

## API

### Simple message
Expand Down Expand Up @@ -97,6 +80,9 @@ warning(title='', text='', width=330, height=120, timeout=None)
>* **timeout** (*int*) – close the window after n seconds
### Question

![basic_dialog_01](docs/img/basic_dialog.png)

```python
question(title='', text='', width=330, height=120, timeout=None)
```
Expand All @@ -114,6 +100,9 @@ question(title='', text='', width=330, height=120, timeout=None)
>_Return type_: bool
### Progress Bar

![basic_dialog_01](docs/img/progressbar.png)

```python
progress_bar(title, text, pulse_mode, callback)
```
Expand All @@ -128,21 +117,40 @@ progress_bar(title, text, pulse_mode, callback)

### Demo

```python
def callback_progress_bar(fraction=None):
global counter
counter += 0.01
if counter <= _max:
return counter
return True

def demo_progress_bar():
progress = progress_bar(
"DEMO TITLE", "DEMO TEXT", False, callback_progress_bar, 350, 30, 10
)
progress.run_progressbar()

demo_progress_bar()
import time
from sgzenity.thread import WorkerThread
from sgzenity import ProgressBar

class WorkingThread(WorkerThread):
def payload(self):
loading = self.data
steps = 10
for s in range(steps):
if self.stop:
break
loading.heartbeat()
print('Pulse {}.'.format(s))
time.sleep(1)
if self.stop:
print('Working thread canceled.')
else:
print('Working thread ended.')
loading.close()


def sg_progress_bar():
loading = ProgressBar("DEMO TITLE", "DEMO TEXT", pulse_mode=True)

workthread = WorkingThread(loading)
loading.show(workthread)
workthread.start()

Gtk.main()


sg_progress_bar()
```

### Entry
Expand Down Expand Up @@ -244,6 +252,15 @@ calendar(text='', day=None, month=None, title='', width=330, height=120, timeout
>
>_Return type_: tuple
![calendar_dialog_01](docs/img/calendar_dialog.png)

And display the result :

```bash
$ python demo.py
$ (year=2017, month=6, day=4)
```

### Color selection

```python
Expand Down
50 changes: 32 additions & 18 deletions demo.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,51 @@
#! /usr/bin/env python3
# -*- coding:utf-8 -*-
import random

import time

import gi

gi.require_version("Gtk", "3.0")
from gi.repository import GLib, Gtk
from gi.repository import GLib, GObject, Gtk

from src.sgzenity.SGProgresBar import ProgressBar
from src.sgzenity.sgszenity import calendar, question
from src.sgzenity.thread import WorkerThread

from src.sgzenity.sgszenity import progress_bar, question

counter = 0
_max = 1
class WorkingThread(WorkerThread):
def payload(self):
loading = self.data
steps = 10
for s in range(steps):
if self.stop:
break
loading.heartbeat()
print('Pulse {}.'.format(s))
time.sleep(1)
if self.stop:
print('Working thread canceled.')
else:
print('Working thread ended.')
loading.close()


def callback_progress_bar(fraction=None):
global counter
counter += 0.01
if counter <= _max:
return counter
return True
def sg_progress_bar():

loading = ProgressBar("DEMO TITLE", "DEMO TEXT", pulse_mode=True)

def demo_progress_bar():
progress = progress_bar(
"DEMO TITLE", "DEMO TEXT", False, callback_progress_bar, 350, 30, 10
)
progress.run_progressbar()
workthread = WorkingThread(loading)
loading.show(workthread)
workthread.start()

Gtk.main()

demo_progress_bar()

sg_progress_bar()

_error = question(
"something went wrong", text="Some big text in small space", height=150, width=400
)
# print(_error)

_calendar = calendar("DEMO CALENDAR")
print(_calendar)
5 changes: 4 additions & 1 deletion docs/AUTHORS.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
Authors\n=======\n\nA huge thanks to all of our contributors:\n
Authors
=======

A huge thanks to all of our contributors:
- Zaharia Constantin
Binary file added docs/img/basic_dialog.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img/calendar_dialog.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img/progressbar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "sgzenity"
version = "0.1.7"
version = "0.1.8"
description = "sgzentry is a library for python which was inspired by Zenity. When you write scripts, you can use sgzentry to create simple dialogs that interact graphically with the user."
homepage="https://github.com/SoftGeekRO/sgzenity"
repository="https://github.com/SoftGeekRO/sgzenity.git"
Expand Down
130 changes: 91 additions & 39 deletions src/sgzenity/SGProgresBar.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,77 +7,129 @@

from gi.repository import GLib, GObject, Gtk

from .base import Base
from .thread import WorkerThread

DEFAULT_WIDTH = 300
DEFAULT_HEIGHT = 60
BORDER_WIDTH = 10

class SGProgressBar(Base):

def __init__(self, title, text, pulse_mode=False, callback=None, *args, **kwargs):
class ProgressBar(Gtk.Window):

def __init__(
self,
title,
text=None,
pulse_mode=True,
width=DEFAULT_WIDTH,
height=DEFAULT_HEIGHT,
border_width=BORDER_WIDTH,
parent=None,
**kwargs,
):
super().__init__(**kwargs)

self.title = title
self.text = text
self.show_text = True if self.text else False
self.pulse_mode = pulse_mode
self.callback = callback

self.dialog = Gtk.ProgressBar()
self.vbox.pack_start(self.dialog, True, True, 0)
self.border_width = border_width
self.width = width
self.height = height
self.pulses = 10
self._count = 0
self.workthread = None

self.timeout_id = GLib.timeout_add(50, self.update_progress, None)
# Create the GUI
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
self.add(vbox)

self.init_progressbar()
self.progressbar = Gtk.ProgressBar()
vbox.pack_start(self.progressbar, True, True, 0)

# Start the background thread.
self.worker = WorkerThread(self)
self.worker.start()
self.timeout_id = GLib.timeout_add(50, self.on_timeout, None)

def run_progressbar(self):
GObject.threads_init()
self.show_all()
Gtk.main()
self.connect("destroy", self.cancel)

def refresh_in_thread(self):
GObject.idle_add(self.update_progress)
self.init()

def init_progressbar(self):
# global config for progress bar
self.set_border_width(10)
def init(self):
self.set_border_width(self.border_width)
self.set_resizable(False)
self.set_default_size(self.width, self.height)

if self.pulse_mode:
self.dialog.pulse()
self.progressbar.pulse()
else:
self.dialog.set_fraction(0.0)
self.progressbar.set_fraction(0.0)

if self.title:
self.set_title(self.title)

if self.show_text:
self.dialog.set_text(self.text)
self.dialog.set_show_text(self.show_text)
if self.text:
self.progressbar.set_text(self.text)
self.progressbar.set_show_text(True if self.text else False)

self.connect("destroy", self._destroy)
self.connect("destroy", self.cancel)

def update_progress(self, progress=None):
"""
def show(self, workthread):
"""Show loading window.
This needs to be called frm Gtk main thread.
Show the loading dialog just before starting the workthread.
:param workthread:
:return:
"""
if self.workthread is not None:
print(
'There is a workthread active. Please call close() or cancel() before starting a new loading event.'
)
return False

callback = self.callback() if callable(self.callback) else None
if callback >= 1:
time.sleep(0.1)
self._destroy()
if workthread is not None:
if not isinstance(workthread, WorkerThread):
raise Exception('The thread needs to be a subclass of WorkingThread.')
self.workthread = workthread

self.show_all()

return False

def on_timeout(self, progress=None):
if self.pulse_mode:
self.dialog.pulse()
self.progressbar.pulse()
else:
self.dialog.set_fraction(callback)
return False
self.progressbar.set_fraction(self._count)
return self.pulse_mode

def heartbeat(self, text=None):

def _destroy(self, widget=None):
self.worker.done = True
if self.pulse_mode:
return False

self._count += 0.1

if text is None:
text = '{0:0.1f}%'.format(self._count * 100)

GLib.idle_add(self.progressbar.set_fraction, self._count)
GLib.idle_add(self.progressbar.set_text, text)

def close(self):
"""Close the loading window.
This should be called when the workthread has finished it's work.
This can be called outside the Gtk main thread.
"""
self.workthread = None
Gtk.main_quit()

def cancel(self, widget=None):
"""Close the loading window.
This should be called when the workthread has finished it's work.
This can be called outside the Gtk main thread.
"""
if self.workthread is not None:
self.workthread.cancel()
self.close()
2 changes: 1 addition & 1 deletion src/sgzenity/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from .sgszenity import (
ProgressBar,
calendar,
color_selection,
entry,
error,
file_selection,
message,
password,
progress_bar,
question,
scale,
sglist,
Expand Down
Loading

0 comments on commit 7c64ac7

Please sign in to comment.