Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add option to set a default file path for the select files option #8

Merged
merged 2 commits into from
May 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 98 additions & 58 deletions filekitty/app.py
Original file line number Diff line number Diff line change
@@ -1,93 +1,143 @@
import os

from PyQt5.QtGui import QIcon, QGuiApplication
from PyQt5.QtWidgets import QApplication, QWidget, QFileDialog, QVBoxLayout, QPushButton, QTextEdit, QLabel
from PyQt5.QtWidgets import QListWidget
from PyQt5.QtCore import QSettings
from PyQt5.QtGui import QIcon, QGuiApplication, QKeySequence
from PyQt5.QtWidgets import (QApplication, QWidget, QFileDialog, QVBoxLayout, QPushButton, QTextEdit,
QLabel, QListWidget, QDialog, QLineEdit, QHBoxLayout, QAction, QMenuBar, QMenu)

ICON_PATH = 'assets/icon/FileKitty-icon.png'

class PreferencesDialog(QDialog):
def __init__(self, parent=None):
super(PreferencesDialog, self).__init__(parent)
self.setWindowTitle('Preferences')
self.initUI()

def initUI(self):
layout = QVBoxLayout()

self.pathEdit = QLineEdit(self)
self.pathEdit.setPlaceholderText("Enter or select default file path...")
layout.addWidget(self.pathEdit)

btnBrowse = QPushButton("Browse...")
btnBrowse.clicked.connect(self.browsePath)
layout.addWidget(btnBrowse)

btnLayout = QHBoxLayout()
btnSave = QPushButton('Save')
btnCancel = QPushButton('Cancel')
btnLayout.addWidget(btnSave)
btnLayout.addWidget(btnCancel)

btnSave.clicked.connect(self.accept)
btnCancel.clicked.connect(self.reject)

layout.addLayout(btnLayout)
self.setLayout(layout)

def browsePath(self):
dir_path = QFileDialog.getExistingDirectory(self, "Select Default Directory")
if dir_path:
self.pathEdit.setText(dir_path)

def get_path(self):
return self.pathEdit.text()

def set_path(self, path):
self.pathEdit.setText(path)

class FilePicker(QWidget):
def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
self.setWindowTitle('FileKitty')
self.setWindowIcon(QIcon(ICON_PATH))
self.setGeometry(100, 100, 800, 600)
self.initUI()
self.createActions()
self.createMenu()

def initUI(self):
layout = QVBoxLayout(self)

# Add a QListWidget to display the selected files
self.fileList = QListWidget(self)
layout.addWidget(self.fileList)

self.textEdit = QTextEdit(self)
self.textEdit.setReadOnly(True)
layout.addWidget(self.textEdit)

# Line count label
self.lineCountLabel = QLabel('Lines ready to copy: 0', self)
layout.addWidget(self.lineCountLabel)

# Refresh button to reload the files
self.btnRefresh = QPushButton('🔄 Refresh Text from Files', self)
self.btnRefresh.clicked.connect(self.refreshFiles)
self.btnRefresh.setEnabled(False)
layout.addWidget(self.btnRefresh)

layout.setStretchFactor(self.fileList, 1)
layout.setStretchFactor(self.textEdit, 3)

btnOpen = QPushButton('📂 Select Files', self)
btnOpen = QPushButton('📂 Select Files', self)
btnOpen.clicked.connect(self.openFiles)
layout.addWidget(btnOpen)

self.btnCopy = QPushButton('📋 Copy to Clipboard', self)
self.btnCopy = QPushButton('📋 Copy to Clipboard', self)
self.btnCopy.clicked.connect(self.copyToClipboard)
self.btnCopy.setEnabled(False)
layout.addWidget(self.btnCopy)

# Calculate the appropriate heights using rounded pixel values
base_height = self.btnCopy.sizeHint().height()
increased_height = round(base_height * 2) # Double the base height for the copy button
slightly_increased_height = round(base_height * 1.1) # Increase by 10% for the open button

# Apply the calculated heights in the style sheets
self.btnCopy.setStyleSheet(
"QPushButton {min-height: %dpx; border-radius: 10px; border: 2px solid #555;}"
"QPushButton:pressed {background-color: #ccc;}" % increased_height)
btnOpen.setStyleSheet(
"QPushButton {min-height: %dpx; border-radius: 6px; border: 2px solid #555;}"
"QPushButton:pressed {background-color: #ccc;}" % slightly_increased_height)
self.btnRefresh.setStyleSheet(
"QPushButton {min-height: %dpx; border-radius: 6px; border: 2px solid #555;}"
"QPushButton:pressed {background-color: #ccc;}" % slightly_increased_height)
self.textEdit.textChanged.connect(self.updateCopyButtonState)

def createActions(self):
self.prefAction = QAction("Preferences", self)
self.prefAction.setShortcut(QKeySequence("Ctrl+,"))
self.prefAction.triggered.connect(self.showPreferences)

def createMenu(self):
menubar = QMenuBar(self)
appMenu = menubar.addMenu('FileKitty')
appMenu.addAction(self.prefAction)
self.layout().setMenuBar(menubar)

def showPreferences(self):
dialog = PreferencesDialog(self)
dialog.set_path(self.get_default_path())
if dialog.exec_():
new_path = dialog.get_path()
self.set_default_path(new_path)

def get_default_path(self):
settings = QSettings('YourCompany', 'FileKitty')
return settings.value('defaultPath', '')

def set_default_path(self, path):
settings = QSettings('YourCompany', 'FileKitty')
settings.setValue('defaultPath', path)

def openFiles(self):
default_path = self.get_default_path() or ""
options = QFileDialog.Options()
files, _ = QFileDialog.getOpenFileNames(self, "Select files to concatenate", "",
files, _ = QFileDialog.getOpenFileNames(self, "Select files to concatenate", default_path,
"All Files (*);;Text Files (*.txt)", options=options)
if files:
self.fileList.clear()
self.currentFiles = files
self.refreshFiles()
self.btnRefresh.setEnabled(True) # Enable the refresh button

common_prefix = os.path.commonpath(files)
common_prefix = os.path.dirname(common_prefix) if os.path.dirname(common_prefix) else common_prefix
concatenated_content = ""
for file in files:
relative_path = os.path.relpath(file, start=common_prefix)
self.fileList.addItem(relative_path)

concatenated_content += f"### `{relative_path}`\n\n```\n"
with open(file, 'r', encoding='utf-8') as file:
content = file.read()
concatenated_content += content
concatenated_content += "\n```\n\n"
self.btnRefresh.setEnabled(True)
concatenated_content = self.concatenate_files(files)
self.textEdit.setText(concatenated_content)

def concatenate_files(self, files):
common_prefix = os.path.commonpath(files)
common_prefix = os.path.dirname(common_prefix) if os.path.dirname(common_prefix) else common_prefix
concatenated_content = ""
for file in files:
relative_path = os.path.relpath(file, start=common_prefix)
self.fileList.addItem(relative_path)
concatenated_content += f"### `{relative_path}`\n\n```\n"
with open(file, 'r', encoding='utf-8') as file:
content = file.read()
concatenated_content += content
concatenated_content += "\n```\n\n"
return concatenated_content.rstrip()

def copyToClipboard(self):
clipboard = QGuiApplication.clipboard()
clipboard.setText(self.textEdit.toPlainText())
Expand All @@ -97,26 +147,16 @@ def updateCopyButtonState(self):
line_count = text.count('\n') + 1 if text else 0
self.lineCountLabel.setText(f'Lines ready to copy: {line_count}')
self.btnCopy.setEnabled(bool(text))
self.btnRefresh.setEnabled(bool(text))

def refreshFiles(self):
if hasattr(self, 'currentFiles') and self.currentFiles:
common_prefix = os.path.commonpath(self.currentFiles)
common_prefix = os.path.dirname(common_prefix) if os.path.dirname(common_prefix) else common_prefix
concatenated_content = ""
for file_path in self.currentFiles:
relative_path = os.path.relpath(file_path, start=common_prefix)
concatenated_content += f"### `{relative_path}`\n\n```\n"
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
concatenated_content += content
concatenated_content += "\n```\n\n"
concatenated_content = concatenated_content.rstrip()
concatenated_content = self.concatenate_files(self.currentFiles)
self.textEdit.setText(concatenated_content)


if __name__ == '__main__':
app = QApplication([])
app.setOrganizationName('YourCompany')
app.setApplicationName('FileKitty')
app.setWindowIcon(QIcon(ICON_PATH))
ex = FilePicker()
ex.show()
Expand Down