-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_gui.py
173 lines (126 loc) · 5.23 KB
/
main_gui.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import json
import os
import sys
import time
from PySide6 import QtCore
from PySide6.QtCore import Qt
from PySide6.QtGui import QPalette
from PySide6.QtWidgets import QMainWindow, QApplication, QAbstractItemView
from GUI import Styling
from GUI.config_creator import ConfigCreator
from GUI.ui_voting import Ui_VotingWindow
from LDJAM_API import LDJAM_API
from util import Config
class VotingWindow(QMainWindow):
theme_library = None
voted_themes = None
config_window = None
def __init__(self):
super().__init__()
self.ui = Ui_VotingWindow()
self.ui.setupUi(self)
Styling.set_style(self)
self.setWindowTitle("pyJAMa by RedCocoa")
self.event_id = LDJAM_API.get_current_event_id()
self.ui.list_themes.setSelectionMode(QAbstractItemView.ExtendedSelection)
self.ui.list_themes.itemSelectionChanged.connect(self.theme_selection_changed)
self.ui.text_theme_search.textChanged.connect(self.search_term_changed)
self.ui.btn_vote_yes.clicked.connect(lambda: self.vote_selected_themes("yes"))
self.ui.btn_vote_no.clicked.connect(lambda: self.vote_selected_themes("no"))
self.ui.btn_flag.clicked.connect(lambda: self.vote_selected_themes("flag"))
self.ui.btn_vote_yes.setEnabled(False)
self.ui.btn_vote_no.setEnabled(False)
self.ui.btn_flag.setEnabled(False)
# dark mode options
dark_mode_enabled = Config.load_config_key(Config.ConfigKeys.DARK_MODE.value)
if dark_mode_enabled is None:
dark_mode_enabled = True
if dark_mode_enabled:
# style items
palette = QPalette()
palette.setColor(QPalette.Text, Qt.white)
self.ui.text_theme_search.setPalette(palette)
self.ui.list_themes.setPalette(palette)
def search_term_changed(self):
self.ui.list_themes.clear()
for theme in self.theme_library['ideas']:
if theme not in self.voted_themes and self.ui.text_theme_search.text().upper() in self.theme_library[
'ideas'].get(theme).upper():
current_theme = self.theme_library['ideas'].get(theme)
self.ui.list_themes.addItem(current_theme)
self.ui.list_themes.sortItems()
if len(self.ui.text_theme_search.text()) > 0:
self.ui.label_status.setText(
f"{len(self.theme_library['ideas'])} themes loaded, {len(self.theme_library['ideas']) - len(self.voted_themes)} unvoted ({self.ui.list_themes.count()} filtered)")
else:
self.ui.label_status.setText(
f"{len(self.theme_library['ideas'])} themes loaded, {len(self.theme_library['ideas']) - len(self.voted_themes)} unvoted")
def theme_selection_changed(self):
selected_themes = self.ui.list_themes.selectedItems()
self.ui.btn_flag.setEnabled(True)
self.ui.btn_vote_no.setEnabled(True)
self.ui.btn_vote_yes.setEnabled(True)
if len(selected_themes) == 0:
self.ui.btn_flag.setEnabled(False)
self.ui.btn_vote_no.setEnabled(False)
self.ui.btn_vote_yes.setEnabled(False)
if len(selected_themes) == 1:
self.ui.btn_flag.setEnabled(True)
else:
self.ui.btn_flag.setEnabled(False)
def get_theme_id(self, theme_name: str) -> int:
for theme_id_ in self.theme_library['ideas']:
if self.theme_library['ideas'].get(theme_id_) == theme_name:
return theme_id_
return -1
def vote_theme(self, theme: str, voting: str) -> None:
theme_id = self.get_theme_id(theme)
print(
f"Vote: {voting}, Theme ID: {theme_id} (To be clear, that is '{self.theme_library['ideas'].get(theme_id)}')")
voting_success = LDJAM_API.vote_theme(theme_id, voting)
if voting_success != 0:
print("Error voting")
def vote_selected_themes(self, voting: str):
self.ui.btn_vote_yes.setEnabled(False)
self.ui.btn_vote_no.setEnabled(False)
self.ui.btn_flag.setEnabled(False)
self.ui.list_themes.setEnabled(False)
self.ui.text_theme_search.setEnabled(False)
counter = 0
theme_count = len(self.ui.list_themes.selectedItems())
for theme in self.ui.list_themes.selectedItems():
self.vote_theme(theme.text(), voting)
counter += 1
theme.setText(f"{theme.text()} ({'Voted ' + voting.upper() if voting != 'flag' else 'FLAGGED'})")
self.ui.label_status.setText(
f"Voting... {round((counter / theme_count) * 100)}% done ({counter} / {theme_count})")
QtCore.QCoreApplication.processEvents()
time.sleep(1)
self.ui.text_theme_search.clear()
self.refresh_theme_list()
self.ui.list_themes.setEnabled(True)
self.ui.text_theme_search.setEnabled(True)
self.ui.text_theme_search.clear()
def refresh_theme_list(self):
request = LDJAM_API.get_event_themes(self.event_id)
self.theme_library = json.loads(request.text)
self.voted_themes = LDJAM_API.get_user_votes(self.event_id)
self.ui.label_status.setText(
f"{len(self.theme_library['ideas'])} themes loaded, {len(self.theme_library['ideas']) - len(self.voted_themes)} unvoted")
self.ui.label_status.setStyleSheet("font-size: 15pt;")
self.ui.list_themes.clear()
for theme in self.theme_library['ideas']:
if theme not in self.voted_themes:
current_theme = self.theme_library['ideas'].get(theme)
self.ui.list_themes.addItem(current_theme)
self.ui.list_themes.sortItems()
app = QApplication([])
voting_window = VotingWindow()
if Config.has_config():
voting_window.refresh_theme_list()
voting_window.show()
else:
cookie_window = ConfigCreator(os.path.dirname(os.path.realpath(__file__)))
cookie_window.set_main_window(voting_window)
cookie_window.show()
sys.exit(app.exec_())