Skip to content

Commit

Permalink
Request to Core on tooltip
Browse files Browse the repository at this point in the history
  • Loading branch information
ichorid committed Dec 24, 2021
1 parent c0f16c9 commit 839069f
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def make_copy(self, tgt_parent_id, attributes_override=None):
dst_dict.update({"origin_id": tgt_parent_id, "status": NEW})
return self.__class__(**dst_dict)

def to_simple_dict(self):
def to_simple_dict(self, include_path=False):
"""
Return a basic dictionary with information about the node
"""
Expand All @@ -310,6 +310,14 @@ def to_simple_dict(self):
"public_key": hexlify(self.public_key),
"status": self.status,
}
if include_path:
parent_nodes = self.get_parent_nodes()[:-1]
simple_dict.update(
{
"parents_path_text": list(p.title for p in parent_nodes),
"parents_path_ids": list(p.id_ for p in parent_nodes)
}
)

return simple_dict

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
)
from tribler_core.utilities.utilities import random_infohash


# pylint: disable=too-many-statements


Expand Down Expand Up @@ -67,8 +66,8 @@ def state(self):

return CHANNEL_STATE.PREVIEW.value

def to_simple_dict(self):
result = super().to_simple_dict()
def to_simple_dict(self, **kwargs):
result = super().to_simple_dict(**kwargs)
result.update(
{
"torrents": self.num_entries,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class MetadataNode(db.ChannelNode):
][1:]
nonpersonal_attributes = db.ChannelNode.nonpersonal_attributes + ('title', 'tags')

def to_simple_dict(self, include_path=False, **kwargs):
def to_simple_dict(self, **kwargs):
"""
Return a basic dictionary with information about the channel.
"""
Expand All @@ -38,17 +38,8 @@ def to_simple_dict(self, include_path=False, **kwargs):
{
"name": self.title,
"category": self.tags,
"parents_path_text": list(p.title for p in self.get_parent_nodes())[:-1]
}
)
if include_path:
parent_nodes = self.get_parent_nodes()[:-1]
simple_dict.update(
{
"parents_path_text": list(p.title for p in parent_nodes),
"parents_path_ids": list(p.int for p in parent_nodes)
}
)

return simple_dict

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def setup_routes(self):
web.delete('', self.delete_channel_entries),
web.get('/torrents/{infohash}/health', self.get_torrent_health),
web.patch(r'/{public_key:\w*}/{id:\w*}', self.update_channel_entry),
web.get(r'/{public_key:\w*}/{id:\w*}', self.get_channel_entries),
web.get(r'/{public_key:\w*}/{id:\w*}', self.get_channel_entry),
]
)

Expand Down Expand Up @@ -158,18 +158,17 @@ async def update_channel_entry(self, request):

@docs(
tags=['Metadata'],
summary='Get channel entries.',
responses={200: {'description': 'Returns a list of entries'}, HTTP_NOT_FOUND: {'schema': HandledErrorSchema}},
summary='Get channel entry.',
responses={200: {'description': 'Returns the entry'}, HTTP_NOT_FOUND: {'schema': HandledErrorSchema}},
)
async def get_channel_entries(self, request):
async def get_channel_entry(self, request):
public_key = unhexlify(request.match_info['public_key'])
id_ = request.match_info['id']
with db_session:
entry = self.mds.ChannelNode.get(public_key=public_key, id_=id_)

if entry:
# TODO: handle costly attributes in a more graceful and generic way for all types of metadata
entry_dict = entry.to_simple_dict()
entry_dict = entry.to_simple_dict(include_path=True)
else:
return RESTResponse({"error": "entry not found in database"}, status=HTTP_NOT_FOUND)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# This dict is used to translate JSON fields into the columns used in Pony for _sorting_.
# id_ is not in the list because there is not index on it, so we never really want to sort on it.
json2pony_columns = {
'public_key': 'public_key',
'category': "tags",
'name': "title",
'size': "size",
Expand Down
61 changes: 51 additions & 10 deletions src/tribler-gui/tribler_gui/widgets/clickabletooltip.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QObject, Qt

from tribler_gui.tribler_request_manager import TriblerNetworkRequest
from tribler_gui.widgets.tablecontentmodel import Column, get_user_name_cached


"""
Adapted for QTableView from
Expand Down Expand Up @@ -228,6 +231,45 @@ class ClickableTooltipFilter(QObject):
def toolTipLinkClicked(self, url):
print(url)

def show_author_tooltip(self, table_view, index, item):
def on_details_received(response):
if not response:
return
item.update(response)
print("BLA", response)

pk = item.get('public_key', "0")
user_name = get_user_name_cached(pk)
tooltip_txt = user_name
if path := item.get('parents_path_text', []):
tooltip_txt += "\n" + " / ".join(str(p) for p in path)
tooltip_txt += "\n" + 'This is <a href="http://test.com/{c}">link {c}</a>'

self.show_tooltip(table_view, index, tooltip_txt)

if "parents_path_text" not in item and "parents_path_ids" not in item:
TriblerNetworkRequest(
f"metadata/{item['public_key']}/{item['id']}",
on_details_received,
method='GET',
)
else:
on_details_received(item)

def show_tooltip(self, table_view, index, txt):

if not txt:
if ClickableTooltip.instance() is not None:
ClickableTooltip.instance().hide()

if txt is not None:
print("SHOWN")
rect = table_view.visualRect(index)
toolTip = ClickableTooltip.showText(QtGui.QCursor.pos(), txt, parent=table_view, rect=rect)
if toolTip is not None:
toolTip.linkActivated.connect(self.toolTipLinkClicked)
return True

def eventFilter(self, source, event):
if event.type() == QtCore.QEvent.ToolTip:
print("TT EVENT")
Expand All @@ -239,17 +281,16 @@ def eventFilter(self, source, event):
index = table_view.indexAt(pos)
if not index.isValid():
return False
txt = table_view.model().data(index, Qt.ToolTipRole)

if not txt:
if ClickableTooltip.instance() is not None:
ClickableTooltip.instance().hide()
model = table_view.model()
column_type = model.columns_shown[index.column()]
if column_type == Column.AUTHOR:
item = table_view.model().data_items[index.row()]
self.show_author_tooltip(table_view, index, item)
return True

if txt is not None:
print("SHOWN")
rect = table_view.visualRect(index)
toolTip = ClickableTooltip.showText(QtGui.QCursor.pos(), txt, parent=table_view, rect=rect)
toolTip.linkActivated.connect(self.toolTipLinkClicked)
txt = table_view.model().data(index, Qt.ToolTipRole)
self.show_tooltip(table_view, index, txt)
return True

return True
return super().eventFilter(source, event)
8 changes: 1 addition & 7 deletions src/tribler-gui/tribler_gui/widgets/tablecontentmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,13 +451,7 @@ def item_txt(self, index, role, is_editing: bool = False):
if item.get('public_key') == "":
return "Anonymous author"

pk = item.get('public_key', "0")
user_name = get_user_name_cached(pk)
tooltip_txt = user_name
if path := item.get('parents_path_text', []):
tooltip_txt += "\n" + " / ".join(str(p) for p in path)
tooltip_txt += "\n" + 'This is <a href="http://test.com/{c}">link {c}</a>'
return tooltip_txt
return "PLACEHOLDER FOR AUTHOR TOOLTIP"

# The 'name' column is special in a sense that we want to draw the title and tags ourselves.
# At the same time, we want to name this column to not break the renaming of torrent files, hence this check.
Expand Down

0 comments on commit 839069f

Please sign in to comment.