content.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import sys, os
  4. from PyQt5 import QtCore
  5. from PyQt5.QtGui import QFont, QSyntaxHighlighter
  6. from PyQt5.QtWidgets import QWidget, QLabel, QHBoxLayout, QVBoxLayout, QSplitter, QListView, QAbstractItemView
  7. import markdown
  8. import re
  9. import json
  10. class Summary(QWidget):
  11. def __init__(self, core):
  12. super(Summary, self).__init__()
  13. self.core = core
  14. sum_json = open(os.path.join(self.core.cwd,'.config/summary.json')).read()
  15. self.sum = json.loads(sum_json)
  16. vbox = QVBoxLayout()
  17. vbox.setContentsMargins(0,0,0,0)
  18. self.list = SummaryList(self)
  19. vbox.addWidget(self.list)
  20. # self.actions = SummaryActions(self)
  21. # self.addWidget(self.actions)
  22. self.setLayout(vbox)
  23. class SumListModel(QtCore.QAbstractListModel):
  24. dragDropFinished = QtCore.pyqtSignal()
  25. def __init__(self,sum):
  26. QtCore.QAbstractItemModel.__init__(self)
  27. self.nodes = ['node0', 'node1', 'node2', 'node3', 'node4', 'node5']
  28. self.lastDroppedItems = []
  29. self.pendingRemoveRowsAfterDrop = False
  30. def rowForItem(self, text):
  31. '''
  32. rowForItem method returns the row corresponding to the passed in item
  33. or None if no such item exists in the model
  34. '''
  35. try:
  36. row = self.nodes.index(text)
  37. except ValueError:
  38. return None
  39. return row
  40. def index(self, row, column, parent):
  41. if row < 0 or row >= len(self.nodes):
  42. return QtCore.QModelIndex()
  43. return self.createIndex(row, column)
  44. def parent(self, index):
  45. return QtCore.QModelIndex()
  46. def rowCount(self, index):
  47. if index.isValid():
  48. return 0
  49. return len(self.nodes)
  50. def data(self, index, role):
  51. if not index.isValid():
  52. return None
  53. if role == QtCore.Qt.DisplayRole:
  54. row = index.row()
  55. if row < 0 or row >= len(self.nodes):
  56. return None
  57. return self.nodes[row]
  58. else:
  59. return None
  60. def supportedDropActions(self):
  61. return QtCore.Qt.MoveAction
  62. def flags(self, index):
  63. if not index.isValid():
  64. return QtCore.Qt.ItemIsEnabled
  65. return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | \
  66. QtCore.Qt.ItemIsDragEnabled | QtCore.Qt.ItemIsDropEnabled
  67. def insertRows(self, row, count, index):
  68. if index.isValid():
  69. return False
  70. if count <= 0:
  71. return False
  72. # inserting 'count' empty rows starting at 'row'
  73. self.beginInsertRows(QtCore.QModelIndex(), row, row + count - 1)
  74. for i in range(0, count):
  75. self.nodes.insert(row + i, '')
  76. self.endInsertRows()
  77. return True
  78. def removeRows(self, row, count, index):
  79. if index.isValid():
  80. return False
  81. if count <= 0:
  82. return False
  83. num_rows = self.rowCount(QtCore.QModelIndex())
  84. self.beginRemoveRows(QtCore.QModelIndex(), row, row + count - 1)
  85. for i in range(count, 0, -1):
  86. self.nodes.pop(row - i + 1)
  87. self.endRemoveRows()
  88. if self.pendingRemoveRowsAfterDrop:
  89. '''
  90. If we got here, it means this call to removeRows is the automatic
  91. 'cleanup' action after drag-n-drop performed by Qt
  92. '''
  93. self.pendingRemoveRowsAfterDrop = False
  94. self.dragDropFinished.emit()
  95. return True
  96. def setData(self, index, value, role):
  97. if not index.isValid():
  98. return False
  99. if index.row() < 0 or index.row() > len(self.nodes):
  100. return False
  101. self.nodes[index.row()] = str(value)
  102. self.dataChanged.emit(index, index)
  103. return True
  104. def mimeTypes(self):
  105. return ['application/vnd.treeviewdragdrop.list']
  106. def mimeData(self, indexes):
  107. mimedata = QtCore.QMimeData()
  108. encoded_data = QtCore.QByteArray()
  109. stream = QtCore.QDataStream(encoded_data, QtCore.QIODevice.WriteOnly)
  110. for index in indexes:
  111. if index.isValid():
  112. text = self.data(index, 0)
  113. stream << QtCore.QByteArray(text.encode('utf-8'))
  114. mimedata.setData('application/vnd.treeviewdragdrop.list', encoded_data)
  115. return mimedata
  116. def dropMimeData(self, data, action, row, column, parent):
  117. if action == QtCore.Qt.IgnoreAction:
  118. return True
  119. if not data.hasFormat('application/vnd.treeviewdragdrop.list'):
  120. return False
  121. if column > 0:
  122. return False
  123. num_rows = self.rowCount(QtCore.QModelIndex())
  124. if num_rows <= 0:
  125. return False
  126. if row < 0:
  127. if parent.isValid():
  128. row = parent.row()
  129. else:
  130. return False
  131. encoded_data = data.data('application/vnd.treeviewdragdrop.list')
  132. stream = QtCore.QDataStream(encoded_data, QtCore.QIODevice.ReadOnly)
  133. new_items = []
  134. rows = 0
  135. while not stream.atEnd():
  136. text = QtCore.QByteArray()
  137. stream >> text
  138. text = bytes(text).decode('utf-8')
  139. index = self.nodes.index(text)
  140. new_items.append((text, index))
  141. rows += 1
  142. self.lastDroppedItems = []
  143. for (text, index) in new_items:
  144. target_row = row
  145. if index < row:
  146. target_row += 1
  147. self.beginInsertRows(QtCore.QModelIndex(), target_row, target_row)
  148. self.nodes.insert(target_row, text)
  149. self.endInsertRows()
  150. self.lastDroppedItems.append(text)
  151. row += 1
  152. self.pendingRemoveRowsAfterDrop = True
  153. return True
  154. class SumListSelectionModel(QtCore.QItemSelectionModel):
  155. def __init__(self, parent=None):
  156. QtCore.QItemSelectionModel.__init__(self, parent)
  157. def onModelItemsReordered(self):
  158. new_selection = QtCore.QItemSelection()
  159. new_index = QtCore.QModelIndex()
  160. for item in self.model().lastDroppedItems:
  161. row = self.model().rowForItem(item)
  162. if row is None:
  163. continue
  164. new_index = self.model().index(row, 0, QtCore.QModelIndex())
  165. new_selection.select(new_index, new_index)
  166. self.clearSelection()
  167. flags = QtCore.QItemSelectionModel.ClearAndSelect | \
  168. QtCore.QItemSelectionModel.Rows | \
  169. QtCore.QItemSelectionModel.Current
  170. self.select(new_selection, flags)
  171. self.setCurrentIndex(new_index, flags)
  172. class SummaryList(QListView):
  173. def __init__(self,prt):
  174. super(SummaryList, self).__init__()
  175. print("list")
  176. self.prt = prt
  177. model = SumListModel(sum)
  178. selectionModel = SumListSelectionModel(model)
  179. model.dragDropFinished.connect(selectionModel.onModelItemsReordered)
  180. self.setModel(model)
  181. self.setSelectionModel(selectionModel)
  182. self.setDragDropMode(QAbstractItemView.InternalMove)
  183. self.setDragDropOverwriteMode(False)
  184. class SummaryActions(QWidget):
  185. def __init__(self,sum):
  186. print("actions")
  187. # for p in self.sum:
  188. # class SummaryActions():
  189. # def
  190. class ContentStack(QWidget):
  191. def __init__(self, core):
  192. super(ContentStack, self).__init__()
  193. # self.grid = QGridLayout()
  194. hbox = QHBoxLayout()
  195. hbox.setContentsMargins(0,0,0,0)
  196. self.setLayout(hbox)
  197. hsplitter = QSplitter(QtCore.Qt.Horizontal)
  198. self.summary = Summary(core)
  199. hsplitter.addWidget(self.summary)
  200. self.mdsource = QLabel("Content (markdown src).")
  201. hsplitter.addWidget(self.mdsource)
  202. self.mdpreview = QLabel("Content (markdown preview).")
  203. hsplitter.addWidget(self.mdpreview)
  204. hbox.addWidget(hsplitter)