How to elide a rich text after multiple lines in Qt? - qt

I am trying to elide rich text (with html type links) using Qt and Pyside. The text strings will be something like this:
u"<a href='FRIEND' style=\"text-decoration: none\">" \
u"<font color=#1abc9c>{}</font></a> agregó <a href='LINK' " \
u"style=\"text-decoration: none\"><font color=#1abc9c>{}</font></a>" \
u" a su biblioteca".format(EntityMocks.friendMock.display_name,
EntityMocks.assetMock1.title)
The documentation of Qt explicitly states that this cannot be done using rich text so I am stripping my text of all the html tags before passing it to Qt´s elider. I am also trying to accomplish this for texts that will elide on multiple lines. This is my test code for just two lines:
class DoubleElidedText(QLabel):
def __init__(self, *args):
super(DoubleElidedText, self).__init__(*args)
def setText(self, text):
self.setToolTip(text)
self.update()
metrics = QFontMetrics(self.font())
elide = metrics.elidedText(strip_tags(text), Qt.ElideRight, self.width()*2 - self.width()/5)
if metrics.width(elide) > self.width():
self.setMinimumHeight(metrics.height()*2)
else:
self.setMinimumHeight(metrics.height())
texto = u"{}".format(elide)
super(DoubleElidedText, self).setText(texto)
class MLStripper(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.reset()
self.fed = []
def handle_data(self, d):
self.fed.append(d)
def get_data(self):
return ''.join(self.fed)
def strip_tags(html):
s = MLStripper()
s.feed(html)
return s.get_data()
This behavior (stripping the html tags) has become unacceptable for my software.
I am trying to develop a way to elide the rich text that will be displayed in a QLabel with multiple lines. Doing this manually raises one particular issue that I am not being able to solve:
What is the length of the displayed lines? (It will depend on the white space added at the end of every line).
Am I addressing this issue correctly or is there some QtMagic that is missing in my research?

Not a Python solution but I assume that my approach from https://stackoverflow.com/a/66412942/3907364 can be ported to Python very easily.
In short: Use a QTextDocument to parse and represent the rich text and then use a QTextCursor to keep removing characters from it until it has the desired width.
Note this does not specifically deal with multiline Strings (in fact my answer focuses on single-line Strings), but I would assume extending it to multiline input should be doable.

Related

paste0 regular and italicized text in R

I need to concatenate two strings within an R object: one is just regular text; the other is italicized. So, I tried a lot of combinations, e.g.
paste0(" This is Regular", italic( This is Italics))
The desired result should be:
This is Regular This is Italics
Any ideia on how to do it?
Thanks!
In plot labels, you can use expressions, see mathematical annotation :
plot(1,xlab=expression("This is regular"~italic("this is italic")))
To provide an string for which an HTML parser will recognise the need to render the text in Italics, wrap the text in <i> and </i>. For example: "This is plain text, but <i>this is in Italics</i>.".
However, most HTML processors will assume that you want your text to appear as-is and will escape their input by default. This means that the special meanings of certain characters - including < and > will be "turned off". You need to tell the processor not to do this. How you do that will depend on context. I can't tell you that because you haven't given me context.
Are you for example, writing to a raw HTML file? (You need do nothing.) Are you writing to a Markdown file? If so, how? In plain text or in a rendered chunk? Are you writing a caption to a graphic? (Waldi has suggested a solution.) Etc, etc....

Create new emphasis command R Markdown

In R Markdown, to make a text bold, we just need to do:
**code**
The the word code shows in bold.
I was wondering if there is a way to create a new command, let's say:
***code***
That would make the text highlighted?
Thanks!
It is not easily possible to create new markup, but one can change the way existing markup commands are rendered. Text enclosed by three stars is interpreted as emphasized strong emphasis. So one has to change that interpretation and change it to something else. One way to do so is via pandoc Lua filters. We just have to match on pandoc's internal representation of emphasized strong text and convert it to whatever we want:
function Strong (strong)
-- if this contains only one element, and if that element
-- is emphasized text, convert it to highlighted text.
local element = #strong.content == 1 and strong.content[1]
if element and element.t == 'Emph' then
table.insert(element.content, 1, pandoc.RawInline('html', '<mark>'))
table.insert(element.content, pandoc.RawInline('html', '</mark>'))
return element.content
end
end
The above works for HTML output. One would have to define what "highlighted text" means for each targeted format.
See this and this question for other approaches to the problem, and for details of how to use the filter with R Markdown.

Escape text in Bokeh Tools

I have a Bokeh TapTool that opens a webpage
TapTool(callback=OpenURL(url='/mypage?key=#name'))
However, the text in the name column of my ColumnDataSource contains unsafe text. I would like to escape this text. Typically I use tornado.escape.url_escape for this.
Is there a way to apply a function like url_escape automatically or do I have to maintain a separate column of escaped text in my ColumnDataSource?
As of 0.12.10 there is no functionality to escape field values in the client, nor are CustomJSTransform models yet applicable to OpenURL. My suggestion would be a CustomJS callback for the tap tool that does the escaping and opens the URL (a couple of lines of JS total).

Implementing syntax highlighting for markdown titles in PySide/PyQt

I am trying to implement a syntax highlighter for markdown for my project in PySide. The current code covers the basic, with bold, italic, code blocks, and some custom tags. Below is an extract of the relevant part of the current code.
What is blocking me right now is how to implement the highlighting for titles (underlined with ===, for the main title, or --- for sub-titles). The method that is used by Qt/PySide to highlight the text is highlightBlock, which processes only one line at a time.
class MySyntaxHighlighter(QtGui.QSyntaxHighlighter):
def highlightBlock(self, text):
# do something with this line of text
self.setCurrentBlockState(0)
startIndex = 0
if self.previousBlockState() != 1:
startIndex = self.blockStartExpression.indexIn(text)
while startIndex >= 0:
endIndex = self.blockEndExpression.indexIn(
text, startIndex)
...
There is a way to recover the previousBlockState, which is useful when a block has a defined start (for instance, the ~~~ syntax at the beginning of a code-block). Unfortunately, there is nothing that defines the start of a title, except for the underlining with === or --- that take place on the next line. All the examples I found only handle cases where there is a defined start of the expression, and so that the previousBlockState gives you an information (as in the example above).
The question is then: is there a way to recover the text of the next line, inside the highlightBlock? To perform a look-ahead, in some sense.
I though about recovering the document currently being worked on, and find the current block in the document, then find the next line and make the regular expression check on this. This would however break if there is a line in the document that has the exact same wording as the title. Plus, it would become quite slow to systematically do this for all lines in the document. Thanks in advance for any suggestion.
If self.currentBlock() gives you the block being highlighted, then:
self.currentBlock().next().text()
should give you the text of the following block.

Multiline title on a JavaFx Chart

Is there some means - short of extending the JavaFX charting base class(es) - to specify a multi-line title string?
As seen in the screenshot the title is the only element 'wanting' more space.
I had seen some references to using a newline '\n' and even a pipe character '|' in the string but they apparently do not work for the title.
I just threw this in a sample I had and it worked.
chart.setTitle("really long title...........\n.............and some more ");
Label l = (Label)chart.lookup(".chart-title");
l.setWrapText(true);
The \n sets the break point if I don't want it at the limit.
As you can see it's just a Label, the hard part is getting it.
You can also use a css file with the selector. I think it's already centered.
.chart-title{
-fx-wrap-text : true;
-fx-alignment : center;
}

Resources