This repository has been archived by the owner on Dec 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit_wallpaper_dialog.py
134 lines (123 loc) · 4.98 KB
/
edit_wallpaper_dialog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import sys
from pathlib import Path
from PySide2.QtWidgets import (
QDialog,
QVBoxLayout,
QPushButton,
QFileDialog,
QLabel,
QLineEdit,
QHBoxLayout,
QMessageBox,
)
from PySide2.QtGui import QPixmap, QIcon
from PySide2.QtCore import Qt
def update_thumbnail(self):
if self.path != "":
pixmap = QPixmap(self.path)
if pixmap.isNull():
criticalMessageBox = QMessageBox()
criticalMessageBox.critical(
None,
"Invalid Image",
f"Image at path {self.path} could not be found.",
criticalMessageBox.Close,
)
else:
aspectRatio = pixmap.size().width() / pixmap.size().height()
if aspectRatio > 1.78:
scaledPixmap = pixmap.scaledToWidth(self.thumbnail.size().width())
else:
scaledPixmap = pixmap.scaledToHeight(self.thumbnail.size().height())
self.thumbnail.setPixmap(scaledPixmap)
class EditWallpaperDialog(QDialog):
def __init__(self, parent):
super(EditWallpaperDialog, self).__init__()
self.setWindowTitle("Edit Wallpaper")
self.isSaved = False
self.parent = parent
self.path = self.parent.path
self.layout = QVBoxLayout()
self.setLayout(self.layout)
self.thumbnail_setup()
self.application_edit_setup()
if not self.parent.isDefaultSetting:
self.process_edit_setup()
self.select_image_button_setup()
self.save_and_cancel_buttons_setup()
self.adjustSize()
self.setFixedSize(self.size())
def application_edit_setup(self):
applicationValue = self.parent.application
self.applicationLayout = QHBoxLayout()
self.applicationLayout.setAlignment(Qt.AlignCenter)
self.application = QLabel(
applicationValue if self.parent.isDefaultSetting else "Application:"
)
self.applicationLayout.addWidget(self.application)
if not self.parent.isDefaultSetting:
self.applicationEdit = QLineEdit()
self.applicationEdit.setText(applicationValue)
self.applicationEdit.setFixedWidth(350)
self.applicationLayout.addWidget(self.applicationEdit)
self.layout.addLayout(self.applicationLayout)
def process_edit_setup(self):
self.processLayout = QHBoxLayout()
self.processLabel = QLabel("Process Name:")
self.processLayout.addWidget(self.processLabel)
self.processEdit = QLineEdit()
self.processEdit.setText(self.parent.process)
self.processEdit.setFixedWidth(350)
self.processLayout.addWidget(self.processEdit)
self.layout.addLayout(self.processLayout)
def thumbnail_setup(self):
self.thumbnail = QLabel()
self.thumbnail.setFixedSize(500, 282)
self.thumbnail.setStyleSheet(
"background-color: black; border: 1px solid black;"
)
self.thumbnail.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
update_thumbnail(self)
self.layout.addWidget(self.thumbnail)
def select_image_button_setup(self):
self.selectButton = QPushButton("Select Image...")
self.selectButton.setIcon(QIcon(f"{sys.argv[0]}/assets/image.svg"))
self.selectButton.clicked.connect(self.handle_select)
self.layout.addWidget(self.selectButton)
def save_and_cancel_buttons_setup(self):
buttonsLayout = QHBoxLayout()
self.saveButton = QPushButton("Save Changes")
self.saveButton.setIcon(QIcon(f"{sys.argv[0]}/assets/save.svg"))
self.saveButton.clicked.connect(self.handle_save)
buttonsLayout.addWidget(self.saveButton)
self.cancelButton = QPushButton("Cancel")
self.cancelButton.setIcon(QIcon(f"{sys.argv[0]}/assets/cancel.svg"))
self.cancelButton.clicked.connect(self.close)
buttonsLayout.addWidget(self.cancelButton)
self.layout.addLayout(buttonsLayout)
def handle_select(self):
fileDialog = QFileDialog()
fileDialog.setFileMode(QFileDialog.ExistingFile)
fileDialog.setNameFilter("Images (*.png *.xpm *.jpg *.jpeg)")
fileDialog.setDirectory(f"{str(Path.home())}/Pictures")
fileDialog.setWindowTitle("Select an Image")
if fileDialog.exec_():
self.path = fileDialog.selectedFiles()[0]
update_thumbnail(self)
def handle_save(self):
handleSaveArgs = [self.path]
if not self.parent.isDefaultSetting:
handleSaveArgs.extend(
[self.applicationEdit.text(), self.processEdit.text()]
)
someArgIsInvalid = any(arg == "" for arg in handleSaveArgs)
if someArgIsInvalid:
errorDialog = QMessageBox.critical(
self,
"Empty Fields",
"Cannot save with any field empty.",
QMessageBox.StandardButton.Close,
)
else:
self.parent.handle_save(*handleSaveArgs)
self.close()