/
outliner.py
173 lines (126 loc) · 5.59 KB
/
outliner.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
"""
" File: outliner.py
" Written By: Gregory Owen
"
" An easy way to synthesize notes into an essay outline.
"""
from Tkinter import *
from tkFileDialog import *
from collections import deque
import tkSimpleDialog
import tkMessageBox
from outlinermodel import OutlinerModel
from outlinergui import OutlinerGUI
class Outliner():
def __init__(self, master=None):
self.model = OutlinerModel(self)
self.gui = OutlinerGUI(master, self, self.model)
""" ----------------------------------------------------------------- """
""" Menu methods """
""" ----------------------------------------------------------------- """
def newProject(self):
""" Create a new project. """
notepath = askopenfilename()
if notepath is not None and notepath is not "":
self.model.newModel(notepath)
self.gui.displayNextNote()
def openProject(self):
""" Open a previous project from its .otln file. """
projectPath = askopenfilename(filetypes=[("Outliner files", "*.otln")])
if projectPath is "":
return
if projectPath[-5:] != ".otln":
errorPrompt = "I'm sorry, but that file is not a valid input " +\
"type.\nPlease choose a valid Outliner file (.otln)"
tkMessageBox.showerror("Error: Invalid File Type", errorPrompt)
return
self.model.openModel(projectPath)
self.gui.openGUI()
def saveProject(self):
""" Save the current state of the project. """
if self.model.filename is None:
self.saveProjectAs()
else:
self.model.saveModel()
def saveProjectAs(self):
""" Save the project under a new name. """
options = {}
options['defaultextension'] = '.otln'
options['filetypes'] = [('all files', '.*'), ('Outliner files', '.otln')]
options['title'] = 'Save your outline'
self.model.filename = asksaveasfilename(**options)
if self.model.filename is not "":
self.saveProject()
def exportOutline(self):
""" Create a .txt outline based off of the notes in the Outliner. """
options = {}
options['defaultextension'] = '.txt'
options['filetypes'] = [('all files', '.*'), ('Text files', '.txt')]
options['title'] = 'Export your outline to a text file'
exportpath = asksaveasfilename(**options)
if exportpath is not "":
self.model.exportModel(exportpath)
def quit(self):
""" Quit the outliner. """
self.gui.root.quit()
""" ------------------------------------------------------------------ """
""" Topic methods """
""" ------------------------------------------------------------------ """
def newTopic(self):
""" Create a new topic. """
topicPrompt = "What would you like to call your new topic?"
topicName = tkSimpleDialog.askstring("New Topic", topicPrompt)
if topicName in self.model.topics:
self.topicAlreadyExists()
topicName = None
if topicName is None:
return
self.model.newTopic(topicName)
self.gui.initializeTopicGUI(self.model.topics[topicName])
def topicAlreadyExists(self):
""" Report to the user that there is already a topic with the name that
they entered. """
errorprompt = "I'm sorry, but a topic by that name already exists in" +\
" this outline.\nPlease select a different name."
tkMessageBox.showerror("Error: Topic Already Exists", errorprompt)
def viewTopic(self, topic):
""" Display the notes that are part of the topic. """
self.gui.viewTopic(topic)
def sortTopics(self):
""" Assign numbers to topics according to the order in which they are
currently arranged. """
ordered = self.gui.topicList.getOrdered()
for i in range(len(ordered)):
topic = ordered[i].widget.topic
topic['number'] = i
def addNoteToTopic(self, topic):
""" Add the currently-displayed note to the topic. """
if len(self.model.notes) > 0:
note = self.model.addNoteToTopic(topic)
self.gui.addNoteToGUI(topic, note)
self.gui.updateTopicGUI(topic)
self.gui.displayNextNote()
""" ----------------------------------------------------------------- """
""" Note methods """
""" ----------------------------------------------------------------- """
def nextNote(self):
""" Display the next note in the list. """
if len(self.model.notes) > 0:
self.model.notes.append(self.model.notes.popleft())
self.gui.displayNextNote()
def prevNote(self):
""" Display the last note in the list. """
if len(self.model.notes) > 0:
self.model.notes.appendleft(self.model.notes.pop())
self.gui.displayNextNote()
def sortNotes(self):
""" Sort the notes in each topic according to the order in which they
are currently arranged. """
for topic in self.model.topics.values():
topic['notes'] = [node.widget.cget('text')
for node in topic['dndlist'].getOrdered()]
""" --------------------------------- main method ------------------------------- """
if __name__ == "__main__":
root = Tk()
outliner = Outliner(root)
root.mainloop()