1
yomichan-anki/yomi_base/preferences.py

181 lines
6.1 KiB
Python
Raw Normal View History

2011-08-28 18:01:32 +00:00
# -*- coding: utf-8 -*-
2011-10-27 15:18:57 +00:00
# Copyright (C) 2013 Alex Yatskov
2011-08-28 18:01:32 +00:00
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2012-12-25 20:51:46 +00:00
from PyQt4 import QtGui, QtCore
from gen import preferences_ui
import copy
2011-08-28 18:01:32 +00:00
class DialogPreferences(QtGui.QDialog, preferences_ui.Ui_DialogPreferences):
2011-08-28 18:01:32 +00:00
def __init__(self, parent, preferences, anki):
QtGui.QDialog.__init__(self, parent)
2012-12-25 20:51:46 +00:00
self.setupUi(self)
2011-08-28 18:01:32 +00:00
2011-10-27 15:18:57 +00:00
self.accepted.connect(self.onAccept)
self.buttonColorBg.clicked.connect(self.onButtonColorBgClicked)
self.buttonColorFg.clicked.connect(self.onButtonColorFgClicked)
self.comboBoxModel.currentIndexChanged.connect(self.onModelChanged)
self.comboFontFamily.currentFontChanged.connect(self.onFontFamilyChanged)
self.spinFontSize.valueChanged.connect(self.onFontSizeChanged)
2011-08-28 18:01:32 +00:00
2011-10-08 16:32:51 +00:00
self.preferences = preferences
self.anki = anki
2011-08-28 18:01:32 +00:00
2011-10-08 16:32:51 +00:00
self.dataToDialog()
2011-08-28 18:01:32 +00:00
2011-10-08 16:32:51 +00:00
def dataToDialog(self):
self.checkCheckForUpdates.setChecked(self.preferences['checkForUpdates'])
self.checkLoadRecentFile.setChecked(self.preferences['loadRecentFile'])
self.checkStripReadings.setChecked(self.preferences['stripReadings'])
self.spinScanLength.setValue(self.preferences['scanLength'])
2011-08-28 18:01:32 +00:00
2011-10-08 16:32:51 +00:00
self.updateSampleText()
font = self.textSample.font()
self.comboFontFamily.setCurrentFont(font)
self.spinFontSize.setValue(font.pointSize())
2011-08-28 18:01:32 +00:00
if self.anki is not None:
self.tabAnki.setEnabled(True)
self.profiles = copy.deepcopy(self.preferences['profiles'])
self.profileToDialog()
2011-08-28 18:01:32 +00:00
2011-10-08 16:32:51 +00:00
def dialogToData(self):
self.preferences['checkForUpdates'] = self.checkCheckForUpdates.isChecked()
self.preferences['loadRecentFile'] = self.checkLoadRecentFile.isChecked()
self.preferences['scanLength'] = self.spinScanLength.value()
self.preferences['stripReadings'] = self.checkStripReadings.isChecked()
if self.anki is not None:
self.dialogToProfile()
self.preferences['profiles'] = self.profiles
def dialogToProfile(self):
self.setActiveProfile({
'deck': unicode(self.comboBoxDeck.currentText()),
'model': unicode(self.comboBoxModel.currentText()),
'fields': self.ankiFields()
})
def profileToDialog(self):
profile = self.activeProfile()
deck = str() if profile is None else profile['deck']
model = str() if profile is None else profile['model']
self.comboBoxDeck.addItems(self.anki.deckNames())
self.comboBoxDeck.setCurrentIndex(self.comboBoxDeck.findText(deck))
self.comboBoxModel.blockSignals(True)
self.comboBoxModel.addItems(self.anki.modelNames())
self.comboBoxModel.blockSignals(False)
self.comboBoxModel.setCurrentIndex(self.comboBoxModel.findText(model))
2011-08-28 18:01:32 +00:00
2011-10-08 16:32:51 +00:00
def updateSampleText(self):
palette = self.textSample.palette()
palette.setColor(QtGui.QPalette.Base, QtGui.QColor(self.preferences['bgColor']))
palette.setColor(QtGui.QPalette.Text, QtGui.QColor(self.preferences['fgColor']))
self.textSample.setPalette(palette)
2011-08-28 18:01:32 +00:00
font = self.textSample.font()
font.setFamily(self.preferences['fontFamily'])
font.setPointSize(self.preferences['fontSize'])
self.textSample.setFont(font)
2011-08-28 18:01:32 +00:00
def setAnkiFields(self, fields, fieldsPrefs):
if fields is None:
fields = list()
2012-12-24 00:02:00 +00:00
self.tableFields.setRowCount(len(fields))
2011-08-28 18:01:32 +00:00
for i, name in enumerate(fields):
2011-08-28 18:01:32 +00:00
columns = list()
itemName = QtGui.QTableWidgetItem(name)
itemName.setFlags(QtCore.Qt.ItemIsSelectable)
columns.append(itemName)
itemValue = QtGui.QTableWidgetItem(fieldsPrefs.get(name, unicode()))
columns.append(itemValue)
for j, column in enumerate(columns):
self.tableFields.setItem(i, j, column)
2011-08-28 18:01:32 +00:00
2011-10-08 16:32:51 +00:00
def ankiFields(self):
2011-08-28 18:01:32 +00:00
result = dict()
for i in range(0, self.tableFields.rowCount()):
itemName = unicode(self.tableFields.item(i, 0).text())
itemValue = unicode(self.tableFields.item(i, 1).text())
2011-08-28 18:01:32 +00:00
result[itemName] = itemValue
return result
2011-10-08 16:32:51 +00:00
def onAccept(self):
self.dialogToData()
2011-08-28 18:01:32 +00:00
2011-10-08 16:32:51 +00:00
def onButtonColorFgClicked(self):
color, ok = QtGui.QColorDialog.getRgba(self.preferences['fgColor'], self)
2011-08-28 18:01:32 +00:00
if ok:
self.preferences['fgColor'] = color
2011-10-08 16:32:51 +00:00
self.updateSampleText()
2011-08-28 18:01:32 +00:00
2011-10-08 16:32:51 +00:00
def onButtonColorBgClicked(self):
color, ok = QtGui.QColorDialog.getRgba(self.preferences['bgColor'], self)
2011-08-28 18:01:32 +00:00
if ok:
self.preferences['bgColor'] = color
2011-10-08 16:32:51 +00:00
self.updateSampleText()
2011-08-28 18:01:32 +00:00
2011-10-08 16:32:51 +00:00
def onFontFamilyChanged(self, font):
self.preferences['fontFamily'] = str(font.family())
2011-10-08 16:32:51 +00:00
self.updateSampleText()
2011-08-28 18:01:32 +00:00
2011-10-08 16:32:51 +00:00
def onFontSizeChanged(self, size):
self.preferences['fontSize'] = size
2011-10-08 16:32:51 +00:00
self.updateSampleText()
2012-12-24 00:02:00 +00:00
def onModelChanged(self, index):
modelName = self.comboBoxModel.currentText()
2012-12-25 02:53:41 +00:00
fieldNames = self.anki.modelFieldNames(modelName) or list()
profile = self.activeProfile()
fields = dict() if profile is None else profile['fields']
self.setAnkiFields(fieldNames, fields)
def activeProfile(self):
key = 'vocab' if self.radioButtonVocab.isChecked() else 'kanji'
return self.profiles.get(key)
def setActiveProfile(self, profile):
key = 'vocab' if self.radioButtonVocab.isChecked() else 'kanji'
self.profiles[key] = profile