Fit minimal size of a QDockPanel after QLabel content update - python-3.x

I have a QDockWidget with a QGroupBox as the top widget, with QVBoxLayout applied. This contains a QChartView and a QLabel.
The QLabel contains a text composed of several lines of different length.
First, I would like to have the QChartView the same width and height as the QLabel.
Second, as the content of the QLabel is updated by setText(...) in a Slot method, its content changes in height and width. So I would like to update the width and height of the QChartView above it.
For the moment, I only achieved to grow the entire DockPanel to accomodate longer and wider QLabel content, but not to shrink back when this content is narrower and less wide...
I did play with a lot of things, without real success (here a snippet of the Slot method that updates the QLabel content...):
self.fkChartView.hide()
self.pPDetailsLabel.hide()
self.pPDetailsLabel.parent().hide()
self.pPDetailsLabel.setText(self.canvas.grayScottModel.getPearsonPatternDescription(specie=type))
self.pPDetailsLabel.updateGeometry()
self.pPDetailsLabel.parent().updateGeometry()
self.pPDetailsLabel.parent().update()
self.pPDetailsDock.updateGeometry()
self.pPDetailsDock.update()
self.pPDetailsLabel.show()
self.pPDetailsLabel.parent().show()
# print(self.pPDetailsLabel.sizeHint())
self.fkChartView.setMinimumHeight(self.pPDetailsLabel.size().width())
self.fkChartView.setMaximumHeight(self.pPDetailsLabel.size().width())
self.fkChartView.setMinimumWidth(self.pPDetailsLabel.size().width())
self.fkChartView.setMaximumWidth(self.pPDetailsLabel.size().width())
self.fkChartView.updateGeometry()
self.fkChartView.show()
self.fkChartView.updateGeometry()
self.fkChartView.update()
I tried to hide the widgets, so they forget their sizes and/or sizeHint (not sure).
I tried a few updateGeometry() and update() too, but it does not seem to help.
An idea, anyone?

I had it working as intended in the end by doing this in the slot method that updates the content of the QLabel:
self.fkChartView.hide()
self.pPDetailsLabel.setText(self.canvas.grayScottModel.getPearsonPatternDescription(specie=type))
self.pPDetailsLabel.adjustSize()
self.pPDetailsLabel.parent().adjustSize()
# Sets the dimensions of the chart folowing the label width
self.fkChartView.setMinimumHeight(self.pPDetailsLabel.size().width())
self.fkChartView.setMaximumHeight(self.pPDetailsLabel.size().width())
self.fkChartView.setMinimumWidth(self.pPDetailsLabel.size().width())
self.fkChartView.setMaximumWidth(self.pPDetailsLabel.size().width())
self.fkChartView.adjustSize()
self.fkChartView.show()
self.pPDetailsDock.adjustSize()
So I first hide the QChartView, so its dimensions do not interfere,
then all the magic happens thanks to the adjustSize() call. I do this on the QLabel after its content has been updated,
then on its parent (the top QGroupBox in my QDockWidget).
The QChartView still hidden, I force its dimensions,
then show it and
finally adjust the size of the QDockWidget.
It does precisely what I want:
if text is broader, the chart expands but stays square,
if text is narrower, chart shrinks, but stays square.
Total height of the dock adjusts also to fit, without staying the longer and longer it had to be to accomodate for a big former QLabel content.

Related

Create dynamic static grid layer for editable gridstack grid

I am trying to create dashboard edit mode grid with gridstack javscript library.
Here is what I am trying to accomplish which is similar to this Databox platform. This is what it looks like when you edit any of the dashboard to view/edit it in grid mode.
So when I drag the blue border handle and scale the div(or even move with cursor hand) then the around grid(which is background layer with absolute position in reality) is automatically adjusted to match the corresponding div. Like this.
Even if there are multiple widgets there, resizing is working everything perfectly fine.
e.g. if there was any sibling widget then resizing this widget would also resize the sibling widget but can not go beyong 1 * 1 grid which is smallest size there.
Even after resizing both widgets, the background static grid boxes (with yellow border) fills the remaining vacant space similar to what is the concept of dense packing algorithm in css grid.
They are removed when widget is grown and added when it is shrinked.
As it seems super complicated to achieve this, is there any default feature like this with grid-stack library?

Codename one - scrollable layout restrictions

I have done my own version of the PropertyCross Demo (provided in their demo section).
The problem I currently face is the size of the "Recent Search" area. While I have a non-scrollable container, I can easily define the preferred height. As the Box Layout adheres to the preferred size, all is well, with the little issue of not being able to scroll it and see more than one result:
recentSearchContainer = new Container(new BoxLayout(BoxLayout.Y_AXIS)); recentSearchContainer.setPreferredH((int)(this.getContentP‌​ane().getHeight() * 0.1f));
Once I set the container to scrollable, the preferred height gets overwritten and takes up as much space as it needs, taking too much space from the BorderLayout Center piece above it.
How to manipulate the preferred size of scrollable components?
You don't manipulate the preferred size. Scrollables take up more space so if you need them to take up a specific amount of space you need to use the right type of layout which in this case might not be border layout...
Border layout gives NORTH/SOUTH elements their preferred height which might not be what you want. You might want a grid layout which will divide the height 50/50. You might want a table layout where you can define the height in percentages etc.
For those who are interested, here is the solution:
Setup a table layout with a single column and as many rows as you need (similar to box layout y axis or border layout which only north, center and south).
Set the table layout to non-scrollable so it defaults to 100% of your screen.
add the components with height % of the screen they should take up.
those components can be scrollable and will still stick to the height constraint!
// inside a form object, setup the layout
TableLayout tl = new TableLayout(3, 1);
tl.setGrowHorizontally(true);
setScrollable(false);
setLayout(tl);
...
// and add stuff to it
add(tl.createConstraint().heightPercentage(15), labelDesc);
add(tl.createConstraint().heightPercentage(50), compGroup);
add(tl.createConstraint().heightPercentage(35), recentSearchContainer);
Works like a charm!

PyQt - Keeping spacing at zero during window resize, grid layout

I'm making an emacs-esque toy text editor. At startup, there's one large window (a QTextEdit derivative) in the top center of the screen, with a minibuffer (QLineEdit derivative) underneath. Both of the actual editing widgets are contained in the grids of parent classes called Window and MiniWindow (Window also keeps track of a QLabel that appears directly beneath the QTextEdit).
My Window object is at location 1, 1 in the grid, and my MiniWindow object is at 2, 1. I've set content margins to 0 and spacing to 0, which looks great at first, but when I try to grow the window by dragging on the corner, this starts to happen:
As you can see, the screen is divided into two rows (as it should be), but half of the vertical length of the screen is dedicated to each row. What I need is for the top Window to stretch its length during resizing so that it is always adjacent to the MiniWindow underneath. Is there some other option I need to be setting?
Nevermind, got it.
I was having this problem because the QLineEdit object was in the grid of my container class, MiniWindow. The height of a MiniWindow object is free to vary with the window resizing in a way that a QLineEdit alone would not be. The fix was set to the maximumHeight of MiniWindow to approximately the height of a QLineEdit, which is around 16.
Works great now.

FabricJS Scale/Zoom Canvas to fit container

I have my my FabricJS canvas inside a container with a fixed height/width in pixels. On the side I have a dropdown to change the canvas size to various presets. What I would like to do is automatically zoom out whenever I select a size that is either taller and/or wider than the container. So for example, if my container div is 800x600 and I select a canvas size of 1024x768, I want to zoom out so that the entire canvas is still visible. Conversely, I'd like to zoom in up to 100% if I select a smaller size (e.g. 200x200).
I found this example of zooming, but no matter how much you zoom out, the canvas size appears to stay the same. See screenshot:
How can I accomplish this?
As you can see here, the area with the thin red border is the canvas container. The white box is the canvas.
You can use the canvas.setDimensions function to accomplish this. You pass two arguments to it; the first is an object specifying the height and width to change the canvas to and the second indicates whether to change the css, the actual size of the canvas, or both.
For example,canvas.setDimensions({width: 750, height: 750}, {backstoreOnly: true} will set the actual size of the canvas to be 750x750, regardless of the display.
canvas.setDimensions({width: '750px', height: '750px'}, {cssOnly: true}) will set the display size of the canvas to be 750x750, regardless of the actual size.
I'm not 100% sure whether the width or height require a px after it for css or for backstore, but that's a pretty simple thing to check. Also, if no argument is passed for the second parameter, it will change both.

Computing border size of GtkFrame when window isn't visible

I want to compute the left and top border sizes of a GtkFrame. I use the following code:
static void computeborder(GtkWidget *frame)
{
GtkAllocation alloc, child_alloc;
int borderTop, borderLeft;
gtk_widget_get_allocation(frame, &alloc);
GTK_FRAME_GET_CLASS(frame)->compute_child_allocation(GTK_FRAME(frame), &child_alloc);
borderTop = child_alloc.y - alloc.y;
borderLeft = child_alloc.x - alloc.x;
printf("BORDER TOP: %d BORDER LEFT %d\n", borderTop, borderLeft);
}
This is working fine but only if the top-level window hosting the GtkFrame is already shown. However, I need to find out about border sizes before the top-level window which contains my GtkFrame widget is shown.
If the top-level window which contains my GtkFrame hasn't been opened yet, (-1,-1) is returned in alloc and (1,1) is returned in child_alloc. If the top-level window has already been opened, the correct sizes are returned and I can calculate the border sizes.
However, I need to calculate the border sizes when the top-level window is hidden. So how can I calculate the border sizes of my GtkFrame in this case?
I'm using GTK 2.
Those values depends on realization, since they need information about the target display etc. Different displays can in theory have different settings that will make the sizes change.
You can try gtk_widget_realize() or gtk_widget_map() or both on the toplevel window before requesting the sizes

Resources