DzBasicDialog - getting the dialog to always open at just the right size?

3dcheapskate3dcheapskate Posts: 2,689
edited December 1969 in Daz Script Developer Discussion

This one's been driving me nuts for ages, and I've finally got to the stage where I have to ask. I just CANNOT get my dialogues to open with the correct dimensions!

It's got to be something so simple - how on earth do you set a dialog to be just the right size for its contents ? I thought that this would do it...

var wDlg = new DzBasicDialog;
wDlg.caption = "...blah...";
   
var wLInfo = new DzLabel( wDlg );
wLInfo.text = "...blah...";
wDlg.addWidget( wLInfo );

// all the other gidgets

// Set/Limit the dialog size to the minimum required size
//wDlg.maxWidth = wDlg.minWidth;
//wDlg.maxHeight = wDlg.minHeight;
wDlg.width = wDlg.minWidth;
wDlg.height = wDlg.minHeight;

if( !wDlg.exec() ){
     ...blah...
}

...but every time I run the script the dialog seems to open with the exact same dimensions as when I last closed it down!? In both DS3 and DS4.

TIA Pete

Comments

  • SimonJMSimonJM Posts: 5,942
    edited December 1969

    Whilst being an ex-coder I have no claims to knowing the DS scripting language, but my first thoughtd are: what are the values of the properties minWidth and minHeight and also, are the height and width properties in the dDialog case sensitive and need to be Width and Height?

  • BlackFeather1973BlackFeather1973 Posts: 739
    edited December 1969

    Strange indeed, according to the sample scripts it should work.
    Setting width/height doesn't seem to have any effect, although they read out perfectly fine.
    (Must admit i don't actually use the DzBasicDialog myself)

    Using maxHeight and maxWidth instead, seems to work, but then the dialog becomes non-resizable.

    wDlg.maxWidth = wDlg.minWidth;
    wDlg.maxHeight = wDlg.minHeight;
  • rbtwhizrbtwhiz Posts: 2,171
    edited May 2014

    When using DzBasicDialog, you can use the following for unique size and position...

    // Create a basic dialog
    var wDlg = new DzBasicDialog();
    wDlg.caption = "My Dialog";
    
    // Strip the space for a settings key
    var sKey = wDlg.caption.replace( new RegExp(" ", "g"), "" ) + "Dlg";
    
    // Create a widget
    var wLabel = new DzLabel( wDlg );
    wLabel.text = qsTr("This my label.\nThere are many like it, but this one is mine!");
    wDlg.addWidget( wLabel );
    
    // Get the wrapped widget
    var oWidget = wDlg.getWidget();
    
    // Set a [unique] object name on the wrapped dialog widget;
    // this is used for recording position and size separately
    // from all other [uniquely named] DzBasicDialog instances
    oWidget.objectName = sKey;
    
    // Get the minimum height
    var sizeHint = oWidget.minimumSizeHint;
    var nHeight = sizeHint.height;
    
    // Set the fixed height to the minimum
    wDlg.setFixedHeight( nHeight );
    
    // Display the dialog
    wDlg.exec();
    
    // --- Debugging ---
    
    // Create a settings helper
    var oSettingsHelper = new DzSettingsHelper();
    // Define the key to retrieve the dialog geometry from
    var sKeyPath = String("WindowGeometries/%1").arg( sKey );
    // Print the settings
    print( "Width =", oSettingsHelper.get( sKeyPath, "width", -1 ) );
    print( "Height =", oSettingsHelper.get( sKeyPath, "height", -1 ) );
    print( "X =", oSettingsHelper.get( sKeyPath, "x", -1 ) );
    print( "Y =", oSettingsHelper.get( sKeyPath, "y", -1 ) );

    Setting the [unique] objectName for the wrapped dialog widget will cause the width, height, x and y to be recorded to a DzAppSettings. Using DzSettingsHelper, we can retrieve [or even set] the value of those settings fairly easily.

    -Rob

    Post edited by rbtwhiz on
  • SimonJMSimonJM Posts: 5,942
    edited December 1969

    Cheers, Rob. Would I be right in assuming that after 'getting the widget' (the var oWidget = wDlg.getWidget(); line), you could set the various size properties directly with lines like: oWidget.x = 20; and oWidget.height = 720; or am I missing the plot entirely? ;)

  • rbtwhizrbtwhiz Posts: 2,171
    edited May 2014

    In addition to providing standard/consistent buttons for dialogs, one of the things DzBasicDialog does is automatically record and restore the position/size last recorded for the dialog with the given name. When a name isn't specified, all DzBasicDialog instances share the same default name (i.e. 'BasicDlg'). Not setting a unique name is part of why a DzBasicDialog will seem to not keep its own size and/or position, but instead seems influenced by the last script that displays a DzBasicDialog (which also didn't specify a unique name).

    The size and position of a DzBasicDialog is read as the dialog is displayed; reading occurs as a result of the 'show' event. So, except for the initial sizing, manually setting the size and/or position is essentially competing with the recorded settings that will be restored... And since the show event, triggered by exec(), happens after you would be manually setting the size, the recorded settings would win.

    Now, if you look at the debugging code in my example, and then my comment after it, can you see how you might use that to set the size and position of a uniquely named DzBasicDialog instead of get? The key being, setting the values prior to calling exec(). If you do this, the other important thing to understand is that all four keys (i.e. height, width, x, y) must exist for the settings to be 'restored.' If one is missing, they are all discarded. You should realize, however, that doing it this way is a bit of a hack. The settings are actually there so that the user can resize and reposition the dialog and have it show up in the same place and at the same size as when it was last presented to them.

    If the goal is to set a fixed dimension (i.e. the height) based on the minimum [height] of the widgets in the dialog, I'd suggest getting the minimumSizeHint of the wrapped dialog and then setting the fixed height of the DzBasicDialog as I do in the example. The fixed height wins over the recorded settings because a fixed dimension isn't allowed to dynamically change unless you explicitly set another fixed value. When the show event occurs and the settings are read, all four settings exist so they get applied... but the height is not applied as a 'fixed' height so the existing fixed value remains.

    -Rob

    Post edited by rbtwhiz on
  • SimonJMSimonJM Posts: 5,942
    edited December 1969

    I think I follow that, including the background info - thanks! :)

Sign In or Register to comment.