13
13
from __future__ import unicode_literals
14
14
15
15
import os
16
+ import re
16
17
import sys
17
18
import json
18
19
import logging
@@ -337,10 +338,6 @@ def __init__(self, manager, tree_widget=None, locale='en_us'):
337
338
preview_area .addWidget (self .image_space )
338
339
preview_area .addWidget (self .img_preview_progress_bar )
339
340
340
- # Create a widget that displays the link of the file on OSF
341
- self .link_widget = self .__create_link_widget ()
342
- self .link_widget .hide ()
343
-
344
341
## Create layouts
345
342
346
343
# The box layout holding all elements
@@ -352,8 +349,6 @@ def __init__(self, manager, tree_widget=None, locale='en_us'):
352
349
info_grid .setSpacing (10 )
353
350
info_grid .addLayout (preview_area )
354
351
info_grid .addLayout (properties_pane )
355
- info_grid .addStretch ()
356
- info_grid .addWidget (self .link_widget )
357
352
358
353
# The widget to hold the infogrid
359
354
self .info_frame = QtWidgets .QWidget ()
@@ -403,26 +398,6 @@ def __init__(self, manager, tree_widget=None, locale='en_us'):
403
398
self .tree .refreshFinished .connect (self .__tree_refresh_finished )
404
399
405
400
### Private functions
406
- def __create_link_widget (self ):
407
- wdgt = QtWidgets .QWidget (self )
408
- layout = QtWidgets .QHBoxLayout (wdgt )
409
-
410
- osf_link_label = QtWidgets .QLabel (_ (u"Link" ))
411
- osf_link_label .setStyleSheet ('font-weight: bold' )
412
- self .osf_link_field = QtWidgets .QLabel ()
413
- copy_icon = QtGui .QIcon .fromTheme ('accessories-clipboard' ,
414
- qta .icon ('fa.clipboard' ))
415
- button_copy_to_clipboard = QtWidgets .QPushButton (copy_icon ,
416
- _ (u"Copy" ))
417
- visit_osf_icon = QtGui .QIcon .fromTheme ('web-browser' , qta .icon ('fa.globe' ))
418
- button_open_in_browser = QtWidgets .QPushButton (visit_osf_icon ,
419
- _ (u"View online" ))
420
- layout .addWidget (osf_link_label )
421
- layout .addWidget (self .osf_link_field )
422
- layout .addWidget (button_copy_to_clipboard )
423
- layout .addWidget (button_open_in_browser )
424
- return wdgt
425
-
426
401
def __resizeImagePreview (self , event ):
427
402
""" Resize the image preview (if there is any) after a resize event """
428
403
if not self .current_img_preview is None :
@@ -537,22 +512,26 @@ def __create_properties_pane(self):
537
512
labelStyle = 'font-weight: bold'
538
513
539
514
self .common_fields = ['Name' ,'Type' ]
540
- self .file_fields = ['Size' ,'Created' ,'Modified' ]
515
+ self .file_fields = ['Size' ,'Created' ,'Modified' , 'Link' ]
541
516
542
517
self .properties = {}
543
518
for field in self .common_fields + self .file_fields :
544
519
label = QtWidgets .QLabel (_ (field ))
545
520
label .setStyleSheet (labelStyle )
546
- value = QElidedLabel ('' )
547
- value .setWindowFlags (QtCore .Qt .Dialog )
521
+ if field == "Link" :
522
+ # Initialize label with some HTML to trigger the rich text mode
523
+ value = QtWidgets .QLabel ('<a></a>' )
524
+ value .setOpenExternalLinks (True )
525
+ else :
526
+ value = QElidedLabel ('' )
527
+ value .setWindowFlags (QtCore .Qt .Dialog )
548
528
self .properties [field ] = (label ,value )
549
529
properties_pane .addRow (label ,value )
550
530
551
531
# Make sure the fields specific for files are shown
552
532
for row in self .file_fields :
553
533
for field in self .properties [row ]:
554
534
field .hide ()
555
-
556
535
return properties_pane
557
536
558
537
### Public functions
@@ -735,6 +714,37 @@ def set_file_properties(self, data):
735
714
for field in self .properties [row ]:
736
715
field .show ()
737
716
717
+ # Get the link to the file on the website of OSF. This is not readily
718
+ # available from the returned API data, but can be parsed from the
719
+ # comments URL, of which it is the [target] filter parameter
720
+ # Sadly, this is URL is not always available for all files, so hide the
721
+ # row if parsing fails.
722
+ try :
723
+ comments_url = data ["relationships" ]["comments" ]["links" ]["related" ]\
724
+ ["href" ]
725
+ except KeyError as e :
726
+ warnings .warn ('Could not retrieve comments url, because of missing field {}' .format (e ))
727
+ self .properties ["Link" ][0 ].hide ()
728
+ self .properties ["Link" ][1 ].hide ()
729
+ else :
730
+ # Use regular expression to search for the relevant part of the url
731
+ try :
732
+ target = re .search ('filter\[target\]\=\w+' , comments_url ).group (0 )
733
+ except AttributeError :
734
+ # If this didn't work, hide the row altogether
735
+ self .properties ["Link" ][0 ].hide ()
736
+ self .properties ["Link" ][1 ].hide ()
737
+ else :
738
+ # Get the ID part of the filter parameter and generate the url
739
+ web_id = target .split ("=" )[1 ]
740
+ web_url = u"https://osf.io/{}" .format (web_id )
741
+ a = u"<a href=\" {0}\" >{0}</a>" .format (web_url )
742
+ # Set the URL in the field
743
+ self .properties ["Link" ][1 ].setText (a )
744
+ # Show the row
745
+ self .properties ["Link" ][0 ].show ()
746
+ self .properties ["Link" ][1 ].show ()
747
+
738
748
def set_folder_properties (self , data ):
739
749
"""
740
750
Fills the contents of the properties pane for folders. Make sure the
@@ -851,13 +861,11 @@ def __slot_currentItemChanged(self, item, col):
851
861
self .upload_button .setDisabled (True )
852
862
self .delete_button .setDisabled (False )
853
863
self .new_folder_button .setDisabled (True )
854
- self .link_widget .show ()
855
864
elif kind == "folder" :
856
865
self .set_folder_properties (data )
857
866
self .new_folder_button .setDisabled (False )
858
867
self .download_button .setDisabled (True )
859
868
self .upload_button .setDisabled (False )
860
- self .link_widget .hide ()
861
869
# Check if the parent node is a project
862
870
# If so the current 'folder' must be a storage provider (e.g. dropbox)
863
871
# which should not be allowed to be deleted.
0 commit comments