The widgets sub-package#

Package with modified widgets required for creating the pydidas graphical user interface.

class pydidas.widgets.CreateWidgetsMixIn#

Bases: object

A mixin class to allow easy widget creation in their host classes.

The CreateWidgetsMixIn class includes methods for easy adding of widgets to the layout. The create_something methods from the factories are called, and in addition, the layout and positions can be set.

Use the “gridPos” keyword to define the widget position in the parent’s layout.

add_any_widget(ref: str | None, widget_instance: QWidget, **kwargs: Any)#

Add any existing widget to the layout and apply settings to it.

Parameters:
  • ref (str | None) – The widget reference key.

  • widget_instance (QWidget) – The widget instance.

  • **kwargs (dict) – Any attributes supported by the specific QWidget with a setAttribute method are valid kwargs. In addition, ‘layout_kwargs’ is a valid key to pass a dictionary with attributes for the widget’s layout.

create_any_widget(ref: str | None, widget_class: type, *args: Any, **kwargs: Any)#

Create any widget with any settings and add it to the layout.

Note

Widgets must support generic args and kwargs arguments. This means that generic PyQt widgets cannot be created using this method. They can be added, however, using the add_any_widget method.

Parameters:
  • ref (str | None) – The reference name in the _widgets dictionary.

  • widget_class (type) – The class type of the widget.

  • *args (Any) – Any arguments for the widget creation.

  • **kwargs (dict) – Keyword arguments for the widget creation.

Raises:

TypeError – If the reference “ref” is not of type string.

create_button(ref: str | None, text: str, **kwargs: Any)#

Create a QPushButton and store the widget.

Parameters:
  • ref (str | None) – The reference string for storing the widget. If None, the widget will automatically get a unique reference number.

  • text (str) – The button’s displayed text.

  • **kwargs (dict) – Any attributes supported by QPushButton with a setAttribute method are valid kwargs. In addition, the ‘gridPos’ keyword can be used to specify the button’s position in its parent’s layout. The ‘fontsize_offset’, ‘bold’, ‘italic’, ‘underline’ keywords can be used to control the font properties. The button’s clicked method can be connected directly by specifying the slot through the ‘clicked’ kwarg.

create_check_box(ref: str | None, text, **kwargs: Any)#

Create a PydidasCheckBox and store the widget.

Parameters:
  • ref (str | None) – The reference string for storing the widget. If None, the widget will automatically get a unique reference number.

  • text (str) – The CheckBox’s descriptive text.

  • **kwargs (dict) – Any attributes supported by QCheckBox with a setAttribute method are valid kwargs. In addition, the ‘gridPos’ keyword can be used to specify the QProgressBar’s position in its parent’s layout. The ‘fontsize_offset’, ‘bold’, ‘italic’, ‘underline’ can be used to control the font properties or generic Qt properties.

create_combo_box(ref: str | None, **kwargs: Any)#

Create a PydidasComboBox and store the widget.

Parameters:
  • ref (str | None) – The reference string for storing the widget. If None, the widget will automatically get a unique reference number.

  • **kwargs (dict) – Any attributes supported by QComboBox with a setAttribute method are valid kwargs. In addition, the ‘gridPos’ keyword can be used to specify the QComboBox’s position in its parent’s layout. The ‘fontsize_offset’, ‘bold’, ‘italic’, ‘underline’ can be used to control the font properties or generic Qt properties.

create_double_spin_box(ref: str | None, **kwargs: Any)#

Create a QDoubleSpinBox for floating point values and store the widget.

Parameters:
  • ref (str | None) – The reference string for storing the widget. If None, the widget will automatically get a unique reference number.

  • **kwargs (dict) – Any attributes supported by QSpinBox with a setAttribute method are valid kwargs. In addition, the ‘gridPos’ keyword can be used to specify the SpinBox’s position in its parent’s layout. The default range is set to (0, 1) if it is not overridden with the ‘range’ keyword.

create_empty_widget(ref: str | None, **kwargs: Any)#

Create an empty QWidget with a grid layout.

Parameters:
  • ref (str | None) – The reference string for storing the widget. If None, the widget will automatically get a unique reference number.

  • **kwargs (dict) – Any attributes supported by the generic QWidget with a setAttribute method are valid kwargs. In addition, the ‘gridPos’ keyword can be used to specify the QWidget’s position in its parent’s layout.

create_label(ref: str | None, text: str, **kwargs: Any)#

Create a PydidasLabel and store the widget.

Parameters:
  • ref (str | None) – The reference string for storing the widget. If None, the widget will automatically get a unique reference number.

  • text (str) – The label’s displayed text.

  • **kwargs (dict) – Any attributes supported by QLabel with a setAttribute method are valid kwargs. In addition, the ‘gridPos’ keyword can be used to specify the label’s position in its parent’s layout. The ‘fontsize_offset’, ‘bold’, ‘italic’, ‘underline’ keywords can be used to control the font properties.

create_line(ref: str | None, **kwargs: Any)#

Create a line widget.

This method creates a line widget (implemented as flat QFrame) as a separator and adds it to the parent widget.

Parameters:
  • ref (str | None) – The reference string for storing the widget. If None, the widget will automatically get a unique reference number.

  • **kwargs (dict) – Any additional keyword arguments. All QFrame attributes with setAttribute implementation are valid kwargs.

create_lineedit(ref: str | None, **kwargs: Any)#

Create a PydidasLineEdit and store the widget.

Parameters:
  • ref (str | None) – The reference string for storing the widget. If None, the widget will automatically get a unique reference number.

  • **kwargs (dict) – Any attributes supported by QLineEdit with a setAttribute method are valid kwargs. In addition, the ‘gridPos’ keyword can be used to specify the LineEdit’s position in its parent’s layout. The ‘fontsize_offset’, ‘bold’, ‘italic’, ‘underline’ keywords can be used to control the font properties.

create_progress_bar(ref: str | None, **kwargs: Any)#

Create a QProgressBar and store the widget.

Parameters:
  • ref (str | None) – The reference string for storing the widget. If None, the widget will automatically get a unique reference number.

  • **kwargs (dict) – Any attributes supported by QProgressBar with a setAttribute method are valid kwargs. In addition, the ‘gridPos’ keyword can be used to specify the QProgressBar’s position in its parent’s layout.

create_radio_button_group(ref: str | None, entries: list[str], **kwargs: Any)#

Create a RadioButtonGroup widget and set its properties.

Parameters:
  • ref (str | None) – The reference string for storing the widget. If None, the widget will automatically get a unique reference number.

  • entries (list[str]) – The list of entries for the buttons.

  • **kwargs (dict) – Any attributes supported by the generic QWidget with a setAttribute method are valid kwargs. In addition, the ‘gridPos’ keyword can be used to specify the RadioButtonGroup’s position in its parent’s layout. The ‘fontsize_offset’, ‘bold’, ‘italic’, ‘underline’ can be used to control the font properties or generic Qt properties. The ‘entries’ keyword takes a list of entries for the buttons, and the number of rows and columns can be specified with the ‘rows’ and ‘columns’ keywords, respectively.

create_spacer(ref: str | None, **kwargs: Any)#

Create a QSpacerItem and set its properties.

Parameters:
  • ref (str | None) – The reference string for storing the widget. If None, the widget will automatically get a unique reference number.

  • **kwargs (Any) – Any attributes supported by QSpacerItem with a setAttribute method are valid kwargs. In addition, the gridPos key allows specifying the spacer’s position in its parent’s layout.

create_spin_box(ref: str | None, **kwargs: Any)#

Create a QSpinBox for integer values and store the widget.

Parameters:
  • ref (str | None) – The reference string for storing the widget. If None, the widget will automatically get a unique reference number.

  • **kwargs (dict) – Any attributes supported by QSpinBox with a setAttribute method are valid kwargs. In addition, the ‘gridPos’ keyword can be used to specify the SpinBox’s position in its parent’s layout. The default range is set to (0, 1) if it is not overridden with the ‘range’ keyword.

create_vertical_line(ref: str | None, **kwargs: Any)#

Create a vertical line widget.

This method creates a vertical line widget (implemented as flat QFrame) as a separator and adds it to the parent widget.

Parameters:
  • ref (str | None) – The reference string for storing the widget. If None, the widget will automatically get a unique reference number.

  • **kwargs (dict) – Any additional keyword arguments. All QFrame attributes with setAttribute implementation are valid kwargs.

class pydidas.widgets.PydidasFileDialog(*args: Any, **kwargs: Any)#

Bases: SingletonObject, QFileDialog, CreateWidgetsMixIn, PydidasQsettingsMixin

pydidas’s subclassed QFileDialog with additional functionality.

The PydidasFileDialogWidget is a subclassed QFileDialog with two additional buttons which allow fast navigation to the ScanContext base directory and to the latest selected directory (in any dialog).

The usage is the same as for the generic QFileDialog.

exec_() int#

Execute the dialog.

Returns:

The return code of the file dialog.

Return type:

int

get_existing_directory(**kwargs: dict) str | None#

Execute the dialog and get an existing directory.

Parameters:

**kwargs (dict) –

Optional keyword arguments. Supported keywords are:

captionstr, optional

The window title caption. The default is ‘Select directory’.

referencestr | int, optional

A reference key to store the selection only during the active instance. The default is None.

qsettings_refstr, optional

The reference key for storing the selection in the QSettings registry. The qsettings_ref will take precedence over the reference keyword.

info_stringstr, optional

An additional info string to be displayed at the top of the file selection for user information.

Returns:

The directory path, if selected or None.

Return type:

str | None

get_existing_filename(**kwargs: Any) str | None#

Execute the dialog and get the full path of an existing file.

Parameters:

**kwargs (Any) –

Optional keyword arguments. Supported keywords are:

captionstr, optional

The window title caption. The default is ‘Select existing file’.

referencestr | int, optional

A reference key to store the selection only during the active instance. The default is None.

select_multiplebool, optional

If True, multiple files can be selected. The default is False.

qsettings_refstr, optional

The reference key for storing the selection in the QSettings registry. The qsettings_ref will take precedence over the reference keyword.

Returns:

The full file path, if selected, or None.

Return type:

str | None

get_existing_filenames(**kwargs: Any) list[str]#

Execute the dialog and get a list of existing files.

This is a wrapper around get_existing_filename with the necessary kwargs settings taken care of for the user.

Please refer to get_existing_filename for more information and Parameters.

get_saving_filename(**kwargs: dict) str | None#

Execute the dialog and get the full path of a file for saving.

The file may exist or a new filename can be entered.

Parameters:

**kwargs (dict) –

Optional keyword arguments. Supported keywords are:

captionstr, optional

The window title caption. The default is ‘Select filename’.

referencestr | int, optional

A reference key to store the selection only during the active instance. The default is None.

qsettings_refstr, optional

The reference key for storing the selection in the QSettings registry. The qsettings_ref will take precedence over the reference keyword.

Returns:

The full file path, if selected, or None.

Return type:

str | None

goto_latest_location()#

Open the latest location from any dialogue.

goto_scan_base_dir()#

Open the ScanContext home directory, if set.

initialize(*args: Any, **kwargs: Any) None#

Initialize the PydidasFileDialog.

Parameters:
  • *args (Any) – Positional arguments.

  • **kwargs (Any) –

    Keyword arguments. Supported keywords are:

    parentUnion[None, QWidget], optional

    The dialog’s parent widget. The default is None.

    captionstr | None, optional

    The dialog caption. If None, the caption will be selected based on the type of dialog. The default is None.

    dialog_typestr

    The type of the dialog. Must be open_file, save_file, or open_directory. The default is open_file.

    formatsstr | None, optional

    The list of format filters for filenames, if used. The default is None.

    info_stringstr | None, optional

    An additional information string which is displayed in the FileDialog widget. The default is None.

set_curr_dir(reference: str | int, item: Path | str)#

Set the current directory to the directory of the given item.

Parameters:
  • reference (str | int) – The stored reference name.

  • item (Path | str) – The filename or directory name.

class pydidas.widgets.ScrollArea(parent=None, **kwargs)#

Bases: QScrollArea

Convenience class to simplify the setup of a QScrollArea.

Parameters:
  • parent (QWidget, optional) – The parent widget. The default is None.

  • **kwargs (dict) – Any additional keyword arguments. All Qt attributes with a setAttribute method are valid keywords.

force_width_from_size_hint()#

Enforce a fixed width based on the own sizeHint.

sizeHint()#

Get the size hint.

If a widget has been set, the ScrollArea will use the widget’s sizeHint to determine the required size. If no widget is set, it will default to the QScrollArea sizeHint.

Returns:

The size hint for the ScrollArea.

Return type:

QtCore.QSize

class pydidas.widgets.WidgetWithParameterCollection(**kwargs: dict)#

Bases: QWidget, PydidasQsettingsMixin, CreateWidgetsMixIn, ParameterCollectionMixIn, ParameterWidgetsMixIn

A widget which has full access to Pydidas’s ParameterCollection and widget creation methods.

pydidas.widgets.create_default_grid_layout() QGridLayout#

Create a QGridLayout with default parameters.

The default parameters are

  • vertical spacing: 5

  • horizontal spacing: 5

  • alignment: left | top

Returns:

layout – The layout.

Return type:

QGridLayout

pydidas.widgets.delete_all_items_in_layout(layout: QLayout)#

Recursively delete items in a QLayout.

Parameters:

layout (QLayout) – The layout to be cleared.

pydidas.widgets.get_max_pixel_width_of_entries(entries: str | tuple | list) int#

Get the maximum width from a number of entries.

This function calculates the width (in pixels) of a list of given strings.

Parameters:

entries (Union[str, tuple, list]) – The entries.

Returns:

width – The maximum width of the entries in pixels.

Return type:

int

pydidas.widgets.get_pyqt_icon_from_str(ref_string: str) QIcon#

Get a QIcon from the reference string.

Four types of strings can be processed:
  1. A reference number of a QStandardIcon, preceded by a ‘qt-std::’.

  2. A reference to an image file in the file system. The filename must be preceded by ‘path::’.

  3. A reference to an icon in pydidas.core.icons with the filename preceded by a ‘pydidas::’

  4. A reference to a mdi icon with the filename preceded by a ‘mdi::’ Note that this only works for those mdi icons which have been included in pydidas.

Parameters:

ref_string (str) – The reference string for the icon.

Raises:

TypeError – If no correct preceding type has been found.

Returns:

The icon

Return type:

QtGui.QIcon

pydidas.widgets.get_widget_layout_args(parent: QWidget, **kwargs: dict)#

Get the arguments for adding a widget to the layout of the parent.

Parameters:
  • parent (QWidget) – The parent QWidget to which the new widget shall be added.

  • **kwargs (dict) – The keyword arguments dictionary from the calling method. This method only takes the “gridPos”, “stretch” and “alignment” arguments from the kwargs.

Raises:

PydidasGuiError – If the parent widget has no supported layout. Supported are QBoxLayout, QStackedLayout or QGridLayout and subclasses.

Returns:

The list of layout arguments required for adding the widget to the layout of the parent widget.

Return type:

list

pydidas.widgets.icon_with_inverted_colors(icon: QIcon) QIcon#

Invert the colors of a QIcon.

Parameters:

icon (QIcon) – The icon to be inverted.

Returns:

The inverted icon.

Return type:

QIcon

pydidas.widgets.update_param_and_widget_choices(param_widget: QWidget, new_choices: list)#

Update the choices for the given Parameter and in its widget.

This function will update the choices and also set the combo box widget to an allowed choice.

Parameters: