forked from BSCrumpton/Deadline2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
135 lines (123 loc) · 5.35 KB
/
config.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
# Anki Deadline2
# Anki 2.1 plugin
# Author: BSC
# Version 2_3
# Description: Adjusts 'New Cards per Day' setting of options group to ensure all cards
# are seen by deadline.
# License: GNU GPL v3 <www.gnu.org/licenses/gpl.html>
import datetime, time, math
from PyQt6.QtWidgets import *
from anki.hooks import wrap, addHook
from aqt import *
from . import ConfigForm, CalForm
from aqt.main import AnkiQt
from anki.utils import intTime
from aqt.utils import showWarning, openHelp, getOnlyText, askUser, showInfo, openLink
class DeadlineDialog(QDialog):
def __init__(self):
QDialog.__init__(self, parent=mw) #, Qt.Window)
self.mw = aqt.mw
self.deadlines = mw.addonManager.getConfig(__name__)
self.deadlines.pop("test")
self.form = ConfigForm.Ui_Dialog()
self.form.setupUi(self)
self.setWindowTitle(_("Deadline") )
self.form.ProcessDeadlineBox.clicked.connect(self.callDeadlines)
self.fillFields()
self.setupSignals()
if(self.deadlines.get("oneOrMany","")=="Many"):
self.form.OneOrManyBox.setCurrentIndex(1)
else:
self.form.OneOrManyBox.setCurrentIndex(0)
self.resize(500, 500)
self.Calwindow=QDialog(self)
self.LayoutForCal=CalForm.Ui_Dialog()
self.LayoutForCal.setupUi(self.Calwindow)
self.LayoutForCal.pushButton.clicked.connect(self.readValues)
self.open()
def callDeadlines(self):
from . import manualDeadlines
tempString=str(self.form.OneOrManyBox.currentText())
if(tempString.find("Single")!=-1):
self.deadlines["oneOrMany"]="One"
else:
self.deadlines["oneOrMany"]="Many"
mw.addonManager.writeConfig(__name__, self.deadlines)
manualDeadlines()
def fillFields(self):
self.form.fieldList.clear()
for user in self.deadlines["deadlines"]:
if user != str(aqt.mw.pm.name):
continue
for deck, deadline in self.deadlines["deadlines"].get(user).items():
if(deadline==""):
continue
self.form.fieldList.addItem("user:{{{}}} deck:{{{}}} date:{{{}}}".format(user, deck,deadline))
def setupSignals(self):
f = self.form
f.AddDeadlineButton.clicked.connect(self.onAdd)
f.DeleteDeadlineButton.clicked.connect(self.onDelete)
f.buttonBox.helpRequested.connect(self.onHelp)
def readValues(self):
user=str(aqt.mw.pm.name)
deck=str(self.LayoutForCal.comboBox.currentText())
year=self.LayoutForCal.calendarWidget.selectedDate().year()
month=self.LayoutForCal.calendarWidget.selectedDate().month()
day=self.LayoutForCal.calendarWidget.selectedDate().day()
date="{}-{}-{}".format(year,str(month).zfill(2),str(day).zfill(2))
self.user=user
self.deck=deck
self.date=date
self.Calwindow.close()
if(not self.user in self.deadlines["deadlines"]):
self.deadlines["deadlines"][self.user]={}
self.deadlines["deadlines"][self.user][self.deck]=self.date
tempString=str(self.form.OneOrManyBox.currentText())
if(tempString.find("Single")!=-1):
self.deadlines["oneOrMany"]="One"
else:
self.deadlines["oneOrMany"]="Many"
self.user=""
self.deck=""
self.date=""
self.fillFields()
mw.addonManager.writeConfig(__name__, self.deadlines)
dconf = mw.col.decks.all_config()
tempID=0
DeckIDToUpdate= mw.col.decks.id_for_name(deck)
for conf in dconf:
# if the specialized config already exists, use that
if(conf['name']==deck):
tempID=conf['id']
if(tempID==0):
# else, create the new config
# TODO: use the new function names to do this
tempID = mw.col.decks.confId(deck,mw.col.decks.confForDid(DeckIDToUpdate))
deckToUpdate = mw.col.decks.byName(deck)
deckToUpdate['conf']=tempID
mw.col.decks.save(deckToUpdate)
def onAdd(self):
self.Calwindow.show()
self.user = ""
self.LayoutForCal.comboBox.clear()
for deck in sorted(aqt.mw.col.decks.allNames()):
deckId=mw.col.decks.byName(deck)['id']
new_cards = mw.col.db.scalar("""select count() from cards where type = 0 and queue != -1 and did = ?""", deckId)
if(new_cards<1):
continue
self.LayoutForCal.comboBox.addItem(deck)
def onDelete(self):
while self.form.fieldList.selectedIndexes():
temp=self.form.fieldList.item(self.form.fieldList.selectedIndexes()[0].row()).text()
self.form.fieldList.takeItem(self.form.fieldList.selectedIndexes()[0].row())
fields=temp.split("}")
user=fields[0].split("{")[1]
deck=fields[1].split("{")[1]
date=fields[2].split("{")[1]
self.deadlines["deadlines"].get(user).pop(deck)
mw.addonManager.writeConfig(__name__, self.deadlines)
delConfId=mw.col.decks.byName(deck)['conf']
mw.col.decks.remConf(delConfId)
# self.fillFields()
def onHelp(self):
openLink('https://github.com/BSCrumpton/Deadline2')