From 04cfa6a730978ae23a2744ca265a32fbc1270367 Mon Sep 17 00:00:00 2001 From: Tim Bentley Date: Sat, 4 Sep 2010 09:12:54 +0100 Subject: [PATCH] Move spell and tag dialog to it"s own file --- openlp/core/lib/__init__.py | 1 + openlp/core/lib/spelltextedit.py | 154 ++++++++++++++++++ openlp/core/ui/__init__.py | 135 --------------- .../plugins/custom/forms/editcustomdialog.py | 3 +- openlp/plugins/songs/forms/editversedialog.py | 3 +- 5 files changed, 157 insertions(+), 139 deletions(-) create mode 100644 openlp/core/lib/spelltextedit.py diff --git a/openlp/core/lib/__init__.py b/openlp/core/lib/__init__.py index 721b6ae23..c9858901e 100644 --- a/openlp/core/lib/__init__.py +++ b/openlp/core/lib/__init__.py @@ -316,6 +316,7 @@ def expand_tags(text): text = text.replace(tag[u'end tag'], tag[u'end html']) return text +from spelltextedit import SpellTextEdit from eventreceiver import Receiver from settingsmanager import SettingsManager from plugin import PluginStatus, Plugin diff --git a/openlp/core/lib/spelltextedit.py b/openlp/core/lib/spelltextedit.py new file mode 100644 index 000000000..54279d8fd --- /dev/null +++ b/openlp/core/lib/spelltextedit.py @@ -0,0 +1,154 @@ +# -*- coding: utf-8 -*- +# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4 + +############################################################################### +# OpenLP - Open Source Lyrics Projection # +# --------------------------------------------------------------------------- # +# Copyright (c) 2008-2010 Raoul Snyman # +# Portions copyright (c) 2008-2010 Tim Bentley, Jonathan Corwin, Michael # +# Gorven, Scott Guerrieri, Meinert Jordan, Andreas Preikschat, Christian # +# Richter, Philip Ridout, Maikel Stuivenberg, Martin Thompson, Jon Tibble, # +# Carsten Tinggaard, Frode Woldsund # +# --------------------------------------------------------------------------- # +# 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 # +############################################################################### + +import re +import sys +try: + import enchant + enchant_available = True +except ImportError: + enchant_available = False + +# based on code from +# http://john.nachtimwald.com/2009/08/22/qplaintextedit-with-in-line-spell-check/ + +from PyQt4 import QtCore, QtGui +from openlp.core.lib import html_expands, translate, context_menu_action + +class SpellTextEdit(QtGui.QPlainTextEdit): + def __init__(self, *args): + QtGui.QPlainTextEdit.__init__(self, *args) + # Default dictionary based on the current locale. + if enchant_available: + self.dict = enchant.Dict() + self.highlighter = Highlighter(self.document()) + self.highlighter.setDict(self.dict) + + def mousePressEvent(self, event): + 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): + popup_menu = self.createStandardContextMenu() + # Select the word under the cursor. + cursor = self.textCursor() + cursor.select(QtGui.QTextCursor.WordUnderCursor) + self.setTextCursor(cursor) + # Check if the selected word is misspelled and offer spelling + # suggestions if it is. + if enchant_available and self.textCursor().hasSelection(): + text = unicode(self.textCursor().selectedText()) + if not self.dict.check(text): + spell_menu = QtGui.QMenu(translate('OpenLP.SpellTextEdit', + 'Spelling Suggestions')) + for word in self.dict.suggest(text): + 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. + if len(spell_menu.actions()) != 0: + popup_menu.insertSeparator(popup_menu.actions()[0]) + popup_menu.insertMenu(popup_menu.actions()[0], spell_menu) + tag_menu = QtGui.QMenu(translate('OpenLP.SpellTextEdit', + 'Formatting Tags')) + for html in html_expands: + action = SpellAction( html[u'desc'], tag_menu) + action.correct.connect(self.htmlTag) + tag_menu.addAction(action) + popup_menu.insertSeparator(popup_menu.actions()[0]) + popup_menu.insertMenu(popup_menu.actions()[0], tag_menu) + + popup_menu.exec_(event.globalPos()) + + 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. + """ + for html in html_expands: + 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']) + +class Highlighter(QtGui.QSyntaxHighlighter): + + WORDS = u'(?iu)[\w\']+' + + def __init__(self, *args): + QtGui.QSyntaxHighlighter.__init__(self, *args) + self.dict = None + + def setDict(self, dict): + self.dict = dict + + def highlightBlock(self, text): + if not self.dict: + return + text = unicode(text) + format = QtGui.QTextCharFormat() + format.setUnderlineColor(QtCore.Qt.red) + format.setUnderlineStyle(QtGui.QTextCharFormat.SpellCheckUnderline) + for word_object in re.finditer(self.WORDS, text): + if not self.dict.check(word_object.group()): + self.setFormat(word_object.start(), + word_object.end() - word_object.start(), format) + +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( + unicode(self.text()))) diff --git a/openlp/core/ui/__init__.py b/openlp/core/ui/__init__.py index dad79cb7b..33ba25046 100644 --- a/openlp/core/ui/__init__.py +++ b/openlp/core/ui/__init__.py @@ -27,141 +27,6 @@ The :mod:`ui` module provides the core user interface for OpenLP """ -# http://john.nachtimwald.com/2009/08/22/qplaintextedit-with-in-line-spell-check/ - -import re -import sys -try: - import enchant - enchant_available = True -except ImportError: - enchant_available = False - -from PyQt4 import QtCore, QtGui -from openlp.core.lib import html_expands, translate, context_menu_action - -class SpellTextEdit(QtGui.QPlainTextEdit): - def __init__(self, *args): - QtGui.QPlainTextEdit.__init__(self, *args) - # Default dictionary based on the current locale. - if enchant_available: - self.dict = enchant.Dict() - self.highlighter = Highlighter(self.document()) - self.highlighter.setDict(self.dict) - - def mousePressEvent(self, event): - 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): - popup_menu = self.createStandardContextMenu() - # Select the word under the cursor. - cursor = self.textCursor() - cursor.select(QtGui.QTextCursor.WordUnderCursor) - self.setTextCursor(cursor) - # Check if the selected word is misspelled and offer spelling - # suggestions if it is. - if enchant_available and self.textCursor().hasSelection(): - text = unicode(self.textCursor().selectedText()) - if not self.dict.check(text): - spell_menu = QtGui.QMenu(translate('OpenLP.SpellTextEdit', - 'Spelling Suggestions')) - for word in self.dict.suggest(text): - 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. - if len(spell_menu.actions()) != 0: - popup_menu.insertSeparator(popup_menu.actions()[0]) - popup_menu.insertMenu(popup_menu.actions()[0], spell_menu) - tag_menu = QtGui.QMenu(translate('OpenLP.SpellTextEdit', - 'Formatting Tags')) - for html in html_expands: - action = SpellAction( html[u'desc'], tag_menu) - action.correct.connect(self.htmlTag) - tag_menu.addAction(action) - popup_menu.insertSeparator(popup_menu.actions()[0]) - popup_menu.insertMenu(popup_menu.actions()[0], tag_menu) - - popup_menu.exec_(event.globalPos()) - - 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. - """ - for html in html_expands: - 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']) - -class Highlighter(QtGui.QSyntaxHighlighter): - - WORDS = u'(?iu)[\w\']+' - - def __init__(self, *args): - QtGui.QSyntaxHighlighter.__init__(self, *args) - - self.dict = None - - def setDict(self, dict): - self.dict = dict - - def highlightBlock(self, text): - if not self.dict: - return - - text = unicode(text) - - format = QtGui.QTextCharFormat() - format.setUnderlineColor(QtCore.Qt.red) - format.setUnderlineStyle(QtGui.QTextCharFormat.SpellCheckUnderline) - - for word_object in re.finditer(self.WORDS, text): - if not self.dict.check(word_object.group()): - self.setFormat(word_object.start(), - word_object.end() - word_object.start(), format) - -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( - unicode(self.text()))) - class HideMode(object): """ This is basically an enumeration class which specifies the mode of a Bible. diff --git a/openlp/plugins/custom/forms/editcustomdialog.py b/openlp/plugins/custom/forms/editcustomdialog.py index d8557b5e2..84a310cb9 100644 --- a/openlp/plugins/custom/forms/editcustomdialog.py +++ b/openlp/plugins/custom/forms/editcustomdialog.py @@ -26,8 +26,7 @@ from PyQt4 import QtCore, QtGui -from openlp.core.lib import build_icon, translate -from openlp.core.ui import SpellTextEdit +from openlp.core.lib import build_icon, translate, SpellTextEdit class Ui_CustomEditDialog(object): def setupUi(self, customEditDialog): diff --git a/openlp/plugins/songs/forms/editversedialog.py b/openlp/plugins/songs/forms/editversedialog.py index 64d56322f..43dc8b96d 100644 --- a/openlp/plugins/songs/forms/editversedialog.py +++ b/openlp/plugins/songs/forms/editversedialog.py @@ -26,8 +26,7 @@ from PyQt4 import QtCore, QtGui -from openlp.core.lib import build_icon, translate -from openlp.core.ui import SpellTextEdit +from openlp.core.lib import build_icon, translate, SpellTextEdit from openlp.plugins.songs.lib import VerseType class Ui_EditVerseDialog(object):