Debian Codegraph @

Nagstamon/qui/widgets/draggables.py

raw
1 # Nagstamon - Nagios status monitor for your desktop
2 # Copyright (C) 2008-2026 Henri Wahl <[email protected]> et al.
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17
18 from time import time
19
20 from Nagstamon.config import conf
21 from Nagstamon.qui.globals import statuswindow_properties
22 from Nagstamon.qui.qt import (get_global_position,
23 QLabel,
24 QSizePolicy,
25 QSvgWidget,
26 QWidget,
27 Qt,
28 Signal)
29
30
31 class DraggableWidget(QWidget):
32 """
33 used to give various top area and statusbar widgets draggability
34 """
35 # yell if statusbar is moved
36 window_moved = Signal()
37
38 # needed for popup after hover
39 mouse_entered = Signal()
40
41 # needed for popup after click
42 mouse_pressed = Signal()
43 mouse_released = Signal()
44 # needed to close window in some configurations
45 mouse_released_in_window = Signal()
46
47 # keep state of right button pressed to avoid dragging and
48 # unwanted repositioning of statuswindow
49 right_mouse_button_pressed = False
50
51 parent_statuswindow = None
52
53 def __init__(self, parent=None):
54 QWidget.__init__(self, parent=parent)
55
56 def set_menu(self, menu):
57 self.menu = menu
58
59 def mousePressEvent(self, event):
60 """
61 react differently to mouse button presses:
62 1 - left button, move window
63 2 - right button, popup menu
64 """
65 # update access to status window
66 self.parent_statuswindow = self.parentWidget().parentWidget()
67 if event.button() == Qt.MouseButton.LeftButton:
68 self.mouse_pressed.emit()
69 if event.button() == Qt.MouseButton.RightButton:
70 self.right_mouse_button_pressed = True
71
72 # keep x and y relative to statusbar
73 # if not set calculate relative position
74 if not statuswindow_properties.relative_x and \
75 not statuswindow_properties.relative_y:
76 # Qt5 & Qt6 have different methods for getting the global position so take it from qt.py
77 global_position = get_global_position(event)
78 statuswindow_properties.relative_x = global_position.x() - self.parent_statuswindow.x()
79 statuswindow_properties.relative_y = global_position.y() - self.parent_statuswindow.y()
80
81 def mouseReleaseEvent(self, event):
82 """
83 decide if moving or menu should be treated after mouse button was released
84 """
85 if event.button() == Qt.MouseButton.LeftButton:
86 # if popup window should be closed by clicking do it now
87 if statuswindow_properties.is_shown and \
88 (conf.close_details_clicking or
89 conf.close_details_clicking_somewhere) and \
90 not conf.fullscreen and not conf.windowed:
91 statuswindow_properties.is_hiding_timestamp = time()
92 self.mouse_released_in_window.emit()
93 elif not statuswindow_properties.is_shown:
94 self.mouse_released.emit()
95
96 # reset all helper values
97 statuswindow_properties.moving = False
98 statuswindow_properties.relative_x = 0
99 statuswindow_properties.relative_y = 0
100
101 if event.button() == Qt.MouseButton.RightButton:
102 self.right_mouse_button_pressed = False
103 self.menu.show_at_cursor()
104
105 def mouseMoveEvent(self, event):
106 """
107 do the moving action
108 """
109 # if window should close when being clicked it might be problematic if it
110 # will be moved unintendedly so try to filter this events out by waiting 0.5 seconds
111 if not (conf.close_details_clicking and
112 statuswindow_properties.is_shown and
113 statuswindow_properties.is_shown_timestamp + 0.5 < time()):
114 if not conf.fullscreen and not conf.windowed and not self.right_mouse_button_pressed:
115 # Qt5 & Qt6 have different methods for getting the global position so take it from qt.py
116 global_position = get_global_position(event)
117 # lock window as moving
118 # if not set calculate relative position
119 if not statuswindow_properties.relative_x and not statuswindow_properties.relative_y:
120 statuswindow_properties.relative_x = global_position.x() - self.parent_statuswindow.x()
121 statuswindow_properties.relative_y = global_position.y() - self.parent_statuswindow.y()
122 statuswindow_properties.moving = True
123 # TODO: shall become a signal
124 self.parent_statuswindow.move(int(global_position.x() - statuswindow_properties.relative_x),
125 int(global_position.y() - statuswindow_properties.relative_y))
126
127 self.window_moved.emit()
128
129 def enterEvent(self, event):
130 """
131 tell the world that mouse entered the widget - interesting for hover popup and only if top area hasn't been
132 clicked a moment ago
133 """
134 if statuswindow_properties.is_shown is False and \
135 statuswindow_properties.is_hiding_timestamp + 0.2 < time():
136 self.mouse_entered.emit()
137
138
139 class DraggableLabel(QLabel, DraggableWidget):
140 """
141 label with dragging capabilities used by top area
142 """
143 # yell if statusbar is moved
144 window_moved = Signal()
145
146 # needed for popup after hover
147 mouse_entered = Signal()
148
149 # needed for popup after click
150 mouse_pressed = Signal()
151 mouse_released = Signal()
152 # needed to close window in some configurations
153 mouse_released_in_window = Signal()
154
155 def __init__(self, text='', parent=None):
156 QLabel.__init__(self, text, parent=parent)