1+ import sys
2+ import logging
3+ from PySide6 .QtWidgets import (
4+ QApplication ,
5+ QMainWindow ,
6+ QPushButton ,
7+ QHBoxLayout ,
8+ QVBoxLayout ,
9+ QWidget ,
10+ QLabel , QSizePolicy
11+ )
12+ from PySide6 .QtCore import Qt , QPoint , QEvent
13+
14+ # logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
15+
16+
17+ class MainWindow (QMainWindow ):
18+ def __init__ (self ):
19+ super ().__init__ ()
20+
21+ self .setWindowFlags (Qt .FramelessWindowHint )
22+ self .setAttribute (Qt .WA_TranslucentBackground )
23+ self .setStyleSheet ("background-color: rgb(150, 150, 150);" )
24+ self .setGeometry (100 , 100 , 600 , 450 )
25+ self .border_size = 5
26+
27+ # self.main_layout = QVBoxLayout()
28+ # self.main_layout.setContentsMargins(0, 0, 0, 0)
29+ # self.main_layout.setSpacing(0)
30+ # central_widget = QWidget()
31+ # central_widget.setLayout(self.main_layout)
32+ # self.setCentralWidget(central_widget)
33+ #
34+ # self.title_bar = QWidget()
35+ # self.title_bar.setObjectName("BarraDeTitulo")
36+ # self.title_bar.setFixedHeight(40)
37+ # self.title_bar.setStyleSheet("background-color: #333; color: white;")
38+ # self.title_bar.setMouseTracking(True)
39+ # title_bar_layout = QHBoxLayout(self.title_bar)
40+ # title_bar_layout.setContentsMargins(0, 0, 0, 0)
41+ # title_bar_layout.setSpacing(0)
42+ # self.title_label = QLabel("Movimento e Redimensionamento Nativo")
43+ # title_bar_layout.addWidget(self.title_label)
44+ # title_bar_layout.addStretch()
45+ #
46+ # self.minimize_button = QPushButton("−");
47+ # # self.minimize_button.setFixedSize(30, 25);
48+ # self.minimize_button.setStyleSheet("background-color: #555; color: white; border: none; font-size: 16px;");
49+ # self.minimize_button.clicked.connect(self.showMinimized);
50+ # title_bar_layout.addWidget(self.minimize_button)
51+ # self.maximize_button = QPushButton("□");
52+ # self.maximize_button.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding));
53+ # self.maximize_button.setContentsMargins(0, 0, 20, 0);
54+ # # self.maximize_button.setFixedSize(30, 25);
55+ # self.maximize_button.setStyleSheet("background-color: #555; color: white; border: none; font-size: 16px;");
56+ # self.maximize_button.clicked.connect(self.toggle_maximize_restore);
57+ # title_bar_layout.addWidget(self.maximize_button)
58+ # self.close_button = QPushButton("X");
59+ # # self.close_button.setFixedSize(30, 25);
60+ # self.close_button.setStyleSheet("background-color: #f00; color: white; border: none; font-size: 16px;");
61+ # self.close_button.clicked.connect(self.close);
62+ # title_bar_layout.addWidget(self.close_button)
63+ # title_bar_layout.setStretchFactor(self.title_label, 15)
64+ #
65+ # self.content_widget = QWidget()
66+ # self.content_widget.setObjectName("mainContent")
67+ # self.content_layout = None
68+ # # self.content_layout = QVBoxLayout(self.content_widget)
69+ # # self.content_layout.setContentsMargins(0, 0, 0, 0)
70+ # # self.content_layout.setSpacing(0)
71+ #
72+ # self.main_layout.addWidget(self.title_bar)
73+ # self.main_layout.addWidget(self.content_widget)
74+ #
75+ # # # logging.info("Instalando event filter na BarraDeTitulo e AreaDeConteudo")
76+ # self.title_bar.installEventFilter(self)
77+ # self.content_widget.installEventFilter(self)
78+ self .installEventFilter (self )
79+
80+ def update_title (self , new_title : str ):
81+ """
82+ Atualiza o texto do título na barra de título.
83+
84+ @param new_title: Novo texto para o título da janela.
85+ """
86+ self .title_label .setText (new_title )
87+
88+ def set_content_layout (self , new_layout ):
89+ """
90+ Substitui o layout do content_widget pelo novo_layout.
91+ """
92+ self .content_layout = new_layout
93+ current_layout = self .content_widget .layout ()
94+ if current_layout is not None :
95+ current_layout .deleteLater ()
96+
97+ self .content_widget .setLayout (new_layout )
98+
99+ new_layout .setContentsMargins (0 , 0 , 0 , 0 )
100+ new_layout .setSpacing (0 )
101+
102+ def toggle_maximize_restore (self ):
103+ if self .isMaximized ():
104+ self .showNormal ()
105+ else :
106+ self .showMaximized ()
107+
108+ def get_edge (self , pos : QPoint ):
109+ rect = self .rect ()
110+ left = pos .x () < self .border_size
111+ top = pos .y () < self .border_size
112+ right = pos .x () > rect .width () - self .border_size
113+ bottom = pos .y () > rect .height () - self .border_size
114+
115+ # if top and left: return Qt.TopEdge | Qt.LeftEdge
116+ if top and right : return Qt .TopEdge | Qt .RightEdge
117+ if bottom and left : return Qt .BottomEdge | Qt .LeftEdge
118+ if bottom and right : return Qt .BottomEdge | Qt .RightEdge
119+ if left : return Qt .LeftEdge
120+ # if top: return Qt.TopEdge
121+ if right : return Qt .RightEdge
122+ if bottom : return Qt .BottomEdge
123+ return None
124+
125+ def eventFilter (self , watched_object , event ):
126+ obj_name = watched_object .objectName () or "QMainWindow"
127+
128+ if event .type () == QEvent .Type .MouseMove :
129+ global_pos = event .globalPosition ().toPoint ()
130+ pos_na_janela = self .mapFromGlobal (global_pos )
131+
132+ edge = self .get_edge (pos_na_janela )
133+ if edge :
134+ if edge == (Qt .TopEdge | Qt .LeftEdge ) or edge == (Qt .BottomEdge | Qt .RightEdge ):
135+ self .setCursor (Qt .SizeFDiagCursor )
136+ elif edge == (Qt .TopEdge | Qt .RightEdge ) or edge == (Qt .BottomEdge | Qt .LeftEdge ):
137+ self .setCursor (Qt .SizeBDiagCursor )
138+ elif edge == Qt .LeftEdge or edge == Qt .RightEdge :
139+ self .setCursor (Qt .SizeHorCursor )
140+ else :
141+ self .setCursor (Qt .SizeVerCursor )
142+ else :
143+ self .unsetCursor ()
144+
145+ elif event .type () == QEvent .Type .Leave :
146+ # logging.info(f"Mouse saiu do widget {obj_name}. Restaurando cursor.")
147+ self .unsetCursor ()
148+
149+ elif event .type () == QEvent .Type .MouseButtonPress :
150+ if event .button () == Qt .LeftButton :
151+ global_pos = event .globalPosition ().toPoint ()
152+ pos_na_janela = self .mapFromGlobal (global_pos )
153+ edge = self .get_edge (pos_na_janela )
154+
155+ if edge :
156+ self .windowHandle ().startSystemResize (edge )
157+ elif self .title_bar .rect ().contains (self .title_bar .mapFromGlobal (global_pos )):
158+ self .windowHandle ().startSystemMove ()
159+
160+ return super ().eventFilter (watched_object , event )
161+
162+
163+ if __name__ == "__main__" :
164+ app = QApplication (sys .argv )
165+ janela = MainWindow ()
166+ janela .show ()
167+ sys .exit (app .exec ())
0 commit comments