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.

113 lines
3.6KB

  1. """
  2. qt_widgets.py - some simple widgets generated by QT
  3. """
  4. import os
  5. import sys
  6. from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QFileDialog, QMessageBox
  7. class SimpleWidgets(QWidget):
  8. """ Simple widgets """
  9. def __init__(self):
  10. app_name = os.path.basename(sys.argv[0])
  11. base, ext = os.path.splitext(app_name)
  12. if ext.lower() in ('.py', '.pyw', '.exe'):
  13. app_name = base
  14. if sys.platform.startswith("darwin"):
  15. # Set app name, if PyObjC is installed
  16. # Python 3: pip3 install pyobjc-framework-Cocoa
  17. try:
  18. from Foundation import NSBundle
  19. bundle = NSBundle.mainBundle()
  20. if bundle:
  21. app_info = bundle.localizedInfoDictionary() or bundle.infoDictionary()
  22. if app_info:
  23. app_info['CFBundleName'] = app_name
  24. except ImportError:
  25. pass
  26. app = QApplication([app_name])
  27. super().__init__()
  28. self._app = app
  29. self._app_name = app_name
  30. self.move(300, 200)
  31. def alert(self, title, text):
  32. """ show alert """
  33. if title is None:
  34. title = self._app_name
  35. QMessageBox.critical(self, title, text)
  36. def info(self, title, text):
  37. """ show info """
  38. if title is None:
  39. title = self._app_name
  40. QMessageBox.information(self, title, text)
  41. def get_text(self, title, label, default=""):
  42. """ get text """
  43. if title is None:
  44. title = self._app_name
  45. val, ok_btn = QInputDialog.getText(self, title, label, text=default)
  46. if not ok_btn:
  47. val = None
  48. return val
  49. def get_int(self, title, label, default=0,
  50. min_val=-2147483647, max_val=2147483647):
  51. """ get integer """
  52. if title is None:
  53. title = self._app_name
  54. val, ok_btn = QInputDialog.getInt(self, title, label, default,
  55. min_val, max_val)
  56. if not ok_btn:
  57. val = None
  58. return val
  59. def get_float(self, title, label, default=0.0,
  60. min_val=-2147483647.0, max_val=2147483647.0):
  61. """ get float """
  62. if title is None:
  63. title = self._app_name
  64. val, ok_btn = QInputDialog.getDouble(self, title, label, default,
  65. min_val, max_val, decimals=6)
  66. if not ok_btn:
  67. val = None
  68. return val
  69. def get_open_filename(self, title, start_dir="", filters=None):
  70. """
  71. get filename to open
  72. filters is a sequence of (label, pattern) tuples.
  73. """
  74. if title is None:
  75. title = self._app_name
  76. start_dir = os.path.expanduser(start_dir)
  77. if filters:
  78. filt_str = ";;".join("{} ({})".format(f[0], f[1]) for f in filters)
  79. else:
  80. filt_str = ""
  81. val = QFileDialog.getOpenFileName(self, title, start_dir, filt_str)[0]
  82. if not val:
  83. val = None
  84. return val
  85. def get_save_filename(self, title, start_dir="", filters=None):
  86. """
  87. get filename to save
  88. filters is a sequence of (label, pattern) tuples.
  89. """
  90. if title is None:
  91. title = self._app_name
  92. start_dir = os.path.expanduser(start_dir)
  93. if filters:
  94. filt_str = ";;".join("{} ({})".format(f[0], f[1]) for f in filters)
  95. else:
  96. filt_str = ""
  97. val = QFileDialog.getSaveFileName(self, title, start_dir, filt_str)[0]
  98. if not val:
  99. val = None
  100. return val