コード例 #1
0
ファイル: spelling.py プロジェクト: donny08/mishkal-02
    def contextMenuEvent(self, event):
        popup_menu = self.createStandardContextMenu()
        #~popup_menu = QMenu()
        #~self.setContextMenuPolicy()
        RightToLeft = 1
        # Select the word under the cursor.
        cursor = self.textCursor()
        cursor.select(QTextCursor.WordUnderCursor)
        self.setTextCursor(cursor)

        # Check if the selected word is misspelled and offer spelling
        # suggestions if it is.
        if self.textCursor().hasSelection():

            #~text = (unicode(self.textCursor().selectedText()))
            #this is a workaround for QT bug when double click selects Arabic punctuation marks
            # plus the word in the text editor see https://bugreports.qt-project.org/browse/QTBUG-42397
            #~ originaltext = unicode(self.textCursor().selectedText())
            originaltext = self.textCursor().selectedText()

            arabicmarks = [u'؟', u'،', u'؛', u'“', u'”', u'‘', u'’']
            holder = originaltext[-1]
            if holder in arabicmarks:
                self.pretxt = holder
            else:
                self.pretxt = ''
            text = originaltext.strip(u'؟،؛“”‘’')
            spell_menu = QMenu(u'المزيد...')
            spell_menu.setLayoutDirection(RightToLeft)
            # the word is aleady analyzed
            if self.dict.check(text):

                suggests = self.dict.suggest(text)
                for word in suggests[:10]:
                    action = SpellAction(word, spell_menu)
                    action.correct.connect(self.correctWord)
                    #~spell_menu.addAction(action)
                    popup_menu.addAction(action)

                #~spell_menu.setStyleSheet("QMenu {font: 32px;  margin: 2px;}")
                popup_menu.setStyleSheet("QMenu {font: 24px;}")
                # 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)
                if len(suggests) > 10:
                    for word in suggests[10:]:
                        action = SpellAction(word, spell_menu)
                        action.correct.connect(self.correctWord)
                        #~spell_menu.addAction(action)
                        spell_menu.addAction(action)
                    spell_menu.setStyleSheet("QMenu {font: 24px;}")

                    popup_menu.addSeparator()
                    popup_menu.addMenu(spell_menu)

                if len(suggests) == 1 and not araby.is_vocalized(suggests[0]):
                    addtodict_action = popup_menu.addAction(u'أضف للقاموس')
                    #~addtodict_action.triggered.connect( lambda x = x = originaltext: self.add_to_dict(x))
                    addtodict_action.triggered.connect(
                        lambda: self.add_to_dict(originaltext))
                    # if the word hs no suggestions
                    # we lookup for customized vocalization
                    suggests = self.dict.custom_dict.lookup(word)
                    for word in suggests:
                        action = SpellAction(word, spell_menu)
                        action.correct.connect(self.correctWord)
                        #~spell_menu.addAction(action)
                        popup_menu.addAction(action)

            else:
                # redo taskeel for this word
                #~pass;
                # if the word hs no suggestions
                # we lookup for customized vocalization
                suggests = self.dict.custom_dict.lookup(text)
                for word in suggests:
                    action = SpellAction(word, spell_menu)
                    action.correct.connect(self.correctWord)
                    #~spell_menu.addAction(action)
                    popup_menu.addAction(action)
        popup_menu.exec_(event.globalPos())