kwave  18.07.70
Splash.cpp
Go to the documentation of this file.
1 /*************************************************************************
2  Splash.cpp - splash screen for Kwave
3  -------------------
4  begin : Tue Jun 24 2003
5  copyright : Copyright (C) 2003 Gilles CAULIER
6  email : Gilles CAULIER <caulier.gilles@free.fr>
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "config.h"
19 
20 #include <QApplication>
21 #include <QDesktopWidget>
22 #include <QFont>
23 #include <QLabel>
24 #include <QPainter>
25 #include <QPixmap>
26 #include <QString>
27 #include <QThread>
28 
29 #include <KAboutData>
30 #include <KLocalizedString>
31 
32 #include <QStandardPaths>
33 
34 #include "libkwave/String.h"
35 
36 #include "Splash.h"
37 
38 // static pointer to the current instance
39 QPointer<Kwave::Splash> Kwave::Splash::m_splash = Q_NULLPTR;
40 
41 //***************************************************************************
42 Kwave::Splash::Splash(const QString &PNGFile)
43  :QFrame(Q_NULLPTR, Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint),
44  m_font(),
45  m_pixmap(QStandardPaths::locate(QStandardPaths::DataLocation, PNGFile)),
46  m_message(_(" "))
47 {
48  const int w = m_pixmap.width();
49  const int h = m_pixmap.height();
50  setFixedSize(w, h);
51 
52  QPainter p;
53  p.begin(&m_pixmap);
54 
55  // get all the strings that we should display
56  const KAboutData about_data = KAboutData::applicationData();
57  QString version = i18nc("%1=Version number", "v%1", about_data.version());
58 
59  m_font.setBold(true);
60  m_font.setStyleHint(QFont::Decorative);
61  m_font.setWeight(QFont::Black);
62  p.setFont(m_font);
63 
64  QFontMetrics fm(m_font);
65  QRect rect = fm.boundingRect(version);
66 
67  // version
68  const int r = 5;
69  const int th = rect.height();
70  const int tw = rect.width();
71  const int x = w - 10 - tw;
72  const int y = h - 10 - th;
73 
74  const QBrush brush(palette().background().color());
75  p.setBrush(brush);
76  p.setOpacity(0.50);
77  p.setPen(Qt::NoPen);
78  p.drawRoundRect(
79  x - r, y - r,
80  tw + 2 * r, th + (2 * r),
81  (200 * r) / th, (200 * r) / th
82  );
83 
84  p.setOpacity(1.0);
85  p.setPen(palette().buttonText().color());
86  p.drawText(x, y, tw, th, Qt::AlignCenter, version);
87 
88  p.end();
89 
90  // center on the screen
91  rect = QRect(QPoint(), m_pixmap.size());
92  resize(rect.size());
93  move(QApplication::desktop()->screenGeometry().center() - rect.center());
94 
95  m_splash = this;
96 }
97 
98 //***************************************************************************
100 {
101  // check: start() must be called from the GUI thread only!
102  Q_ASSERT(this->thread() == QThread::currentThread());
103  Q_ASSERT(this->thread() == qApp->thread());
104  m_splash = Q_NULLPTR;
105  hide();
106 }
107 
108 //***************************************************************************
110 {
111  m_splash = Q_NULLPTR;
112 }
113 
114 //***************************************************************************
115 void Kwave::Splash::showMessage(const QString &message)
116 {
117  if (!m_splash) return;
118  m_splash->m_message = message;
119  m_splash->repaint();
120  QApplication::processEvents(QEventLoop::AllEvents);
121 }
122 
123 //***************************************************************************
124 void Kwave::Splash::paintEvent(QPaintEvent *)
125 {
126  // special handling: a null message tells us to hide
127  if (!m_message.length()) {
128  m_splash = Q_NULLPTR;
129  hide();
130  return;
131  }
132 
133  QRect rect(this->rect());
134  const int border = 5;
135  rect.setRect(
136  rect.x() + border,
137  rect.y() + border,
138  rect.width() - (2 * border),
139  rect.height() - (2 * border)
140  );
141 
142  QPainter p(this);
143  p.drawPixmap(this->rect(), m_pixmap);
144  p.setFont(m_font);
145  p.setPen(palette().buttonText().color());
146  p.drawText(rect, Qt::AlignLeft | Qt::AlignTop, m_message);
147 }
148 
149 //***************************************************************************
151 {
152  done();
153 }
154 
155 //***************************************************************************
156 //***************************************************************************
QString m_message
Definition: Splash.h:70
Splash(const QString &PNGFile)
Definition: Splash.cpp:42
virtual ~Splash() Q_DECL_OVERRIDE
Definition: Splash.cpp:109
static void showMessage(const QString &message)
Definition: Splash.cpp:115
static QPointer< Kwave::Splash > m_splash
Definition: Splash.h:73
QPixmap m_pixmap
Definition: Splash.h:67
static const char * background[]
#define _(m)
Definition: memcpy.c:66
virtual void mousePressEvent(QMouseEvent *) Q_DECL_OVERRIDE
Definition: Splash.cpp:150
virtual void paintEvent(QPaintEvent *e) Q_DECL_OVERRIDE
Definition: Splash.cpp:124
void done()
Definition: Splash.cpp:99
QFont m_font
Definition: Splash.h:64
static double rect(double param)
Definition: Functions.cpp:29