Friday 7 June 2019

Customized dialog for airspace database - part 1

In this post we want to achieve customized attribute dialog that looks as follows:
Note: All data used in this post are only for test purposes only and has nothing to do with real data.

Needless to say customized attribute dialogs are much more better than default ones - we can tailored dialog to meet specific 'needs' of the layer, for example:
  • skip attributes that are automatically filled by database system (added_date in our example) and user don't need to see them
  • add drop-down list that contains available values for certain attribute (e.g. Prohibited, Restricted, Danger etc. for asp_type attribute which prevent form making typos etc.)
  • add script to attribute dialog that will check if entries meet specific needs before saving (e. g. check if upper limit is indeed upper than lower limit) 
In this post we will focus on the layout of the attribute form - adding python script to validate dialog before saving will be done in another post.

As you can se some of the control forms such as FIR, Country, Type are 'highlighted' which means that those attributes are can not be 'NULL'. Notice that 'OK' button is also disabled.
 
First, we need to alter airspace table in following way to achieve 'highlighted' of mandatory fields:
  • remember that asp_type column has default value 'NO_DATA' so far. We have to remove those constraints to  'force' the user to choose type during creation new airspace:
ALTER TABLE airspace
ALTER COLUMN asp_type DROP DEFAULT;
  • attributes (or column values) related to location of airspace (country and FIR) and upper and lower limits should be also mandatory (there is no sense in building airspace database without its vertical limits)  we have to populate data with value before we impose constraint NOT NULL on those columns. Because database at this stage is only for development and educational purposes let's fill this column with some 'test' data that are 'default':

    UPDATE airspace
    SET ctry_iso2 = '-1',
        tmp_lower_limit = 0,
        tmp_lower_limit_uom = 'FL',
        tmp_upper_limit = 1000,
        tmp_upper_limit_uom = 'FL',
        asp_type = 'TEST_TYPE',
        asp_class = 'NO_DATA';
    
    • now, we are ready to add NOT NULL constraint:

    ALTER TABLE airspace
    ALTER COLUMN ctry_iso2 SET NOT NULL,
    ALTER COLUMN tmp_upper_limit SET NOT NULL,
    ALTER COLUMN tmp_upper_limit_uom SET NOT NULL,
    ALTER COLUMN tmp_lower_limit SET NOT NULL,
    ALTER COLUMN tmp_lower_limit_uom SET NOT NULL,
    ALTER COLUMN asp_type SET NOT NULL,
    ALTER COLUMN asp_class SET NOT NULL;
    

    When database is 'prepared', now we are ready to create layout of attribute dialog, which later will be bind with layer.
    Create 'Dialog with Buttons Bottom' in QT Designer,  and create GUI that looks as follows:


    You can drag and drop widgets from Widget Box panel on dialog layout, and modify properties of each widget in Property Editor panel. Remember to name widgets so the names are us the same as appropriate columns in airspace table in database:
    Now, associated airspace layer with 'layout' file that you have just created:
    1. Open QGIS project created in previous post
    2. Open Layer Properties dialog of airspace layer. 
    3. Go to Attribute dialog
    4. Select Provide ui-file and choose file created in Qt Designer:
    5.  We want that user will be able to select value of the attribute from drop-down list (QComboBox widgets). In order to enable this functionality we have select widget from field list and and select widget type Value Map. Then input value that will be stored in database (notice that layer corresponds to table in database) and corresponding description that user will see during feature (airspace) creation:
    6.  Repeat above item for other QComboBox widgets and click OK to approve changes.
    Let's check if customized dialog works!

    Toggle editing mode on airsapace layer and add feature button. After completion of drawing feature, you will see customized attribute dialog. For my example with filled attributes it looks:

    After accepting attribute dialog (OK button) we ad switching of editing mode we will be ask w if we want to save changes to layer airspace. We want, so click OK. Now we can check if what we edited in customized form have been properly inserted into airspace table. Click Identify tool and then click on the airspace you have just added:



    No comments:

    Post a Comment