def dark_theme(self): file = QFile(":/stylesheet/dark.txt") file.open(QFile.ReadOnly | QFile.Text) stream = QTextStream(file) self.setStyleSheet(stream.readAll()) self.setIcons("white") self.spinner.setColor(QtGui.QColor(206, 206, 206))
def switch_theme(self): """ Toggle the stylesheet to use the desired path in the Qt resource system (prefixed by `:/`) or generically (a path to a file on system). This is quoted : https://stackoverflow.com/questions/48256772/dark-theme-for-qt-widgets """ # get the QApplication instance, or crash if not set app = QApplication.instance() if app is None: raise RuntimeError("No Qt Application found.") text = "" if self.parent.style_theme == "light": self.parent.style_theme = "dark" self.parent.style_theme_sheet = ":/dark.qss" self.update_statusbar() text = "Light" elif self.parent.style_theme == "dark": self.parent.style_theme = "light" self.parent.style_theme_sheet = ":/light.qss" self.update_statusbar() text = "Dark" file = QFile(self.parent.style_theme_sheet) self.parent.theme_button.setText(text) file.open(QFile.ReadOnly | QFile.Text) stream = QTextStream(file) app.setStyleSheet(stream.readAll())
def light_theme(self): file = QFile(":/stylesheet/light.txt") file.open(QFile.ReadOnly | QFile.Text) stream = QTextStream(file) self.setStyleSheet(stream.readAll()) self.setIcons("black") self.spinner.setColor(QtGui.QColor(26, 25, 28))
def __init__(self, id, *argv): super(SingleApplication, self).__init__(*argv) self._id = id self._activationWindow = None self._activateOnMessage = False # Is there another instance running? self._outSocket = QLocalSocket() self._outSocket.connectToServer(self._id) self._isRunning = self._outSocket.waitForConnected() if self._isRunning: # Yes, there is. self._outStream = QTextStream(self._outSocket) self._outStream.setCodec('UTF-8') else: # No, there isn't. self._outSocket = None self._outStream = None self._inSocket = None self._inStream = None self._server = QLocalServer() self._server.listen(self._id) self._server.newConnection.connect(self._onNewConnection)
def loadFile(self, fileName): file = QFile(fileName) if not file.open(QFile.ReadOnly | QFile.Text): QMessageBox.warning( self, "Pablo", "Cannot read file %s:\n%s." % (fileName, file.errorString())) return inFile = QTextStream(file) QApplication.setOverrideCursor(Qt.WaitCursor) if (QFileInfo(fileName).suffix() in ("pbl")): self.paged_text_edit.setHtml(inFile.readAll()) elif (QFileInfo(fileName).suffix() in ("html")): # FIXME: Prevent double setting of the paged_text_edit where necessary # FIXME: Double setting may cause bad UX with large files self.paged_text_edit.setHtml(inFile.readAll()) cleanHtml = HTMLCleaner.clean( self.paged_text_edit.toHtml()).decode("utf-8") self.paged_text_edit.setHtml(cleanHtml) else: self.paged_text_edit.setPlainText(inFile.readAll()) QApplication.restoreOverrideCursor() self.setCurrentFile(fileName) self.statusBar.writeMessageOnStatus("File loaded", 2000)
def saveFileAs(self): dir = QDir() docsPath = '{}/Documents'.format(dir.homePath()) if not dir.exists(docsPath): docsPath = '{}/Documentos'.format(dir.homePath()) #Regresa una tupla (filename, filter) filename = QFileDialog.getSaveFileName(self, 'Guardar como', docsPath, self.fileFilter) if not filename: return if self.filename: windowTitle = self.windowTitle().split('-')[0] self.setWindowTitle('{} - {}'.format(windowTitle, filename[0])) else: self.setWindowTitle('{} - {}'.format(self.windowTitle(), filename[0])) self.filename = filename[0] file = QFile(self.filename) if not file.open(QIODevice.WriteOnly): return data = self.textContent.toPlainText() stream = QTextStream(file) stream.setCodec('UTF-8') stream << data file.flush() file.close() self.showMessage('Archivo guardado')
def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.centralWidget = QWidget() self.mainLayout = QVBoxLayout() # Initialize board self.boardWidget = hichess.BoardWidget() # The user can interract with both colors of the board self.boardWidget.accessibleSides = hichess.BOTH_SIDES # Enable drag and drop self.boardWidget.dragAndDrop = True # background image self.boardWidget.setBoardPixmap( defaultPixmap=QPixmap(":/images/chessboard.png"), flippedPixmap=QPixmap(":/images/flipped_chessboard.png")) # qss qss = QFile(":/style/styles.css") if qss.open(QFile.ReadOnly): textStream = QTextStream(qss) self.boardWidget.setStyleSheet(textStream.readAll()) self.flipButton = QPushButton("Flip") # flip the board when the button is pressed self.flipButton.clicked.connect(self.boardWidget.flip) self.mainLayout.addWidget(self.boardWidget) self.mainLayout.addWidget(self.flipButton) self.centralWidget.setLayout(self.mainLayout) self.setCentralWidget(self.centralWidget)
def __init__(self): super(AboutWindow, self).__init__() about_ui = abtform.Ui_Dialog() about_ui.setupUi(self) file = QFile(":/text/about.txt") file.open(QFile.ReadOnly | QFile.Text) stream = QTextStream(file) about_ui.text.setHtml(stream.readAll())
def __init__(self): super(DocsWindow, self).__init__() docs_ui = docsform.Ui_Dialog() docs_ui.setupUi(self) file = QFile(":/text/docum.txt") file.open(QFile.ReadOnly | QFile.Text) stream = QTextStream(file) docs_ui.textEdit.setHtml(stream.readAll())
def _check_data(self, data_set): for data, lines in data_set: stream = QTextStream(data) res = [] while not stream.atEnd(): res.append(stream.readLine()) self.assertEqual(res, lines)
def _read_file(self, file_path: str) -> str: from PySide2.QtCore import QFile from PySide2.QtCore import QTextStream from PySide2.QtCore import QIODevice file = QFile(file_path) file.open(QIODevice.ReadOnly) ts = QTextStream(file) string = ts.readAll() return string
def _onNewConnection(self): if self._inSocket: self._inSocket.readyRead.disconnect(self._onReadyRead) self._inSocket = self._server.nextPendingConnection() if not self._inSocket: return self._inStream = QTextStream(self._inSocket) self._inStream.setCodec('UTF-8') self._inSocket.readyRead.connect(self._onReadyRead) if self._activateOnMessage: self.activateWindow()
def load_file(self, fileName): file = QFile(fileName) if not file.open(QFile.ReadOnly | QFile.Text): QMessageBox.warning(self, "Cryptix", f"Cannot read {fileName} :\n{file.errorString()}") return stream = QTextStream(file) self.encryptEdit.setPlainText(stream.readAll()) self.statusBar().showMessage("File loaded", 2000)
def __onNewConnection(self): """ On New Socket Connection From Clint """ if self._socket: self._socket.readyRead.disconnect(self.__onReadyRead) self._socket = self._server.nextPendingConnection() if not self._socket: return self._stream_str = QTextStream(self._socket) self._stream_str.setCodec('UTF-8') self._socket.readyRead.connect(self.__onReadyRead)
def openFile(self, path = ""): fileName = path if not fileName: fileName, _ = QFileDialog.getOpenFileName(self, self.tr("Open File"), "", "qmake Files (*.pro *.prf *.pri)") if fileName: inFile = QFile(fileName) if inFile.open(QFile.ReadOnly | QFile.Text): stream = QTextStream(inFile) self.editor.setPlainText(stream.readAll())
def load(self): filename, _ = QFileDialog.getOpenFileName(self) if not filename: return file = QFile(filename) if not file.open(QFile.ReadOnly | QFile.Text): QMessageBox.warning( self, "Panel Słoneczny", "nie mógł wczytać pliku %s:\n%s." % (filename, file.errorString())) return inf = QTextStream(file) #QApplication.setOverrideCursor(QtCore.Qt.WaitCursor) self.textEdit.setPlainText(inf.readAll()) #with open(file, 'r') as input: with open('Breslau.txt', 'r') as f: # f = self.nameEdit.text() # f = self.latitudeEdit.text() # f = self.longitudeEdit.text() # f = self.dateandtimeEdit.text() # print(self.nameEdit.text()) # print(self.latitudeEdit.text()) # print(self.longitudeEdit.text()) # print(self.dateandtimeEdit.text()) # for line in f: # print (line, end='') f_name = f.readline() #f_name = self.nameEdit.text() self.text1.setText(f_name) f_lat = f.readline() f_lat = self.latitudeEdit.text() f_lon = f.readline() f_lon = self.longitudeEdit.text() f_dnt = f.readline() f_dnt = self.dateandtimeEdit.text() # for line in f: # if 'name' in line: # self.text1.setText(line) #if 'lat' in line: #self.text1.setText(self.textEdit.setPlainText(inf.readAll())) #self.text1.setText(self.nameEdit.text()) #QApplication.restoreOverrideCursor() #self.setCurrentFile(filename) self.statusBar().showMessage("Wczytano plik", 2000)
class QTextStreamShiftTest(unittest.TestCase): def setUp(self): self.ba = QByteArray() self.read = QTextStream(self.ba, QIODevice.ReadOnly) self.write = QTextStream(self.ba, QIODevice.WriteOnly) def testNumber(self): '''QTextStream << number''' self.write << '4' self.write.flush() res = self.read.readLine() self.assertTrue(isinstance(res, py3k.unicode)) self.assertEqual(res, '4')
def loadFile(self, fileName): file = QFile(fileName) if not file.open(QFile.ReadOnly | QFile.Text): QMessageBox.warning(self, "Application", "Cannot read file" "{}:\n{}".format(fileName, file.errorString())) return False in_ = QTextStream(file) QApplication.setOverrideCursor(Qt.WaitCursor) self.textEdit.setPlainText(in_.readAll()) QApplication.restoreOverrideCursor() self.setCurrentFile(fileName) self.statusBar().showMessage("File loaded", 2000)
def setupUi(self): self.ui.setupUi(self) self.setWindowTitle(Constants.PROGRAM_NAME + " - version " + Constants.CSV2JSON_VERSION) self.ui.cbLogger.setEnabled(False) self.ui.cbConverter.setEnabled(False) self.ui.leSourcePath.setText(Settings().getSpoolPath()) self.ui.pbStyle.setVisible(Settings().isStyleButtonVisible()) self.ui.pteEditor.setMaximumBlockCount(Settings().getMaxLogRowCount()) self.ui.pteEditor.setReadOnly(True) file = QFile(":/qss/style") file.open(QFile.ReadOnly | QFile.Text) ts = QTextStream(file) styleSheet = ts.readAll() self.setStyleSheet(styleSheet)
def readMarkdownFile(fileName): inFile = QFile(fileName) if not inFile.open(QFile.ReadOnly | QFile.Text): inFile.close() print("Couldn't read {}".format(fileName)) return "" stream = QTextStream(inFile) contents = stream.readAll() inFile.close() return contents
def save_file(self, file_name): """ :param file_name: A canonical file path or whatever QFileDialog.getSaveFileName returns. :return: boolean for use in closeEvent method. """ error = None QApplication.setOverrideCursor(Qt.WaitCursor) file = QSaveFile(file_name) if file.open(QFile.WriteOnly | QFile.Text): outstr = QTextStream(file) outstr << self.text_edit.toPlainText() if not file.commit(): error = "Cannot write file {}:\n{}.".format( file_name, file.errorString()) else: error = "Cannot open file {}:\n{}.".format(file_name, file.errorString()) QApplication.restoreOverrideCursor() if error: QMessageBox.warning(self, "OneHandTextEdit", error) return False self.set_current_file(file_name) self.statusBar().showMessage("File saved", 2000) return True
def __init__(self, parent=None): super(DragWidget, self).__init__(parent) dictionaryFile = QFile(':/dictionary/words.txt') dictionaryFile.open(QIODevice.ReadOnly) x = 5 y = 5 for word in QTextStream(dictionaryFile).readAll().split(): wordLabel = DragLabel(word, self) wordLabel.move(x, y) wordLabel.show() x += wordLabel.width() + 2 if x >= 195: x = 5 y += wordLabel.height() + 2 newPalette = self.palette() newPalette.setColor(QPalette.Window, Qt.white) self.setPalette(newPalette) self.setAcceptDrops(True) self.setMinimumSize(400, max(200, y)) self.setWindowTitle("Draggable Text")
def loadFile(self, fileName): file = QFile(fileName) if not file.open(QFile.ReadOnly | QFile.Text): QMessageBox.warning(self, "MDI", "Cannot read file %s:\n%s." % (fileName, file.errorString())) return False instr = QTextStream(file) QApplication.setOverrideCursor(Qt.WaitCursor) self.setPlainText(instr.readAll()) QApplication.restoreOverrideCursor() self.setCurrentFile(fileName) self.document().contentsChanged.connect(self.documentWasModified) return True
def SpongesMorphotypes() -> dict: if len(Loader._sponges_morphotypes) == 0: content = "" file = QFile(Loader.MORPHOTYPES_FILE_PATH) if file.open(QIODevice.ReadOnly | QFile.Text): stream = QTextStream(file) stream.setCodec("UTF-8") content = stream.readAll() file.close() obj = json.loads(content) for i, m in enumerate(obj): Loader._sponges_morphotypes[i] = Morphotype( m["name"], QColor(m["color"])) return Loader._sponges_morphotypes
def set_dark_style(self): f = QFile(Resource.darkstyle) if not f.exists(): LOGGER.error( "Unable to load dark stylesheet, file not found in resources") return else: f.open(QFile.ReadOnly | QFile.Text) ts = QTextStream(f) stylesheet = ts.readAll() self.setStyleSheet(stylesheet) KnechtSettings.app['app_style'] = 'fusion-dark' IconRsc.update_icons(self.ui) self.setStyle('Fusion')
def slotSourceDownloaded(self): reply = self.sender() self.textEdit = QTextEdit() self.textEdit.setAttribute(Qt.WA_DeleteOnClose) self.textEdit.show() self.textEdit.setPlainText(QTextStream(reply).readAll()) self.textEdit.resize(600, 400) reply.deleteLater()
def saveFile(self): if not self.filename: self.saveFileAs() return file = QFile(self.filename) if not file.open(QIODevice.WriteOnly): return data = self.textContent.toPlainText() stream = QTextStream(file) stream.setCodec('UTF-8') stream << data file.flush() file.close() self.showMessage('Archivo guardado')
def _dependenciesComboBoxChanged(self, idx: int): data = self._dependencies_list.currentData() self._version.setText(data["version"]) self._license.setText(data["license"]) self._author.setText(data["author"]) self._link.setText("<a href='%s'>%s</a>" % (data["link"], data["link"])) license_file = QFile(":/documents/licenses/%s" % data["license-file"]) text = "" if license_file.open(QIODevice.ReadOnly | QFile.Text): stream = QTextStream(license_file) stream.setCodec("UTF-8") text = stream.readAll() license_file.close() self._license_area.setText(text)
def __connect(self): """ Create A Local Socket And Try To Connect """ if self._socket: self._socket.close() socket = QLocalSocket() socket.connectToServer(self._uid) connected = socket.waitForConnected() if not connected: socket.close() return False self._socket = socket self._stream_str = QTextStream(self._socket) self._stream_str.setCodec('UTF-8') return True
def __ConfigureHistoryFile(self): self.__historyFile = QFile("history.txt") if self.__historyFile.open(QFile.ReadWrite or QFile.Append): self.__historyFileStream = QTextStream(self.__historyFile) else: self.__statusBar.showMessage( self.__GetCurrentTime() + " - History fie create failed!", 2000)
class QTextStreamGetSet(unittest.TestCase): def setUp(self): self.obj = QTextStream() def testCodec(self): '''QTextStream set/get Codec''' codec = QTextCodec.codecForName('ISO8859-1') self.obj.setCodec(codec) self.assertEqual(codec, self.obj.codec()) def testDevice(self): '''QTextStream get/set Device''' device = QFile() self.obj.setDevice(device) self.assertEqual(device, self.obj.device()) self.obj.setDevice(None) self.assertEqual(None, self.obj.device())
def testConstruction(self): '''QTextStream construction''' obj = QTextStream() self.assertEqual(obj.codec(), QTextCodec.codecForLocale()) self.assertEqual(obj.device(), None) self.assertEqual(obj.string(), None) self.assertTrue(obj.atEnd()) self.assertEqual(obj.readAll(), '')
def __init__(self, id, *argv): super(QtSingleApplication, self).__init__(*argv) self._id = id self._activationWindow = None self._activateOnMessage = False # Is there another instance running? self._outSocket = QLocalSocket() self._outSocket.connectToServer(self._id) self._isRunning = self._outSocket.waitForConnected() if self._isRunning: # Yes, there is. self._outStream = QTextStream(self._outSocket) self._outStream.setCodec('UTF-8') else: # No, there isn't. self._outSocket = None self._outStream = None self._inSocket = None self._inStream = None self._server = QLocalServer() self._server.listen(self._id) self._server.newConnection.connect(self._onNewConnection)
def testIt(self): device = MyIODevice() device.open(QIODevice.ReadOnly) s = QTextStream(device) self.assertEqual(s.read(4), "\0a\0a") self.assertEqual(device.readLine(), "\0b\0b\0b\0b")
def setUp(self): self.ba = QByteArray() self.read = QTextStream(self.ba, QIODevice.ReadOnly) self.write = QTextStream(self.ba, QIODevice.WriteOnly)
def setUp(self): self.obj = QTextStream()
class QtSingleApplication(QApplication): messageReceived = Signal() def __init__(self, id, *argv): super(QtSingleApplication, self).__init__(*argv) self._id = id self._activationWindow = None self._activateOnMessage = False # Is there another instance running? self._outSocket = QLocalSocket() self._outSocket.connectToServer(self._id) self._isRunning = self._outSocket.waitForConnected() if self._isRunning: # Yes, there is. self._outStream = QTextStream(self._outSocket) self._outStream.setCodec('UTF-8') else: # No, there isn't. self._outSocket = None self._outStream = None self._inSocket = None self._inStream = None self._server = QLocalServer() self._server.listen(self._id) self._server.newConnection.connect(self._onNewConnection) def isRunning(self): return self._isRunning def id(self): return self._id def activationWindow(self): return self._activationWindow def setActivationWindow(self, activationWindow, activateOnMessage=True): self._activationWindow = activationWindow self._activateOnMessage = activateOnMessage def activateWindow(self): if not self._activationWindow: return self._activationWindow.setWindowState( self._activationWindow.windowState() & ~Qt.WindowMinimized) self._activationWindow.raise_() self._activationWindow.activateWindow() def sendMessage(self, msg): if not self._outStream: return False self._outStream << msg << '\n' self._outStream.flush() return self._outSocket.waitForBytesWritten() def _onNewConnection(self): if self._inSocket: self._inSocket.readyRead.disconnect(self._onReadyRead) self._inSocket = self._server.nextPendingConnection() if not self._inSocket: return self._inStream = QTextStream(self._inSocket) self._inStream.setCodec('UTF-8') self._inSocket.readyRead.connect(self._onReadyRead) if self._activateOnMessage: self.activateWindow() def _onReadyRead(self): while True: msg = self._inStream.readLine() if not msg: break self.messageReceived.emit(msg)