-
Notifications
You must be signed in to change notification settings - Fork 26
/
window.py
285 lines (248 loc) · 8.85 KB
/
window.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import os
import subprocess
import sys
from PySide import QtGui, QtCore
from unipath import Path
from dialogs import OpenDialog
from editor import Editor
from preview import Preview, PDFPane
from tree import Tree
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
# super(MainWindow, self).__init__(parent)
if sys.platform == 'darwin':
# Workaround for Qt issue on OS X that causes QMainWindow to
# hide when adding QToolBar, see
# https://bugreports.qt-project.org/browse/QTBUG-4300
super(MainWindow, self).__init__(
parent,
QtCore.Qt.MacWindowToolBarButtonHint
)
else:
super(MainWindow, self).__init__(parent)
def setup_app(self):
self.setupActions()
splitter = QtGui.QSplitter(QtCore.Qt.Horizontal)
self.tree = Tree()
self.editor = Editor()
self.tab_widget = QtGui.QTabWidget()
self.preview = Preview()
self.pdf_pane = PDFPane()
self.tab_widget.addTab(self.preview, "HTML")
self.tab_widget.addTab(self.pdf_pane, "PDF")
self.file_path = None
self.output_html_path = None
self.setCentralWidget(splitter)
splitter.addWidget(self.tree)
splitter.addWidget(self.editor)
splitter.addWidget(self.tab_widget)
self.setWindowTitle("Sphinx Docs Editor")
self.createMenus()
self.createToolBars()
self.showMaximized()
def setupActions(self):
"""
Set up the top menu actions and keyboard shortcuts.
"""
# File Menu --------------------------------------------------
self.openAction = QtGui.QAction(
# QtGui.QIcon(":/images/open.png"),
"&Open File",
self,
shortcut="Ctrl+O",
statusTip="Open File",
triggered=self.openFile
)
self.openFolderAction = QtGui.QAction(
# QtGui.QIcon(":/images/open.png"),
"Open Folder",
self,
shortcut="Ctrl+Shift+O",
statusTip="Open Folder",
triggered=self.openFolder
)
self.saveAction = QtGui.QAction(
# QtGui.QIcon(":/images/save.png"),
"&Save File",
self,
shortcut="Ctrl+S",
statusTip="Save File",
triggered=self.saveFile
)
self.saveAsAction = QtGui.QAction(
# QtGui.QIcon(":/images/save.png"),
"Save As File",
self,
shortcut="Ctrl+Shift+S",
statusTip="Save File As...",
triggered=self.saveFileAs
)
self.quitAction = QtGui.QAction(
# QtGui.QIcon(':/images/save.png'),
"&Quit",
self,
shortcut="Ctrl+Q",
statusTip="Quit",
triggered=self.close
)
# Build Menu --------------------------------------------------
self.buildHTMLAction = QtGui.QAction(
"Build &HTML",
self,
shortcut="Ctrl+B",
statusTip="Build HTML",
triggered=self.buildHTML
)
self.buildPDFAction = QtGui.QAction(
"Build &PDF",
self,
shortcut="Ctrl+Shift+B",
statusTip="Build PDF",
triggered=self.buildPDF
)
def createMenus(self):
self.fileMenu = self.menuBar().addMenu("&File")
self.fileMenu.addAction(self.openAction)
self.fileMenu.addAction(self.openFolderAction)
self.fileMenu.addSeparator()
self.fileMenu.addAction(self.saveAction)
self.fileMenu.addAction(self.saveAsAction)
self.fileMenu.addSeparator()
self.fileMenu.addAction(self.quitAction)
self.buildMenu = self.menuBar().addMenu("&Build")
self.buildMenu.addAction(self.buildHTMLAction)
self.buildMenu.addAction(self.buildPDFAction)
def createToolBars(self):
self.fileToolBar = self.addToolBar("File")
self.fileToolBar.addAction(self.openAction)
self.fileToolBar.addAction(self.openFolderAction)
self.fileToolBar.addAction(self.saveAction)
# self.fileToolBar.addAction(self.saveAsAction)
# self.fileToolBar.addAction(self.quitAction)
self.buildToolBar = self.addToolBar("Build")
self.buildToolBar.addAction(self.buildHTMLAction)
self.buildToolBar.addAction(self.buildPDFAction)
def openFile(self, path=None):
"""
Ask the user to open a file via the Open File dialog.
Then open it in the tree, editor, and HTML preview windows.
"""
if not path:
dialog = OpenDialog()
dialog.set_folders_only(False)
path = dialog.getOpenFileName(
self,
"Open File",
'',
"ReStructuredText Files (*.rst *.txt)"
)
if path:
file_path = Path(path[0])
filename = file_path.name
tree_dir = file_path.parent.absolute()
self.handleFileChanged(tree_dir, filename)
def saveFile(self):
if self.file_path:
text = self.editor.toPlainText()
try:
f = open(self.file_path.absolute(), "wb")
f.write(text)
f.close()
# self.rebuildHTML()
except IOError:
QtGui.QMessageBox.information(
self,
"Unable to open file: %s" % self.file_path.absolute()
)
def saveFileAs(self):
filename, _ = QtGui.QFileDialog.getSaveFileName(
self,
'Save File As',
'',
"ReStructuredText Files (*.rst *.txt)"
)
if filename:
text = self.editor.toPlainText()
try:
f = open(filename, "wb")
f.write(text)
f.close()
# self.rebuildHTML()
except IOError:
QtGui.QMessageBox.information(
self,
"Unable to open file: %s" % filename
)
def openFolder(self, path=None):
"""
Ask the user to open a folder (directory) via
the Open Folder dialog. Then open it in the tree,
editor, and HTML preview windows.
"""
if not path:
dialog = OpenDialog()
dialog.set_folders_only(True)
path = dialog.getExistingDirectory(self, "Open Folder", '')
if path:
self.handleFileChanged(path)
def handleFileChanged(self, dir, filename=None):
"""
This is called whenever the active file is changed.
It sets the tree, editor, and preview panes to the new file.
"""
if not filename:
# TODO: find first rst file if index.rst doesn't exist.
filename = "index.rst"
self.file_path = Path(dir, filename)
file_stem = str(self.file_path.stem)
html_str = "_build/html/{0}.html".format(file_stem)
self.output_html_path = Path(dir, html_str).absolute()
# Load the file into the editor
self.editor.open_file(self.file_path)
# Load the directory containing the file into the tree.
self.tree.load_from_dir(dir)
# Load corresponding HTML file from pre-built Sphinx docs
self.preview.load_html(self.output_html_path)
def buildHTML(self):
"""
Builds the .html version of the active file and reloads
it in the preview pane.
"""
# TODO: make this configurable via a dialog
os.chdir(self.file_path.parent)
proc = subprocess.Popen(
["make", "clean"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT
)
for line in proc.stdout:
print("stdout: " + line.rstrip())
print('----------------')
proc = subprocess.Popen(
["make", "html"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT
)
proc.wait()
for line in proc.stdout:
print("stdout: " + line.rstrip())
# Load corresponding HTML file from newly-built Sphinx docs
self.preview.load_html(self.output_html_path)
def buildPDF(self):
"""
Builds the .pdf version of the active file.
"""
# TODO: get this working
# TODO: make this configurable via a dialog
os.chdir(self.file_path.parent)
proc = subprocess.Popen(
["make", "latexpdf"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT
)
proc.wait()
for line in proc.stdout:
print("stdout: " + line.rstrip())