Well after digging, and I mean A LOT of digging I found I could use QStyle to draw those little arrows
and since QStyleOptionViewItem has states, I can choose how the arrow is pointing.
This is a little example of code:
def paintEvent(self, event: QPaintEvent):
if self.isVisible():
block: QTextBlock = self.editor.firstVisibleBlock()
height: int = self.fontMetrics().height()
number: int = block.blockNumber()
painter = QPainter(self)
painter.fillRect(event.rect(), QColor(53, 53, 53))
# painter.drawRect(0, 0, event.rect().width() - 1, event.rect().height() - 1)
font = painter.font()
font.setPointSize(15)
currentBlockNumber: int = self.editor.textCursor().block().blockNumber() + 1
while block.isValid():
blockGeometry: QRectF = self.editor.blockBoundingGeometry(block)
offset: QPointF = self.editor.contentOffset()
blockTop: int = int(blockGeometry.translated(offset).top() + 1)
number += 1
if number == currentBlockNumber:
font.setBold(True)
else:
font.setBold(False)
painter.setFont(font)
rect: QRect = QRect(0, blockTop, self.width() - 5, height)
painter.drawText(rect, Qt.AlignCenter, str(number))
if number in self.lines:
options = QStyleOptionViewItem()
options.rect = QRect(0, blockTop, self.width() + 45, height)
options.state = (QStyle.State_Active |
QStyle.State_Item |
QStyle.State_Children |
QStyle.State_Open)
self.style().drawPrimitive(QStyle.PE_IndicatorBranch, options,
painter, self)
print("drawn")
if blockTop > event.rect().bottom():
break
block = block.next()
painter.end()
That is the whole paintEvent function and the magic happens here:
if number in self.lines:
options = QStyleOptionViewItem()
options.rect = QRect(0, blockTop, self.width() + 45, height)
options.state = (QStyle.State_Active |
QStyle.State_Item |
QStyle.State_Children |
QStyle.State_Open)
self.style().drawPrimitive(QStyle.PE_IndicatorBranch, options,
painter, self)
print("drawn")
when painting numbers I will check that if the current number is in a list of numbers where the user can fold at (this is done parsing a syntax tree, if anyone is interested in that please let me know!) and if it is in there then it will create a rect and draw a little arrow that points down (this is because QStyle.State_Open) is in the options.state variable
if you remove that, the arrow points right. That will be useful when after folding
Here's a little preview:
0_1566332422283_ee3f8570-58e3-45a6-97df-4084ce47bc8e-Screenshot 2019-08-20 at 23.19.53.png