2010-09-04 08:12:54 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# --------------------------------------------------------------------------- #
|
2010-12-26 11:04:47 +00:00
|
|
|
# Copyright (c) 2008-2011 Raoul Snyman #
|
2011-05-26 16:25:54 +00:00
|
|
|
# Portions copyright (c) 2008-2011 Tim Bentley, Gerald Britton, Jonathan #
|
|
|
|
# Corwin, Michael Gorven, Scott Guerrieri, Matthias Hub, Meinert Jordan, #
|
2011-05-26 17:11:22 +00:00
|
|
|
# Armin Köhler, Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias #
|
2011-06-12 16:02:52 +00:00
|
|
|
# Põldaru, Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith, #
|
2011-06-12 15:41:01 +00:00
|
|
|
# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Frode Woldsund #
|
2010-09-04 08:12:54 +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; version 2 of the License. #
|
|
|
|
# #
|
|
|
|
# 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, write to the Free Software Foundation, Inc., 59 #
|
|
|
|
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
|
|
|
|
###############################################################################
|
2011-04-07 17:34:49 +00:00
|
|
|
import logging
|
2010-09-04 08:12:54 +00:00
|
|
|
import re
|
|
|
|
try:
|
|
|
|
import enchant
|
2010-09-20 16:33:38 +00:00
|
|
|
from enchant import DictNotFoundError
|
2011-06-23 05:26:20 +00:00
|
|
|
from enchant.errors import Error
|
2011-01-18 04:32:24 +00:00
|
|
|
ENCHANT_AVAILABLE = True
|
2010-09-04 08:12:54 +00:00
|
|
|
except ImportError:
|
2011-01-18 04:32:24 +00:00
|
|
|
ENCHANT_AVAILABLE = False
|
2010-09-04 08:12:54 +00:00
|
|
|
|
|
|
|
# based on code from
|
2010-12-02 14:37:38 +00:00
|
|
|
# http://john.nachtimwald.com/2009/08/22/qplaintextedit-with-in-line-spell-check
|
2010-09-04 08:12:54 +00:00
|
|
|
|
|
|
|
from PyQt4 import QtCore, QtGui
|
2011-04-14 13:53:02 +00:00
|
|
|
|
2010-12-23 17:42:28 +00:00
|
|
|
from openlp.core.lib import translate, DisplayTags
|
2011-04-14 13:53:02 +00:00
|
|
|
from openlp.core.lib.ui import checkable_action
|
2010-09-04 08:12:54 +00:00
|
|
|
|
2011-04-07 17:34:49 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2010-09-04 08:12:54 +00:00
|
|
|
class SpellTextEdit(QtGui.QPlainTextEdit):
|
2010-12-02 14:37:38 +00:00
|
|
|
"""
|
|
|
|
Spell checking widget based on QPlanTextEdit.
|
|
|
|
"""
|
2010-09-04 08:12:54 +00:00
|
|
|
def __init__(self, *args):
|
2011-04-07 17:54:17 +00:00
|
|
|
global ENCHANT_AVAILABLE
|
2010-09-04 08:12:54 +00:00
|
|
|
QtGui.QPlainTextEdit.__init__(self, *args)
|
|
|
|
# Default dictionary based on the current locale.
|
2011-01-18 04:32:24 +00:00
|
|
|
if ENCHANT_AVAILABLE:
|
2010-09-20 05:06:18 +00:00
|
|
|
try:
|
2011-01-26 17:13:26 +00:00
|
|
|
self.dictionary = enchant.Dict()
|
2011-04-07 17:34:49 +00:00
|
|
|
self.highlighter = Highlighter(self.document())
|
|
|
|
self.highlighter.spellingDictionary = self.dictionary
|
2011-06-23 05:26:20 +00:00
|
|
|
except Error, DictNotFoundError:
|
2011-04-07 17:54:17 +00:00
|
|
|
ENCHANT_AVAILABLE = False
|
2011-04-07 17:34:49 +00:00
|
|
|
log.debug(u'Could not load default dictionary')
|
2010-09-04 08:12:54 +00:00
|
|
|
|
|
|
|
def mousePressEvent(self, event):
|
2011-01-26 17:13:26 +00:00
|
|
|
"""
|
|
|
|
Handle mouse clicks within the text edit region.
|
|
|
|
"""
|
2010-09-04 08:12:54 +00:00
|
|
|
if event.button() == QtCore.Qt.RightButton:
|
|
|
|
# Rewrite the mouse event to a left button event so the cursor is
|
|
|
|
# moved to the location of the pointer.
|
|
|
|
event = QtGui.QMouseEvent(QtCore.QEvent.MouseButtonPress,
|
|
|
|
event.pos(), QtCore.Qt.LeftButton, QtCore.Qt.LeftButton,
|
|
|
|
QtCore.Qt.NoModifier)
|
|
|
|
QtGui.QPlainTextEdit.mousePressEvent(self, event)
|
|
|
|
|
|
|
|
def contextMenuEvent(self, event):
|
2011-01-26 17:13:26 +00:00
|
|
|
"""
|
|
|
|
Provide the context menu for the text edit region.
|
|
|
|
"""
|
2010-12-02 14:37:38 +00:00
|
|
|
popupMenu = self.createStandardContextMenu()
|
2010-09-04 08:12:54 +00:00
|
|
|
# Select the word under the cursor.
|
|
|
|
cursor = self.textCursor()
|
2010-09-04 10:39:48 +00:00
|
|
|
# only select text if not already selected
|
|
|
|
if not cursor.hasSelection():
|
|
|
|
cursor.select(QtGui.QTextCursor.WordUnderCursor)
|
2010-09-04 08:12:54 +00:00
|
|
|
self.setTextCursor(cursor)
|
2011-04-14 13:53:02 +00:00
|
|
|
# Add menu with available languages.
|
|
|
|
if ENCHANT_AVAILABLE:
|
|
|
|
lang_menu = QtGui.QMenu(
|
|
|
|
translate('OpenLP.SpellTextEdit', 'Language:'))
|
|
|
|
for lang in enchant.list_languages():
|
|
|
|
action = checkable_action(
|
|
|
|
lang_menu, lang, lang == self.dictionary.tag)
|
|
|
|
action.setText(lang)
|
|
|
|
lang_menu.addAction(action)
|
|
|
|
popupMenu.insertSeparator(popupMenu.actions()[0])
|
|
|
|
popupMenu.insertMenu(popupMenu.actions()[0], lang_menu)
|
|
|
|
QtCore.QObject.connect(lang_menu,
|
|
|
|
QtCore.SIGNAL(u'triggered(QAction*)'), self.setLanguage)
|
2010-09-04 08:12:54 +00:00
|
|
|
# Check if the selected word is misspelled and offer spelling
|
|
|
|
# suggestions if it is.
|
2011-04-07 17:54:17 +00:00
|
|
|
if ENCHANT_AVAILABLE and self.textCursor().hasSelection():
|
2010-09-04 08:12:54 +00:00
|
|
|
text = unicode(self.textCursor().selectedText())
|
2011-01-26 17:13:26 +00:00
|
|
|
if not self.dictionary.check(text):
|
2010-09-04 08:12:54 +00:00
|
|
|
spell_menu = QtGui.QMenu(translate('OpenLP.SpellTextEdit',
|
|
|
|
'Spelling Suggestions'))
|
2011-01-26 17:13:26 +00:00
|
|
|
for word in self.dictionary.suggest(text):
|
2010-09-04 08:12:54 +00:00
|
|
|
action = SpellAction(word, spell_menu)
|
|
|
|
action.correct.connect(self.correctWord)
|
|
|
|
spell_menu.addAction(action)
|
|
|
|
# Only add the spelling suggests to the menu if there are
|
|
|
|
# suggestions.
|
2011-04-14 13:53:02 +00:00
|
|
|
if len(spell_menu.actions()):
|
2010-12-02 14:37:38 +00:00
|
|
|
popupMenu.insertMenu(popupMenu.actions()[0], spell_menu)
|
|
|
|
tagMenu = QtGui.QMenu(translate('OpenLP.SpellTextEdit',
|
2010-09-04 08:12:54 +00:00
|
|
|
'Formatting Tags'))
|
2011-01-09 08:17:17 +00:00
|
|
|
for html in DisplayTags.get_html_tags():
|
2011-04-14 13:53:02 +00:00
|
|
|
action = SpellAction(html[u'desc'], tagMenu)
|
2010-09-04 08:12:54 +00:00
|
|
|
action.correct.connect(self.htmlTag)
|
2010-12-02 14:37:38 +00:00
|
|
|
tagMenu.addAction(action)
|
|
|
|
popupMenu.insertSeparator(popupMenu.actions()[0])
|
|
|
|
popupMenu.insertMenu(popupMenu.actions()[0], tagMenu)
|
|
|
|
popupMenu.exec_(event.globalPos())
|
2010-09-04 08:12:54 +00:00
|
|
|
|
2011-04-14 13:53:02 +00:00
|
|
|
def setLanguage(self, action):
|
|
|
|
"""
|
|
|
|
Changes the language for this spelltextedit.
|
|
|
|
|
|
|
|
``action``
|
|
|
|
The action.
|
|
|
|
"""
|
|
|
|
self.dictionary = enchant.Dict(action.text())
|
|
|
|
self.highlighter.spellingDictionary = self.dictionary
|
|
|
|
self.highlighter.highlightBlock(self.toPlainText())
|
|
|
|
self.highlighter.rehighlight()
|
|
|
|
|
2010-09-04 08:12:54 +00:00
|
|
|
def correctWord(self, word):
|
|
|
|
"""
|
|
|
|
Replaces the selected text with word.
|
|
|
|
"""
|
|
|
|
cursor = self.textCursor()
|
|
|
|
cursor.beginEditBlock()
|
|
|
|
cursor.removeSelectedText()
|
|
|
|
cursor.insertText(word)
|
|
|
|
cursor.endEditBlock()
|
|
|
|
|
|
|
|
def htmlTag(self, tag):
|
|
|
|
"""
|
|
|
|
Replaces the selected text with word.
|
|
|
|
"""
|
2011-01-09 08:17:17 +00:00
|
|
|
for html in DisplayTags.get_html_tags():
|
2010-09-04 08:12:54 +00:00
|
|
|
if tag == html[u'desc']:
|
|
|
|
cursor = self.textCursor()
|
|
|
|
if self.textCursor().hasSelection():
|
|
|
|
text = cursor.selectedText()
|
|
|
|
cursor.beginEditBlock()
|
|
|
|
cursor.removeSelectedText()
|
|
|
|
cursor.insertText(html[u'start tag'])
|
|
|
|
cursor.insertText(text)
|
|
|
|
cursor.insertText(html[u'end tag'])
|
|
|
|
cursor.endEditBlock()
|
|
|
|
else:
|
|
|
|
cursor = self.textCursor()
|
|
|
|
cursor.insertText(html[u'start tag'])
|
|
|
|
cursor.insertText(html[u'end tag'])
|
|
|
|
|
|
|
|
|
2011-01-26 17:13:26 +00:00
|
|
|
class Highlighter(QtGui.QSyntaxHighlighter):
|
|
|
|
"""
|
|
|
|
Provides a text highlighter for pointing out spelling errors in text.
|
|
|
|
"""
|
2010-09-04 08:12:54 +00:00
|
|
|
WORDS = u'(?iu)[\w\']+'
|
|
|
|
|
|
|
|
def __init__(self, *args):
|
|
|
|
QtGui.QSyntaxHighlighter.__init__(self, *args)
|
2011-01-26 17:13:26 +00:00
|
|
|
self.spellingDictionary = None
|
2010-09-04 08:12:54 +00:00
|
|
|
|
|
|
|
def highlightBlock(self, text):
|
2011-01-26 17:13:26 +00:00
|
|
|
"""
|
|
|
|
Highlight misspelt words in a block of text
|
|
|
|
"""
|
|
|
|
if not self.spellingDictionary:
|
2010-09-04 08:12:54 +00:00
|
|
|
return
|
|
|
|
text = unicode(text)
|
2011-01-26 17:13:26 +00:00
|
|
|
charFormat = QtGui.QTextCharFormat()
|
|
|
|
charFormat.setUnderlineColor(QtCore.Qt.red)
|
|
|
|
charFormat.setUnderlineStyle(QtGui.QTextCharFormat.SpellCheckUnderline)
|
2010-09-04 08:12:54 +00:00
|
|
|
for word_object in re.finditer(self.WORDS, text):
|
2011-01-26 17:13:26 +00:00
|
|
|
if not self.spellingDictionary.check(word_object.group()):
|
2010-09-04 08:12:54 +00:00
|
|
|
self.setFormat(word_object.start(),
|
2011-01-26 17:13:26 +00:00
|
|
|
word_object.end() - word_object.start(), charFormat)
|
|
|
|
|
2010-09-04 08:12:54 +00:00
|
|
|
|
|
|
|
class SpellAction(QtGui.QAction):
|
|
|
|
"""
|
|
|
|
A special QAction that returns the text in a signal.
|
|
|
|
"""
|
|
|
|
correct = QtCore.pyqtSignal(unicode)
|
|
|
|
|
|
|
|
def __init__(self, *args):
|
|
|
|
QtGui.QAction.__init__(self, *args)
|
|
|
|
self.triggered.connect(lambda x: self.correct.emit(
|
2011-01-09 08:17:17 +00:00
|
|
|
unicode(self.text())))
|