Skip to content

Commit

Permalink
stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
yuckdevchan committed Apr 12, 2024
1 parent 5d942c1 commit 61ce7d3
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 86 deletions.
Binary file modified __pycache__/everything_api.cpython-312.pyc
Binary file not shown.
Binary file modified __pycache__/main.cpython-312.pyc
Binary file not shown.
Binary file modified __pycache__/utils.cpython-312.pyc
Binary file not shown.
6 changes: 5 additions & 1 deletion everything_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
# Load the Everything DLL
everything_dll = ctypes.WinDLL(os.path.abspath(Path('Everything64.dll')))

def search(query: str):
def search(query: str) -> list:
directory = Path.home()
# Prepend the directory and a wildcard to the search query
query = f"{directory}\\*{query}*"

# Set the search query
everything_dll.Everything_SetSearchW(query)

Expand Down
10 changes: 10 additions & 0 deletions image_viewer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import sys
from PySide6.QtWidgets import QApplication
from main import ImageViewer

if __name__ == '__main__':
# qt boilerplate
app = QApplication(sys.argv)
window = ImageViewer()
window.show()
sys.exit(app.exec())
37 changes: 0 additions & 37 deletions installer.spec

This file was deleted.

155 changes: 110 additions & 45 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from PySide6.QtCore import Qt, Slot, QTimer, QUrl, QFileInfo, QThreadPool, QRunnable, QObject, Signal
from PySide6.QtGui import QIcon, QPainterPath, QColor, QFont, QTextCursor, QAction
from PySide6.QtMultimedia import QMediaPlayer
from PySide6.QtPrintSupport import QPrinter, QPrintDialog
from PySide6 import QtCore, QtWidgets, QtGui
from markdown import markdown
from pathlib import Path
Expand All @@ -12,13 +13,16 @@
import keyboard, wmi
from qtacrylic import WindowEffect

from utils import list_programs, narrow_down, determine_program, load_qt_styles, load_themes, is_calculation, get_windows_theme, program_name_to_shortcut, conversion, is_youtube_url, download_youtube
from utils import list_programs, narrow_down, determine_program, load_qt_styles, load_themes, is_calculation, get_windows_theme, program_name_to_shortcut, conversion, is_youtube_url, download_youtube, is_image
from scripts.config_tools import get_config, get_core_config, get_program_directory, current_user

class ImageViewer(QtWidgets.QWidget):
def __init__(self):
super().__init__()

self.setWindowTitle("Image Preview")
self.resize(800, 600)

self.layout = QtWidgets.QHBoxLayout(self)

self.scaling_methods = ["Nearest Neighbor", "Bilinear", "Bicubic", "Lanczos"]
Expand All @@ -28,18 +32,34 @@ def __init__(self):
self.file_menu = self.menubar.addMenu("&File")
self.open_action = self.file_menu.addAction("&Open")
self.open_action.triggered.connect(self.select_image)
self.open_action.setShortcut("Ctrl+O")
self.print_action = self.file_menu.addAction("&Print")
self.print_action.setIcon(QtGui.QIcon(str(get_program_directory() / "images" / f"print-{self.theme}.svg")))
self.print_action.triggered.connect(self.print_image)
self.print_action.setShortcut("Ctrl+P")
self.exit_action = self.file_menu.addAction("&Exit")
self.exit_action.setIcon(QtGui.QIcon(str(get_program_directory() / "images" / f"exit-{self.theme}.svg")))
self.edit_menu = self.menubar.addMenu("&Edit")
self.scaling_menu = self.edit_menu.addMenu("&Scaling Method")
self.exit_action.setShortcut("Ctrl+Q")
self.exit_action.triggered.connect(self.close)
self.view_menu = self.menubar.addMenu("&View")
self.scaling_menu = self.view_menu.addMenu("&Scaling Method")
self.zoom_in = self.view_menu.addAction("Zoom In")
self.zoom_in.triggered.connect(lambda: self.image_view.scale(1.25, 1.25))
self.zoom_in.setShortcut("Ctrl+=")
self.zoom_out = self.view_menu.addAction("Zoom Out")
self.zoom_out.setShortcut("Ctrl+-")
self.zoom_out.triggered.connect(lambda: self.image_view.scale(0.8, 0.8))
self.zoom_fit = self.view_menu.addAction("Zoom to Fit")
self.zoom_fit.setShortcut("Ctrl+0")
self.scaling_actions = [QAction(method, self) for method in self.scaling_methods]
for action in self.scaling_actions:
self.scaling_menu.addAction(action)
self.exit_action.triggered.connect(self.close)
self.help_menu = self.menubar.addMenu("&Help")
self.github_action = self.help_menu.addAction("&GitHub")
self.github_action.triggered.connect(lambda: QtGui.QDesktopServices.openUrl(QUrl("https://github.com/yuckdevchan/kaboom")))
self.about_action = self.help_menu.addAction("&About")
self.layout.setMenuBar(self.menubar)

self.open_action.setShortcut("Ctrl+O")
self.exit_action.setShortcut("Ctrl+Q")

self.options_layout = QtWidgets.QVBoxLayout()
self.layout.addLayout(self.options_layout)
Expand All @@ -65,14 +85,29 @@ def __init__(self):
self.pathbar_layout.addWidget(self.pathbar_text)

self.pathbar = QtWidgets.QLineEdit()
self.pathbar.setText("C:\\Users\\ethan\\Downloads\\gigi-AEgvTRJXm-U-unsplash.jpg")
self.pathbar.setText("C:\\Users\\ethan\\Downloads\\arab_steve.png")
self.pathbar.returnPressed.connect(self.update_image)
self.pathbar_layout.addWidget(self.pathbar)

self.path_select_button = QtWidgets.QPushButton("Select Image")
self.path_select_button.clicked.connect(self.select_image)
self.pathbar_layout.addWidget(self.path_select_button)

self.view_buttons_layout = QtWidgets.QHBoxLayout()
self.options_layout.addLayout(self.view_buttons_layout)

self.zoom_in_button = QtWidgets.QPushButton("+")
self.zoom_in_button.clicked.connect(lambda: self.image_view.scale(1.25, 1.25))
self.view_buttons_layout.addWidget(self.zoom_in_button)

self.zoom_out_button = QtWidgets.QPushButton("-")
self.zoom_out_button.clicked.connect(lambda: self.image_view.scale(0.8, 0.8))
self.view_buttons_layout.addWidget(self.zoom_out_button)

self.zoom_fit_button = QtWidgets.QPushButton("Fit")
self.zoom_fit_button.clicked.connect(lambda: self.image_view.resetTransform())
self.view_buttons_layout.addWidget(self.zoom_fit_button)

self.image_scene = QtWidgets.QGraphicsScene()
self.image_view = QtWidgets.QGraphicsView(self.image_scene)
self.image_view.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
Expand All @@ -81,12 +116,14 @@ def __init__(self):
self.image_view.setInteractive(True)
self.layout.addWidget(self.image_view)

# set image view to take up all available space
self.layout.setStretch(1, 1)

self.options_layout.addStretch()
self.layout.addStretch()

QtWidgets.QApplication.instance().installEventFilter(self)
self.update_image()
keyboard.on_release_key('space', self.close_on_space)
self.toggle_window()
self.toggle_window()

Expand All @@ -96,36 +133,46 @@ def toggle_window(self):
else:
self.show()

def print_image(self):
printer = QPrinter()
dialog = QPrintDialog(printer, self)
if dialog.exec_() == QtWidgets.QDialog.Accepted:
painter = QtGui.QPainter(printer)
painter.setRenderHint(QtGui.QPainter.Antialiasing)
painter.setRenderHint(QtGui.QPainter.SmoothPixmapTransform)
self.image_scene.render(painter)
painter.end()

def select_image(self):
path = QtWidgets.QFileDialog.getOpenFileName(self, "Select Image", str(Path.home() / "Pictures"), "Images (*.png *.jpg *.jpeg *.bmp *.gif *.tiff)")[0]
if path:
self.pathbar.setText(path.replace("/", "\\"))
self.update_image()

def update_image(self):
scaling_method = self.scaling_combobox.currentText()
path = self.pathbar.text()
if os.path.exists(path):
img = Image.open(path)
if scaling_method == "Nearest Neighbor":
resample_method = Image.NEAREST
elif scaling_method == "Bilinear":
resample_method = Image.BILINEAR
elif scaling_method == "Bicubic":
resample_method = Image.BICUBIC
elif scaling_method == "Lanczos":
resample_method = Image.LANCZOS

img = img.resize((self.width(), self.height()), resample=resample_method)

qimg = ImageQt.ImageQt(img)
pixmap = QtGui.QPixmap.fromImage(qimg)

# Add the pixmap to the scene
self.image_scene.clear()
self.image_scene.addPixmap(pixmap)
else:
self.text.setText("File not found")
scaling_method = self.scaling_combobox.currentText()
path = self.pathbar.text()
if os.path.exists(path):
img = Image.open(path)
if scaling_method == "Nearest Neighbor":
resample_method = Image.NEAREST
elif scaling_method == "Bilinear":
resample_method = Image.BILINEAR
elif scaling_method == "Bicubic":
resample_method = Image.BICUBIC
elif scaling_method == "Lanczos":
resample_method = Image.LANCZOS
img = img.resize((self.width(), self.height()), resample=resample_method)
qimg = ImageQt.ImageQt(img)
pixmap = QtGui.QPixmap.fromImage(qimg)
# Add the pixmap to the scene
self.image_scene.clear()
self.image_scene.addPixmap(pixmap)
else:
self.text.setText("File not found")

def wheelEvent(self, event):
# Zoom in or out when the scroll wheel is used
Expand All @@ -148,6 +195,9 @@ def wheelEvent(self, event):
# Move scene to old position
delta = new_pos - old_pos
self.image_view.translate(delta.x(), delta.y())

# Ignore the event to prevent scrolling
event.ignore()

def mousePressEvent(self, event):
self.image_view.setDragMode(QtWidgets.QGraphicsView.ScrollHandDrag)
Expand All @@ -157,11 +207,6 @@ def mouseReleaseEvent(self, event):
self.image_view.setDragMode(QtWidgets.QGraphicsView.NoDrag)
super().mouseReleaseEvent(event)

def close_on_space(self, event):
print("Space Released")
self.hide()
self.parent().show()

class SettingsPopup2(QtWidgets.QDialog):
def __init__(self, parent=None):
super().__init__(parent)
Expand Down Expand Up @@ -1218,7 +1263,8 @@ def __init__(self):
keyboard.add_hotkey("up", self.up_pressed)
keyboard.add_hotkey("ctrl + x", self.on_yank_key_pressed)
keyboard.add_hotkey("ctrl + y", self.on_yank_key_pressed)
# keyboard.add_hotkey("space", self.open_image_viewer, suppress=True, timeout=10)
keyboard.add_hotkey("tab", self.open_image_viewer, suppress=True, timeout=10)
keyboard.add_hotkey("tab", self.close_image_viewer, suppress=True, timeout=10, trigger_on_release=True)

self.layout = QtWidgets.QVBoxLayout(self)
self.textbox_layout = QtWidgets.QHBoxLayout()
Expand Down Expand Up @@ -1376,7 +1422,7 @@ def __init__(self):
ktype = "Unknown"
if ktype == "None":
ktype = ""
title = program_list[i].replace(".lnk", "").replace(".desktop", "").replace(".app", "").split(".")[0]
title = program_list[i].replace(".lnk", "").replace(".desktop", "").replace(".app", "").replace(f".kaboom.{ktype}", "")
self.button = QtWidgets.QPushButton(title + "\n- " + ktype, self)
self.button.setToolTip("Click to launch")
global button_qss
Expand Down Expand Up @@ -1669,7 +1715,7 @@ def on_text_changed(self, text):
ktype = "Unknown"
if ktype == "None":
ktype = ""
title = program_list[i].replace(".lnk", "").replace(".desktop", "").replace(".app", "").split(".")[0]
title = program_list[i].replace(".lnk", "").replace(".desktop", "").replace(".app", "").replace(f".kaboom.{ktype}", "")
self.button = QtWidgets.QPushButton(title + "\n- " + ktype, self)
self.button.setToolTip("Click to launch")
global button_qss
Expand Down Expand Up @@ -1824,9 +1870,21 @@ def on_enter_pressed(self):
self.button = QtWidgets.QPushButton(downloading_status, self)
self.button.setStyleSheet(button_qss)
self.buttons_layout.addWidget(self.button)
elif narrow_down(text)[0].split(".")[-2] == "kaboom" and (narrow_down(text)[0].split(".")[-1] == "File" or narrow_down(text)[0].split(".")[-1] == "Folder" or narrow_down(text)[0].split(".")[-1] == "Image File"):
if narrow_down(text)[0].split(".")[-1] == "Image File":
self.open_image_viewer()
else:
ktype = narrow_down(text)[0].split(".")[-1]
if platform.system() == "Windows":
os.startfile(narrow_down(text)[0].replace(f".kaboom.{ktype}", ""))
elif platform.system() == "Linux":
os.system(f"xdg-open {narrow_down(text)[0].split('.')[0]}")
elif platform.system() == "Darwin":
os.system(f"open {narrow_down(text[0]).split('.')[0]}")
self.toggle_window()
else:
program = narrow_down(self.search_bar.text())[0]
if self.search_bar.text() == "exit":
program = narrow_down(text)[0]
if text == "exit":
os._exit(0)
elif self.buttons_layout.count() == 1 and self.buttons_layout.itemAt(0).widget().text() == core_config["Settings"]["no_results_text"]:
self.search_bar.clear()
Expand Down Expand Up @@ -1889,9 +1947,15 @@ def on_yank_key_pressed(self):
self.search_bar.setFocus()

def open_image_viewer(self):
self.image_viewer.setWindowFlag(Qt.WindowStaysOnTopHint)
self.hide()
self.image_viewer.show()
if self.isVisible:
self.hide()
if not self.image_viewer.isVisible():
self.image_viewer.show()

def close_image_viewer(self):
if self.image_viewer.isVisible():
self.image_viewer.hide()
self.show()

if platform.system() == "Linux" or platform.system() == "Darwin":
def read_value_from_file(filename):
Expand Down Expand Up @@ -1945,7 +2009,8 @@ def update_config():
file.truncate()
os.execl(sys.executable, sys.executable, *sys.argv)


global close_image_viewer_on_space_released
close_image_viewer_on_space_released = True
global config_path
config_path = get_config()
print("Config Directory: " + str(config_path))
Expand Down
16 changes: 13 additions & 3 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from scripts.os_tools import current_user
from everything_api import search
from setproctitle import setproctitle
from PIL import Image

setproctitle("kaboom")

Expand Down Expand Up @@ -356,14 +357,23 @@ def narrow_down(search_text):
program_ = program
if search_text_ in program_:
narrowed_list.append(program)
if len(narrowed_list) == 0:
narrowed_list = [core_config["Settings"]["no_results_text"]]
max_results = config["Settings"]["max_results"]
narrowed_list = narrowed_list[:max_results]
if config["Settings"]["search_filesystem"] and len(search_text) > 3:
narrowed_list += search(search_text)
files_list = search(search_text)
files_list = [file + ".kaboom.Image File" if is_image(file) else file + "kaboom.File" for file in files_list]
narrowed_list += files_list
if len(narrowed_list) == 0:
narrowed_list = [core_config["Settings"]["no_results_text"]]
return narrowed_list

def is_image(path):
try:
Image.open(path)
return True
except Exception:
return False

def send_notification(title, message):
notification.notify(
title=title,
Expand Down

0 comments on commit 61ce7d3

Please sign in to comment.