use group box instead of tabs in card layout screen

This commit is contained in:
Damien Elmes 2020-05-14 17:26:40 +10:00
parent 5167bb57be
commit c5d574e9f4
5 changed files with 233 additions and 185 deletions

View File

@ -29,13 +29,13 @@ from aqt.utils import (
showInfo,
showWarning,
tooltip,
TR, tr, shortcut
)
from aqt.webview import AnkiWebView
# fixme: previewing with empty fields
# fixme: deck name on new cards
# fixme: card count when removing
# fixme: i18n
# fixme: change tracking and tooltip in fields
# fixme: replay suppression
@ -106,17 +106,9 @@ class CardLayout(QDialog):
self.topAreaForm.templatesBox.currentIndexChanged,
self.update_current_ordinal_and_redraw,
)
self.topAreaForm.card_type_label.setText(tr(TR.CARD_TEMPLATES_CARD_TYPE))
def updateTopArea(self):
cnt = self.mw.col.models.useCount(self.model)
self.topAreaForm.changesLabel.setText(
ngettext(
"Changes below will affect the %(cnt)d note that uses this card type.",
"Changes below will affect the %(cnt)d notes that use this card type.",
cnt,
)
% dict(cnt=cnt)
)
self.updateCardNames()
def updateCardNames(self):
@ -162,16 +154,21 @@ class CardLayout(QDialog):
return s
def setupShortcuts(self):
for i in range(1, 9):
QShortcut(
QKeySequence("Ctrl+%d" % i),
self,
activated=lambda i=i: self.selectCard(i),
) # type: ignore
def selectCard(self, n):
self.ord = n - 1
self.redraw_everything()
self.tform.front_button.setToolTip(shortcut("Ctrl+1"))
self.tform.back_button.setToolTip(shortcut("Ctrl+2"))
self.tform.style_button.setToolTip(shortcut("Ctrl+3"))
QShortcut(
QKeySequence("Ctrl+1"),
self,
activated=lambda: self.tform.front_button.setChecked(True))
QShortcut(
QKeySequence("Ctrl+2"),
self,
activated=lambda: self.tform.back_button.setChecked(True))
QShortcut(
QKeySequence("Ctrl+3"),
self,
activated=lambda: self.tform.style_button.setChecked(True))
# Main area
##########################################################################
@ -185,22 +182,35 @@ class CardLayout(QDialog):
# template area
tform = self.tform = aqt.forms.template.Ui_Form()
tform.setupUi(left)
# tform.groupBox_3.setTitle(_("Styling (shared between cards)"))
qconnect(tform.front.textChanged, self.write_edits_to_template_and_redraw)
qconnect(tform.css.textChanged, self.write_edits_to_template_and_redraw)
qconnect(tform.back.textChanged, self.write_edits_to_template_and_redraw)
qconnect(tform.tabWidget.currentChanged, self.on_editor_changed)
qconnect(tform.edit_area.textChanged, self.write_edits_to_template_and_redraw)
tform.front_button.setText(tr(TR.CARD_TEMPLATES_FRONT_TEMPLATE))
qconnect(tform.front_button.toggled, self.on_editor_toggled)
tform.back_button.setText(tr(TR.CARD_TEMPLATES_BACK_TEMPLATE))
qconnect(tform.back_button.toggled, self.on_editor_toggled)
tform.style_button.setText(tr(TR.CARD_TEMPLATES_TEMPLATE_STYLING))
qconnect(tform.style_button.toggled, self.on_editor_toggled)
tform.groupBox.setTitle(tr(TR.CARD_TEMPLATES_TEMPLATE_BOX))
cnt = self.mw.col.models.useCount(self.model)
self.tform.changes_affect_label.setText(self.col.tr(
TR.CARD_TEMPLATES_CHANGES_WILL_AFFECT_NOTES, count=cnt))
l.addWidget(left, 5)
self.search_box = search = QLineEdit()
search.setPlaceholderText("Search")
qconnect(search.textChanged, self.on_search_changed)
qconnect(search.returnPressed, self.on_search_next)
tform.tabWidget.setCornerWidget(search)
self.setup_edit_area()
widg = tform.search_edit
widg.setPlaceholderText("Search")
qconnect(widg.textChanged, self.on_search_changed)
qconnect(widg.returnPressed, self.on_search_next)
# preview area
right = QWidget()
self.pform: Any = aqt.forms.preview.Ui_Form()
pform = self.pform
pform.setupUi(right)
pform.preview_front.setText(tr(TR.CARD_TEMPLATES_FRONT_PREVIEW))
pform.preview_back.setText(tr(TR.CARD_TEMPLATES_BACK_PREVIEW))
pform.preview_box.setTitle(tr(TR.CARD_TEMPLATES_PREVIEW_BOX))
if self._isCloze():
nums = self.note.cloze_numbers_in_fields()
@ -218,6 +228,15 @@ class CardLayout(QDialog):
l.addWidget(right, 5)
w.setLayout(l)
def setup_edit_area(self):
self.current_editor_index = 0
self.tform.edit_area.setAcceptRichText(False)
if qtminor < 10:
self.tform.edit_area.setTabStopWidth(30)
else:
tab_width = self.fontMetrics().width(" " * 4)
self.tform.edit_area.setTabStopDistance(tab_width)
def setup_cloze_number_box(self):
names = (_("Cloze %d") % n for n in self.cloze_numbers)
self.pform.cloze_number_combo.addItems(names)
@ -231,27 +250,24 @@ class CardLayout(QDialog):
self.pform.cloze_number_combo.currentIndexChanged, self.on_change_cloze
)
def current_editor(self) -> QTextEdit:
idx = self.tform.tabWidget.currentIndex()
if idx == 0:
return self.tform.front
elif idx == 1:
return self.tform.back
else:
return self.tform.css
def on_change_cloze(self, idx: int) -> None:
self.ord = self.cloze_numbers[idx] - 1
self._renderPreview()
def on_editor_changed(self, idx: int) -> None:
if idx == 0:
def on_editor_toggled(self):
if self.tform.front_button.isChecked():
self.current_editor_index = 0
self.pform.preview_front.setChecked(True)
elif idx == 1:
elif self.tform.back_button.isChecked():
self.current_editor_index = 1
self.pform.preview_back.setChecked(True)
else:
self.current_editor_index = 2
self.fill_fields_from_template()
def on_search_changed(self, text: str):
editor = self.current_editor()
editor = self.tform.edit_area
if not editor.find(text):
# try again from top
cursor = editor.textCursor()
@ -260,16 +276,10 @@ class CardLayout(QDialog):
editor.find(text)
def on_search_next(self):
self.on_search_changed(self.search_box.text())
text = self.tform.search_edit.text()
self.on_search_changed(text)
def setupWebviews(self):
if theme_manager.night_mode and not theme_manager.macos_dark_mode():
# the grouping box renders incorrectly in the fusion theme. 5.9+
# 5.13 behave differently to 5.14, but it looks bad in either case,
# and adjusting the top margin makes the 'save PDF' button show in
# the wrong place, so for now we just disable the border instead
self.setStyleSheet("QGroupBox { border: 0; }")
pform = self.pform
pform.frontWeb = AnkiWebView(title="card layout")
pform.verticalLayout.addWidget(pform.frontWeb)
@ -350,31 +360,32 @@ class CardLayout(QDialog):
def fill_fields_from_template(self):
t = self.current_template()
self.ignore_change_signals = True
self.tform.front.setPlainText(t["qfmt"])
self.tform.css.setPlainText(self.model["css"])
self.tform.back.setPlainText(t["afmt"])
self.tform.front.setAcceptRichText(False)
self.tform.css.setAcceptRichText(False)
self.tform.back.setAcceptRichText(False)
if qtminor < 10:
self.tform.front.setTabStopWidth(30)
self.tform.css.setTabStopWidth(30)
self.tform.back.setTabStopWidth(30)
if self.current_editor_index == 0:
text = t["qfmt"]
elif self.current_editor_index == 1:
text = t["afmt"]
else:
tab_width = self.fontMetrics().width(" " * 4)
self.tform.front.setTabStopDistance(tab_width)
self.tform.css.setTabStopDistance(tab_width)
self.tform.back.setTabStopDistance(tab_width)
text = self.model["css"]
self.tform.edit_area.setPlainText(text)
self.ignore_change_signals = False
def write_edits_to_template_and_redraw(self):
if self.ignore_change_signals:
return
self.changed = True
t = self.current_template()
t["qfmt"] = self.tform.front.toPlainText()
t["afmt"] = self.tform.back.toPlainText()
self.model["css"] = self.tform.css.toPlainText()
text = self.tform.edit_area.toPlainText()
if self.current_editor_index == 0:
self.current_template()['qfmt'] = text
elif self.current_editor_index == 1:
self.current_template()['afmt'] = text
else:
self.model["css"] = text
self.renderPreview()
# Preview

View File

@ -17,7 +17,16 @@
<property name="spacing">
<number>3</number>
</property>
<property name="margin">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
@ -26,9 +35,9 @@
<number>12</number>
</property>
<item>
<widget class="QLabel" name="label">
<widget class="QLabel" name="card_type_label">
<property name="text">
<string>Card Type:</string>
<string notr="true">CARD TYPE:</string>
</property>
</widget>
</item>
@ -73,16 +82,6 @@
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="changesLabel">
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>

View File

@ -14,68 +14,63 @@
<string notr="true">Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<property name="sizeConstraint">
<enum>QLayout::SetMinimumSize</enum>
</property>
<item>
<widget class="QGroupBox" name="front_back_box">
<property name="title">
<string/>
</property>
<property name="flat">
<bool>true</bool>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="sizeConstraint">
<enum>QLayout::SetMinimumSize</enum>
<widget class="QGroupBox" name="preview_box">
<property name="title">
<string notr="true">GroupBox</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QRadioButton" name="preview_front">
<property name="text">
<string notr="true">FRONT</string>
</property>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QRadioButton" name="preview_front">
<property name="text">
<string notr="true">FRONT</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="preview_back">
<property name="text">
<string notr="true">BACK</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QComboBox" name="cloze_number_combo"/>
</item>
</layout>
</item>
</layout>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="preview_back">
<property name="text">
<string notr="true">BACK</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QComboBox" name="cloze_number_combo"/>
</item>
</layout>
</item>
</layout>
</widget>
</item>
</layout>
</widget>

View File

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>525</width>
<height>721</height>
<width>786</width>
<height>1081</height>
</rect>
</property>
<property name="sizePolicy">
@ -19,7 +19,7 @@
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="leftMargin">
<number>0</number>
</property>
@ -33,47 +33,77 @@
<number>0</number>
</property>
<item>
<widget class="QLabel" name="changes_affect_label">
<property name="text">
<string notr="true">CHANGES_WILL_AFFECT</string>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string notr="true">GroupBox</string>
</property>
</widget>
</item>
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
<string notr="true">FRONT</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QTextEdit" name="front"/>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string notr="true">BACK</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QTextEdit" name="back"/>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_3">
<attribute name="title">
<string notr="true">STYLING</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_5">
<item>
<widget class="QTextEdit" name="css"/>
</item>
</layout>
</widget>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="leftMargin">
<number>12</number>
</property>
<property name="topMargin">
<number>12</number>
</property>
<property name="rightMargin">
<number>12</number>
</property>
<property name="bottomMargin">
<number>12</number>
</property>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QRadioButton" name="front_button">
<property name="toolTip">
<string notr="true"/>
</property>
<property name="text">
<string notr="true">FRONT</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="back_button">
<property name="toolTip">
<string notr="true"/>
</property>
<property name="text">
<string notr="true">BACK</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="style_button">
<property name="toolTip">
<string notr="true"/>
</property>
<property name="text">
<string notr="true">STYLE</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QLineEdit" name="search_edit"/>
</item>
<item>
<widget class="QLabel" name="changes_affect_label">
<property name="text">
<string notr="true">CHANGES_WILL_AFFECT</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QTextEdit" name="edit_area"/>
</item>
</layout>
</widget>
</item>
</layout>

View File

@ -0,0 +1,13 @@
card-templates-changes-will-affect-notes =
{ $count ->
[one] Changes below will affect the { $count } note that uses this card type.
*[other] Changes below will affect the { $count } notes that use this card type.
}
card-templates-card-type = Card Type:
card-templates-front-template = Front Template
card-templates-back-template = Back Template
card-templates-template-styling = Styling
card-templates-front-preview = Front Preview
card-templates-back-preview = Back Preview
card-templates-preview-box = Preview
card-templates-template-box = Template