Skip to content

Commit 702bff4

Browse files
committed
Now providing a link to online version of file
1 parent 919f183 commit 702bff4

File tree

1 file changed

+40
-32
lines changed

1 file changed

+40
-32
lines changed

QOpenScienceFramework/widgets.py

+40-32
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from __future__ import unicode_literals
1414

1515
import os
16+
import re
1617
import sys
1718
import json
1819
import logging
@@ -337,10 +338,6 @@ def __init__(self, manager, tree_widget=None, locale='en_us'):
337338
preview_area.addWidget(self.image_space)
338339
preview_area.addWidget(self.img_preview_progress_bar)
339340

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-
344341
## Create layouts
345342

346343
# The box layout holding all elements
@@ -352,8 +349,6 @@ def __init__(self, manager, tree_widget=None, locale='en_us'):
352349
info_grid.setSpacing(10)
353350
info_grid.addLayout(preview_area)
354351
info_grid.addLayout(properties_pane)
355-
info_grid.addStretch()
356-
info_grid.addWidget(self.link_widget)
357352

358353
# The widget to hold the infogrid
359354
self.info_frame = QtWidgets.QWidget()
@@ -403,26 +398,6 @@ def __init__(self, manager, tree_widget=None, locale='en_us'):
403398
self.tree.refreshFinished.connect(self.__tree_refresh_finished)
404399

405400
### 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-
426401
def __resizeImagePreview(self, event):
427402
""" Resize the image preview (if there is any) after a resize event """
428403
if not self.current_img_preview is None:
@@ -537,22 +512,26 @@ def __create_properties_pane(self):
537512
labelStyle = 'font-weight: bold'
538513

539514
self.common_fields = ['Name','Type']
540-
self.file_fields = ['Size','Created','Modified']
515+
self.file_fields = ['Size','Created','Modified','Link']
541516

542517
self.properties = {}
543518
for field in self.common_fields + self.file_fields:
544519
label = QtWidgets.QLabel(_(field))
545520
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)
548528
self.properties[field] = (label,value)
549529
properties_pane.addRow(label,value)
550530

551531
# Make sure the fields specific for files are shown
552532
for row in self.file_fields:
553533
for field in self.properties[row]:
554534
field.hide()
555-
556535
return properties_pane
557536

558537
### Public functions
@@ -735,6 +714,37 @@ def set_file_properties(self, data):
735714
for field in self.properties[row]:
736715
field.show()
737716

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+
738748
def set_folder_properties(self, data):
739749
"""
740750
Fills the contents of the properties pane for folders. Make sure the
@@ -851,13 +861,11 @@ def __slot_currentItemChanged(self, item, col):
851861
self.upload_button.setDisabled(True)
852862
self.delete_button.setDisabled(False)
853863
self.new_folder_button.setDisabled(True)
854-
self.link_widget.show()
855864
elif kind == "folder":
856865
self.set_folder_properties(data)
857866
self.new_folder_button.setDisabled(False)
858867
self.download_button.setDisabled(True)
859868
self.upload_button.setDisabled(False)
860-
self.link_widget.hide()
861869
# Check if the parent node is a project
862870
# If so the current 'folder' must be a storage provider (e.g. dropbox)
863871
# which should not be allowed to be deleted.

0 commit comments

Comments
 (0)