content.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import sys, os
  4. from PyQt5 import QtCore
  5. from PyQt5.QtCore import QSettings
  6. from PyQt5.QtGui import QFont, QSyntaxHighlighter
  7. from PyQt5.QtWidgets import QWidget, QLabel, QHBoxLayout, QVBoxLayout, QSplitter, QListWidget, QListWidgetItem, QAbstractItemView, QButtonGroup, QPushButton, QInputDialog
  8. import markdown
  9. import re
  10. import json
  11. # from classes import orderablelist
  12. class Summary(QWidget):
  13. def __init__(self, core):
  14. super(Summary, self).__init__()
  15. self.core = core
  16. self.jsonfilepath = os.path.join(self.core.cwd,'.config/summary.json')
  17. sum_json = open(self.jsonfilepath).read()
  18. self.sum = json.loads(sum_json)
  19. vbox = QVBoxLayout()
  20. vbox.setContentsMargins(0,0,0,0)
  21. self.list = SummaryList(self)
  22. vbox.addWidget(self.list)
  23. self.actions = SummaryActions(self, core)
  24. vbox.addWidget(self.actions)
  25. self.setLayout(vbox)
  26. def addItem(self, text):
  27. # file
  28. filename = re.sub(r'\W', "_", text)+".md"
  29. # TODO: check if file does not already exists
  30. filepath = os.path.join(self.core.cwd,'contents',filename)
  31. with open(filepath, 'w') as fp:
  32. fp.write('#'+text)
  33. # json
  34. item = {"title":text,"file":filename}
  35. self.sum.append(item)
  36. with open(self.jsonfilepath, "w") as fp:
  37. json.dump(self.sum, fp, ensure_ascii=False, indent="\t")
  38. # refresh list
  39. self.list.addNewItem(item)
  40. def recordNewList(self):
  41. newdata = []
  42. for i in range(0,self.list.count()):
  43. # print(self.item(i).item['title'])
  44. newdata.append(self.list.item(i).item)
  45. # print(newdata)
  46. self.sum = newdata
  47. with open(self.jsonfilepath, "w") as fp:
  48. json.dump(newdata, fp, ensure_ascii=False, indent="\t")
  49. class SummaryList(QListWidget):
  50. def __init__(self, parent):
  51. super(SummaryList, self).__init__(parent)
  52. self.parent = parent
  53. # self.sum = sum
  54. # print(self.sum)
  55. # self.setSortingEnabled(True)
  56. self.setDragEnabled(True)
  57. self.setSelectionMode(QAbstractItemView.SingleSelection)
  58. self.setAcceptDrops(True)
  59. self.setDropIndicatorShown(True)
  60. self.setDragDropMode(QAbstractItemView.InternalMove)
  61. self.model().rowsMoved.connect(self.onRowsMoved)
  62. print(self.model())
  63. for item in self.parent.sum:
  64. self.addNewItem(item)
  65. def onRowsMoved(self, model, start, end, dest):
  66. # print("onRowsMoved")
  67. self.parent.recordNewList()
  68. def addNewItem(self, item):
  69. # slw = SummaryListWidgetItem(self,item)
  70. # lwi = QListWidgetItem(self)
  71. # # Set size hint
  72. # lwi.setSizeHint(slw.sizeHint())
  73. # self.addItem(lwi)
  74. # self.setItemWidget(lwi, slw)
  75. # # self.addItem(QListWidgetItem())
  76. self.addItem(SummaryListWidgetItem(self,item))
  77. class SummaryListWidgetItem(QListWidgetItem):
  78. def __init__(self,parent,item):
  79. super(SummaryListWidgetItem, self).__init__(parent)
  80. self.parent = parent
  81. self.item = item
  82. self.setText(item['title'])
  83. self.setToolTip(item['file'])
  84. class SummaryActions(QWidget):
  85. def __init__(self,parent,core):
  86. super(SummaryActions, self).__init__(parent)
  87. self.parent = parent
  88. self.core = core
  89. self.hbox = QHBoxLayout()
  90. self.hbox.setContentsMargins(0,0,0,0)
  91. new = QPushButton("New Page", self)
  92. new.setShortcut('Ctrl+Shift+n')
  93. # new.setIcon(Icon(ico)))
  94. new.clicked.connect(self.onAddPage)
  95. self.hbox.addWidget(new)
  96. delete = QPushButton("Delete Page", self)
  97. delete.setShortcut('Ctrl+Shift+sup')
  98. # delete.setIcon(Icon(ico)))
  99. delete.clicked.connect(self.onDeletePage)
  100. self.hbox.addWidget(delete)
  101. self.setLayout(self.hbox)
  102. def onAddPage(self):
  103. text, ok = QInputDialog.getText(self, 'Input Dialog', 'Page Name:')
  104. if ok:
  105. self.parent.addItem(text)
  106. def onDeletePage(self):
  107. print("onDeletePage")
  108. # TODO: get the current selected page
  109. # TODO: ask for confirmation for deleting the current selecred page
  110. # TODO: call for summary widget to delete the page
  111. class ContentStack(QWidget):
  112. def __init__(self, core):
  113. super(ContentStack, self).__init__()
  114. # self.grid = QGridLayout()
  115. hbox = QHBoxLayout()
  116. hbox.setContentsMargins(0,0,0,0)
  117. self.setLayout(hbox)
  118. self.hsplitter = QSplitter(QtCore.Qt.Horizontal)
  119. self.summary = Summary(core)
  120. self.hsplitter.addWidget(self.summary)
  121. self.mdsource = QLabel("Content (markdown src).")
  122. self.hsplitter.addWidget(self.mdsource)
  123. self.mdpreview = QLabel("Content (markdown preview).")
  124. self.hsplitter.addWidget(self.mdpreview)
  125. self.hsplitter.splitterMoved.connect(self.movedSplitter)
  126. hbox.addWidget(self.hsplitter)
  127. self.restorePrefs()
  128. def restorePrefs(self):
  129. settings = QSettings('FiguresLibres', 'Cascade')
  130. vals = settings.value('content/hsplitter/sizes', None)
  131. if vals:
  132. sizes = []
  133. for size in vals: sizes.append(int(size))
  134. self.hsplitter.setSizes(sizes)
  135. def movedSplitter(self):
  136. settings = QSettings('FiguresLibres', 'Cascade')
  137. # print(self.hsplitter.sizes())
  138. settings.setValue('content/hsplitter/sizes', self.hsplitter.sizes())