Add config, persistent window size, save warning, start/end option
This commit is contained in:
parent
dc07939025
commit
a6723d282b
1
.gitignore
vendored
1
.gitignore
vendored
@ -4,3 +4,4 @@
|
|||||||
|
|
||||||
# Ignore LoadOrderEditoDD.spec file
|
# Ignore LoadOrderEditoDD.spec file
|
||||||
LoadOrderEditorDD.spec
|
LoadOrderEditorDD.spec
|
||||||
|
config.ini
|
@ -1,54 +1,99 @@
|
|||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
import json
|
||||||
|
import shutil
|
||||||
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QListWidget, QPushButton, QListWidgetItem, QMessageBox
|
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QListWidget, QPushButton, QListWidgetItem, QMessageBox
|
||||||
from PyQt5.QtCore import Qt
|
from PyQt5.QtCore import Qt
|
||||||
import json
|
import configparser
|
||||||
import qtvscodestyle
|
import qtvscodestyle
|
||||||
import shutil
|
|
||||||
|
|
||||||
class DragDropApp(QWidget):
|
class DragDropApp(QWidget):
|
||||||
def __init__(self, mod_dir):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.setWindowTitle('Drag and Drop JSON Reorder')
|
self.setWindowTitle('Drag and Drop JSON Reorder')
|
||||||
self.resize(400, 300)
|
|
||||||
|
|
||||||
|
# Initialize window size from config.ini
|
||||||
|
self.initWindowSize()
|
||||||
|
|
||||||
|
self.initUI()
|
||||||
|
|
||||||
|
def initWindowSize(self):
|
||||||
|
self.mod_dir = os.getcwd()
|
||||||
|
self.config = configparser.ConfigParser()
|
||||||
|
self.config_path = os.path.join(self.mod_dir, 'config.ini')
|
||||||
|
|
||||||
|
if not os.path.exists(self.config_path):
|
||||||
|
# Create config.ini with default values
|
||||||
|
self.createDefaultConfig()
|
||||||
|
|
||||||
|
# Read width and height from config.ini
|
||||||
|
self.config.read(self.config_path)
|
||||||
|
width = int(self.config.get('DEFAULT', 'width', fallback='400'))
|
||||||
|
height = int(self.config.get('DEFAULT', 'height', fallback='300'))
|
||||||
|
self.resize(width, height)
|
||||||
|
|
||||||
|
def createDefaultConfig(self):
|
||||||
|
self.config['DEFAULT'] = {
|
||||||
|
'sptBaseDir': '',
|
||||||
|
'addNewItemsTo': 'end',
|
||||||
|
'reminder': 'True',
|
||||||
|
'width': '400',
|
||||||
|
'height': '300'
|
||||||
|
}
|
||||||
|
with open(self.config_path, 'w') as configfile:
|
||||||
|
self.config.write(configfile)
|
||||||
|
|
||||||
|
def initUI(self):
|
||||||
layout = QVBoxLayout(self)
|
layout = QVBoxLayout(self)
|
||||||
|
|
||||||
self.list_widget = QListWidget()
|
self.list_widget = QListWidget()
|
||||||
layout.addWidget(self.list_widget)
|
layout.addWidget(self.list_widget)
|
||||||
|
|
||||||
self.save_button = QPushButton('Save Order')
|
self.save_button = QPushButton('Save Order')
|
||||||
layout.addWidget(self.save_button)
|
layout.addWidget(self.save_button)
|
||||||
|
|
||||||
self.mod_dir = mod_dir
|
self.mod_dir = os.getcwd()
|
||||||
|
self.config = configparser.ConfigParser()
|
||||||
|
self.config_path = os.path.join(self.mod_dir, 'config.ini')
|
||||||
|
self.loadConfig()
|
||||||
|
|
||||||
if not self.check_mods_directory():
|
if not self.checkModsDirectory():
|
||||||
return
|
return
|
||||||
|
|
||||||
self.load_data(mod_dir)
|
self.initial_order = []
|
||||||
|
self.loadData(self.mod_dir)
|
||||||
|
|
||||||
self.list_widget.setDragDropMode(QListWidget.InternalMove)
|
self.list_widget.setDragDropMode(QListWidget.InternalMove)
|
||||||
self.list_widget.setAlternatingRowColors(True)
|
self.list_widget.setAlternatingRowColors(True)
|
||||||
|
|
||||||
self.save_button.clicked.connect(lambda: self.save_order(mod_dir))
|
self.save_button.clicked.connect(lambda: self.saveOrder(self.mod_dir))
|
||||||
|
|
||||||
def check_mods_directory(self):
|
# Apply the dark theme
|
||||||
|
stylesheet = qtvscodestyle.load_stylesheet(qtvscodestyle.Theme.DARK_VS)
|
||||||
|
self.setStyleSheet(stylesheet)
|
||||||
|
|
||||||
|
def loadConfig(self):
|
||||||
|
self.config.read(self.config_path)
|
||||||
|
self.spt_base_dir = self.config.get('DEFAULT', 'sptBaseDir', fallback='')
|
||||||
|
self.add_new_items_to = self.config.get('DEFAULT', 'addNewItemsTo', fallback='end')
|
||||||
|
self.reminder = self.config.getboolean('DEFAULT', 'reminder', fallback=True)
|
||||||
|
self.width = int(self.config.get('DEFAULT', 'width', fallback='400'))
|
||||||
|
self.height = int(self.config.get('DEFAULT', 'height', fallback='300'))
|
||||||
|
|
||||||
|
def checkModsDirectory(self):
|
||||||
mods_dir = os.path.join(self.mod_dir, 'user', 'mods')
|
mods_dir = os.path.join(self.mod_dir, 'user', 'mods')
|
||||||
if not os.path.exists(mods_dir):
|
if not os.path.exists(mods_dir):
|
||||||
error_message = f"Could not find user/mods. This exe belongs in the base directory of SPT-AKI!\nCurrent Directory: {self.mod_dir}"
|
error_message = f"Could not find user/mods. This exe belongs in the base directory of SPT-AKI!\nCurrent Directory: {self.mod_dir}\nconfig.ini has been created in the current directory, where you can set the path if you prefer."
|
||||||
QMessageBox.critical(self, "Error", error_message)
|
QMessageBox.critical(self, "Error", error_message)
|
||||||
sys.exit(1) # Add this line to exit the application
|
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def load_data(self, mod_dir):
|
def loadData(self, mod_dir):
|
||||||
print("Loading mod data...")
|
print("Loading mod data...")
|
||||||
mod_info = []
|
mod_info = []
|
||||||
order_file_path = os.path.join(mod_dir, 'user', 'mods', 'order.json')
|
order_file_path = os.path.join(mod_dir, 'user', 'mods', 'order.json')
|
||||||
if os.path.exists(order_file_path):
|
if os.path.exists(order_file_path):
|
||||||
with open(order_file_path, 'r') as order_file:
|
with open(order_file_path, 'r') as order_file:
|
||||||
order_data = json.load(order_file)
|
order_data = json.load(order_file)
|
||||||
ordered_mods = order_data.get("order", []) # Get the ordered list of mods from order.json
|
ordered_mods = order_data.get("order", [])
|
||||||
|
|
||||||
for entry in os.scandir(os.path.join(mod_dir, "user", "mods")):
|
for entry in os.scandir(os.path.join(mod_dir, "user", "mods")):
|
||||||
if entry.is_dir():
|
if entry.is_dir():
|
||||||
@ -57,65 +102,93 @@ class DragDropApp(QWidget):
|
|||||||
print(f"Found package.json in {entry.path}")
|
print(f"Found package.json in {entry.path}")
|
||||||
with open(package_file, 'r') as file:
|
with open(package_file, 'r') as file:
|
||||||
data = json.load(file)
|
data = json.load(file)
|
||||||
folder_name = os.path.basename(entry.path) # Extract folder name
|
folder_name = os.path.basename(entry.path)
|
||||||
mod_info.append({
|
mod_info.append({
|
||||||
"name": data["name"],
|
"name": data["name"],
|
||||||
"folder_name": folder_name, # Use folder name as mod identifier
|
"folder_name": folder_name,
|
||||||
"version": data["version"],
|
"version": data["version"],
|
||||||
"author": data["author"],
|
"author": data["author"],
|
||||||
"akiVersion": data.get("akiVersion", "")
|
"akiVersion": data.get("akiVersion", "")
|
||||||
})
|
})
|
||||||
print(f"akiVersion for {folder_name}: {data.get('akiVersion', '')}")
|
print(f"akiVersion for {folder_name}: {data.get('akiVersion', '')}")
|
||||||
|
|
||||||
# Reorder mod_info based on order.json
|
|
||||||
if os.path.exists(order_file_path):
|
if os.path.exists(order_file_path):
|
||||||
ordered_mod_info = []
|
ordered_mod_info = []
|
||||||
for mod_name in ordered_mods:
|
for mod_name in ordered_mods:
|
||||||
for mod in mod_info:
|
for mod in mod_info:
|
||||||
if mod["folder_name"] == mod_name:
|
if mod["folder_name"] == mod_name:
|
||||||
ordered_mod_info.append(mod)
|
ordered_mod_info.append(mod)
|
||||||
mod_info.remove(mod) # Remove matched mod from mod_info list
|
mod_info.remove(mod)
|
||||||
break
|
break
|
||||||
ordered_mod_info.extend(mod_info) # Append remaining mods to the ordered list
|
ordered_mod_info.extend(mod_info)
|
||||||
mod_info = ordered_mod_info
|
mod_info = ordered_mod_info
|
||||||
|
|
||||||
for mod in mod_info:
|
if self.add_new_items_to == 'start':
|
||||||
list_item = QListWidgetItem(mod["folder_name"])
|
self.list_widget.clear()
|
||||||
tooltip_text = "\n".join([f"{key}: {value}" for key, value in mod.items()])
|
for mod in mod_info:
|
||||||
list_item.setToolTip(tooltip_text)
|
self.addListItem(mod, 0)
|
||||||
self.list_widget.addItem(list_item)
|
elif self.add_new_items_to == 'end':
|
||||||
|
for mod in mod_info:
|
||||||
|
self.addListItem(mod)
|
||||||
|
|
||||||
def save_order(self, mod_dir):
|
self.initial_order = [item.text() for item in [self.list_widget.item(i) for i in range(self.list_widget.count())]]
|
||||||
|
|
||||||
|
def addListItem(self, mod, position=None):
|
||||||
|
list_item = QListWidgetItem(mod["folder_name"])
|
||||||
|
tooltip_text = "\n".join([f"{key}: {value}" for key, value in mod.items()])
|
||||||
|
list_item.setToolTip(tooltip_text)
|
||||||
|
if position is None:
|
||||||
|
self.list_widget.addItem(list_item)
|
||||||
|
else:
|
||||||
|
self.list_widget.insertItem(position, list_item)
|
||||||
|
|
||||||
|
def saveOrder(self, mod_dir):
|
||||||
print("Saving order...")
|
print("Saving order...")
|
||||||
print(f"Mod directory: {mod_dir}")
|
print(f"Mod directory: {mod_dir}")
|
||||||
new_order = [item.text() for item in [self.list_widget.item(i) for i in range(self.list_widget.count())]]
|
new_order = [self.list_widget.item(i).text() for i in range(self.list_widget.count())]
|
||||||
print(f"New order: {new_order}")
|
print(f"New order: {new_order}")
|
||||||
order_file_path = os.path.join(mod_dir, 'user', 'mods', 'order.json')
|
order_file_path = os.path.join(mod_dir, 'user', 'mods', 'order.json')
|
||||||
backup_file_path = os.path.join(mod_dir, 'user', 'mods', 'order.bkp')
|
backup_file_path = os.path.join(mod_dir, 'user', 'mods', 'order.bkp')
|
||||||
|
|
||||||
# Backup existing order.json as order.bkp
|
|
||||||
try:
|
try:
|
||||||
shutil.copy(order_file_path, backup_file_path)
|
shutil.copy(order_file_path, backup_file_path)
|
||||||
print('Backup created successfully!')
|
print('Backup created successfully!')
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f'Error creating backup: {e}')
|
print(f'Error creating backup: {e}')
|
||||||
|
|
||||||
# Save new order to order.json
|
|
||||||
with open(order_file_path, 'w') as file:
|
with open(order_file_path, 'w') as file:
|
||||||
json.dump({"order": new_order}, file, indent=4)
|
json.dump({"order": new_order}, file, indent=4)
|
||||||
print('Order saved successfully!')
|
print('Order saved successfully!')
|
||||||
|
|
||||||
|
self.initial_order = new_order
|
||||||
|
|
||||||
|
self.config.set('DEFAULT', 'width', str(self.width))
|
||||||
|
self.config.set('DEFAULT', 'height', str(self.height))
|
||||||
|
with open(self.config_path, 'w') as configfile:
|
||||||
|
self.config.write(configfile)
|
||||||
|
|
||||||
|
def resizeEvent(self, event):
|
||||||
|
self.width = self.size().width()
|
||||||
|
self.height = self.size().height()
|
||||||
|
event.accept()
|
||||||
|
|
||||||
|
def closeEvent(self, event):
|
||||||
|
if self.reminder and self.list_widget.count() > 0:
|
||||||
|
current_order = [self.list_widget.item(i).text() for i in range(self.list_widget.count())]
|
||||||
|
if current_order != self.initial_order:
|
||||||
|
reply = QMessageBox.question(self, 'Reminder', 'You have unsaved changes.\nSave before closing?', QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
|
||||||
|
if reply == QMessageBox.Yes:
|
||||||
|
self.saveOrder(self.mod_dir)
|
||||||
|
print(f"Current width: {self.width}, Current height: {self.height}")
|
||||||
|
self.config.set('DEFAULT', 'width', str(self.width))
|
||||||
|
self.config.set('DEFAULT', 'height', str(self.height))
|
||||||
|
print(f"Updated width: {self.width}, Updated height: {self.height}")
|
||||||
|
with open(self.config_path, 'w') as configfile:
|
||||||
|
self.config.write(configfile)
|
||||||
|
event.accept()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
mod_directory = os.getcwd() # Assume current directory if --sptakidir is not given
|
|
||||||
if len(sys.argv) >= 3 and sys.argv[1] == "--sptakidir":
|
|
||||||
mod_directory = sys.argv[2]
|
|
||||||
|
|
||||||
app = QApplication(sys.argv)
|
app = QApplication(sys.argv)
|
||||||
|
window = DragDropApp()
|
||||||
# Apply VS Code's default theme
|
|
||||||
stylesheet = qtvscodestyle.load_stylesheet(qtvscodestyle.Theme.DARK_VS)
|
|
||||||
app.setStyleSheet(stylesheet)
|
|
||||||
print(f"mod_directory: {mod_directory}")
|
|
||||||
window = DragDropApp(mod_directory)
|
|
||||||
window.show()
|
window.show()
|
||||||
sys.exit(app.exec_())
|
sys.exit(app.exec_())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user