Way to exclude a char from a word selection - c++

I'm extending a QPlainTextEdit.
When I double click on a word containing a pipe char ex : {"foo"|upper|reverse}
the whole text is surrounded.
I'd like to exclude the pipe char "|" from the selection and don't know what to do
Is there a way to change the behavior of QTextCursor::WordUnderCursor?
I'd like that char to act the same as a space or more generally as an unselectable char.
As stated in QT docs:
Selects the word under the cursor. If
the cursor is not positioned within a
string of selectable characters, no
text is selected.

Currently there is no official way to change the way a text edit finds the word boundaries. See http://bugreports.qt-project.org/browse/QTBUG-150.
You may use their private API to change the behaviour of QTextEngine::atWordSeparator. This way is not recommanded by Qt. The pipe is recognized as word separator in 4.6 but not in 4.5.1 or earlier. I would suggest to update your Qt version, if that is an option. Otherwise you may give your QTextEdit a new QTextLayout with a modified QTextEngine.

Related

ICU Layout sample renders text differently than Microsoft Notepad and Word

I have a bidirectional text
1002 -ابو ماجد الانصاري
Most editors notepad++, notepad etc. show the text as it is shown here. But when I get this text processed through ICU the number is shifted to the right then spaces and hyphen and then Arabic. ICU's sample application layout.exe also shows the number on the right.
I have modified paragraphlayout.cpp and set all possible reordering modes but result is still the same:
Can someone help to configure ICU to provide output as other display engines do.
If I understand correctly, your text 'begins' with the numeric, which is followed by the hyphen and text. Notepad and other editors let you choose the 'writing direction'. If you choose right-to-left, you get the same result as your screenshot,
If you want to maintain left-to-right writing direction, you can set it explicitly
ubidi_setPara(para, "1002 -ابو ماجد الانصاري", ‭25, UBIDI_LTR, NULL, pErrorCode);
or you can embed a UNICODE flag U+202A (LEFT-TO-RIGHT EMBEDDING) into your string that will enforce this direction. If your code is in C++, you can write something like
icu::UnicodeString string_to_layout = "\x202a";
string_to_layout += "1002 -ابو ماجد الانصاري";
and not you can use string_to_layout as input parameter for renderParagraph() (see http://icu-project.org/apiref/icu4c-latest/ubidi_8h.htm).

QTextEdit changes control characters

I've encountered some feature in Qt widgets QTextEdit, QPlainTextEdit, etc that I wouldn't like to encounter. And I actually have no idea how to change this issue.
I want to cipher some text, replacing one unicode character with another unicode character, even if it is control one.
So I delcare a simple string this way:
QChar ch = (QChar)0x20;
QChar ch2 = (QChar)0xA0;
QString str; str.append(ch); str.append(ch2);
These are usual space (0x20) and some unusual No-Break Space (0xA0). In memory the string is absolutely perfect. But if I set it as a text of QTextEdit with ui->txt_ciphered->setPlainText(str); this No-Break Space (0xA0) becomes usual space. I guess that's because they are for some similar purposes, but I still want to get No-Break Space character in TextEdit so that I could copy it etc. like I can in Notepad++ in example.
How could I change this?
QTextEdit does not accept non-break space, why? How can I
still use non-break space with Qt edit controls?
The problem is that QTextEdit::plainText property expressively prevents non-break space from being inserted and converts it to just space character.
plainText : QString
This property gets and sets the text editor's contents as plain text.
Previous contents are removed and undo/redo history is reset when the
property is set.
If the text edit has another content type, it will not be replaced by
plain text if you call toPlainText(). The only exception to this is
the non-break space, nbsp;, that will be converted into standard
space.
Suggestion: use QTextEdit::setText.
If it still does an unwanted conversion or does not treat that symbol right, you need to choose HTML form of test for QTextEdit::setText by inserting the text containing in an envelope of html tags or <html>My text</html>.

Move insertion point to the beginning of the blank line between paragraphs MS Word

I often need to translate a document in MS Word and I do it paragraph by paragraph, the translation text follows each individual paragraph of the original text. What I need is a keyboard shortcut to move the insertion point to the blank space after the following paragraph I need to translate, i.e to move the cursor from the end of the red colored text in the picture to the blank space after the following paragraph ending with "..and call it a day"
Ctrl+Down Arrow shortcut in Word places the insertion point at the beginning of every following paragraph, while I need it placed at the beginning of the blank line above it so I can immediately start typing.
I am looking for a Word shortcut key, regex expression or an autohotkey script that could perform this task, it would come handy to me in doing translation in MS Word.
Thank you for the help!
On MCL's suggestion I've created a simple Autohotkey script to solve the problem, combining Word keyboard shortcuts into one. I also added a code from another script which sets dark red color of the text which is being typed into the original document to make the contrast with the default text color of the original. This is a convenient option for translators which also allows saving only the translated text by using Find function in Word, and removes the need for any further editing of the translated document. Here's the script:
#IfWinActive ahk_class OpusApp
^2::
Send ^{Down}
Send ^{Down}
Send ^{Left}
Send {Enter}
{
oWord := ComObjActive("Word.Application") ; MS Word object
wdColorDarkRed = 128
oWord.Selection.Font.Color := wdColorDarkRed
}
return

SCI_AUTOCSHOW on Scintilla

I have a question, when I use
CallScintilla(SCI_AUTOCSHOW, nLen, (LPARAM)m_strCandidate.c_str())
to show the window about autocompletion, it works when I input a word but it doesn't always work when I press the backspace button, I wonder if there are some conflicts about the key backspace?
use SCI_AUTOCSETSEPARATOR to set the separator character used to separate words in the SCI_AUTOCSHOW list. The default is the space character.

Change tab stop size in rendered HTML using Qt's QLabel class

I'm rendering some HTML in a QT QLabel. The HTML looks like this:
<pre>foo\tbar</pre>
(note that I've put "\t" where there is a tab chracter in the code).
This renders fine, but the tab character appears to be rendered as eight spaces, whereas I want it to be redered as 4. How can I change this without having to change the source HTML?
According to W3 (HTML4):
The horizontal tab character (decimal 9 in [ISO10646] and [ISO88591]) is usually interpreted by visual user agents as the smallest non-zero number of spaces necessary to line characters up along tab stops that are every 8 characters. We strongly discourage using horizontal tabs in preformatted text since it is common practice, when editing, to set the tab-spacing to other values, leading to misaligned documents.
It's implementation-defined, essencially. Most, if not all, browsers/renderers use eight spaces for tabs. This cannot be configured in Qt.
It is, however somewhat trivial to go through your HTML and replace tabs with however many spaces you wish. Write a simple parser for that. Pseudocode:
for each <pre> block {
for each line in block {
position_in_line = 0
for each character in line {
if character is a tab {
remove tab character
do {
add a space character
++position_in_line
} while position_in_line % 8 != 0
} else {
++position_in_line
}
}
}
}
In case you're curious, HTML3 specifies the use of eight-character tabs:
Within <PRE>, the tab should be interpreted to shift the horizontal column position to the next position which is a multiple of 8 on the same line; that is, col := (col+8) mod 8.
While QLabel uses a QTextDocument internally when rendering rich text, it does not allow access to it in it's API. However, since QTextDocument is a QObject, you can try to use
QTextDocument * tl = label->findChild<QTextDocument>();
to get access to it (will work if the QLabel creates the QTextDocument as a (direct or indirect) child of itself).
Once you have a pointer to the text document, you can use the QTextDocument API, e.g. QTextOption::setTabsStop(), to change the tab stops.
The last step is to somehow make the QLabel repaint itself. Probably a call to QWidget::update() suffices, but caching (or worse, recreating the text document) might thward that. In this case, you can register an event listener on the label to adjust the text document just prior to a paintEvent(), but note that the sizeHint() might also change when the tab stops change, so it might be more complicated still.
That said, it's how I'd approach the problem.
Try this:
<pre style="tab-interval:0.5in">foo\tbar</pre>
Could work