-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathviews.py
385 lines (296 loc) · 12.8 KB
/
views.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import platform
from PySide.QtGui import (
QWidget, QPixmap, QLabel, QFont, QFrame, QPushButton, QPainter, QBrush,
QProgressBar, QColor, QLineEdit, QHBoxLayout, QVBoxLayout, QCheckBox, QFileDialog)
from PySide.QtCore import Signal, Slot, QDir, Qt
from localsettings import get_settings, SettingsKeys
import crypt
class View(QWidget):
"""Base `View` class. Defines behavior common in all views"""
# Selecting a good font family for each platform
osname = platform.system()
if osname == 'Windows':
fontFamily = 'Segoe UI'
elif osname == 'Linux':
fontFamily = ''
else:
fontFamily = ''
def __init__(self, parent=None):
"""
Init method. Initializes parent classes
:param parent: Reference to a `QWidget` object to be used as parent
"""
super(View, self).__init__(parent)
@staticmethod
def labelsFont():
"""Returns the `QFont` that `QLabels` should use"""
return View.font(True)
@staticmethod
def editsFont():
"""Returns the `QFont` that `QLineEdits` should use"""
return View.font(False)
@staticmethod
def font(bold):
"""
Returns a `QFont` object to be used in `View` derived classes.
:param bold: Indicates whether or not the font will be bold
"""
font = QFont(View.fontFamily, 9, 50, False)
font.setBold(bold)
return font
def setLogo(self):
"""Sets the company logo in the same place in all views"""
logoPixmap = QPixmap(':/resources/logo.png')
self.iconLabel = QLabel(self)
self.iconLabel.setPixmap(logoPixmap)
self.iconLabel.setGeometry(20, 20, logoPixmap.width(), logoPixmap.height())
self.linkLabel = QLabel(self)
self.linkLabel.setText(
"""<font size="1"><a href="http://www.iqstorage.com/fromiqbox.php">
Developed at IQ Storage FTP Hosing Services</a></font>""")
self.linkLabel.setOpenExternalLinks(True)
# Defines a visual line separator to be placed under the `logoPixmap` `QLabel`
self.line = QFrame()
self.line.setFrameShape(QFrame.HLine);
self.line.setFrameShadow(QFrame.Sunken);
class LoginView(View):
"""`View` derived class. Defines the log in widget"""
login = Signal((str, str, str, bool,))
def __init__(self, parent=None):
"""
Init method. Initializes parent classes
:param parent: Reference to a `QWidget` object to be used as parent
"""
super(LoginView, self).__init__(parent)
self.createWidgets()
self.createLayouts()
self.setFixedSize(250, 340)
def createLayouts(self):
"""Put widgets into layouts, thus creating the widget"""
mainLayout = QHBoxLayout()
fieldsLayout = QVBoxLayout()
ftpInfoLayout = QHBoxLayout()
buttonLayout = QHBoxLayout()
mainLayout.addStretch(20)
fieldsLayout.addStretch(80)
fieldsLayout.addWidget(self.linkLabel)
fieldsLayout.addWidget(self.line)
fieldsLayout.addStretch(20)
ftpInfoLayout.addWidget(self.hostLabel, 50, Qt.AlignLeft)
ftpInfoLayout.addStretch(20)
ftpInfoLayout.addWidget(self.sslLabel, 20, Qt.AlignRight)
ftpInfoLayout.addWidget(self.sslCheck, 10, Qt.AlignRight)
fieldsLayout.addLayout(ftpInfoLayout)
fieldsLayout.addWidget(self.hostEdit)
fieldsLayout.addWidget(self.usernameLabel)
fieldsLayout.addWidget(self.usernameEdit)
fieldsLayout.addWidget(self.passwdLabel)
fieldsLayout.addWidget(self.passwdEdit)
fieldsLayout.addStretch(30)
buttonLayout.addStretch(50)
buttonLayout.addWidget(self.loginButton, 50, Qt.AlignRight)
fieldsLayout.addLayout(buttonLayout)
fieldsLayout.addStretch(20)
mainLayout.addLayout(fieldsLayout, 30)
mainLayout.addStretch(20)
self.setLayout(mainLayout)
def createWidgets(self):
"""Create children widgets needed by this view"""
fieldsWidth = 200
labelsFont = View.labelsFont()
editsFont = View.editsFont()
self.setLogo()
self.hostLabel = QLabel(self)
self.hostEdit = QLineEdit(self)
self.sslLabel = QLabel(self)
self.sslCheck = QCheckBox(self)
self.hostLabel.setText('FTP Location')
self.hostLabel.setFont(labelsFont)
self.hostEdit.setFixedWidth(fieldsWidth)
self.hostEdit.setFont(editsFont)
self.sslLabel.setText('SSL')
self.sslLabel.setFont(labelsFont)
self.usernameLabel = QLabel(self)
self.usernameEdit = QLineEdit(self)
self.usernameLabel.setText('Username')
self.usernameLabel.setFont(labelsFont)
self.usernameEdit.setFixedWidth(fieldsWidth)
self.usernameEdit.setFont(editsFont)
self.passwdLabel = QLabel(self)
self.passwdEdit = QLineEdit(self)
self.passwdLabel.setText('Password')
self.passwdLabel.setFont(labelsFont)
self.passwdEdit.setFixedWidth(fieldsWidth)
self.passwdEdit.setEchoMode(QLineEdit.Password)
self.passwdEdit.setFont(editsFont)
self.passwdEdit.returnPressed.connect(self.onLoginClicked)
self.loginButton = QPushButton(self)
self.loginButton.setText('Login')
self.loginButton.setFont(labelsFont)
self.loginButton.setFixedWidth(fieldsWidth / 2)
self.loginButton.clicked.connect(self.onLoginClicked)
# Sets previously stored values into the fields, if any
settings = get_settings()
self.hostEdit.setText(settings.value(SettingsKeys['host'], ''))
self.usernameEdit.setText(settings.value(SettingsKeys['username'], ''))
self.passwdEdit.setText(crypt.decrypt(settings.value(SettingsKeys['passwd'], '')))
# Unicode to boolean conversion
ssl = settings.value(SettingsKeys['ssl'], u'true')
ssl = True if ssl == u'true' else False
self.sslCheck.setChecked(ssl)
@Slot()
def onLoginClicked(self):
"""
Slot. Called on the user clicks on the `loginButton` button
"""
# Takes out the user input from the fields
host = self.hostEdit.text()
username = self.usernameEdit.text()
passwd = self.passwdEdit.text()
ssl = self.sslCheck.isChecked()
print 'Logging in: %s, %s, %s' % (host, username, '*' * len(passwd))
if len(host) > 0:
# If the fields are valid, store them using a `QSettings` object
# and triggers a log in request
settings = get_settings()
settings.setValue(SettingsKeys['host'], host)
settings.setValue(SettingsKeys['username'], username)
settings.setValue(SettingsKeys['passwd'], crypt.encrypt(passwd))
settings.setValue(SettingsKeys['ssl'], ssl)
self.setEnabled(False)
self.login.emit(host.strip(), username, passwd, ssl)
@Slot()
def onFailedLogIn(self):
"""
Slot. Called when the log in request fails
"""
# Enables the fields again for user input
self.setEnabled(True)
class SyncView(View):
"""`View` derived class. Defines the sync widget"""
sync = Signal((str,))
def __init__(self, parent=None):
"""
Init method. Initializes parent classes
:param parent: Reference to a `QWidget` object to be used as parent
"""
super(SyncView, self).__init__(parent)
self.createWidgets()
self.createLayouts()
self.setFixedSize(580, 340)
self.status.setMessage('Ready')
def createLayouts(self):
"""Put widgets into layouts, thus creating the widget"""
mainLayout = QHBoxLayout()
fieldsLayout = QVBoxLayout()
pathLayout = QHBoxLayout()
buttonLayout = QHBoxLayout()
mainLayout.addStretch(10)
fieldsLayout.addStretch(50)
fieldsLayout.addWidget(self.linkLabel)
fieldsLayout.addWidget(self.line)
fieldsLayout.addWidget(self.localdirLabel)
pathLayout.addWidget(self.localdirEdit)
pathLayout.addWidget(self.browseButton)
fieldsLayout.addLayout(pathLayout)
buttonLayout.addStretch(50)
buttonLayout.addWidget(self.syncButton, 50, Qt.AlignRight)
fieldsLayout.addLayout(buttonLayout)
fieldsLayout.addStretch(10)
fieldsLayout.addWidget(self.statusLabel)
fieldsLayout.addWidget(self.status)
fieldsLayout.addStretch(80)
mainLayout.addLayout(fieldsLayout, 60)
mainLayout.addStretch(10)
self.setLayout(mainLayout)
def createWidgets(self):
"""Create children widgets needed by this view"""
fieldsWidth = 450
labelsFont = View.labelsFont()
editsFont = View.editsFont()
self.setLogo()
self.localdirLabel = QLabel(self)
self.localdirEdit = QLineEdit(self)
self.localdirLabel.setText('Choose a folder')
self.localdirLabel.setFont(labelsFont)
self.localdirEdit.setFixedWidth(fieldsWidth)
self.localdirEdit.setReadOnly(False)
self.localdirEdit.setFont(editsFont)
self.browseButton = QPushButton(self)
self.browseButton.setText('Browse')
self.browseButton.setFont(labelsFont)
self.syncButton = QPushButton(self)
self.syncButton.setText('Sync')
self.syncButton.setFont(labelsFont)
self.browseButton.clicked.connect(self.onBrowseClicked)
self.syncButton.clicked.connect(self.onSyncClicked)
settings = get_settings()
self.localdirEdit.setText(settings.value(SettingsKeys['localdir'], ''))
self.statusLabel = QLabel(self)
self.statusLabel.setText('Status')
self.statusLabel.setFont(View.labelsFont())
self.status = StatusArea(self)
@Slot()
def onBrowseClicked(self):
"""Slot. Called when the user clicks on the `browseButton` button"""
# Presents the user with a native directory selector window
localdir = QFileDialog.getExistingDirectory()
localdir = QDir.fromNativeSeparators(localdir)
if len(localdir) > 0:
# If `localdir`'s value is good, store it using a `QSettings` object
# and triggers a sync request.
# Careful with '\' separators on Windows.
localdir = QDir.toNativeSeparators(localdir)
get_settings().setValue(SettingsKeys['localdir'], localdir)
self.localdirEdit.setText(localdir)
@Slot()
def onSyncClicked(self):
"""Slot. Called when the user clicks on the `syncButton` button"""
localdir = self.localdirEdit.text()
if len(localdir) > 0:
self.syncButton.setEnabled(False)
self.sync.emit(localdir)
class StatusArea(QWidget):
def __init__(self, parent=None):
super(StatusArea, self).__init__(parent)
self.setStyleSheet('StatusArea {background: yellow}')
self.msg = QLabel(self)
self.file = QLabel(self)
self.progress = QProgressBar(self)
self.msg.setFont(View.labelsFont())
self.file.setFont(View.editsFont())
self.progress.setMaximum(100)
self.progress.setMinimum(0)
self.progress.setTextVisible(False)
self.progress.setStyleSheet("""
QProgressBar {
border: 2px solid grey;
border-radius: 5px;
width: 60px;
height: 10px;
}
QProgressBar::chunk {
background-color: #05B8CC;
width: 5px;
}""")
layout = QHBoxLayout()
layout.addWidget(self.msg, 0, Qt.AlignLeft)
layout.addWidget(self.file, 0, Qt.AlignLeft)
layout.addWidget(self.progress, 0, Qt.AlignRight)
self.setLayout(layout)
@Slot(str)
@Slot(str, str, str)
def setMessage(self, msg, file='', progress=None):
if not progress:
self.progress.hide()
self.progress.setValue(0)
else:
self.progress.setValue(progress)
self.progress.show()
self.msg.setText(msg)
self.file.setText(file)
def paintEvent(self, event):
p = QPainter()
p.begin(self)
p.fillRect(self.rect(), QBrush(QColor(240, 200, 0)))
p.end()