Exemplo n.º 1
0
void FindDialog::replace()
{
	QString text = m_find_string->text();
	if (text.isEmpty()) {
		return;
	}

	QTextEdit* document = m_documents->currentDocument()->text();
	QTextCursor cursor = document->textCursor();
	Qt::CaseSensitivity cs = m_ignore_case->isChecked() ? Qt::CaseInsensitive : Qt::CaseSensitive;
	if (!m_regular_expressions->isChecked()) {
		if (QString::compare(cursor.selectedText(), text, cs) == 0) {
			cursor.insertText(m_replace_string->text());
			document->setTextCursor(cursor);
		}
	} else {
		QRegExp regex(text, cs, QRegExp::RegExp2);
		QString match = cursor.selectedText();
		if (regex.exactMatch(match)) {
			match.replace(regex, m_replace_string->text());
			cursor.insertText(match);
			document->setTextCursor(cursor);
		}
	}

	find();
}
Exemplo n.º 2
0
void FindDialog::replaceAll()
{
	QString text = m_find_string->text();
	if (text.isEmpty()) {
		return;
	}

	QTextDocument::FindFlags flags;
	if (!m_ignore_case->isChecked()) {
		flags |= QTextDocument::FindCaseSensitively;
	}
	if (m_whole_words->isChecked()) {
		flags |= QTextDocument::FindWholeWords;
	}

	// Count instances
	int found = 0;
	QTextEdit* document = m_documents->currentDocument()->text();
	QTextCursor cursor = document->textCursor();
	cursor.movePosition(QTextCursor::Start);
	forever {
		cursor = document->document()->find(text, cursor, flags);
		if (!cursor.isNull()) {
			found++;
		} else {
			break;
		}
	}
	if (found) {
		if (QMessageBox::question(this, tr("Question"), tr("Replace %n instance(s)?", "", found), QMessageBox::Yes | QMessageBox::No) == QMessageBox::No) {
			return;
		}
	} else {
		QMessageBox::information(this, tr("Sorry"), tr("Phrase not found."));
		return;
	}

	// Replace instances
	QTextCursor start_cursor = document->textCursor();
	forever {
		cursor = document->document()->find(text, cursor, flags);
		if (!cursor.isNull()) {
			cursor.insertText(m_replace_string->text());
			document->setTextCursor(cursor);
		} else {
			break;
		}
	}
	document->setTextCursor(start_cursor);
}
Exemplo n.º 3
0
void MainWindow::updateTexts() {
    QObject *sender_obj = QObject::sender();
    QTextEdit *sender = dynamic_cast<QTextEdit*>(sender_obj);
    //QTextCursor cursor = sender->textCursor();
    int oldPos(sender->textCursor().selectionStart());


    ui->bit7TextEdit->blockSignals(true);
    ui->ASCIItextEdit->blockSignals(true);
    ui->UCStextEdit->blockSignals(true);
    ui->UTF8textEdit->blockSignals(true);
    ui->textEdit->blockSignals(true);

    ui->bit7TextEdit->setPlainText(  myString.toBit7() );
    //ui->ASCIItextEdit->setPlainText( myString.toASCII() );
    //ui->UCStextEdit->setPlainText(   myString.toUCS() );
    //hui->UTF8textEdit->setPlainText(  myString.toUTF8() );
    ui->textEdit->setPlainText(      myString.toQString() );

    ui->bit7TextEdit->blockSignals(false);
    ui->ASCIItextEdit->blockSignals(false);
    ui->UCStextEdit->blockSignals(false);
    ui->UTF8textEdit->blockSignals(false);
    ui->textEdit->blockSignals(false);
    //updateAllowed = true;

    //sender->setTextCursor(cursor);
    QTextCursor cursor(sender->textCursor());
    cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, oldPos);
    sender->setTextCursor(cursor);
}
Exemplo n.º 4
0
void FindDialog::find(bool backwards)
{
	QString text = m_find_string->text();
	if (text.isEmpty()) {
		return;
	}

	QTextDocument::FindFlags flags;
	if (!m_ignore_case->isChecked()) {
		flags |= QTextDocument::FindCaseSensitively;
	}
	if (m_whole_words->isChecked()) {
		flags |= QTextDocument::FindWholeWords;
	}
	if (backwards) {
		flags |= QTextDocument::FindBackward;
	}

	QTextEdit* document = m_documents->currentDocument()->text();
	QTextCursor cursor = document->document()->find(text, document->textCursor(), flags);
	if (cursor.isNull()) {
		cursor = document->textCursor();
		cursor.movePosition(!backwards ? QTextCursor::Start : QTextCursor::End);
		cursor = document->document()->find(text, cursor, flags);
	}

	if (!cursor.isNull()) {
		document->setTextCursor(cursor);
	} else {
		QMessageBox::information(this, tr("Sorry"), tr("Phrase not found."));
	}
}
Exemplo n.º 5
0
void MainWindow::on_insertTitle_clicked()
{
    QTextEdit *pEdit = textEditor.editor;
    QTextCursor cursor = pEdit->textCursor();

    //set alignment center
    pEdit->setAlignment(Qt::AlignHCenter);
    groupCheck(0);

    //insert title
    cursor.insertText("“    ”学院 2011—2012学年第一学期\n");

    //insert paper info
    cursor.insertText("《           》期末考试试卷(  卷) 考核形式(  卷)\n");

    //set alignment left
    pEdit->setAlignment(Qt::AlignLeft);
    groupCheck(2);

    QFont font = pEdit->font();
    font.setBold(true);
    font.setPointSize(16);
    pEdit->setFont(font);
    pEdit->setTextCursor(cursor);
    pEdit->setFocus();
}
Exemplo n.º 6
0
SEXP
qt_qsetCursorPosition_QTextEdit(SEXP x, SEXP pos)
{
    QTextEdit *te = unwrapSmoke(x, QTextEdit);
    QTextCursor tc = te->textCursor();
    tc.setPosition(asInteger(pos));
    te->setTextCursor(tc);
    return R_NilValue;
}
void QWordCompleter::replaceCurrentWord(QString text)
{
    QTextEdit* textEdit = qobject_cast<QTextEdit*>(widget());
    QTextCursor textCursor = textEdit->textCursor();
    textCursor.movePosition(QTextCursor::StartOfWord);
    textCursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
    if (text == "void") text += " " + name + "() {\n\n}";
    else if (text == "if") text += " ( ) {\n\n} else {\n\n}";
    textCursor.insertText(text);
    textEdit->setTextCursor(textCursor);
}
Exemplo n.º 8
0
 /*!
    * \class SplitCellCommand
    * \author Anders Fernström
    * \date 2006-04-26
    *
    * \brief Split the cell
    */
  void SplitCellCommand::execute()
  {
    try
    {
      if( document()->getCursor()->currentCell() )
      {
        if( typeid( *document()->getCursor()->currentCell() ) == typeid( TextCell ) ||
          typeid( *document()->getCursor()->currentCell() ) == typeid( InputCell ) )
        {
          // extraxt text
          QTextEdit* editor = document()->getCursor()->currentCell()->textEdit();
          if( editor )
          {
            QTextCursor cursor = editor->textCursor();
            cursor.movePosition( QTextCursor::End, QTextCursor::KeepAnchor );
            QTextDocumentFragment text = cursor.selection();
            cursor.removeSelectedText();

            // add new cell
            if( typeid( *document()->getCursor()->currentCell() ) == typeid( TextCell ) )
            {
              AddCellCommand addcellCommand;
              addcellCommand.setApplication( application() );
              addcellCommand.setDocument( document() );
              addcellCommand.execute();
            }
            else
            {
              // inputcell
              CreateNewCellCommand newcellCommand( "Input" );
              newcellCommand.setApplication( application() );
              newcellCommand.setDocument( document() );
              newcellCommand.execute();
            }

            // add text to new cell
            QTextEdit* newEditor = document()->getCursor()->currentCell()->textEdit();
            QTextCursor newCursor = newEditor->textCursor();
            newCursor.insertFragment( text );
            newCursor.movePosition( QTextCursor::Start );
            newEditor->setTextCursor( newCursor );

            // update document
            document()->setChanged( true );
          }
        }
      }
    }
    catch( exception &e )
    {
      string str = string("SplitCellCommand(), Exception: ") + e.what();
      throw runtime_error( str.c_str() );
    }
  }
void	QsvTextOperationsWidget::setTextCursor(QTextCursor c)
{
	QTextEdit *t = qobject_cast<QTextEdit*>(parent());
	if (t) {
		t->setTextCursor(c);
	} else {
		QPlainTextEdit *pt = qobject_cast<QPlainTextEdit*>(parent());
		if (pt) {
			pt->setTextCursor(c);
		}
	}
}
Exemplo n.º 10
0
bool FindDialog::find() {
    QTextEdit* textEdit = getCurrentTextEdit();
    if (textEdit == NULL) return false;
    int from = textEdit->textCursor().selectionEnd();
    QTextCursor cursor = textEdit->document()->find(ui->findString->text(), from);
    if (!cursor.isNull()) {
        textEdit->setTextCursor(cursor);
        return true;
    }
    else {
        return false;
    }
}
Exemplo n.º 11
0
    void editMenuActivated(QAction *action)
    {
        QWidget* w = qApp->focusWidget();
        QTextEdit* te = qobject_cast<QTextEdit*>(w);
        QLineEdit* le = qobject_cast<QLineEdit*>(w);

        if (action == selectAction) {
            highlighting = this;
            return;
        }
        highlighting = 0;

        if (action == copyAction) {
            if (te) {
                if (te->hasEditFocus()) {
                    te->copy();
                } else {
                    QTextCursor c = te->textCursor();
                    te->selectAll();
                    te->copy();
                    te->setTextCursor(c);   // reset selection
                }
            } else if (le) {
                if (le->hasEditFocus()) {
                    le->copy();
                } else {
                    qApp->clipboard()->setText(le->text());
                }
            }
        } else if (action == pasteAction) {
            // assumes clipboard is not empty if 'Paste' is able to be
            // activated, otherwise the line/textedit might be cleared
            // without pasting anything back into it
            if (te) {
                if (!te->hasEditFocus())
                    te->clear();
                te->paste();
                if (!te->hasEditFocus()) {
                    te->moveCursor(QTextCursor::Start);
                    te->ensureCursorVisible();
                }
            } else if (le) {
                if (!le->hasEditFocus())
                    le->clear();
                le->paste();
                if (!le->hasEditFocus())
                    le->home(false);
            }
        }
    }
Exemplo n.º 12
0
int
QUimTextUtil::deleteSelectionTextInQTextEdit( enum UTextOrigin origin,
                                              int former_req_len,
                                              int latter_req_len )
{
    QTextEdit *edit = static_cast<QTextEdit *>( mWidget );
    QTextCursor cursor = edit->textCursor();
    if ( ! cursor.hasSelection() )
        return -1;

    bool cursor_at_beginning = false;
    int current = cursor.position();
    int start = cursor.selectionStart();
    if ( current == start )
        cursor_at_beginning = true;

    QString text = cursor.selectedText();
    int len = text.length();
    int end = start + len;
    if ( origin == UTextOrigin_Beginning ||
         ( origin == UTextOrigin_Cursor && cursor_at_beginning ) ) {
        if ( latter_req_len >= 0 ) {
            if ( len > latter_req_len )
                end = start + latter_req_len;
        } else {
            if (! ( ~latter_req_len
                    & ( ~UTextExtent_Line | ~UTextExtent_Full ) ) )
                return -1;
        }
    } else if ( origin == UTextOrigin_End ||
                ( origin == UTextOrigin_Cursor && !cursor_at_beginning ) ) {
        if ( former_req_len >= 0 ) {
            if ( len > former_req_len )
                start = end - former_req_len;
        } else {
            if (! ( ~former_req_len
                    & ( ~UTextExtent_Line | ~UTextExtent_Full ) ) )
                return -1;
        }
    } else {
        return -1;
    }
    cursor.setPosition( start );
    cursor.setPosition( end, QTextCursor::KeepAnchor );
    edit->setTextCursor( cursor );
    cursor.deleteChar();

    return 0;
}
Exemplo n.º 13
0
void MainWindow::insertQuestionTypeTitle(const QString &title)
{
    QTextEdit *pEdit = textEditor.editor;
    QTextCursor cursor = pEdit->textCursor();

    cursor.insertText(tr("\n"));
    pEdit->setTextCursor(cursor);

    cursor.insertText(title+" ");
    pEdit->setTextCursor(cursor);

    //set font of title and info
    QFont font = pEdit->font();
    QTextCharFormat fmt;
    font.setBold(true);
    font.setPointSize(13);
    fmt.setFont(font);
    cursor.select(QTextCursor::BlockUnderCursor);
    cursor.mergeCharFormat(fmt);

    cursor.select(QTextCursor::WordUnderCursor);
    cursor.insertText("\n");
    pEdit->setTextCursor(cursor);

    //set font of questions
    cursor.insertText(" ");
    font.setBold(false);
    font.setPointSize(10);
    fmt.setFont(font);
    cursor.select(QTextCursor::BlockUnderCursor);
    cursor.mergeCharFormat(fmt);

    cursor.select(QTextCursor::WordUnderCursor);
    cursor.deletePreviousChar();
    pEdit->setTextCursor(cursor);
}
Exemplo n.º 14
0
void FindDialog::replace()
{
	QString text = m_find_string->text();
	if (text.isEmpty()) {
		return;
	}

	QTextEdit* document = m_documents->currentDocument()->text();
	QTextCursor cursor = document->textCursor();
	Qt::CaseSensitivity cs = m_ignore_case->isChecked() ? Qt::CaseInsensitive : Qt::CaseSensitive;
	if (QString::compare(cursor.selectedText(), text, cs) == 0) {
		cursor.insertText(m_replace_string->text());
		document->setTextCursor(cursor);
	}
	find();
}
Exemplo n.º 15
0
void VBoxLogSearchPanel::findCurrent (const QString &aSearchString)
{
    mButtonsNextPrev->setEnabled (0, aSearchString.length());
    mButtonsNextPrev->setEnabled (1, aSearchString.length());
    toggleWarning (!aSearchString.length());
    if (aSearchString.length())
        search (true, true);
    else
    {
        QTextEdit *browser = mViewer->currentLogPage();
        if (browser && browser->textCursor().hasSelection())
        {
            QTextCursor cursor = browser->textCursor();
            cursor.setPosition (cursor.anchor());
            browser->setTextCursor (cursor);
        }
    }
}
Exemplo n.º 16
0
void SourceViewer::doHighlightText(const QString &text, bool caseSensitive)
{
	qDebug() << "doHighlightText : text=" << text;

	workingHighlightText_ = true;
	do
	{
		QTextEdit *logTextEdit = getCurrentSourceEdit();
		if(logTextEdit == NULL)
			break;

		int orgValueVert = logTextEdit->verticalScrollBar()->value();
		int orgValueHor = logTextEdit->horizontalScrollBar()->value();
		QTextCursor orgCursor = logTextEdit->textCursor();
		QTextDocument::FindFlags flags = 0x0;
		if(caseSensitive)
			flags |=  QTextDocument::FindCaseSensitively;

		QList<QTextEdit::ExtraSelection> extraSelections;
		logTextEdit->moveCursor(QTextCursor::Start);

		QColor fgColor = QColor("white");
		QColor bgColor = QColor("#C32438");

		while(logTextEdit->find(text, flags))
		{
			QTextEdit::ExtraSelection extra;
			extra.format.setForeground(fgColor);
			extra.format.setBackground(bgColor);
			extra.cursor = logTextEdit->textCursor();
			extraSelections.append(extra);
		}

		logTextEdit->setExtraSelections(extraSelections);
		logTextEdit->setTextCursor(orgCursor);
		logTextEdit->verticalScrollBar()->setValue(orgValueVert);
		logTextEdit->horizontalScrollBar()->setValue(orgValueHor);
	} while(false);
	workingHighlightText_ = false;
}
Exemplo n.º 17
0
  /*!
     * \class TextCursorInsertLink
   * \author Anders Fernström
   * \date 2005-12-05
     *
     * \brief Command for inserting an link
     */
  void TextCursorInsertLink::execute()

  {

//    QTextCursor cursor( document()->getCursor()->currentCell()->textCursor() );
    if( !cursor.isNull() )
    {
      if( cursor.hasSelection() )
      {
        QDir dir;
        QString currentfilepath = document()->getFilename();
        if( !currentfilepath.isEmpty() && !currentfilepath.isNull() )
          dir.setPath( QFileInfo(currentfilepath).absolutePath() );

        // check if dir exist
        if( !dir.exists() )
          return;
        // get the relative link path
        QString relativepath = dir.relativeFilePath( filepath_ );

        // create html code for the link and insert it to the document'
        QString text = cursor.selection().toHtml();
        int fragmentStart = text.indexOf( "<!--StartFragment-->",
          0, Qt::CaseInsensitive ) + 20;
        int fragmentEnd = text.indexOf( "<!--EndFragment-->",
          fragmentStart, Qt::CaseInsensitive );

        QString html = text.mid( fragmentStart, fragmentEnd - fragmentStart );
        QString htmlcode = "<a href=\"" + relativepath + "\">" +
          html + "</a>";
        cursor.insertFragment( QTextDocumentFragment::fromHtml( htmlcode ));

        // set the cursor, so there is no selection
                QTextEdit *editor = document()->getCursor()->currentCell()->textEdit();
        if( editor )
          editor->setTextCursor( cursor );
      }
    }
  }
Exemplo n.º 18
0
void VBoxLogSearchPanel::search (bool aForward,
                                 bool aStartCurrent)
{
    QTextEdit *browser = mViewer->currentLogPage();
    if (!browser) return;

    QTextCursor cursor = browser->textCursor();
    int pos = cursor.position();
    int anc = cursor.anchor();

    QString text = browser->toPlainText();
    int diff = aStartCurrent ? 0 : 1;

    int res = -1;
    if (aForward && (aStartCurrent || pos < text.size() - 1))
        res = text.indexOf (mSearchString->text(),
                            anc + diff,
                            mCaseSensitive->isChecked() ?
                            Qt::CaseSensitive : Qt::CaseInsensitive);
    else if (!aForward && anc > 0)
        res = text.lastIndexOf (mSearchString->text(), anc - 1,
                                mCaseSensitive->isChecked() ?
                                Qt::CaseSensitive : Qt::CaseInsensitive);

    if (res != -1)
    {
        cursor.movePosition (QTextCursor::Start,
                             QTextCursor::MoveAnchor);
        cursor.movePosition (QTextCursor::NextCharacter,
                             QTextCursor::MoveAnchor, res);
        cursor.movePosition (QTextCursor::NextCharacter,
                             QTextCursor::KeepAnchor,
                             mSearchString->text().size());
        browser->setTextCursor (cursor);
    }

    toggleWarning (res != -1);
}
Exemplo n.º 19
0
static void setCursorPos(QTextEdit &edit, int pos)
{
    QTextCursor cursor(edit.document());
    cursor.setPosition(pos);
    edit.setTextCursor(cursor);
}
Exemplo n.º 20
0
int
QUimTextUtil::deletePrimaryTextInQTextEdit( enum UTextOrigin origin,
                                            int former_req_len,
                                            int latter_req_len )
{
    QTextEdit *edit = static_cast<QTextEdit *>( mWidget );
    QString text = edit->toPlainText(); // excluding preedit string
    int len = text.length();

    int preedit_len = mIc->getPreeditString().length();

    QTextCursor cursor = edit->textCursor();
    int precedence_len = cursor.position();
    int following_len = len - precedence_len;

    int former_del_start;
    int latter_del_end;
    switch ( origin ) {
    case UTextOrigin_Cursor:
        former_del_start = 0;
        if ( former_req_len >= 0 ) {
            if ( precedence_len > former_req_len )
                former_del_start = precedence_len - former_req_len;
        } else {
            if (! ( ~former_req_len
                    & ( ~UTextExtent_Line | ~UTextExtent_Full ) ) )
                return -1;
        }
        latter_del_end = len + preedit_len;
        if ( latter_req_len >= 0 ) {
            if ( following_len > latter_req_len )
                latter_del_end = precedence_len + preedit_len + latter_req_len;
        } else {
            if (! ( ~latter_req_len
                    & ( ~UTextExtent_Line | ~UTextExtent_Full ) ) )
                return -1;
        }
        break;

    case UTextOrigin_Beginning:
        former_del_start = 0;
        latter_del_end = precedence_len + preedit_len;
        if ( latter_req_len >= 0 ) {
            if ( precedence_len < latter_req_len ) {
                if ( following_len >= ( latter_req_len - precedence_len ) )
                    latter_del_end = preedit_len + latter_req_len;
                else
                    latter_del_end = len + preedit_len;
            }
        } else {
            if (! ( ~latter_req_len
                    & ( ~UTextExtent_Line | ~UTextExtent_Full ) ) )
                return -1;
            latter_del_end = len + preedit_len;
        }
        break;

    case UTextOrigin_End:
        former_del_start = precedence_len;
        latter_del_end = len + preedit_len;
        if ( former_req_len < 0 ) {
            if (! ( ~former_req_len
                    & ( ~UTextExtent_Line | ~UTextExtent_Full ) ) )
                return -1;

            former_del_start = 0;
        }
        break;

    case UTextOrigin_Unspecified:
    default:
        return -1;
    }

    // don't call setText() to avoid flicker unlike QLineEdit
    int end_index = latter_del_end - preedit_len;
    if ( precedence_len != end_index ) {
        cursor.setPosition( precedence_len );
        cursor.setPosition( end_index, QTextCursor::KeepAnchor );
        edit->setTextCursor( cursor );
        cursor.deleteChar();
    }
    if ( precedence_len != former_del_start ) {
        cursor.setPosition( precedence_len );
        cursor.setPosition( former_del_start, QTextCursor::KeepAnchor );
        edit->setTextCursor( cursor );
        cursor.deleteChar();
    }

    return 0;
}
Exemplo n.º 21
0
void QTextEditProto::setTextCursor(const QTextCursor &cursor)
{
  QTextEdit *item = qscriptvalue_cast<QTextEdit*>(thisObject());
  if (item)
    item->setTextCursor(cursor);
}
Exemplo n.º 22
0
void osk_dialog_frame::Create(const std::string& title, const std::u16string& message, char16_t* init_text, u32 charlimit, u32 options)
{
	state = OskDialogState::Open;

	static const auto& lineEditWidth = []() {return QLabel("This is the very length of the lineedit due to hidpi reasons.").sizeHint().width(); };

	if (m_dialog)
	{
		m_dialog->close();
		delete m_dialog;
	}

	m_dialog = new custom_dialog(false);
	m_dialog->setModal(true);

	// Title
	m_dialog->setWindowTitle(qstr(title));

	// Message
	QLabel* message_label = new QLabel(QString::fromStdU16String(message));

	// Text Input Counter
	const QString text = QString::fromStdU16String(std::u16string(init_text));
	QLabel* inputCount = new QLabel(QString("%1/%2").arg(text.length()).arg(charlimit));

	// Ok Button
	QPushButton* button_ok = new QPushButton("Ok", m_dialog);

	// Button Layout
	QHBoxLayout* buttonsLayout = new QHBoxLayout;
	buttonsLayout->setAlignment(Qt::AlignCenter);
	buttonsLayout->addStretch();
	buttonsLayout->addWidget(button_ok);
	buttonsLayout->addStretch();

	// Input Layout
	QHBoxLayout* inputLayout = new QHBoxLayout;
	inputLayout->setAlignment(Qt::AlignHCenter);

	// Text Input
	if (options & CELL_OSKDIALOG_NO_RETURN)
	{
		QLineEdit* input = new QLineEdit(m_dialog);
		input->setFixedWidth(lineEditWidth());
		input->setMaxLength(charlimit);
		input->setText(text);
		input->setFocus();

		if (options & CELL_OSKDIALOG_NO_SPACE)
		{
			input->setValidator(new QRegExpValidator(QRegExp("^\\S*$"), this));
		}

		connect(input, &QLineEdit::textChanged, [=](const QString& text)
		{
			inputCount->setText(QString("%1/%2").arg(text.length()).arg(charlimit));
			SetOskText(text);
			on_osk_input_entered();
		});
		connect(input, &QLineEdit::returnPressed, m_dialog, &QDialog::accept);

		inputLayout->addWidget(input);
	}
	else
	{
		QTextEdit* input = new QTextEdit(m_dialog);
		input->setFixedWidth(lineEditWidth());
		input->setText(text);
		input->setFocus();
		input->moveCursor(QTextCursor::End);
		m_text_old = text;

		connect(input, &QTextEdit::textChanged, [=]()
		{
			QString text = input->toPlainText();

			if (text == m_text_old)
			{
				return;
			}

			QTextCursor cursor = input->textCursor();
			const int cursor_pos_new = cursor.position();
			const int cursor_pos_old = cursor_pos_new + m_text_old.length() - text.length();

			// Reset to old state if character limit was reached
			if ((u32)m_text_old.length() >= charlimit && (u32)text.length() > charlimit)
			{
				input->blockSignals(true);
				input->setPlainText(m_text_old);
				cursor.setPosition(cursor_pos_old);
				input->setTextCursor(cursor);
				input->blockSignals(false);
				return;
			}

			int cursor_pos = cursor.position();

			// Clear text of spaces if necessary
			if (options & CELL_OSKDIALOG_NO_SPACE)
			{
				int trim_len = text.length();
				text.remove(QRegExp("\\s+"));
				trim_len -= text.length();
				cursor_pos -= trim_len;
			}

			// Crop if more than one character was pasted and the character limit was exceeded
			text.chop(text.length() - charlimit);

			// Set new text and block signals to prevent infinite loop
			input->blockSignals(true);
			input->setPlainText(text);
			cursor.setPosition(cursor_pos);
			input->setTextCursor(cursor);
			input->blockSignals(false);

			m_text_old = text;

			inputCount->setText(QString("%1/%2").arg(text.length()).arg(charlimit));
			SetOskText(text);
			on_osk_input_entered();
		});

		inputLayout->addWidget(input);
	}

	inputLayout->addWidget(inputCount);

	QFormLayout* layout = new QFormLayout(m_dialog);
	layout->setFormAlignment(Qt::AlignHCenter);
	layout->addRow(message_label);
	layout->addRow(inputLayout);
	layout->addRow(buttonsLayout);
	m_dialog->setLayout(layout);

	// Events
	connect(button_ok, &QAbstractButton::clicked, m_dialog, &QDialog::accept);

	connect(m_dialog, &QDialog::accepted, [=]
	{
		on_osk_close(CELL_MSGDIALOG_BUTTON_OK);
	});

	connect(m_dialog, &QDialog::rejected, [=]
	{
		on_osk_close(CELL_MSGDIALOG_BUTTON_ESCAPE);
	});

	// Fix size
	m_dialog->layout()->setSizeConstraint(QLayout::SetFixedSize);
	m_dialog->show();
}
Exemplo n.º 23
0
void MainWindow::formatQuestions(const PACKEDQUESTIONS &qset)
{

    TestAssistance *pt = TestAssistance::get();
    QTextEdit *pEdit = textEditor.editor;
    QTextCursor cursor = pEdit->textCursor();

    //set alignment center
    pEdit->setAlignment(Qt::AlignHCenter);
    groupCheck(0);

    //set font of title and info
    QFont font = pEdit->font();
    QTextCharFormat fmt;
    font.setBold(true);
    font.setPointSize(14);
    fmt.setFont(font);
    cursor.select(QTextCursor::BlockUnderCursor);
    cursor.mergeCharFormat(fmt);

    //insert school name
    cursor.insertText(tr("%1\n").arg(pt->getPaperInfo().strSchool));

    //insert title
    cursor.insertText(tr("“%1”专业(学院) 2011—2012学年第一学期\n").arg(pt->getPaperInfo().major));


    //insert paper info
    cursor.insertText(tr("《%1》期末考试试卷(%2卷) 考核形式(%3卷)\n").arg(pt->getPaperInfo().strCourse)
                      .arg(pt->getPaperInfo().strPaperType).arg(((pt->getPaperInfo().bOpenOrClose)?"开":"闭")));

    //insert author
    cursor.insertText(tr("作者:%1\n").arg(pt->getPaperInfo().strAuthor));

    pEdit->setTextCursor(cursor);

    //set alignment left
    pEdit->setAlignment(Qt::AlignLeft);
    groupCheck(2);

    //get judge
    QString text;
    int type = 0;

    const std::vector<QUESTION> &j = qset.judge;
    if(!j.empty()){
        insertQuestionTypeTitle(tr("%1.判断题").arg(digitToChinese(type+1)));
        type++;

        for(size_t i = 0; i != j.size(); ++i){
            cursor.insertText(tr("%1.").arg(i+1) + j[i].txt + "\n");
            if(!j[i].imgBytes.isEmpty()){
                QImage img;
                img.loadFromData(j[i].imgBytes, "PNG");
                cursor.insertImage(img);
                cursor.insertText("\n\n");
            }
            pEdit->setTextCursor(cursor);
        }
    }

    //get choice
    text.clear();
    const std::vector<QUESTION> &c = qset.choice;
    if(!c.empty()){
        insertQuestionTypeTitle(tr("%1.选择题").arg(digitToChinese(type+1)));
        type++;

        for(size_t i = 0; i != c.size(); ++i){
            cursor.insertText(tr("%1.").arg(i+1) + c[i].txt + "\n");
            if(!c[i].imgBytes.isEmpty()){
                QImage img;
                img.loadFromData(c[i].imgBytes, "PNG");
                cursor.insertImage(img);
                cursor.insertText("\n\n");
            }
            else
                cursor.insertText("\n");
            pEdit->setTextCursor(cursor);
        }
    }

    //get fill
    text.clear();
    const std::vector<QUESTION> &f = qset.fill;
    if(!f.empty()){
        insertQuestionTypeTitle(tr("%1.填空题").arg(digitToChinese(type+1)));
        type++;

        for(size_t i = 0; i != f.size(); ++i){
            cursor.insertText(tr("%1.").arg(i+1) + f[i].txt + "\n");
            if(!f[i].imgBytes.isEmpty()){
                QImage img;
                img.loadFromData(f[i].imgBytes, "PNG");
                cursor.insertImage(img);
                cursor.insertText("\n\n");
            }
            pEdit->setTextCursor(cursor);
        }
    }

    //get calc
    text.clear();
    const std::vector<QUESTION> &l = qset.calc;
    if(!l.empty()){
        insertQuestionTypeTitle(tr("%1.计算题").arg(digitToChinese(type+1)));
        type++;

        for(size_t i = 0; i != l.size(); ++i){
            cursor.insertText(tr("%1.").arg(i+1) + l[i].txt + "\n");
            if(!l[i].imgBytes.isEmpty()){
                QImage img;
                img.loadFromData(l[i].imgBytes, "PNG");
                cursor.insertImage(img);
                cursor.insertText("\n\n");
            }
            pEdit->setTextCursor(cursor);
        }
    }

    //get ans
    text.clear();
    const std::vector<QUESTION> &a = qset.ans;
    if(!a.empty()){
        insertQuestionTypeTitle(tr("%1.问答题").arg(digitToChinese(type+1)));
        type++;

        for(size_t i = 0; i != a.size(); ++i){
            cursor.insertText(tr("%1.").arg(i+1) + a[i].txt + "\n");
            if(!a[i].imgBytes.isEmpty()){
                QImage img;
                img.loadFromData(a[i].imgBytes, "PNG");
                cursor.insertImage(img);
                cursor.insertText("\n\n");
            }
            else
                cursor.insertText("\n");
            pEdit->setTextCursor(cursor);
        }
    }
}