Unset the explicit height value of a flex component? - apache-flex

When a container component is declared without setting an specific height value, the contaniner's height will be automatically set to a value which makes possible to display all of its content/children without scrolling (when possible).
After the component is initialized with the proper height, I explicitly change the height value of the container.
My question is, after I change the component's height, is it possible to switch back to the original height that was automatically set based on the containers contents? I'm looking for some way to unset the explicit value of the component's height.

One trick that may be useful for you is to set the height to "NaN", which effectively "unsets" the explicit value you set earlier.

Without storing the original height before it was manually changed, I don't believe this is possible.
What you could do is extend whichever base object you're using as a container to add a new property called OriginalHeight (might as well add OriginalWidth while you're there).
Then you would override the set height function to store the original height in your new OriginalHeight property before it gets changed. When you need to set it back, you would just set the container's height = OriginalHeight.

contaniner's height will be
automatically set to a value
This is not quite automatic. A component will always set the height and width of children; but it can never set the height and width of itself. It can only make suggestions (using the measure method by setting the measuredHeight and measuredWidth property). It is up to the parent container whether it wants to use or ignore those measuredHeight / measuredWidth values.
The Flex Framework containers all have code written for calculating height / width / positioning. IF you are using those, it may seem automatic; but it is actually a fairly manual process. You're just using code someone else wrote.
If you explicitly change the height value of the container, how do you do it? Inside the container (this.height = newvalue ) ? Or does the parent somehow change the height (myContainer.height=newValue)?
Beyond that, it depends what the original height value was. It is possible you can use the measuredHeight value, but I wouldn't count on it. Although, if you are specifying absolute height and absolute width then measure will never be run.
Probably Jason's answer about storing the old value is the best one.

Related

Why does prefWidth in javafx takes an argument called height?

So I've been doing some work for my java class and noticed that Intellij IDEA gives me a warning I haven't seen before, when trying to set Pane's prefWidth. " 'WINDOW_WIDTH' should probably not be passed as parameter 'height' ". I didn't understanding this at first, but after some investigation I saw that Pane's prefWidth function takes an argument called height. And prefHeight takes an argument called width. Why is it like this? Did they accidentally mix up the names or is there something that I don't get?
Here's what the documentation of Node#prefWidth(double) says:
Returns the node's preferred width for use in layout calculations. If the node is resizable, its parent should treat this value as the node's ideal width within its range. If the node is not resizable, just returns its layoutBounds width, which should be treated as the rigid width of the node.
Layout code which calls this method should first check the content-bias of the node. If the node has a vertical content-bias, then callers should pass in a height value that the preferred width should be based on. If the node has either a horizontal or null content-bias, then the caller should pass in -1.
Node subclasses with a vertical content-bias should honor the height parameter whether -1 or a positive value. All other subclasses may ignore the height parameter (which will likely be -1).
Parameters:
height - the height that should be used if preferred width depends on it
Returns:
the preferred width that the node should be resized to during layout The result will never be NaN, nor will it ever be negative.
The documentation for the other width/height methods is similar. The above also mentions a content-bias which is handled by Node#getContentBias():
Returns the orientation of a node's resizing bias for layout purposes. If the node type has no bias, returns null. If the node is resizable and it's height depends on its width, returns HORIZONTAL, else if its width depends on its height, returns VERTICAL.
Resizable subclasses should override this method to return an appropriate value.
Returns:
orientation of width/height dependency or null if there is none
You'll also want to look at the documentation of Node#isResizable() (for brevity, I'm not going to quote it here).
So the reason Node#prefWidth(double) takes a parameter named "height" is because the computation of the preferred width may depend on the height. For instance, the FlowPane layout overrides getContentBias() and has it return the same value as its orientation property. When a FlowPane has an orientation of VERTICAL then the preferred width depends on the available height and if it has an orientation of HORIZONTAL then the preferred height depends on the available width.
Take note that there's a method for the minimum, preferred, and maximum width, same for the minimum, preferred, and maximum height:
Node#minWidth(double) - parameter name is "height"
Node#minHeight(double) - parameter name is "width"
Node#prefWidth(double) - parameter name is "height"
Node#prefHeight(double) - parameter name is "width"
Node#maxWidth(double) - parameter name is "height"
Node#maxHeight(double) - parameter name is "width"
Note: The above methods should probably not be called unless one is creating a layout. If you need the dimensions of a Node then look at its boundsInLocal or boundsInParent properties; if the Node is a Region it will also have read-only width and height properties.
Note: The above methods are different from properties of Region (e.g. Region#prefWidthProperty()).

Flex 4: How to get actual width of a group when using 100% and top/bottom/left/right?

I have an application with width/height of 100%. I have several nested groups within and their measurements are all not set, instead being defined as top=5 or left/right=10, etc.
I'm trying to get the actual with of a group that should be 390. I've set the swf object size to be 400 from the html embed code, and the group is inside another group that is width=100%. The group has a left=5 and right=5 so the width should be 400-10. When I display the .width and .measuredWidth of this group, the width is always 400.
I'm fairly new to flex/flash. Do I need to have explicitly set widths in order to get the width of child containers? Is there something I'm doing wrong here?
Thanks!
Percentage size of Component is calculated at parent's updateDisplayList(),so it's available only through width & height.
Methods getExplicitOrMeasuredWidth/Height() may return following:
Explicit width/height
Or
Measured width/height when mode is set to Wrap. This is calculated during Component's measure().
If you do not plan to override measure or updateDisplayList you simply should not use getExplicit... at all.
Use the getExplicitOrMeasuredWidth() method of the UIComponent. This returns the explicit width if set (e.g., width="700") or the measured width (in pixels) if not. If a percentage width is set, it returns the actual measured width.
It turns out there was an inner group with a path that was throwing the layout off. When I removed this element, the .width property worked as it had before.
One thing that surprised me is that .measuredWidth and getExplicitOrMeasuredWidth() both returned 10 when .width returned 390.
I believe that it's returning the right value. If you have an application A, width 400. Then you put group B inside it with width 100% and some padding. Group B really is 400, because it's extended all the way to the edges of the application. However, by giving it left/right/padding/whatever, it, in turn, can adjust its children so they aren't so close to the edges. You may need to look for the maximum width of group B's children to get what you want.
Update:
According to documentation:
http://livedocs.adobe.com/flex/3/html/help.html?content=size_position_5.html
Using constraint-based layouts overrides width and percentWidth. Try measuredWidth, though it may not be filled until the component has had time to go through its measure() phase during invalidation...

QTreeWidget set height of each row depending on content

I want to make editable cells with multi-lines content in QTreeWidget and I use for this purpose QPlainTextEdit as a delegate. I need to set proper size to all rows that switching between editing and displaying went smooth, without any visible changes.
rect = textEdit.blockBoundingRect(textEdit.firstVisibleBlock())
With this I can find out the height I need to set for the row, but I missing the place where I can do it.
How can I set proper height to QTreeWidget's rows on initialization stage and how to handle it's changes?
You need to reimplement delegate's sizeHint(). It will automatically handle row's height and width.
And note, that QTreeWidget::uniformRowHeight property must be false in this case, though it will slow tree element rendering if it contains many rows.

how do I find the height of the children in a TabNavigator, without the height of the Tabs?

I'm having sizing issues with a TabNavigator. The direct children of the TabNavigator are Canvases, and within these I am adding Images. I'm trying to resize the images to fit within the Canvas without scrollbars. The Canvas height is set to 100% of the parent, which is the TabNav. Then I'm setting the image.height = parent.height * .9
The result is that Flex is generating scrollbars because the images are too high. It appears that the root cause is that the height property of the TabNavigator is the height of the entire component, including the height of the tabs. I'm assuming therefore it also contains that little strip of space between the tabs and the children of the TabNavigator.
This makes sense, but is there a property that returns only the height of the children?
I'm aware of the scrollbar policy properties and I've experimented with those. I know I can also try a different multiplier for the image size to get it to fit. It just seems like there should be a property to get the size of the space for the children.
Looking through the source, there's a "contentHeight" property that seems to be the thing you want.
The method ends with this line:
return unscaledHeight - tabBarHeight - vmTop - vmBottom;
which seems to be exactly what you're looking for. Unfortunately this property is protected, as is the case with many things we developers want out of the Flex library. :)
Your quickest solution using this would be to sub-class TabNavigator and expose this property publically; or you could just do this calculation yourself:
image.height = (parent.height - TabNavigator(parent).tabBar.height) * .9);

In Flex, how can I get the dimensions of a childless canvas component at runtime?

One of my components looks like this:
<mx:Canvas id="grid" width="100%" height="100%"></mx:Canvas>
On creationComplete, I load some spirte that I want to scale and position based on the dimensions of the canvas to create a custom grid layout, but when I access the dimensions of 'grid' I get 0 and 0. Is there any way to get the dimensions without assigning absolute values?
I can't check this at the moment but can you access the measuredHeight and measuredWidth?
My understanding is that unless the Canvas contains one or more DisplayObject children, it will always report its width and height properties as 0, regardless of the percentage sizings you may have applied to it.
You could always add an empty dummy DisplayObject to the Canvas, but that wouldn't be very elegant. Depending on how you've planned to implement your custom grid, it's possible you'll have to rethink the design...
Is it possibly created but not yet displayed (e.g. visible)? Since final size and shape is derived, it doesn't happen until the thing actually needs to be drawn. Width and height are documented to be the values actually in use - there are even events for when they change.
Worst case, try trapping it out in the canvas Resize event.
have you tried binding the width and height of the Canvas to its parent? If it is 100%, than it will have the same size as its parent and you could either bind or size your Sprite (UIComponent) based on the parent container of the Canvas.

Resources