openmedialibrary/oml/ui.py

115 lines
3.6 KiB
Python
Raw Normal View History

2014-05-16 15:48:48 +00:00
# encoding: utf-8
2019-01-21 05:09:08 +00:00
import sys
import os
2014-05-16 15:48:48 +00:00
try:
2019-01-23 08:07:03 +00:00
import gi
gi.require_version('Gtk', '3.0')
2014-05-16 15:48:48 +00:00
from gi.repository import Gtk, GObject
use_Gtk = True
except:
from tkinter import Tk, PhotoImage
2014-09-02 22:32:44 +00:00
import tkinter.filedialog
2014-05-16 15:48:48 +00:00
use_Gtk = False
2019-01-21 05:09:08 +00:00
DEBUG = False
2019-01-23 08:07:03 +00:00
def short_home(path):
home = os.path.expanduser('~')
if path and path.startswith(home):
path = path.replace(home, '~')
return path
2014-05-16 15:48:48 +00:00
class GtkUI:
def selectFolder(self, data):
2019-01-23 08:07:03 +00:00
dialog = Gtk.FileChooserDialog(title=data.get("title", "Select Folder"),
action=Gtk.FileChooserAction.SELECT_FOLDER)
dialog.add_buttons(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN, Gtk.ResponseType.OK)
2014-05-16 15:48:48 +00:00
dialog.set_default_response(Gtk.ResponseType.OK)
response = dialog.run()
if response == Gtk.ResponseType.OK:
filename = dialog.get_filename()
if DEBUG:
2014-09-02 22:32:44 +00:00
print(filename, 'selected')
2014-05-16 15:48:48 +00:00
elif response == Gtk.ResponseType.CANCEL:
if DEBUG:
2014-09-02 22:32:44 +00:00
print('Closed, no files selected')
2014-05-16 15:48:48 +00:00
filename = None
2019-01-23 08:37:02 +00:00
else:
filename = None
2014-05-16 15:48:48 +00:00
dialog.destroy()
while Gtk.events_pending():
Gtk.main_iteration()
if DEBUG:
2014-09-02 22:32:44 +00:00
print("done")
2019-01-23 08:07:03 +00:00
return short_home(filename)
2014-05-16 15:48:48 +00:00
def selectFile(self, data):
2019-01-23 08:07:03 +00:00
dialog = Gtk.FileChooserDialog(title=data.get("title", "Select File"),
action=Gtk.FileChooserAction.OPEN)
dialog.add_buttons(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN, Gtk.ResponseType.OK)
2014-05-16 15:48:48 +00:00
dialog.set_default_response(Gtk.ResponseType.OK)
response = dialog.run()
if response == Gtk.ResponseType.OK:
filename = dialog.get_filename()
if DEBUG:
2014-09-02 22:32:44 +00:00
print(filename, 'selected')
2014-05-16 15:48:48 +00:00
elif response == Gtk.ResponseType.CANCEL:
if DEBUG:
2014-09-02 22:32:44 +00:00
print('Closed, no files selected')
2014-05-16 15:48:48 +00:00
filename = None
2019-01-23 08:37:02 +00:00
else:
filename = None
2014-05-16 15:48:48 +00:00
dialog.destroy()
while Gtk.events_pending():
Gtk.main_iteration()
if DEBUG:
2014-09-02 22:32:44 +00:00
print("done")
2019-01-23 08:07:03 +00:00
return short_home(filename)
2014-05-16 15:48:48 +00:00
2014-05-16 15:48:48 +00:00
class TkUI:
def __init__(self):
self.root = Tk(className="Open Media Library")
2019-01-24 06:42:36 +00:00
#png = os.path.join(os.path.dirname(os.path.abspath('__file__')), 'static', 'png', 'oml.png')
#icon = PhotoImage(file=png)
#self.root.tk.call('wm', 'iconphoto', self.root._w, icon)
self.root.withdraw() # hiding tkinter window
2019-01-21 05:09:08 +00:00
if sys.platform == 'darwin':
self.root.lift()
self.root.call('wm', 'attributes', '.', '-topmost', True)
self.root.update()
self.root.after_idle(self.root.call, 'wm', 'attributes', '.', '-topmost', False)
2016-06-24 10:27:05 +00:00
2014-05-16 15:48:48 +00:00
def selectFolder(self, data):
2019-01-23 08:07:03 +00:00
folder = tkinter.filedialog.askdirectory(parent=self.root, title=data.get("title", "Select Folder"))
return short_home(folder)
2014-05-16 15:48:48 +00:00
def selectFile(self, data):
2019-01-23 08:07:03 +00:00
filename = tkinter.filedialog.askopenfilename(parent=self.root, title=data.get("title", "Select File"))
return short_home(filename)
2019-01-31 13:41:13 +00:00
def main(args):
2019-01-23 08:07:03 +00:00
base = '~'
2019-01-31 13:41:13 +00:00
if len(args) >= 2:
base = args[1]
2019-01-23 08:07:03 +00:00
base = os.path.expanduser(base)
2019-01-25 09:17:36 +00:00
if os.path.exists(base):
os.chdir(base)
2019-01-31 13:41:13 +00:00
if args and args[0] == 'folder':
2014-09-02 22:32:44 +00:00
print(ui.selectFolder({}))
2014-05-16 15:48:48 +00:00
else:
2014-09-02 22:32:44 +00:00
print(ui.selectFile({}))
2019-01-31 13:41:13 +00:00
if use_Gtk:
ui = GtkUI()
else:
ui = TkUI()
if __name__ == '__main__':
main(sys.argv[1:])