-
Notifications
You must be signed in to change notification settings - Fork 42
/
widgets.py
3314 lines (2776 loc) Β· 107 KB
/
widgets.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Contains the main widgets used by the client to display things in the UI.
Copyright (C) 2018 The Freedom of the Press Foundation.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import logging
import arrow
import html
import sys
from gettext import gettext as _
from typing import Dict, List, Union # noqa: F401
from uuid import uuid4
from PyQt5.QtCore import Qt, pyqtSlot, pyqtSignal, QEvent, QTimer, QSize, pyqtBoundSignal, \
QObject, QPoint
from PyQt5.QtGui import QIcon, QPalette, QBrush, QColor, QFont, QLinearGradient, QKeySequence, \
QCursor
from PyQt5.QtWidgets import QApplication, QListWidget, QLabel, QWidget, QListWidgetItem, \
QHBoxLayout, QVBoxLayout, QLineEdit, QScrollArea, QDialog, QAction, QMenu, QMessageBox, \
QToolButton, QSizePolicy, QPlainTextEdit, QStatusBar, QGraphicsDropShadowEffect, QPushButton, \
QDialogButtonBox
from securedrop_client.db import DraftReply, Source, Message, File, Reply, User
from securedrop_client.storage import source_exists
from securedrop_client.export import ExportStatus, ExportError
from securedrop_client.gui import SecureQLabel, SvgLabel, SvgPushButton, SvgToggleButton
from securedrop_client.logic import Controller
from securedrop_client.resources import load_icon, load_image, load_movie
from securedrop_client.utils import humanize_filesize
logger = logging.getLogger(__name__)
class TopPane(QWidget):
"""
Top pane of the app window.
"""
def __init__(self):
super().__init__()
# Fill the background with a gradient
self.online_palette = QPalette()
gradient = QLinearGradient(0, 0, 1553, 0)
gradient.setColorAt(0, QColor('#1573d8'))
gradient.setColorAt(0.22, QColor('#0060d3'))
gradient.setColorAt(1, QColor('#002c53'))
self.online_palette.setBrush(QPalette.Background, QBrush(gradient))
self.offline_palette = QPalette()
gradient = QLinearGradient(0, 0, 1553, 0)
gradient.setColorAt(0, QColor('#1e1e1e'))
gradient.setColorAt(0.22, QColor('#122d61'))
gradient.setColorAt(1, QColor('#0d4a81'))
self.offline_palette.setBrush(QPalette.Background, QBrush(gradient))
self.setPalette(self.offline_palette)
self.setAutoFillBackground(True)
# Set layout
layout = QHBoxLayout(self)
self.setLayout(layout)
# Remove margins and spacing
layout.setContentsMargins(10, 0, 0, 0)
layout.setSpacing(0)
# Sync icon
self.sync_icon = SyncIcon()
# Activity status bar
self.activity_status_bar = ActivityStatusBar()
# Error status bar
self.error_status_bar = ErrorStatusBar()
# Create space the size of the status bar to keep the error status bar centered
spacer = QWidget()
# Create space ths size of the sync icon to keep the error status bar centered
spacer2 = QWidget()
spacer2.setFixedWidth(42)
# Set height of top pane to 42 pixels
self.setFixedHeight(42)
self.sync_icon.setFixedHeight(42)
self.activity_status_bar.setFixedHeight(42)
self.error_status_bar.setFixedHeight(42)
spacer.setFixedHeight(42)
spacer2.setFixedHeight(42)
# Add widgets to layout
layout.addWidget(self.sync_icon, 1)
layout.addWidget(self.activity_status_bar, 1)
layout.addWidget(self.error_status_bar, 1)
layout.addWidget(spacer, 1)
layout.addWidget(spacer2, 1)
def setup(self, controller):
self.sync_icon.setup(controller)
self.error_status_bar.setup(controller)
def set_logged_in(self):
self.sync_icon.enable()
self.setPalette(self.online_palette)
def set_logged_out(self):
self.sync_icon.disable()
self.setPalette(self.offline_palette)
def update_activity_status(self, message: str, duration: int):
self.activity_status_bar.update_message(message, duration)
def update_error_status(self, message: str, duration: int, retry: bool):
self.error_status_bar.update_message(message, duration, retry)
def clear_error_status(self):
self.error_status_bar.clear_message()
class LeftPane(QWidget):
"""
Represents the left side pane that contains user authentication actions and information.
"""
def __init__(self):
super().__init__()
# Set layout
layout = QVBoxLayout(self)
self.setLayout(layout)
# Remove margins and spacing
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
layout.setAlignment(Qt.AlignBottom)
self.setFixedWidth(198)
self.setMinimumHeight(558)
# Set background image
self.logo = QWidget()
self.online_palette = QPalette()
# the sd logo on the background image becomes more faded in offline mode
self.online_palette.setBrush(QPalette.Background, QBrush(load_image('left_pane.svg')))
self.offline_palette = QPalette()
self.offline_palette.setBrush(QPalette.Background,
QBrush(load_image('left_pane_offline.svg')))
self.logo.setPalette(self.offline_palette)
self.logo.setAutoFillBackground(True)
self.logo.setMaximumHeight(884)
self.logo.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.logo.setEnabled(False)
# User profile
self.user_profile = UserProfile()
# Hide user profile widget until user logs in
self.user_profile.hide()
# Add widgets to layout
layout.addWidget(self.user_profile)
layout.addWidget(self.logo)
def setup(self, window, controller):
self.user_profile.setup(window, controller)
def set_logged_in_as(self, db_user: User):
"""
Update the UI to reflect that the user is logged in as "username".
"""
self.user_profile.set_user(db_user)
self.user_profile.show()
self.logo.setPalette(self.online_palette)
def set_logged_out(self):
"""
Update the UI to a logged out state.
"""
self.user_profile.hide()
self.logo.setPalette(self.offline_palette)
class SyncIcon(QLabel):
"""
An icon that shows sync state.
"""
CSS = '''
#sync_icon {
border: none;
color: #fff;
}
'''
def __init__(self):
# Add svg images to button
super().__init__()
self.setObjectName('sync_icon')
self.setStyleSheet(self.CSS)
self.setFixedSize(QSize(24, 20))
self.sync_animation = load_movie("sync_disabled.gif")
self.sync_animation.setScaledSize(QSize(24, 20))
self.setMovie(self.sync_animation)
self.sync_animation.start()
def setup(self, controller):
"""
Assign a controller object (containing the application logic).
"""
self.controller = controller
self.controller.sync_events.connect(self._on_sync)
def _on_sync(self, data):
if data == 'syncing':
self.sync_animation = load_movie("sync_active.gif")
self.sync_animation.setScaledSize(QSize(24, 20))
self.setMovie(self.sync_animation)
self.sync_animation.start()
def enable(self):
self.sync_animation = load_movie("sync.gif")
self.sync_animation.setScaledSize(QSize(24, 20))
self.setMovie(self.sync_animation)
self.sync_animation.start()
def disable(self):
self.sync_animation = load_movie("sync_disabled.gif")
self.sync_animation.setScaledSize(QSize(24, 20))
self.setMovie(self.sync_animation)
self.sync_animation.start()
class ActivityStatusBar(QStatusBar):
"""
A status bar for displaying messages about application activity to the user. Messages will be
displayed for a given duration or until the message updated with a new message.
"""
CSS = '''
#activity_status_bar {
font-family: 'Source Sans Pro';
font-weight: 600;
font-size: 12px;
color: #d3d8ea;
}
'''
def __init__(self):
super().__init__()
# Set css id
self.setObjectName('activity_status_bar')
# Set styles
self.setStyleSheet(self.CSS)
# Remove grip image at bottom right-hand corner
self.setSizeGripEnabled(False)
def update_message(self, message: str, duration: int):
"""
Display a status message to the user.
"""
self.showMessage(message, duration)
class ErrorStatusBar(QWidget):
"""
A pop-up status bar for displaying messages about application errors to the user. Messages will
be displayed for a given duration or until the message is cleared or updated with a new message.
"""
CSS = '''
#error_vertical_bar {
background-color: #ff3366;
}
#error_icon {
background-color: qlineargradient(
x1: 0,
y1: 0,
x2: 0,
y2: 1,
stop: 0 #fff,
stop: 0.2 #fff,
stop: 1 #fff
);
}
#error_status_bar {
background-color: qlineargradient(
x1: 0,
y1: 0,
x2: 0,
y2: 1,
stop: 0 #fff,
stop: 0.2 #fff,
stop: 1 #fff
);
font-family: 'Source Sans Pro';
font-weight: 400;
font-size: 14px;
color: #0c3e75;
}
QPushButton#retry_button {
border: none;
padding-right: 30px;
background-color: #fff;
color: #0065db;
font-family: 'Source Sans Pro';
font-weight: 600;
font-size: 12px;
}
'''
def __init__(self):
super().__init__()
# Set styles
self.setStyleSheet(self.CSS)
# Set layout
layout = QHBoxLayout(self)
self.setLayout(layout)
# Remove margins and spacing
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
# Error vertical bar
self.vertical_bar = QWidget()
self.vertical_bar.setObjectName('error_vertical_bar') # Set css id
self.vertical_bar.setFixedWidth(10)
# Error icon
self.label = SvgLabel('error_icon.svg', svg_size=QSize(20, 20))
self.label.setObjectName('error_icon') # Set css id
self.label.setFixedWidth(42)
# Error status bar
self.status_bar = QStatusBar()
self.status_bar.setObjectName('error_status_bar') # Set css id
self.status_bar.setSizeGripEnabled(False)
# Retry button
self.retry_button = QPushButton('RETRY')
self.retry_button.setObjectName('retry_button')
self.retry_button.setFixedHeight(42)
# Add widgets to layout
layout.addWidget(self.vertical_bar)
layout.addWidget(self.label)
layout.addWidget(self.status_bar)
layout.addWidget(self.retry_button)
# Hide until a message needs to be displayed
self.vertical_bar.hide()
self.label.hide()
self.status_bar.hide()
self.retry_button.hide()
# Only show errors for a set duration
self.status_timer = QTimer()
self.status_timer.timeout.connect(self._on_status_timeout)
def _hide(self):
self.vertical_bar.hide()
self.label.hide()
self.status_bar.hide()
self.retry_button.hide()
def _show(self):
self.vertical_bar.show()
self.label.show()
self.status_bar.show()
def _on_status_timeout(self):
self._hide()
def setup(self, controller):
self.controller = controller
self.retry_button.clicked.connect(self._on_retry_clicked)
def _on_retry_clicked(self) -> None:
self.clear_message()
self._hide()
self.controller.resume_queues()
def update_message(self, message: str, duration: int, retry: bool) -> None:
"""
Display a status message to the user for a given duration. If the duration is zero,
continuously show message.
"""
if retry:
self.retry_button.show()
self.status_bar.showMessage(message, duration)
if duration != 0:
self.status_timer.start(duration)
self._show()
def clear_message(self):
"""
Clear any message currently in the status bar.
"""
self.status_bar.clearMessage()
self._hide()
class UserProfile(QLabel):
"""
A widget that contains user profile information and options.
Displays user profile icon, name, and menu options if the user is logged in. Displays a login
button if the user is logged out.
"""
CSS = '''
QLabel#user_profile {
padding: 15px;
}
QLabel#user_icon {
border: none;
background-color: #9211ff;
padding-left: 3px;
padding-bottom: 4px;
font-family: 'Source Sans Pro';
font-weight: 600;
font-size: 15px;
color: #fff;
}
'''
def __init__(self):
super().__init__()
# Set css id
self.setObjectName('user_profile')
# Set styles
self.setStyleSheet(self.CSS)
# Set background
palette = QPalette()
palette.setBrush(QPalette.Background, QBrush(QColor('#0096DC')))
self.setPalette(palette)
self.setAutoFillBackground(True)
self.setMinimumHeight(20)
self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
# Set layout
layout = QHBoxLayout(self)
self.setLayout(layout)
# Remove margins
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
# Login button
self.login_button = LoginButton()
# User icon
self.user_icon = QLabel()
self.user_icon.setObjectName('user_icon') # Set css id
self.user_icon.setFixedSize(QSize(30, 30))
self.user_icon.setAlignment(Qt.AlignCenter)
self.user_icon_font = QFont()
self.user_icon_font.setLetterSpacing(QFont.AbsoluteSpacing, 0.58)
self.user_icon.setFont(self.user_icon_font)
# User button
self.user_button = UserButton()
# Add widgets to user auth layout
layout.addWidget(self.login_button, 1)
layout.addWidget(self.user_icon, 1)
layout.addWidget(self.user_button, 4)
# Align content to the top left
layout.addStretch()
layout.setAlignment(Qt.AlignTop)
def setup(self, window, controller):
self.user_button.setup(controller)
self.login_button.setup(window)
def set_user(self, db_user: User):
self.user_icon.setText(_(db_user.initials))
self.user_button.set_username(db_user.fullname)
def show(self):
self.login_button.hide()
self.user_icon.show()
self.user_button.show()
def hide(self):
self.user_icon.hide()
self.user_button.hide()
self.login_button.show()
class UserButton(SvgPushButton):
"""An menu button for the journalist menu
This button is responsible for launching the journalist menu on click.
"""
CSS = '''
SvgPushButton:focus {
outline: none;
}
SvgPushButton#user_button {
border: none;
font-family: 'Source Sans Pro';
font-weight: 700;
font-size: 12px;
color: #fff;
text-align: left;
}
SvgPushButton::menu-indicator {
image: none;
}
'''
def __init__(self):
super().__init__('dropdown_arrow.svg', svg_size=QSize(9, 6))
# Set css id
self.setObjectName('user_button')
# Set styles
self.setStyleSheet(self.CSS)
self.setFixedHeight(30)
self.setLayoutDirection(Qt.RightToLeft)
self.menu = UserMenu()
self.setMenu(self.menu)
# Set cursor.
self.setCursor(QCursor(Qt.PointingHandCursor))
def setup(self, controller):
self.menu.setup(controller)
def set_username(self, username):
formatted_name = _('{}').format(html.escape(username))
self.setText(formatted_name)
if len(formatted_name) > 21:
# The name will be truncated, so create a tooltip to display full
# name if the mouse hovers over the widget.
self.setToolTip(_('{}').format(html.escape(username)))
class UserMenu(QMenu):
"""A menu next to the journalist username.
A menu that provides login options.
"""
def __init__(self):
super().__init__()
self.logout = QAction(_('SIGN OUT'))
self.logout.setFont(QFont("OpenSans", 10))
self.addAction(self.logout)
self.logout.triggered.connect(self._on_logout_triggered)
def setup(self, controller):
"""
Store a reference to the controller (containing the application logic).
"""
self.controller = controller
def _on_logout_triggered(self):
"""
Called when the logout button is selected from the menu.
"""
self.controller.logout()
class LoginButton(QPushButton):
"""
A button that opens a login dialog when clicked.
"""
CSS = '''
#login {
border: none;
background-color: #05edfe;
font-family: 'Montserrat';
font-weight: 600;
font-size: 14px;
color: #2a319d;
}
#login:pressed {
background-color: #85f6fe;
}
'''
def __init__(self):
super().__init__(_('SIGN IN'))
# Set css id
self.setObjectName('login')
# Set styles
self.setStyleSheet(self.CSS)
self.setFixedHeight(40)
# Set click handler
self.clicked.connect(self._on_clicked)
def setup(self, window):
"""
Store a reference to the GUI window object.
"""
self.window = window
def _on_clicked(self):
"""
Called when the login button is clicked.
"""
self.window.show_login()
class MainView(QWidget):
"""
Represents the main content of the application (containing the source list
and main context view).
"""
CSS = '''
#main_view {
min-height: 558;
}
#view_holder {
min-width: 667;
border: none;
background-color: #f3f5f9;
}
QLabel#no-source {
font-family: Montserrat-Regular;
font-size: 35px;
color: #a5b3e9;
padding: 100px;
qproperty-alignment: AlignLeft;
}
'''
def __init__(self, parent: QObject):
super().__init__(parent)
# Set id and styles
self.setObjectName('main_view')
self.setStyleSheet(self.CSS)
# Set layout
self.layout = QHBoxLayout(self)
self.setLayout(self.layout)
# Set margins and spacing
self.layout.setContentsMargins(0, 0, 0, 0)
self.layout.setSpacing(0)
# Create SourceList widget
self.source_list = SourceList()
self.source_list.itemSelectionChanged.connect(self.on_source_changed)
# Create widgets
self.view_holder = QWidget()
self.view_holder.setObjectName('view_holder')
self.view_layout = QVBoxLayout()
self.view_holder.setLayout(self.view_layout)
self.view_layout.setContentsMargins(0, 0, 0, 0)
self.view_layout.setSpacing(0)
self.empty_conversation_view = EmptyConversationView()
self.view_layout.addWidget(self.empty_conversation_view)
# Add widgets to layout
self.layout.addWidget(self.source_list)
self.layout.addWidget(self.view_holder)
# Note: We should not delete SourceConversationWrapper when its source is unselected. This
# is a temporary solution to keep copies of our objects since we do delete them.
self.source_conversations = {} # type: Dict[Source, SourceConversationWrapper]
def setup(self, controller):
"""
Pass through the controller object to this widget.
"""
self.controller = controller
self.source_list.setup(controller)
def show_sources(self, sources: List[Source]):
"""
Update the left hand sources list in the UI with the passed in list of
sources.
"""
if len(sources) == 0:
self.empty_conversation_view.show_no_sources_message()
self.empty_conversation_view.show()
else:
self.empty_conversation_view.show_no_source_selected_message()
self.empty_conversation_view.show()
self.source_list.update(sources)
def on_source_changed(self):
"""
Show conversation for the currently-selected source if it hasn't been deleted. If the
current source no longer exists, clear the conversation for that source.
"""
source = self.source_list.get_current_source()
if source:
self.controller.session.refresh(source)
# Try to get the SourceConversationWrapper from the persistent dict,
# else we create it.
try:
conversation_wrapper = self.source_conversations[source]
# Redraw the conversation view such that new messages, replies, files appear.
conversation_wrapper.conversation_view.update_conversation(source.collection)
except KeyError:
conversation_wrapper = SourceConversationWrapper(source, self.controller)
self.source_conversations[source] = conversation_wrapper
self.set_conversation(conversation_wrapper)
else:
self.clear_conversation()
def set_conversation(self, widget):
"""
Update the view holder to contain the referenced widget.
"""
old_widget = self.view_layout.takeAt(0)
if old_widget and old_widget.widget():
old_widget.widget().hide()
self.empty_conversation_view.hide()
self.view_layout.addWidget(widget)
widget.show()
def clear_conversation(self):
while self.view_layout.count():
child = self.view_layout.takeAt(0)
if child.widget():
child.widget().deleteLater()
class EmptyConversationView(QWidget):
CSS = '''
#content {
font-family: Montserrat;
font-weight: 400;
font-size: 35px;
color: #a5b3e9;
qproperty-alignment: AlignLeft;
}
'''
def __init__(self):
super().__init__()
# Set id and styles
self.setObjectName('view')
self.setStyleSheet(self.CSS)
# Set layout
layout = QHBoxLayout(self)
self.setLayout(layout)
# Set margins and spacing
layout.setContentsMargins(0, 100, 0, 0)
layout.setSpacing(0)
# Create widgets
self.content = QLabel(self)
self.content.setObjectName('content')
self.content.setWordWrap(True)
content_layout = QVBoxLayout()
content_layout.addStretch(1)
content_layout.addWidget(self.content, 8)
content_layout.addStretch(1)
# Add widgets
layout.addStretch(1)
layout.addWidget(self.content, 5)
layout.addStretch(1)
def show_no_sources_message(self):
self.content.setText(
'Nothing to see just yet!\n\n'
'Source submissions will be listed to the left, once downloaded and decrypted.\n\n'
'This is where you will read messages, reply to sources, and work with files.\n\n')
def show_no_source_selected_message(self):
self.content.setText(
'Select a source from the list, to:\n\n'
'β’ Read a conversation\n'
'β’ View or retrieve files\n'
'β’ Send a response\n')
class SourceList(QListWidget):
"""
Displays the list of sources.
"""
CSS = '''
QListView {
border: none;
show-decoration-selected: 0;
border-right: 3px solid #f3f5f9;
}
QListView::item:selected {
background-color: #f3f5f9;
}
QListView::item:hover{
border: 500px solid #f9f9f9;
}
'''
def __init__(self):
super().__init__()
# Set id and styles.
self.setObjectName('sourcelist')
self.setStyleSheet(self.CSS)
self.setFixedWidth(445)
self.setUniformItemSizes(True)
# Set layout.
layout = QVBoxLayout(self)
self.setLayout(layout)
# To hold references to SourceWidget instances indexed by source UUID.
self.source_widgets = {}
def setup(self, controller):
self.controller = controller
self.controller.reply_succeeded.connect(self.set_snippet)
self.controller.message_ready.connect(self.set_snippet)
self.controller.reply_ready.connect(self.set_snippet)
self.controller.file_ready.connect(self.set_snippet)
self.controller.file_missing.connect(self.set_snippet)
def update(self, sources: List[Source]):
"""
Reset and update the list with the passed in list of sources.
"""
current_source = self.get_current_source()
current_source_id = current_source and current_source.id
self.clear()
for source in sources:
new_source = SourceWidget(source)
new_source.setup(self.controller)
self.source_widgets[source.uuid] = new_source
list_item = QListWidgetItem(self)
list_item.setSizeHint(new_source.sizeHint())
self.addItem(list_item)
self.setItemWidget(list_item, new_source)
if source.id == current_source_id:
self.setCurrentItem(list_item)
def get_current_source(self):
source_item = self.currentItem()
source_widget = self.itemWidget(source_item)
if source_widget and source_exists(self.controller.session, source_widget.source.uuid):
return source_widget.source
def set_snippet(self, source_uuid, message_uuid, content):
"""
Given a UUID of a source, if the referenced message is the latest
message, then update the source's preview snippet to the referenced
content.
"""
source_widget = self.source_widgets.get(source_uuid)
if source_widget:
source_widget.set_snippet(source_uuid, message_uuid, content)
class SourceWidget(QWidget):
"""
Used to display summary information about a source in the list view.
-----------------------------------------------------------------------------
| |
| ----------------------------------------------------------------- |
| | | |
| | | |
| | ------------- ---------------------------- ------------------ | |
| | | ------ | | ------ | | ----------- | | |
| | | |star| | | |name| | | |paperclip| | | |
| | | ------ | | ------ | | ----------- | | |
| | | | | --------- | | ----------- | | |
| | | | | |preview| | | |timestamp| | | |
| | | | | --------- | | ----------- | | |
| | | | | | | | | |
| | | gutter | | summary | | metadata | | |
| | ------------- ---------------------------- ------------------ | |
| | | |
| | source_widget | |
| ----------------------------------------------------------------- |
| SourceWidget |
-----------------------------------------------------------------------------
"""
CSS = '''
QWidget#source_widget {
border-bottom: 1px solid #9b9b9b;
}
QWidget#gutter {
min-width: 40px;
max-width: 40px;
}
QWidget#metadata {
max-width: 60px;
}
QLabel#preview {
font-family: 'Source Sans Pro';
font-weight: 400;
font-size: 13px;
color: #383838;
}
QLabel#source_name {
font-family: 'Montserrat';
font-weight: 500;
font-size: 13px;
color: #383838;
}
QLabel#timestamp {
font-family: 'Montserrat';
font-weight: 500;
font-size: 13px;
color: #383838;
}
'''
SIDE_MARGIN = 10
SOURCE_WIDGET_VERTICAL_MARGIN = 10
PREVIEW_WIDTH = 312
PREVIEW_HEIGHT = 60
def __init__(self, source: Source):
super().__init__()
# Store source
self.source = source
# Set styles
self.setStyleSheet(self.CSS)
# Set layout
layout = QHBoxLayout(self)
self.setLayout(layout)
# Set cursor.
self.setCursor(QCursor(Qt.PointingHandCursor))
# Remove margins and spacing
layout.setContentsMargins(self.SIDE_MARGIN, 0, self.SIDE_MARGIN, 0)
layout.setSpacing(0)
# Set up gutter
self.gutter = QWidget()
self.gutter.setObjectName('gutter')
gutter_layout = QVBoxLayout(self.gutter)
gutter_layout.setContentsMargins(0, 0, 0, 0)
gutter_layout.setSpacing(0)
self.star = StarToggleButton(self.source)
gutter_layout.addWidget(self.star)
gutter_layout.addStretch()
# Set up summary
self.summary = QWidget()
self.summary.setObjectName('summary')
summary_layout = QVBoxLayout(self.summary)
summary_layout.setContentsMargins(0, 0, 0, 0)
summary_layout.setSpacing(0)