如何在QScintilla编辑器中查找单词并转行?

时间:2019-05-27 15:21:54

标签: python pyqt5 scintilla qscintilla

我想创建一个用于搜索单词并在Qscintilla编辑器中突出显示它们的功能,以及另一个功能以转到编辑器中的特定行。

我知道这些函数可以在QPlainTextEdit中使用,但是由于我正在学习Qscintilla,并且大多数文档都是用C ++编写的,所以我很难理解,并且方法在Scintilla中也大不相同。

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.Qsci import *
#from PyQt5.QtCore import *

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.textEdit = QsciScintilla(self.centralwidget)
        self.textEdit.setObjectName("textEdit")
        self.gridLayout.addWidget(self.textEdit, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        self.menuFile = QtWidgets.QMenu(self.menubar)
        self.menuFile.setObjectName("menuFile")
        self.menuSearch = QtWidgets.QMenu(self.menubar)
        self.menuSearch.setObjectName("menuSearch")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.actionOpen = QtWidgets.QAction(MainWindow)
        self.actionOpen.setObjectName("actionOpen")
        self.actionSave = QtWidgets.QAction(MainWindow)
        self.actionSave.setObjectName("actionSave")
        self.actionExit = QtWidgets.QAction(MainWindow)
        self.actionExit.setObjectName("actionExit")
        self.actionFind = QtWidgets.QAction(MainWindow)
        self.actionFind.setObjectName("actionFind")
        self.actionWord_Count = QtWidgets.QAction(MainWindow)
        self.actionWord_Count.setObjectName("actionWord_Count")
        self.actionNew = QtWidgets.QAction(MainWindow)
        self.actionNew.setObjectName("actionNew")
        self.menuFile.addAction(self.actionNew)
        self.menuFile.addAction(self.actionOpen)
        self.menuFile.addAction(self.actionSave)
        self.menuFile.addAction(self.actionExit)
        self.menuSearch.addAction(self.actionFind)
        self.menuSearch.addAction(self.actionWord_Count)
        self.menubar.addAction(self.menuFile.menuAction())
        self.menubar.addAction(self.menuSearch.menuAction())

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.menuFile.setTitle(_translate("MainWindow", "File"))
        self.menuSearch.setTitle(_translate("MainWindow", "Search"))
        self.actionOpen.setText(_translate("MainWindow", "Open"))
        self.actionOpen.setShortcut(_translate("MainWindow", "Ctrl+O"))
        self.actionSave.setText(_translate("MainWindow", "Save"))
        self.actionSave.setShortcut(_translate("MainWindow", "Ctrl+S"))
        self.actionExit.setText(_translate("MainWindow", "Exit"))
        self.actionExit.setShortcut(_translate("MainWindow", "Ctrl+Q"))
        self.actionFind.setText(_translate("MainWindow", "Find"))
        self.actionFind.setShortcut(_translate("MainWindow", "Ctrl+F"))
        self.actionWord_Count.setText(_translate("MainWindow", "Go to Line"))
        self.actionNew.setText(_translate("MainWindow", "New"))
        self.actionNew.setShortcut(_translate("MainWindow", "Ctrl+N"))

class Ui_Dock_Find(object):
    def setupUi(self, Dock_Find):
        Dock_Find.setObjectName("Dock_Find")
        Dock_Find.resize(320, 65)
        Dock_Find.setMinimumSize(QtCore.QSize(320, 65))
        font = QtGui.QFont()
        font.setPointSize(10)
        Dock_Find.setFont(font)
        icon = QtGui.QIcon()

        icon.addPixmap(QtGui.QPixmap("Ok.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)

        Dock_Find.setWindowIcon(icon)
        self.dockWidgetContents = QtWidgets.QWidget()
        self.dockWidgetContents.setObjectName("dockWidgetContents")
        self.serachLabel = QtWidgets.QLabel(self.dockWidgetContents)
        self.serachLabel.setGeometry(QtCore.QRect(10, 10, 71, 16))
        self.serachLabel.setObjectName("serachLabel")
        self.findLine = QtWidgets.QLineEdit(self.dockWidgetContents)
        self.findLine.setGeometry(QtCore.QRect(80, 10, 151, 20))
        self.findLine.setObjectName("findLine")
        self.findButton = QtWidgets.QPushButton(self.dockWidgetContents)
        self.findButton.setGeometry(QtCore.QRect(240, 10, 75, 23))
        self.findButton.setObjectName("findButton")
        Dock_Find.setWidget(self.dockWidgetContents)

        self.retranslateUi(Dock_Find)
        QtCore.QMetaObject.connectSlotsByName(Dock_Find)

    def retranslateUi(self, Dock_Find):
        _translate = QtCore.QCoreApplication.translate
        Dock_Find.setWindowTitle(_translate("Dock_Find", "Find"))
        self.serachLabel.setText(_translate("Dock_Find", "Search For:"))
        self.findButton.setText(_translate("Dock_Find", "Find"))


class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)

        #Defining Menu Actions
        self.actionNew.triggered.connect(self.newFile)
        self.actionOpen.triggered.connect(self.openFile)
        #self.actionSave.triggered.connect(self.saveFile)
        #self.actionExit.triggered.connect(self.exitFile)
        self.actionFind.triggered.connect(self.findWord)
        self.actionWord_Count.triggered.connect(self.gotoLine)
        self.showMaximized()



    def newFile(self):
        self.textEdit.clear()

    def openFile(self):
        filename = QFileDialog.getOpenFileName(self, 'Open File', ".","(*.txt *.py *.log *.csv)")
        if filename[0]:
            #wordcounts = Counter()
            with open(filename[0], 'rt') as fp:
                for line in fp:
                    #wordcounts.update(line.split())
                    data = fp.read()
                    self.textEdit.setText(data)


    def saveFile(self):
        filename = QFileDialog.getSaveFileName(self, 'Save File', ".", "(*.txt)")
        if filename[0]:
            f = open(filename[0], 'wt')
            with f:
                text = self.textEdit.toPlainText()
                f.write(text)
                QMessageBox.about(self, "Save File", "File Saved Successfully")

    def exitFile(self):
        choice = QMessageBox.question(self, 'Close', "Do you want to close?", QMessageBox.Yes | QMessageBox.No)
        if choice == QMessageBox.Yes:
            self.saveFile()
            self.close()
        else:
            pass

    def findWord(self):

        self.dock = Dock_Find()
        self.addDockWidget(Qt.TopDockWidgetArea, self.dock)
        self.dock.show()

        def handleFind():

            word = self.dock.findLine.text()
            if self.textEdit.SCI_FINDTEXT(self.textEdit.SCFIND_WHOLEWORD(), word):
                self.textEdit.SCFIND_WHOLEWORD(word)

            else:
                self.textEdit.SCFIND_WHOLEWORD(word)
                return

        self.dock.findButton.clicked.connect(handleFind)

    def gotoLine(self):
        window = GoTo()
        window.show()
        def handleGoTo():
            ln = int(window.findText.text())
            """messenger = self.textEdit.SendScintilla()
            messenger(self.textEdit.SCI_SETTARGETSTART, 0)
            messenger(self.textEdit.SCI_SETTARGETEND, len(self.textEdit))
            pos = messenger(self.textEdit.SCI_SEARCHINTARGET, len("ln"), "ln")
            print(pos)"""
            linecursor = self.textEdit.wordAtLineIndex(ln, self.textEdit.getCursorPosition()[1])
            print(linecursor)
            self.textEdit.SCI_GOTOPOS(window.findText.text())
            window.hide()
        window.findButton.clicked.connect(handleGoTo)


class Dock_Find(QtWidgets.QDockWidget, Ui_Dock_Find):
    def __init__(self, parent=None):
        super(Dock_Find, self).__init__(parent)
        self.setupUi(self)
        self.findLine.setPlaceholderText("Type Here")

class GoTo(QDialog):
    def __init__(self):
        super(GoTo, self).__init__()
        self.initUI()
        self.setWindowTitle("GoTo")
        icon = QIcon()
        icon.addPixmap(QPixmap(":/image/graphy_100px.png"), QIcon.Normal, QIcon.Off)
        self.setWindowIcon(icon)

    def initUI(self):
        self.lb1 = QLabel("Go To Line:", self)
        self.lb1.setStyleSheet("font-size: 15px")
        self.lb1.move(10, 10)
        self.findText = QLineEdit(self)
        self.findText.move(10, 40)
        self.findText.resize(200, 20)
        self.findText.setPlaceholderText('Type the line number')
        self.findText.setClearButtonEnabled(True)
        self.findText.setInputMask("9999999999") #For allowing up to number digit only as input
        self.findButton = QPushButton('GO', self)
        self.findButton.move(220, 40)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    QApplication.setStyle(QStyleFactory.create('Fusion'))
    myGUI = MainWindow()

    sys.exit(app.exec_())

我不知道如何实现这两个功能,我一直在尝试,但找不到肯定的结果。但是对于第二个功能,我可以打印以行号开头的单词,但是我想在搜索功能转到行时用行号突出显示该单词。

0 个答案:

没有答案