Skip to content

Commit

Permalink
Overhaul UI and add missing features
Browse files Browse the repository at this point in the history
  • Loading branch information
syntaxaire committed Jul 9, 2019
1 parent 8c4d463 commit 2bd0ad9
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 145 deletions.
87 changes: 60 additions & 27 deletions qud_explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,14 @@

from PySide2.QtGui import QStandardItemModel, QStandardItem, QIcon
from PySide2.QtWidgets import QMainWindow, QApplication, QTreeView, QSizePolicy, QAbstractItemView, \
QFileDialog, QLabel
QFileDialog, QLabel, QHeaderView

import qud_object_tree
from config import config
from qud_explorer_window import Ui_MainWindow
from qud_object import QudObject


def recursive_expand(item: QStandardItem, treeview: QTreeView, model: QStandardItemModel):
index = model.indexFromItem(item)
treeview.expand(index)
if item.parent() is not None:
recursive_expand(item.parent(), treeview, model)


class QudTreeView(QTreeView):
def __init__(self, selection_handler, *args, **kwargs):
"""selection_handler: a function in the parent window to pass selected indices to"""
Expand All @@ -42,16 +35,19 @@ def __init__(self, app: QApplication, *args, **kwargs):
self.app = app
self.setupUi(self) # lay out the inherited UI as in the graphical designer
# instantiate custom QTreeView subclass
self.treeView = QudTreeView(self.tree_selection_handler, self.centralwidget)
self.treeView = QudTreeView(self.tree_selection_handler, self.tree_target_widget)
self.init_qud_tree_view()
icon = QIcon("book.png")
self.setWindowIcon(icon)
self.lineEdit.textChanged.connect(self.search_changed)
self.search_line_edit.textChanged.connect(self.search_changed)
self.actionOpen_ObjectBlueprints_xml.triggered.connect(self.open_xml)
if os.path.exists('last_xml_location'):
with open('last_xml_location') as f:
filename = f.read()
self.open_xml(filename)
self.expand_all_button.clicked.connect(self.expand_all)
self.collapse_all_button.clicked.connect(self.collapse_all)
self.restore_all_button.clicked.connect(self.expand_default)
self.show()

def open_xml(self, filename: None):
Expand All @@ -63,8 +59,7 @@ def open_xml(self, filename: None):
self.init_qud_tree_model()
with open('last_xml_location', 'w') as f:
f.write(filename)
filename_widget = QLabel(text=filename)
self.statusbar.addWidget(filename_widget)
self.setWindowTitle("Qud Blueprint Explorer - " + filename)

def init_qud_tree_view(self):
size_policy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
Expand All @@ -81,42 +76,80 @@ def init_qud_tree_view(self):
def init_qud_tree_model(self):
self.qud_object_model = QStandardItemModel()
self.treeView.setModel(self.qud_object_model)
# self.qud_object_model.setHorizontalHeaderLabels(['Name', 'Display Name'])
self.qud_object_model.setHorizontalHeaderLabels(['Name', 'Display Name'])
header = self.treeView.header()
header.setSectionResizeMode(QHeaderView.ResizeToContents)
self.items_to_expand = [] # filled out during recursion of the Qud object tree

# # Multi column test
# col1 = QStandardItem('id')
# col2 = QStandardItem('displayname')
# self.qud_object_model.appendRow([col1, col2])

# We only need to add Object to the model, since all other Objects are loaded as children:
root_item = self.init_qud_object(self.qud_object_model, self.qud_object_root)
root_item.setSelectable(False)
self.qud_object_model.appendRow(root_item)
for item in self.items_to_expand:
recursive_expand(item, self.treeView, self.qud_object_model)
root = self.init_qud_object(self.qud_object_model, self.qud_object_root)
root.setSelectable(False)
display_name = QStandardItem(root.data().ui_displayname())
display_name.setSelectable(False)
self.qud_object_model.appendRow([root, display_name])
self.expand_default()

def init_qud_object(self, model: QStandardItemModel, qud_object: QudObject):
"""Recursive function to translate hierarchy from the Qud object AnyTree model to the
Qt StandardItemModel model."""
item = QStandardItem(qud_object.name)
if not qud_object.is_leaf:
for child in qud_object.children:
item.appendRow(self.init_qud_object(model, child))
display_name = QStandardItem(qud_object.ui_displayname())
item.setData(qud_object)
if qud_object.is_specified('tag_BaseObject'):
item.setSelectable(False)
item.setCheckable(True)
item.setData(qud_object)
display_name.setSelectable(False)
if not qud_object.is_leaf:
for child in qud_object.children:
sub_item = self.init_qud_object(model, child)
sub_display_name = QStandardItem(child.ui_displayname())
sub_display_name.setSelectable(False)
item.appendRow([sub_item, sub_display_name])

if qud_object.name in config['Interface']['Initial expansion targets']:
self.items_to_expand.append(item)
return item

def recursive_expand(self, item: QStandardItem):
index = self.qud_object_model.indexFromItem(item)
self.treeView.expand(index)
if item.parent() is not None:
self.recursive_expand(item.parent())

def tree_selection_handler(self, indices):
"""Registered with custom QudTreeView class as the handler for selection."""
self.statusbar.clearMessage()
text = ""
for index in indices:
item = self.qud_object_model.itemFromIndex(index)
qud_object = item.data()
text += qud_object.wikify()
if index.column() == 0:
item = self.qud_object_model.itemFromIndex(index)
qud_object = item.data()
text += qud_object.wikify()
self.statusbar.showMessage(qud_object.ui_inheritance_path())
self.plainTextEdit.setPlainText(text)

def collapse_all(self):
self.treeView.collapseAll()

def expand_all(self):
self.treeView.expandAll()

def expand_default(self):
self.collapse_all()
for item in self.items_to_expand:
self.recursive_expand(item)

def search_changed(self):
pass
self.treeView.keyboardSearch(self.search_line_edit.text())
selected = self.treeView.selectedIndexes()
if len(selected) > 0:
item = self.qud_object_model.itemFromIndex(selected[0]).data()
print(item)
self.recursive_expand(self.qud_object_model.itemFromIndex(selected[0]))


app = QApplication(sys.argv)
Expand Down
87 changes: 36 additions & 51 deletions qud_explorer_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Form implementation generated from reading ui file 'qud_explorer_window.ui',
# licensing of 'qud_explorer_window.ui' applies.
#
# Created: Sat Jul 6 17:53:28 2019
# Created: Mon Jul 8 22:14:23 2019
# by: pyside2-uic running on PySide2 5.13.0
#
# WARNING! All changes made in this file will be lost!
Expand All @@ -13,60 +13,48 @@
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 641)
MainWindow.resize(1024, 768)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setObjectName("label_2")
self.horizontalLayout_2.addWidget(self.label_2)
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout_2.addWidget(self.lineEdit)
self.verticalLayout_2.addLayout(self.horizontalLayout_2)
self.horizontalLayout.addLayout(self.verticalLayout_2)
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.label = QtWidgets.QLabel(self.centralwidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.label.sizePolicy().hasHeightForWidth())
self.label.setSizePolicy(sizePolicy)
self.label.setObjectName("label")
self.horizontalLayout_3.addWidget(self.label)
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.comboBox.sizePolicy().hasHeightForWidth())
self.comboBox.setSizePolicy(sizePolicy)
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.horizontalLayout_3.addWidget(self.comboBox)
self.verticalLayout.addLayout(self.horizontalLayout_3)
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.plainTextEdit = QtWidgets.QPlainTextEdit(self.centralwidget)
self.plainTextEdit.setAutoFillBackground(False)
self.plainTextEdit.setObjectName("plainTextEdit")
self.verticalLayout.addWidget(self.plainTextEdit)
self.horizontalLayout.addLayout(self.verticalLayout)
self.gridLayout.addLayout(self.horizontalLayout, 0, 0, 1, 1)
self.verticalLayout_2.addWidget(self.plainTextEdit)
self.verticalLayout.addLayout(self.verticalLayout_2)
self.verticalLayout_3 = QtWidgets.QVBoxLayout()
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.search_label = QtWidgets.QLabel(self.centralwidget)
self.search_label.setObjectName("search_label")
self.horizontalLayout_2.addWidget(self.search_label)
self.search_line_edit = QtWidgets.QLineEdit(self.centralwidget)
self.search_line_edit.setObjectName("search_line_edit")
self.horizontalLayout_2.addWidget(self.search_line_edit)
self.expand_all_button = QtWidgets.QPushButton(self.centralwidget)
self.expand_all_button.setObjectName("expand_all_button")
self.horizontalLayout_2.addWidget(self.expand_all_button)
self.collapse_all_button = QtWidgets.QPushButton(self.centralwidget)
self.collapse_all_button.setObjectName("collapse_all_button")
self.horizontalLayout_2.addWidget(self.collapse_all_button)
self.restore_all_button = QtWidgets.QPushButton(self.centralwidget)
self.restore_all_button.setObjectName("restore_all_button")
self.horizontalLayout_2.addWidget(self.restore_all_button)
self.verticalLayout_3.addLayout(self.horizontalLayout_2)
self.tree_target_widget = QtWidgets.QWidget(self.centralwidget)
self.tree_target_widget.setObjectName("tree_target_widget")
self.verticalLayout_3.addWidget(self.tree_target_widget)
self.verticalLayout.addLayout(self.verticalLayout_3)
self.gridLayout.addLayout(self.verticalLayout, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setGeometry(QtCore.QRect(0, 0, 1024, 21))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
Expand All @@ -87,13 +75,10 @@ def setupUi(self, MainWindow):

def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtWidgets.QApplication.translate("MainWindow", "Qud Blueprint Explorer", None, -1))
self.label_2.setText(QtWidgets.QApplication.translate("MainWindow", "Search:", None, -1))
self.label.setText(QtWidgets.QApplication.translate("MainWindow", "View:", None, -1))
self.comboBox.setItemText(0, QtWidgets.QApplication.translate("MainWindow", "Wiki template", None, -1))
self.comboBox.setItemText(1, QtWidgets.QApplication.translate("MainWindow", "XML source", None, -1))
self.comboBox.setItemText(2, QtWidgets.QApplication.translate("MainWindow", "XML with virtual inherited attributes", None, -1))
self.comboBox.setItemText(3, QtWidgets.QApplication.translate("MainWindow", "Internal representation", None, -1))
self.comboBox.setItemText(4, QtWidgets.QApplication.translate("MainWindow", "Internal representation with virtual inherited attributes", None, -1))
self.search_label.setText(QtWidgets.QApplication.translate("MainWindow", "Search:", None, -1))
self.expand_all_button.setText(QtWidgets.QApplication.translate("MainWindow", "Expand all", None, -1))
self.collapse_all_button.setText(QtWidgets.QApplication.translate("MainWindow", "Collapse all", None, -1))
self.restore_all_button.setText(QtWidgets.QApplication.translate("MainWindow", "Revert to configured expansions", None, -1))
self.menuFile.setTitle(QtWidgets.QApplication.translate("MainWindow", "File", None, -1))
self.actionOpen_ObjectBlueprints_xml.setText(QtWidgets.QApplication.translate("MainWindow", "Open ObjectBlueprints.xml...", None, -1))
self.actionExit.setText(QtWidgets.QApplication.translate("MainWindow", "Exit", None, -1))
Expand Down
Loading

0 comments on commit 2bd0ad9

Please sign in to comment.