gsplash
Loading...
Searching...
No Matches
gsplash.cc
Go to the documentation of this file.
1// splash
2#include "gsplash.h"
3using std::string;
4
5// Factory creation:
6// - Returns nullptr when GUI is disabled (headless runs).
7// - Otherwise constructs GSplash and attempts to load the requested image.
8std::unique_ptr<GSplash> GSplash::create(
9 const std::shared_ptr<GOptions>& gopts,
10 const string& img) {
11 if (!gopts || !gopts->getSwitch("gui"))
12 return nullptr; // head-less run → no splash
13 return std::unique_ptr<GSplash>(new GSplash(gopts, img));
14}
15
16// Constructor summary:
17// - Resolves the image either from GSPLASH env var (special token) or from a provided name/path.
18// - Tries filesystem path first, then Qt resource lookup.
19// - Creates and shows QSplashScreen only when a valid pixmap is available.
20GSplash::GSplash(const std::shared_ptr<GOptions>& gopts, const string& imageName)
21 : GBase(gopts, GSPLASH_LOGGER) {
22 QPixmap pixmap;
23
24 // If no explicit image is selected, load it from the GSPLASH environment variable.
25 if (imageName == NOSPLASHIMAGESELECTED) {
26 if (const char* filename = std::getenv(GSPLASHENVIRONMENT); filename) {
27 pixmap.load(filename); // loads or leaves null
28 }
29 else {
31 "Environment variable ", GSPLASHENVIRONMENT,
32 " must point to an image file.");
33 }
34 }
35 else {
36 // Try filesystem path first (e.g. "example.png"), then Qt resource (":/example.png")
37 if (!pixmap.load(QString::fromStdString(imageName))) {
38 pixmap.load(QString(":/%1").arg(QString::fromStdString(imageName)));
39 }
40
41 if (pixmap.isNull())
42 log->error(ERR_NOSPLASHENVFOUND, "Image ", imageName, " not found.");
43 }
44
45 // Create the splash only when we have a valid pixmap; otherwise leave it inactive.
46 if (!pixmap.isNull()) {
47 splash = std::make_unique<QSplashScreen>(pixmap);
48 splash->show();
49 QCoreApplication::processEvents(QEventLoop::AllEvents, 50);
50 }
51}
52
53
54void GSplash::messageAfter(int delay_ms, const std::string& msg) {
55 if (!splash) return;
56
57 // Guard against the splash being destroyed before the timer fires.
58 QPointer<QSplashScreen> sp = splash.get();
59 QTimer::singleShot(delay_ms, sp, [sp, qmsg = QString::fromStdString(msg)] {
60 if (!sp) return;
61 sp->showMessage(qmsg, Qt::AlignLeft, Qt::black);
62 QCoreApplication::processEvents(QEventLoop::AllEvents, 50);
63 });
64}
65
66
67void GSplash::message(const std::string& msg) {
68 if (!splash) return;
69
70 splash->showMessage(QString::fromStdString(msg), Qt::AlignLeft, Qt::black);
71 QCoreApplication::processEvents(QEventLoop::AllEvents, 50);
72}
std::shared_ptr< GLogger > log
void error(int exit_code, Args &&... args) const
Splash screen utility for GUI runs.
Definition gsplash.h:65
void message(const std::string &msg)
Displays a message on the splash screen immediately.
Definition gsplash.cc:67
static std::unique_ptr< GSplash > create(const std::shared_ptr< GOptions > &gopts, const std::string &imageName="gemcArchitecture")
Factory method for creating a GSplash instance.
Definition gsplash.cc:8
void messageAfter(int delay, const std::string &msg)
Displays a message on the splash screen after a delay.
Definition gsplash.cc:54
#define GSPLASHENVIRONMENT
Definition gsplash.h:10
#define NOSPLASHIMAGESELECTED
Definition gsplash.h:11
constexpr const char * GSPLASH_LOGGER
Default logger name used by this module.
Definition gsplash.h:21
#define ERR_NOSPLASHENVFOUND
Definition gsplash.h:14