Skip to content

Commit

Permalink
parse cmd args before creating the application
Browse files Browse the repository at this point in the history
* postpone app-init (relevant for in-built options or invalid args)
* removing the need to handle in-built options
* moving application init completely to createApplication
  • Loading branch information
GitMensch committed Nov 2, 2023
1 parent 293a17b commit 6ff0193
Showing 1 changed file with 37 additions and 19 deletions.
56 changes: 37 additions & 19 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,48 @@ void Q_DECL_UNUSED initRCCIconTheme()
}
#endif

std::unique_ptr<QCoreApplication> createApplication(int& argc, char* argv[])
QStringList getApplicationArgs(int& argc, char* argv[])
{
const std::initializer_list<std::string_view> nonGUIOptions = {"--version", "-v", "--exportTo",
"--help", "-h", "--help-all"};
QStringList arguments;
for (int i = 0; i < argc; ++i) {
arguments << QString::fromUtf8(argv[i]);
}
return arguments;
}

/**
* @brief Create a Qt application instance based on command-line options.
*
* This function creates a Qt application instance based on the provided `argc` and `argv`.
* If the `cmdlineOnly` flag is set to true, a command-line application (QCoreApplication) is created,
* and the `window` pointer is set to nullptr. If `cmdlineOnly` is false, a full GUI application
* (QApplication) is created, and the `window` pointer is set to a new MainWindow instance.
*
* If applicable, this function also configures some attributes for the application.
*
* @param argc The number of command-line arguments.
* @param argv An array of C-style strings representing the command-line arguments.
* @param cmdlineOnly If true, create a command-line application; otherwise, create a GUI application.
* @param window A pointer to a MainWindow instance, which is set to nullptr in command-line mode.
* @return A std::unique_ptr to the created QCoreApplication or QApplication.
*/
std::unique_ptr<QCoreApplication> createApplication(int& argc, char* argv[], bool cmdlineOnly, MainWindow** window)
{
// create command line app if one of the command-line only options are used
for (int i = 1; i < argc; ++i) {
if (std::find(nonGUIOptions.begin(), nonGUIOptions.end(), argv[i]) != nonGUIOptions.end()) {
return std::make_unique<QCoreApplication>(argc, argv);
}
if (cmdlineOnly) {
*window = nullptr;
return std::make_unique<QCoreApplication>(argc, argv);
}

// otherwise create full gui app
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps, true);
#endif
return std::make_unique<QApplication>(argc, argv);
auto app = std::make_unique<QApplication>(argc, argv);
QGuiApplication::setWindowIcon(QIcon(QStringLiteral(":/images/icons/512-hotspot_app_icon.png")));
setupDockWidgets();
*window = new MainWindow();
return app;
}

int main(int argc, char** argv)
Expand All @@ -84,8 +109,6 @@ int main(int argc, char** argv)
QCoreApplication::setApplicationName(QStringLiteral("hotspot"));
QCoreApplication::setApplicationVersion(QStringLiteral(HOTSPOT_VERSION_STRING));

auto app = createApplication(argc, argv);

// init
Util::appImageEnvironment();

Expand Down Expand Up @@ -161,7 +184,10 @@ int main(int argc, char** argv)
QCoreApplication::translate("main", "Optional input files to open on startup, i.e. perf.data files."),
QStringLiteral("[files...]"));

parser.process(*app);
parser.process(getApplicationArgs(argc, argv));

MainWindow* window;
auto app = createApplication(argc, argv, parser.isSet(exportTo), &window);

ThreadWeaver::Queue::instance()->setMaximumNumberOfThreads(QThread::idealThreadCount());

Expand Down Expand Up @@ -200,14 +226,6 @@ int main(int argc, char** argv)
return 1;
}

auto* guiApp = qobject_cast<QApplication*>(app.get());
MainWindow* window = nullptr;
if (guiApp) {
QGuiApplication::setWindowIcon(QIcon(QStringLiteral(":/images/icons/512-hotspot_app_icon.png")));
setupDockWidgets();
window = new MainWindow();
}

const auto originalArguments = QCoreApplication::arguments();
// remove leading executable name and trailing positional arguments
const auto minimalArguments = originalArguments.mid(1, originalArguments.size() - 1 - files.size());
Expand Down

0 comments on commit 6ff0193

Please sign in to comment.