Operation on words and character case
Solved
General and Desktop
-
My editor is using QPlainTextEdit and highligter created by Volker Krause and Dominik Haumann. How do operations on words: select one word (cursor is on word, word can contain Unicode characters), to lower selected text, to upper, not necesarry invert case, capital first letter od words.
To lower and to upper selection is independent from editor, it is operation on QString.
But how do with words bounds? QTextDocument can say where word begins and ends?
It is depend from highligter? for example in C idents starts with letter or underscore and can contains digit whereas in natural languages (.txt) words can't.editor->textCursor().select(QTextCursor::WordUnderCursor); or editor->textCursor().select(QTextCursor::Document);
not works, and how select any portion of text?
because cursor is object, not pointer, follow works fine:QTextCursor cursor = editor->textCursor(); cursor.select(QTextCursor::WordUnderCursor); editor->setTextCursor(cursor);
-
Possible solution:
void MainWindow::selectWord() { if (tabWidget->currentWidget()) { CodeEditor* editor = dynamic_cast<CodeEditor*>(tabWidget->currentWidget()); QTextCursor cursor = editor->textCursor(); cursor.select(QTextCursor::WordUnderCursor); editor->setTextCursor(cursor); } } void MainWindow::toLower() { if (tabWidget->currentWidget()) { CodeEditor* editor = dynamic_cast<CodeEditor*>(tabWidget->currentWidget()); QString selected = editor->textCursor().selectedText(); if (selected=="") { selectWord(); selected = editor->textCursor().selectedText(); } editor->insertPlainText(selected.toLower()); } } void MainWindow::toUpper() { if (tabWidget->currentWidget()) { CodeEditor* editor = dynamic_cast<CodeEditor*>(tabWidget->currentWidget()); QString selected = editor->textCursor().selectedText(); if (selected=="") { selectWord(); selected = editor->textCursor().selectedText(); } editor->insertPlainText(selected.toUpper()); } }