Skip to content

Commit

Permalink
improved UI, Typecast checkbox constants to integer for 2.1.24
Browse files Browse the repository at this point in the history
  • Loading branch information
lovac42 committed Apr 10, 2020
1 parent ef5e8ca commit 469280c
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 74 deletions.
9 changes: 0 additions & 9 deletions src/hoochie_mama/const.py

This file was deleted.

4 changes: 2 additions & 2 deletions src/hoochie_mama/hoochieMama.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from .sort import CUSTOM_SORT
from .utils import *
from .const import *
from .lib.com.lovac42.anki.version import ANKI20



Expand Down Expand Up @@ -205,7 +205,7 @@ def cntFn(did, lim):

anki.sched.Scheduler._fillRev = wrap(anki.sched.Scheduler._fillRev, fillRev, 'around')
anki.sched.Scheduler._resetRevCount = wrap(anki.sched.Scheduler._resetRevCount, resetRevCount, 'around')
if ANKI21:
if not ANKI20:
import anki.schedv2
anki.schedv2.Scheduler._fillRev = wrap(anki.schedv2.Scheduler._fillRev, fillRev, 'around')
anki.schedv2.Scheduler._resetRevCount = wrap(anki.schedv2.Scheduler._resetRevCount, resetRevCount, 'around')
Expand Down
Empty file.
Empty file.
Empty file.
9 changes: 9 additions & 0 deletions src/hoochie_mama/lib/com/lovac42/anki/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2020 Lovac42
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html


__author__ = "lovac42"

__version__ = "0.0.2"

Empty file.
38 changes: 38 additions & 0 deletions src/hoochie_mama/lib/com/lovac42/anki/gui/checkbox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2020 Lovac42
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html


from aqt.qt import *
from anki.lang import _


class TristateCheckbox(QCheckBox):
_descriptions = {
Qt.Unchecked: "Disabled",
Qt.PartiallyChecked: "Partial",
Qt.Checked: "Full"
}

def __init__(self, parent):
super(TristateCheckbox, self).__init__(parent)
self.setTristate(True)
self.stateChanged.connect(self.onStateChanged)

def onStateChanged(self, state):
assert Qt.Unchecked <= state <= Qt.Checked
desc = self._descriptions[state]
self.setText(_(desc))

def getDescriptions(self):
return self._descriptions

def setDescriptions(self, desc):
assert len(desc) == 3
self._descriptions = desc

def setCheckState(self, state):
assert Qt.Unchecked <= state <= Qt.Checked
super(TristateCheckbox, self).setCheckState(state)
if not state:
self.onStateChanged(Qt.Unchecked)
24 changes: 24 additions & 0 deletions src/hoochie_mama/lib/com/lovac42/anki/gui/muffins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2019-2020 Lovac42
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html


from aqt.qt import *


def getMuffinsTab(ui_pref):
try:
return ui_pref.lrnStageGLayout
except AttributeError:
ui_pref.lrnStage = QWidget()
ui_pref.tabWidget.addTab(ui_pref.lrnStage, "Muffins")
ui_pref.lrnStageGLayout = QGridLayout()
ui_pref.lrnStageVLayout = QVBoxLayout(ui_pref.lrnStage)
ui_pref.lrnStageVLayout.addLayout(ui_pref.lrnStageGLayout)
spacerItem = QSpacerItem(
1, 1,
QSizePolicy.Expanding,
QSizePolicy.Expanding
)
ui_pref.lrnStageVLayout.addItem(spacerItem)
return ui_pref.lrnStageGLayout
24 changes: 24 additions & 0 deletions src/hoochie_mama/lib/com/lovac42/anki/gui/toolbar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2020 Lovac42
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html


from aqt import QMenu

def getMenu(parent, menuName):
menubar = parent.form.menubar
for a in menubar.actions():
if menuName == a.text():
return a.menu()
else:
return menubar.addMenu(menuName)


def getSubMenu(menu, subMenuName):
for a in menu.actions():
if subMenuName == a.text():
return a.menu()
else:
subMenu = QMenu(subMenuName, menu)
menu.addMenu(subMenu)
return subMenu
21 changes: 21 additions & 0 deletions src/hoochie_mama/lib/com/lovac42/anki/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2020 Lovac42
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html


from anki import version

ANKI20 = version.startswith("2.0.")

CCBC = version.endswith("ccbc")

ANKI21 = not CCBC and version.startswith("2.1.")

VERSION = version.split('_')[0] #rm ccbc
m,n,p = VERSION.split('.')

MAJOR_VERSION = int(m)
MINOR_VERSION = int(n)
PATCH_VERSION = int(p)
POINT_VERSION = 0 if ANKI20 else PATCH_VERSION

143 changes: 80 additions & 63 deletions src/hoochie_mama/prefmenu.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,118 +11,135 @@
from anki.hooks import wrap

from .sort import CUSTOM_SORT
from .const import *
from .lib.com.lovac42.anki.version import ANKI21, ANKI20
from .lib.com.lovac42.anki.gui.checkbox import TristateCheckbox
from .lib.com.lovac42.anki.gui import muffins

#Must use IF-ELSE, potention exception using try-catch on some systems.
if ANKI21 and not CCBC:

if ANKI21:
from PyQt5 import QtCore, QtGui, QtWidgets
else:
from PyQt4 import QtCore, QtGui as QtWidgets


def setupUi(self, Preferences):
try:
grid=self.lrnStageGLayout
except AttributeError:
self.lrnStage=QtWidgets.QWidget()
self.tabWidget.addTab(self.lrnStage, "Muffins")
self.lrnStageGLayout=QtWidgets.QGridLayout()
self.lrnStageVLayout=QtWidgets.QVBoxLayout(self.lrnStage)
self.lrnStageVLayout.addLayout(self.lrnStageGLayout)
spacerItem=QtWidgets.QSpacerItem(1, 1, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
self.lrnStageVLayout.addItem(spacerItem)

r=self.lrnStageGLayout.rowCount()
self.hoochieMama=QtWidgets.QCheckBox(self.lrnStage)
self.hoochieMama.setText(_('Hoochie Mama! Randomize Queue'))
self.hoochieMama.setTristate(True)
self.lrnStageGLayout.addWidget(self.hoochieMama, r, 0, 1, 3)
grid_layout = muffins.getMuffinsTab(self)
r = grid_layout.rowCount()
mama_groupbox = QtWidgets.QGroupBox(self.lrnStage)
mama_groupbox.setTitle("Hoochie Mama!")
mama_grid_layout = QtWidgets.QGridLayout(mama_groupbox)
grid_layout.addWidget(mama_groupbox, r, 0, 1, 3)

r=0

self.hoochieMama = TristateCheckbox(mama_groupbox)
self.hoochieMama.setDescriptions({
Qt.Unchecked: "Hoochie Mama addon has been disabled",
Qt.PartiallyChecked: "Randomize review cards, check subdeck limits",
Qt.Checked: "Randomize review cards, discard subdeck limits (~V2)",
})

mama_grid_layout.addWidget(self.hoochieMama, r, 0, 1, 3)
self.hoochieMama.clicked.connect(lambda:toggle(self))

r+=1
self.hoochieMamaSortLbl=QtWidgets.QLabel(self.lrnStage)
self.hoochieMamaSortLbl.setText(_(" Sort RevQ By:"))
self.lrnStageGLayout.addWidget(self.hoochieMamaSortLbl, r, 0, 1, 1)
self.hoochieMamaSortLbl=QtWidgets.QLabel(mama_groupbox)
self.hoochieMamaSortLbl.setText(_(" Sort reviews by:"))
mama_grid_layout.addWidget(self.hoochieMamaSortLbl, r, 0, 1, 1)

self.hoochieMamaSort = QtWidgets.QComboBox(self.lrnStage)
if ANKI21:
itms=CUSTOM_SORT.items()
else:
itms=CUSTOM_SORT.iteritems()
for i,v in itms:
self.hoochieMamaSort = QtWidgets.QComboBox(mama_groupbox)

sort_itms = CUSTOM_SORT.iteritems if ANKI20 else CUSTOM_SORT.items
for i,v in sort_itms():
self.hoochieMamaSort.addItem(_(""))
self.hoochieMamaSort.setItemText(i, _(v[0]))
self.lrnStageGLayout.addWidget(self.hoochieMamaSort, r, 1, 1, 2)
mama_grid_layout.addWidget(self.hoochieMamaSort, r, 1, 1, 3)

r+=1 #Avoid round-robin reviews
self.hoochieMamaPTD=QtWidgets.QCheckBox(self.lrnStage)
self.hoochieMamaPTD.setText(_("Prioritize today's revs first?"))
self.lrnStageGLayout.addWidget(self.hoochieMamaPTD, r, 1, 1, 3)
self.hoochieMamaPTD=QtWidgets.QCheckBox(mama_groupbox)
self.hoochieMamaPTD.setText(_("Prioritize the reviews due today first?"))
mama_grid_layout.addWidget(self.hoochieMamaPTD, r, 1, 1, 3)

r+=1 #Force Extra shuffle
self.hoochieMamaExRand=QtWidgets.QCheckBox(self.lrnStage)
self.hoochieMamaExRand.setText(_('Use Extra Shuffle?'))
self.hoochieMamaExRand.setTristate(True)
self.lrnStageGLayout.addWidget(self.hoochieMamaExRand, r, 1, 1, 3)
self.hoochieMamaExRand.clicked.connect(lambda:togExtRand(self))
self.hoochieMamaExRand = TristateCheckbox(mama_groupbox)
self.hoochieMamaExRand.setDescriptions({
Qt.Unchecked: "Add Extra Shuffle?",
Qt.PartiallyChecked: "Extra Shuffle: Fine (e.g. 1,5,3,2)",
Qt.Checked: "Extra Shuffle: Coarse (e.g. 3,25,9,6)",
})
mama_grid_layout.addWidget(self.hoochieMamaExRand, r, 1, 1, 3)



def load(self, mw):
qc = self.mw.col.conf
cb=qc.get("hoochieMama", 0)

cb=qc.get("hoochieMama", Qt.Unchecked)
self.form.hoochieMama.setCheckState(cb)
cb=qc.get("hoochieMama_prioritize_today", 0)

cb=qc.get("hoochieMama_prioritize_today", Qt.Unchecked)
self.form.hoochieMamaPTD.setCheckState(cb)

cb=qc.get("hoochieMama_extra_shuffle", 0)
cb=qc.get("hoochieMama_extra_shuffle", Qt.Unchecked)
self.form.hoochieMamaExRand.setCheckState(cb)

idx=qc.get("hoochieMamaSort", 0)
self.form.hoochieMamaSort.setCurrentIndex(idx)

toggle(self.form)



def save(self):
toggle(self.form)
qc = self.mw.col.conf
qc['hoochieMama']=self.form.hoochieMama.checkState()
qc['hoochieMama_prioritize_today']=self.form.hoochieMamaPTD.checkState()
qc['hoochieMama_extra_shuffle']=self.form.hoochieMamaExRand.checkState()

qc['hoochieMama']=int(self.form.hoochieMama.checkState())
qc['hoochieMama_prioritize_today']=int(self.form.hoochieMamaPTD.checkState())
qc['hoochieMama_extra_shuffle']=int(self.form.hoochieMamaExRand.checkState())

qc['hoochieMamaSort']=self.form.hoochieMamaSort.currentIndex()



def toggle(self):
checked=self.hoochieMama.checkState()
if checked:
try:
self.serenityNow.setCheckState(0)
self.serenityNow.setCheckState(Qt.Unchecked)
except: pass
grayout=False
grayout = False
else:
grayout=True
grayout = True

if checked==1:
txt='Hoochie Mama! RandRevQ w/ subdeck limit'
self.hoochieMamaExRand.setDisabled(True)
if checked == Qt.PartiallyChecked:
self.hoochieMamaExRand.setText(_('Extra Shuffle (Mandatory)'))
self.hoochieMamaExRand.setDisabled(True)
else:
txt='Hoochie Mama! Randomize Rev Queue'
self.hoochieMamaExRand.setDisabled(grayout)
togExtRand(self)
self.hoochieMama.setText(_(txt))
#refresh checkbox desc text
s = self.hoochieMamaExRand.checkState()
self.hoochieMamaExRand.onStateChanged(s)

self.hoochieMamaPTD.setDisabled(grayout)
self.hoochieMamaSort.setDisabled(grayout)
self.hoochieMamaSortLbl.setDisabled(grayout)

def togExtRand(self):
checked=self.hoochieMamaExRand.checkState()
if checked==2: #hoochie overwhelming
self.hoochieMamaExRand.setText(_('Extra Shuffle: Coarse (e.g. 3,25,9,6)'))
elif checked==1: #less hoochie
self.hoochieMamaExRand.setText(_('Extra Shuffle: Fine (e.g. 1,5,3,2)'))
else:
self.hoochieMamaExRand.setText(_('Use Extra Shuffle?'))


aqt.forms.preferences.Ui_Preferences.setupUi = wrap(aqt.forms.preferences.Ui_Preferences.setupUi, setupUi, "after")
aqt.preferences.Preferences.__init__ = wrap(aqt.preferences.Preferences.__init__, load, "after")
aqt.preferences.Preferences.accept = wrap(aqt.preferences.Preferences.accept, save, "before")


# if point version < 23? Use old wrap
# TODO: Find the point version for these new hooks.

aqt.forms.preferences.Ui_Preferences.setupUi = wrap(
aqt.forms.preferences.Ui_Preferences.setupUi, setupUi, "after")

aqt.preferences.Preferences.__init__ = wrap(
aqt.preferences.Preferences.__init__, load, "after"
)

aqt.preferences.Preferences.accept = wrap(
aqt.preferences.Preferences.accept, save, "before"
)

0 comments on commit 469280c

Please sign in to comment.