-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
1102 lines (869 loc) · 35.5 KB
/
app.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Main App
"""
import os
import sys
import webbrowser
import qtmodern.styles
import qtmodern.windows
import validators
from bs4 import BeautifulSoup
from PyQt5 import Qt, QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QFileDialog, QShortcut
from GUI import GUI
from utils import data
from utils.bypass_browser import getBrowser
from utils.card import Card, Logger
from utils.citer import cite
from utils.clipboard_WIN import clipboard
from utils.distro import tagDisplay
from utils.export import printPDF
from utils.feedback import Feedback
from utils.ISD_theme import ISD
from utils.resource import PATH
from utils.text_scraper import text
from utils.updater import Updater
from utils.version_check import check, pollReleases
class main(GUI):
"""
Adds logic to the GUI class
"""
def __init__(self, isLight=False, ISD=False) -> None:
super().__init__(isLight=isLight, ISD=ISD)
# Close Event
self.closeEvent = self._onClose
# Defining ISD settings
self.ISD = ISD
# Error
self.error = None
# Setting Slots
# Custom Emphasis Levels
self.primary_em.clicked.connect(self._primaryEmphasis)
self.secondary_em.clicked.connect(self._secondaryEmphasis)
self.tertiary_em.clicked.connect(self._tertiaryEmphasis)
# Generic Styling
self.bold.clicked.connect(self._bold_)
self.underline.clicked.connect(self._underline_)
self.italics.clicked.connect(self._italic_)
self.primary_highlight.clicked.connect(self._highlightP)
self.secondary_highlight.clicked.connect(self._highlightS)
self.minimize.clicked.connect(self._minimizeText)
self.clearSelectionFormatting.clicked.connect(self._clearFormatting)
# Misc
self.autocut.clicked.connect(self._auto)
self.autobypass.clicked.connect(self._autoBypass)
self.shortcuts.currentTextChanged.connect(self._updateShortcut)
self.shortcut_input.editingFinished.connect(self._saveShortcut)
self.evidence_box.textChanged.connect(self._addDelimiter)
self.cardSelector.currentIndexChanged.connect(self._cardSelectionChanged)
self.tab_master.currentChanged.connect(self._tabChanged)
self.theme.clicked.connect(self._toggleTheme)
self.new_card.clicked.connect(self._newCard)
self.open_card.clicked.connect(self._loadCard)
self.delete_card.clicked.connect(self._deleteCard)
self.copy_card.clicked.connect(self._copy)
self.save_card.clicked.connect(self._print)
self.updates.clicked.connect(self._updates)
self.submit_feedback.clicked.connect(self._feedback)
# Other __init__ reqs
self._loadSettings(initialLoad = True)
self._loadShortcuts()
self.__loadAllCards()
if not self.error: self._log()
self._loadCard(initialLoad = True)
self.hasClickedDeleteOnce = False
# Prompt update if needed
self._updater_()
# Show docs if needed
self._firstLoad_()
"""
Updater
"""
def _updater_(self):
"""
Determines whether to present an update window
"""
data = pollReleases() # preventing excess GitHub API calls
if not data: return
# Create dialog and show it while forcing it to be resolved
# prior to showing the main gui
self.updateDialog = Updater(parent=self, data=data)
self.updateDialog.exec_()
"""
First Load
"""
def _firstLoad_(self):
"Opens Docs if needed"
if not data.getFirstLoad(): return
webbrowser.open("https://docs.cutit.cards", 1)
data.setFirstLoad()
"""
AutoBypass
"""
def _autoBypass(self):
"""
Generates Bypassed Browser
"""
getBrowser()
"""
Data Functions
"""
def _loadSettings(self, initialLoad = False):
"""
Loads all user data into instance vars
Fills in UI with Settings if initialLoad
"""
# Adding Preferences
self._font_ = data.getPref("Font")
self._zoom_ = data.getPref("Zoom")
self.Primary_Highlight_Color = data.getPref("Primary Highlight Color")
self.Secondary_Highlight_Color = data.getPref("Secondary Highlight Color")
self.unscaled_Font_Size_Normal = data.getPref("Font Size of Normal Text")
self.Font_Size_Normal = self.unscaled_Font_Size_Normal + self._zoom_
font = QtGui.QFont()
font.setFamily(self._font_)
font.setPointSize(self.unscaled_Font_Size_Normal)
self.evidence_box.setFont(font)
self.unscaled_Font_Size_Min = data.getPref("Font Size of Minimized Text")
self.Font_Size_Min = self._zoom_ + self.unscaled_Font_Size_Min
self.Primary_Em = data.getPref("Primary Emphasis Settings")
self.Secondary_Em = data.getPref("Secondary Emphasis Settings")
self.Tertiary_Em = data.getPref("Tertiary Emphasis Settings")
self.Font_Size_Primary_Em = self.Primary_Em[4] + self._zoom_
self.Font_Size_Secondary_Em = self.Secondary_Em[4] + self._zoom_
self.Font_Size_Tertiary_Em = self.Tertiary_Em[4] + self._zoom_
self.Theme = data.getPref("Theme")
# Adding Shortcuts
self.primaryEmphasis = data.getShort("Primary Emphasis")
self.secondaryEmphasis = data.getShort("Secondary Emphasis")
self.tertiaryEmphasis = data.getShort("Tertiary Emphasis")
self.clearFormatting = data.getShort("Clear Formatting")
self.minimizeText = data.getShort("Minimize Text")
self.newCard = data.getShort("Cut a New Card")
self.autoPoll = data.getShort("AutoPoll")
self.autoCite = data.getShort("AutoCite")
self.autoCiteAndPoll = data.getShort("AutoPoll + AutoCite")
self.print = data.getShort("Save As PDF")
self.closeWindow = data.getShort("Close Window")
# Set Zoom
self.evidence_box.zoomIn(self._zoom_)
if self.tertiaryEmphasis == "Ctrl+.": self.error = True
self.__loadSettingsUI() if initialLoad else ... # We don't need to reapply settings to UI if user has already selected them
def __loadSettingsUI(self):
"""
Loads the Settings into the GUI
"""
self.font.setCurrentText(self._font_)
self.font_size_normal.setValue(self.unscaled_Font_Size_Normal)
self.font_size_min.setValue(self.unscaled_Font_Size_Min)
self.zoom.setValue(self._zoom_)
self.highlight_1.setCurrentText(self.Primary_Highlight_Color)
self.highlight_2.setCurrentText(self.Secondary_Highlight_Color)
self.primary_bold.setCheckState(self.Primary_Em[0])
self.primary_bold.setTristate(False)
self.primary_italicised.setCheckState(self.Primary_Em[1])
self.primary_italicised.setTristate(False)
self.primary_underline.setCheckState(self.Primary_Em[2])
self.primary_underline.setTristate(False)
self.primary_highlight_2.setCurrentText(str(self.Primary_Em[3]))
self.primary_size.setValue(self.Primary_Em[4])
self.secondary_bold.setCheckState(self.Secondary_Em[0])
self.secondary_bold.setTristate(False)
self.secondary_italicised.setCheckState(self.Secondary_Em[1])
self.secondary_italicised.setTristate(False)
self.secondary_underline.setCheckState(self.Secondary_Em[2])
self.secondary_underline.setTristate(False)
self.secondary_highlight_2.setCurrentText(str(self.Secondary_Em[3]))
self.secondary_size.setValue(self.Secondary_Em[4])
self.tertiary_bold.setCheckState(self.Tertiary_Em[0])
self.tertiary_bold.setTristate(False)
self.tertiary_italicised.setCheckState(self.Tertiary_Em[1])
self.tertiary_italicised.setTristate(False)
self.tertiary_underline.setCheckState(self.Tertiary_Em[2])
self.tertiary_underline.setTristate(False)
self.tertiary_highlight.setCurrentText(str(self.Tertiary_Em[3]))
self.tertiary_size.setValue(self.Tertiary_Em[4])
all_shortcuts = data.getPrefData()["shortcuts"]
for shortcut in list(all_shortcuts.keys()):
self.shortcuts.addItem(shortcut)
def _updateShortcut(self):
"""
Updates shortcut when user wants to view a separate one
"""
try:
newSequence = data.getShort(self.shortcuts.currentText())
except KeyError:
return
if newSequence == 'Choose a shortcut to view/edit . . . (applies on restart)':
return
self.shortcut_input.setKeySequence(newSequence)
def _saveShortcut(self):
"""
Saves shortcut to memory when user is done editing
"""
currentSequence = self.shortcut_input.keySequence()
data.setShort(self.shortcuts.currentText(), currentSequence.toString())
def _loadShortcuts(self):
"""
Inits custom keybindings for all user-defined shortcuts
"""
self.clearFormatting_ = QShortcut(QtGui.QKeySequence(self.clearFormatting), self.evidence_box) # Standard alternative
self.clearFormatting_.activated.connect(self._clearFormatting)
self.primaryEmphasis_ = QShortcut(QtGui.QKeySequence(self.primaryEmphasis), self.evidence_box)
self.primaryEmphasis_.activated.connect(self._primaryEmphasis)
self.secondaryEmphasis_ = QShortcut(QtGui.QKeySequence(self.secondaryEmphasis), self.evidence_box)
self.secondaryEmphasis_.activated.connect(self._secondaryEmphasis)
self.tertiaryEmphasis_ = QShortcut(QtGui.QKeySequence(self.tertiaryEmphasis), self.evidence_box)
self.tertiaryEmphasis_.activated.connect(self._tertiaryEmphasis)
self.minimizeText_ = QShortcut(QtGui.QKeySequence(self.minimizeText), self.evidence_box)
self.minimizeText_.activated.connect(self._minimizeText)
self.newCard_ = QShortcut(QtGui.QKeySequence(self.newCard), self.evidence_box)
self.newCard_.activated.connect(self._newCard)
self.autoPoll_ = QShortcut(QtGui.QKeySequence(self.autoPoll), self.evidence_box)
self.autoPoll_.activated.connect(self._autoPoll)
self.autoCite_ = QShortcut(QtGui.QKeySequence(self.autoCite), self.evidence_box)
self.autoCite_.activated.connect(self._autoCite)
self.autoCiteAndPoll_ = QShortcut(QtGui.QKeySequence(self.autoCiteAndPoll), self.evidence_box)
self.autoCiteAndPoll_.activated.connect(self._autoCiteAndPoll)
self.print_ = QShortcut(QtGui.QKeySequence(self.print), self.evidence_box)
self.print_.activated.connect(self._print)
self.closeWindow_ = QShortcut(QtGui.QKeySequence(self.closeWindow), self.evidence_box)
self.closeWindow_.activated.connect(self._onClose)
self.closeWindow__ = QShortcut(QtGui.QKeySequence(self.closeWindow), self.feedback)
self.closeWindow__.activated.connect(self._onClose)
self.closeWindow___ = QShortcut(QtGui.QKeySequence(self.closeWindow), self.distro)
self.closeWindow___.activated.connect(self._onClose)
def _saveSettings(self):
"""
Saves settings to data.json
"""
data.setPref("Font", self.font.currentText())
# Allow zoom reapplication w/o reboot
data.setPref("Zoom", self.zoom.value())
data.setPref("Primary Highlight Color", self.highlight_1.currentText())
data.setPref("Secondary Highlight Color", self.highlight_2.currentText())
data.setPref("Font Size of Normal Text", self.font_size_normal.value())
data.setPref("Font Size of Minimized Text", self.font_size_min.value())
data.setPref("Primary Emphasis Settings", [
self.primary_bold.isChecked(),
self.primary_italicised.isChecked(),
self.primary_underline.isChecked(),
self.primary_highlight_2.currentText(),
self.primary_size.value()
])
data.setPref("Secondary Emphasis Settings", [
self.secondary_bold.isChecked(),
self.secondary_italicised.isChecked(),
self.secondary_underline.isChecked(),
self.secondary_highlight_2.currentText(),
self.secondary_size.value()
])
data.setPref("Tertiary Emphasis Settings", [
self.tertiary_bold.isChecked(),
self.tertiary_italicised.isChecked(),
self.tertiary_underline.isChecked(),
self.tertiary_highlight.currentText(),
self.tertiary_size.value()
])
"""
Misc. Utils
"""
def _updates(self):
"""
Changes text of button to notify if update is needed or not
"""
self.updates.setText(check())
def _feedback(self):
"""
Submits feedback
"""
self.t2 = Feedback(message=self.feedback.toPlainText())
self.t2.start()
self.feedback.clear()
def _toggleTheme(self):
"""
Changes theme from current to reciprocal (applies on reboot)
(eg. light -> dark, dark -> light)
"""
# Define current theme, new theme
currentTheme = data.getPref("Theme")
newTheme = "light" if currentTheme == "dark" else "dark"
self.isLight = True if newTheme == "light" else False
GUI.updateStyling(self)
# Change theme on current instance
if newTheme == "light" and not self.ISD: qtmodern.styles.light(app)
elif newTheme == "light" and self.ISD: ISD(app)
else: qtmodern.styles.dark(app)
# Store new preference for future instances
data.setPref("Theme", newTheme)
def _addDelimiter(self):
"""
Triggered when the evidence box's text changes
Adds closing bracket ] when an opening one [ is typed
"""
cursor = self.evidence_box.textCursor()
if cursor.hasSelection():
return None
START = cursor.selectionStart()-1
if START < 0:
return None
cursor.setPosition(START, QtGui.QTextCursor.KeepAnchor)
latestChar = cursor.selectedText()
if latestChar == "[":
cursor.clearSelection()
cursor.setPosition(START+1, QtGui.QTextCursor.KeepAnchor)
cursor.insertText("[] ")
cursor.setPosition(START+1, QtGui.QTextCursor.MoveAnchor)
self.evidence_box.setTextCursor(cursor)
def _toHTML(self, copy = False, isForExport = False):
"""
Returns list: [text of card, html of card]
Copies plain & rich text to clipboard IFF :param: copy -> True
"""
doc = self.evidence_box.document()
soup = BeautifulSoup(doc.toHtml(), 'html.parser')
paragraphs = soup.find_all('p')
html = ""
for p in paragraphs:
html += str(p)
html = html.replace('<br>', ' ')
# Formatting for export
if copy or isForExport:
html = html.replace('font-weight:600;', 'font-weight:bold;')
html = html.replace(f'font-size:{self.Font_Size_Min}pt;', f'font-size:{self.unscaled_Font_Size_Min}pt;')
html = html.replace(f'font-size:{self.Font_Size_Normal}pt;', f'font-size:{self.unscaled_Font_Size_Normal}pt;')
html = html.replace(f'font-size:{self.Font_Size_Primary_Em}pt;', f'font-size:{self.Primary_Em[4]}pt;')
html = html.replace(f'font-size:{self.Font_Size_Secondary_Em}pt;', f'font-size:{self.Secondary_Em[4]}pt;')
html = html.replace(f'font-size:{self.Font_Size_Tertiary_Em}pt;', f'font-size:{self.Tertiary_Em[4]}pt;')
html = f'<body style="font-size: {self.Font_Size_Normal}pt; font-family: {self._font_}">' + html + '</body>'
text = str(soup.text)[2:]
clipboard.add(text, html) if copy else ...
return [text, html]
def _auto(self, autoCite = False, autoPoll = False):
"""
Adds MLA & Debate-Grade Citation and/or article text to evidence box
"""
html = f"<p>"
URL = self.link.text()
if not validators.url(URL):
return
# Adding Citation if appropriate
if self.autocite.isChecked() or autoCite:
self.autocite.setCheckState(False)
CREDS = self.creds.text()
TAG = self.warrant.text()
try:
self.msg.clear()
self.msg.setText("Getting citation . . .")
citation = cite(URL)
missingAttrs = citation.getMissingAttrs()
if missingAttrs:
msgStr = "We weren't able to find the following information from the URL - you'll have to enter it manually: "
for attr in missingAttrs:
msgStr += attr + ", "
self.msg.clear()
self.msg.setText(msgStr[:-2])
except Exception:
self.msg.clear()
self.msg.setText("Sorry, there was an error getting your citation.")
return
debate_citation = citation.debate()
mla_citation = citation.mla()
if TAG != "":
html += f"""
<span style='background-color: yellow; font-size: {self.Font_Size_Primary_Em}pt;'><u><strong>
{TAG}
</strong></u></span><br>
"""
html += f"""
<span style='background-color: cyan; font-size: {self.Font_Size_Primary_Em}pt;'><u><strong>
{debate_citation[0]} '{debate_citation[1]}<br>
</strong></u></span>
"""
if CREDS != "":
html += f"""
<i>
{CREDS}<br>
</i>
"""
html += f"""
{debate_citation[2]} • {debate_citation[3]}<br>
{mla_citation}<br><br>
"""
# Adding Article Text if appropriate
if self.autopoll.isChecked() or autoPoll:
self.autopoll.setCheckState(False)
try:
self.msg.clear()
self.msg.setText("Polling text . . .")
article = text.scrape(URL)
html += article
except Exception:
self.msg.clear()
self.msg.setText(f"Sorry, we can't support {URL}! Please use our Chrome Extension instead.")
html += "</p>"
# Inserting Text
cursor = self.evidence_box.textCursor()
cursor.setPosition(0)
cursor.insertHtml(html)
self._toHTML(copy = True)
self.msg.clear()
self.msg.setText("AutoCut complete!")
def _copy(self):
"""
Copies card to clipboard
"""
self._toHTML(copy = True)
def _onClose(self, event = None):
"""
Behavior for window close (saves card first)
"""
self._newCard()
if len(self.cardSelector.currentText()) >= 1:
self.index = self.cardSelector.currentText()[:self.cardSelector.currentText().find(":")]
try:
self.index = int(self.index)
except Exception:
self.index = None
else:
self.index = None
data.setIndex(self.index)
self._saveSettings()
event.accept() if event else self.close()
def _tabChanged(self):
"""
Saves settings and reapplies them on tab change
"""
self._saveSettings()
self._loadSettings()
def _log(self):
return # TODO implement better number of cards cut handler && # downloads
"""
Card History Utilities
"""
def __loadAllCards(self):
"""
Adds all cards to card history selector
"""
# Clearing all data in combobox
self.cardSelector.clear()
self.cardSelector.addItem("Select Card & open with 'Open Selection'")
cards = data.getCardData()["cards"]
# Running Index counter
self.cardIndex = 0 # current max index of card selector
for card in cards:
card = Card(**card) # Construct Card from dict
# If we have a tagline, use that as a primary identifier
TAG = card.TAG.replace('\t', '').replace('\n', '')
TEXT = card.TEXT.replace('\t', '').replace('\n', '')
if TAG.replace(' ', '') != "":
self.cardSelector.addItem(f"{self.cardIndex}: {TAG} - {TEXT}")
# Or just use the card text
else:
self.cardSelector.addItem(f"{self.cardIndex}: {TEXT}")
# Move to next index (we won't have blanks due to the filtering in _saveCard w/ Card.isCard())
self.cardIndex += 1
def _saveCard(self) -> bool:
"""
Saves current card if it has data (is not blank)
"""
evidence_data = self._toHTML()
# Create Card
card = Card(
self.warrant.text(),
self.creds.text(),
self.link.text(),
evidence_data[0],
evidence_data[1]
)
# Return if card has no data (avoid saving blank cards to .json)
if not card.isCard():
return False
if self.index is None:
data.addCard(card)
else:
data.addCard(card, idx = self.index)
return True
def _newCard(self):
"""
Saves old card and opens new one
"""
# Save card
self._saveCard()
# Prepare new card framework
self._addToCardSelector()
self.autocite.setCheckState(True)
self.autocite.setTristate(False)
self.autopoll.setCheckState(True)
self.autopoll.setTristate(False)
document = self.evidence_box.document()
document.clear()
self.warrant.setText("")
self.creds.setText("")
self.link.setText("")
self.index = None
self.cardSelector.setCurrentIndex(0)
def _loadCard(self, initialLoad = False):
"""
Loads most recent card (saves previous one as well and adds to card selector)
"""
if len(self.cardSelector.currentText()) >= 1 and not self.cardSelector.currentText()[0].isnumeric():
self.index = None
return
# Add to selector, recheck auto's
if not initialLoad:
self._addToCardSelector()
self.autocite.setCheckState(True)
self.autocite.setTristate(False)
self.autopoll.setCheckState(True)
self.autopoll.setTristate(False)
self._saveCard()
# Get index of most recent card
if len(self.cardSelector.currentText()) >= 1:
self.index = data.getIndex() if initialLoad else self.cardSelector.currentText()[:self.cardSelector.currentText().find(":")]
else:
self.index = None
# Convert to int if appropriate
try:
self.index = int(self.index)
except Exception:
self.index = None
# Clear Fields
document = self.evidence_box.document()
document.clear()
if self.index is None:
self.warrant.setText("")
self.creds.setText("")
self.link.setText("")
return
# If we do have a card, get it and set all the respective attrs in the GUI
card = data.getCard(self.index)
# If we don't have a previous card -> Set all fields as empty and return
if self.index is None or card is None:
self.warrant.setText("")
self.creds.setText("")
self.link.setText("")
return
self.warrant.setText(card.TAG)
self.creds.setText(card.CREDS)
self.link.setText(card.URL)
self.cardSelector.setCurrentIndex(self.index + 1)
# Insert html
cursor = self.evidence_box.textCursor()
cursor.insertHtml(card.HTML)
cursor.setPosition(0, QtGui.QTextCursor.MoveAnchor)
self.evidence_box.setTextCursor(cursor)
def _deleteCard(self):
"""
Deletes currently open card after second click for safety
"""
if self.hasClickedDeleteOnce:
data.deleteCard(self.index)
# Clearing fields
self.msg.clear()
self.warrant.setText("")
self.creds.setText("")
self.link.setText("")
document = self.evidence_box.document()
document.clear()
# Removing from card selector
try:
if len(self.cardSelector.currentText()) > 0:
currentIndex = int(self.cardSelector.currentText()[:self.cardSelector.currentText().find(":")])
else:
currentIndex = None
except Exception:
currentIndex = None
self.cardSelector.removeItem(currentIndex + 1) if currentIndex is not None else self.cardSelector.setCurrentIndex(0)
self.msg.setText("Card deleted successfully!")
self.hasClickedDeleteOnce = False
# Reload all cards to avoid index errors
self.__loadAllCards()
else:
# If the card in Card Selector isn't what is actually open, alert user.
try:
if len(self.cardSelector.currentText()) > 0:
currentIndex = int(self.cardSelector.currentText()[:self.cardSelector.currentText().find(":")])
else:
currentIndex = None
except Exception:
currentIndex = None
if (currentIndex is not None) and (currentIndex != self.index):
self.msg.clear()
message = "<b>WARNING!</b> You are attempting to delete the card that is currently open, "
message += "NOT what is selected in the Card History bar. If you want to proceed, click the delete button again."
self.msg.insertHtml(message)
else:
self.msg.clear()
self.msg.setText("""You are attempting to delete the currently opened card. Click the delete button again to confirm.""")
self.hasClickedDeleteOnce = True
def _addToCardSelector(self):
"""
Adds current card to card selector
"""
# If the card already has an index, it already exists in the selector
if self.index is not None:
return
# Create Card
evidence_data = self._toHTML()
card = Card(
self.warrant.text(),
self.creds.text(),
self.link.text(),
evidence_data[0],
evidence_data[1]
)
# If the card isn't a card, don't add it
if not card.isCard():
return
# Add card to selector
TAG = card.TAG.replace('\t', '').replace('\n', '')
TEXT = card.TEXT.replace('\t', '').replace('\n', '')
if TAG.replace(' ', '') != "":
self.cardSelector.addItem(f"{self.cardIndex}: {TAG} - {TEXT}")
else:
self.cardSelector.addItem(f"{self.cardIndex}: {TEXT}")
self.cardIndex += 1
def _cardSelectionChanged(self):
"""
Resets the delete status (clicked once) if the selected card changes
"""
self.hasClickedDeleteOnce = False
"""
Shortcut Functions
"""
def _autoCiteAndPoll(self):
"""
Adds MLA & Debate-Grade Citation & article text to evidence box
"""
self._auto(True, True)
def _autoCite(self):
"""
Adds MLA & Debate-Grade Citation to evidence box
"""
self._auto(True, False)
def _autoPoll(self):
"""
Adds article text to evidence box
"""
self._auto(False, True)
def _print(self):
"""
Triggers User Input for Directory and Saves Card as PDF
"""
self.msg.clear()
self.msg.setText("Printing card . . .")
# Getting Current User's Home Dir
startingDir = os.path.expanduser("~")
# Getting input for target dir
destDir = QFileDialog.getExistingDirectory(None,
'Folder To Save In',
startingDir,
QFileDialog.ShowDirsOnly)
# Getting HTML, filename
html = self._toHTML(isForExport = True)[1]
cardName = self.warrant.text() if self.warrant.text() != "" else "Cut-It Export"
path = f"{destDir}"
try:
if path == "":
self.msg.clear()
self.msg.setText("Invalid destination selected.")
return
printPDF(html, path, cardName = cardName)
self.msg.clear()
self.msg.setText("Saved PDF Successfully!")
except Exception as e:
self.msg.clear()
self.msg.setText("There was an error saving your .pdf.")
"""
High-Level Emphasis Functions
"""
def __getSelectedText(self) -> tuple:
"""
Returns cursor's start index and currently selected text in a tuple
"""
# Get text cursor
cursor = self.evidence_box.textCursor()
# If there's no selection return None
if not cursor.hasSelection():
return None
# Return tuple
return (cursor.selectionStart(), cursor.selectedText())
def __addText(self, html, index):
"""
Inserts formatted text at cursor position and reselects text
Copies text to clipboard
"""
# Get text cursor
cursor = self.evidence_box.textCursor()
# Add HTML
cursor.insertHtml(html)
# Reset Position
cursor.setPosition(index, QtGui.QTextCursor.KeepAnchor)
# Copy to clipboard
self._toHTML(copy=True)
"""
Mid-Level Emphasis Functions
"""
def _primaryEmphasis(self):
"""
Styles text with Primary Emphasis
"""
selection_data = self.__getSelectedText()
if selection_data == None:
return
text = selection_data[1]
text = self._bold(text) if self.Primary_Em[0] else text
text = self._italic(text) if self.Primary_Em[1] else text
text = self._underline(text) if self.Primary_Em[2] else text
text = self._highlight(text, self.Primary_Em[3]) if self.Primary_Em[3] != None else text
text = f'<span style="font-size:{self.Font_Size_Primary_Em}pt">{text}</span>'
self.__addText(text, selection_data[0])
self._toHTML(copy = True)
def _secondaryEmphasis(self):
"""
Styles text with Secondary Emphasis
"""
selection_data = self.__getSelectedText()
if selection_data == None:
return
text = selection_data[1]
text = self._bold(text) if self.Secondary_Em[0] else text
text = self._italic(text) if self.Secondary_Em[1] else text
text = self._underline(text) if self.Secondary_Em[2] else text
text = self._highlight(text, self.Secondary_Em[3]) if self.Secondary_Em[3] != None else text
text = f'<span style="font-size:{self.Font_Size_Secondary_Em}">{text}</span>'
self.__addText(text, selection_data[0])
self._toHTML(copy = True)
def _tertiaryEmphasis(self):
"""
Styles text with Tertiary Emphasis
"""
selection_data = self.__getSelectedText()
if selection_data == None:
return
text = selection_data[1]
text = self._bold(text) if self.Tertiary_Em[0] else text
text = self._italic(text) if self.Tertiary_Em[1] else text
text = self._underline(text) if self.Tertiary_Em[2] else text
text = self._highlight(text, self.Tertiary_Em[3]) if self.Tertiary_Em[3] != None else text
text = f'<span style="font-size:{self.Font_Size_Tertiary_Em}">{text}</span>'
self.__addText(text, selection_data[0])
self._toHTML(copy = True)
def _highlightP(self):
"""
Highlights text with Primary Highlight Color
"""
selection_data = self.__getSelectedText()
if selection_data == None:
return
HTML = f"<span style='background-color: {self.Primary_Highlight_Color}'>{selection_data[1]}</span>"
self.__addText(HTML, selection_data[0])
self._toHTML(copy = True)
def _highlightS(self):
"""
Highlights text with Secondary Highlight Color
"""
selection_data = self.__getSelectedText()
if selection_data == None:
return
HTML = f"<span style='background-color: {self.Secondary_Highlight_Color}'>{selection_data[1]}</span>"
self.__addText(HTML, selection_data[0])
self._toHTML(copy = True)
def _bold_(self):
"""
Bolds selected text
"""
selection_data = self.__getSelectedText()
if selection_data == None:
return
self.__addText(self._bold(selection_data[1]), selection_data[0])
self._toHTML(copy = True)
def _underline_(self):
"""
Underlines selected text
"""
selection_data = self.__getSelectedText()
if selection_data == None:
return