|
17 | 17 | # |
18 | 18 | import json |
19 | 19 |
|
20 | | -from qtpy.QtWidgets import QWidget |
| 20 | +from qtpy.QtWidgets import QFileDialog, QWidget |
21 | 21 |
|
22 | 22 | from dpp.core.plugin import PluginType |
23 | 23 | from dpp.ui.view.classic import CodecTab |
@@ -129,41 +129,47 @@ def _focus_input_text(self, callback): |
129 | 129 | # Connector functions |
130 | 130 | ############################################# |
131 | 131 |
|
132 | | - def _open_file_action(self) -> str: |
133 | | - filename = super()._open_file_action() |
134 | | - if filename: |
135 | | - try: |
136 | | - with open(filename) as f: |
137 | | - save_file = json.loads(f.read()) |
138 | | - for tab_config in save_file: |
139 | | - index, tab = self.newTab(title=tab_config["name"]) |
140 | | - frame_index = 0 |
141 | | - for frame_config in tab_config["frames"]: |
142 | | - if frame_index == 0: |
143 | | - # New tabs already contain one empty frame |
144 | | - frame = tab.frames().getFrames()[0] |
145 | | - frame.setInputText(frame_config["text"]) |
146 | | - frame.setStatus(frame_config["status"]["type"], frame_config["status"]["message"]) |
147 | | - else: |
148 | | - frame = tab.frames().newFrame(frame_config["text"], |
149 | | - frame_config["title"], |
150 | | - frame_index, |
151 | | - frame_config["status"]["type"], |
152 | | - frame_config["status"]["message"]) |
153 | | - frame.fromDict(frame_config) |
154 | | - frame_index = frame_index + 1 |
155 | | - self._context.logger.info("Successfully loaded {}!".format(filename)) |
156 | | - except Exception as e: |
157 | | - self._context.logger.error("Unexpected error loading file. {}".format(e)) |
158 | | - |
159 | | - def _save_as_file_action(self) -> str: |
160 | | - filename = super()._save_as_file() |
161 | | - if filename: |
162 | | - try: |
163 | | - self._context.saveAsFile(filename, str(json.dumps(self.toDict(), default=lambda x: x.__dict__))) |
164 | | - self._context.logger.info("Successfully saved session in {}!".format(filename)) |
165 | | - except Exception as e: |
166 | | - self._context.logger.error("Unexpected error saving file. {}".format(e)) |
| 132 | + @menu.register_menu_item(id=MenuItem.OPEN_FILE, text="&Open File...", shortcut_key="Ctrl+O") |
| 133 | + def _open_file_action(self): |
| 134 | + filename, _ = QFileDialog.getOpenFileName(self, 'Open File') |
| 135 | + if not filename: |
| 136 | + return |
| 137 | + |
| 138 | + try: |
| 139 | + with open(filename) as f: |
| 140 | + save_file = json.loads(f.read()) |
| 141 | + for tab_config in save_file: |
| 142 | + index, tab = self.newTab(title=tab_config["name"]) |
| 143 | + frame_index = 0 |
| 144 | + for frame_config in tab_config["frames"]: |
| 145 | + if frame_index == 0: |
| 146 | + # New tabs already contain one empty frame |
| 147 | + frame = tab.frames().getFrames()[0] |
| 148 | + frame.setInputText(frame_config["text"]) |
| 149 | + frame.setStatus(frame_config["status"]["type"], frame_config["status"]["message"]) |
| 150 | + else: |
| 151 | + frame = tab.frames().newFrame(frame_config["text"], |
| 152 | + frame_config["title"], |
| 153 | + frame_index, |
| 154 | + frame_config["status"]["type"], |
| 155 | + frame_config["status"]["message"]) |
| 156 | + frame.fromDict(frame_config) |
| 157 | + frame_index = frame_index + 1 |
| 158 | + self._context.logger.info("Successfully loaded {}!".format(filename)) |
| 159 | + except Exception as e: |
| 160 | + self._context.logger.error("Unexpected error loading file. {}".format(e)) |
| 161 | + |
| 162 | + @menu.register_menu_item(id=MenuItem.SAVE_AS_FILE, text="&Save As...", shortcut_key="Ctrl+S") |
| 163 | + def _save_as_file_action(self): |
| 164 | + filename, _ = QFileDialog.getSaveFileName(self, 'Save As File') |
| 165 | + if not filename: |
| 166 | + return |
| 167 | + |
| 168 | + try: |
| 169 | + self._context.saveAsFile(filename, str(json.dumps(self.toDict(), default=lambda x: x.__dict__))) |
| 170 | + self._context.logger.info("Successfully saved session in {}!".format(filename)) |
| 171 | + except Exception as e: |
| 172 | + self._context.logger.error("Unexpected error saving file. {}".format(e)) |
167 | 173 |
|
168 | 174 | ############################################# |
169 | 175 | # Public functions |
|
0 commit comments