<commit_before># -*- coding: utf-8 -*-

# python stuff
import io
import os
import sys
import types
import traceback

# binja stuff
from binaryninja import log
from binaryninja import binaryview
from binaryninja.settings import Settings
from binaryninja import _binaryninjacore as core
from binaryninjaui import View, ViewType, ViewFrame, UIContext, HexEditor, getMonospaceFont

from PySide2.QtCore import Qt
from PySide2.QtWidgets import QScrollArea, QWidget, QVBoxLayout, QGroupBox, QTreeWidget, QTreeWidgetItem, QLineEdit, QHeaderView

from . import menu

if sys.version_info[0] == 2:
	import kshelpers
else:
	from . import kshelpers

class MyQTreeWidget(QTreeWidget):
	def __init__(self):
		QTreeWidget.__init__(self)

		self.oldWidth = 0
		self.oldHeight = 0

		self.queueInitialPresentation = False

	def resizeEvent(self, event):
		QTreeWidget.resizeEvent(self, event)

		if self.queueInitialPresentation:
			self.initialPresentation()

		self.scrollTo(self.currentIndex())

		newWidth = self.width()
		newHeight = self.height()

		#log.log_debug('QTreeWidget resizeEvent(), now I\'m %dx%d' % (newWidth, newHeight))

		if newWidth == self.oldWidth and newHeight == self.oldHeight:
			return

		self.oldWidth = newWidth
		self.oldHeight = newHeight

		self.setColumnWidthsNicely()

	def initialPresentation(self):
		self.expandNicely()

		if self.topLevelItemCount():
			self.topLevelItem(0).setSelected(True)

		self.queueInitialPresentation = False

	def setColumnWidthsNicely(self):
		width = self.width()
		self.setColumnWidth(0, .6*width)
		self.setColumnWidth(1, .2*width)
		self.setColumnWidth(2, .1*width)
		self.setColumnWidth(3, .1*width)

	def expandNicely(self):
		visibleRows = self.topLevelItemCount()
		if not visibleRows:
			return

		height = self.height()
		rowHeight = self.visualItemRect(self.topLevelItem(0)).height()
		rowCapacity = height / rowHeight

		queue = []
		for i in range(visibleRows):
			queue.append(self.topLevelItem(i))

		while visibleRows < rowCapacity:
			#log.log_debug('visibleRows=%d, rowCapacity=%d len(queue)=%d' % (visibleRows, rowCapacity, len(queue)))
			if not queue:
				break

			item = queue[0]
			queue = queue[1:]

			if not item.isExpanded():
				item.setExpanded(True)
				for i in range(item.childCount()):
					queue.append(item.child(i))
					visibleRows += 1

class KaitaiView(QScrollArea, View):
	def __init__(self, parent, binaryView):
		QScrollArea.__init__(self, parent)

		View.__init__(self)
		self.setupView(self)

		# BinaryViewType
		self.binaryView = binaryView

		self.rootSelectionStart = 0
		self.rootSelectionEnd = 1

		self.ioRoot = None
		self.ioCurrent = None

		container = QWidget(self)
		self.layout = QVBoxLayout()

		self.treeWidget = MyQTreeWidget()
		self.treeWidget.setColumnCount(4)
		self.treeWidget.setHeaderLabels(['label','value','start','end'])
		self.treeWidget.itemSelectionChanged.connect(self.onTreeSelect)

		self.structPath = QLineEdit("root")
		self.structPath.setDisabled(True)

		self.hexWidget = HexEditor(binaryView, ViewFrame.viewFrameForWidget(self), 0)

		self.layout.addWidget(self.treeWidget)
		self.layout.addWidget(self.structPath)
		self.layout.addWidget(self.hexWidget)
		#self.layout.addStretch(1)
		container.setLayout(self.layout)<commit_msg>kaitai: allow selection of structPath text<commit_after># -*- coding: utf-8 -*-

# python stuff
import io
import os
import sys
import types
import traceback

# binja stuff
from binaryninja import log
from binaryninja import binaryview
from binaryninja.settings import Settings
from binaryninja import _binaryninjacore as core
from binaryninjaui import View, ViewType, ViewFrame, UIContext, HexEditor, getMonospaceFont

from PySide2.QtCore import Qt
from PySide2.QtWidgets import QScrollArea, QWidget, QVBoxLayout, QGroupBox, QTreeWidget, QTreeWidgetItem, QLineEdit, QHeaderView

from . import menu

if sys.version_info[0] == 2:
	import kshelpers
else:
	from . import kshelpers

class MyQTreeWidget(QTreeWidget):
	def __init__(self):
		QTreeWidget.__init__(self)

		self.oldWidth = 0
		self.oldHeight = 0

		self.queueInitialPresentation = False

	def resizeEvent(self, event):
		QTreeWidget.resizeEvent(self, event)

		if self.queueInitialPresentation:
			self.initialPresentation()

		self.scrollTo(self.currentIndex())

		newWidth = self.width()
		newHeight = self.height()

		#log.log_debug('QTreeWidget resizeEvent(), now I\'m %dx%d' % (newWidth, newHeight))

		if newWidth == self.oldWidth and newHeight == self.oldHeight:
			return

		self.oldWidth = newWidth
		self.oldHeight = newHeight

		self.setColumnWidthsNicely()

	def initialPresentation(self):
		self.expandNicely()

		if self.topLevelItemCount():
			self.topLevelItem(0).setSelected(True)

		self.queueInitialPresentation = False

	def setColumnWidthsNicely(self):
		width = self.width()
		self.setColumnWidth(0, .6*width)
		self.setColumnWidth(1, .2*width)
		self.setColumnWidth(2, .1*width)
		self.setColumnWidth(3, .1*width)

	def expandNicely(self):
		visibleRows = self.topLevelItemCount()
		if not visibleRows:
			return

		height = self.height()
		rowHeight = self.visualItemRect(self.topLevelItem(0)).height()
		rowCapacity = height / rowHeight

		queue = []
		for i in range(visibleRows):
			queue.append(self.topLevelItem(i))

		while visibleRows < rowCapacity:
			#log.log_debug('visibleRows=%d, rowCapacity=%d len(queue)=%d' % (visibleRows, rowCapacity, len(queue)))
			if not queue:
				break

			item = queue[0]
			queue = queue[1:]

			if not item.isExpanded():
				item.setExpanded(True)
				for i in range(item.childCount()):
					queue.append(item.child(i))
					visibleRows += 1

class KaitaiView(QScrollArea, View):
	def __init__(self, parent, binaryView):
		QScrollArea.__init__(self, parent)

		View.__init__(self)
		self.setupView(self)

		# BinaryViewType
		self.binaryView = binaryView

		self.rootSelectionStart = 0
		self.rootSelectionEnd = 1

		self.ioRoot = None
		self.ioCurrent = None

		container = QWidget(self)
		self.layout = QVBoxLayout()

		self.treeWidget = MyQTreeWidget()
		self.treeWidget.setColumnCount(4)
		self.treeWidget.setHeaderLabels(['label','value','start','end'])
		self.treeWidget.itemSelectionChanged.connect(self.onTreeSelect)

		self.structPath = QLineEdit("root")
		self.structPath.setReadOnly(True)

		self.hexWidget = HexEditor(binaryView, ViewFrame.viewFrameForWidget(self), 0)

		self.layout.addWidget(self.treeWidget)
		self.layout.addWidget(self.structPath)
		self.layout.addWidget(self.hexWidget)
		#self.layout.addStretch(1)
		container.setLayout(self.layout)
{'fraction_non_alphanumeric': 0.11005953289574111, 'fraction_numerical': 0.007632422530911311, 'mean_word_length': 4.7982300884955755, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6863126', 'n_tokens_mistral': 2426, 'n_tokens_neox': 2139, 'n_words': 456}