mirror of
https://github.com/khoj-ai/khoj.git
synced 2024-11-27 17:35:07 +01:00
Initialize Settings on Config Screen with Existing Settings from File
This commit is contained in:
parent
3cec6229ad
commit
c1fcf44405
2 changed files with 42 additions and 23 deletions
|
@ -22,6 +22,11 @@ class ConfigureScreen(QtWidgets.QDialog):
|
|||
super(ConfigureScreen, self).__init__(parent=parent)
|
||||
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
|
||||
self.setWindowFlags(Qt.WindowType.WindowStaysOnTopHint)
|
||||
self.setWindowTitle("Khoj - Configure")
|
||||
|
@ -33,24 +38,35 @@ class ConfigureScreen(QtWidgets.QDialog):
|
|||
# Add Settings Panels for each Search Type to Configure Window Layout
|
||||
self.settings_panels = []
|
||||
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)
|
||||
|
||||
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"
|
||||
# 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_layout = QtWidgets.QVBoxLayout(search_type_settings)
|
||||
|
||||
enable_search_type = CheckBox(f"Search {search_type.name}", search_type)
|
||||
input_files = FileBrowser(f'{search_type.name} Files', search_type)
|
||||
input_files.setEnabled(enable_search_type.isChecked())
|
||||
# Add file browser to set input files for given search type
|
||||
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()))
|
||||
|
||||
# Add setting widgets for given search type to panel
|
||||
search_type_layout.addWidget(enable_search_type)
|
||||
search_type_layout.addWidget(input_files)
|
||||
|
||||
parent_layout.addWidget(search_type_settings)
|
||||
|
||||
return search_type_settings
|
||||
|
||||
def add_action_panel(self, parent_layout: QtWidgets.QLayout):
|
||||
|
@ -66,23 +82,22 @@ class ConfigureScreen(QtWidgets.QDialog):
|
|||
|
||||
def save_settings(self, _):
|
||||
"Save the settings to khoj.yml"
|
||||
# Load the default config
|
||||
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
|
||||
# Update config with settings from UI
|
||||
for settings_panel in self.settings_panels:
|
||||
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():
|
||||
del config['content-type'][child.search_type]
|
||||
elif isinstance(child, FileBrowser) and child.search_type in config['content-type']:
|
||||
config['content-type'][child.search_type]['input-files'] = child.getPaths()
|
||||
del self.config['content-type'][child.search_type]
|
||||
elif isinstance(child, FileBrowser):
|
||||
self.config['content-type'][child.search_type]['input-files'] = child.getPaths()
|
||||
print(f"{child.search_type} files are {child.getPaths()}")
|
||||
|
||||
# Save the config to app config file
|
||||
del config['processor']['conversation']
|
||||
yaml_utils.save_config_to_file(config, self.config_file)
|
||||
del self.config['processor']['conversation']
|
||||
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)
|
||||
|
||||
# Configure server with loaded config
|
||||
|
@ -94,4 +109,4 @@ class ConfigureScreen(QtWidgets.QDialog):
|
|||
class CheckBox(QtWidgets.QCheckBox):
|
||||
def __init__(self, text, search_type: SearchType, parent=None):
|
||||
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):
|
||||
def __init__(self, title, search_type: SearchType=None):
|
||||
def __init__(self, title, search_type: SearchType=None, default_files=[]):
|
||||
QtWidgets.QWidget.__init__(self)
|
||||
layout = QtWidgets.QHBoxLayout()
|
||||
self.setLayout(layout)
|
||||
|
@ -15,8 +15,7 @@ class FileBrowser(QtWidgets.QWidget):
|
|||
|
||||
self.filter_name = self.getFileFilter(search_type)
|
||||
self.dirpath = QDir.homePath()
|
||||
self.filepaths = []
|
||||
|
||||
|
||||
self.label = QtWidgets.QLabel()
|
||||
self.label.setText(title)
|
||||
self.label.setFixedWidth(95)
|
||||
|
@ -24,6 +23,7 @@ class FileBrowser(QtWidgets.QWidget):
|
|||
|
||||
self.lineEdit = QtWidgets.QLineEdit(self)
|
||||
self.lineEdit.setFixedWidth(180)
|
||||
self.setFiles(default_files)
|
||||
|
||||
layout.addWidget(self.lineEdit)
|
||||
|
||||
|
@ -51,14 +51,18 @@ class FileBrowser(QtWidgets.QWidget):
|
|||
self.dirpath = path
|
||||
|
||||
def getFile(self):
|
||||
self.filepaths = []
|
||||
filepaths = []
|
||||
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))
|
||||
else:
|
||||
self.filepaths.extend(QtWidgets.QFileDialog.getOpenFileNames(self, caption='Choose Files',
|
||||
filepaths.extend(QtWidgets.QFileDialog.getOpenFileNames(self, caption='Choose Files',
|
||||
directory=self.dirpath,
|
||||
filter=self.filter_name)[0])
|
||||
self.setFiles(filepaths)
|
||||
|
||||
def setFiles(self, paths):
|
||||
self.filepaths = paths
|
||||
if len(self.filepaths) == 0:
|
||||
return
|
||||
elif len(self.filepaths) == 1:
|
||||
|
|
Loading…
Reference in a new issue