class LoadPlugins(QDialog): def __init__(self, plugin_mgr, parent=None): super(LoadPlugins, self).__init__(parent) self._pm = plugin_mgr # type: PluginManager self._installed_plugin_list = None # type: QListWidget self.setWindowTitle('Installed Plugins') self.main_layout = QVBoxLayout() self._init_widgets() self._populate_installed_plugin_list() self.setLayout(self.main_layout) # # Private methods # def _init_plugin_list(self): plugin_group = QGroupBox("Plugins") self._installed_plugin_list = QListWidget(self) sublayout = QVBoxLayout() sublayout.addWidget(self._installed_plugin_list) plugin_group.setLayout(sublayout) layout = QVBoxLayout() # layout.addWidget(auto_load_libs) layout.addWidget(plugin_group) layout.addStretch(0) frame = QFrame(self) frame.setLayout(layout) self.main_layout.addWidget(frame) def _populate_installed_plugin_list(self): for cls in self._pm.loaded_plugins: plugin_item = QPluginListWidgetItem(plugin_cls=cls) if self._pm.get_plugin_instance(cls) is not None: plugin_item.setCheckState(Qt.Checked) else: plugin_item.setCheckState(Qt.Unchecked) self._installed_plugin_list.addItem(plugin_item) def _init_widgets(self): load_button = QPushButton(self) load_button.setText("Load Plugin") load_button.clicked.connect(self._on_load_clicked) self.main_layout.addWidget(load_button) self._init_plugin_list() # buttons ok_button = QPushButton(self) ok_button.setText('OK') ok_button.clicked.connect(self._on_ok_clicked) cancel_button = QPushButton(self) cancel_button.setText('Cancel') cancel_button.clicked.connect(self._on_cancel_clicked) buttons_layout = QHBoxLayout() buttons_layout.addWidget(ok_button) buttons_layout.addWidget(cancel_button) self.main_layout.addLayout(buttons_layout) # # Event handlers # def _on_ok_clicked(self): list_items = self._installed_plugin_list.findItems( '*', Qt.MatchWildcard) # type: List[QPluginListWidgetItem] for i in list_items: checked = i.checkState() == Qt.Checked if checked and self._pm.get_plugin_instance( i.plugin_class) is None: self._pm.activate_plugin(i.plugin_class) elif not checked and self._pm.get_plugin_instance( i.plugin_class) is not None: self._pm.deactivate_plugin(i.plugin_class) else: self._pm.load_plugin(i.plugin_class) self.close() def _on_cancel_clicked(self): self.close() def _on_load_clicked(self): file_path, _ = QFileDialog.getOpenFileName( self, "Open a plugin (select __init__.py for packages)", "", "Python files (*.py)") if not file_path: return plugins = load_plugins_from_file(file_path) if not plugins: QMessageBox.warning(self, "Error", "File contained no plugins") return errors = [x for x in plugins if isinstance(x, Exception)] if errors: QMessageBox.warning(self, "Error", "Loading errored with %s" % errors[0]) return for plugin in plugins: plugin_item = QPluginListWidgetItem(plugin_cls=plugin) plugin_item.setCheckState(Qt.Unchecked) self._installed_plugin_list.addItem(plugin_item)
class LoadPlugins(QDialog): def __init__(self, plugin_mgr, parent=None): super(LoadPlugins, self).__init__(parent) self._pm = plugin_mgr self._installed_plugin_list = None # type: Union[None, QListWidget] self.setWindowTitle('Installed Plugins') self.main_layout = QVBoxLayout() self._init_widgets() self._populate_installed_plugin_list() self.setLayout(self.main_layout) # # Private methods # def _init_plugin_list(self): plugin_group = QGroupBox("Plugins") self._installed_plugin_list = QListWidget(self) sublayout = QVBoxLayout() sublayout.addWidget(self._installed_plugin_list) plugin_group.setLayout(sublayout) layout = QVBoxLayout() # layout.addWidget(auto_load_libs) layout.addWidget(plugin_group) layout.addStretch(0) frame = QFrame(self) frame.setLayout(layout) self.main_layout.addWidget(frame) def _populate_installed_plugin_list(self): for name, cls in self._pm.installed_plugins.items(): plugin_item = QPluginListWidgetItem(plugin_cls=cls) checked = Qt.Checked if name in self._pm.enabled_plugins.keys( ) else Qt.Unchecked plugin_item.setCheckState(checked) self._installed_plugin_list.addItem(plugin_item) def _init_widgets(self): self._init_plugin_list() # buttons ok_button = QPushButton(self) ok_button.setText('OK') ok_button.clicked.connect(self._on_ok_clicked) cancel_button = QPushButton(self) cancel_button.setText('Cancel') cancel_button.clicked.connect(self._on_cancel_clicked) buttons_layout = QHBoxLayout() buttons_layout.addWidget(ok_button) buttons_layout.addWidget(cancel_button) self.main_layout.addLayout(buttons_layout) # # Event handlers # def _on_ok_clicked(self): # TODO: Unload unchecked plugins. Load checked ones. list_items = self._installed_plugin_list.findItems( '*', Qt.MatchWildcard) # type: List[QPluginListWidgetItem] for i in list_items: checked = i.checkState() == Qt.Checked if checked and i.plugin_class.__name__ not in self._pm.enabled_plugins.keys( ): _l.info("Loading plugin: {}".format( i.plugin_class.get_display_name())) plugin = self._pm.enable_plugin(i.plugin_class.__name__) elif not checked and i.plugin_class.__name__ in self._pm.enabled_plugins.keys( ): _l.info("Disabling plugin: {}".format( i.plugin_class.get_display_name())) self._pm.disable_plugin(i.plugin_class.__name__) self.close() def _on_cancel_clicked(self): self.close()
class ClientWindow(QDialog): def __init__(self): super(ClientWindow, self).__init__() self.resize(823,511) self.setWindowTitle('用户客户端') self.friendList = QListWidget() self.friendList.doubleClicked.connect(self.changeFriendText) self.friendList.clicked.connect(self.changeFriendText) self.portLine = QLineEdit() self.portLine.setText('12345') self.connectBtn = QPushButton('连接') self.connectBtn.clicked.connect(self.setupFun) self.messageText = QTextEdit() self.messageLine = QLineEdit() self.sendBtn = QPushButton('发送') self.sendBtn.clicked.connect(self.sendFun) self.text = '' self.userText = {} self.initUI() self.palette = QPalette() self.palette.setBrush(QPalette.Background, QBrush(QPixmap('./image/background.jpg'))) self.setPalette(self.palette) self.client = Client() self.client.trigger.connect(self.updateTextFun) self.ininData() def changeFriendText(self): currentItem = self.friendList.currentItem().text() self.text = self.userText[currentItem] self.messageText.setText(self.text) def ininData(self): self.friendList.addItem('broadcast') self.userText['broadcast'] = '' self.friendList.setCurrentItem(self.friendList.item(0)) def initUI(self): mainLayout = QVBoxLayout() mainWidget = QWidget() widget1 = QWidget() layout1 = QHBoxLayout() layout1.addWidget(self.portLine) layout1.addWidget(self.connectBtn) widget1.setLayout(layout1) mainLayout.addWidget(widget1) widget2 = QWidget() layout2 = QHBoxLayout() layout2.addWidget(self.messageText) layout2.addWidget(self.friendList) layout2.setStretch(0,2) layout2.setStretch(0,1) widget2.setLayout(layout2) mainLayout.addWidget(widget2) # mainLayout.addStretch(1) widget3 = QWidget() layout3 = QHBoxLayout() layout3.addWidget(self.messageLine) layout3.addWidget(self.sendBtn) widget3.setLayout(layout3) mainLayout.addWidget(widget3) self.setLayout(mainLayout) def setupFun(self): port = int(self.portLine.text()) self.client.setupFun(port) self.client.start() def sendFun(self): addr = self.friendList.currentItem().text() message = self.messageLine.text() if message != 'add' and message != 'del': self.userText[addr] += '我:' + message + '\n' self.changeFriendText() if addr != 'broadcast': addr = ('localhost',int(addr)) self.client.sendFun(message,addr) self.messageLine.setText('') def updateTextFun(self,data): currentItem = self.friendList.currentItem().text() data = json.loads(data) if 'cmd' in data.keys(): if data['cmd'] == 'add': fromUser = str(data['from']) addr = ('127.0.0.1',int(fromUser)) item = self.friendList.findItems(fromUser,Qt.MatchExactly) if fromUser not in self.userText.keys(): self.userText[fromUser] = '' if len(item) == 0: self.friendList.addItem(fromUser) self.client.sendFun('add',addr) # 已添加 elif data['cmd'] == 'del': fromUser = str(data['from']) rows = self.friendList.count() for row in range(rows): if fromUser == self.friendList.item(row).text(): self.friendList.takeItem(row) else: message = data['message'] fromUser = str(data['from']) if fromUser != 'broadcast': addr = ('127.0.0.1', int(fromUser)) item = self.friendList.findItems(fromUser, Qt.MatchExactly) if fromUser not in self.userText.keys(): self.userText[fromUser] = '' if len(item) == 0: self.friendList.addItem(fromUser) self.client.sendFun('add', addr) # 已添加 if message != 'add' and message != 'del': self.userText[fromUser] += fromUser + ':' + message + '\n' if fromUser == currentItem: self.text = self.userText[fromUser] self.messageText.setText(self.text) def closeEvent(self, event:PySide2.QtGui.QCloseEvent): self.client.closeFun()
class SnippetEditor(QDialog): def __init__(self, snippetManager: SnippetManager): super(SnippetEditor, self).__init__() self.snippetManager = snippetManager self.snippetDict = deepcopy(self.snippetManager.codeSnippets) self.setWindowFlag(Qt.WindowContextHelpButtonHint, False) self.setWindowTitle("Edit code snippets") self.setStyleSheet("background-color: #3E3E42; color: white;") self.setFixedSize(700, 400) self.setWindowIcon(QIcon(main.resource_path("resources/app_icon"))) self.listView = QListWidget() self.nameEdit = QLineEdit() self.nameEdit.setStyleSheet( "font-size: 14px; background-color: #1E1E1E; color: white; font-family: comic-sans; border: none;") self.editor = TextEditor() self.editor.setStyleSheet( "font-size: 14px; background-color: #1E1E1E; color: white; font-family: comic-sans; border: none;") self.editor.setTabStopWidth(4 * QFontMetrics(self.font()).width(' ')) self.listView.setStyleSheet("background-color: #2D2D30; color: white;") for snippet in self.snippetManager.getSnippetsAbbs(): self.listView.addItem(SnippetListWidgetItem(snippet)) self.hbox = QHBoxLayout() self.hbox2 = QHBoxLayout() self.hbox3 = QHBoxLayout() self.vbox = QVBoxLayout() self.vbox2 = QVBoxLayout() self.addButton = QPushButton("Add") self.addButton.setStyleSheet("background-color: #2D2D30; color: white;") self.removeButton = QPushButton("Remove") self.removeButton.setStyleSheet("background-color: #2D2D30; color: white;") self.applyButton = QPushButton("Apply") self.applyButton.setStyleSheet("background-color: #2D2D30; color: white;") self.cancelButton = QPushButton("Cancel") self.cancelButton.setStyleSheet("background-color: #2D2D30; color: white;") self.okButton = QPushButton("OK") self.okButton.setStyleSheet("background-color: #2D2D30; color: white;") self.resetButton = QPushButton("Reset to default") self.resetButton.setStyleSheet("background-color: #2D2D30; color: white;") self.vbox.addWidget(self.listView) self.hbox2.addWidget(self.addButton) self.hbox2.addWidget(self.removeButton) self.hbox2.addWidget(self.applyButton) self.vbox.addLayout(self.hbox2) self.vbox2.addWidget(self.nameEdit) self.vbox2.addWidget(self.editor) self.hbox3.addWidget(self.cancelButton) self.hbox3.addWidget(self.resetButton) self.hbox3.addWidget(self.okButton) self.vbox2.addLayout(self.hbox3) self.hbox.addLayout(self.vbox) self.hbox.addLayout(self.vbox2) self.setLayout(self.hbox) self.listView.currentItemChanged.connect(self.updateEditor) self.okButton.clicked.connect(self.okButtonClicked) self.removeButton.clicked.connect(self.removeButtonClicked) self.addButton.clicked.connect(self.addButtonClicked) self.applyButton.clicked.connect(self.appyButtonClicked) self.cancelButton.clicked.connect(self.cancelButtonClicked) self.resetButton.clicked.connect(self.resetButtonClicked) def resetingSnippetsWarning(self): msg = QMessageBox() msg.setStyleSheet("background-color: #2D2D30; color: white;") msg.setParent(None) msg.setModal(True) msg.setWindowTitle("Confirm reset to default") msg.setText("This will delete all snippets that were created by the user!") msg.setInformativeText("Do you want to continue?") msg.setStandardButtons(QMessageBox.Yes | QMessageBox.No) msg.setDefaultButton(QMessageBox.Yes) retValue = msg.exec_() if retValue == QMessageBox.Yes: return True def resetButtonClicked(self): if not self.resetingSnippetsWarning(): return self.snippetDict.clear() for key in SnippetManager.DEFAULT_SNIPPETS: self.snippetDict[key] = SnippetManager.DEFAULT_SNIPPETS[key] self.updateList() def removeButtonClicked(self): snippet = self.listView.currentItem() if snippet: answer = QMessageBox.question(None, "Delete snippet", "Are you sure you want to delete snippet '{}'?".format( str(snippet)), QMessageBox.Yes | QMessageBox.No) if not answer == QMessageBox.Yes: return del self.snippetDict[str(snippet)] self.updateList() def checkSnippetName(self, name, checkSelected=False): if checkSelected: isSelected = name == str(self.listView.currentItem()) else: isSelected = False if not isSelected and name in self.snippetDict: msg = QMessageBox() msg.setStyleSheet("background-color: #2D2D30; color: white;") msg.setModal(True) msg.setIcon(QMessageBox.Critical) msg.setText("Snippet abbreviation already exists.") msg.setWindowTitle("Wrong snippet abbreviation") msg.exec_() return False if ' ' in name: msg = QMessageBox() msg.setStyleSheet("background-color: #2D2D30; color: white;") msg.setModal(True) msg.setIcon(QMessageBox.Critical) msg.setText("Snippet abbreviation cannot contain whitespace characters.") msg.setWindowTitle("Wrong snippet abbreviation") msg.exec_() return False if name.strip() == '': msg = QMessageBox() msg.setStyleSheet("background-color: #2D2D30; color: white;") msg.setModal(True) msg.setIcon(QMessageBox.Critical) msg.setText("Snippet abbreviation cannot be an empty string.") msg.setWindowTitle("Wrong snippet abbreviation") msg.exec_() return False if name.strip() in AsemblerSintaksa.keywords or name.strip() in AsemblerSintaksa.declarations or name.strip() \ in AsemblerSintaksa.registers or name.strip() in CSyntax.functions or name.strip() in CSyntax.keywords_c: if not self.nameOvershadowedMsg(name.strip()): return False return True def nameOvershadowedMsg(self, name): msg = QMessageBox() msg.setStyleSheet("background-color: #2D2D30; color: white;") msg.setParent(None) msg.setModal(True) msg.setWindowTitle("Name overshadowed") msg.setText("Shippet name '{}' overshadows a built in name." "\n\nThis may interfere with auto completion of the overshadowed name.".format(name)) msg.setInformativeText("Do you want to use '{}' for the snippet name regardlessly?".format(name)) msg.setStandardButtons(QMessageBox.Yes | QMessageBox.No) msg.setDefaultButton(QMessageBox.Yes) retValue = msg.exec_() if retValue == QMessageBox.Yes: return True def addButtonClicked(self): name, entered = QInputDialog.getText(None, "Add code snippet", "Enter snippet abbreviation ", QLineEdit.Normal, "") if entered: if not self.checkSnippetName(name): return self.snippetDict[name] = "" item = SnippetListWidgetItem(name) self.listView.addItem(item) self.listView.setCurrentItem(item) def updateList(self, selectedName=""): self.listView.clear() for snippet in self.snippetDict: self.listView.addItem(SnippetListWidgetItem(snippet)) if selectedName: items = self.listView.findItems(selectedName, Qt.MatchExactly) if len(items) > 0: for item in items: self.listView.setItemSelected(item, True) def appyButtonClicked(self): snippet = self.listView.currentItem() if snippet: name = self.nameEdit.text() if not self.checkSnippetName(name, checkSelected=True): return del self.snippetDict[str(snippet)] self.snippetDict[name] = self.editor.toPlainText() self.updateList(selectedName=name) self.snippetManager.updateSnippets(self.snippetDict) return True def okButtonClicked(self): self.snippetManager.updateSnippets(self.snippetDict) if self.appyButtonClicked(): self.accept() def updateEditor(self, snippet, previous): if snippet and str(snippet) in self.snippetDict: self.nameEdit.setText(str(snippet)) self.editor.setText(self.snippetDict[str(snippet)]) def cancelButtonClicked(self): self.reject() def closeEvent(self, arg__1): self.reject()