fosKeyMan
Loading...
Searching...
No Matches
hoverinfo.py
Go to the documentation of this file.
1from PySide6.QtWidgets import QDialog, QLabel, QVBoxLayout
2from PySide6.QtCore import Qt, QEvent, QTimer, QPoint
3from frontend.keystatus import ActivationStatus
4
5
6class HoverInfo(QDialog):
7 r"""
8 A tooltip dialog that provides detailed information about a table cell when the mouse hovers over it.
9
10 This class creates a small hover window that displays information such as the cell's content,
11 the activation status of the keyfile, and its status in the database. It is triggered when the mouse
12 hovers over a cell in the associated table widget.
13 """
14 def __init__(self, table_widget, parent=None):
15 r"""
16 Initialize the HoverInfo dialog with the table widget to monitor.
17
18 \param table_widget (QTableWidget): The table widget to track mouse hover events on.
19 \param parent (QWidget, optional): The parent widget.
20 """
21
22 super(HoverInfo, self).__init__(parent)
23
24 self.setWindowFlags(Qt.WindowType.ToolTip)
25 layout = QVBoxLayout()
26
27 self.cell_label = QLabel(self.tr("Cell: "))
28 layout.addWidget(self.cell_label)
29
30 self.activate_label = QLabel(self.tr("Activation Status: "))
31 layout.addWidget(self.activate_label)
32
33 self.setLayout(layout)
34
35 # Initialize a timer for delayed tooltip display
36 self.timer = QTimer(self)
38
39 # Associated table widget to track mouse movements
40 self.table_widget = table_widget
41 self.table_widget.setMouseTracking(True)
42 self.table_widget.viewport().installEventFilter(self)
43
44 def eventFilter(self, source, event):
45 r"""
46 Capture mouse hover events and display hover info.
47
48 This method handles mouse movement over the table widget to determine which cell the mouse is over,
49 and then triggers the tooltip display after a delay.
50
51 \param source (QObject): The source widget that the event is coming from (table widget viewport).
52 \param event (QEvent): The event being processed (mouse movement).
53 \return (bool): Returns True if the event is handled, otherwise False to pass the event to the base class.
54 """
55 if event.type() == QEvent.Type.MouseMove:
56 if source == self.table_widget.viewport():
57 pos = event.pos()
58 item = self.table_widget.itemAt(pos)
59 if item:
60 column = item.column()
61 if column >= 2:
63 self.timer.timeout.disconnect()
64 self.timer_signal_connected = False
65
66 # Connect the timer's timeout signal to display hover info after a delay
67 self.timer.timeout.connect(lambda: self.show_hover_info(item, pos))
68 self.timer_signal_connected = True
69 self.timer.start(1000)
70 else:
71 if self.isVisible():
72 self.hide()
73 self.timer.stop()
74
75 elif event.type() == QEvent.Type.Leave:
76 if self.isVisible():
77 self.hide()
78 self.timer.stop()
79
80 return super().eventFilter(source, event)
81
82 def show_hover_info(self, item, pos):
83 r"""
84 Show the hover info dialog with details about the cell.
85 Hover info includes the table header name of this column, cell value, activation status,
86 and database status.
87
88 \param item (QTableWidgetItem): The table cell item that is being hovered over.
89 \param pos (QPoint): The current position of the mouse cursor.
90 """
91 row = item.row()
92 col = item.column()
93
94 column_name = self.table_widget.horizontalHeaderItem(col).text()
95 if col == 2:
96 cell_value = item.data(Qt.ItemDataRole.UserRole + 2)
97 else:
98 cell_value = item.text()
99
100 activation_status = self.table_widget.item(row, 1).data(Qt.ItemDataRole.UserRole + 1)
101 if activation_status == ActivationStatus.ACTIVATED:
102 activate_status = self.tr("Keyfile is Activated")
103 elif activation_status == ActivationStatus.DEACTIVATED:
104 activate_status = self.tr("Keyfile is Deactivated")
105 else:
106 activate_status = self.tr("")
107
108 self.cell_label.setText(f"{column_name}: {cell_value}")
109 self.activate_label.setText(
110 self.tr("Activation Status: {activate_status}").format(activate_status=activate_status))
111
112 table_pos = self.table_widget.mapToGlobal(pos)
113 self.move(table_pos + QPoint(20, 50))
114 self.show()
115
A tooltip dialog that provides detailed information about a table cell when the mouse hovers over it.
Definition hoverinfo.py:6
eventFilter(self, source, event)
Capture mouse hover events and display hover info.
Definition hoverinfo.py:44
show_hover_info(self, item, pos)
Show the hover info dialog with details about the cell.
Definition hoverinfo.py:82
__init__(self, table_widget, parent=None)
Initialize the HoverInfo dialog with the table widget to monitor.
Definition hoverinfo.py:14