Пример #1
0
class Editor(QMainWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        
        self.current_dir = os.getcwd()
        self.current_file = None
        
        self.init_ui()
    
    def init_ui(self):
        self.text_edit = QTextEdit(self)
        self.setCentralWidget(self.text_edit)
        
        font = QFont("Menlo", 12)
        # TODO: Create a layout and change the line spacing
        #spacing = QFontMetrics(font).lineSpacing()
        self.text_edit.setFont(font)
        self.highlighter = Highlighter(self.text_edit.document(), DEFAULT_RULES, DEFAULT_STYLE)
        #print("Highlighter doc: {}".format(self.text_edit.document()))
        
        menu_bar = self.menuBar()
        m_file = menu_bar.addMenu("File")
        
        i_open = QAction("Open", self)
        i_open.setShortcut('Ctrl+O')
        i_open.triggered.connect(self.on_open)
        
        m_file.addAction(i_open)
        
        self.setGeometry(300, 300, 350, 300)
        self.setWindowTitle("tea")
    
    def on_open(self):
        file_path, other = QFileDialog.getOpenFileName(self, "Open File", self.current_dir)
        #print([a for a in dir(QFileDialog) if not a.startswith("_")])
        print("File name: {}, Other: {}".format(file_path, other))
        if file_path:
            dirname = os.path.basename(os.path.dirname(file_path))
            filename = os.path.basename(file_path)
            self.setWindowTitle("{} - {}".format(filename, dirname))
            with open(file_path) as f:
                text = f.read()
                #formatted = markup_python(text)
                #formatted = "<font color=red size=24>{}</font>".format(text)
                self.text_edit.setText(text)
Пример #2
0
class TextEditDialog(QDialog):
    def __init__(self, font, textColor, parent=None):
        super(TextEditDialog, self).__init__(parent)
        self.setWindowFlags(QtCore.Qt.Window | QtCore.Qt.FramelessWindowHint)
        self.resize(QtCore.QSize(400, 300))
        self.layout = QVBoxLayout(self)
        self.layout.setContentsMargins(2, 2, 2, 2)
        self.te = QTextEdit()
        self._font = font
        self._font = QtGui.QFont(font)
        self.te.setFont(self._font)
        self.te.setTextColor(textColor)
        self.layout.addWidget(self.te)
        self.buttons = QDialogButtonBox(
            QDialogButtonBox.Ok | QDialogButtonBox.Cancel, self)
        self.buttons.accepted.connect(self.onAccept)
        self.buttons.rejected.connect(self.onReject)
        self.layout.addWidget(self.buttons)
        self._result = None
        self.te.textChanged.connect(
            lambda color=textColor: self.te.setTextColor(color))

    def zoomIn(self, factor):
        self.te.zoomIn(factor)

    def setHtml(self, html):
        self.te.setHtml(html)

    def onReject(self):
        self._result = "", False
        self.reject()

    def getResult(self):
        return self._result

    def onAccept(self):
        self.te.selectAll()
        # self._font.setPointSize(self.initialPointSize)
        self.te.setFont(self._font)
        self._result = self.te.toHtml(), True
        self.accept()