Skip to content

Commit

Permalink
Settings menu added which theme combobox added
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeSullivan7 committed Mar 15, 2024
1 parent 5b77d30 commit 6c4e811
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 6 deletions.
10 changes: 10 additions & 0 deletions mantidimaging/gui/mvp_base/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
import time
from logging import getLogger

from PyQt5 import QtCore
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtWidgets import QMainWindow, QMessageBox, QDialog
from qt_material import apply_stylesheet

from mantidimaging.gui.utility import compile_ui

LOG = getLogger(__name__)
perf_logger = getLogger("perf." + __name__)
settings = QtCore.QSettings('mantidproject', 'Mantid Imaging')


class BaseMainWindowView(QMainWindow):
Expand All @@ -25,6 +28,13 @@ def __init__(self, parent, ui_file=None):
if ui_file is not None:
compile_ui(ui_file, self)

print(f"mvp_case: {settings.value('theme_selection')=}")

if settings.value('theme_selection') == 'Fusion':
self.setStyleSheet('Fusion')
else:
apply_stylesheet(self, theme=settings.value('theme_selection'), invert_secondary=False)

def closeEvent(self, e):
LOG.debug('UI window closed')
self.cleanup()
Expand Down
9 changes: 8 additions & 1 deletion mantidimaging/gui/ui/main_window.ui
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<x>0</x>
<y>0</y>
<width>1100</width>
<height>25</height>
<height>22</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
Expand All @@ -46,6 +46,8 @@
<addaction name="separator"/>
<addaction name="actionLiveViewer"/>
<addaction name="separator"/>
<addaction name="actionSettings"/>
<addaction name="separator"/>
<addaction name="actionExit"/>
</widget>
<widget class="QMenu" name="menuWorkflow">
Expand Down Expand Up @@ -239,6 +241,11 @@
<string>Open Live Viewer</string>
</property>
</action>
<action name="actionSettings">
<property name="text">
<string>Settings</string>
</property>
</action>
</widget>
<resources/>
<connections/>
Expand Down
100 changes: 100 additions & 0 deletions mantidimaging/gui/ui/settings_window.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>SettingsWindow</class>
<widget class="QMainWindow" name="SettingsWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>418</width>
<height>348</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QWidget" name="verticalWidget" native="true">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>1921</width>
<height>951</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
<horstretch>1</horstretch>
<verstretch>1</verstretch>
</sizepolicy>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QTabWidget" name="settingsTabWidget">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>1</horstretch>
<verstretch>1</verstretch>
</sizepolicy>
</property>
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="appearanceTab">
<attribute name="title">
<string>Appearance</string>
</attribute>
<widget class="QWidget" name="formLayoutWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>341</width>
<height>241</height>
</rect>
</property>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="themeLabel">
<property name="text">
<string>Theme:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="themeName">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="1" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</widget>
</widget>
</item>
</layout>
</widget>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
30 changes: 29 additions & 1 deletion mantidimaging/gui/windows/main/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
from uuid import UUID

import numpy as np
from PyQt5.QtCore import Qt, pyqtSignal, QUrl, QPoint
from PyQt5.QtCore import Qt, pyqtSignal, QUrl, QPoint, QSettings
from PyQt5.QtGui import QIcon, QDragEnterEvent, QDropEvent, QDesktopServices
from PyQt5.QtWidgets import QAction, QDialog, QLabel, QMessageBox, QMenu, QFileDialog, QSplitter, \
QTreeWidgetItem, QTreeWidget
from qt_material import apply_stylesheet

from mantidimaging.core.data import ImageStack
from mantidimaging.core.data.dataset import StrictDataset
Expand All @@ -36,6 +37,7 @@
from mantidimaging.gui.windows.nexus_load_dialog.view import NexusLoadDialog
from mantidimaging.gui.windows.operations import FiltersWindowView
from mantidimaging.gui.windows.recon import ReconstructWindowView
from mantidimaging.gui.windows.settings.view import SettingsWindowView
from mantidimaging.gui.windows.spectrum_viewer.view import SpectrumViewerWindowView
from mantidimaging.gui.windows.live_viewer.view import LiveViewerWindowView
from mantidimaging.gui.windows.stack_choice.compare_presenter import StackComparePresenter
Expand All @@ -51,6 +53,8 @@

LOG = getLogger(__name__)

settings = QSettings('mantidproject', 'Mantid Imaging')


class QTreeDatasetWidgetItem(QTreeWidgetItem):

Expand Down Expand Up @@ -91,12 +95,14 @@ class MainWindowView(BaseMainWindowView):
actionLoadNeXusFile: QAction
actionSaveImages: QAction
actionSaveNeXusFile: QAction
actionSettings: QAction
actionExit: QAction

filters: FiltersWindowView | None = None
recon: ReconstructWindowView | None = None
spectrum_viewer: SpectrumViewerWindowView | None = None
live_viewer: LiveViewerWindowView | None = None
settings_window: SettingsWindowView | None = None

image_load_dialog: ImageLoadDialog | None = None
image_save_dialog: ImageSaveDialog | None = None
Expand All @@ -105,6 +111,8 @@ class MainWindowView(BaseMainWindowView):
add_to_dataset_dialog: AddImagesToDatasetDialog | None = None
move_stack_dialog: MoveStackDialog | None = None

default_theme_enabled: int = 1

def __init__(self, open_dialogs: bool = True):
super().__init__(None, "gui/ui/main_window.ui")

Expand Down Expand Up @@ -185,6 +193,7 @@ def setup_shortcuts(self):
self.actionRecon.triggered.connect(self.show_recon_window)
self.actionSpectrumViewer.triggered.connect(self.show_spectrum_viewer_window)
self.actionLiveViewer.triggered.connect(self.live_view_choose_directory)
self.actionSettings.triggered.connect(self.show_settings_window)

self.actionCompareImages.triggered.connect(self.show_stack_select_dialog)

Expand Down Expand Up @@ -364,6 +373,23 @@ def show_nexus_save_dialog(self):
self.nexus_save_dialog = NexusSaveDialog(self, self.strict_dataset_list)
self.nexus_save_dialog.show()

def show_settings_window(self):
if not self.settings_window:
self.settings_window = SettingsWindowView(self)
self.settings_window.show()
else:
self.settings_window.activateWindow()
self.settings_window.raise_()
self.settings_window.show()

def set_theme(self, theme: str) -> None:
for window in [self, self.recon, self.live_viewer, self.spectrum_viewer, self.filters, self.settings_window]:
if window:
if theme == 'Fusion':
window.setStyleSheet(theme)
else:
apply_stylesheet(window, theme=theme, invert_secondary=False)

def show_recon_window(self):
if not self.recon:
self.recon = ReconstructWindowView(self)
Expand Down Expand Up @@ -499,6 +525,8 @@ def closeEvent(self, event):
self.spectrum_viewer.close()
if self.filters:
self.filters.close()
if self.settings_window:
self.settings_window.close()

else:
# Ignore the close event, keeping window open
Expand Down
3 changes: 3 additions & 0 deletions mantidimaging/gui/windows/settings/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Copyright (C) 2024 ISIS Rutherford Appleton Laboratory UKRI
# SPDX - License - Identifier: GPL-3.0-or-later
from __future__ import annotations
33 changes: 33 additions & 0 deletions mantidimaging/gui/windows/settings/presenter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright (C) 2024 ISIS Rutherford Appleton Laboratory UKRI
# SPDX - License - Identifier: GPL-3.0-or-later
from __future__ import annotations

from logging import getLogger
from typing import TYPE_CHECKING

from PyQt5.QtCore import QSettings

from mantidimaging.gui.mvp_base import BasePresenter

LOG = getLogger(__name__)

if TYPE_CHECKING:
from mantidimaging.gui.windows.settings.view import SettingsWindowView # pragma: no cover
from mantidimaging.gui.windows.main import MainWindowView

settings = QSettings('mantidproject', 'Mantid Imaging')


class SettingsWindowPresenter(BasePresenter):
view: 'SettingsWindowView'

def __init__(self, view: 'SettingsWindowView', main_window: 'MainWindowView'):
super().__init__(view)
self.view = view
self.main_window = main_window
self.current_theme = settings.value('theme_selection')

def set_theme(self):
self.current_theme = self.view.current_theme
settings.setValue('theme_selection', self.current_theme)
self.main_window.set_theme(self.current_theme)
44 changes: 44 additions & 0 deletions mantidimaging/gui/windows/settings/view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Copyright (C) 2024 ISIS Rutherford Appleton Laboratory UKRI
# SPDX - License - Identifier: GPL-3.0-or-later
from __future__ import annotations
from logging import getLogger
from typing import TYPE_CHECKING

from PyQt5.QtCore import QSettings
from PyQt5.QtWidgets import QTabWidget, QWidget, QComboBox, QLabel

from mantidimaging.gui.mvp_base import BaseMainWindowView

from qt_material import list_themes

from mantidimaging.gui.windows.settings.presenter import SettingsWindowPresenter

if TYPE_CHECKING:
from mantidimaging.gui.windows.main import MainWindowView # noqa:F401 # pragma: no cover

LOG = getLogger(__name__)

settings = QSettings('mantidproject', 'Mantid Imaging')


class SettingsWindowView(BaseMainWindowView):
settingsTabWidget: QTabWidget
appearanceTab: QWidget
themeName: QComboBox
themeLabel: QLabel

def __init__(self, main_window: 'MainWindowView'):
super().__init__(None, 'gui/ui/settings_window.ui')
self.setWindowTitle('Settings')
self.main_window = main_window
self.presenter = SettingsWindowPresenter(self, main_window)

self.themeName.addItem('Fusion')
self.themeName.addItems(list_themes())
self.themeName.setCurrentText(self.presenter.current_theme)

self.themeName.currentTextChanged.connect(self.presenter.set_theme)

@property
def current_theme(self) -> str:
return self.themeName.currentText()
17 changes: 13 additions & 4 deletions mantidimaging/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@
from mantidimaging import helper as h
from mantidimaging.core.utility.command_line_arguments import CommandLineArguments

from qt_material import apply_stylesheet

formatwarning_orig = warnings.formatwarning
warnings.formatwarning = lambda message, category, filename, lineno, line=None: formatwarning_orig(
message, category, filename, lineno, line="")

settings = QtCore.QSettings('mantidproject', 'Mantid Imaging')

if settings.contains("theme_selection"):
# there is the key in QSettings
print('Checking for theme preference in config')
theme_selection = settings.value('theme_selection')
print('Found theme_selection in config:' + theme_selection)
else:
print('theme_selection not found in config. Using default Fusion')
settings.setValue('theme_selection', 'Fusion')


def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Mantid Imaging GUI")
Expand Down Expand Up @@ -59,9 +68,9 @@ def parse_args() -> argparse.Namespace:
def setup_application() -> QApplication:
QGuiApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
q_application = QApplication(sys.argv)
#q_application.setStyle('Fusion')
q_application.setStyle(settings.value('theme_selection'))

apply_stylesheet(q_application, theme='light_blue.xml', invert_secondary=True)
#apply_stylesheet(q_application, theme='light_blue.xml', invert_secondary=True)
q_application.setApplicationName("Mantid Imaging")
q_application.setOrganizationName("mantidproject")
q_application.setOrganizationDomain("mantidproject.org")
Expand Down

0 comments on commit 6c4e811

Please sign in to comment.