Show a dialog with Kdialog (part 3)

This third part is dedicated to another set of constrained choice dialogs useful to drive the user among a discrete number of options in order to handle easier certain input data in our scripts. Besides the file dialogs seen in the second part, Kdialog offers menus, with single and multiple-choice, and particular input dialogs which may return date, a color or a system icon.

Choice off the menu

Let’s start to talk about menu dialogs with the simplest among them: the –combobox dialog. It requires just a text and a list of space-separated options to display which will be returned once selected and confirmed by the Ok button.

$ kdialog --combobox "What's your favorite color?" Blue Black Orange Teal Yellow 'None of them' --default Teal --title 'Pick one!'

kdialog --combobox

kdialog --combobox

Without the –default option it will default to an unselected status, returning a blank item. The limitation of this dialog is due to the kind of data returned which is identical to the label shown. Most of the time we want just keep the two domains, data model and data view, separated, that’s why we have to look forward to other dialogs.

The evolution in that sense is represented by the –menu dialog:

$ kdialog --menu 'Order your pizza' 1 'Margherita' 2 'Peperoni' 3 'Quattro formaggi' 4 'Funghi e salsiccia' 5 'Capricciosa' 6 'Marinara' --title 'Dinner time'

kdialog --menu

kdialog --menu

This time for each item we have an index that belongs to the data model, preceding a text which represents the data view (the label). The index might also be an alphanumeric code or whatever else can be useful to the programmer to better identify a certain input. and all that without sacrifice the user readability.

Let’s think to translated labels in our code, referring to the user choice using the same labels might become soon a pain every time a new language is added, an index instead never changes.

Unfortunately, this last dialog doesn’t include multiple selections, let’s thus introduce the –checklist and –radiolist dialogs: they are very similar except for the number of selectable items. As their name might suggest the checklist allows multiple selections while a radiolist is dedicated to a single choice.

$ kdialog --checklist 'Select your hobbies:' 1 Cinema off 2 Music off 3 Books on 4 TV off 5 Photography on 6 Food off 7 Blogging on --separate-output

kdialog --checklist

kdialog --checklist

This time a third parameter after the data model and the data view is introduced: the on / off constant which implements the selected or unselected status for each item. With checklist we can also use the –separate-output switch which, as seen for the file dialogs, comes in handy when the dialog returns multiple params displaying them in separated lines instead of a space-separated list.

The radiolist accepts exactly the same parameters but only the latest item left on will result as preselected.

$ kdialog --radiolist 'How is your day:' 1 Great off 2 Fine off 3 'Not bad' on 4 'So so' off 5 'Another question?' on

kdialog --radiolist

kdialog --radiolist

Miscellanea dialogs

Last but not least this set of miscellanea dialogs can be useful for several and not common situations.

For instance, to get a number between 1 and 100, a slider can be the right solution:

$ kdialog --slider "Choice a number" 0 100 10

kdialog --slider

kdialog --slider

The option accepted are in the order: a title, the minimum value, the maximum value and the step from a number to another using the arrow key. Be aware that it’s still possible to select a number between two steps using the mouse pointer.

To request a date there is the calendar dialog, which returns the format: weekday name, month name, day of the month, year (%a %b %d %Y):

$ kdialog --calendar 'Enter your birthday' --title 'Insert the date'

kdialog --calendar

kdialog --calendar

We can simply get a color using the getcolor dialog:

$ kdialog --getcolor --format 'Red: %3d, Green: %3d, Blue: %3d' --title 'Choice a color' --default '#55AAFF'

kdialog --getcolor

kdialog --getcolor

The –format switch is optional and defaults to #%2x%2x%2x, for a decimal format just use %3d as in the example above. The default color can be specified by the –default switch followed by the related HTML format.

The last dialog to show is geticon which lists only the icons in the current theme, belonging to a certain group and context when specified.

$ kdialog --geticon Applications

The group of an icon refers to the place it’s shown:

  • Desktop
  • Toolbar
  • MainToolbar
  • Small
  • Panel
  • Dialog
  • User

While the context refers to the type of the icon:

  • Action
  • Application
  • Device
  • FileSystem
  • MimeType
  • Animation
  • Category
  • Emblem
  • Emote
  • International
  • Place
  • StatusIcon

    kdialog --geticon

kdialog --geticon

For some reason my version of Kdialog ignores completely the group and consider the context as the first parameter only, probably something will be fixed in the next releases.

All the dialogs shown use the same mechanism seen in the second part to communicate the button clicked (exit status) and the input captured (standard output), so in this last example I will just show a small game with dates:

#!/bin/bash
date=`LANG=en_US.UTF-8 kdialog --calendar "Choice a day"`

if [ $? -eq 0 ]; then
  day_of_year=`date --date "$date" +'%j'`
  week_of_year=`date --date "$date" +'%W'`

  kdialog --msgbox "The selected date is the ${day_of_year##+(0)}th day of year and belongs to the ${week_of_year##+(0)}th week of the year"
fi

The script uses the date command to reformat the output from calendar, printing the day of the year and the week of the year and shows how kdialog works well with the other CLI applications usually installed.

Final thoughts

The long list of Kdialog’s dialog is finally ended up, with that simple command-line application and some line of Bash code, useful system scripts can be written to achieve basic or advanced tasks without involving more complex programming languages.

For more info the KDE tech base includes good references.


License

Creative Commons License This work is licensed under a Creative Commons Attribution-NoCommercial-ShareAlike 4.0 International License

Share to


Related articles


Comments