chore: Add settings dialog and functionality
This commit is contained in:
102
app.py
102
app.py
@ -1,11 +1,15 @@
|
|||||||
import sys
|
import sys
|
||||||
|
import json
|
||||||
|
import os
|
||||||
from PyQt5.QtWidgets import (
|
from PyQt5.QtWidgets import (
|
||||||
QApplication, QWidget, QVBoxLayout, QHBoxLayout,
|
QApplication, QWidget, QVBoxLayout, QHBoxLayout,
|
||||||
QLineEdit, QPushButton, QListWidget, QTextEdit, QComboBox, QMessageBox, QSpacerItem, QSizePolicy
|
QLineEdit, QPushButton, QListWidget, QTextEdit, QComboBox,
|
||||||
|
QMessageBox, QSpacerItem, QSizePolicy, QLabel, QFileDialog, QDialog, QFormLayout
|
||||||
)
|
)
|
||||||
from PyQt5.QtGui import QIcon, QPixmap
|
from PyQt5.QtGui import QIcon, QPixmap
|
||||||
from PyQt5.QtCore import Qt
|
from PyQt5.QtCore import Qt, QUrl
|
||||||
from PyQt5.QtSvg import QSvgWidget
|
from PyQt5.QtSvg import QSvgWidget
|
||||||
|
from PyQt5.QtGui import QDesktopServices
|
||||||
|
|
||||||
|
|
||||||
class URLManager(QWidget):
|
class URLManager(QWidget):
|
||||||
@ -30,12 +34,14 @@ class URLManager(QWidget):
|
|||||||
self.settings_button = QPushButton()
|
self.settings_button = QPushButton()
|
||||||
self.settings_button.setFixedHeight(self.url_input.sizeHint().height())
|
self.settings_button.setFixedHeight(self.url_input.sizeHint().height())
|
||||||
self.settings_button.setFixedWidth(self.url_input.sizeHint().height())
|
self.settings_button.setFixedWidth(self.url_input.sizeHint().height())
|
||||||
|
self.settings_button.clicked.connect(self.show_settings_dialog)
|
||||||
self.url_layout.addWidget(self.settings_button)
|
self.url_layout.addWidget(self.settings_button)
|
||||||
|
|
||||||
# Info button
|
# Info button
|
||||||
self.info_button = QPushButton()
|
self.info_button = QPushButton()
|
||||||
self.info_button.setFixedHeight(self.url_input.sizeHint().height())
|
self.info_button.setFixedHeight(self.url_input.sizeHint().height())
|
||||||
self.info_button.setFixedWidth(self.url_input.sizeHint().height())
|
self.info_button.setFixedWidth(self.url_input.sizeHint().height())
|
||||||
|
self.info_button.clicked.connect(self.show_info_dialog)
|
||||||
self.url_layout.addWidget(self.info_button)
|
self.url_layout.addWidget(self.info_button)
|
||||||
|
|
||||||
self.layout.addLayout(self.url_layout)
|
self.layout.addLayout(self.url_layout)
|
||||||
@ -79,6 +85,9 @@ class URLManager(QWidget):
|
|||||||
|
|
||||||
self.urls = {}
|
self.urls = {}
|
||||||
|
|
||||||
|
# Load settings
|
||||||
|
self.load_settings()
|
||||||
|
|
||||||
def update_icon_color(self):
|
def update_icon_color(self):
|
||||||
# Heuristic to check if the application is in dark mode
|
# Heuristic to check if the application is in dark mode
|
||||||
palette = self.palette()
|
palette = self.palette()
|
||||||
@ -137,6 +146,95 @@ class URLManager(QWidget):
|
|||||||
QMessageBox.information(self, "Search", f"Searching for: {search_term}\nURL: {search_url}")
|
QMessageBox.information(self, "Search", f"Searching for: {search_term}\nURL: {search_url}")
|
||||||
self.search_input.clear()
|
self.search_input.clear()
|
||||||
|
|
||||||
|
def show_info_dialog(self):
|
||||||
|
dialog = QDialog(self)
|
||||||
|
dialog.setWindowTitle("About tabRemember")
|
||||||
|
|
||||||
|
layout = QVBoxLayout()
|
||||||
|
|
||||||
|
# SVG logo
|
||||||
|
svg_widget = QSvgWidget('assets/logo.svg')
|
||||||
|
svg_widget.setFixedSize(256, 256)
|
||||||
|
layout.addWidget(svg_widget, alignment=Qt.AlignCenter)
|
||||||
|
|
||||||
|
# Written by label
|
||||||
|
written_by_label = QLabel("Written by Axel Rafn Benediktsson")
|
||||||
|
layout.addWidget(written_by_label, alignment=Qt.AlignCenter)
|
||||||
|
|
||||||
|
# Link label
|
||||||
|
link_label = QLabel()
|
||||||
|
link_label.setText('<a href="https://git.axelrafn.is/axelrafn/tabRemember">https://git.axelrafn.is/axelrafn/tabRemember</a>')
|
||||||
|
link_label.setOpenExternalLinks(True)
|
||||||
|
layout.addWidget(link_label, alignment=Qt.AlignCenter)
|
||||||
|
|
||||||
|
dialog.setLayout(layout)
|
||||||
|
dialog.exec_()
|
||||||
|
|
||||||
|
def show_settings_dialog(self):
|
||||||
|
dialog = SettingsDialog(self)
|
||||||
|
dialog.exec_()
|
||||||
|
|
||||||
|
def load_settings(self):
|
||||||
|
settings_path = 'data/settings.json'
|
||||||
|
if os.path.exists(settings_path):
|
||||||
|
if os.path.getsize(settings_path) > 0:
|
||||||
|
with open(settings_path, 'r') as file:
|
||||||
|
settings = json.load(file)
|
||||||
|
# Use settings (for now, we only have the data_directory)
|
||||||
|
self.data_directory = settings.get('data_directory', 'data/')
|
||||||
|
else:
|
||||||
|
self.data_directory = 'data/'
|
||||||
|
self.save_settings()
|
||||||
|
else:
|
||||||
|
self.data_directory = 'data/'
|
||||||
|
self.save_settings()
|
||||||
|
|
||||||
|
def save_settings(self):
|
||||||
|
settings_path = 'data/settings.json'
|
||||||
|
os.makedirs(os.path.dirname(settings_path), exist_ok=True)
|
||||||
|
with open(settings_path, 'w') as file:
|
||||||
|
settings = {
|
||||||
|
'data_directory': self.data_directory
|
||||||
|
}
|
||||||
|
json.dump(settings, file)
|
||||||
|
|
||||||
|
|
||||||
|
class SettingsDialog(QDialog):
|
||||||
|
def __init__(self, parent=None):
|
||||||
|
super().__init__(parent)
|
||||||
|
self.setWindowTitle("Settings")
|
||||||
|
|
||||||
|
self.parent = parent
|
||||||
|
|
||||||
|
layout = QVBoxLayout()
|
||||||
|
|
||||||
|
# File/Directory selection
|
||||||
|
form_layout = QFormLayout()
|
||||||
|
self.directory_input = QLineEdit(self.parent.data_directory)
|
||||||
|
self.browse_button = QPushButton("Browse...")
|
||||||
|
self.browse_button.clicked.connect(self.browse_directory)
|
||||||
|
form_layout.addRow("Data Directory:", self.directory_input)
|
||||||
|
form_layout.addWidget(self.browse_button)
|
||||||
|
|
||||||
|
layout.addLayout(form_layout)
|
||||||
|
|
||||||
|
# Save button
|
||||||
|
self.save_button = QPushButton("Save")
|
||||||
|
self.save_button.clicked.connect(self.save_settings)
|
||||||
|
layout.addWidget(self.save_button)
|
||||||
|
|
||||||
|
self.setLayout(layout)
|
||||||
|
|
||||||
|
def browse_directory(self):
|
||||||
|
directory = QFileDialog.getExistingDirectory(self, "Select Directory")
|
||||||
|
if directory:
|
||||||
|
self.directory_input.setText(directory)
|
||||||
|
|
||||||
|
def save_settings(self):
|
||||||
|
self.parent.data_directory = self.directory_input.text()
|
||||||
|
self.parent.save_settings()
|
||||||
|
self.accept()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app = QApplication(sys.argv)
|
app = QApplication(sys.argv)
|
||||||
|
|||||||
1
data/settings.json
Normal file
1
data/settings.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"data_directory": "data/"}
|
||||||
Reference in New Issue
Block a user