1
yomichan-anki/yomi_base/preferences.py

150 lines
5.6 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/>.
2011-11-05 16:45:41 +00:00
from PyQt4 import QtGui, QtCore, uic
from util import buildResPath
2011-08-28 18:01:32 +00:00
2011-11-05 16:45:41 +00:00
class DialogPreferences(QtGui.QDialog):
2011-08-28 18:01:32 +00:00
def __init__(self, parent, preferences, anki):
QtGui.QDialog.__init__(self, parent)
2011-11-19 16:34:27 +00:00
uic.loadUi(buildResPath('ui/preferences.ui'), 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)
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
2011-10-08 16:32:51 +00:00
self.checkAnkiShowIcon.setChecked(self.preferences.ankiShowIcon)
self.tabAnki.setEnabled(bool(self.anki))
if self.anki:
self.setAnkiFields(self.anki.fields(), self.preferences.ankiFields)
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
self.preferences.ankiShowIcon = self.checkAnkiShowIcon.isChecked()
if self.anki:
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):
2011-08-28 18:01:32 +00:00
self.tableAnkiFields.setRowCount(len(fieldsAnki))
for i, (name, required, unique) in enumerate(fieldsAnki):
columns = list()
itemName = QtGui.QTableWidgetItem(name)
itemName.setFlags(QtCore.Qt.ItemIsSelectable)
columns.append(itemName)
itemValue = QtGui.QTableWidgetItem(fieldsPrefs.get(name, unicode()))
columns.append(itemValue)
itemRequired = QtGui.QTableWidgetItem()
itemRequired.setFlags(QtCore.Qt.ItemIsUserCheckable)
itemRequired.setCheckState(QtCore.Qt.Checked if required else QtCore.Qt.Unchecked)
columns.append(itemRequired)
itemUnique = QtGui.QTableWidgetItem()
itemUnique.setFlags(QtCore.Qt.ItemIsUserCheckable)
itemUnique.setCheckState(QtCore.Qt.Checked if unique else QtCore.Qt.Unchecked)
columns.append(itemUnique)
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()