Fri, 01 Jun 2018 13:44:20 +0200
QsciScintillaCompat: some 'beautifications' for the 'updateUserListSize()' code.
QScintilla/QsciScintillaCompat.py | file | annotate | diff | comparison | revisions |
--- a/QScintilla/QsciScintillaCompat.py Thu May 31 23:06:41 2018 +0200 +++ b/QScintilla/QsciScintillaCompat.py Fri Jun 01 13:44:20 2018 +0200 @@ -1511,73 +1511,73 @@ def updateUserListSize(self): """ - Public method to resize the autocompletion list to fit with contents. + Public method to resize the completion list to fit with contents. """ - childs = self.findChildren(QListWidget) - if not childs: - return - - ch = childs[-1] - geom = ch.geometry() - - baseHeight = geom.height() - - # Workaround for getting all items instead of ch.items() call with - # unknown mime types. - all_items = ch.findItems('', Qt.MatchStartsWith) - if not all_items: - return - - width = 0 - maxItemHeight = 0 - for item in all_items: - visualRect = ch.visualItemRect(item) - itemWidth = visualRect.width() - if itemWidth > width: - width = itemWidth - itemHeight = visualRect.height() - if itemHeight > maxItemHeight: - maxItemHeight = itemHeight - - height = min(self.maxLines, len(all_items)) * maxItemHeight - # Just a fiddling factor: 2 for better readability, - # e.g. underscores at the end of the list. - height += 2 - - # Borders - borders = geom.size() - ch.contentsRect().size() - width += borders.width() - height += borders.height() - - font = ch.font() - fm = QFontMetrics(font) - averageCharWidth = fm.averageCharWidth() - maxWidth = averageCharWidth * self.maxChars - if width > maxWidth: - width = maxWidth - height += ch.horizontalScrollBar().sizeHint().height() - # List box doesn't honor limited size to show scroll bars, e.g. - # Python 2 on Win 10. So just force it. - ch.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn) - - if len(all_items) > self.maxLines: - width += ch.verticalScrollBar().sizeHint().width() - - # Special case, where the space below current line where to less - yPos = geom.y() - charPos = self.SendScintilla(QsciScintilla.SCI_GETCURRENTPOS) - currentYPos = self.SendScintilla( - QsciScintilla.SCI_POINTYFROMPOSITION, 0, charPos) - - # X position doesn't matter: set to 0 - globalPos = self.mapToGlobal(QPoint(0, currentYPos)) - if yPos < globalPos.y(): - deltaHeight = baseHeight - height - geom.setY(yPos + deltaHeight - 4) - - geom.setWidth(width) - geom.setHeight(height) - ch.setGeometry(geom) + children = self.findChildren(QListWidget) + if children: + userListWidget = children[-1] + geom = userListWidget.geometry() + + baseHeight = geom.height() + + # Workaround for getting all items instead of + # userListWidget.items() call with unknown mime types. + all_items = userListWidget.findItems('', Qt.MatchStartsWith) + if not all_items: + return + + width = 0 + maxItemHeight = 0 + for item in all_items: + visualRect = userListWidget.visualItemRect(item) + itemWidth = visualRect.width() + if itemWidth > width: + width = itemWidth + itemHeight = visualRect.height() + if itemHeight > maxItemHeight: + maxItemHeight = itemHeight + + height = min(self.maxLines, len(all_items)) * maxItemHeight + # Just a fiddling factor: 2 for better readability, + # e.g. underscores at the end of the list. + height += 2 + + # Borders + borders = geom.size() - userListWidget.contentsRect().size() + width += borders.width() + height += borders.height() + + font = userListWidget.font() + fm = QFontMetrics(font) + averageCharWidth = fm.averageCharWidth() + maxWidth = averageCharWidth * self.maxChars + if width > maxWidth: + width = maxWidth + height += userListWidget.horizontalScrollBar().sizeHint()\ + .height() + # List box doesn't honor limited size to show scroll bars, e.g. + # Python 2 on Win 10. So just force it. + userListWidget.setHorizontalScrollBarPolicy( + Qt.ScrollBarAlwaysOn) + + if len(all_items) > self.maxLines: + width += userListWidget.verticalScrollBar().sizeHint().width() + + # Special case, where the space below current line where to less + yPos = geom.y() + charPos = self.SendScintilla(QsciScintilla.SCI_GETCURRENTPOS) + currentYPos = self.SendScintilla( + QsciScintilla.SCI_POINTYFROMPOSITION, 0, charPos) + + # X position doesn't matter: set to 0 + globalPos = self.mapToGlobal(QPoint(0, currentYPos)) + if yPos < globalPos.y(): + deltaHeight = baseHeight - height + geom.setY(yPos + deltaHeight - 4) + + geom.setWidth(width) + geom.setHeight(height) + userListWidget.setGeometry(geom) def __completionListSelected(self, listId, txt): """