QComboBox elided text on selected item - qt5

So, I have a QComboBox.
If the currentText() is too long for the widget then I want to show an ellipsis.
Like this :
So :
void MyComboBox::paintEvent(QPaintEvent * )
{
QStylePainter painter(this);
QStyleOptionComboBox opt;
initStyleOption(&opt);
painter.drawComplexControl(QStyle::CC_ComboBox, opt);
QRect rect = this->rect();
//this is not ideal
rect.setLeft(rect.left() + 7);
rect.setRight(rect.width() - 15);
//
QTextOption option;
option.setAlignment(Qt::AlignVCenter);
QFontMetrics fontMetric(painter.font());
const QString elidedText = QAbstractItemDelegate::elidedText(fontMetric, rect.width(), Qt::ElideRight, this->currentText());
painter.drawText( rect, elidedText, option);
}
This is working flawlessy.
The problem is the code in between the comments, because I am hardcoding the distances from the left and right border. It makes me cringe.
The result without that code is:
Does anybody know a more general way to do this, without hardcoding?
Thank you

Where the text should be drawn exactly depends on the used style. You can get information about (some of) the positioning of subelements with QStyle::subControlRect. The subcontrol that matches the combo box text best seems to be QStyle::SC_ComboBoxEditField, though if the item has an icon, this needs to be taken into account as well. If the items do not have icons, you can go with
QRect textRect = style()->subControlRect(QStyle::CC_ComboBox, &opt, QStyle::SC_ComboBoxEditField, this);
QFontMetrics fontMetric(painter.font());
const QString elidedText = QAbstractItemDelegate::elidedText(fontMetric, textRect.width(), Qt::ElideRight, this->currentText());
opt.currentText = elidedText;
painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
You might want to have a look at how e.g. QFusionStyle::drawControl works for details.
In general, if you want all your combo boxes to elide the text, you should consider implementing your own QProxyStyle and only override MyStyle::drawControl for QStyle::CE_ComboBoxLabel.

This is the solution I've been using:
void CustomComboBox::paintEvent(QPaintEvent * /*event*/)
{
QStyleOptionComboBox opt;
initStyleOption(&opt);
QStylePainter p(this);
p.drawComplexControl(QStyle::CC_ComboBox, opt);
QRect textRect = style()->subControlRect(QStyle::CC_ComboBox, &opt, QStyle::SC_ComboBoxEditField, this);
opt.currentText = p.fontMetrics().elidedText(opt.currentText, Qt::ElideRight, textRect.width());
p.drawControl(QStyle::CE_ComboBoxLabel, opt);
}
This approach is very similar to a combination of your sample code and the snippet E4z9 suggested. I just thought I'd include the whole method for others coming here in the future.

Related

Custom CollapsingTopAppBar Jetpack Compose

The essence of the problem is that I want to write my own version of the AppBar that would include content as another Compose function. After looking at the source code of the current CollapsingTopAppBar implementation, I saw the following lines:
#Composable
private fun TwoRowsTopAppBar(
...
scrollBehavior: TopAppBarScrollBehavior?
) {
...
val pinnedHeightPx: Float = 64.dp
val maxHeightPx: Float = 152.dp
LocalDensity.current.run {
pinnedHeightPx = pinnedHeight.toPx()
maxHeightPx = maxHeight.toPx()
}
// Sets the app bar's height offset limit to hide just the bottom title area and keep top title
// visible when collapsed.
SideEffect {
if (scrollBehavior?.state?.heightOffsetLimit != pinnedHeightPx - maxHeightPx) {
scrollBehavior?.state?.heightOffsetLimit = pinnedHeightPx - maxHeightPx
}
}
...
Surface(...) {
Column {
TopAppBarLayout(
...
heightPx = pinnedHeightPx
...
)
TopAppBarLayout(
...
heightPx = maxHeightPx - pinnedHeightPx + (scrollBehavior?.state?.heightOffset
?: 0f),
...
)
}
}
}
As I understand it, scrollBehavior is used to handle the collapse and expansion behavior. In the current implementation, just constant values are put in heightOffsetLimit. And since I need my appbar implementation to be able to contain content of any size, I need to somehow know the size of this content in advance and put this value in heightOffsetLimit.
I have already written the code for my AppBar, so that it also contains content. But since I can't pass the height value of the content to scrollBehavior, the AppBar doesn't collapse to the end.
you need to calculate the height that the appbar will have before drawing it into the screen. I have followed this issue and solved my problem with the last solution. hope it helps:
Get height of element Jetpack Compose
use the content you can put (ex. an image or a huge text) as the MainContent
use your appbar as the DependentContent and use the size given in lambda to give the height to your appbar
finally set placeMainContent false as I believe you don't need to draw the image (or any other composable) directly in a box
and you will good to go

How to expose PDFSharp XBrushes to be picked from GUI?

First time working with PDFSharp, I have a win form that enables the user to select a font and size of it for the PDF to be created. Also the app draws some rectangles on the page with currently hardcoded color like this:
rect = new XRect(5, 300, 25, 15);
gfx.DrawRectangle(XBrushes.SeaShell, rect); //SET COLOR TO RECT
tf.Alignment = XParagraphAlignment.Center;
tf.DrawString("No", invoiceItemsHeaderFont, XBrushes.Black, rect,
XStringFormats.TopLeft);
I want the user to be able to pick the color via a drop down. How can I load all of the colors from XBrushes. to a list and then parse it as such, this is one of the attempts:
foreach(XBrushes xbrush in typeof(XBrushes)
{
colorsRect.Add(xbrush.ToString());
}
Thank you all!
I know this is a bit late but maybe this could help someone else.
I take it you are just looking to get a collection of the different brushes?
I looked at the XBrushes class and all its public static properties seem to be brushes. You could use reflection to go through all its properties and get the values of the ones that are brushes.
Something like:
public static IEnumerable<XSolidBrush> Brushes
{
get
{
return typeof(XBrushes).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static)
.Select(p => p.GetValue(null))
.Where(b=>b.GetType()==typeof(XSolidBrush))
.Cast<XSolidBrush>();
}
}

Creating a flexible UI contianer for an image and a label

I thought this would be like pretty simple task to do, but now I have tried for hours and cant figure out how to get around this.
I have a list of friends which should be displayed in a scrollable list. Each friend have a profile image and a name associated to him, so each item in the list should display the image and the name.
The problem is that I cant figure out how to make a flexible container that contains both the image and the name label. I want to be able to change the width and height dynamically so that the image and the text will scale and move accordingly.
I am using Unity 5 and Unity UI.
I want to achieve the following for the container:
The width and height of the container should be flexible
The image is a child of the container and should be left aligned, the height should fill the container height and should keep its aspect ratio.
The name label is a child of the contianer and should be left aligned to the image with 15 px left padding. The width of the text should fill the rest of the space in the container.
Hope this is illustrated well in the following attached image:
I asked the same question here on Unity Answers, but no answers so far. Is it really possible that such a simple task is not doable in Unity UI without using code?
Thanks a lot for your time!
Looks like can be achieved with layout components.
The image is a child of the container and should be left aligned, the height should fill the container height and should keep its aspect ratio.
For this try to add Aspect Ratio Fitter Component with Aspect mode - Width Controls Height
The name label is a child of the container and should be left aligned to the image with 15 px left padding. The width of the text should fill the rest of the space in the container.
For this you can simply anchor and stretch your label to the container size and use BestFit option on the Text component
We never found a way to do this without code. I am very unsatisfied that such a simple task cannot be done in the current UI system.
We did create the following layout script that does the trick (tanks to Angry Ant for helping us out). The script is attached to the text label:
using UnityEngine;
using UnityEngine.EventSystems;
[RequireComponent (typeof (RectTransform))]
public class IndentByHeightFitter : UIBehaviour, UnityEngine.UI.ILayoutSelfController
{
public enum Edge
{
Left,
Right
}
[SerializeField] Edge m_Edge = Edge.Left;
[SerializeField] float border;
public virtual void SetLayoutHorizontal ()
{
UpdateRect ();
}
public virtual void SetLayoutVertical() {}
#if UNITY_EDITOR
protected override void OnValidate ()
{
UpdateRect ();
}
#endif
protected override void OnRectTransformDimensionsChange ()
{
UpdateRect ();
}
Vector2 GetParentSize ()
{
RectTransform parent = transform.parent as RectTransform;
return parent == null ? Vector2.zero : parent.rect.size;
}
RectTransform.Edge IndentEdgeToRectEdge (Edge edge)
{
return edge == Edge.Left ? RectTransform.Edge.Left : RectTransform.Edge.Right;
}
void UpdateRect ()
{
RectTransform rect = (RectTransform)transform;
Vector2 parentSize = GetParentSize ();
rect.SetInsetAndSizeFromParentEdge (IndentEdgeToRectEdge (m_Edge), parentSize.y + border, parentSize.x - parentSize.y);
}
}

Nested Grid in GWT

I need to develop a control which is similar to the Nested Grid in the Smart GWT.
User will be having a column for expansion images, when user clicking on the image in a particular row, a sub grid has to be opened there itself. Here all remaining rows needs to move down.
How can i achieve that functionality? Can anybody give me some clues so that i can proceed.
I have already a grid which is a celltable with custom header(with search functionality implemented).
Thanks,
Saritha.
Create your nested widget (myNestedWidget) that you want to show. It should have a CSS style "position: absolute", unless your grid is added to the LayoutPanel (or similar), in which case you can position your widget directly. Let's call the parent widget of your grid gridParentWidget.
In your CellTable add the following handler:
myTable.addCellPreviewHandler(new Handler<myObject>() {
#Override
public void onCellPreview(CellPreviewEvent<myObject> event) {
if ("click".equals(event.getNativeEvent().getType())) {
if (event.getColumn() == 0) {
int top = myTable.getRowElement(event.getIndex()).getAbsoluteBottom();
int left = myTable.getRowElement(event.getIndex()).getAbsoluteLeft();
myNestedWidget.getElement().getStyle().setTop(top, Unit.PX);
myNestedWidget.getElement().getStyle().setLeft(left, Unit.PX);
gridParentWidget.add(myNestedWidget);
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
#Override
public void execute() {
int height = myNestedWidget.getOffsetHeight();
myTable.getRowElement(event.getIndex()).getStyle().setHeight(height + "px");
]
});
}
}
});
}
This is obviously an outline of the solution. The details of the implementation may vary slightly depending on which widgets you use for your parent widget and your nested widget. If you change z-indexes somewhere, you have to take it into account too. You also need to make sure that your nested widget fits into the width of your grid, or you'll need to wrap it in a ScrollPanel and set a width to it explicitly.

SWT Composite maximum size

I have a ScrolledComposite, the contents of which are being truncated. I have Googled and am aware that it is a known issue on Windows.
The only suggested workaround I can find is to use the canvas.scroll functionality.
Given the age of the issue, I was wondering if there is a nicer workaround?
Thank you!
(EDIT: At the time of writing, the link was: http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet48.java?view=markup&content-type=text%2Fvnd.viewcvs-markup&revision=HEAD)
(the link you posted gave a 400 Error)
Not sure if my issue was the same, but I ran into a truncation issue with ScrolledComposite as well. The problem was that when I resized the Composite to be scrolled and the Scrollbar became visible, the controls didn't account for the space taken up by the scrollbar. To solve this, I added a kind-of recursive bit of code to my Resize listener on the scrolled composite:
After you have set the size of your content composite, check if the scrolledComposite's scrollbar (getVerticalBar() for example) has just become visible. If so, send a new Resize event to your listener. Here's a snippet from my code...
public void handleEvent(Event event)
{
int newWidth = scrolledComposite.getSize().x;
boolean hasScroll = false;
ScrollBar scrollBar = scrolledComposite.getVerticalBar();
if (scrollBar.isVisible())
{
hasScroll = true;
newWidth -= scrolledComposite.getVerticalBar().getSize().x;
}
newWidth -= 8;
Point size = contentComposite.computeSize(newWidth, SWT.DEFAULT);
contentComposite.setSize(size);
int scroll_multiplier = size.y / 50;
scrollBar.setIncrement(scroll_multiplier);
/**
* If the scroll bar became visible because of the resize, then
* we actually need to resize it again, because of the scroll
* bar taking up some extra space.
*/
if (scrollBar.isVisible() && !hasScroll)
{
scrolledComposite.notifyListeners(SWT.Resize, null);
}
}
Hope this helps!
Edit: wow I didn't notice the date of the OP. Hope this ends up helping someone either way...