mirror of
https://github.com/spalencsar/bearhub.git
synced 2026-07-09 06:24:15 +02:00
[ui][settings] improvements
This commit is contained in:
@@ -2,8 +2,9 @@ import re
|
||||
import time
|
||||
import traceback
|
||||
from threading import Thread
|
||||
from typing import List, Set, Type
|
||||
from typing import List, Set, Type, Tuple
|
||||
|
||||
from bauh import ROOT_DIR
|
||||
from bauh.api.abstract.controller import SoftwareManager, SearchResult, ApplicationContext
|
||||
from bauh.api.abstract.disk import DiskCacheLoader
|
||||
from bauh.api.abstract.handler import ProcessWatcher
|
||||
@@ -410,6 +411,30 @@ class GenericSoftwareManager(SoftwareManager):
|
||||
man_comp = man.get_settings()
|
||||
|
||||
if man_comp:
|
||||
settings_forms.append(TabComponent(man.__class__.__name__, man_comp))
|
||||
modname = man.__module__.split('.')[-2]
|
||||
icon_path = "{r}/gems/{n}/resources/img/{n}.svg".format(r=ROOT_DIR, n=modname)
|
||||
settings_forms.append(TabComponent(None, man_comp, icon_path, modname))
|
||||
|
||||
return TabGroupComponent(settings_forms)
|
||||
|
||||
def save_settings(self, component: TabGroupComponent) -> Tuple[bool, List[str]]:
|
||||
|
||||
saved, warnings = True, []
|
||||
|
||||
for man in self.managers:
|
||||
if man:
|
||||
modname = man.__module__.split('.')[-2]
|
||||
tab = component.get_tab(modname)
|
||||
|
||||
if not tab:
|
||||
self.logger.warning("Tab for {} was not found".format(man.__class__.__name__))
|
||||
else:
|
||||
success, errors = man.save_settings(tab.content)
|
||||
|
||||
if not success:
|
||||
saved = False
|
||||
|
||||
if errors:
|
||||
warnings.extend(errors)
|
||||
|
||||
return saved, warnings
|
||||
|
||||
@@ -2,12 +2,12 @@ from pathlib import Path
|
||||
from typing import Tuple
|
||||
|
||||
from PyQt5.QtCore import Qt
|
||||
from PyQt5.QtGui import QIcon, QPixmap
|
||||
from PyQt5.QtGui import QIcon, QPixmap, QIntValidator
|
||||
from PyQt5.QtWidgets import QRadioButton, QGroupBox, QCheckBox, QComboBox, QGridLayout, QWidget, \
|
||||
QLabel, QSizePolicy, QLineEdit, QToolButton, QHBoxLayout, QFormLayout, QFileDialog, QTabWidget
|
||||
QLabel, QSizePolicy, QLineEdit, QToolButton, QHBoxLayout, QFormLayout, QFileDialog, QTabWidget, QVBoxLayout
|
||||
|
||||
from bauh.api.abstract.view import SingleSelectComponent, InputOption, MultipleSelectComponent, SelectViewType, \
|
||||
TextInputComponent, FormComponent, FileChooserComponent, ViewComponent, TabGroupComponent
|
||||
TextInputComponent, FormComponent, FileChooserComponent, ViewComponent, TabGroupComponent, PanelComponent
|
||||
from bauh.view.qt import css
|
||||
from bauh.view.util import resource
|
||||
from bauh.view.util.translation import I18n
|
||||
@@ -162,6 +162,9 @@ class TextInputQt(QGroupBox):
|
||||
|
||||
self.text_input = QLineEdit()
|
||||
|
||||
if model.only_int:
|
||||
self.text_input.setValidator(QIntValidator())
|
||||
|
||||
if model.placeholder:
|
||||
self.text_input.setPlaceholderText(model.placeholder)
|
||||
|
||||
@@ -302,6 +305,20 @@ class IconButton(QWidget):
|
||||
self.bt.setToolTip(self.default_tootip)
|
||||
|
||||
|
||||
class PanelQt(QWidget):
|
||||
|
||||
def __init__(self, model: PanelComponent, i18n: I18n, parent: QWidget = None):
|
||||
super(PanelQt, self).__init__(parent=parent)
|
||||
self.model = model
|
||||
self.i18n = i18n
|
||||
self.setLayout(QVBoxLayout())
|
||||
self.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)
|
||||
|
||||
if model.components:
|
||||
for c in model.components:
|
||||
self.layout().addWidget(to_widget(c, i18n))
|
||||
|
||||
|
||||
class FormQt(QGroupBox):
|
||||
|
||||
def __init__(self, model: FormComponent, i18n: I18n):
|
||||
@@ -334,6 +351,9 @@ class FormQt(QGroupBox):
|
||||
def _new_text_input(self, c: TextInputComponent) -> Tuple[QLabel, QLineEdit]:
|
||||
line_edit = QLineEdit()
|
||||
|
||||
if c.only_int:
|
||||
line_edit.setValidator(QIntValidator())
|
||||
|
||||
if c.tooltip:
|
||||
line_edit.setToolTip(c.tooltip)
|
||||
|
||||
@@ -388,10 +408,11 @@ class TabGroupQt(QTabWidget):
|
||||
def __init__(self, model: TabGroupComponent, i18n: I18n, parent: QWidget = None):
|
||||
super(TabGroupQt, self).__init__(parent=parent)
|
||||
self.model = model
|
||||
self.setTabPosition(QTabWidget.West)
|
||||
self.setTabPosition(QTabWidget.North)
|
||||
|
||||
for c in model.components:
|
||||
self.addTab(to_widget(c.content, i18n), c.label)
|
||||
for c in model.tabs:
|
||||
icon = QIcon(c.icon_path) if c.icon_path else None
|
||||
self.addTab(to_widget(c.content, i18n), icon, c.label)
|
||||
|
||||
|
||||
def new_single_select(model: SingleSelectComponent) -> QWidget:
|
||||
@@ -424,5 +445,7 @@ def to_widget(comp: ViewComponent, i18n: I18n, parent: QWidget = None) -> QWidge
|
||||
return FormQt(comp, i18n)
|
||||
elif isinstance(comp, TabGroupComponent):
|
||||
return TabGroupQt(comp, i18n, parent)
|
||||
elif isinstance(comp, PanelComponent):
|
||||
return PanelQt(comp, i18n, parent)
|
||||
else:
|
||||
raise Exception("Cannot render instances of " + comp.__class__.__name__)
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QToolBar, QSizePolicy, QToolButton
|
||||
from io import StringIO
|
||||
|
||||
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QToolBar, QSizePolicy, QToolButton, QPushButton
|
||||
|
||||
from bauh.api.abstract.controller import SoftwareManager
|
||||
from bauh.view.qt.components import to_widget
|
||||
from bauh.api.abstract.view import MessageType
|
||||
from bauh.view.qt import dialog
|
||||
from bauh.view.qt.components import to_widget, new_spacer
|
||||
from bauh.view.util.translation import I18n
|
||||
|
||||
|
||||
@@ -15,15 +19,39 @@ class SettingsWindow(QWidget):
|
||||
self.i18n = i18n
|
||||
self.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)
|
||||
|
||||
settings_model = self.manager.get_settings()
|
||||
self.settings_model = self.manager.get_settings()
|
||||
|
||||
self.layout().addWidget(to_widget(settings_model, i18n))
|
||||
self.layout().addWidget(to_widget(self.settings_model, i18n))
|
||||
|
||||
action_bar = QToolBar()
|
||||
action_bar.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)
|
||||
|
||||
bt_save = QToolButton()
|
||||
bt_save.setText('Save')
|
||||
bt_close = QPushButton()
|
||||
bt_close.setText(self.i18n['close'].capitalize())
|
||||
bt_close.clicked.connect(lambda: self.close())
|
||||
action_bar.addWidget(bt_close)
|
||||
|
||||
action_bar.addWidget(new_spacer())
|
||||
|
||||
bt_save = QPushButton()
|
||||
bt_save.setText(self.i18n['save'].capitalize())
|
||||
bt_save.clicked.connect(self._save_settings)
|
||||
action_bar.addWidget(bt_save)
|
||||
|
||||
self.layout().addWidget(action_bar)
|
||||
|
||||
def _save_settings(self):
|
||||
success, warnings = self.manager.save_settings(self.settings_model)
|
||||
|
||||
if success:
|
||||
self.close()
|
||||
else:
|
||||
msg = StringIO()
|
||||
msg.write("It was not possible to properly the settings\n")
|
||||
|
||||
for w in warnings:
|
||||
msg.write(w + '\n')
|
||||
|
||||
msg.seek(0)
|
||||
|
||||
dialog.show_message(title="Warning", body=msg.read(), type_=MessageType.WARNING)
|
||||
|
||||
Reference in New Issue
Block a user