Adding new lines and spaces to the old Qt PieMenu - c++

I need to use the old Qt pieMenu, I know it's deprecated, But I've found the source code here, and the installations instructions here(they are in the zip file as well)
I've successfully compiled and ran the examples, but my problem is that I need to add spaces and manipulate the font in the text of the pie menu
for example in the editor.cpp example, these lines insert items to the pie menu with the first parameter as the displayed text :
markMenu->insertItem("Cut", this, SLOT(cut()));
markMenu->insertItem("Del", this, SLOT(del()));
markMenu->insertItem("Copy", this, SLOT(copy()));
I tried setting the lines like this as a test :
markMenu->insertItem("item cut", this, SLOT(cut()));
markMenu->insertItem(tr("Del item"), this, SLOT(del()));
markMenu->insertItem("test on item", this, SLOT(copy()));
and the results are like in the photo(everything after the space character is not displayed)
then I used the '\t' instead of using space directly, it worked as one space instead of a tab.
markMenu->insertItem("test\ton\titem", this, SLOT(copy()));
but when trying to add a new line to the text things didn't work right.
I tried using '\n' but didn't work for me.
Another issue, I was trying to change the font settings of the menu, I tried :
markMenu->setFont(QFont("Arial", 9, 5, true)); // size 9, weight 5, italic
but this didn't affect the font, I tried styleSheets as well but no success.
Any ideas how can I display the new liens and adjust the font of the text?

In the link provided by OP, scrolling to bottom, the source code can be found:
painter.drawText(center.x() + x
- metrics.width(itemText(i)) / 2,
center.y() + y, itemText(i));
Now, a look into the Qt doc. of QPainter::drawText() provides the explanation:
This function does not handle the newline character (\n), as it cannot break text into multiple lines, and it cannot display the newline character. Use the QPainter::drawText() overload that takes a rectangle instead if you want to draw multiple lines of text with the newline character, or if you want the text to be wrapped.
...and a possible fix as well. (Emphasize by me.)
As the source code is available, this can be fixed.
To achieve a proper pre-calculation of boundary rectangle, QFontMetrics can be used.
The answer to How to automatically increase/decrease text size in label in Qt shows an example for text boundary determining with QFontMetrics which even considers automatic word-wrapping.

Related

Moving over tab spaces in ncurses

I am creating a basic text editor using ncurses. It can display text fine, but navigating with arrow keys causes a problem when tabs are encountered. Calling move(y, x) will freely move the cursor onto a tab space, where most text editors will jump to the next character. Is there functionality within ncurses to jump over tab spaces or do I need to find a way to do it myself?
You have to do it yourself: wmove moves to the given coordinates, ignoring the way characters are displayed on the screen.
If a destructive (filling with spaces) tab works for your application, then you could use waddch:
If ch is a tab, newline, carriage return or backspace, the
cursor is moved appropriately within the window:
Tabs are considered to be at every eighth column. The
tab interval may be altered by setting the TABSIZE
variable.
For an editor, you probably do not want that behavior (though it probably would be helpful to use the TABSIZE feature when displaying text).

Proper code indentation for tabs in LibreOffice/OpenOffice and Ms Word

I have some text documents (.doc and .odf) with portions of colored code appearing inside.
This code was copied as RTF from Notepad++, that's how it got colored.
However, in Notepad++ (and in many IDEs as well), the line wrap function works makes the indented code look better when in does not fit and goes to the next line.
In LibreOffice/OpenOffice and Ms Word it's possible to achieve a similar line wrap with the "increase indent" button.
So, what I'd like to do, is to automatically replace the tabs (or 4 spaces, if you like) with proper indents. Or make the tabs behave like I expect them to. Is it possible? Thanks.
Here's a visualization of the problem
I achieved what I want through some (not that many) manual steps.
find out what is the maximum number of tabs (or sets of 4 spaces), say it's 3
open the search and replace window, input 3 tabs (or 12 spaces), and click "Find All"
now all groups of 3 tabs are selected, and you are working on all the lines with maximum indentation
park or close the search and replace window, click 3 times on the "Increase indent" button (or set the left indentation in the paragraph style menu)
delete the selected groups of 3 tabs
open the search and replace window, input 2 tabs (or 8 spaces), and click "Find All"
park or close the search and replace window, click 2 times on the "Increase indent" button
delete the selected groups of 2 tabs
open the search and replace window, input 1 tabs (or 4 spaces), and click "Find All"
park or close the search and replace window, click 1 time on the "Increase indent" button
delete the selected single tabs
Now you have a nice code indentation.
If you are using Microsoft Word, then there's no "find all", but there is a way to apply paragraph styles directly from the search and replace menu. But the steps are a little different.
First decide how wide a single indentation should be (e.g. 0.5 cm)
open the find and replace window, input 3 tabs (or 12 spaces) in the Find bar
leave the Replace bar empty, but click on it
if you don't see the Search options group, click More
click on Format
click on Paragraph
set a left indentation of 3 * the indentation width you want (e.g. 1.5 cm)
click Replace All, the paragraph style will be applied but the tabs/spaces will NOT be removed
click on the empty Replace bar (again)
click No Formatting
click on Replace All (again)
now the tabs/spaces will be deleted
Rinse and repeat until you get a nice indentation.
If you are using Python (or if you want to keep your white spaces) then instead of deleting the tabs (or spaces), you can replace them with a placeholder character you don't use in the rest of the code, say £ and replace them back in one pass when you are done. However, you'll get a skewed indentation.
I guess there's a way to do this with macros, but this was good enough for me.

long text printed on screen creating a horizontal line and going of of bounds while printing

I am working on textarea where the user can enter text to create the letter. now if they keep typing continuously, it creates a lengthy string and once they save it in database, the same letter is available for print. Now while printing, it shows a very long horizontal bar. While printing, text is being cut off
I tried using the WRAP function of Function, but that creates extra Tags where they are not needed, Although in letter, there are couple of instances where they are needed. using the Following Code replaces all  .
#Wrap(qryGetLetterDetails.letter,'([[:print:]])( )([[:print:]])','\1 \3','all'),75)#
My Next Approach was using the Custom Lib Function WRAP available on CFLIB.org
Again with this Tag, it generated the source HTML on screen while wrapping the Letter. It did not worked as expected.
I tried searching CSS ways to force to break word but in vain. I am using IE7+ versions only, No other Browser Support is provided.
can you try with
#ReplaceList(qryGetLetterDetails.letter, "#chr(10)#,#chr(13)#", "<br />")#

How do tabs work for text boxes?

I'm making a game gui api and I'm wondering how to implement tabs. I'm using freetype for text. When I try to render '\t' It looks like a square. I'm wondering how tabs are implemented because they are not a fixed width.
Thanks
For a fixed-width font you could compute how many spaces to the next tab stop, but the general solution is to stop rendering when you hit a tab, move to the next tabstop, and then render the text that comes after the tab character starting from there. Where the tabstops are is up to you, but a good default is probably something like every 8 ems.
The simplest strategy is to swap a tab out for a some number of spaces. I.e. when the user pushes tab, pretend they pushed tab four times instead. (Or just print out four spaces, whatever works for you)

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