Collection of GNS3 GUI Tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.6 KiB

"""
qt_widgets.py - some simple widgets generated by QT
"""
import os
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QFileDialog, QMessageBox
class SimpleWidgets(QWidget):
""" Simple widgets """
def __init__(self):
app_name = os.path.basename(sys.argv[0])
base, ext = os.path.splitext(app_name)
if ext.lower() in ('.py', '.pyw', '.exe'):
app_name = base
if sys.platform.startswith("darwin"):
# Set app name, if PyObjC is installed
# Python 3: pip3 install pyobjc-framework-Cocoa
try:
from Foundation import NSBundle
bundle = NSBundle.mainBundle()
if bundle:
app_info = bundle.localizedInfoDictionary() or bundle.infoDictionary()
if app_info:
app_info['CFBundleName'] = app_name
except ImportError:
pass
app = QApplication([app_name])
super().__init__()
self._app = app
self._app_name = app_name
self.move(300, 200)
def alert(self, title, text):
""" show alert """
if title is None:
title = self._app_name
QMessageBox.critical(self, title, text)
def info(self, title, text):
""" show info """
if title is None:
title = self._app_name
QMessageBox.information(self, title, text)
def get_text(self, title, label, default=""):
""" get text """
if title is None:
title = self._app_name
val, ok_btn = QInputDialog.getText(self, title, label, text=default)
if not ok_btn:
val = None
return val
def get_int(self, title, label, default=0,
min_val=-2147483647, max_val=2147483647):
""" get integer """
if title is None:
title = self._app_name
val, ok_btn = QInputDialog.getInt(self, title, label, default,
min_val, max_val)
if not ok_btn:
val = None
return val
def get_float(self, title, label, default=0.0,
min_val=-2147483647.0, max_val=2147483647.0):
""" get float """
if title is None:
title = self._app_name
val, ok_btn = QInputDialog.getDouble(self, title, label, default,
min_val, max_val, decimals=6)
if not ok_btn:
val = None
return val
def get_open_filename(self, title, start_dir="", filters=None):
"""
get filename to open
filters is a sequence of (label, pattern) tuples.
"""
if title is None:
title = self._app_name
start_dir = os.path.expanduser(start_dir)
if filters:
filt_str = ";;".join("{} ({})".format(f[0], f[1]) for f in filters)
else:
filt_str = ""
val = QFileDialog.getOpenFileName(self, title, start_dir, filt_str)[0]
if not val:
val = None
return val
def get_save_filename(self, title, start_dir="", filters=None):
"""
get filename to save
filters is a sequence of (label, pattern) tuples.
"""
if title is None:
title = self._app_name
start_dir = os.path.expanduser(start_dir)
if filters:
filt_str = ";;".join("{} ({})".format(f[0], f[1]) for f in filters)
else:
filt_str = ""
val = QFileDialog.getSaveFileName(self, title, start_dir, filt_str)[0]
if not val:
val = None
return val