linkchecker/linkcheck/gui/options.py

111 lines
4.1 KiB
Python
Raw Normal View History

# -*- coding: iso-8859-1 -*-
# Copyright (C) 2009-2014 Bastian Kleineidam
#
# 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 2 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.
#
2009-07-24 21:58:20 +00:00
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2010-11-05 00:13:37 +00:00
import os
2010-11-18 22:26:34 +00:00
from PyQt4 import QtGui
from .linkchecker_ui_options import Ui_Options
2010-11-05 00:13:37 +00:00
from .editor import EditorWindow
from ..fileutil import is_writable
from .. import configuration
2010-11-05 00:13:37 +00:00
class LinkCheckerOptions (QtGui.QDialog, Ui_Options):
"""Hold options for current URL to check."""
def __init__ (self, parent=None):
2011-02-17 18:59:02 +00:00
"""Reset all options and initialize the editor window."""
super(LinkCheckerOptions, self).__init__(parent)
self.setupUi(self)
2010-11-05 00:13:37 +00:00
self.editor = EditorWindow(self)
2010-11-18 22:26:34 +00:00
self.closeButton.clicked.connect(self.close)
self.user_config_button.clicked.connect(self.edit_user_config)
self.reset()
def reset (self):
2010-11-05 00:13:37 +00:00
"""Reset GUI and config options."""
self.user_config = configuration.get_user_config()
self.reset_gui_options()
2010-11-05 00:13:37 +00:00
self.reset_config_options()
def reset_gui_options (self):
"""Reset GUI options to default values."""
self.recursionlevel.setValue(-1)
self.verbose.setChecked(False)
2010-10-16 17:26:46 +00:00
self.debug.setChecked(False)
self.warninglines.setPlainText(u"")
self.ignorelines.setPlainText(u"")
2010-11-05 00:13:37 +00:00
def reset_config_options (self):
"""Reset configuration file edit buttons."""
2012-01-26 19:23:15 +00:00
self.user_config_writable = is_writable(self.user_config)
2010-11-05 00:13:37 +00:00
set_edit_button(self.user_config, self.user_config_button,
self.user_config_filename, self.user_config_writable)
2010-11-05 00:13:37 +00:00
def edit_user_config (self):
2011-02-17 18:59:02 +00:00
"""Show editor for user specific configuration file."""
2010-11-05 00:13:37 +00:00
return start_editor(self.user_config, self.user_config_writable,
self.editor)
2010-11-17 20:25:13 +00:00
def get_options (self):
"""Return option data as dictionary."""
return dict(
debug=self.debug.isChecked(),
verbose=self.verbose.isChecked(),
recursionlevel=self.recursionlevel.value(),
warninglines=unicode(self.warninglines.toPlainText()),
ignorelines=unicode(self.ignorelines.toPlainText()),
2010-11-17 20:25:13 +00:00
)
def set_options (self, data):
2011-02-17 18:59:02 +00:00
"""Set GUI options from given data."""
2012-01-26 19:23:15 +00:00
if data.get("debug") is not None:
2010-11-17 20:25:13 +00:00
self.debug.setChecked(data["debug"])
2012-01-26 19:23:15 +00:00
if data.get("verbose") is not None:
2010-11-17 20:25:13 +00:00
self.verbose.setChecked(data["verbose"])
2012-01-26 19:23:15 +00:00
if data.get("recursionlevel") is not None:
2010-11-17 20:25:13 +00:00
self.recursionlevel.setValue(data["recursionlevel"])
2012-01-26 19:23:15 +00:00
if data.get("warninglines") is not None:
self.warninglines.setPlainText(data["warninglines"])
2012-01-26 19:23:15 +00:00
if data.get("ignorelines") is not None:
self.ignorelines.setPlainText(data["ignorelines"])
2010-11-17 20:25:13 +00:00
2010-11-05 00:13:37 +00:00
def start_editor (filename, writable, editor):
2011-02-17 18:59:02 +00:00
"""Start editor for given filename."""
2010-11-05 00:13:37 +00:00
if not os.path.isfile(filename):
# file vanished
return
editor.load(filename)
2011-02-17 18:59:02 +00:00
# all config files are in INI format
2010-11-05 00:13:37 +00:00
editor.setContentType("text/plain+ini")
editor.editor.setReadOnly(not writable)
editor.show()
def set_edit_button (filename, button, label, writable):
2011-02-17 18:59:02 +00:00
"""Update edit button depending on writable flag of file."""
label.setText(filename)
2010-11-05 00:13:37 +00:00
if os.path.isfile(filename):
button.setEnabled(True)
if writable:
button.setText(_(u"Edit"))
else:
button.setText(_(u"Read"))
else:
button.setEnabled(False)
button.setText(_(u"File not found"))