Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
Customizing the Main Window using Qt-Stylesheets

In a final step, we want to further customize the appearance of our mainWindow to give it an distinct non-native look and feel. We want to achieve this by pursuing the following aims:

  • Change the background and widget colors
  • Change the tab-widget and ToolButton style, also with respect to mouse-over-button (hovering) effects
  • Completing the non-native tab-widget like impression of the perspectives by gluing tab-bar and perspective's PageComposite together
  • DICOM Import functionality

For GUI customization, we will modify the Qt-Stylesheets files already used by blueberry applications. Within the Qt-Stylesheet-Files, all widgets can globally and locally be adressed inside the main window for style changes. We have to adress the berry::IQtStyleManager to tell the BlueBerry workbench to use a specific Qt-Stylesheet. This is done inside the WorkbenchAdvisor in the CustomViewerWorkbenchAdvisor::Initialize() method:

void CustomViewerWorkbenchAdvisor::Initialize(berry::IWorkbenchConfigurer::Pointer configurer)
{
ctkPluginContext *pluginContext = org_mitk_example_gui_customviewer_Activator::GetPluginContext();
ctkServiceReference serviceReference = pluginContext->getServiceReference<berry::IQtStyleManager>();
// always granted by org.blueberry.ui.qt
Q_ASSERT(serviceReference);
berry::IQtStyleManager *styleManager = pluginContext->getService<berry::IQtStyleManager>(serviceReference);
Q_ASSERT(styleManager);
QString styleName = "CustomStyle";
styleManager->AddStyle(":/customstyle.qss", styleName);
styleManager->SetStyle(":/customstyle.qss");
// styleManager->AddStyle("/home/me/customstyle.qss", styleName);
// styleManager->SetStyle("/home/me/customstyle.qss");
}

The style manager is taken from the application's plugin context via service reference. Invoking the berry::IQtStyleManager::AddStyle() and berry::IQtStyleManager::SetStyle() methods, the workbench will now use the announced qss-File to style our Workbench Window. In a production system, the stylesheets are usually compiled into the plug-in or application using the Qt resource system. However, during developement of the stylesheets it is often more convenient to reference them using a hard-coded path to the local file system (see live update functionality below).

Before we start customization we will first provide some customization convenience. We add an UpdateStyle()-slot to our CustomViewerWorkbenchWindowAdvisor where we explicitly reset the css-File to the style manager:

void CustomViewerWorkbenchWindowAdvisor::UpdateStyle()
{
ctkPluginContext *pluginContext = org_mitk_example_gui_customviewer_Activator::GetPluginContext();
ctkServiceReference serviceReference = pluginContext->getServiceReference<berry::IQtStyleManager>();
// granted by org.blueberry.ui.qt
Q_ASSERT(serviceReference);
berry::IQtStyleManager *styleManager = pluginContext->getService<berry::IQtStyleManager>(serviceReference);
Q_ASSERT(styleManager);
styleManager->SetStyle("/home/me/customstyle.qss");
}

By integrating an update style button to the Application's main window and connecting this button with the previously defined slot, we can now button-push-update the style on runtime. This will of course only work for stylesheets which are referenced from the local file system.

StyledMainWindow0.png
The unstyled Main Window

First we might want to change the background color style by setting the background color of the QWidget::CentralWidget to a linear gradient from light to dark blue:

1 QWidget#CentralWidget
2 {
3 background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgb(43, 75, 116), stop:1 rgb(197, 245, 254));
4 }

Then, we give the page composite control widget a slight grey border (except for the upper border where no border should be visible) and the same background color as the activated tab widget:

1 QWidget#PageCompositeControlWidget
2 {
3 border-left: 1px solid #a0a0a0;
4 border-bottom: 1px solid #a0a0a0;
5 border-right: 1px solid #a0a0a0;
6 margin: 0px;
7 padding: 10px;
8 background: rgb(210, 221, 239);
9 }

The image below depicts the style changes.

StyledMainWindow2.png
Background-color changed Central and Composite Control Widgets

Concerning the tab-widget style, four states have to be customized: QtPerspectiveSwitcherTabBar::tab (the tab in general), QtPerspectiveSwitcherTabBar::tab:selected (when tab is selected), QtPerspectiveSwitcherTabBar::tab:selected:hover (when tab is selected and the mouse is hovering above), QtPerspectiveSwitcherTabBar::tab:!selected:hover (respectively).

All tabs are given round corners using border-top-left- and border-top-right-radius definitions. Additionally, all tabs is provided a gap to its neighbor defining a positive margin right. Selected tabs appear bigger by defining a negative upper margin, and they have no lower frame in the unselected state so a tab-widget appearance is provided. Finally, they have a brighter background color also used by the QWidget::ClientComposite. Hovering tabs are colored yellow and have a visible lower border:

1 QtPerspectiveSwitcherTabBar::tab
2 {
3 min-height: 24px;
4 border-top: 1px solid palette(mid);
5 border-left: 1px solid palette(mid);
6 border-bottom: 0px;
7 border-top-left-radius: 8px;
8 border-top-right-radius: 8px;
9 margin-top: -1px;
10 margin-right: 4px;
11 padding-left: 5px;
12 background: rgb(210, 221, 239);
13 }
14 
15 QtPerspectiveSwitcherTabBar::tab:selected:hover {
16 border-bottom: 1px solid palette(mid);
17 background: yellow;
18 }
19 
20 QtPerspectiveSwitcherTabBar::tab:!selected {
21 margin-top: 5px;
22 background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgb(156, 181, 220), stop:1 rgb(130, 160, 210));
23 }
24 
25 QtPerspectiveSwitcherTabBar::tab:!selected:hover {
26 background: yellow;
27 }

Finally, we customize the Push- and Tool-Buttons in a similar way:

1 QToolButton#FileOpenButton
2 {
3 min-height: 20px;
4 border-top-left-radius: 4px;
5 border-top-right-radius: 4px;
6 border-bottom-left-radius: 4px;
7 border-bottom-right-radius: 4px;
8 border: 1px solid gray;
9 padding-left: 5px;
10 background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgb(156, 181, 220), stop:1 rgb(130, 160, 210));
11 }
12 QToolButton#FileOpenButton:hover
13 {
14 background: yellow;
15 }
16 
17 QPushButton#StyleUpdateButton
18 {
19 min-height: 25px;
20 border-top-left-radius: 4px;
21 border-top-right-radius: 4px;
22 border-bottom-left-radius: 4px;
23 border-bottom-right-radius: 4px;
24 background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgb(210, 221, 239), stop:1 rgb(197, 245, 254));
25 }

The resulting style-customized main window is shown below (the style update button removed).

StyledMainWindow_final1.png
The final version of our Custom Viewer (viewer perspective).
StyledMainWindow_final2.png
The final version of our Custom Viewer (DICOM perspective).

Proceed to the previous page Adding functionality: Data Manager, Render Window, File Opening and DICOM Import.