Skip to content

Commit

Permalink
Merge pull request #112 from davidlatwe/fix-qjson-sorting
Browse files Browse the repository at this point in the history
Fix JsonView sorting and serializing
  • Loading branch information
davidlatwe authored Oct 21, 2020
2 parents 1d7f3db + 2ca472a commit 2a7b10b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
6 changes: 5 additions & 1 deletion allzpark/dock.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,11 @@ def on_copy_full():
else:
data = model_.json()

text = str(data)
text = json.dumps(data,
indent=4,
sort_keys=True,
ensure_ascii=False)

app = QtWidgets.QApplication.instance()
app.clipboard().setText(text)

Expand Down
8 changes: 8 additions & 0 deletions allzpark/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,9 @@ def poll(self):


class JsonModel(qjsonmodel.QJsonModel):

JsonRole = QtCore.Qt.UserRole + 1

def setData(self, index, value, role):
# Support copy/paste, but prevent edits
return False
Expand All @@ -562,8 +565,13 @@ def data(self, index, role):
if index.column() == 1:
return item.value

if role == self.JsonRole:
return self.json(item)

return super(JsonModel, self).data(index, role)

reset = qjsonmodel.QJsonModel.clear


class EnvironmentModel(JsonModel):
def load(self, data):
Expand Down
9 changes: 2 additions & 7 deletions allzpark/vendor/qjsonmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def load(self, value, parent=None, sort=True):
elif isinstance(value, list):
for index, value in enumerate(value):
child = self.load(value, rootItem)
child.key = str(index)
child.key = index
child.type = type(value)
rootItem.appendChild(child)

Expand All @@ -125,15 +125,13 @@ def load(self, value, parent=None, sort=True):


class QJsonModel(QtCore.QAbstractItemModel):
JsonRole = QtCore.Qt.UserRole + 1

def __init__(self, parent=None):
super(QJsonModel, self).__init__(parent)

self._rootItem = QJsonTreeItem()
self._headers = ("key", "value")

def reset(self):
def clear(self):
self.load({})

def load(self, document):
Expand Down Expand Up @@ -190,9 +188,6 @@ def data(self, index, role):
if index.column() == 1:
return item.value

if role == QJsonModel.JsonRole:
return self.json(item)

def setData(self, index, value, role):
if role == QtCore.Qt.EditRole:
if index.column() == 1:
Expand Down

0 comments on commit 2a7b10b

Please sign in to comment.