Files
h2inc-old/Gui test/h2inc_gtk.py
Lerking 30894e1b39 h2inc_gtk.py split
h2inc_gtk.py split into h2inc_globals.py (global variables), h2inc_mp.py (multiprocessing) and h2inc_fp.py (file processing)
2018-05-08 11:01:39 +02:00

192 lines
7.2 KiB
Python
Executable File

#!/usr/bin/env python3.5
# You are free to use and/or change this code for
# your own needs.
# Original code (c)2018 Jan Lerking
# Program to convert C-header (*.h) files to nasm include files (*.inc),
# for direct usage in assembly programming using nasm/yasm.
import sys
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gio, GObject as gobject
#import time
from h2inc_globals import defsrc, destdir, defdest, filelist
from h2inc_mp import Worker, start_workers
from h2inc_fp import sourcedir_filecnt, sourcedir_foldercnt
class H2INC:
global app
global destlabel
def __init__(self):
self.filecnt = 0
self.fileindex = 0
self.process = None
self.app = Gtk.Application.new("org.h2inc", Gio.ApplicationFlags(0))
self.app.connect("activate", self.on_app_activate)
self.app.connect("shutdown", self.on_app_shutdown)
app = self.app
def on_app_activate(self, app):
builder = Gtk.Builder()
builder.add_from_file("h2inc.glade")
builder.connect_signals(self)
self.obj = builder.get_object
self.obj("window").set_application(app)
self.obj("window").set_wmclass("h2inc_gtk","h2inc_gtk")
self.obj("window").show_all()
self.obj("source_entry").set_text(defsrc)
self.obj("default_dir_checkbutton").set_active(False)
self.obj("include_checkbutton").set_active(True)
button = Gtk.Button.new_from_stock(Gtk.STOCK_CANCEL)
button.set_property("can-default",True)
button = Gtk.Button.new_from_stock(Gtk.STOCK_APPLY)
button.set_property("can-default",True)
def on_app_shutdown(self, app):
self.app.quit()
def run(self, argv):
self.app.run(argv)
def callbackDisplay(self, obj, fraction, text, index, data=None):
self.obj("progress_label").set_text("{} of {}".format(index+1, self.filecnt))
self.obj("progressbar").set_fraction(fraction)
def callbackFinished(self, obj, data=None):
if self.process==None:
raise RuntimeError("No worker process started")
print("all done; joining worker process")
self.process.join()
self.process = None
self.obj("progressbar").set_fraction(1.0)
self.obj("sourceframe").set_sensitive(True)
self.obj("translate_button").set_sensitive(True)
self.obj("destination_label").set_sensitive(True)
self.obj("destination_entry").set_sensitive(True)
self.obj("destination_button").set_sensitive(True)
self.obj("include_checkbutton").set_sensitive(True)
self.obj("translation_frame").set_sensitive(True)
def on_window_destroy(self,window):
window.close()
def on_dialog_close(self,widget,*event):
widget.hide_on_delete()
return True
def on_filechooser_dialog_response(self,widget,response):
if response == -6:
print("Cancel")
elif response == -5:
print("File selection: %s" % widget.get_filename())
self.on_dialog_close(widget)
def on_source_button_clicked(self,widget):
dialog = Gtk.FileChooserDialog("Select source directory!",
self.obj("window"),
Gtk.FileChooserAction.SELECT_FOLDER,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_APPLY, Gtk.ResponseType.OK))
dialog.set_default_size(600, 300)
Gtk.FileChooser.set_filename(dialog, srcdir)
response = dialog.run()
if response == Gtk.ResponseType.OK:
self.filecnt = sourcedir_filecnt(dialog.get_filename())
if self.filecnt >0:
print(self.filecnt)
self.obj("source_entry").set_text(dialog.get_filename())
self.obj("destination_label").set_sensitive(True)
self.obj("destination_entry").set_sensitive(True)
self.obj("destination_button").set_sensitive(True)
self.obj("numfiles_label").set_text(str(self.filecnt))
self.obj("progress_label").set_text("{} of {}".format(fileindex, self.filecnt))
foldercnt = sourcedir_foldercnt(dialog.get_filename())
if foldercnt >0:
self.obj("numfolders_label").set_text(str(foldercnt))
elif response == Gtk.ResponseType.CANCEL:
print("Cancel")
dialog.destroy()
def on_destination_button_clicked(self,widget):
global destdir
global incinc
dialog = Gtk.FileChooserDialog("Select destination directory!",
self.obj("window"),
Gtk.FileChooserAction.SELECT_FOLDER,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_APPLY, Gtk.ResponseType.OK))
dialog.set_default_size(600, 300)
response = dialog.run()
if response == Gtk.ResponseType.OK:
destdir = dialog.get_filename()
self.obj("destination_entry").set_text(dialog.get_filename())
self.obj("include_checkbutton").set_sensitive(True)
self.obj("translation_frame").set_sensitive(True)
print(srcdir)
if self.obj("include_checkbutton").get_active() == True:
incinc = '/include'
destdir = destdir+incinc
print(destdir)
elif response == Gtk.ResponseType.CANCEL:
print("Cancel")
dialog.destroy()
def on_include_checkbutton_toggled(self, widget):
global destdir
global incinc
if self.obj("include_checkbutton").get_active() == True:
incinc = '/include'
destdir = destdir+incinc
if self.obj("include_checkbutton").get_active() == False:
incinc = ''
destdir = destdir.replace('/include', '')
self.obj("destination_entry").set_text(destdir)
print(destdir)
def on_default_dir_checkbutton_toggled(self, widget):
global defdir
global srcdir
global destdir
global incinc
defdir = self.obj("default_dir_checkbutton").get_active()
if defdir == True:
srcdir = defsrc
self.obj("source_entry").set_text(srcdir)
destdir = defdest+incinc
self.obj("destination_entry").set_text(destdir)
self.obj("include_checkbutton").set_sensitive(True)
self.obj("source_label").set_sensitive(False)
self.obj("source_entry").set_sensitive(False)
self.obj("source_button").set_sensitive(False)
self.obj("translation_frame").set_sensitive(True)
filecnt = sourcedir_filecnt(defsrc)
if filecnt >0:
print(filecnt)
foldercnt = sourcedir_foldercnt(defsrc)
if foldercnt >0:
self.obj("numfolders_label").set_text(str(foldercnt))
def on_translate_button_clicked(self, widget, data=None):
self.obj("sourceframe").set_sensitive(False)
self.obj("translate_button").set_sensitive(False)
start_workers()
app = H2INC()
app.run(sys.argv)