forked from Arjav96/noteCV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
executable file
·337 lines (266 loc) · 10.6 KB
/
Main.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import sys, MySQLdb
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import QFont
from UIconstants import *
import opencv as opencv
gSubject = None
class TextEditor(QtGui.QTextEdit) :
# class for the main editor window
# Inherited from QTextEdit
def __init__(self, parent = None) :
super(TextEditor,self).__init__(parent)
font = self.font()
font.setPointSize(PARAFONT)
self.setFont(font)
self.setFontPointSize(PARAFONT)
# Heading format
def setHeadingFormat(self) :
self.setFontWeight(QFont.Black)
self.setFontPointSize(HEADFONT)
self.setAlignment(QtCore.Qt.AlignCenter)
# Sub Heading format
def setSubHeadingFormat(self) :
self.setFontWeight(55)
self.setFontPointSize(SUBHEADFONT)
self.setAlignment(QtCore.Qt.AlignLeft)
# Paragraph format
def setParagraphFormat(self) :
self.setFontWeight(QFont.Normal)
self.setFontPointSize(PARAFONT)
self.setAlignment(QtCore.Qt.AlignLeft)
# Bold format
def setBoldFont(self) :
if self.fontWeight() != QFont.Black :
self.setFontWeight(QFont.Black)
else :
self.setFontWeight(QFont.Normal)
# Italic format
def setItalicFont(self) :
self.setFontItalic(not self.fontItalic())
# Underline format
def setUnderlineFont(self) :
self.setFontUnderline(not self.fontUnderline())
# Left Align format
def setLeftAlign(self) :
self.setAlignment(QtCore.Qt.AlignLeft)
# Center Align format
def setCenterAlign(self) :
self.setAlignment(QtCore.Qt.AlignCenter)
# Right Align format
def setRightAlign(self) :
self.setAlignment(QtCore.Qt.AlignRight)
# Highlight format
def setHighlight(self) :
if self.textBackgroundColor() != QtGui.QColor(255,255,0) :
self.setTextBackgroundColor(QtGui.QColor(255,255,0))
else :
self.setTextBackgroundColor(QtGui.QColor(255,255,255))
# Add diagram code
def addDiagram(self) :
global gSubject
loc = opencv.draw(gSubject, 1)
print(loc)
image = QtGui.QImage(loc, 'JPG')
cursor = QtGui.QTextCursor(self.document())
cursor.movePosition(QtGui.QTextCursor.End,QtGui.QTextCursor.MoveAnchor)
cursor.insertImage(image, loc)
#cursor.insertImage(QtCore.QString(loc))
# Subroutine for adding graph api :: made by baba ;P
def addGraph(self) :
loc = opencv.draw(gSubject, 2)
print(loc)
image = QtGui.QImage(loc, 'JPG')
cursor = QtGui.QTextCursor(self.document())
cursor.movePosition(QtGui.QTextCursor.End,QtGui.QTextCursor.MoveAnchor)
cursor.insertImage(image, loc)
return
class Widget(QtGui.QWidget) :
# Widget class inherited from QWidget
# Only to create a central Widget
def __init__(self, parent = None) :
super(Widget, self).__init__(parent)
self.createTextEditor()
self.createQuickAccessToolbar()
def createTextEditor(self) :
# Create text editor of the
self.textEditor = TextEditor(self)
self.textEditor.move(STARTPOSEDITOR[0], STARTPOSEDITOR[1])
self.textEditor.resize(WIDTH - STARTPOSEDITOR[0]-40, HEIGHT-STARTPOSEDITOR[1]-40)
def createQuickAccessToolbar(self) :
# Create quick access tooolbar for easy access
# Heading button
self.headingBtn = QtGui.QPushButton("HEADING",self)
self.headingBtn.clicked.connect(self.textEditor.setHeadingFormat)
self.headingBtn.move(20,STARTPOSEDITOR[1]+10)
self.headingBtn.resize(STARTPOSEDITOR[0]-35, 40)
# Sub Heading Button
self.subHeadingBtn = QtGui.QPushButton("SUB HEADING",self)
self.subHeadingBtn.clicked.connect(self.textEditor.setSubHeadingFormat)
self.subHeadingBtn.move(20,STARTPOSEDITOR[1]+10+60)
self.subHeadingBtn.resize(STARTPOSEDITOR[0]-35, 40)
# Paragraph Button
self.paragraphBtn = QtGui.QPushButton("PARAGRAPH",self)
self.paragraphBtn.clicked.connect(self.textEditor.setParagraphFormat)
self.paragraphBtn.move(20,STARTPOSEDITOR[1]+10+60+60)
self.paragraphBtn.resize(STARTPOSEDITOR[0]-35, 40)
# Bold Button
self.boldBtn = QtGui.QPushButton("",self)
self.boldBtn.clicked.connect(self.textEditor.setBoldFont)
self.boldBtn.move(20,STARTPOSEDITOR[1]+10+60+60+60)
self.boldBtn.resize(40, 40)
self.boldBtn.setIcon(QtGui.QIcon('./images/icons/bold.png'))
self.boldBtn.setIconSize(QtCore.QSize(35,35))
# Italics Button
self.italicBtn = QtGui.QPushButton("",self)
self.italicBtn.clicked.connect(self.textEditor.setItalicFont)
self.italicBtn.move(84,STARTPOSEDITOR[1]+10+60+60+60)
self.italicBtn.resize(40, 40)
self.italicBtn.setIcon(QtGui.QIcon('./images/icons/italics.png'))
self.italicBtn.setIconSize(QtCore.QSize(20,20))
# UnderLine button
self.underlineBtn = QtGui.QPushButton("",self)
self.underlineBtn.clicked.connect(self.textEditor.setUnderlineFont)
self.underlineBtn.move(73+73,STARTPOSEDITOR[1]+10+60+60+60)
self.underlineBtn.resize(40, 40)
self.underlineBtn.setIcon(QtGui.QIcon('./images/icons/underline.png'))
self.underlineBtn.setIconSize(QtCore.QSize(25,25))
# Left alignment :)
self.leftBtn = QtGui.QPushButton("",self)
self.leftBtn.clicked.connect(self.textEditor.setLeftAlign)
self.leftBtn.move(20,STARTPOSEDITOR[1]+10+60+60+60+60)
self.leftBtn.resize(40, 40)
self.leftBtn.setIcon(QtGui.QIcon('./images/icons/left-align.png'))
self.leftBtn.setIconSize(QtCore.QSize(35,35))
# Center align button
self.centerBtn = QtGui.QPushButton("",self)
self.centerBtn.clicked.connect(self.textEditor.setCenterAlign)
self.centerBtn.move(84,STARTPOSEDITOR[1]+10+60+60+60+60)
self.centerBtn.resize(40, 40)
self.centerBtn.setIcon(QtGui.QIcon('./images/icons/center-align.png'))
self.centerBtn.setIconSize(QtCore.QSize(34,34))
# Right button
self.rightBtn = QtGui.QPushButton("",self)
self.rightBtn.clicked.connect(self.textEditor.setRightAlign)
self.rightBtn.move(73+73,STARTPOSEDITOR[1]+10+60+60+60+60)
self.rightBtn.resize(40, 40)
self.rightBtn.setIcon(QtGui.QIcon('./images/icons/right-align.png'))
self.rightBtn.setIconSize(QtCore.QSize(34,34))
# Highlight button
self.highlightBtn = QtGui.QPushButton("HIGHLIGHT",self)
self.highlightBtn.clicked.connect(self.textEditor.setHighlight)
self.highlightBtn.move(20,STARTPOSEDITOR[1]+10+60+60+60+60+60)
self.highlightBtn.resize(STARTPOSEDITOR[0]-35, 40)
# Add diagram button
self.diagramBtn = QtGui.QPushButton("ADD DIAGRAM",self)
self.diagramBtn.clicked.connect(self.textEditor.addDiagram)
self.diagramBtn.move(20,STARTPOSEDITOR[1]+10+60+60+60+60+60+120)
self.diagramBtn.resize(STARTPOSEDITOR[0]-35, 60)
# Add graph button
self.graphBtn = QtGui.QPushButton("ADD GRAPH",self)
self.graphBtn.clicked.connect(self.textEditor.addGraph)
self.graphBtn.move(20,STARTPOSEDITOR[1]+10+60+60+60+60+60+100+100)
self.graphBtn.resize(STARTPOSEDITOR[0]-35, 60)
class Editor(QtGui.QMainWindow) :
gParent = None
def __init__(self, subject = None, filename = "", parent = None) :
super(Editor, self).__init__(parent)
global gParent, gSubject
gParent = parent
gSubject = subject
self.filename = filename
self.changesSaved = False
self.makeUI()
self.openFile(filename)
def makeUI(self) :
self.textWidget = Widget(self)
self.move(0,0)
self.resize(WIDTH, HEIGHT)
self.setCentralWidget(self.textWidget)
self.show()
# def main() :
# app = QtGui.QApplication(sys.argv)
# print("Buffalo")
# sys.exit(app.exec_())
# # main()
self.setCentralWidget(self.textWidget)
#################################################################################
## Defining Quick access toolbar
## Features : Open Save Undo Redo
## Future development : Font, Font size, color, etc
#################################################################################
self.actionSave = QtGui.QAction(QtGui.QIcon("./images/icons/save.png"), "", self)
self.actionSave.setShortcut("Ctrl+S")
self.actionSave.triggered.connect(self.performSave)
self.actionOpen = QtGui.QAction(QtGui.QIcon("./images/icons/open.png"), "", self)
self.actionOpen.setShortcut("Ctrl+O")
self.actionOpen.triggered.connect(self.performOpen)
self.actionUndo = QtGui.QAction(QtGui.QIcon("./images/icons/undo.ico"), "", self)
self.actionUndo.setShortcut("Ctrl+Z")
self.actionUndo.triggered.connect(self.performUndo)
self.actionRedo = QtGui.QAction(QtGui.QIcon("./images/icons/redo.png"), "", self)
self.actionRedo.setShortcut("Ctrl+Y")
self.actionRedo.triggered.connect(self.performRedo)
self.toolbar = self.addToolBar("QuickAccessToolbar")
self.toolbar.addAction(self.actionOpen)
self.toolbar.addAction(self.actionSave)
self.toolbar.addAction(self.actionUndo)
self.toolbar.addAction(self.actionRedo)
def changed(self) :
self.changesSaved = False
def closeEvent(self,event):
if self.changesSaved:
event.accept()
else:
confirmExitWindow = QtGui.QMessageBox(self)
confirmExitWindow.setIcon(QtGui.QMessageBox.Warning)
confirmExitWindow.setText("Changes occured!!!")
confirmExitWindow.setInformativeText("Do you want to save your changes?")
confirmExitWindow.setStandardButtons(QtGui.QMessageBox.Save | QtGui.QMessageBox.Cancel | QtGui.QMessageBox.Discard)
confirmExitWindow.setDefaultButton(QtGui.QMessageBox.Save)
answer = confirmExitWindow.exec_()
if answer == QtGui.QMessageBox.Save:
self.performSave()
elif answer == QtGui.QMessageBox.Discard:
event.accept()
else:
event.ignore()
def openFile(self,filename) :
with open(filename,"rt") as file:
self.textWidget.textEditor.setText(file.read())
def performSave(self):
global gSubject
if not self.filename:
self.filename = QtGui.QFileDialog.getSaveFileName(self, 'Save File')
if self.filename:
# Append extension if not there yet
if not str(self.filename).endswith(".html"):
self.filename += ".html"
with open(self.filename,"wt") as file:
file.write(self.textWidget.textEditor.toHtml())
self.changesSaved = True
db = MySQLdb.connect("localhost", "root", "root", "noteCV")
connection = db.cursor()
temp = connection.execute("SELECT fname,subject FROM NOTES WHERE fpath=%s and Subject=%s;",(str(self.filename),str(gSubject)))
results = connection.fetchall()
if len(results) == 0:
fname = (self.filename.split('.')[0]).split('/')[-1]
print(str(self.filename), str(fname), str(gSubject))
connection.execute("INSERT INTO NOTES VALUES(%s, %s, %s);",(str(self.filename), str(fname), str(gSubject)))
db.commit()
print('Here')
db.close()
def performOpen(self) :
self.filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File',".","(*.html)")
if self.filename:
with open(self.filename,"rt") as file:
self.textWidget.textEditor.setText(file.read())
def performUndo(self) :
self.textWidget.textEditor.undo()
def performRedo(self) :
self.textWidget.textEditor.redo()
##############################################
# def main() :
# # app = QtGui.QApplication(sys.argv)
# editorWindow = Editor("maths")
# editorWindow.show()
# # sys.exit(app.exec_())