-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_gui.py
executable file
·207 lines (172 loc) · 7.24 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# -*- coding: utf-8 -*-
"""
Main program for the graphical user interface
@author: L. Calvo-Bartolome
"""
import pathlib
import sys
import os
import argparse
from PyQt5.QtWidgets import QDialog, QApplication, QFileDialog
from PyQt5.uic import loadUi
from pathlib import Path
from src.graphical_user_interface.main_window import *
from src.graphical_user_interface.messages import Messages
from src.task_manager import TaskManagerGUI
from src.graphical_user_interface.output_wrapper import OutputWrapper
class PreConfig(QDialog):
"""
Main class for the Domain Classification GUI. It starts the GUI application
by first creating a starting window, which after a correct selection of the
input parameters (i.e. project and source folder), redirects the user to
the main window of the application.
It can be invoked in the following ways:
1) python main_gui.py --p path_to_project_folder
--source path_to_source_folder --zeroshot path_to_zeroshot_folder
---> The project, source and zeroshot folders are automatically
updated in the starting window, and the START button can be
directly clicked, without the necessity of further configurations.
2) python main_gui.py
---> The needed folders to be manually selected by
clicking on their respective buttons.
"""
def __init__(self, widget, args):
"""
Initializes the main class for the Domain Classification GUI.
Parameters
----------
widget : QtWidgets.QStackedWidget
Window to which the application's main window is attached to
args : argparse.Namespace
List of positional arguments leftover after parsing options.
"""
super(PreConfig, self).__init__()
# Load UI
loadUi("UIs/menuConfig.ui", self)
# Center window
self.center()
# Attributes
self.widget = widget
self.args = args
# Redirect stdout and stderr
self.stdout = OutputWrapper(self, True)
self.stderr = OutputWrapper(self, False)
# Get home in any operating system
self.home = str(Path.home())
# Variables for saving the project and source data folders
self.sourceFolder = (
self.args.source if self.args.source is not None else "")
self.projectFolder = self.args.p if self.args.p is not None else ""
self.zeroshotFolder = (
self.args.zeroshot if self.args.p is not None else "")
# Update image
pixmap = QPixmap('UIs/Images/dc_logo.png')
self.label.setPixmap(pixmap)
# Update project and source folder in the GUI if provided through the
# command line
self.showProjectFolder.setText(
self.args.p) if self.args.p is not None else print(
"p not provided")
self.showSourceDataFolder.setText(
self.args.source) if self.args.source is not None else print(
"source not provided")
self.showZeroShot.setText(
self.args.zeroshot) if self.args.zeroshot is not None else print(
"zero-shot folder not provided")
# CONNECTION WITH HANDLER FUNCTIONS
#######################################################################
self.selectProjectFolder.clicked.connect(self.get_project_folder)
self.selectSourceDataFolder.clicked.connect(
self.get_source_data_folder)
self.selectZeroShot.clicked.connect(self.get_zeroshot_folder)
self.start.clicked.connect(self.start_application)
def center(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def get_project_folder(self):
self.projectFolder = \
QFileDialog.getExistingDirectory(
self, 'Create or select an an existing project', self.home)
self.showProjectFolder.setText(self.projectFolder)
def get_source_data_folder(self):
self.sourceFolder = \
QFileDialog.getExistingDirectory(
self, 'Select the source data folder', self.home)
self.showSourceDataFolder.setText(self.sourceFolder)
def get_zeroshot_folder(self):
self.zeroshotFolder = \
QFileDialog.getExistingDirectory(
self, 'Select the folder with the zero shot model', self.home)
# self.showSourceDataFolder.setText(self.zeroshotFolder)
self.showZeroShot.setText(self.zeroshotFolder)
def start_application(self):
# We show a warning message if one or both folders have not selected
if self.projectFolder == "" or self.sourceFolder == "" or self.zeroshotFolder == "":
QtWidgets.QMessageBox.warning(
self, Messages.DC_MESSAGE,
Messages.INCORRECT_INPUT_PARAM_SELECTION)
return
# Create the TaskManager object
tm = TaskManagerGUI(
self.projectFolder, path2source=self.sourceFolder,
path2zeroshot=self.zeroshotFolder)
# Load or create new project depending on whether the selected project folder is a new project
if pathlib.Path(self.projectFolder).is_dir():
# In case the project folder was created with the GUI
if len(os.listdir(self.projectFolder)) == 0:
print("A new project folder was selected. Proceeding with "
"its configuration...")
tm.create()
tm.setup()
else:
print("An existing project folder was selected. Proceeding with "
"its loading...")
tm.load()
else:
print("A new project folder was selected. Proceeding with "
"its configuration...")
tm.create()
tm.setup()
# Change to the main menu
main_window = MainWindow(
self.projectFolder, self.sourceFolder, tm, self.widget,
self.stdout, self.stderr)
self.widget.addWidget(main_window)
self.widget.setCurrentIndex(self.widget.currentIndex() + 1)
self.widget.showMaximized()
return
########################################################################
# Main
########################################################################
def main():
# Read input arguments
parser = argparse.ArgumentParser()
parser.add_argument(
'--p', type=str,
default="../project_folder",
help="path to a new or an existing project")
parser.add_argument(
'--source', type=str,
default="../datasets",
help="path to the source data folder")
parser.add_argument(
'--zeroshot', type=str,
default='../zero_shot_model/Sciro-Shot',
help="path to the zero-shot model folder")
args = parser.parse_args()
# Create application
app = QApplication(sys.argv)
app.setWindowIcon(QIcon('UIs/Images/dc_logo.png'))
# Configure widgets
widget = QtWidgets.QStackedWidget()
widget.setWindowTitle(Messages.WINDOW_TITLE)
# Create main menu window
config_window = PreConfig(widget, args)
widget.addWidget(config_window)
widget.showMaximized()
widget.show()
app.exec_()
if __name__ == "__main__":
main()