Widget

Contents

Widget#

Custom Widget, etc.#

Custom widget.

This module provides customuized widgets.

class Button(parent=None, text='', toggle=False, size=None, bold=False)[source]#

Bases: QPushButton

Button widget.

Parameters:
  • parent (QWidget, optional) -- parent.

  • text (str, optional) -- text.

  • toggle (bool, optional) -- toggle button ?

  • size (int, optional) -- font size.

  • bold (bool, optional) -- bold face ?

class Check(parent=None, text='', size=None, bold=False)[source]#

Bases: QCheckBox

Check widget.

Parameters:
  • parent (QWidget, optional) -- parent.

  • text (str, optional) -- text.

  • size (int, optional) -- font size.

  • bold (bool, optional) -- bold face ?

is_checked()[source]#

Is checked ?

Returns:

  • (bool) -- checked ?

class ColorSelector(parent=None, current='', color_type='color', size=None, bold=False)[source]#

Bases: QComboBox

Color selector widget.

Parameters:
  • parent (QWidget, optional) -- parent.

  • current (str, optional) -- default color.

  • color_type (str, optional) -- color/colormap/color_both

  • size (int, optional) -- font size.

  • bold (bool, optional) -- bold face ?

Notes

  • connect currentTextChanged.

class Combo(parent=None, item=None, init=None, size=None, bold=False)[source]#

Bases: QComboBox

Combo widget.

Parameters:
  • parent (QWidget, optional) -- parent.

  • item (list, optional) -- list of items, [str].

  • init (str, optional) -- initial value.

  • size (int, optional) -- font size.

  • bold (bool, optional) -- bold face ?

find_index(key)[source]#

Find index (including match).

Parameters:

key (str) -- item key.

Returns:

  • (int) -- index.

get_item()[source]#

Get item.

Returns:

  • (list) -- item list.

set_item(item)[source]#

Set item.

Parameters:

item (list) -- item list.

class DSpin(parent=None, minimum=0.0, maximum=1.0, step=0.1, size=None, bold=False)[source]#

Bases: QDoubleSpinBox

Spin widget.

Parameters:
  • parent (QWidget, optional) -- parent.

  • minimum (float, optional) -- minimum value.

  • maximum (float, optional) -- maximum value.

  • step (float, optional) -- step value.

  • size (int, optional) -- font size.

  • bold (bool, optional) -- bold face ?

class Editor(parent=None, text='', validator=None, color='black', size=None, bold=False, mathjax=None)[source]#

Bases: Panel

Editor widget with math/text display.

Parameters:
  • parent (QWidget, optional) -- parent.

  • text (str, optional) -- text.

  • validator (tuple, optional) -- (validator_type, option).

  • color (str, optional) -- color name.

  • size (int, optional) -- font size.

  • bold (bool, optional) -- bold face ?

  • mathjax (MathJaxSVG, optional) -- mathjax converter.

clearFocus()[source]#

Exit edit mode, safely restoring display.

mouseDoubleClickEvent(_)[source]#

Switch to edit mode on double click.

mousePressEvent(event)[source]#

Handle focus changes safely.

setText(text)[source]#

Set editor and display text.

text()[source]#

Return current text.

  • During editing: return raw_text() from LineEdit.

  • Otherwise: return display text.

class HBar(parent=None)[source]#

Bases: QFrame

Horizontal bar widget.

Parameters:

parent (QWidget, optional) -- parent.

class HSpacer[source]#

Bases: QSpacerItem

Horizontal spacer.

class Label(parent=None, text='', color='black', size=None, bold=False)[source]#

Bases: QLabel

Label widget.

Parameters:
  • parent (QWidget, optional) -- parent.

  • text (str, optional) -- text.

  • color (str, optional) -- font color.

  • size (int, optional) -- font size (pt).

  • bold (bool, optional) -- bold font ?

class Layout(parent=None)[source]#

Bases: QGridLayout

Layout widget.

Parameters:

parent (QWidget, optional) -- parent.

class LineEdit(parent=None, text='', validator=None, size=None, bold=False)[source]#

Bases: QLineEdit

class MathWidget(parent=None, text='', color='black', size=None, mathjax=None)[source]#

Bases: QWidget

class Panel(parent=None)[source]#

Bases: QWidget

Panel widget.

Parameters:

parent (QWidget, optional) -- parent.

class Spin(parent=None, minimum=0, maximum=1, size=None, bold=False)[source]#

Bases: QSpinBox

Spin widget.

Parameters:
  • parent (QWidget, optional) -- parent.

  • minimum (int, optional) -- minimum value.

  • maximum (int, optional) -- maximum value.

  • size (int, optional) -- font size.

  • bold (bool, optional) -- bold face ?

class VSpacer[source]#

Bases: QSpacerItem

Vertical spacer.

Validator type.

  • type: option.
    • int: (min, max)

    • float: (min, max, digit)

    • list_int: (shape, var)

    • list_float: (shape, var ,digit)

    • math: (shape, var)

    • site: (use var?)

    • bond: (use var?)

    • site_bond: (use var?)

    • vector_site_bond: (use var?)

    • orbital_site_bond: (use var?)

check_symbol(expr)[source]#

Check symbol.

Parameters:

expr (sympy or ndarray)

Returns:

  • (bool) -- if expr contains symbol, return True otherwise False.

convert_to_bond(bond, use_var=False)[source]#

Convert to bond from str.

Parameters:
  • bond (str) -- bond.

  • use_var (bool, optional) -- use [x,y,z] for site/bond ?

Returns:

  • (ndarray) -- bond vector.

  • (ndarray) -- bond center.

Note

  • bond string is "[tail];[head]/[vector]@[center]/[start]:[vector]".

validator_bond(s, use_var=False)[source]#

Validator for bond.

Parameters:
  • s (str) -- bond string.

  • use_var (bool, optional) -- use [x,y,z] for bond ?

Returns:

  • (str) -- input s if it is valid, otherwise None.

Note

  • bond sytles, start:vector, tail;head, vector@center, are accepted.

validator_float(text, **opt)[source]#

Validator for float.

Parameters:
  • text (str) -- float string.

  • opt (dict, optional) -- option, "min/max/digit". (default: "*","*",5)

Returns:

  • (str) -- formatted string if it is valid, otherwise None.

validator_int(text, **opt)[source]#

Validator for int.

Parameters:
  • text (str) -- int string.

  • opt (dict, optional) -- option, "min/max". (default: "*","*")

Returns:

  • (str) -- formatted string if it is valid, otherwise None.

validator_list_float(text, **opt)[source]#

Validator for list float.

Parameters:
  • text (str) -- string with list.

  • opt (dict, optional) -- option, "digit/shape/var". (default: 5,None,[""]) e.g., 15, (), (n,), (n,m), ..., ["x", "y", ...].

Returns:

  • (str) -- formatted string if it is valid, otherwise None.

validator_list_int(text, **opt)[source]#

Validator for list int.

Parameters:
  • text (str) -- string with list.

  • opt (dict, optional) -- option, "shape/var". (default: None,[""]) e.g., (), (n,), (n,m), ..., ["x", "y", ...].

Returns:

  • (str) -- formatted string if it is valid, otherwise None.

validator_math(text, **opt)[source]#

Validator for math to LaTeX.

Parameters:
  • text (str) -- sympy string.

  • opt (dict, optional) -- option, "shape/var". (default: None,None)

Returns:

  • (str) -- LaTeX string if it is valid, otherwise None.

validator_orbital_site_bond(s, use_var=False)[source]#

Validator for orbital on site or bond.

Parameters:
  • s (str) -- orbital on site or bond string.

  • use_var (bool, optional) -- use [x,y,z] for site/bond ?

Returns:

  • (str) -- input s if it is valid, otherwise None.

Note

  • orbital can contain x, y, z, r.

  • bond sytles, start:vector, tail;head, vector@center, are accepted.

validator_site(s, use_var=False)[source]#

Validator for site.

Parameters:
  • s (str) -- site string, [x,y,z].

  • use_var (bool, optional) -- use [x,y,z] for site ?

Returns:

  • (str) -- input s if it is valid, otherwise None.

validator_site_bond(s, use_var=False)[source]#

Validator for site or bond.

Parameters:
  • s (str) -- site or bond string.

  • use_var (bool, optional) -- use [x,y,z] for site/bond ?

Returns:

  • (str) -- input s if it is valid, otherwise None.

Note

  • bond sytles, start:vector, tail;head, vector@center, are accepted.

validator_vector_site_bond(s, use_var=False)[source]#

Validator for vector on site or bond.

Parameters:
  • s (str) -- vector on site or bond string.

  • use_var (bool, optional) -- use [x,y,z] for site/bond ?

Returns:

  • (str) -- input s if it is valid, otherwise None.

Note

  • bond sytles, start:vector, tail;head, vector@center, are accepted.

MessageBox dialog.

This module provides message box dialog.

class MessageBox(msg, title='Message Box')[source]#

Bases: QDialog

Message box.

Parameters:
  • msg (str) -- message.

  • title (str) -- window title.

TableView widget.

This module provides table view widget.

class TableView(parent=None, data=None, header=None, vertical=False, color='black', size=12, mathjax=None)[source]#

Bases: QTableWidget

Table view (math).

Parameters:
  • parent (QWidget, optional) -- parent.

  • data (list, optional) -- table data in LaTeX code without "$".

  • header (list, optional) -- header string.

  • vertical (bool, optional) -- show vertical number header ?

  • color (str, optional) -- color name.

  • size (int, optional) -- font size.

MathJaxSVG converter.

This module provides mathjax to SVG converter.

Data View and Model#

Tab of GroupView.

This module provides a set of group view classes in tab format.

class TabGroupView(parent=None, models=None, title='Data', mathjax=None)[source]#

Bases: QDialog

Data view group.

Parameters:
  • parent (QWidget, optional) -- parent.

  • models (dict, optional) -- set of models, {object_type: GroupModel}.

  • title (str, optional) -- window title.

select_tab(object_type)[source]#

Select tab.

Parameters:

object_type (str) -- object type.

View panel for GroupModel.

This module provides a view class to show object data. By clicking right button of mouse, the context menu appears.

class GroupView(parent=None, model=None, use_delegate=True, mathjax=None)[source]#

Bases: QTreeView

Group view.

Parameters:
  • parent (QWidget, optional) -- parent.

  • model (GroupModel, optional) -- group model.

  • use_delegate (bool, optional) -- use delegate or plain text ?

clear_selection()[source]#

Clear selection.

context_menu(position)[source]#

Context menu.

copy_row()[source]#

Copy row.

insert_row()[source]#

Insert row.

keyPressEvent(event)[source]#

Key press event for ESC and up and down keys.

mousePressEvent(event)[source]#

Mouse press event for focus or clear selection.

remove_row()[source]#

Remove row.

select_row(index)[source]#

Select row.

Parameters:

index (QModelIndex) -- index.

selection_changed(selected, deselected)[source]#

For Selection changed.

Parameters:
  • selected (list) -- selected indexes.

  • deselected (list) -- deselected indexes.

set_widget(item=None)[source]#

Set widget.

Parameters:

item (QStandardItem, optional) -- item.

Delegate for GroupModel and GroupView.

This module provides delegate for color selector, combo, and editor.

class ColorDelegate(parent, default, option)[source]#

Bases: Delegate

class ComboDelegate(parent, default, option)[source]#

Bases: Delegate

class Delegate(parent)[source]#

Bases: QStyledItemDelegate

class EditorDelegate(parent, default, option, t, color, size, mathjax)[source]#

Bases: Delegate

Two-layer tree model for managing objects.

This module provides a class to manage objects to draw. The objects are grouped by the first column, "name". All the objects in the same group can be modified together. When there are more than two objects in the same group, a child tree appears and the parent tree is the same as the first row of the child tree.

The raw data is maintained by dict, and the index model is just to keep the relation between parent and child, which is necessary to use other Qt functionalities.

class GroupModel(parent=None, name='model', column_info=None)[source]#

Bases: QStandardItemModel

Group data model (2 layer parent-child tree model).

Parameters:
  • parent (QWidget, optional) -- parent.

  • name (str, optional) -- model name.

  • column_info (list, optional) -- {header: (type,option,default)} for each column.

action_copy_row(indexes)[source]#

Slot for copy row action.

Parameters:

indexes (list) -- list of index.

action_insert_row(indexes)[source]#

Slot for insert row action.

Parameters:

indexes (list) -- list of index.

Returns:

  • (QModelIndex) -- parent index of inserted row.

action_remove_row(indexes)[source]#

Slot for remove row action.

Parameters:

indexes (list) -- list of index.

append_row(row_data=None, role=None, index=<PySide6.QtCore.QModelIndex(-1, -1, 0x0, QObject(0x0))>, copy_row=False)[source]#

Append row.

Parameters:
  • row_data (list, optional) -- row data.

  • role (int, optional) -- role.

  • index (QModelIndex, optional) -- index.

  • copy_row (bool, optional) -- copy from current index ?

clear_data()[source]#

Clear data with keeping header and column info.

debug_data_changed(topLeft, bottomRight, roles=[])[source]#

Debug for dataChanged signal.

Parameters:
  • topLeft (QModelIndex) -- top left index.

  • bottomRight (QModelIndex) -- bottom right index.

  • roles (list, optional) -- list of roles.

debug_item_tree()[source]#

Debug for showing item tree.

debug_updata_data(name, row_data, role)[source]#

Debug for updateData signal.

Parameters:
  • name (str) -- group name.

  • row_data (list) -- row data.

  • role (int) -- role.

emit_update_all()[source]#

Emit update for all data.

emit_update_data(name, row_data, role, index)[source]#

Emit update data.

Parameters:
  • name (str) -- group name.

  • row_data (list) -- row_data.

  • role (int) -- role.

  • index (QModelIndex) -- index.

find_item(text, column=0, child=True)[source]#

Find item.

Parameters:
  • text (str) -- str to find.

  • column (int, optional) -- column.

  • child (bool, optional) -- find also for child ?

Returns:

  • (list) -- found items.

get_role_str(role)[source]#

Get role string.

Parameters:

role (int) -- role.

Returns:

  • (str) -- role string.

get_row_data(index, column=None)[source]#

Get row data.

Parameters:
  • index (QModelIndex) -- index.

  • column (int, optional) -- column.

Returns:

  • (list) -- row data.

Note

  • bool string is replaced by bool.

is_parent(index)[source]#

Is parent index ?

Parameters:

index (QModelIndex) -- index.

Returns:

  • (bool) -- parent index ?

move_row(index, value)[source]#

Move row.

Parameters:
  • index (QModelIndex) -- index.

  • value (QVariant) -- value.

remove_row(index, role=None)[source]#

Remove row.

Parameters:
  • index (QModelIndex) -- index.

  • role (int, optional) -- role.

setData(index, value, role=ItemDataRole.EditRole)[source]#

Set data (override).

Parameters:
  • index (QModelIndex) -- index.

  • value (QVariant) -- value to set.

  • role (int, optional) -- role.

Returns:

  • (bool) -- success to set data ?

set_check(index, column, data)[source]#

Set check state.

Parameters:
  • index (QModelIndex) -- index.

  • column (int) -- column.

  • data (QCheckState) -- check state.

set_check_state(item, row_data)[source]#

Set check state of row.

Parameters:
  • item (QItemModel) -- item of row(s).

  • row_data (list) -- row data, [str].

set_data(data)[source]#

Set data from list data.

Parameters:

- (list)

set_row_data(index, column, data)[source]#

Set row data.

Parameters:
  • index (QModelIndex) -- index.

  • column (int) -- column.

  • data (str) -- data.

show_index(index)[source]#

Get raw index.

Parameters:

index (QModelIndex) -- index.

Returns:

  • (str) -- raw index, P/C(row,column).

tolist()[source]#

Convert to list.

Parameters:

raw (bool, optional) -- raw data ?

Returns:

  • (list) -- list data.

tolist_index(topLeft, bottomRight)[source]#

Row data for given index.

Parameters:
  • topLeft (QModelIndex) -- index at top left.

  • bottomRight (QModelIndex) -- index at buttom right.

Returns:

  • (list) -- row data.

update_check_state(topLeft, bottomRight, roles=None)[source]#

Update check state data.

Parameters:
  • topLeft (QModelIndex) -- top left index.

  • bottomRight (QModelIndex) -- bottom right index.

  • roles (list, optional) -- list of roles.

Note

  • set check state (bool) in column+1.

property group_name#

Group name.

Returns:

  • (str) -- group name.

property header#

Header label.

Returns:

  • (list) -- header label.

Event Loop & Logging#

Control Qt event loop.

This module provides functions to control Qt event loop.

class ExceptionHook(parent=None)[source]#

Bases: QObject

Exception hook.

Parameters:

parent (QObject, optional) -- parent.

Note

  • call this before super().__init__() in __init__() of QMainWindow class as follows.
    • self.app = get_qt_application()

    • ExceptionHook()

hook(type, value, traceback)[source]#

Callback for uncaught exceptions.

Parameters:
  • type (Any) -- type of exception.

  • value (Any) -- arguments.

  • traceback (TraceBack) -- traceback.

reset()[source]#

Reset exception hook.

get_qt_application()[source]#

Get Qt application.

Returns:

  • (QApplication) -- Qt application.

Note

  • call this before super().__init__() in __init__() of QMainWindow class as follows.
    • self.app = get_qt_application()

    • ExceptionHook()

gui_qt()[source]#

Execute Qt GUI mode in IPython (if available).

Logging utility.

This module provides utilities for logging.

class LogHandler(level=10, stream=None, text_widget=None)[source]#

Bases: Handler

Log handler.

Parameters:
  • level (Level, optional) -- log level.

  • stream (TextIO, optional) -- stream.

  • text_widget (LogWidget, optional) -- text widget.

Note

  • if stream/text_widget is None, it is not used.

emit(record)[source]#

Emit message.

Parameters:

record (LogRecord) -- log record.

class LogWidget(parent=None, title='log message', level=10, stream=None, hook=True)[source]#

Bases: QWidget

Log widget.

Parameters:
  • parent (QObject, optional) -- parent.

  • title (str, optional) -- window title.

  • level (Level, optional) -- log level.

  • stream (TextIO, optional) -- stream for logging.

  • hook (bool, optional) -- exception hook ?

Note

  • if stream is True, sys.stderr is used.

  • if level is None, no logging.

append_text(text)[source]#

Append text.

Parameters:

text (str) -- text.

clear()[source]#

Clear log message.

save()[source]#

Save log message.

set_text(text)[source]#

Set text.

Parameters:

text (str) -- text.

start_logging(level=10, stream=None, text_widget=None, hook=True)[source]#

Start logging.

Parameters:
  • level (Level, optional) -- logging level.

  • stream (TextIO, optional) -- stream for logging.

  • text_widget (LogWidget, optional) -- text widget.

  • hook (bool, optional) -- exception hook ?

Note

  • stream is True, sys.stderr is used.

Misc.#

Simple PDF viewer.

This module contains a class for simple PDF viewer. It can load PDF file, and save the same file to different directory (just by copying it).

class PDFViewer(filename=None)[source]#

Bases: QWidget

Simple PDF viewer.

Parameters:

filename (str, optional) -- file name to load.

load()[source]#

Load PDF file.

save()[source]#

Save PDF file (default = current directory).

set_title(filename=None)[source]#

Set window title.

Parameters:

filename (str, optional) -- file name.

Color selector widget.

This module provides color selector widget.

color2pixmap(color_type, size, aspect_ratio=4, step=50)[source]#

Convert color/colormap to QPixmap.

Parameters:
  • color_type (str) -- color type, "color/colormap/color_both".

  • size (int) -- size of pixmap.

  • aspect_ratio (int, optional) -- aspect ratio of pximap (width/height).

  • steps (int, optional) -- division of colorbar gradation.

Retruns:
  • (dict) -- pixmap dict {name: pixmap}.

  • (list) -- separator position.

color_palette(name)[source]#

Color pallete.

Parameters:

name (str) -- color name.

Returns:

  • (QPalette) -- color palette.