Exemple #1
0
void WizTitleEdit::onTextEdit(const QString& text)
{
    if (!c)
        return;

    QString completionPrefix = textUnderCursor();
    bool isSeparator = (!completionPrefix.isEmpty() || charBeforeCursor() == m_separator) ? true : false;

    if (!isSeparator) {
        c->popup()->hide();
        return;
    }


    static QString eow("~!#$%^&*()_+{}|:\"<>?,./;'[]\\-="); // end of word

    if (!isSeparator && (text.isEmpty()
                      || eow.contains(text.right(1)))) {
        c->popup()->hide();
        return;
    }

    if (completionPrefix != c->completionPrefix()) {
        updateCompleterPopupItems(completionPrefix);
    }

    QRect cr = cursorRect();
    cr.setWidth(c->popup()->sizeHintForColumn(0)
                + c->popup()->verticalScrollBar()->sizeHint().width() + 20); // bigger
    c->complete(cr); // popup it up!
}
void LiteEditorWidget::codeCompleter()
{
    QTextCursor cursor = this->textCursor();
    bool isInImport = false;
    if (m_textLexer->isInStringOrComment(cursor)) {
        isInImport = m_textLexer->isInImport(cursor);
        if (!isInImport) {
            return;
        }
    }
    if (isInImport) {
        QString completionPrefix = importUnderCursor(textCursor());
        m_completer->setCompletionContext(LiteApi::CompleterImportContext);
        m_completer->setCompletionPrefix("");
        m_completer->startCompleter(completionPrefix);
    } else {
        QString completionPrefix = textUnderCursor(textCursor());
        if (completionPrefix.startsWith(".")) {
            completionPrefix.insert(0,'@');
        }
        m_completer->setCompletionContext(LiteApi::CompleterCodeContext);
        m_completer->setCompletionPrefix("");
        emit completionPrefixChanged(completionPrefix,true);
        m_completer->startCompleter(completionPrefix);
    }
}
Exemple #3
0
void LineEdit::keyPressEvent (QKeyEvent *e)
{
    if (c && c->popup()->isVisible()) {
            // The following keys are forwarded by the completer to the widget
           switch (e->key()) {
           case Qt::Key_Enter:
           case Qt::Key_Return:
           case Qt::Key_Escape:
           case Qt::Key_Tab:
           case Qt::Key_Backtab:
                e->ignore();
                return; // let the completer do default behavior
           default:
               break;
           }
        }

        bool isShortcut = ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_E); // CTRL+E
        if ((e->key () == Qt::Key_Enter) || (e->key () == Qt::Key_Return)) {
            e->ignore ();
            emit returnPressed();
            return;
        }
        if (!c || !isShortcut) // do not process the shortcut when we have a completer
            QTextEdit::keyPressEvent(e);

        const bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
        if (!c || (ctrlOrShift && e->text().isEmpty()))
            return;

        static QString eow("~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-="); // end of word
        bool hasModifier = (e->modifiers() != Qt::NoModifier) && !ctrlOrShift;
        QString completionPrefix = textUnderCursor();

        if (!isShortcut && (hasModifier || e->text().isEmpty()|| completionPrefix.length() < 2
                          || eow.contains(e->text().right(1)))) {
            c->popup()->hide();
            return;
        }

        if (completionPrefix != c->completionPrefix()) {
            c->setCompletionPrefix(completionPrefix);
            c->popup()->setCurrentIndex(c->completionModel()->index(0, 0));
        }
        QRect cr = cursorRect();
        cr.setWidth(c->popup()->sizeHintForColumn(0)
                    + c->popup()->verticalScrollBar()->sizeHint().width());
        c->complete(cr); // popup it up!

    /*
    if ((e->key () == Qt::Key_Enter) || (e->key () == Qt::Key_Return)) {
        e->ignore ();
        emit returnPressed();
    }
    else
        QTextEdit::keyPressEvent (e);
        */
}
Exemple #4
0
void MimeTextEdit::keyPressEvent(QKeyEvent *e)
{
	if (mCompleter && mCompleter->popup()->isVisible()) {
		// The following keys are forwarded by the completer to the widget
		switch (e->key()) {
		case Qt::Key_Enter:
		case Qt::Key_Return:
		case Qt::Key_Escape:
		case Qt::Key_Tab:
		case Qt::Key_Backtab:
			mCompleter->popup()->hide();
			mForceCompleterShowNextKeyEvent=false;
			e->ignore();
			return; // let the completer do default behavior
		default:
			break;
		}
	}

	bool isShortcut = ((e->modifiers() & mCompleterKeyModifiers) && e->key() == mCompleterKey);
	if (isShortcut && !mForceCompleterShowNextKeyEvent) {
		mCompleterStartString.clear();
	}
	isShortcut |= mForceCompleterShowNextKeyEvent;
	if (!mCompleter || !isShortcut) // do not process the shortcut when we have a completer
		QTextEdit::keyPressEvent(e);

	if (!mCompleter) return; //Nothing else to do if not mCompleter initialized

	if (!isShortcut && (mCompleter && !mCompleter->popup()->isVisible())) {
		return;
	}

	if (!mForceCompleterShowNextKeyEvent) {
		static QString eow(" ~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-="); // end of word
		if (!isShortcut && !e->text().isEmpty() && eow.contains(e->text())){
			mCompleter->popup()->hide();
			return;
		}
	}

	QString completionPrefix = textUnderCursor();
	if (completionPrefix != mCompleter->completionPrefix()) {
		mCompleter->setCompletionPrefix(completionPrefix);
		mCompleter->popup()->setCurrentIndex(mCompleter->completionModel()->index(0, 0));
	}

	QRect cr = cursorRect();
	cr.setWidth(mCompleter->popup()->sizeHintForColumn(0) + mCompleter->popup()->verticalScrollBar()->sizeHint().width());
	mCompleter->complete(cr); // popup it up!

	if (mCompleter->completionCount()==0 && isShortcut){
		QTextEdit::keyPressEvent(e);// Process the key if no match
	}
	mForceCompleterShowNextKeyEvent = false;
}
Exemple #5
0
void WizTitleEdit::onInsertCompletion(const QModelIndex& index)
{
    if (c->widget() != this)
        return;

    QString strOrigin = text();
    QString strExtra = c->completionModel()->data(index, Qt::DisplayRole).toString();
    int nDel = textUnderCursor().size();
    QString strNew = strOrigin.left(strOrigin.size() - nDel) + strExtra + " ";
    setText(strNew);
}
Exemple #6
0
void SCsCodeEditor::keyPressEvent(QKeyEvent *e)
 {
    if (mCompleter->popup()->isVisible())
    {
        switch (e->key())
        {
        case Qt::Key_Enter:
        case Qt::Key_Return:
        case Qt::Key_Escape:
        case Qt::Key_Tab:
        case Qt::Key_Backtab:
             e->ignore();
             return;
        default:
            break;
        }
     }

     bool isShortcut = ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_E);
     if (!isShortcut)
         QPlainTextEdit::keyPressEvent(e);

     const bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
     if (ctrlOrShift && e->text().isEmpty())
         return;

     bool hasModifier = (e->modifiers() != Qt::NoModifier) && !ctrlOrShift;
     QString completionPrefix = textUnderCursor();

     if (!isShortcut && ( hasModifier ||
                          e->text().isEmpty() ||
                          completionPrefix.length() < SCsCodeCompleter::MinCompletetionLength ||
                          !SCsCodeAnalyzer::isIdentifier(e->text().right(1)) ||
                          mAnalyzer->isInEmptyBlock(textCursor().position()) ))
     {
         mCompleter->popup()->hide();
         return;
     }

     if (completionPrefix != mCompleter->completionPrefix()) {
         mCompleter->setCompletionPrefix(completionPrefix);
         mCompleter->popup()->setCurrentIndex(mCompleter->completionModel()->index(0, 0));
     }
     QRect cr = cursorRect();
     cr.setWidth(mCompleter->popup()->sizeHintForColumn(0)
                 + mCompleter->popup()->verticalScrollBar()->sizeHint().width());
     mCompleter->complete(cr);
}
Exemple #7
0
QStringList TextEditor::getWords()
{
    QString text = toPlainText();

    char chars[] = "#.,!()[]'<>:/{}_|=+;`";
    for (unsigned int i = 0; i < strlen(chars); ++i) {
        text = text.replace(chars[i], " ");
    }

    QStringList words = text.split(QRegExp("(\\s|\\n|\\r)+"), QString::SkipEmptyParts).toSet().toList();

    foreach (QString str, words) {
        if(str == textUnderCursor()) {
            words.removeAt(words.indexOf(str));
        }
    }

    return words;
}
Exemple #8
0
void TextEdit::keyPressEvent(QKeyEvent* e)
{
	if (m_Completer && m_Completer->popup()->isVisible()) {
		switch (e->key()) {
		case Qt::Key_Enter:
		case Qt::Key_Return:
		case Qt::Key_Escape:
		case Qt::Key_Tab:
		case Qt::Key_Backtab:
			e->ignore();
			return;
		default:
			break;
		}
	}

	bool isShortcut = ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_Space);
	if (!m_Completer || !isShortcut)
		QTextEdit::keyPressEvent(e);

	const bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
	if (!m_Completer || (ctrlOrShift && e->text().isEmpty()))
		return;

	static QString eow("~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-=");
	bool hasModifier = (e->modifiers() != Qt::NoModifier) && !ctrlOrShift;
	QString completionPrefix = textUnderCursor();

	if (!isShortcut && (hasModifier || e->text().isEmpty()|| completionPrefix.length() < 3
						|| eow.contains(e->text().right(1)))) {
		m_Completer->popup()->hide();
		return;
	}

	if (completionPrefix != m_Completer->completionPrefix()) {
		m_Completer->setCompletionPrefix(completionPrefix);
		m_Completer->popup()->setCurrentIndex(m_Completer->completionModel()->index(0, 0));
	}
	QRect cr = cursorRect();
	cr.setWidth(m_Completer->popup()->sizeHintForColumn(0)
				+ m_Completer->popup()->verticalScrollBar()->sizeHint().width());
	m_Completer->complete(cr);
}
Exemple #9
0
//! [7]
void ContactListEdit::keyPressEvent(QKeyEvent* key_event)
  {
  if (_completer && _completer->popup()->isVisible())
    // The following keys are forwarded by the completer to the widget
    switch (key_event->key())
      {
    case Qt::Key_Enter:
    case Qt::Key_Return:
    case Qt::Key_Escape:
    case Qt::Key_Tab:
    case Qt::Key_Backtab:
      key_event->ignore();
      return;       // let the completer do default behavior
    default:
      break;
      }
  bool isShortcut = ((key_event->modifiers() & Qt::ControlModifier) && key_event->key() == Qt::Key_E);   // CTRL+E
  if (!_completer || !isShortcut)   // do not process the shortcut when we have a completer
    QTextEdit::keyPressEvent(key_event);
  //! [7]

  //! [8]
  const bool ctrlOrShift = key_event->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
  if (!_completer || (ctrlOrShift && key_event->text().isEmpty()))
    return;

  static QString eow("~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-=");   // end of word
  bool           hasModifier = (key_event->modifiers() != Qt::NoModifier) && !ctrlOrShift;
  QString        completionPrefix = textUnderCursor();

  if (!isShortcut && (hasModifier || key_event->text().isEmpty() || completionPrefix.length() == 0
                      || eow.contains(key_event->text().right(1))))
    {
    _completer->popup()->hide();
    return;
    }

  showCompleter(completionPrefix);
  }
Exemple #10
0
void ChatEdit::complete()
{
    QString completionPrefix = textUnderCursor();

    if (completionPrefix.isEmpty()) {
        if (cc->popup()->isVisible())
            cc->popup()->hide();

        return;
    }

    if (!cc->popup()->isVisible() || completionPrefix.length() < cc->completionPrefix().length()) {
        QString pattern = QString("(\\[.*\\])?%1.*").arg( QRegExp::escape(completionPrefix) );
        QStringList nicks = cc_model->findItems(pattern, Qt::MatchRegExp, 0);

        if (nicks.isEmpty())
            return;

        if (nicks.count() == 1) {
            insertToPos(nicks.last(), textCursor().position() - completionPrefix.length());
            return;
        }

        NickCompletionModel *tmpModel = new NickCompletionModel(nicks, cc);
        cc->setModel(tmpModel);
    }

    if (completionPrefix != cc->completionPrefix()) {
        cc->setCompletionPrefix(completionPrefix);
        cc->popup()->setCurrentIndex(cc->completionModel()->index(0, 0));
    }

    QRect cr = cursorRect();
    cr.setWidth(cc->popup()->sizeHintForColumn(0)
                + cc->popup()->verticalScrollBar()->sizeHint().width());

    cc->complete(cr);
}
Exemple #11
0
void CodeArea::onTextChange()
 {
     QStringList lineList = toPlainText().split("\n");
     QString cleanData;
     foreach (QString str, lineList)
     {
         if(!str.startsWith("--"))
         {
             cleanData+=str+" ";
         }
     }
     QRegExp ex("(\\s|\\(|\\)|\\=|,|\\[|\\]|\\{|\\}|\\.|-)");
     QStringList wordList = cleanData.split(ex);
     wordList.removeAll("");
     wordList.removeAll(textUnderCursor());
     wordList.append(mWordList);
     wordList.append( AutoComplete::autoCompleteList.toSet().toList());
     wordList.append( AutoComplete::autoCompleteUOList.toSet().toList());
     wordList.sort();

     if(mCompleter)
        mCompleter->setModel(new QStringListModel(wordList.toSet().toList(), mCompleter));
 }
 void QFCompleterTextEditWidget::keyPressEvent(QKeyEvent *event) {
     if (c && c->popup()->isVisible()) {
         // The following keys are forwarded by the completer to the widget
        switch (event->key()) {
            case Qt::Key_Enter:
            case Qt::Key_Return:
            case Qt::Key_Escape:
            case Qt::Key_Tab:
            case Qt::Key_Backtab:
                 event->ignore();
                 return; // completer widgets handles these keys!
            default:
                break;
        }
     } else {
        // if the completer is not visible, we may treat some special
        // key press events (convert TAB to spaces ...
        if (event->key()==Qt::Key_Tab) { // convert TAB to <tabwidth> spaces
            processTab(event);
            return; // let the completer do default behavior
        } else if (event->key()==Qt::Key_Backtab || (event->key()==Qt::Key_Tab && event->modifiers()==Qt::ShiftModifier)) {
            indentDec();
            return; // let the completer do default behavior
        } else if (event->key()==Qt::Key_Return || event->key()==Qt::Key_Enter) {
            // indent when last non-space character in current line is '{'
            QTextCursor tc = textCursor();

            // if text has been selected, the user wants to overwrite this text
            // with a linebreak, so we first delete the text
            if (tc.hasSelection()) tc.deleteChar();
            //tc.select(QTextCursor::LineUnderCursor);
            tc.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor);
            QString line=tc.selectedText();

            // count leading spaces
            int lspaces=0;
            QChar* data=line.data();
            while (data->isSpace()) {
                ++data;
                lspaces++;
            }
            // get right-most non-space character
            int indx=line.size()-1;
            data=line.data();
            if (indx>=0) while (data[indx].isSpace() && (indx>0)) {
                indx--;
            }
            QChar last=data[indx];

            if (last=='{') {
                QTextCursor c(textCursor());
                c.insertText("\n"+QString(lspaces, QChar(' '))+tabToSpaces());
                setTextCursor(c);
            } else if (last=='}' && line.indexOf('}')==lspaces) {
                // '}' is the first non-space character in this line
                QTextCursor c(textCursor());
                QTextCursor c1(textCursor());
                c1.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor);
                QString left=c1.selectedText();
                if (lspaces>0 && left=="}") {
                    long clspaces=lspaces; // the number of leading spaces in the current line
                    long indention=qMax((long)0, (long)lspaces-(long)tabwidth); // new number of leading spaces
                    // here we try to find the matching '{' and reduce the indention to the
                    // indention of the matching '{'. If the matching '{' was not found,
                    // the indention will not be reduced
                    QTextCursor cc(textCursor());
                    int cnt=1;
                    int pos=cc.selectionStart()-2;
                    while ((cnt>0) && (pos>=0)) {
                        cc.setPosition(pos);
                        cc.setPosition(pos+1, QTextCursor::KeepAnchor);
                        QString curChar=cc.selectedText();
                        if (curChar=="{") cnt--;
                        if (curChar=="}") cnt++;
                        //std::cout<<"'"<<(char*)curChar.toLatin1().data()<<"'  cnt="<<cnt<<"  pos="<<pos<<std::endl;
                        pos--;
                    }
                    // here we found the matching '{' and count its leading spaces
                    if (pos>=0){
                        cc.select(QTextCursor::LineUnderCursor);
                        lspaces=0;
                        QChar* data=cc.selectedText().data();
                        while (data->isSpace()) {
                            ++data;
                            lspaces++;
                        }
                        indention=lspaces;
                    }
                    //std::cout<<"indention="<<indention<<"   clspaces="<<clspaces<<"    lspaces="<<lspaces<<std::endl;
                    c=textCursor();
                    c.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor);
                    c.insertText(QString(indention, QChar(' '))+line.right(qMax((long)1,(long)line.size()-clspaces))+"\n"+QString(indention, QChar(' ')));
                    //c.movePosition(QTextCursor::Left);
                    //c.insertText("\n"+QString(lspaces, QChar(' ')));
                } else {
                    c=textCursor();
                    c.insertText("\n");
                }
                setTextCursor(c);
            } else {
                QTextCursor c(textCursor());
                c.insertText("\n"+QString(lspaces, QChar(' ')));
                setTextCursor(c);
            }
            return;
        }
     }


     bool isShortcut = ((event->modifiers() & Qt::ControlModifier) && event->key() == Qt::Key_E); // CTRL+E
     if (!c || !isShortcut) // dont process the shortcut when we have a completer
         QTextEdit::keyPressEvent(event);

     const bool ctrlOrShift = event->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
     if (!c || (ctrlOrShift && event->text().isEmpty()))
         return;

     static QString eow("~!@#$%^&*()+{}|:\"<>?,./;'[]\\-="); // end of word
     bool hasModifier = (event->modifiers() != Qt::NoModifier) && !ctrlOrShift;
     QString completionPrefix = textUnderCursor();
     /*QMessageBox::information(this, "", tr("completionPrefix=%1\nisShortcut=%2\nctrlOrShif=%3\nhasModifier=%4")
                                            .arg(completionPrefix)
                                            .arg(isShortcut)
                                            .arg(ctrlOrShift)
                                            .arg(hasModifier)
                                            );*/

     if (!isShortcut && (hasModifier || event->text().isEmpty()|| completionPrefix.length() < 2
                       || eow.contains(event->text().right(1)))) {
         c->popup()->hide();
         return;
     }
     /*QMessageBox::information(this, "", tr("c->completionPrefix=%1\ncompletionPrefix=%2")
                                            .arg(c->completionPrefix())
                                            .arg(completionPrefix)
                                            );*/
     if (completionPrefix != c->completionPrefix()) {
         c->setCompletionPrefix(completionPrefix);
         /*QMessageBox::information(this, "", tr("c->completionModel()->rowCount()=%1")
                                            .arg(c->completionModel()->rowCount())
                                            );*/
         c->popup()->setCurrentIndex(c->completionModel()->index(0, 0));
     }
     QRect cr = cursorRect();
     cr.setWidth(c->popup()->sizeHintForColumn(0)
                 + c->popup()->verticalScrollBar()->sizeHint().width());
     c->complete(cr); // popup it up!
     /*QMessageBox::information(this, "", tr("cr.width=%1\ncr.height=%2\ncr.x=%3\ncr.y=%4")
                                            .arg(cr.width())
                                            .arg(cr.height())
                                            .arg(cr.x())
                                            .arg(cr.y())
                                            );*/
}
Exemple #13
0
void SyntaxTextEditor::keyPressEvent(QKeyEvent *e)
{
    if (editCompleter && editCompleter->popup()->isVisible()) {
        // The following keys are forwarded by the completer to the widget
        switch (e->key()) {
        case Qt::Key_Enter:
        case Qt::Key_Return:
        case Qt::Key_Escape:
        case Qt::Key_Tab:
        case Qt::Key_Backtab:            
            e->ignore();
            return; // let the completer do default behavior
        default:
            break;
        }
    }
    if (e->key() == Qt::Key_Tab) {
        indentText(document(), textCursor(),true);
        e->accept();
        return;
    } else if (e->key() == Qt::Key_Backtab) {
        indentText(document(),textCursor(),false);
        e->accept();
        return;
    }

    if ( e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return ) {
        if (this->autoIndent) {
            indentEnter(textCursor());
        } else {
            e->accept();
        }
        emit update();
        return;
    }

    if (this->autoBlock && e->key() == '{') {
        QTextCursor cursor(this->textCursor());
        cursor.insertText("{}");
        cursor.movePosition(QTextCursor::PreviousCharacter);
        setTextCursor(cursor);
        e->accept();
        return;
    }

    if (!this->autoWord) {
        QPlainTextEdit::keyPressEvent(e);
        return;
    }

    bool isShortcut = ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_E); // CTRL+E
    if (!editCompleter || !isShortcut) // do not process the shortcut when we have a completer
        QPlainTextEdit::keyPressEvent(e);

    const bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
    if (!editCompleter || (ctrlOrShift && e->text().isEmpty()))
        return;

    static QString eow("~!@#$%^&*()+{}|:\"<>?,./;'[]\\-= "); // end of word
    bool hasModifier = (e->modifiers() != Qt::NoModifier) && !ctrlOrShift;

    QString word = wordUnderCursor();
    QString text = textUnderCursor();

    if (editCompleter->underCursor(e->key(),textCursor(), text,word)) {
        return;
    }

    if (!isShortcut && (hasModifier || e->text().isEmpty() || text.length() <= 2
                        || eow.contains(e->text().right(1)))) {
        editCompleter->hidePopup();
        return;
    }
    editCompleter->showPopup(word);
}
void ContactListEdit::onTextChanged()
{
    /** Remember last changed text range in _activeCursor member. Will be used to replace entered text
        whith contact entry.
    */
    QString text = textUnderCursor(&_activeCursor);

    if(_skipCompletion == false && _completer != nullptr && isReadOnly() == false &&
            text.isEmpty() == false)
    {
        TAutoSkipCompletion asc(this);

        std::string textKey = text.toStdString();

        bts::addressbook::wallet_contact matchedContact;

        bool contactFound = false;
        try
        {
            auto aBook = bts::get_profile()->get_addressbook();

            auto contact = aBook->get_contact_by_display_name(textKey);
            contactFound = contact;
            if(contactFound == false)
                contact = aBook->get_contact_by_dac_id(textKey);

            if(contact)
            {
                contactFound = true;
                matchedContact = *contact;
            }
        }
        catch(const fc::exception& e)
        {
            wlog( "${e}", ("e",e.to_detail_string()) );
        }

        bool semanticallyValidKey = false;
        if(contactFound)
        {
            /// Delete previously entered text - will be replaced with image control holding contact info
            deleteEnteredText();
            /// Update PK-like text with displayed form
            text = QString::fromStdString(matchedContact.get_display_name());
            /// If text already points to some found contact, just add it
            addContactEntry(text, matchedContact, false);
        }
        else if(public_key_address::is_valid(textKey, &semanticallyValidKey) && semanticallyValidKey)
        {
            /// Delete previously entered text - will be replaced with image control holding contact info
            deleteEnteredText();

            public_key_address converter(textKey);
            fc::ecc::public_key parsedKey = converter.key;

            if(Utils::matchContact(parsedKey, &matchedContact))
            {
                QString displayText = _completerModel->getDisplayText(Contact(matchedContact));
                addContactEntry(displayText, matchedContact, false);

                QString txt = tr("Known public key has been replaced with matching contact...");
                /// Don't pass here parent widget to avoid switching active window when tooltip appears
                QToolTip::showText(QCursor::pos(), txt, nullptr, QRect(), 3000);
            }
            else
            {
                QString tooltip = tr("Valid, but <b>unknown</b> public key, cannot be matched to any contact "
                                     "present in address book...");
                addContactEntry(text, matchedContact, true, &tooltip);
            }
        }
        else
        {
            QTextCharFormat fmt;
            fmt.setFontWeight(QFont::Bold);
            fmt.setForeground(QColor(Qt::red));
            fmt.setToolTip(tr("Unknown contact - can be <b>ignored</b> while sending message"));
            _activeCursor.mergeCharFormat(fmt);

            showCompleter(text);
        }
    }

    fitHeightToDocument();
}
Exemple #15
0
void CodeArea::keyPressEvent(QKeyEvent *e)
{

    if (mCompleter && mCompleter->popup()->isVisible())
    {
             // The following keys are forwarded by the completer to the widget
            switch (e->key())
            {
                case Qt::Key_Enter:
                case Qt::Key_Return:
                case Qt::Key_Escape:
                case Qt::Key_Tab:
                case Qt::Key_Backtab:
                     e->ignore();
                     return; // let the completer do default behavior
                default:
                    break;
            }
    }

    {
       switch (e->key()) {
       case Qt::Key_Tab:
       {
           QTextCursor tc = textCursor();
           if(tc.hasSelection())
           {

           }
           if(!(e->modifiers() & (Qt::ShiftModifier | Qt::ControlModifier)))
           {
               this->insertPlainText("    ");
               QTextCursor tc = textCursor();
               tc.setPosition(tc.position());
               setTextCursor(tc);
               return;
           }

       }
       case Qt::Key_Backtab:
       {
           QTextCursor tc = textCursor();

           QTextBlock tb = tc.block();
           QString str = tb.text();
           int space = 4;
           tc.movePosition(QTextCursor::StartOfLine);
           foreach(QString s, str)
           {
               if(s == " "&& space!=0)
               {
                   space--;
                   tc.movePosition(QTextCursor::Right);
                   tc.deletePreviousChar();

               }
               else
                   break;
           }
           return;
       }
       case Qt::Key_Return:
       {
           QTextCursor tc = textCursor();
           QTextBlock tb = tc.block();
           QString str = tb.text();
           int space = 0;
           foreach(QString s, str)
           {
               if(s == " ")
                   space++;
               else
                   break;
           }
           insertPlainText("\n");
           for(int x= 0; x <space;++x)
               insertPlainText(" ");
           tc.movePosition(QTextCursor::EndOfLine);
           setTextCursor(tc);
           e->accept();
           return;
       }
       default:
           break;
       }
    }

    bool isShortcut = ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_Space); // CTRL+E
    if (!mCompleter || !isShortcut) // do not process the shortcut when we have a completer
        QPlainTextEdit::keyPressEvent(e);

    const bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
    if (!mCompleter || (ctrlOrShift && e->text().isEmpty()))
        return;

    static QString eow("~!@#$%^&*_+(){}|\"<>?,:./;'[]\\-="); // end of word
    bool hasModifier = (e->modifiers() != Qt::NoModifier) && !ctrlOrShift;
    QString completionPrefix = textUnderCursor();
    /*if(completionPrefix.endsWith(")"))
    {
        completionPrefix.remove(completionPrefix.size()-1);
    }*/

    if (!isShortcut && (hasModifier || e->text().isEmpty()|| completionPrefix.length() < 3
                      || eow.contains(e->text().right(1)))) {
        mCompleter->popup()->hide();
        return;
    }

    if (completionPrefix != mCompleter->completionPrefix()) {
        mCompleter->setCompletionPrefix(completionPrefix);
        mCompleter->popup()->setCurrentIndex(mCompleter->completionModel()->index(0, 0));
    }
    QRect cr = cursorRect();
    cr.setWidth(mCompleter->popup()->sizeHintForColumn(0)
                + mCompleter->popup()->verticalScrollBar()->sizeHint().width());
    mCompleter->complete(cr); // popup it up!
}
Exemple #16
0
void TextEdit::keyPressEvent(QKeyEvent *e)
{
    if (c)
    {
        if (c->popup()->isVisible())
        {
            // The following keys are forwarded by the completer to the widget
            switch (e->key())
            {
            case Qt::Key_Enter:
            case Qt::Key_Return:
            case Qt::Key_Escape:
            case Qt::Key_Tab:
            case Qt::Key_Backtab:
                e->ignore();
                return; // let the completer do default behavior
            default:
                break;
            }
        }
        /*
        else if (e->key() == Qt::Key_Tab)
        {
            QTextCursor tc = textCursor();
            QStringList lines = tc.selectedText().split("\n");
            QString tabbedText = QString();
            int linesNumber = lines.size();

            for (int i = 0; i < linesNumber; ++i)
            {
                QString line = lines.at(i);
                line.prepend("\t");

                if (linesNumber > 1)
                    line.append("\n");

                tabbedText += line;
            }

            tc.insertText(tabbedText);
            setTextCursor(tc);
        }
        */
    }

    bool isShortcut = ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_Space);
    if (!c || !isShortcut) // dont process the shortcut when we have a completer
        QPlainTextEdit::keyPressEvent(e);

    const bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
    if (!c || (ctrlOrShift && e->text().isEmpty()))
        return;

    static QString eow("~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-="); // end of word
    bool hasModifier = (e->modifiers() != Qt::NoModifier) && !ctrlOrShift;
    QString completionPrefix = textUnderCursor();

    if (!isShortcut && (hasModifier || e->text().isEmpty()|| completionPrefix.length() < 1 || eow.contains(e->text().right(1))))
    {
        c->popup()->hide();
        return;
    }

    if (completionPrefix != c->completionPrefix())
    {
        c->setCompletionPrefix(completionPrefix);
        c->popup()->setCurrentIndex(c->completionModel()->index(0, 0));
    }

    QRect cr = cursorRect();
    cr.setWidth(c->popup()->sizeHintForColumn(0) + c->popup()->verticalScrollBar()->sizeHint().width());
    c->complete(cr);
}
Exemple #17
0
void RtfCssEditor::keyPressEvent(QKeyEvent *e) {
  if (currentCompleter == ALTERNATE) {
    currentCompleter = DEFAULT;
    setCompleter(standardCompleter);
  }
  
  //If enter is pressed
  if (((e->key() == Qt::Key_Enter) || (e->key() == Qt::Key_Return)) && (!c->popup()->isVisible())) {
    QTextCursor tc = textCursor();
    int currentPos = tc.position();
    tc.select(QTextCursor::LineUnderCursor);
    QString line = tc.selectedText();
    tc.setPosition(currentPos);
  
    if (line.indexOf("{") != -1) {
      insertBrace();
      return;
    }
    else {
      insertTabulation();
      return;
    }
  }
  
  if (c && c->popup()->isVisible()) {
      // The following keys are forwarded by the completer to the widget
    switch (e->key()) {
    case Qt::Key_Enter:
      return;
    case Qt::Key_Return:
      return;
    case Qt::Key_Escape:
    case Qt::Key_Tab:
    case Qt::Key_Backtab:
	  e->ignore();
	  return; // let the completer do default behavior
    default:
	break;
    }
  }

  bool isShortcut = ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_Space); // CTRL+E
  if (!c || !isShortcut) // dont process the shortcut when we have a completer
    QTextEdit::keyPressEvent(e);
  const bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
  if (!c || (ctrlOrShift && e->text().isEmpty()))
    return;

  static QString eow("~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-="); // end of word
  bool hasModifier = (e->modifiers() != Qt::NoModifier) && !ctrlOrShift;
  QString completionPrefix = textUnderCursor();
  
  if (isUnit() && isShortcut) {
    insertPropriety(UNIT);
    return;
  }
  
  if (isPropriety() && isShortcut) {
    insertPropriety(VALUE);
    return;
  }


  if (!isShortcut && (hasModifier || e->text().isEmpty()|| completionPrefix.length() < 3 || eow.contains(e->text().right(1)))) {
    c->popup()->hide();
    return;
  }

  if (completionPrefix != c->completionPrefix()) {
    c->setCompletionPrefix(completionPrefix);
    c->popup()->setCurrentIndex(c->completionModel()->index(0, 0));
  }
  QRect cr = cursorRect();
  cr.setWidth(c->popup()->sizeHintForColumn(0)
	      + c->popup()->verticalScrollBar()->sizeHint().width());
  c->complete(cr); // popup it up!
}
Exemple #18
0
void LiteEditorWidget::keyPressEvent(QKeyEvent *e)
{
    if (m_completer && m_completer->popup()->isVisible()) {
        // The following keys are forwarded by the completer to the widget
        switch (e->key()) {
        case Qt::Key_Enter:
        case Qt::Key_Return:
        case Qt::Key_Escape:
        case Qt::Key_Tab:
        case Qt::Key_Backtab:
            e->ignore();
            return; // let the completer do default behavior
        default:
            break;
        }
    }

    bool isShortcut = ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_E); // CTRL+E
    bool hasControl = e->modifiers() & Qt::ControlModifier;
    if (!m_completer || !isShortcut) {// do not process the shortcut when we have a completer
        if (!hasControl) {
            LiteEditorWidgetBase::keyPressEvent(e);
        } else {
            QKeyEvent *evt = new QKeyEvent(e->type(), e->key(), e->modifiers(), "");
            QPlainTextEdit::keyPressEvent(evt);
            delete evt;
        }
    }

    const bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
    if (!m_completer || (ctrlOrShift && e->text().isEmpty()))
        return;

    if (hasControl) {
        m_completer->popup()->hide();
        return;
    }
    if (e->key() == Qt::Key_Tab || e->key() == Qt::Key_Backtab) {
        return;
    }
    //static QString eow("~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-="); // end of word
    static QString eow("~!@#$%^&*()+{}|:\"<>?,/;'[]\\-="); // end of word
    bool hasModifier = (e->modifiers() != Qt::NoModifier) && !ctrlOrShift;
    QString completionPrefix = textUnderCursor(textCursor());    
    if (completionPrefix.startsWith(".")) {
        completionPrefix.insert(0,'@');
    }

    if (!isShortcut && (hasModifier || e->text().isEmpty()||
                        ( completionPrefix.length() < m_completionPrefixMin && completionPrefix.right(1) != ".")
                        || eow.contains(e->text().right(1)))) {
        m_completer->popup()->hide();
        return;
    }

    // Do not pass data to the completer when we're in a spell-checked region
    if (m_bSpellCheckZoneDontComplete && isSpellCheckingAt(textCursor())) {
        return;
    }

    emit completionPrefixChanged(completionPrefix);

    if (completionPrefix != m_completer->completionPrefix()) {
        m_completer->setCompletionPrefix(completionPrefix);
        m_completer->popup()->setCurrentIndex(m_completer->completionModel()->index(0, 0));
    }

    if (m_completer->currentCompletion() == completionPrefix) {
        m_completer->popup()->hide();
        return;
    }

    QRect cr = cursorRect();
    cr.setWidth(m_completer->popup()->sizeHintForColumn(0)
                + m_completer->popup()->verticalScrollBar()->sizeHint().width());

    m_completer->complete(cr); // popup it up!
}
Exemple #19
0
void TextEdit::keyPressEvent(QKeyEvent *e)
{
    if (c && c->popup()->isVisible()) {
        // The following keys are forwarded by the completer to the widget
       switch (e->key()) {
       case Qt::Key_Enter:
       case Qt::Key_Return:
       case Qt::Key_Escape:
       case Qt::Key_Tab:
       case Qt::Key_Backtab:
            e->ignore(); 
            return; // let the completer do default behavior
       default:
           break;
       }
    }
    DomModel *dommodel = ((QCmdCompleter*) c)->domModel();
    Q_CHECK_PTR( dommodel );
    bool isShortcut = ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_E); // CTRL+E
    if ( isShortcut ) {
//        qDebug("...isShortcut....(1)....");
    }
    bool isShortcutLine = ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_L); // CTRL+L
    if ( isShortcutLine ) {
         QRect cr = cursorRect();
        showFromWidget( cr );
        return; 
    }
    if (!c || !isShortcut) {// dont process the shortcut when we have a completer
        QTextEdit::keyPressEvent(e);
        return;
   }

    const bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
    if (!c || (ctrlOrShift && e->text().isEmpty()))
        return;

    static QString eow("~!@#$%^&*()_+{}|:\"<>?,./;'[]="); // end of word
    bool hasModifier = (e->modifiers() != Qt::NoModifier) && !ctrlOrShift;
    QString completionPrefix = textUnderCursor();

    if (!isShortcut && (hasModifier || e->text().isEmpty()|| completionPrefix.length() < 3
                      || eow.contains(e->text().right(1)))) {
        c->popup()->hide();	
    }

    if (completionPrefix != c->completionPrefix()) {
        c->setCompletionPrefix(completionPrefix);   
        c->popup()->setCurrentIndex(c->completionModel()->index(0, 0));
    }
    QRect cr = cursorRect();
    cr.setWidth(c->popup()->sizeHintForColumn(0)
                + c->popup()->verticalScrollBar()->sizeHint().width());

    QStringList list;
    bool listcommand = searchPrvsToken(list);
    if ( !listcommand ) {
        dommodel->getCommands();

    }
    else {
        QStringList infolist = list.at( list.count()-1 ).split(SafetYAWL::LISTSEPARATORCHARACTER,QString::SkipEmptyParts);
        Q_ASSERT( infolist.count() > 0 );

        fields = dommodel->getFields(infolist.at(0) );
        for(int i = 0; i < list.count()-1; i++ ) {
            QString field = list.at(i).trimmed();
            QString pattern = QString("%1\\*?(\\:\\:\\:.+)?").arg(field);
            QRegExp rx(pattern);
            int pos = fields.indexOf(rx);
            if ( pos != -1 && !dommodel->isRepetibleField(field)) {
                fields.removeAt(pos);
            }
        }
        dommodel->setStringList( fields );
    }	


    c->complete(cr); // popup it up!



}
//-----------------------------------------------------------------------------------------
void GenericTextEditorDocument::keyPressEvent(QKeyEvent *event)
{
    if(mCompleter && mCompleter->popup()->isVisible()) 
    {
        switch (event->key()) 
        {
        case Qt::Key_Enter:
        case Qt::Key_Return:
        case Qt::Key_Escape:
        case Qt::Key_Tab:
        case Qt::Key_Backtab:
            event->ignore();
            return;
        default:
            break;
        }
    }

    // Auto start next line with the indentation of previous line...
    if(event->key() == Qt::Key_Return)
    {
        QTextCursor tc = textCursor();
        int savePos = tc.position();
        //Get Previous bracket position
        tc.movePosition(QTextCursor::Start, QTextCursor::KeepAnchor);
        QString tmpStr = tc.selectedText();

        int count = calculateIndentation(tmpStr);

        tc.setPosition(savePos);
        QString txt = "\n";
        for(int z = 0;z < count;z++)
            txt += " ";
        tc.insertText(txt);
        tc.setPosition(savePos + count + 1);
        setTextCursor(tc);
        event->accept();
        return;
    }

    // Insert 4 spaces instead of tab
    if(event->key() == Qt::Key_Tab)
    {
        QTextCursor tc = textCursor();
        int savePos = tc.position();
        QString txt = "    ";
        tc.insertText(txt);
        tc.setPosition(savePos + 4);
        setTextCursor(tc);
        event->accept();
        return;
    }

    bool isShortcut = ((event->modifiers() & Qt::ControlModifier) && event->key() == Qt::Key_Space); 
    if(!mCompleter || !isShortcut) 
    {
        mCodec->onKeyPressEvent(event);
        QPlainTextEdit::keyPressEvent(event);
    }

    const bool ctrlOrShift = event->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
    if(!mCompleter || (ctrlOrShift && event->text().isEmpty()))
        return;

    static QString eow("~!@#$%^&*()+{}|:\"<>?,./;'[]\\-= "); // end of word
    bool hasModifier = (event->modifiers() != Qt::NoModifier) && !ctrlOrShift;
    QString completionPrefix = textUnderCursor();

    if(!isShortcut && (hasModifier || event->text().isEmpty() || completionPrefix.length() < 1
        || eow.contains(event->text().right(1))))
    {
        mCompleter->popup()->hide();
        return;
    }

    if(completionPrefix != mCompleter->completionPrefix()) 
    {
        mCompleter->setCompletionPrefix(completionPrefix);
        mCompleter->popup()->setCurrentIndex(mCompleter->completionModel()->index(0, 0));
    }
    QRect cr = cursorRect();
    cr.setWidth(mCompleter->popup()->sizeHintForColumn(0) + mCompleter->popup()->verticalScrollBar()->sizeHint().width());
    mCompleter->complete(cr);

    mCodec->onKeyPressEvent(event);
}
void LiteEditorWidget::keyPressEvent(QKeyEvent *e)
{
    if (m_completer && m_completer->popup()->isVisible()) {
        // The following keys are forwarded by the completer to the widget
        switch (e->key()) {
        case Qt::Key_Enter:
        case Qt::Key_Return:
        case Qt::Key_Escape:
        case Qt::Key_Tab:
        case Qt::Key_Backtab:
            e->ignore();
            return; // let the completer do default behavior
        default:
            break;
        }
    }

    LiteEditorWidgetBase::keyPressEvent(e);

    const bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
    if (!m_completer || (ctrlOrShift && e->text().isEmpty()))
        return;

    if (e->modifiers() & Qt::ControlModifier) {
        m_completer->popup()->hide();
        return;
    }
    if (e->key() == Qt::Key_Tab || e->key() == Qt::Key_Backtab) {
        return;
    }
    //static QString eow("~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-="); // end of word
    static QString eow("~!@#$%^&*()+{}|:\"<>?,/;'[]\\-="); // end of word
    bool hasModifier = (e->modifiers() != Qt::NoModifier) && !ctrlOrShift;
    QString completionPrefix = textUnderCursor(textCursor());
    if (completionPrefix.startsWith(".")) {
        completionPrefix.insert(0,'@');
    }

    if (hasModifier || e->text().isEmpty()||
                        ( completionPrefix.length() < m_completionPrefixMin && completionPrefix.right(1) != ".")
                        || eow.contains(e->text().right(1))) {
        m_completer->popup()->hide();
        return;
    }
    emit completionPrefixChanged(completionPrefix);

    if (completionPrefix != m_completer->completionPrefix()) {
        m_completer->setCompletionPrefix(completionPrefix);
        m_completer->popup()->setCurrentIndex(m_completer->completionModel()->index(0, 0));
    }

    if (m_completer->currentCompletion() == completionPrefix) {
        m_completer->popup()->hide();
        return;
    }

    QRect cr = cursorRect();
    cr.setWidth(m_completer->popup()->sizeHintForColumn(0)
                + m_completer->popup()->verticalScrollBar()->sizeHint().width());

    m_completer->complete(cr); // popup it up!
}
void LiteEditorWidget::keyPressEvent(QKeyEvent *e)
{
    if (!m_completer) {
        LiteEditorWidgetBase::keyPressEvent(e);
        return;
    }

    if (m_completer->popup()->isVisible()) {
        // The following keys are forwarded by the completer to the widget
        switch (e->key()) {
        case Qt::Key_Enter:
        case Qt::Key_Return:
        case Qt::Key_Escape:
        case Qt::Key_Tab:
        case Qt::Key_Backtab:
        case Qt::Key_Shift:
            e->ignore();
            return; // let the completer do default behavior
        case Qt::Key_N:
        case Qt::Key_P:
            if (e->modifiers() == Qt::ControlModifier) {
                e->ignore();
                return;
            }
        default:
            break;
        }
    }

    LiteEditorWidgetBase::keyPressEvent(e);

    bool isInImport = false;
    if (m_textLexer->isInStringOrComment(this->textCursor())) {
        isInImport = m_textLexer->isInImport(this->textCursor());
        if (!isInImport) {
            m_completer->hidePopup();
            return;
        }
    }

    const bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);

    //always break if ctrl is pressed and there's a key
//    if (((e->modifiers() & Qt::ControlModifier) && !e->text().isEmpty())) {
//        return;
//    }
    if (e->modifiers() & Qt::ControlModifier) {
        if (!e->text().isEmpty()) {
            m_completer->hidePopup();
        }
        return;
    }

    if (e->key() == Qt::Key_Tab || e->key() == Qt::Key_Backtab) {
        return;
    }

    if (e->text().isEmpty()) {
        if (e->key() != Qt::Key_Backspace) {
            m_completer->hidePopup();
            return;
        }
    }
    //import line
    if (isInImport) {
        QString completionPrefix = importUnderCursor(textCursor());
        m_completer->setCompletionContext(LiteApi::CompleterImportContext);
        m_completer->setCompletionPrefix("");
        m_completer->startCompleter(completionPrefix);
        return;
    }

    //static QString eow("~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-="); // end of word
    static QString eow("~!@#$%^&*()+{}|:\"<>?,/;'[]\\-="); // end of word
    bool hasModifier = (e->modifiers() != Qt::NoModifier) && !ctrlOrShift;
    QString completionPrefix = textUnderCursor(textCursor());
    if (completionPrefix.startsWith(".")) {
        completionPrefix.insert(0,'@');
    }

    if (hasModifier || e->text().isEmpty()||
                        ( completionPrefix.length() < m_completionPrefixMin && completionPrefix.right(1) != ".")
                        || eow.contains(e->text().right(1))) {
        if (m_completer->popup()->isVisible()) {
            m_completer->popup()->hide();
            //fmt.Print( -> Print
            if (e->text() == "(") {
                QTextCursor cur = textCursor();
                cur.movePosition(QTextCursor::Left);
                QString lastPrefix = textUnderCursor(cur);
                if (lastPrefix.startsWith(".")) {
                    lastPrefix.insert(0,"@");
                }
                if (!lastPrefix.isEmpty() &&
                        lastPrefix == m_completer->completionPrefix() ) {
                    if (lastPrefix == m_completer->currentCompletion() ||
                            lastPrefix.endsWith("."+m_completer->currentCompletion())) {
                        m_completer->updateCompleteInfo(m_completer->currentIndex());
                    }
                }
            }
        }
        return;
    }
    m_completer->setCompletionContext(LiteApi::CompleterCodeContext);
    emit completionPrefixChanged(completionPrefix,false);
    m_completer->startCompleter(completionPrefix);
}
void ScriptEditorWidget::keyPressEvent(QKeyEvent * e)
{
	if(m_pCompleter && m_pCompleter->popup()->isVisible())
	{
		switch(e->key())
		{
			case Qt::Key_Enter:
			case Qt::Key_Return:
			case Qt::Key_Escape:
			case Qt::Key_Tab:
			case Qt::Key_Backtab:
				e->ignore();
				return;
			default:
				break;
		}
	}

	if(e->modifiers() == Qt::ControlModifier)
	{
		switch(e->key())
		{
			case Qt::Key_B:
				insertPlainText("$b");
				return;
			case Qt::Key_K:
				insertPlainText("$k");
				return;
			case Qt::Key_O:
				insertPlainText("$o");
				return;
			case Qt::Key_U:
				insertPlainText("$u");
				return;
			case Qt::Key_Enter:
			case Qt::Key_Return:
			case Qt::Key_PageUp:
				 e->ignore(); // allow the parent to process it
				 return;
			break;
		}
	}
// Adapted from QT4 QCompleter example
	bool isShortcut = ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_E); // CTRL+E
	if (!m_pCompleter || !isShortcut) // don't process the shortcut when we have a completer
		QTextEdit::keyPressEvent(e);
	const bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
	if (!m_pCompleter || (ctrlOrShift && e->text().isEmpty()))
		return;
	static QString eow("~!@#$%^&*()_+{}|:\"<>?,/;'[]\\-="); // end of word
	bool hasModifier = (e->modifiers() != Qt::NoModifier) && !ctrlOrShift;
	QString completionPrefix = textUnderCursor();
	if (!isShortcut && (hasModifier || e->text().isEmpty()|| completionPrefix.length() < 3 || eow.contains(e->text().right(1))))
	{
		m_pCompleter->popup()->hide();
		return;
	}
	if (completionPrefix != m_pCompleter->completionPrefix())
	{
		m_pCompleter->setCompletionPrefix(completionPrefix);
		m_pCompleter->popup()->setCurrentIndex(m_pCompleter->completionModel()->index(0, 0));
	}
	QRect cr = cursorRect();
	cr.setWidth(m_pCompleter->popup()->sizeHintForColumn(0)+ m_pCompleter->popup()->verticalScrollBar()->sizeHint().width());
	m_pCompleter->complete(cr);
}
Exemple #24
0
void TikzEditor::keyPressEvent(QKeyEvent *event)
{
	const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();

	/* completer */
	if (m_completer && m_completer->popup()->isVisible())
	{
		// the following keys are forwarded by the completer to the widget
		switch (event->key())
		{
			case Qt::Key_Enter:
			case Qt::Key_Return:
			case Qt::Key_Escape:
			case Qt::Key_Tab:
			case Qt::Key_Backtab:
				event->ignore();
				return;
		}
	}

	/* scroll viewport when Ctrl+Up and Ctrl+Down are pressed */
	if (event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_Up)
	{
		const int dy = -1 + verticalScrollBar()->value();
		verticalScrollBar()->setValue(dy);
	}
	else if (event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_Down)
	{
		const int dy = 1 + verticalScrollBar()->value();
		verticalScrollBar()->setValue(dy);
	}
	/* ensure that PageUp and PageDown keep the cursor at the same visible place */
/*
	else if (event->key() == Qt::Key_PageUp || event->key() == Qt::Key_PageDown)
	{
		const QTextCursor::MoveOperation moveOperation
		    = (event->key() == Qt::Key_PageUp) ? QTextCursor::Up : QTextCursor::Down;
		QTextCursor cursor = textCursor();
		const QFontMetrics fontMetrics(document()->defaultFont());
		const int repeat = viewport()->height() / fontMetrics.lineSpacing() - 1;
		const int oldPosition = cursorRect().top();
		if (modifier & Qt::ShiftModifier)
			cursor.movePosition(moveOperation, QTextCursor::KeepAnchor, repeat);
		else
			cursor.movePosition(moveOperation, QTextCursor::MoveAnchor, repeat);
		setTextCursor(cursor);
		const int newPosition = verticalScrollBar()->value() + cursorRect().top() - oldPosition;
		verticalScrollBar()->setValue(newPosition);
		ensureCursorVisible();
	}
*/
	/* the first time End is pressed moves the cursor to the end of the line, the second time to the end of the block */
	else if (event->key() == Qt::Key_Home
	    && !(modifier & Qt::ControlModifier)
	    && !(modifier & Qt::ShiftModifier))
	{
		QTextCursor cursor = textCursor();
		const int oldPosition = cursor.position();
		cursor.movePosition(QTextCursor::StartOfLine);
		if (cursor.position() == oldPosition)
			cursor.movePosition(QTextCursor::StartOfBlock);
		setTextCursor(cursor);
		ensureCursorVisible();
	}
	else if (event->key() == Qt::Key_End
	    && !(modifier & Qt::ControlModifier)
	    && !(modifier & Qt::ShiftModifier))
	{
		QTextCursor cursor = textCursor();
		const int oldPosition = cursor.position();
		cursor.movePosition(QTextCursor::EndOfLine);
		if (cursor.position() == oldPosition)
			cursor.movePosition(QTextCursor::EndOfBlock);
		setTextCursor(cursor);
		ensureCursorVisible();
	}
	/* keys that change the content without moving the cursor may alter the brackets too */
	else if (event->key() == Qt::Key_Delete || (event->key() == Qt::Key_Z && (modifier & Qt::ControlModifier)))
	{
		QPlainTextEdit::keyPressEvent(event);
		highlightCurrentLine();
		matchBrackets(); // calculate new bracket highlighting
	}
	/* go to next argument in text inserted with code completion */
	else if (event->key() == Qt::Key_Tab || event->key() == Qt::Key_Backtab)
	{
		QTextCursor cursor = textCursor();
		QTextBlock block = cursor.block();
		QTextDocument::FindFlags flags = 0;
		if (event->key() == Qt::Key_Backtab)
			flags = QTextDocument::FindBackward;
		if (block.isValid() && block.text().contains(s_completionPlaceHolder))
		{
			cursor = document()->find(s_completionPlaceHolder, cursor, flags);
			if (!cursor.isNull())
				setTextCursor(cursor);
			else
				QPlainTextEdit::keyPressEvent(event);
		}
		else
			QPlainTextEdit::keyPressEvent(event);
	}
	else
		QPlainTextEdit::keyPressEvent(event);

	/* completer */
	if (m_completer)
	{
//		const QString endOfWord("~!@#$%^&*()_+{}|:\"<>?,./;'[]-= ");
		const QString completionPrefix = textUnderCursor();
		if ((event->modifiers() & (Qt::ControlModifier | Qt::AltModifier))
		    || (event->text().isEmpty() && event->key() != Qt::Key_AltGr)
		    || completionPrefix.length() < 3)
//		    || endOfWord.contains(event->text().right(1)))
		{
			m_completer->popup()->hide();
		}
		else
		{
			if (completionPrefix != m_completer->completionPrefix())
			{
				m_completer->setCompletionPrefix(completionPrefix);
				m_completer->popup()->setCurrentIndex(m_completer->completionModel()->index(0, 0));
			}
			if (m_completer->completionPrefix() != m_completer->currentCompletion())
//			    || m_completer->completionCount() > 1)
			{
				QTextCursor cursor = textCursor();
//				cursor.movePosition(QTextCursor::StartOfWord);
				cursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, m_completer->completionPrefix().length());
				QRect rect = cursorRect(cursor);
				rect.translate(5, 5);
				rect.setWidth(m_completer->popup()->sizeHintForColumn(0)
				    + m_completer->popup()->verticalScrollBar()->sizeHint().width());
				m_completer->complete(rect); // show popup
			}
			else
				m_completer->popup()->hide();
		}
	}
}