|
1 | 1 | #!/usr/bin/env python |
2 | 2 | # -*- coding: utf-8 -*- |
3 | 3 | """ |
4 | | -Bootstrap para exibir a splash screen imediatamente e depois carregar o aplicativo. |
| 4 | +Bootstrap para exibir uma splash screen customizada com barra de progresso, mensagens dinâmicas com fundo e efeito de fade-out, |
| 5 | +e depois carregar o aplicativo principal. |
5 | 6 | """ |
6 | | -from PyQt5.QtWebEngineWidgets import QWebEngineView |
| 7 | + |
7 | 8 | import sys |
8 | 9 | import os |
9 | | -from PyQt5.QtWidgets import QApplication, QSplashScreen |
10 | | -from PyQt5.QtGui import QPixmap, QColor |
11 | | -from PyQt5.QtCore import Qt |
| 10 | +from PyQt5.QtCore import Qt, QTimer, QPropertyAnimation |
| 11 | +from PyQt5.QtGui import QPixmap, QColor, QFont |
| 12 | +from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout, QProgressBar |
| 13 | + |
| 14 | +# Importa o QWebEngineView logo no início (para evitar erros de inicialização) |
| 15 | +from PyQt5.QtWebEngineWidgets import QWebEngineView |
| 16 | + |
| 17 | +class CustomSplashScreen(QWidget): |
| 18 | + def __init__(self, image_path, parent=None): |
| 19 | + super().__init__(parent) |
| 20 | + # Remove a borda e garante que a janela fique sempre no topo |
| 21 | + self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint) |
| 22 | + # Permite transparência (opcional) |
| 23 | + self.setAttribute(Qt.WA_TranslucentBackground) |
| 24 | + |
| 25 | + # Layout principal |
| 26 | + self.layout = QVBoxLayout(self) |
| 27 | + self.layout.setContentsMargins(20, 20, 20, 20) |
| 28 | + self.layout.setSpacing(10) |
| 29 | + |
| 30 | + # Label com a imagem da splash |
| 31 | + self.image_label = QLabel(self) |
| 32 | + if os.path.exists(image_path): |
| 33 | + pixmap = QPixmap(image_path) |
| 34 | + else: |
| 35 | + # Cria um pixmap padrão se a imagem não for encontrada |
| 36 | + pixmap = QPixmap(400, 300) |
| 37 | + pixmap.fill(QColor("white")) |
| 38 | + self.image_label.setPixmap(pixmap) |
| 39 | + self.image_label.setAlignment(Qt.AlignCenter) |
| 40 | + self.layout.addWidget(self.image_label) |
| 41 | + |
| 42 | + # Label para mensagens de carregamento com fundo semi-transparente |
| 43 | + self.message_label = QLabel("Carregando o aplicativo...", self) |
| 44 | + self.message_label.setAlignment(Qt.AlignCenter) |
| 45 | + self.message_label.setFont(QFont("Arial", 12)) |
| 46 | + # Aplica um estilo com fundo preto semi-transparente e texto branco |
| 47 | + self.message_label.setStyleSheet(""" |
| 48 | + background-color: rgba(0, 0, 0, 150); |
| 49 | + color: white; |
| 50 | + padding: 5px; |
| 51 | + border-radius: 5px; |
| 52 | + """) |
| 53 | + self.layout.addWidget(self.message_label) |
| 54 | + |
| 55 | + # Barra de progresso |
| 56 | + self.progress_bar = QProgressBar(self) |
| 57 | + self.progress_bar.setRange(0, 100) |
| 58 | + self.progress_bar.setValue(0) |
| 59 | + self.progress_bar.setTextVisible(True) |
| 60 | + self.layout.addWidget(self.progress_bar) |
| 61 | + |
| 62 | + # Atributo para armazenar a animação (evita coleta prematura) |
| 63 | + self.fade_animation = None |
| 64 | + |
| 65 | + def update_progress(self, value, message=None): |
| 66 | + """Atualiza a barra de progresso e, se fornecida, a mensagem exibida.""" |
| 67 | + self.progress_bar.setValue(value) |
| 68 | + if message: |
| 69 | + self.message_label.setText(message) |
12 | 70 |
|
13 | 71 | def main(): |
14 | 72 | app = QApplication(sys.argv) |
15 | 73 |
|
16 | | - # Define o caminho da imagem da splash screen |
17 | | - splash_image = "splash.png" # Substitua pelo caminho correto, se necessário |
| 74 | + # Caminho da imagem da splash screen |
| 75 | + splash_image = "splash.png" |
| 76 | + splash = CustomSplashScreen(splash_image) |
| 77 | + splash.show() |
18 | 78 |
|
19 | | - if os.path.exists(splash_image): |
20 | | - pixmap = QPixmap(splash_image) |
21 | | - else: |
22 | | - # Se a imagem não existir, cria um pixmap simples de 400x300 com fundo branco |
23 | | - pixmap = QPixmap(400, 300) |
24 | | - pixmap.fill(QColor("white")) |
| 79 | + # Definindo as etapas de carregamento: (porcentagem, mensagem) |
| 80 | + steps = [ |
| 81 | + (10, "Carregando módulos básicos..."), |
| 82 | + (30, "Inicializando a interface..."), |
| 83 | + (50, "Carregando plugins..."), |
| 84 | + (70, "Carregando recursos adicionais..."), |
| 85 | + (90, "Finalizando configuração..."), |
| 86 | + (100, "Pronto!") |
| 87 | + ] |
| 88 | + current_step = 0 |
| 89 | + progress = 0 |
| 90 | + timer = QTimer() |
25 | 91 |
|
26 | | - # Cria e exibe a splash screen |
27 | | - splash = QSplashScreen(pixmap, Qt.WindowStaysOnTopHint) |
28 | | - splash.showMessage("Carregando o aplicativo...", Qt.AlignBottom | Qt.AlignCenter, Qt.black) |
29 | | - splash.show() |
30 | | - app.processEvents() # Garante que a splash seja exibida imediatamente |
| 92 | + def update_loading(): |
| 93 | + nonlocal progress, current_step |
| 94 | + progress += 5 # Incrementa 5% a cada atualização (ajuste conforme necessário) |
| 95 | + if current_step < len(steps) and progress >= steps[current_step][0]: |
| 96 | + splash.update_progress(progress, steps[current_step][1]) |
| 97 | + current_step += 1 |
| 98 | + else: |
| 99 | + splash.update_progress(progress) |
| 100 | + |
| 101 | + if progress >= 100: |
| 102 | + timer.stop() |
| 103 | + # Animação de fade-out para a splash screen |
| 104 | + splash.fade_animation = QPropertyAnimation(splash, b"windowOpacity") |
| 105 | + splash.fade_animation.setDuration(500) # Duração do fade-out em milissegundos |
| 106 | + splash.fade_animation.setStartValue(1.0) |
| 107 | + splash.fade_animation.setEndValue(0.0) |
| 108 | + splash.fade_animation.start() |
| 109 | + splash.fade_animation.finished.connect(lambda: splash.close()) |
| 110 | + |
| 111 | + # Importa e exibe o aplicativo principal após a splash |
| 112 | + from calendar_widget import CalendarApp # Seu módulo principal |
| 113 | + window = CalendarApp() |
| 114 | + window.show() |
| 115 | + app.setStyle("Fusion") |
| 116 | + app.main_window = window |
31 | 117 |
|
32 | | - # Agora, importe e inicialize o aplicativo principal. |
33 | | - # Essa importação só ocorrerá depois que a splash já estiver visível. |
34 | | - from calendar_widget import CalendarApp # Seu módulo principal |
| 118 | + timer.timeout.connect(update_loading) |
| 119 | + timer.start(200) # Atualiza a cada 200 ms (ajuste conforme necessário) |
35 | 120 |
|
36 | | - window = CalendarApp() |
37 | | - window.show() |
38 | | - app.setStyle("Fusion") |
39 | | - # Fecha a splash screen assim que o aplicativo estiver pronto |
40 | | - splash.finish(window) |
41 | 121 | sys.exit(app.exec_()) |
42 | 122 |
|
43 | 123 | if __name__ == "__main__": |
|
0 commit comments