class SourceViewer(QDialog): def __init__(self, browser): super(SourceViewer, self).__init__(browser.parentWidget()) layout = QVBoxLayout() layout.setContentsMargins(4, 4, 4, 4) self.setLayout(layout) self.urlLabel = QLabel(wordWrap=True) layout.addWidget(self.urlLabel) self.textbrowser = QTextBrowser() layout.addWidget(self.textbrowser) self.urlLabel.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred) self.textbrowser.setLineWrapMode(QTextBrowser.NoWrap) app.settingsChanged.connect(self.readSettings) self.readSettings() app.translateUI(self) qutil.saveDialogSize(self, "helpbrowser/sourceviewer/size", QSize(400, 300)) def translateUI(self): self.setWindowTitle(app.caption(_("LilyPond Source"))) def readSettings(self): data = textformats.formatData('editor') self.textbrowser.setPalette(data.palette()) self.textbrowser.setFont(data.font) highlighter.highlight(self.textbrowser.document()) def showReply(self, reply): reply.setParent(self) self.urlLabel.setText(reply.url().toString()) reply.finished.connect(self.loadingFinished) self._reply = reply self.textbrowser.clear() self.show() def loadingFinished(self): data = self._reply.readAll() self._reply.close() self._reply.deleteLater() del self._reply self.textbrowser.clear() self.textbrowser.setText(unicode(data, 'utf-8', 'replace')) highlighter.highlight(self.textbrowser.document())
class SourceViewer(QDialog): def __init__(self, browser): super(SourceViewer, self).__init__(browser.parentWidget()) layout = QVBoxLayout() layout.setContentsMargins(4, 4, 4, 4) self.setLayout(layout) self.urlLabel = QLabel(wordWrap=True) layout.addWidget(self.urlLabel) self.textbrowser = QTextBrowser() layout.addWidget(self.textbrowser) self.urlLabel.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred) self.textbrowser.setLineWrapMode(QTextBrowser.NoWrap) app.settingsChanged.connect(self.readSettings) self.readSettings() app.translateUI(self) qutil.saveDialogSize(self, "helpbrowser/sourceviewer/size", QSize(400, 300)) def translateUI(self): self.setWindowTitle(app.caption(_("LilyPond Source"))) def readSettings(self): data = textformats.formatData('editor') self.textbrowser.setPalette(data.palette()) self.textbrowser.setFont(data.font) highlighter.highlight(self.textbrowser.document()) def showReply(self, reply): reply.setParent(self) self.urlLabel.setText(reply.url().toString()) self._reply = reply reply.finished.connect(self.loadingFinished) self.textbrowser.clear() self.show() def loadingFinished(self): data = self._reply.readAll() self._reply.close() self._reply.deleteLater() del self._reply self.textbrowser.clear() self.textbrowser.setText(str(data, 'utf-8', 'replace')) highlighter.highlight(self.textbrowser.document())
class PreferencesWindow(BaseWindow): def __init__(self, *args, **kwargs): super(PreferencesWindow, self).__init__(layoutCls = QVBoxLayout, *args, **kwargs) self.setWindowTitle("Settings") self.setWindowIcon(QIcon(SETTINGS['icon'])) self._add_steam_box() self._add_dota_box() self._add_additional_prefs() self._add_log_box() self._add_log_watcher() Settings().signals.changed.connect(self.update_path) def _add_log_watcher(self): self.watcher = QFileSystemWatcher() self.watcher.addPath(abspath(log.file_name)) self.watcher.fileChanged.connect(self.update_log) def show(self): self.update_log(abspath(log.file_name)) return super(PreferencesWindow, self).show() def update_path(self, setting_key, new_value): if setting_key == "dota_path": self.dota_path.setText(new_value) elif setting_key == "steam_path": self.steam_path.setText(new_value) def _add_steam_box(self): box = QGroupBox("Steam Location") box.setLayout(QHBoxLayout(box)) self.steam_path = QLineEdit(box) self.steam_path.setReadOnly(True) self.steam_path.setText(Settings().get("steam_path")) change_btn = QPushButton("Change...", box) change_btn.clicked.connect(self.change_steam_path) box.layout().addWidget(self.steam_path) box.layout().addWidget(change_btn) self.layout().addWidget(box) def _add_dota_box(self): box = QGroupBox("Dota Location") box.setLayout(QHBoxLayout(box)) self.dota_path = QLineEdit(box) self.dota_path.setReadOnly(True) self.dota_path.setText(Settings().get("dota_path")) change_btn = QPushButton("Change...", box) change_btn.clicked.connect(self.change_dota_path) box.layout().addWidget(self.dota_path) box.layout().addWidget(change_btn) self.layout().addWidget(box) def change_steam_path(self): self._change_path("steam_path", is_steam_path_valid) def change_dota_path(self): self._change_path("dota_path", is_dota_path_valid) def _change_path(self, path_key, is_valid): new_folder = str(QFileDialog.getExistingDirectory(parent=self, caption="Select new path", directory=Settings().get(path_key)) ) if new_folder and exists(new_folder): if is_valid(new_folder): Settings().set(path_key, new_folder) else: from d2mp.ui import Message Message.critical("Path is not valid", "Path was not saved in settings.\nPlease select a directory with the right executable in it!") def _add_additional_prefs(self): box = QGroupBox("Additional Preferences") box.setLayout(QHBoxLayout(box)) log_btn = QPushButton("View Log", box) log_btn.clicked.connect(self.open_log_file) reset_btn = QPushButton("Reset Settings", box) reset_btn.clicked.connect(Settings().reset) box.layout().addWidget(log_btn) box.layout().addWidget(reset_btn) self.layout().addWidget(box) def _add_log_box(self): box = QGroupBox("Application log") box.setLayout(QHBoxLayout(box)) self.log_area = QTextBrowser(box) self.log_area.setLineWrapMode(QTextEdit.NoWrap) box.layout().addWidget(self.log_area) self.layout().addWidget(box) def open_log_file(self): log.INFO("TODO: open file in standard editor") print(abspath(log.file_name)) def update_log(self, log_file): content = "" for line in open(log_file): if "========= new programm start =========" in line: content = "" else: content += line[37:] if content: self.log_area.setText(content)
class PreferencesWindow(BaseWindow): def __init__(self, *args, **kwargs): super(PreferencesWindow, self).__init__(layoutCls=QVBoxLayout, *args, **kwargs) self.setWindowTitle("Settings") self.setWindowIcon(QIcon(SETTINGS['icon'])) self._add_steam_box() self._add_dota_box() self._add_additional_prefs() self._add_log_box() self._add_log_watcher() Settings().signals.changed.connect(self.update_path) def _add_log_watcher(self): self.watcher = QFileSystemWatcher() self.watcher.addPath(abspath(log.file_name)) self.watcher.fileChanged.connect(self.update_log) def show(self): self.update_log(abspath(log.file_name)) return super(PreferencesWindow, self).show() def update_path(self, setting_key, new_value): if setting_key == "dota_path": self.dota_path.setText(new_value) elif setting_key == "steam_path": self.steam_path.setText(new_value) def _add_steam_box(self): box = QGroupBox("Steam Location") box.setLayout(QHBoxLayout(box)) self.steam_path = QLineEdit(box) self.steam_path.setReadOnly(True) self.steam_path.setText(Settings().get("steam_path")) change_btn = QPushButton("Change...", box) change_btn.clicked.connect(self.change_steam_path) box.layout().addWidget(self.steam_path) box.layout().addWidget(change_btn) self.layout().addWidget(box) def _add_dota_box(self): box = QGroupBox("Dota Location") box.setLayout(QHBoxLayout(box)) self.dota_path = QLineEdit(box) self.dota_path.setReadOnly(True) self.dota_path.setText(Settings().get("dota_path")) change_btn = QPushButton("Change...", box) change_btn.clicked.connect(self.change_dota_path) box.layout().addWidget(self.dota_path) box.layout().addWidget(change_btn) self.layout().addWidget(box) def change_steam_path(self): self._change_path("steam_path", is_steam_path_valid) def change_dota_path(self): self._change_path("dota_path", is_dota_path_valid) def _change_path(self, path_key, is_valid): new_folder = str( QFileDialog.getExistingDirectory( parent=self, caption="Select new path", directory=Settings().get(path_key))) if new_folder and exists(new_folder): if is_valid(new_folder): Settings().set(path_key, new_folder) else: from d2mp.ui import Message Message.critical( "Path is not valid", "Path was not saved in settings.\nPlease select a directory with the right executable in it!" ) def _add_additional_prefs(self): box = QGroupBox("Additional Preferences") box.setLayout(QHBoxLayout(box)) log_btn = QPushButton("View Log", box) log_btn.clicked.connect(self.open_log_file) reset_btn = QPushButton("Reset Settings", box) reset_btn.clicked.connect(Settings().reset) box.layout().addWidget(log_btn) box.layout().addWidget(reset_btn) self.layout().addWidget(box) def _add_log_box(self): box = QGroupBox("Application log") box.setLayout(QHBoxLayout(box)) self.log_area = QTextBrowser(box) self.log_area.setLineWrapMode(QTextEdit.NoWrap) box.layout().addWidget(self.log_area) self.layout().addWidget(box) def open_log_file(self): log.INFO("TODO: open file in standard editor") print(abspath(log.file_name)) def update_log(self, log_file): content = "" for line in open(log_file): if "========= new programm start =========" in line: content = "" else: content += line[37:] if content: self.log_area.setText(content)