Add files for each search type. Extract config on clicking start

- Only allow adding files with appropriate file extension for each search type
  - e.g .org for org-mode search, directory for image search

- Extract file paths added to config and enablement state of each search type
  - This extracted state will be used to populate the khoj.yml config file
This commit is contained in:
Debanjum Singh Solanky
2022-08-10 03:23:37 +03:00
parent d74134e6cc
commit daef276fd1
2 changed files with 103 additions and 14 deletions

View File

@@ -0,0 +1,75 @@
# External Packages
from PyQt6 import QtWidgets
from PyQt6.QtCore import QDir
# Internal Packages
from src.utils.config import SearchType
class FileBrowser(QtWidgets.QWidget):
def __init__(self, title, search_type: SearchType=None):
QtWidgets.QWidget.__init__(self)
layout = QtWidgets.QHBoxLayout()
self.setLayout(layout)
self.search_type = search_type
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)
layout.addWidget(self.label)
self.lineEdit = QtWidgets.QLineEdit(self)
self.lineEdit.setFixedWidth(180)
layout.addWidget(self.lineEdit)
self.button = QtWidgets.QPushButton('Add')
self.button.clicked.connect(self.getFile)
layout.addWidget(self.button)
layout.addStretch()
def setMode(self, search_type):
self.search_type = search_type
def getFileFilter(self, search_type):
if search_type == SearchType.Org:
return 'Org-Mode Files (*.org)'
elif search_type == SearchType.Ledger:
return 'Beancount Files (*.bean *.beancount)'
elif search_type == SearchType.Markdown:
return 'Markdown Files (*.md *.markdown)'
elif search_type == SearchType.Music:
return 'Org-Music Files (*.org)'
elif search_type == SearchType.Image:
return 'Images (*.jp[e]g)'
def setDefaultDir(self, path):
self.dirpath = path
def getFile(self):
self.filepaths = []
if self.search_type == SearchType.Image:
self.filepaths.append(QtWidgets.QFileDialog.getExistingDirectory(self, caption='Choose Directory',
directory=self.dirpath))
else:
self.filepaths.extend(QtWidgets.QFileDialog.getOpenFileNames(self, caption='Choose Files',
directory=self.dirpath,
filter=self.filter_name)[0])
if len(self.filepaths) == 0:
return
elif len(self.filepaths) == 1:
self.lineEdit.setText(self.filepaths[0])
else:
self.lineEdit.setText(",".join(self.filepaths))
def setLabelWidth(self, width):
self.label.setFixedWidth(width)
def setlineEditWidth(self, width):
self.lineEdit.setFixedWidth(width)
def getPaths(self):
return self.filepaths