How to place a view in the window¶
This page shows how to choose where a view of a Qt session appears: in a dock, in the centre, in a menu, in a toolbar or in the status bar. Point at a part of the main window to read what goes there:
How a frontend shows a session on screen explains what a placement is and how a frontend checks it.
Prerequisites¶
You need a session built on QtSession, and a view
whose constructor starts with (name: str, parent: QWidget). See
Write a component.
Put a widget in a dock or in the centre¶
Set placement on the class. Dock takes the edge of the
window, "left", "right", "top" or "bottom":
from qtpy.QtWidgets import QWidget
from redsun import Placement
from redsun.qt import Central, Dock
class MyView(QWidget):
placement: Placement = Dock("left")
def __init__(self, name: str, parent: QWidget) -> None:
super().__init__(parent)
self.name = name
class ImageView(QWidget):
placement: Placement = Central()
def __init__(self, name: str, parent: QWidget) -> None:
super().__init__(parent)
self.name = name
The title of a dock is the name the view is declared under. When several
views ask for Central, the centre shows them as tabs,
each titled with its view's name.
Put an action in a menu or a toolbar¶
A view placed as a MenuItem or a
ToolBarItem is a QAction, and it takes the same
constructor arguments as any Qt view:
from qtpy.QtGui import QAction
from qtpy.QtWidgets import QWidget
from redsun import Placement
from redsun.qt import MenuItem, ToolBarItem
class SnapAction(QAction):
placement: Placement = MenuItem("Acquire")
def __init__(self, name: str, parent: QWidget) -> None:
super().__init__("Snap", parent)
self.name = name
class StopAction(QAction):
placement: Placement = ToolBarItem("Acquisition")
def __init__(self, name: str, parent: QWidget) -> None:
super().__init__("Stop", parent)
self.name = name
qtpy exports QAction from QtGui only for the binding it selects, so
mypy needs the flags qtpy mypy-args prints, with QT_API set, to check a
class built on it.
The session creates the menu or toolbar the first time a view names it, and adds every later view naming it to the same one. If the command needs no view of its own, see Add menu actions.
Put a widget in a toolbar¶
A toolbar can also hold controls with live contents, such as a button next to
a spin box. Make the view a QWidgetAction, which is a QAction and so may
be a ToolBarItem, and hand it the widget with setDefaultWidget. The
toolbar then shows the widget where an action would show a button:
from qtpy.QtWidgets import (
QHBoxLayout,
QPushButton,
QSpinBox,
QWidget,
QWidgetAction,
)
from redsun import Placement
from redsun.qt import ToolBarItem
class AcquireControls(QWidgetAction):
placement: Placement = ToolBarItem("Acquisition")
def __init__(self, name: str, parent: QWidget) -> None:
super().__init__(parent)
self.name = name
self.acquire = QPushButton("Acquire")
self.passes = QSpinBox()
controls = QWidget()
layout = QHBoxLayout(controls)
layout.setContentsMargins(0, 0, 0, 0)
layout.addWidget(self.acquire)
layout.addWidget(self.passes)
self.setDefaultWidget(controls)
The view stays an action, so to move it to a dock later you change the class it builds on, not only its placement.
Put a widget in the status bar¶
A view placed as a StatusBarItem is a QWidget
the session keeps at the right of the status bar, where a message shown there
never covers it. It suits a row of labels read at a glance, such as a
position or a device's state:
from qtpy.QtWidgets import QHBoxLayout, QLabel, QWidget
from redsun import Placement
from redsun.qt import StatusBarItem
class PositionView(QWidget):
placement: Placement = StatusBarItem()
def __init__(self, name: str, parent: QWidget) -> None:
super().__init__(parent)
self.name = name
self.position = QLabel("x: 0.00 um")
width = self.position.fontMetrics().horizontalAdvance("x: -0000.00 um")
self.position.setFixedWidth(width)
layout = QHBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.addWidget(self.position)
The widget keeps the width its size hint gives it. A label whose text changes length needs a fixed width, as above; otherwise the widgets to its left move at each update. Several views in the status bar sit in the order they are declared. The window shows no title for such a view, and the Window menu does not list it.
Override a view's default placement¶
Every view class has a default place in the window, its placement. To put
a view somewhere else, give placement when you declare it, and the session
uses your value instead of the default. It doesn't pass placement to the
view, so this works for any view, including the built-in ones:
from typing import Annotated
from redsun import AsView, Declare
from redsun.qt import Central, Dock, QtSession
from redsun.view.qt.builtins import LogView, PositionerView
class MyApp(QtSession):
stage: Annotated[AsView[PositionerView], Declare(placement=Dock("left"))]
log: Annotated[AsView[LogView], Declare(placement=Central())]
In a session file, write placement as a word or a short mapping:
placement |
Where the view goes |
|---|---|
left, right, top, bottom |
a dock on that edge |
central |
the central area |
{dock: left, group: tools} |
a dock on that edge, in a tab with the other docks of group tools |
{menu: Acquire} |
an entry in the menu Acquire |
{toolbar: Acquisition} |
an entry in the toolbar Acquisition |
status_bar |
a widget at the right of the status bar |
A view placed in the menu Window joins the Window menu the session adds,
above the entries that show and hide the docks.
The session checks the placement before it builds anything, so if a view fails
to build, its error message appears where you placed it. The session refuses a
view whose constructor has its own placement parameter, because it keeps
that name for itself. It also refuses a view whose class attribute and
declaration give no placement. A placement property does not count, because
the session needs the placement before the view exists.
Give a view a title¶
A view's title is the text the window
shows for it: on its dock, on its tab in the centre, in the Window menu and in
the list of keyboard shortcuts. A view with no title shows its name, so a
session declaring my_view shows my_view. Give a title to show
something a person reads more easily. The name stays as it is, because the
saved layout, the wiring and the session file all find the view by it.
A view class gives its default title as a class attribute, next to its placement:
from qtpy.QtWidgets import QWidget
from redsun import Placement
from redsun.qt import Dock
class MyView(QWidget):
placement: Placement = Dock("left")
title = "Commands"
To change the title of a view you declare, including a built-in one, give
title in its declaration, or as a key of its entry in the session file:
from typing import Annotated
from redsun import AsView, Declare
from redsun.qt import QtSession
from redsun.view.qt.builtins import LightView
class MyApp(QtSession):
lights: Annotated[AsView[LightView], Declare(title="Lasers")]
The declaration and the session file both win over the class attribute, and
when both give a title, the declaration's wins, as for every keyword. The
session keeps title for itself, as it does placement, so it refuses a
view whose constructor has its own title parameter, and a title that is
empty or not a string. A view placed in a menu or a toolbar is an action that
shows its own text, so its title changes nothing there.
Tab docks together¶
Docks on the same edge with the same group open as tabs. The tabs follow the order you declare the views in:
from typing import Annotated
from redsun import AsView, Declare
from redsun.qt import Dock, QtSession
from redsun.view.qt.builtins import LightView, PositionerView
class MyApp(QtSession):
stage: Annotated[
AsView[PositionerView], Declare(placement=Dock("right", group="hardware"))
]
lights: Annotated[
AsView[LightView], Declare(placement=Dock("right", group="hardware"))
]
Lay out the whole window¶
When placements aren't enough, declare the whole first layout of the window
in the session. Return a WindowLayout from
window_layout(): it says what fills each edge and the centre, how much of
the window an edge takes, and which docks start hidden.
from redsun import AsView, Column, Row, Tabs, WindowLayout
from redsun.qt import QtSession
from redsun.view.qt.builtins import LightView, LogView, PositionerView
class MyApp(QtSession):
stage: AsView[PositionerView]
lights: AsView[LightView]
log: AsView[LogView]
def window_layout(self) -> WindowLayout | None:
return WindowLayout(
regions={"left": Column("stage", "lights", sizes=(2, 1)), "bottom": "log"},
sizes={"left": 0.25},
hidden=["log"],
)
The stages sit above the lights on the left edge and take two thirds of it.
The edge takes a quarter of the window's width, and the log starts hidden at
the bottom until you show it from the Window menu. A
Row puts views side by side, a Column
stacks them, and Tabs shows them as tabs, with current
on top.
A session file says the same in a layout section, which replaces what
window_layout() returns:
layout:
regions:
left: {column: [stage, lights], sizes: [2, 1]}
bottom: log
sizes: {left: 0.25}
hidden: [log]
A view the layout leaves out still goes where its placement asks. The session file reference lists every key.
See a changed placement take effect¶
A changed placement shows the next time you start the session. A session
started with run saves where the user left the docks when it ends, and
puts them back the next time, but only while every view asks for the place
it asked for when the layout was saved. So when you change a placement, add
a view or remove one, every dock starts from its placement, and the log says
why. A view that fails to build keeps the place its declaration gives it, so
the saved layout stays.
To put the docks back where their placements say at any time, choose Reset layout in the Window menu. The same menu lists every dock, so you can show a dock again after closing it with the button on its title bar. Find the settings file says where the saved layout is kept.
Read a failure¶
The session leaves out a view that asks for a placement Qt doesn't show, or
that isn't the type its placement needs, and the build summary lists it under
Not built. Here MyView, a QWidget, asks for a MenuItem; the real
lines end with the file and line they were logged from: