Style fixes, fixed pyside/pyqt signal difference, added change language/disable.

This commit is contained in:
Ricky 2016-08-07 04:06:21 -07:00
parent 145f3aa134
commit 4202c2ba42
2 changed files with 67 additions and 36 deletions

View File

@ -43,7 +43,7 @@ class InputLineEdit(InputLineSpell):
def keyPressEvent(self, event): def keyPressEvent(self, event):
key = event.key() key = event.key()
modifiers = event.modifiers() modifiers = event.modifiers()
bar = self.scroll_widget.verticalScrollBar() scroll = self.scroll_widget.verticalScrollBar()
newline = (key == QtCore.Qt.Key_Enter or key == QtCore.Qt.Key_Return) newline = (key == QtCore.Qt.Key_Enter or key == QtCore.Qt.Key_Return)
if modifiers == QtCore.Qt.ControlModifier: if modifiers == QtCore.Qt.ControlModifier:
if key == QtCore.Qt.Key_PageUp: if key == QtCore.Qt.Key_PageUp:
@ -58,19 +58,19 @@ class InputLineEdit(InputLineSpell):
elif key in (QtCore.Qt.Key_Right, QtCore.Qt.Key_Down): elif key in (QtCore.Qt.Key_Right, QtCore.Qt.Key_Down):
self.bufferSwitchNext.emit() self.bufferSwitchNext.emit()
elif key == QtCore.Qt.Key_PageUp: elif key == QtCore.Qt.Key_PageUp:
bar.setValue(bar.value() - (bar.pageStep() / 10)) scroll.setValue(scroll.value() - (scroll.pageStep() / 10))
elif key == QtCore.Qt.Key_PageDown: elif key == QtCore.Qt.Key_PageDown:
bar.setValue(bar.value() + (bar.pageStep() / 10)) scroll.setValue(scroll.value() + (scroll.pageStep() / 10))
elif key == QtCore.Qt.Key_Home: elif key == QtCore.Qt.Key_Home:
bar.setValue(bar.minimum()) scroll.setValue(scroll.minimum())
elif key == QtCore.Qt.Key_End: elif key == QtCore.Qt.Key_End:
bar.setValue(bar.maximum()) scroll.setValue(scroll.maximum())
else: else:
InputLineSpell.keyPressEvent(self, event) InputLineSpell.keyPressEvent(self, event)
elif key == QtCore.Qt.Key_PageUp: elif key == QtCore.Qt.Key_PageUp:
bar.setValue(bar.value() - bar.pageStep()) scroll.setValue(scroll.value() - scroll.pageStep())
elif key == QtCore.Qt.Key_PageDown: elif key == QtCore.Qt.Key_PageDown:
bar.setValue(bar.value() + bar.pageStep()) scroll.setValue(scroll.value() + scroll.pageStep())
elif key == QtCore.Qt.Key_Up or key == QtCore.Qt.Key_Down: elif key == QtCore.Qt.Key_Up or key == QtCore.Qt.Key_Down:
# Compare position, optionally only nativate history if no change: # Compare position, optionally only nativate history if no change:
pos1 = self.textCursor().position() pos1 = self.textCursor().position()
@ -85,7 +85,7 @@ class InputLineEdit(InputLineSpell):
self._history_navigate(-1) self._history_navigate(-1)
elif key == QtCore.Qt.Key_Down: elif key == QtCore.Qt.Key_Down:
self._history_navigate(1) self._history_navigate(1)
elif (newline and modifiers != QtCore.Qt.ShiftModifier): elif newline and modifiers != QtCore.Qt.ShiftModifier:
self._input_return_pressed() self._input_return_pressed()
else: else:
InputLineSpell.keyPressEvent(self, event) InputLineSpell.keyPressEvent(self, event)
@ -108,6 +108,6 @@ class InputLineEdit(InputLineSpell):
return return
self.setText(self._history[self._history_index]) self.setText(self._history[self._history_index])
# End of line: # End of line:
textCursor = self.textCursor() text_cursor = self.textCursor()
textCursor.setPosition(len(self._history[self._history_index])) text_cursor.setPosition(len(self._history[self._history_index]))
self.setTextCursor(textCursor) self.setTextCursor(text_cursor)

View File

@ -22,13 +22,10 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with QWeeChat. If not, see <http://www.gnu.org/licenses/>. # along with QWeeChat. If not, see <http://www.gnu.org/licenses/>.
# #
import qt_compat
QtCore = qt_compat.import_module('QtCore')
QtGui = qt_compat.import_module('QtGui')
import config
import functools import functools
import re import re
import config
import qt_compat
import weechat.color as color import weechat.color as color
# Spell checker support # Spell checker support
@ -36,6 +33,8 @@ try:
import enchant import enchant
except ImportError: except ImportError:
enchant = None enchant = None
QtCore = qt_compat.import_module('QtCore')
QtGui = qt_compat.import_module('QtGui')
class InputLineSpell(QtGui.QTextEdit): class InputLineSpell(QtGui.QTextEdit):
@ -72,8 +71,8 @@ class InputLineSpell(QtGui.QTextEdit):
self._color = color.Color(config.color_options(), self.debug) self._color = color.Color(config.color_options(), self.debug)
self.initDict() self.initDict()
# Set height to one line: # Set height to one line:
fm = QtGui.QFontMetrics(self.currentFont()) font_metric = QtGui.QFontMetrics(self.currentFont())
self.setMinimumHeight(fm.height() + 8) self.setMinimumHeight(font_metric.height() + 8)
size_policy = self.sizePolicy() size_policy = self.sizePolicy()
size_policy.setHeightForWidth(True) size_policy.setHeightForWidth(True)
size_policy.setVerticalPolicy(QtGui.QSizePolicy.Preferred) size_policy.setVerticalPolicy(QtGui.QSizePolicy.Preferred)
@ -82,7 +81,8 @@ class InputLineSpell(QtGui.QTextEdit):
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.textChanged.connect(lambda: self.updateGeometry()) self.textChanged.connect(lambda: self.updateGeometry())
def hasHeightForWidth(self): @staticmethod
def hasHeightForWidth():
return True return True
def heightForWidth(self, width): def heightForWidth(self, width):
@ -105,8 +105,8 @@ class InputLineSpell(QtGui.QTextEdit):
self.heightForWidth(original_hint.width())) self.heightForWidth(original_hint.width()))
def scroll_bottom(self): def scroll_bottom(self):
bar = self.verticalScrollBar() scroll = self.verticalScrollBar()
bar.setValue(bar.maximum()) scroll.setValue(scroll.maximum())
def initDict(self, lang=None): def initDict(self, lang=None):
if enchant: if enchant:
@ -125,6 +125,12 @@ class InputLineSpell(QtGui.QTextEdit):
self.highlighter.setDict(self.spelldict) self.highlighter.setDict(self.spelldict)
self.highlighter.rehighlight() self.highlighter.rehighlight()
def toggleDict(self, label=None):
if self.spelldict:
self.killDict()
else:
self.initDict()
def killDict(self): def killDict(self):
self.highlighter.setDocument(None) self.highlighter.setDocument(None)
self.spelldict = None self.spelldict = None
@ -164,22 +170,47 @@ class InputLineSpell(QtGui.QTextEdit):
if len(suggestions) != 0: if len(suggestions) != 0:
popup_menu.insertSeparator(popup_menu.actions()[0]) popup_menu.insertSeparator(popup_menu.actions()[0])
topAction = popup_menu.actions()[0] top_action = popup_menu.actions()[0]
for suggestion in suggestions: for suggest in suggestions:
action = QtGui.QAction(suggestion, popup_menu) self._menu_action(suggest, popup_menu, self.correctWord, after=top_action)
action.connect(action, QtCore.SIGNAL("triggered()"), functools.partial(self.correctWord, word = suggestion)) popup_menu.insertSeparator(top_action)
popup_menu.insertAction(topAction, action) add_action = QtGui.QAction("Add to dictionary", self)
popup_menu.insertSeparator(topAction) add_action.triggered.connect(lambda: self.addWord(text))
addAction = QtGui.QAction("Add to dictionary", self) popup_menu.insertAction(top_action, add_action)
addAction.triggered.connect(lambda: self.addWord(text)) # FIXME: disable spellcheck option
popup_menu.insertAction(topAction, addAction) spell_menu = QtGui.QMenu(popup_menu)
# FIXME: add change dict and disable spellcheck options spell_menu.setTitle('Spellcheck')
# spellmenu = QtGui.QMenu('Spell-checker options') popup_menu.insertMenu(top_action, spell_menu)
# for lang in enchant.list_languages(): for lang in enchant.list_languages():
# popup_menu.insertMenu(topAction, spellmenu) self._menu_action(lang, spell_menu, self.initDict,
checked=(lang == self.spelldict.tag))
toggle = self._menu_action('Check the spelling', spell_menu,
self.toggleDict,
checked=(self.spelldict != False))
spell_menu.insertSeparator(toggle)
elif enchant:
toggle = self._menu_action('Check the spelling', popup_menu,
self.toggleDict, checked=False)
popup_menu.insertSeparator(toggle)
popup_menu.exec_(event.globalPos()) popup_menu.exec_(event.globalPos())
@staticmethod
def _menu_action(text, menu, method, after=None, checked=None):
action = QtGui.QAction(text, menu)
action.connect(
action,
QtCore.SIGNAL("triggered()"),
functools.partial(method, text)
)
if checked is not None:
action.setCheckable(True)
action.setChecked(checked)
if after is not None:
menu.insertAction(after, action)
else:
menu.addAction(action)
return action
def addWord(self, word): def addWord(self, word):
self.spelldict.add(word) self.spelldict.add(word)
self.highlighter.rehighlight() self.highlighter.rehighlight()
@ -199,7 +230,7 @@ class InputLineSpell(QtGui.QTextEdit):
class SpellHighlighter(QtGui.QSyntaxHighlighter): class SpellHighlighter(QtGui.QSyntaxHighlighter):
WORDS = u'(?iu)[\w\']+' WORDS = r'(?iu)[\w\']+'
def __init__(self, *args): def __init__(self, *args):
QtGui.QSyntaxHighlighter.__init__(self, *args) QtGui.QSyntaxHighlighter.__init__(self, *args)