Skip to content

Instantly share code, notes, and snippets.

@oglops
Last active July 22, 2016 06:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oglops/3f289a4a07cbd8b770d06bed7fb44a6d to your computer and use it in GitHub Desktop.
Save oglops/3f289a4a07cbd8b770d06bed7fb44a6d to your computer and use it in GitHub Desktop.
test qcompleter similar to ctrl+r in sublime
#!/usr/bin/env python
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import QtGui, QtCore
import sys
import re
import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)
def get_symbols(file='/home/oglop/tmp/enhancedScriptEditor.py'):
symbols = []
symbol_regex = re.compile('^(\s*)(class|def)\s*([^\(\s]+\([^\(]*\))')
with open(file) as f:
for n, line in enumerate(f.readlines()):
if symbol_regex.search(line):
groups = symbol_regex.search(line).groups()
symbols.append((n, groups[1], groups[0]+groups[2]))
print groups
return symbols
class MyModel(QStandardItemModel):
def __init__(self, parent=None):
super(MyModel, self).__init__(parent)
def data(self, index, role):
symbol = self.symbol_data[index.row()]
if role == Qt.DisplayRole:
if symbol[1] == 'class':
return symbol[2]
elif symbol[1] == 'def':
return re.search('[^\()]*', symbol[2]).group()+'(...)'
elif role == Qt.UserRole:
return symbol[0]
def setup(self, data):
self.symbol_data = data
for n, type_, name in data:
item = QStandardItem(name)
self.appendRow(item)
class MyGui(QDialog):
def __init__(self, parent=None):
super(MyGui, self).__init__(parent)
symbols = get_symbols()
model = MyModel()
model.setup(symbols)
layout = QtGui.QVBoxLayout(self)
self.line = QtGui.QLineEdit(self)
layout.addWidget(self.line)
self.button = QtGui.QPushButton('pop')
layout.addWidget(self.button)
self.setLayout(layout)
completer = CustomQCompleter()
completer.setModel(model)
completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
completer.setCompletionMode(QCompleter.UnfilteredPopupCompletion)
completer.setWrapAround(False)
self.line.setCompleter(completer)
self.completer = completer
self.button.clicked.connect(self.pop)
self.completer.highlighted[QModelIndex].connect(self.test)
# qApp.processEvents()
QTimer.singleShot(0, self.completer.complete)
self.line.textChanged[QString].connect(self.pop)
def pop(self, *x):
print 'x:', x
if isinstance(x[0], bool) or str(x[0]) == u'@':
print 'is @'
text = ''
else:
text = x[0]
self.completer.splitPath(text)
QTimer.singleShot(0, self.completer.complete)
def test(self, *x):
print 'line:', self.completer.model().sourceModel().symbol_data[x[0].row()][0]
class CustomQCompleter(QCompleter):
def __init__(self, parent=None):
super(CustomQCompleter, self).__init__(parent)
self.local_completion_prefix = ""
self.source_model = None
def setModel(self, model):
self.source_model = model
self._proxy = QSortFilterProxyModel(
self, filterCaseSensitivity=Qt.CaseInsensitive)
self._proxy.setSourceModel(model)
super(CustomQCompleter, self).setModel(self._proxy)
def splitPath(self, path):
self.local_completion_prefix = str(path)
self._proxy.setFilterFixedString(path)
return ""
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
gui = MyGui()
gui.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment