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 and easier handling 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.

The follow up section links to the previous parts of this series.

Choice off the menu

There are several ways to ask for a value among a list of options, each one with its particular properties which cover a wide range of situations.

Combobox

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!'

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'

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 can be an alphanumeric code or anything useful to the programmer for easily identify the returned input, while the label can be more near to the humain language.

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

Checks and Radios

Unfortunately, the previous dialog doesn’t include multiple selections, neither the ability to preselect the items, let’s thus introduce the –checklist and –radiolist dialogs: they are very similar except for the number of selectable items.

As their name suggests, the checklist allows multiple selections while in a radiolist only one item is chosen.

$ 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

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 selected.

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

Miscellanea dialogs

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

Numbers as Slider

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

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

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.

Date

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'

Colors

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'

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.

Icons

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

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

A small example

All the dialogs shown use the same mechanism seen previously to communicate which button has been clicked (exit status) and the input captured (standard output), so in this last example I will just show a small utility involving 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.


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


Follow-up articles


Leave a Comment