common.ui#

Collection of common utility methods used by UI elements.

The app has some DPI awareness, although, I’m pretty confident it wasn’t implemented correctly. Still, all size values must be queried using size() to get a DPI dependent pixel value.

Functions:

active_index([idx])

Get the active index of the current, or given list index.

center_window(w)

Move the given widget to the available screen geometry's middle.

color(v)

Get and cache a QColor.

current_tab()

The current tab index.

draw_aliased_text(painter, font, rect, text, ...)

Allows drawing aliased text using QPainterPaths.

fit_screen_geometry(w)

Fit the given widget's size to the available screen geometry.

get_selected_index(widget)

Find the index of a selected item of widget.

init_stylesheet()

Loads and stores the custom stylesheet used by the app.

model([idx])

Retrieves a list widget's proxy model.

move_widget_to_available_geo(w)

Moves the given widget inside available screen geometry.

restore_selection(widget, *args, **kwargs)

Restore a previously saved item selection.

restore_window_geometry(widget, *args, **kwargs)

Restore the previously saved window geometry.

restore_window_state(widget, *args, **kwargs)

Restore the previously saved window state.

rgb(v)

Returns the rgba(r,g,b,a) string representation of a QColor.

save_selection(widget, *args, **kwargs)

Save selected item to the user settings file.

save_window_state(widget, *args, **kwargs)

Saves the current state of a window.

select_index(widget, v, *args[, role])

Utility function used to select and reveal an item index in a view.

selected_index([idx])

Retrieves a list widget's active index.

set_stylesheet(w)

Apply the app's custom stylesheet to the given widget.

size(v)

Return a size value based on the current UI scale factors.

source_model([idx])

Retrieves a list widget's source model.

status_bar_message(message)

Decorator function used to show a status bar message.

widget([idx])

Retrieves the currently visible list widget if idx is None, otherwise, returns the specified widget.

bookmarks.common.ui.active_index(idx=None)[source]#

Get the active index of the current, or given list index.

Parameters:

idx (int, optional) – A tab index number, e.g. common.FileTab.

bookmarks.common.ui.center_window(w)[source]#

Move the given widget to the available screen geometry’s middle.

Parameters:

w (QWidget) – The widget to center.

bookmarks.common.ui.color(v)[source]#

Get and cache a QColor.

bookmarks.common.ui.current_tab()[source]#

The current tab index.

Returns:

The currently visible tab’s index.

Return type:

int

bookmarks.common.ui.draw_aliased_text(painter, font, rect, text, align, color, elide=None)[source]#

Allows drawing aliased text using QPainterPaths.

This is slow to calculate but ensures the rendered text looks smooth (on Windows especially, I noticed a lot of aliasing issues). We’re also eliding the given text to the width of the given rectangle.

Parameters:
  • painter (QPainter) – The active painter.

  • font (QFont) – The font to use to paint.

  • rect (QRect) – The rectangle to fit the text in.

  • text (str) – The text to paint.

  • align (Qt.AlignmentFlag) – The alignment flags.

  • color (QColor) – The color to use.

  • elide (bool) – Elides the source text if True.

Returns:

The width of the drawn text in pixels.

Return type:

int

bookmarks.common.ui.fit_screen_geometry(w)[source]#

Fit the given widget’s size to the available screen geometry.

Parameters:

w (QtWidget) – The widget to fit to screen geometry.

bookmarks.common.ui.get_selected_index(widget)[source]#

Find the index of a selected item of widget.

Parameters:

widget (QWidget) – The widget to get the selection from.

Returns:

The selected index.

Return type:

QPersistentModelIndex

bookmarks.common.ui.init_stylesheet()[source]#

Loads and stores the custom stylesheet used by the app.

The stylesheet template is stored in the rsc/stylesheet.qss file, and we use the values in config.json to expand it.

Returns:

The stylesheet.

Return type:

str

bookmarks.common.ui.model(idx=None)[source]#

Retrieves a list widget’s proxy model.

If idx is None, it returns the currently visible items’ model, otherwise, returns the specified widget’s model.

Parameters:

idx (int, optional) – A tab index number, e.g. common.FileTab.

Returns:

A list widget’s proxy model.

Return type:

FilterProxyModel

bookmarks.common.ui.move_widget_to_available_geo(w)[source]#

Moves the given widget inside available screen geometry.

Parameters:

w (QWidget) – The widget to move.

bookmarks.common.ui.restore_selection(widget, *args, **kwargs)[source]#

Restore a previously saved item selection.

Parameters:

widget (QWidget) – The widget to get the selection from.

bookmarks.common.ui.restore_window_geometry(widget, *args, **kwargs)[source]#

Restore the previously saved window geometry.

Parameters:

widget (QWidget) – The widget to restore the window geometry.

bookmarks.common.ui.restore_window_state(widget, *args, **kwargs)[source]#

Restore the previously saved window state.

Parameters:

widget (QWidget) – The widget to restore the window geometry.

bookmarks.common.ui.rgb(v)[source]#

Returns the rgba(r,g,b,a) string representation of a QColor.

Parameters:

v (QColor or int) – A color.

Returns:

The string representation of the color.

Return type:

str

bookmarks.common.ui.save_selection(widget, *args, **kwargs)[source]#

Save selected item to the user settings file.

Parameters:

widget (QWidget) – The widget to get the selection from.

bookmarks.common.ui.save_window_state(widget, *args, **kwargs)[source]#

Saves the current state of a window.

Parameters:

widget (QWidget) – A widget or window.

bookmarks.common.ui.select_index(widget, v, *args, role=QtCore.Qt.DisplayRole, **kwargs)[source]#

Utility function used to select and reveal an item index in a view.

Parameters:
  • widget (QWidget) – The view containing the item to select.

  • v (str) – The value of role.

  • role (QtCore.Qt.ItemDataRole) – The value’s role.

bookmarks.common.ui.selected_index(idx=None)[source]#

Retrieves a list widget’s active index.

If idx is None, it returns the currently visible items’ active index, otherwise, returns the specified widget’s active index.

Parameters:

idx (int, optional) – A tab index number, e.g. common.FileTab.

Returns:

A list widget’s active index.

Return type:

QtCore.QModelIndex

bookmarks.common.ui.set_stylesheet(w)[source]#

Apply the app’s custom stylesheet to the given widget.

Parameters:

w (QWidget) – A widget to apply the stylesheet to.

Returns:

The stylesheet applied to the widget.

Return type:

str

bookmarks.common.ui.size(v)[source]#

Return a size value based on the current UI scale factors.

Parameters:

v (int) – A size value in pixels.

bookmarks.common.ui.source_model(idx=None)[source]#

Retrieves a list widget’s source model.

If idx is None, it returns the currently visible items’ source model, otherwise, returns the specified widget’s source model.

Parameters:

idx (int, optional) – A tab index number, e.g. common.FileTab.

Returns:

A list widget’s source model.

Return type:

ItemModel

bookmarks.common.ui.status_bar_message(message)[source]#

Decorator function used to show a status bar message.

bookmarks.common.ui.widget(idx=None)[source]#

Retrieves the currently visible list widget if idx is None, otherwise, returns the specified widget.

Parameters:

idx (int, optional) – A tab index number, e.g. common.FileTab.

Returns:

A list widget.

Return type:

BaseItemView