-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnew_project.py
More file actions
executable file
·142 lines (125 loc) · 5.69 KB
/
Copy pathnew_project.py
File metadata and controls
executable file
·142 lines (125 loc) · 5.69 KB
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
from view.ui.new_project import Ui_NewProjectDialog
from model.project import Project
from view.main_window import MainWindow
from PyQt5.QtWidgets import QWidget, QFileDialog, QMessageBox
from PyQt5.QtGui import QRegExpValidator
from PyQt5.QtCore import QRegExp, pyqtSignal
from model.fbs import AppInfo
import os.path, unicodedata
'''
- プロジェクト名.sdt(プロジェクトファイル)
- dataset
- test
- OK
- NG
- train
- OK
- truncated
- inspection_results
- images
- tmp
- models
'''
class NewProjectDialog(QWidget):
# Signal for cancel button
back_to_startup = pyqtSignal()
new_project_canceled = pyqtSignal()
close_old_project = pyqtSignal()
def __init__(self):
QWidget.__init__(self)
self.ui = Ui_NewProjectDialog()
self.ui.setupUi(self)
self.ui.project_name_line.textEdited.connect(self.sync_project_name_edit)
self.ui.save_location_line.textEdited.connect(self.sync_save_location_edit)
self.ui.reference_button.clicked.connect(self.on_clicked_reference_button)
self.ui.create_button.clicked.connect(self.on_clicked_create_button)
self.ui.cancel_button.clicked.connect(self.on_clicked_cancel_button)
self.ui.save_location_line.returnPressed.connect(self.on_clicked_create_button)
self.ui.project_name_line.returnPressed.connect(self.on_clicked_create_button)
self.set_create_button_enabled()
self.ui.save_location_line.setText(os.path.expanduser('~')+'/')
self.main_window = None
self.come_from_main_window_flag = False
self.msgBox = None
# '/'の入力を制限(validation)
reg_ex = QRegExp("[^\\\/:\*\?\"<>|]+")
validator = QRegExpValidator(reg_ex, self.ui.project_name_line)
self.ui.project_name_line.setValidator(validator)
def on_clicked_reference_button(self):
save_location_path = QFileDialog.getExistingDirectory(self, '保存先フォルダを選択', os.path.expanduser('~'))
if save_location_path:
self.ui.save_location_line.setText(save_location_path+'/')
def on_clicked_create_button(self):
save_location_path = self.ui.save_location_line.text()
project_name = os.path.basename(self.ui.project_name_line.text())
if not self.is_valid_character(project_name, save_location_path):
self.msgBox = QMessageBox()
self.msgBox.setText('プロジェクト名またはディレクトリ名に、使用できない文字が含まれています。\n'
'全角文字や特殊記号が使われている場合は、名前を変えるか別のディレクトリに変更してください。')
self.msgBox.exec()
return
dir_paths = [
os.path.join(save_location_path, 'dataset/test/OK'),
os.path.join(save_location_path, 'dataset/test/NG'),
os.path.join(save_location_path, 'dataset/train/OK'),
os.path.join(save_location_path, 'dataset/truncated'),
os.path.join(save_location_path, 'inspection_results'),
os.path.join(save_location_path, 'inspection_results/images'),
os.path.join(save_location_path, 'tmp'),
os.path.join(save_location_path, 'models')
]
for dir_path in dir_paths:
os.makedirs(dir_path, exist_ok=True)
# プロジェクトファイル作成部分
project_path = save_location_path
Project.generate_project_file(project_path, project_name)
window_title = project_name + ' - ' + AppInfo().app_name() + ' Version ' + AppInfo().version()
self.main_window = MainWindow()
self.main_window.setWindowTitle(window_title)
self.main_window.show()
self.close_old_project.emit()
self.close()
self.main_window.back_to_new_project.connect(self.open_new_project_widget)
self.main_window.back_to_startup.connect(self.on_back_to_startup_signal)
def on_clicked_cancel_button(self):
if self.come_from_main_window_flag:
self.close()
return
self.new_project_canceled.emit()
self.close()
def sync_project_name_edit(self, text):
project_name = text
save_location_path = self.ui.save_location_line.text()
save_dir_path = os.path.dirname(save_location_path)
path = os.path.join(save_dir_path, project_name)
self.ui.save_location_line.setText(path)
self.set_create_button_enabled()
def sync_save_location_edit(self, text):
save_location_path = text
project_name = os.path.basename(save_location_path)
self.ui.project_name_line.setText(project_name)
self.set_create_button_enabled()
def set_create_button_enabled(self):
if self.ui.project_name_line.text():
self.ui.create_button.setEnabled(True)
elif not self.ui.project_name_line.text():
self.ui.create_button.setEnabled(False)
def open_new_project_widget(self):
self.come_from_main_window_flag = True
self.show()
def on_back_to_startup_signal(self):
self.back_to_startup.emit()
self.main_window = MainWindow()
def is_valid_character(self, project_name, save_location_path):
forbidden_characters = ['\\', ':', '*', '?', '"', '<', '>', '|']
for letter in project_name:
if unicodedata.east_asian_width(letter) != 'Na' or letter in forbidden_characters:
return False
else:
continue
for letter2 in save_location_path:
if unicodedata.east_asian_width(letter2) != 'Na':
return False
else:
continue
return True