-
-
Save oglops/7cd44ef9906283f9dc26f6d96088bfc0 to your computer and use it in GitHub Desktop.
show original line number after item is selected in dropdown menu, mimic sublime ctrl+r
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from PyQt4.QtCore import * | |
from PyQt4.QtGui import * | |
from PyQt4 import QtGui, QtCore | |
import sys | |
import re | |
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: | |
return symbol[2] | |
elif role == Qt.UserRole: | |
return symbol[0] | |
def setup(self, data): | |
self.symbol_data = data | |
# self.symbol_data = [ line,name for line,_,name in data] | |
for _,_,name in data: | |
print 'adding-->%s'% name | |
item = QStandardItem(name) | |
self.appendRow(item) | |
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+1, groups[1], groups[0]+groups[2])) | |
print n+1,[str(x) for x in groups] | |
return symbols | |
class MyGui(QDialog): | |
def __init__(self, parent=None): | |
super(MyGui, self).__init__(parent) | |
# symbols = ['ba','a','aa','b','bb','ab','c'] | |
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(self) | |
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) | |
self.line.textChanged[QString].connect(self.pop) | |
def pop(self, text): | |
print 'text:', '[%s]' %text | |
self.completer.splitPath(text) | |
QTimer.singleShot(0, self.completer.complete) | |
def test(self, index): | |
# index= self.completer.currentIndex() | |
# proxy = self.completer.completionModel() | |
print 'line:', index.data(Qt.UserRole).toPyObject() | |
class CustomQCompleter(QCompleter): | |
def __init__(self, parent=None): | |
super(CustomQCompleter, self).__init__(parent) | |
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): | |
# print 'splitPath=[%s]' % path | |
if str(path).startswith('@'): | |
path=path[1:] | |
self._proxy.setFilterFixedString(path) | |
self.complete() | |
return "" | |
def eventFilter(self, obj, event): | |
if event.type() == QEvent.KeyPress: | |
'This is used to mute the connection to clear lineedit' | |
if event.key() in ( Qt.Key_Down, Qt.Key_Up): | |
curIndex = self.popup().currentIndex() | |
print 'current row:',curIndex.row() | |
selList = self.popup().selectionModel().selectedIndexes() | |
if selList==[]: | |
# print 'selList empty:',curIndex | |
self.popup().setCurrentIndex(curIndex) | |
return True | |
else: | |
pass | |
# print 'selList has something:',curIndex.row() | |
return False | |
super(CustomQCompleter, self).eventFilter(obj, event) | |
return False | |
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