mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-09 21:29:11 +00:00
Initialize Settings on Config Screen with Existing Settings from File
This commit is contained in:
@@ -22,6 +22,11 @@ class ConfigureScreen(QtWidgets.QDialog):
|
|||||||
super(ConfigureScreen, self).__init__(parent=parent)
|
super(ConfigureScreen, self).__init__(parent=parent)
|
||||||
self.config_file = config_file
|
self.config_file = config_file
|
||||||
|
|
||||||
|
# Load config from existing config, if exists, else load from default config
|
||||||
|
self.config = yaml_utils.load_config_from_file(self.config_file)
|
||||||
|
if self.config is None:
|
||||||
|
self.config = yaml_utils.load_config_from_file(constants.app_root_directory / 'config/khoj_sample.yml')
|
||||||
|
|
||||||
# Initialize Configure Window
|
# Initialize Configure Window
|
||||||
self.setWindowFlags(Qt.WindowType.WindowStaysOnTopHint)
|
self.setWindowFlags(Qt.WindowType.WindowStaysOnTopHint)
|
||||||
self.setWindowTitle("Khoj - Configure")
|
self.setWindowTitle("Khoj - Configure")
|
||||||
@@ -33,24 +38,35 @@ class ConfigureScreen(QtWidgets.QDialog):
|
|||||||
# Add Settings Panels for each Search Type to Configure Window Layout
|
# Add Settings Panels for each Search Type to Configure Window Layout
|
||||||
self.settings_panels = []
|
self.settings_panels = []
|
||||||
for search_type in SearchType:
|
for search_type in SearchType:
|
||||||
self.settings_panels += [self.add_settings_panel(search_type, layout)]
|
current_content_config = self.config['content-type'].get(search_type, {})
|
||||||
|
self.settings_panels += [self.add_settings_panel(search_type, current_content_config, layout)]
|
||||||
self.add_action_panel(layout)
|
self.add_action_panel(layout)
|
||||||
|
|
||||||
def add_settings_panel(self, search_type: SearchType, parent_layout: QtWidgets.QLayout):
|
def add_settings_panel(self, search_type: SearchType, current_content_config: dict, parent_layout: QtWidgets.QLayout):
|
||||||
"Add Settings Panel for specified Search Type. Toggle Editable Search Types"
|
"Add Settings Panel for specified Search Type. Toggle Editable Search Types"
|
||||||
|
# Get current files from config for given search type
|
||||||
|
if search_type == SearchType.Image:
|
||||||
|
current_content_files = current_content_config.get('input-directories', [])
|
||||||
|
else:
|
||||||
|
current_content_files = current_content_config.get('input-files', [])
|
||||||
|
|
||||||
|
# Create widgets to display settings for given search type
|
||||||
search_type_settings = QtWidgets.QWidget()
|
search_type_settings = QtWidgets.QWidget()
|
||||||
search_type_layout = QtWidgets.QVBoxLayout(search_type_settings)
|
search_type_layout = QtWidgets.QVBoxLayout(search_type_settings)
|
||||||
|
|
||||||
enable_search_type = CheckBox(f"Search {search_type.name}", search_type)
|
enable_search_type = CheckBox(f"Search {search_type.name}", search_type)
|
||||||
input_files = FileBrowser(f'{search_type.name} Files', search_type)
|
# Add file browser to set input files for given search type
|
||||||
input_files.setEnabled(enable_search_type.isChecked())
|
input_files = FileBrowser(f'{search_type.name} Files', search_type, current_content_files)
|
||||||
|
|
||||||
|
# Set enabled/disabled based on checkbox state
|
||||||
|
enable_search_type.setChecked(len(current_content_files) > 0)
|
||||||
|
input_files.setEnabled(enable_search_type.isChecked())
|
||||||
enable_search_type.stateChanged.connect(lambda _: input_files.setEnabled(enable_search_type.isChecked()))
|
enable_search_type.stateChanged.connect(lambda _: input_files.setEnabled(enable_search_type.isChecked()))
|
||||||
|
|
||||||
|
# Add setting widgets for given search type to panel
|
||||||
search_type_layout.addWidget(enable_search_type)
|
search_type_layout.addWidget(enable_search_type)
|
||||||
search_type_layout.addWidget(input_files)
|
search_type_layout.addWidget(input_files)
|
||||||
|
|
||||||
parent_layout.addWidget(search_type_settings)
|
parent_layout.addWidget(search_type_settings)
|
||||||
|
|
||||||
return search_type_settings
|
return search_type_settings
|
||||||
|
|
||||||
def add_action_panel(self, parent_layout: QtWidgets.QLayout):
|
def add_action_panel(self, parent_layout: QtWidgets.QLayout):
|
||||||
@@ -66,23 +82,22 @@ class ConfigureScreen(QtWidgets.QDialog):
|
|||||||
|
|
||||||
def save_settings(self, _):
|
def save_settings(self, _):
|
||||||
"Save the settings to khoj.yml"
|
"Save the settings to khoj.yml"
|
||||||
# Load the default config
|
# Update config with settings from UI
|
||||||
config = yaml_utils.load_config_from_file(constants.app_root_directory / 'config/khoj_sample.yml')
|
|
||||||
|
|
||||||
# Update the default config with the settings from the UI
|
|
||||||
for settings_panel in self.settings_panels:
|
for settings_panel in self.settings_panels:
|
||||||
for child in settings_panel.children():
|
for child in settings_panel.children():
|
||||||
|
if isinstance(child, (CheckBox, FileBrowser)) and child.search_type not in self.config['content-type']:
|
||||||
|
continue
|
||||||
if isinstance(child, CheckBox) and not child.isChecked():
|
if isinstance(child, CheckBox) and not child.isChecked():
|
||||||
del config['content-type'][child.search_type]
|
del self.config['content-type'][child.search_type]
|
||||||
elif isinstance(child, FileBrowser) and child.search_type in config['content-type']:
|
elif isinstance(child, FileBrowser):
|
||||||
config['content-type'][child.search_type]['input-files'] = child.getPaths()
|
self.config['content-type'][child.search_type]['input-files'] = child.getPaths()
|
||||||
print(f"{child.search_type} files are {child.getPaths()}")
|
print(f"{child.search_type} files are {child.getPaths()}")
|
||||||
|
|
||||||
# Save the config to app config file
|
# Save the config to app config file
|
||||||
del config['processor']['conversation']
|
del self.config['processor']['conversation']
|
||||||
yaml_utils.save_config_to_file(config, self.config_file)
|
yaml_utils.save_config_to_file(self.config, self.config_file)
|
||||||
|
|
||||||
# Load config from app config file
|
# Load parsed, validated config from app config file
|
||||||
args = cli(state.cli_args)
|
args = cli(state.cli_args)
|
||||||
|
|
||||||
# Configure server with loaded config
|
# Configure server with loaded config
|
||||||
@@ -94,4 +109,4 @@ class ConfigureScreen(QtWidgets.QDialog):
|
|||||||
class CheckBox(QtWidgets.QCheckBox):
|
class CheckBox(QtWidgets.QCheckBox):
|
||||||
def __init__(self, text, search_type: SearchType, parent=None):
|
def __init__(self, text, search_type: SearchType, parent=None):
|
||||||
self.search_type = search_type
|
self.search_type = search_type
|
||||||
super(CheckBox, self).__init__(text, parent=parent)
|
super(CheckBox, self).__init__(text, parent=parent)
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ from src.utils.config import SearchType
|
|||||||
|
|
||||||
|
|
||||||
class FileBrowser(QtWidgets.QWidget):
|
class FileBrowser(QtWidgets.QWidget):
|
||||||
def __init__(self, title, search_type: SearchType=None):
|
def __init__(self, title, search_type: SearchType=None, default_files=[]):
|
||||||
QtWidgets.QWidget.__init__(self)
|
QtWidgets.QWidget.__init__(self)
|
||||||
layout = QtWidgets.QHBoxLayout()
|
layout = QtWidgets.QHBoxLayout()
|
||||||
self.setLayout(layout)
|
self.setLayout(layout)
|
||||||
@@ -15,8 +15,7 @@ class FileBrowser(QtWidgets.QWidget):
|
|||||||
|
|
||||||
self.filter_name = self.getFileFilter(search_type)
|
self.filter_name = self.getFileFilter(search_type)
|
||||||
self.dirpath = QDir.homePath()
|
self.dirpath = QDir.homePath()
|
||||||
self.filepaths = []
|
|
||||||
|
|
||||||
self.label = QtWidgets.QLabel()
|
self.label = QtWidgets.QLabel()
|
||||||
self.label.setText(title)
|
self.label.setText(title)
|
||||||
self.label.setFixedWidth(95)
|
self.label.setFixedWidth(95)
|
||||||
@@ -24,6 +23,7 @@ class FileBrowser(QtWidgets.QWidget):
|
|||||||
|
|
||||||
self.lineEdit = QtWidgets.QLineEdit(self)
|
self.lineEdit = QtWidgets.QLineEdit(self)
|
||||||
self.lineEdit.setFixedWidth(180)
|
self.lineEdit.setFixedWidth(180)
|
||||||
|
self.setFiles(default_files)
|
||||||
|
|
||||||
layout.addWidget(self.lineEdit)
|
layout.addWidget(self.lineEdit)
|
||||||
|
|
||||||
@@ -51,14 +51,18 @@ class FileBrowser(QtWidgets.QWidget):
|
|||||||
self.dirpath = path
|
self.dirpath = path
|
||||||
|
|
||||||
def getFile(self):
|
def getFile(self):
|
||||||
self.filepaths = []
|
filepaths = []
|
||||||
if self.search_type == SearchType.Image:
|
if self.search_type == SearchType.Image:
|
||||||
self.filepaths.append(QtWidgets.QFileDialog.getExistingDirectory(self, caption='Choose Directory',
|
filepaths.append(QtWidgets.QFileDialog.getExistingDirectory(self, caption='Choose Directory',
|
||||||
directory=self.dirpath))
|
directory=self.dirpath))
|
||||||
else:
|
else:
|
||||||
self.filepaths.extend(QtWidgets.QFileDialog.getOpenFileNames(self, caption='Choose Files',
|
filepaths.extend(QtWidgets.QFileDialog.getOpenFileNames(self, caption='Choose Files',
|
||||||
directory=self.dirpath,
|
directory=self.dirpath,
|
||||||
filter=self.filter_name)[0])
|
filter=self.filter_name)[0])
|
||||||
|
self.setFiles(filepaths)
|
||||||
|
|
||||||
|
def setFiles(self, paths):
|
||||||
|
self.filepaths = paths
|
||||||
if len(self.filepaths) == 0:
|
if len(self.filepaths) == 0:
|
||||||
return
|
return
|
||||||
elif len(self.filepaths) == 1:
|
elif len(self.filepaths) == 1:
|
||||||
|
|||||||
Reference in New Issue
Block a user