void HelpViewer::showHelp(const String& org_url, String entry) { String url = org_url; String fragment; if (url.has('#')) { url = url.before("#"); fragment = org_url.after("#"); } QUrl qurl = QUrl::fromLocalFile((base_dir_ + url).c_str()); if (fragment != "") qurl.setFragment(fragment.c_str()); browser_->setSource(qurl); QTextCursor ct = browser_->textCursor(); if (!ct.atStart()) { ct.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor); browser_->setTextCursor(ct); } if (entry != "") browser_->find(entry.c_str(), QTextDocument::FindCaseSensitively); if (!isVisible()) { show(); setFloating(true); showMaximized(); } }
void TextZone::cursorPositionChangedSlot() { if (QApplication::mouseButtons() == Qt::NoButton) { centerCursor(); } emit cursorPositionChanged(this->textCursor().position()); // for textstyle : QTextCursor tCursor = this->textCursor(); if((tCursor.atStart() == true || tCursor.position() == 1 || tCursor.position() == 0) && tCursor.hasSelection() == false){ this->changeTextStyleSlot(textStyles->defaultStyleIndex()); } int currentStyleIndex = textStyles->compareStylesWithText(tCursor.charFormat(), tCursor.blockFormat()); emit setStyleSelectionSignal(currentStyleIndex); }
void CCodeEditor::_DeleteCurrentLine() { QTextCursor currentCursor = this->textCursor(); currentCursor.select(QTextCursor::BlockUnderCursor); currentCursor.removeSelectedText(); ++m_textChangeTime; if (currentCursor.atStart()) { currentCursor.deleteChar(); ++m_textChangeTime; } }
bool TextEditEx::isAllSelected() { QTextCursor cur = textCursor(); if (!cur.hasSelection()) return false; const int start = cur.selectionStart(); const int end = cur.selectionEnd(); cur.setPosition(start); if (cur.atStart()) { cur.setPosition(end); return cur.atEnd(); } else if (cur.atEnd()) { cur.setPosition(start); return cur.atStart(); } return false; }
void QtUtils::selectLine(QTextEdit* ted, int line) { QTextCursor tcur = ted->textCursor(); tcur.beginEditBlock(); tcur.clearSelection(); tcur.movePosition(QTextCursor::Start); bool ok; ok = tcur.atStart(); if (ok) { tcur.movePosition(QTextCursor::Down,QTextCursor::MoveAnchor,line); tcur.movePosition(QTextCursor::Up,QTextCursor::KeepAnchor,1); } tcur.endEditBlock(); ted->setTextCursor(tcur); ted->ensureCursorVisible(); }
void TextZone::insertFromMimeData (const QMimeData *source ) { QTextCursor cursor = this->textCursor(); int bottMargin; int textIndent; int leftMargin; Qt::Alignment textAlignment; int textHeight; QString fontFamily; if(cursor.atStart() == true || cursor.position() == 1 || cursor.position() == 0){ int defaultIndex = textStyles->defaultStyleIndex(); bottMargin = textStyles->blockBottomMarginAt(defaultIndex); textIndent = textStyles->blockFirstLineIndentAt(defaultIndex); leftMargin = textStyles->blockLeftMarginAt(defaultIndex); textAlignment = textStyles->blockAlignmentTrueNameAt(defaultIndex); textHeight = textStyles->fontSizeAt(defaultIndex); fontFamily = textStyles->fontFamilyAt(defaultIndex); } else{ bottMargin = cursor.blockFormat().bottomMargin(); textIndent = cursor.blockFormat().textIndent(); leftMargin = cursor.blockFormat().leftMargin(); textAlignment = cursor.blockFormat().alignment(); textHeight = cursor.charFormat().fontPointSize(); fontFamily = cursor.charFormat().fontFamily(); } if(source->hasHtml() && !forceCopyWithoutFormatting){ QByteArray richtext; if (source->hasFormat(QLatin1String("text/rtf"))) { richtext = source->data(QLatin1String("text/rtf")); } else if (source->hasHtml()) { richtext = mimeToRtf(source); } QTextEdit *textEdit = new QTextEdit(0); RTF::Reader reader; QBuffer buffer(&richtext); buffer.open(QIODevice::ReadOnly); reader.read(&buffer, textEdit->textCursor()); buffer.close(); QString sourceString = textEdit->toHtml(); sourceString.remove(QChar::ReplacementCharacter); sourceString.remove(QChar::ObjectReplacementCharacter); sourceString.remove(QChar::Null); //htmlText QTextDocument *document = new QTextDocument; document->setHtml(Utils::parseHtmlText(sourceString)); QTextBlockFormat blockFormat; blockFormat.setBottomMargin(bottMargin); blockFormat.setTextIndent(textIndent); blockFormat.setLeftMargin(leftMargin); blockFormat.setAlignment(textAlignment); blockFormat.setRightMargin(0); QTextCharFormat charFormat; charFormat.setFontPointSize(textHeight); charFormat.setFontFamily(fontFamily); charFormat.setBackground(QBrush(Qt::NoBrush)); charFormat.setForeground(QBrush(Qt::NoBrush)); charFormat.setAnchor(false); charFormat.setUnderlineStyle(QTextCharFormat::NoUnderline); charFormat.setFontStrikeOut(false); QTextCursor *tCursor = new QTextCursor(document); tCursor->movePosition(QTextCursor::Start, QTextCursor::MoveAnchor,1); tCursor->movePosition(QTextCursor::End, QTextCursor::KeepAnchor,1); tCursor->mergeCharFormat(charFormat); tCursor->mergeBlockFormat(blockFormat); QTextCursor cursor = this->textCursor(); cursor.insertHtml(document->toHtml("utf-8")); qDebug() << "insertFromMimeData Html"; } else if(source->hasText() || forceCopyWithoutFormatting){ QTextDocument *document = new QTextDocument; document->setPlainText(qvariant_cast<QString>(source->text())); QTextBlockFormat blockFormat; blockFormat.setBottomMargin(bottMargin); blockFormat.setTextIndent(textIndent); blockFormat.setLeftMargin(leftMargin); blockFormat.setAlignment(textAlignment); blockFormat.setRightMargin(0); QTextCharFormat charFormat; charFormat.setFontPointSize(textHeight); charFormat.setFontFamily(fontFamily); charFormat.clearForeground(); charFormat.setBackground(QBrush(Qt::NoBrush)); charFormat.setForeground(QBrush(Qt::NoBrush)); charFormat.setAnchor(false); charFormat.setUnderlineStyle(QTextCharFormat::NoUnderline); charFormat.setFontStrikeOut(false); QTextCursor *tCursor = new QTextCursor(document); tCursor->movePosition(QTextCursor::Start, QTextCursor::MoveAnchor,1); tCursor->movePosition(QTextCursor::End, QTextCursor::KeepAnchor,1); tCursor->mergeCharFormat(charFormat); tCursor->mergeBlockFormat(blockFormat); QTextCursor cursor = this->textCursor(); cursor.insertHtml(document->toHtml("utf-8")); qDebug() << "insertFromMimeData plainText"; } }
void Note::linkDialog() { QTextCursor textCursor = m_graphicsTextItem->textCursor(); bool gotUrl = false; if (textCursor.anchor() == textCursor.selectionStart()) { // the selection returns empty since we're between characters // so select one character forward or one character backward // to see whether we're in a url int wasAnchor = textCursor.anchor(); bool atEnd = textCursor.atEnd(); bool atStart = textCursor.atStart(); if (!atStart) { textCursor.setPosition(wasAnchor - 1, QTextCursor::KeepAnchor); QString html = textCursor.selection().toHtml(); if (UrlTag.indexIn(html) >= 0) { gotUrl = true; } } if (!gotUrl && !atEnd) { textCursor.setPosition(wasAnchor + 1, QTextCursor::KeepAnchor); QString html = textCursor.selection().toHtml(); if (UrlTag.indexIn(html) >= 0) { gotUrl = true; } } textCursor.setPosition(wasAnchor, QTextCursor::MoveAnchor); } else { QString html = textCursor.selection().toHtml(); DebugDialog::debug(html); if (UrlTag.indexIn(html) >= 0) { gotUrl = true; } } LinkDialog ld; QString originalText; QString originalUrl; if (gotUrl) { originalUrl = UrlTag.cap(1); ld.setUrl(originalUrl); QString html = m_graphicsTextItem->toHtml(); // assumes html is in xml form QString errorStr; int errorLine; int errorColumn; QDomDocument domDocument; if (!domDocument.setContent(html, &errorStr, &errorLine, &errorColumn)) { return; } QDomElement root = domDocument.documentElement(); if (root.isNull()) { return; } if (root.tagName() != "html") { return; } DebugDialog::debug(html); QList<QDomElement> aElements; findA(root, aElements); foreach (QDomElement a, aElements) { // TODO: if multiple hrefs point to the same url this will only find the first one QString href = a.attribute("href"); if (href.isEmpty()) { href = a.attribute("HREF"); } if (href.compare(originalUrl) == 0) { QString text; if (TextUtils::findText(a, text)) { ld.setText(text); break; } else { return; } } } }
void SoTextEdit::keyPressEvent(QKeyEvent *input) { QTextCursor cursor = textCursor(); QString text; QStringList list; QTextBlock block; int position = 0; // int position_in_block = 0; int count; if (input->modifiers() != Qt::NoModifier && input->modifiers() != Qt::ShiftModifier) { input->ignore(); return; } switch (input->key()) { case Qt::Key_Shift: case Qt::Key_Control: case Qt::Key_unknown: input->ignore(); return; case Qt::Key_BraceLeft: cursor.insertText(input->text(), colorFormat(default_color)); std::cout << block_stack << std::endl; return; case Qt::Key_BraceRight: cursor.insertText("}"); position = autoIndent(cursor); cursor.setPosition(position); setTextCursor(cursor); return; case Qt::Key_Return: emit returnPressed(); position = cursor.position(); cursor.movePosition(QTextCursor::NoMove, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor); text = cursor.selectedText(); for (count = 0; count <= text.size(); count++) { if (text[count] == QChar('{')) { block_stack++; } else if (text[count] == QChar('}')) { block_stack--; } } cursor.setPosition(position); if (cursor.atBlockEnd()) { cursor.insertText(input->text()); position++; } else { cursor.setPosition(position + 1, QTextCursor::MoveAnchor); cursor.setPosition(position - 1, QTextCursor::KeepAnchor); text = cursor.selectedText(); for (count = 0; count < exception->count(); count++) { if (text[0] == (*exception)[count][0] || text[1] == (*exception)[count][0]) { cursor.setPosition(position); cursor.insertText(input->text()); position = autoIndent(cursor); cursor.setPosition(position); setTextCursor(cursor); return; } } cursor.setPosition(position, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor); text = cursor.selectedText(); switch (checkType(text)) { case t_TYPE: cursor.insertText(text, colorFormat(type_color)); break; case t_KEYWORD: cursor.insertText(text, colorFormat(keyword_color)); break; case t_DEFAULT: cursor.insertText(text, colorFormat(default_color)); } cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); text = cursor.selectedText(); switch (checkType(text)) { case t_TYPE: cursor.insertText(text, colorFormat(type_color)); break; case t_KEYWORD: cursor.insertText(text, colorFormat(keyword_color)); break; case t_DEFAULT: cursor.insertText(text, colorFormat(default_color)); } cursor.setPosition(position); cursor.insertText(input->text()); position++; } position = autoIndent(cursor); cursor.setPosition(position); setTextCursor(cursor); return; case Qt::Key_Tab: cout << "tab" << endl; position = autoIndent(cursor); cursor.setPosition(position); setTextCursor(cursor); std::cout << "block_stack" << block_stack << std::endl; return; case Qt::Key_Left: if (cursor.atBlockStart()) { cursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor); position = cursor.position(); cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor); text = cursor.selectedText(); count = 0; while (count < text.size()) { if (text[count] == QChar('{')) { block_stack--; } else if (text[count] == QChar('}')) { block_stack++; } count++; } cursor.setPosition(position, QTextCursor::MoveAnchor); } else { cursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor); } setTextCursor(cursor); std::cout << block_stack << std::endl; return; case Qt::Key_Right: if (cursor.atEnd()) { return; } else if (cursor.atBlockEnd()) { cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor); text = cursor.selectedText(); count = 0; while (count < text.size()) { if (text[count] == QChar('{')) { block_stack++; } else if (text[count] == QChar('}')) { block_stack--; } count++; } } cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor); setTextCursor(cursor); std::cout << block_stack << std::endl; return; case Qt::Key_Down: block = cursor.block(); if ((block.blockNumber() + 1 ) == blockCount()) { return; } else { position = cursor.position(); cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor); text = cursor.selectedText(); count = 0; while (count < text.size()) { if (text[count] == QChar('{')) { block_stack++; } else if (text[count] == QChar('}')) { block_stack--; } count++; } cursor.setPosition(position); cursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor); setTextCursor(cursor); std::cout << block_stack << std::endl; return; } case Qt::Key_Up: block = cursor.block(); if (block.blockNumber() == 0) { return; } else { cursor.movePosition(QTextCursor::Up, QTextCursor::MoveAnchor); position = cursor.position(); cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor); text = cursor.selectedText(); count = 0; while (count < text.size()) { if (text[count] == QChar('{')) { block_stack--; } else if (text[count] == QChar('}')) { block_stack++; } count++; } cursor.setPosition(position); setTextCursor(cursor); std::cout << block_stack << std::endl; return; } case Qt::Key_Space: if (cursor.atBlockEnd()) { cursor.insertText(input->text()); setTextCursor(cursor); return; } else { position = cursor.position(); cursor.setPosition(position - 1, QTextCursor::MoveAnchor); cursor.setPosition(position + 1, QTextCursor::KeepAnchor); text = cursor.selectedText(); for (count = 0; count < exception->count(); count++) { if (text[0] == (*exception)[count][0] || text[1] == (*exception)[count][0]) { cursor.setPosition(position); cursor.insertText(input->text()); setTextCursor(cursor); return; } } cursor.setPosition(position, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor); text = cursor.selectedText(); switch (checkType(text)) { case t_TYPE: cursor.insertText(text, colorFormat(type_color)); break; case t_KEYWORD: cursor.insertText(text, colorFormat(keyword_color)); break; case t_DEFAULT: cursor.insertText(text, colorFormat(default_color)); } cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); text = cursor.selectedText(); switch (checkType(text)) { case t_TYPE: cursor.insertText(text, colorFormat(type_color)); break; case t_KEYWORD: cursor.insertText(text, colorFormat(keyword_color)); break; case t_DEFAULT: cursor.insertText(text, colorFormat(default_color)); } cursor.setPosition(position); cursor.insertText(input->text()); setTextCursor(cursor); return; } case Qt::Key_Delete: position = cursor.position(); cursor.deleteChar(); cursor.movePosition(QTextCursor::NoMove, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor); text = cursor.selectedText(); cursor.setPosition(position); for (count = 0; count < exception->count(); count++) { if (text[0] == (*exception)[count][0]) { cursor.setPosition(position - 1); } } cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); text = cursor.selectedText(); break; case Qt::Key_Backspace: if (cursor.atStart()) { return; } else if (cursor.atBlockStart()) { position = cursor.position() - 1; cursor.deletePreviousChar(); cursor.movePosition(QTextCursor::NoMove, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor); text = cursor.selectedText(); std::cout << "debug_text " << qPrintable(text) << std::endl; count = 0; while (count < text.size()) { if (text[count] == QChar('{')) { block_stack--; } else if (text[count] == QChar('}')) { block_stack++; } count++; } cursor.setPosition(position, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor); text = cursor.selectedText(); } else { position = cursor.position() - 1; cursor.deletePreviousChar(); cursor.movePosition(QTextCursor::NoMove, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor); text = cursor.selectedText(); for (count = 0; count < exception->count(); count++) { if (text[0] == (*exception)[count][0]) { cursor.setPosition(position - 1); cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); text = cursor.selectedText(); break; } } if (count == exception->count()) { cursor.setPosition(position); cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); text = cursor.selectedText(); } } std::cout << block_stack <<std::endl; break; default: position = cursor.position() + 1; cursor.insertText(input->text()); cursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); text = cursor.selectedText(); } qDebug() << text; if (text.isEmpty()) { return; } switch (checkType(text)) { case t_TYPE: cursor.insertText(text, colorFormat(type_color)); break; case t_KEYWORD: cursor.insertText(text, colorFormat(keyword_color)); break; case t_DEFAULT: cursor.insertText(text, colorFormat(default_color)); } cursor.setPosition(position); setTextCursor(cursor); return; }
void CharacterHandler::prehandle() { // // Получим необходимые значения // // ... курсор в текущем положении QTextCursor cursor = editor()->textCursor(); // ... блок текста в котором находится курсор QTextBlock currentBlock = cursor.block(); // ... текст блока QString currentBlockText = currentBlock.text().trimmed(); // // Пробуем определить кто сейчас должен говорить // if (currentBlockText.isEmpty()) { QString previousCharacter, character; // // ... для этого ищем предпоследнего персонажа сцены // cursor.movePosition(QTextCursor::PreviousBlock); while (!cursor.atStart() && ScenarioBlockStyle::forBlock(cursor.block()) != ScenarioBlockStyle::SceneHeading) { if (ScenarioBlockStyle::forBlock(cursor.block()) == ScenarioBlockStyle::Character) { // // Нашли предыдущего персонажа // if (previousCharacter.isEmpty()) { previousCharacter = CharacterParser::name(cursor.block().text()); } // // Нашли потенциального говорящего // else { // // Выберем его в списке вариантов // character = CharacterParser::name(cursor.block().text()); if (character != previousCharacter) { break; } } } cursor.movePosition(QTextCursor::PreviousBlock); cursor.movePosition(QTextCursor::StartOfBlock); } // // Показываем всплывающую подсказку // QAbstractItemModel* model = 0; if (!character.isEmpty()) { m_sceneCharactersModel->setStringList(QStringList() << character.toUpper()); model = m_sceneCharactersModel; } else if (!previousCharacter.isEmpty()) { m_sceneCharactersModel->setStringList(QStringList() << previousCharacter.toUpper()); model = m_sceneCharactersModel; } else { model = StorageFacade::characterStorage()->all(); } editor()->complete(model, QString::null); } }
void CharacterHandler::handleOther(QKeyEvent*) { // // Получим необходимые значения // // ... курсор в текущем положении QTextCursor cursor = editor()->textCursor(); // ... блок текста в котором находится курсор QTextBlock currentBlock = cursor.block(); // ... текст блока QString currentBlockText = currentBlock.text(); // ... текст до курсора QString cursorBackwardText = currentBlock.text().left(cursor.positionInBlock()); // ... текущая секция CharacterParser::Section currentSection = CharacterParser::section(cursorBackwardText); // // Получим модель подсказок для текущей секции и выведем пользователю // QAbstractItemModel* sectionModel = 0; // // ... в соответствии со введённым в секции текстом // QString sectionText; switch (currentSection) { case CharacterParser::SectionName: { QStringList sceneCharacters; // // Когда введён один символ имени пробуем оптимизировать поиск персонажей из текущей сцены // if (cursorBackwardText.length() < 2) { cursor.movePosition(QTextCursor::PreviousBlock); while (!cursor.atStart() && ScenarioBlockStyle::forBlock(cursor.block()) != ScenarioBlockStyle::SceneHeading) { if (ScenarioBlockStyle::forBlock(cursor.block()) == ScenarioBlockStyle::Character) { const QString character = CharacterParser::name(cursor.block().text()); const QString characterName = character.toUpper().simplified(); if (character.startsWith(cursorBackwardText, Qt::CaseInsensitive) && !sceneCharacters.contains(characterName)) { sceneCharacters.append(characterName); } } else if (ScenarioBlockStyle::forBlock(cursor.block()) == ScenarioBlockStyle::SceneCharacters) { const QStringList characters = SceneCharactersParser::characters(cursor.block().text()); foreach (const QString& character, characters) { const QString characterName = character.toUpper().simplified(); if (characterName.startsWith(cursorBackwardText, Qt::CaseInsensitive) && !sceneCharacters.contains(characterName)) { sceneCharacters.append(characterName); } } } cursor.movePosition(QTextCursor::PreviousBlock); cursor.movePosition(QTextCursor::StartOfBlock); } } // // По возможности используем список персонажей сцены // if (!sceneCharacters.isEmpty()) { m_sceneCharactersModel->setStringList(sceneCharacters); sectionModel = m_sceneCharactersModel; } else { sectionModel = StorageFacade::characterStorage()->all(); } sectionText = CharacterParser::name(currentBlockText); break; } case CharacterParser::SectionState: { sectionModel = StorageFacade::characterStateStorage()->all(); sectionText = CharacterParser::state(currentBlockText); break; } default: { break; } }