Skip to content

Commit

Permalink
Do some refactoring and fix key bindings and play menu
Browse files Browse the repository at this point in the history
  • Loading branch information
innir committed Jun 12, 2020
1 parent 7a9c06d commit 0e26be2
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 218 deletions.
197 changes: 112 additions & 85 deletions bin/gtranscribe

Large diffs are not rendered by default.

82 changes: 29 additions & 53 deletions data/ui/gTranscribe.glade
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<!-- Generated with glade 3.22.2 -->
<interface>
<requires lib="gtk+" version="3.12"/>
<object class="GtkAdjustment" id="adjustment_seek">
Expand All @@ -14,29 +14,14 @@
<property name="step_increment">0.10000000000000001</property>
<property name="page_increment">0.10000000000000001</property>
</object>
<object class="GtkImage" id="image2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="stock">gtk-file</property>
</object>
<object class="GtkImage" id="image4">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="stock">gtk-go-down</property>
</object>
<object class="GtkImage" id="image5">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="stock">gtk-go-up</property>
</object>
<object class="GtkWindow" id="gtranscribe_window">
<property name="can_focus">False</property>
<property name="title">gTranscribe</property>
<property name="default_width">450</property>
<property name="default_height">550</property>
<signal name="delete-event" handler="quit" swapped="no"/>
<signal name="destroy" handler="quit" swapped="no"/>
<child>
<child type="titlebar">
<placeholder/>
</child>
<child>
Expand All @@ -61,34 +46,30 @@
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkImageMenuItem" id="open">
<property name="label">gtk-open</property>
<object class="GtkMenuItem" id="open">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Open</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
<signal name="activate" handler="open" swapped="no"/>
<accelerator key="o" signal="activate" modifiers="GDK_CONTROL_MASK"/>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="open_text">
<property name="label" translatable="yes">Open Text</property>
<object class="GtkMenuItem" id="open_text">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Open Text</property>
<property name="use_underline">True</property>
<property name="image">image2</property>
<property name="use_stock">False</property>
<signal name="activate" handler="open_text" swapped="no"/>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="save">
<property name="label">gtk-save</property>
<object class="GtkMenuItem" id="save">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Save</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
<signal name="activate" handler="save_text" swapped="no"/>
<accelerator key="s" signal="activate" modifiers="GDK_CONTROL_MASK"/>
</object>
Expand All @@ -100,12 +81,11 @@
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="quit">
<property name="label">gtk-quit</property>
<object class="GtkMenuItem" id="quit">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Quit</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
<signal name="activate" handler="quit" swapped="no"/>
<accelerator key="q" signal="activate" modifiers="GDK_CONTROL_MASK"/>
</object>
Expand All @@ -125,69 +105,62 @@
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkImageMenuItem" id="play">
<property name="label">gtk-media-play</property>
<object class="GtkMenuItem" id="play">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="events">GDK_BUTTON_PRESS_MASK | GDK_STRUCTURE_MASK</property>
<property name="label" translatable="yes">Play</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
<signal name="activate" handler="play" object="button_play" swapped="no"/>
<accelerator key="space" signal="activate" modifiers="GDK_CONTROL_MASK"/>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="slower">
<property name="label" translatable="yes">Slower</property>
<object class="GtkMenuItem" id="slower">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Slower</property>
<property name="use_underline">True</property>
<property name="image">image4</property>
<property name="use_stock">False</property>
<signal name="activate" handler="dec_speed" swapped="no"/>
<accelerator key="d" signal="activate" modifiers="GDK_CONTROL_MASK"/>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="faster">
<property name="label" translatable="yes">Faster</property>
<object class="GtkMenuItem" id="faster">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Faster</property>
<property name="use_underline">True</property>
<property name="image">image5</property>
<property name="use_stock">False</property>
<signal name="activate" handler="inc_speed" swapped="no"/>
<accelerator key="f" signal="activate" modifiers="GDK_CONTROL_MASK"/>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="forward">
<property name="label">gtk-media-forward</property>
<object class="GtkMenuItem" id="forward">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Forward</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
<signal name="activate" handler="forward" swapped="no"/>
<accelerator key="Right" signal="activate" modifiers="GDK_SHIFT_MASK | GDK_CONTROL_MASK"/>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="rewind">
<property name="label">gtk-media-rewind</property>
<object class="GtkMenuItem" id="rewind">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Rewind</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
<signal name="activate" handler="rewind" swapped="no"/>
<accelerator key="Left" signal="activate" modifiers="GDK_SHIFT_MASK | GDK_CONTROL_MASK"/>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="jump">
<property name="label">gtk-jump-to</property>
<object class="GtkMenuItem" id="jump">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Jump To</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
<signal name="activate" handler="jump" swapped="no"/>
<accelerator key="j" signal="activate" modifiers="GDK_CONTROL_MASK"/>
</object>
Expand All @@ -207,12 +180,11 @@
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkImageMenuItem" id="about">
<property name="label">gtk-about</property>
<object class="GtkMenuItem" id="about">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">About</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
<signal name="activate" handler="about" swapped="no"/>
</object>
</child>
Expand All @@ -239,6 +211,8 @@
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label">00:00.0</property>
<property name="width_chars">6</property>
<property name="xalign">0</property>
</object>
<packing>
<property name="expand">False</property>
Expand Down Expand Up @@ -309,6 +283,8 @@
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label">00:00.0</property>
<property name="width_chars">6</property>
<property name="xalign">1</property>
</object>
<packing>
<property name="expand">False</property>
Expand Down
40 changes: 17 additions & 23 deletions gtranscribe/helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# gTranscribe is a software focussed on easy transcription of spoken words.
# Copyright (C) 2013-2018 Philip Rinn <rinni@inventati.org>
# gTranscribe is a software focused on easy transcription of spoken words.
# Copyright (C) 2013-2020 Philip Rinn <rinni@inventati.org>
# Copyright (C) 2010 Frederik Elwert <frederik.elwert@web.de>
#
# This program is free software: you can redistribute it and/or modify
Expand All @@ -14,9 +14,10 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

#pylint: disable=wrong-import-position
import os.path
import datetime
import hashlib
from hashlib import md5
import re
import gettext
from gettext import gettext as _
Expand All @@ -27,19 +28,16 @@


def trim(timestring, digits=1):
"""
Trim a time string to contain only a given number of digits
"""
"""Trim a time string to contain only a given number of digits."""
pos = re.search("[.-]", timestring).start()
if pos > -1:
return timestring[:pos + 1 + digits]
else:
return timestring
return timestring


def ns_to_time(ns):
"""
Converts nanoseconds to a datetime.time object.
Convert nanoseconds to a datetime.time object.
:Parameters:
- 'ns': Nanoseconds as int.
Expand All @@ -59,7 +57,7 @@ def ns_to_time(ns):

def time_to_ns(time):
"""
Converts a datetime.time object to nanoseconds.
Convert a datetime.time object to nanoseconds.
:Parameters:
- 'time': A datetime.time object.
Expand All @@ -76,7 +74,7 @@ def time_to_ns(time):


def get_open_filename(self, title, filter_name, filter_mime):
"""Displays a file open dialog and returns the filename."""
"""Display a file open dialog and returns the filename."""
filename = None
chooser = Gtk.FileChooserDialog(title, self.window,
Gtk.FileChooserAction.OPEN,
Expand All @@ -96,7 +94,7 @@ def get_open_filename(self, title, filter_name, filter_mime):


def get_save_filename(self):
"""Displays a file save dialog and returns the filename."""
"""Display a file save dialog and returns the filename."""
filename = None
chooser = Gtk.FileChooserDialog(_("Save Text File"), self.window,
Gtk.FileChooserAction.SAVE,
Expand All @@ -111,7 +109,7 @@ def get_save_filename(self):


def error_message(self, message):
"""Displays the string 'message' in an error dialog."""
"""Display the string 'message' in an error dialog."""
dialog = Gtk.MessageDialog(self.window,
Gtk.DialogFlags.MODAL |
Gtk.DialogFlags.DESTROY_WITH_PARENT,
Expand All @@ -123,23 +121,19 @@ def error_message(self, message):

def md5_of_file(fname):
"""
Calculate the md5 hash of the given file
Calculate the md5 hash of the given file.
:Parameters:
- 'fname': filename.
:Return:
- A string of length 32, containing the md5 hash as hexadecimal digits.
"""
md5 = hashlib.md5()
f = open(fname, 'rb')
while True:
data = f.read(81920)
if not data:
break
md5.update(data)
f.close()
return md5.hexdigest()
md5_hash = md5()
with open(fname, "rb") as data:
for chunk in iter(lambda: data.read(81920), b""):
md5_hash.update(chunk)
return md5_hash.hexdigest()


def get_data_file(root_dir, *path_segments):
Expand Down
39 changes: 17 additions & 22 deletions gtranscribe/metadata.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# gTranscribe is a software focussed on easy transcription of spoken words.
# Copyright (C) 2013-2016 Philip Rinn <rinni@inventati.org>
# gTranscribe is a software focused on easy transcription of spoken words.
# Copyright (C) 2013-2020 Philip Rinn <rinni@inventati.org>
# Copyright (C) 2010 Frederik Elwert <frederik.elwert@web.de>
#
# This program is free software: you can redistribute it and/or modify
Expand All @@ -24,7 +24,7 @@
database = os.path.join(cache_dir, "metadata.db")


class MetaData(object):
class MetaData():
"""
Query and store information about a given file.
Expand All @@ -38,9 +38,7 @@ def __init__(self, filepath, md5):
self._cache = {}

def _get_data(self, attribute):
"""
A generic method to hide the verbose process of getting attributes.
"""
"""Hide the verbose process of getting attributes."""
# Use cached values if available to limit requests
if attribute in self._cache:
return self._cache[attribute]
Expand All @@ -56,9 +54,7 @@ def _get_data(self, attribute):
return value

def _set_data(self, attribute, value):
"""
A generic method to hide the verbose process of setting attributes.
"""
"""Hide the verbose process of setting attributes."""
logger.debug('Set attribute "%s": %s', attribute, value)
query = 'UPDATE metadata SET ' + attribute + '=? WHERE md5=?'
con = sqlite3.connect(database)
Expand Down Expand Up @@ -102,17 +98,16 @@ def store_md5(self):
cur.close()
con.close()

def init_db(self):
"""Create the database for meta data if necessary."""
# make sure our cache directory exists
if not os.path.exists(cache_dir):
os.makedirs(cache_dir)

def init_db():
"""Create the database for meta data if necessary."""
# make sure our cache directory exists
if not os.path.exists(cache_dir):
os.makedirs(cache_dir)

con = sqlite3.connect(database)
cur = con.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS metadata(md5 TEXT PRIMARY KEY,\
position INTEGER, speed REAL)')
con.commit()
cur.close()
con.close()
con = sqlite3.connect(database)
cur = con.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS metadata(md5 TEXT PRIMARY KEY,\
position INTEGER, speed REAL)')
con.commit()
cur.close()
con.close()
Loading

0 comments on commit 0e26be2

Please sign in to comment.