1
yomichan-anki/yomi_base/preferences.py

156 lines
5.9 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
2011-08-28 18:01:32 +00:00
# Copyright (C) 2011 Alex Yatskov
#
# 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
2011-11-05 16:45:41 +00:00
from util import buildResPath
2012-12-25 20:51:46 +00:00
from ui_gen import preferences
2011-08-28 18:01:32 +00:00
2012-12-25 20:51:46 +00:00
class DialogPreferences(QtGui.QDialog, preferences.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.buttonContentColorFg.clicked.connect(self.onButtonColorFgClicked)
self.buttonContentColorBg.clicked.connect(self.onButtonColorBgClicked)
self.comboContentFontFamily.currentFontChanged.connect(self.onFontFamilyChanged)
self.spinContentFontSize.valueChanged.connect(self.onFontSizeChanged)
2012-12-24 00:02:00 +00:00
self.comboBoxAnkiModel.currentIndexChanged.connect(self.onAnkiModelChanged)
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.checkGeneralRecentLoad.setChecked(self.preferences.generalRecentLoad)
self.checkGeneralReadingsStrip.setChecked(self.preferences.generalReadingsStrip)
self.checkGeneralFindUpdates.setChecked(self.preferences.generalFindUpdates)
2011-08-28 18:01:32 +00:00
2011-10-08 16:32:51 +00:00
self.updateSampleText()
2011-08-28 18:01:32 +00:00
font = self.textContentSample.font()
self.comboContentFontFamily.setCurrentFont(font)
self.spinContentFontSize.setValue(font.pointSize())
2011-10-08 16:32:51 +00:00
self.spinSearchScanMax.setValue(self.preferences.searchScanMax)
self.spinSearchResultMax.setValue(self.preferences.searchResultMax)
self.checkSearchGroupByExp.setChecked(self.preferences.searchGroupByExp)
2011-08-28 18:01:32 +00:00
self.tabAnki.setEnabled(self.anki is not None)
2011-10-08 16:32:51 +00:00
if self.anki:
2012-12-24 02:11:14 +00:00
self.comboBoxAnkiDeck.addItems(self.anki.deckNames())
self.comboBoxAnkiDeck.setCurrentIndex(self.comboBoxAnkiDeck.findText(self.preferences.ankiDeck))
2012-12-24 00:02:00 +00:00
self.comboBoxAnkiModel.blockSignals(True)
self.comboBoxAnkiModel.addItems(self.anki.modelNames())
self.comboBoxAnkiModel.blockSignals(False)
self.comboBoxAnkiModel.setCurrentIndex(self.comboBoxAnkiModel.findText(self.preferences.ankiModel))
2011-08-28 18:01:32 +00:00
2011-10-08 16:32:51 +00:00
def dialogToData(self):
self.preferences.generalRecentLoad = self.checkGeneralRecentLoad.isChecked()
self.preferences.generalReadingsStrip = self.checkGeneralReadingsStrip.isChecked()
self.preferences.generalFindUpdates = self.checkGeneralFindUpdates.isChecked()
2011-08-28 18:01:32 +00:00
2011-10-08 16:32:51 +00:00
self.preferences.searchScanMax = self.spinSearchScanMax.value()
self.preferences.searchResultMax = self.spinSearchResultMax.value()
self.preferences.searchGroupByExp = self.checkSearchGroupByExp.isChecked()
2011-08-28 18:01:32 +00:00
2011-10-08 16:32:51 +00:00
if self.anki:
2012-12-24 02:11:14 +00:00
self.preferences.ankiDeck = unicode(self.comboBoxAnkiDeck.currentText())
2012-12-24 00:02:00 +00:00
self.preferences.ankiModel = unicode(self.comboBoxAnkiModel.currentText())
2011-10-08 16:32:51 +00:00
self.preferences.ankiFields = self.ankiFields()
2011-08-28 18:01:32 +00:00
2011-10-08 16:32:51 +00:00
def updateSampleText(self):
2011-08-28 18:01:32 +00:00
palette = self.textContentSample.palette()
2011-10-08 16:32:51 +00:00
palette.setColor(QtGui.QPalette.Base, QtGui.QColor(self.preferences.uiContentColorBg))
palette.setColor(QtGui.QPalette.Text, QtGui.QColor(self.preferences.uiContentColorFg))
2011-08-28 18:01:32 +00:00
self.textContentSample.setPalette(palette)
font = self.textContentSample.font()
2011-10-08 16:32:51 +00:00
font.setFamily(self.preferences.uiContentFontFamily)
font.setPointSize(self.preferences.uiContentFontSize)
2011-08-28 18:01:32 +00:00
self.textContentSample.setFont(font)
2011-10-08 16:32:51 +00:00
def setAnkiFields(self, fieldsAnki, fieldsPrefs):
2012-12-24 00:02:00 +00:00
if fieldsAnki is None:
fieldsAnki = list()
2011-08-28 18:01:32 +00:00
self.tableAnkiFields.setRowCount(len(fieldsAnki))
for i, name in enumerate(fieldsAnki):
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.tableAnkiFields.setItem(i, j, column)
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.tableAnkiFields.rowCount()):
itemName = unicode(self.tableAnkiFields.item(i, 0).text())
itemValue = unicode(self.tableAnkiFields.item(i, 1).text())
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.uiContentColorFg, self)
2011-08-28 18:01:32 +00:00
if ok:
2011-10-08 16:32:51 +00:00
self.preferences.uiContentColorFg = color
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.uiContentColorBg, self)
2011-08-28 18:01:32 +00:00
if ok:
2011-10-08 16:32:51 +00:00
self.preferences.uiContentColorBg = color
self.updateSampleText()
2011-08-28 18:01:32 +00:00
2011-10-08 16:32:51 +00:00
def onFontFamilyChanged(self, font):
self.preferences.uiContentFontFamily = font.family()
self.updateSampleText()
2011-08-28 18:01:32 +00:00
2011-10-08 16:32:51 +00:00
def onFontSizeChanged(self, size):
self.preferences.uiContentFontSize = size
self.updateSampleText()
2012-12-24 00:02:00 +00:00
def onAnkiModelChanged(self, index):
2012-12-25 02:53:41 +00:00
modelName = self.comboBoxAnkiModel.currentText()
fieldNames = self.anki.modelFieldNames(modelName) or list()
2012-12-24 01:50:49 +00:00
self.setAnkiFields(fieldNames, self.preferences.ankiFields)