game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
addgamedialog.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// qt/addgamedialog.cpp
4// ==============================================================================
5// Add/Edit game dialog implementation
6// ==============================================================================
7
9#include "theme_manager.h"
10
11#include <QDialogButtonBox>
12#include <QDir>
13#include <QFileDialog>
14#include <QFileInfo>
15#include <QFormLayout>
16#include <QGroupBox>
17#include <QHBoxLayout>
18#include <QMessageBox>
19#include <QTabWidget>
20#include <QVBoxLayout>
21
22// ==============================================================================
23// Styling Helper - Consistent Group Box Visual Treatment
24// ==============================================================================
25
26static void applyGroupStyle(QGroupBox* group, const ThemeColors& c, bool highlight = false)
27{
28 QString borderColor = highlight ? c.accent.name() : c.border.name();
29 QString titleColor = highlight ? c.accent.name() : c.textPrimary.name();
30
31 group->setStyleSheet(QString("QGroupBox {"
32 " font-weight: bold;"
33 " border: 1px solid %1;"
34 " border-radius: 6px;"
35 " margin-top: 12px;"
36 " padding-top: 8px;"
37 " color: %3;"
38 "}"
39 "QGroupBox::title {"
40 " subcontrol-origin: margin;"
41 " left: 12px;"
42 " padding: 0 6px;"
43 " color: %2;"
44 "}"
45 "QCheckBox { color: %3; spacing: 8px; }"
46 "QRadioButton { color: %3; spacing: 8px; }"
47 "QLabel { color: %3; }")
48 .arg(borderColor, titleColor, c.textPrimary.name()));
49}
50
51AddGameDialog::AddGameDialog(QWidget* parent, ThemeManager* themeManager)
52 : QDialog(parent), themeManager_(themeManager ? themeManager : &ThemeManager::instance())
53{
54 setupUI();
55 applyTheme();
56 setWindowTitle("Add Game");
57 resize(520, 480);
58
59 connect(themeManager_, &ThemeManager::themeChanged, this, &AddGameDialog::applyTheme);
60}
61
62void AddGameDialog::setupUI()
63{
64 QVBoxLayout* mainLayout = new QVBoxLayout(this);
65 mainLayout->setSpacing(12);
66
67 // Tab widget for different sections
68 tabWidget_ = new QTabWidget(this);
69
70 QWidget* basicTab = new QWidget();
71 QWidget* runnerTab = new QWidget();
72 QWidget* advancedTab = new QWidget();
73
74 setupBasicTab(basicTab);
75 setupRunnerTab(runnerTab);
76 setupAdvancedTab(advancedTab);
77
78 tabWidget_->addTab(basicTab, "Basic");
79 tabWidget_->addTab(runnerTab, "Runner");
80 tabWidget_->addTab(advancedTab, "Advanced");
81
82 mainLayout->addWidget(tabWidget_);
83
84 // Button box
85 QDialogButtonBox* buttonBox = new QDialogButtonBox(this);
86
87 autoDetectButton_ = new QPushButton("Auto-Detect");
88 buttonBox->addButton(autoDetectButton_, QDialogButtonBox::ActionRole);
89
90 okButton_ = buttonBox->addButton(QDialogButtonBox::Ok);
91 cancelButton_ = buttonBox->addButton(QDialogButtonBox::Cancel);
92
93 connect(autoDetectButton_, &QPushButton::clicked, this, &AddGameDialog::onAutoDetect);
94 connect(okButton_, &QPushButton::clicked, this, &AddGameDialog::onAccept);
95 connect(cancelButton_, &QPushButton::clicked, this, &QDialog::reject);
96
97 mainLayout->addWidget(buttonBox);
98}
99
100void AddGameDialog::setupBasicTab(QWidget* tab)
101{
102 QFormLayout* layout = new QFormLayout(tab);
103 layout->setSpacing(10);
104
105 // Name
106 nameEdit_ = new QLineEdit();
107 nameEdit_->setPlaceholderText("Game name");
108 layout->addRow("Name:", nameEdit_);
109
110 // Executable
111 QHBoxLayout* exeLayout = new QHBoxLayout();
112 executableEdit_ = new QLineEdit();
113 executableEdit_->setPlaceholderText("/path/to/game.exe or game");
114 QPushButton* browseExeBtn = new QPushButton("Browse...");
115 connect(browseExeBtn, &QPushButton::clicked, this, &AddGameDialog::onBrowseExecutable);
116 connect(executableEdit_, &QLineEdit::textChanged, this, &AddGameDialog::onExecutableChanged);
117 exeLayout->addWidget(executableEdit_);
118 exeLayout->addWidget(browseExeBtn);
119 layout->addRow("Executable:", exeLayout);
120
121 // Working Directory
122 QHBoxLayout* workDirLayout = new QHBoxLayout();
123 workingDirEdit_ = new QLineEdit();
124 workingDirEdit_->setPlaceholderText("Leave empty for executable's directory");
125 QPushButton* browseWorkDirBtn = new QPushButton("Browse...");
126 connect(browseWorkDirBtn, &QPushButton::clicked, this, &AddGameDialog::onBrowseWorkingDir);
127 workDirLayout->addWidget(workingDirEdit_);
128 workDirLayout->addWidget(browseWorkDirBtn);
129 layout->addRow("Working Dir:", workDirLayout);
130
131 // Arguments
132 argumentsEdit_ = new QLineEdit();
133 argumentsEdit_->setPlaceholderText("-fullscreen -nolog");
134 layout->addRow("Arguments:", argumentsEdit_);
135
136 // Source
137 sourceCombo_ = new QComboBox();
138 sourceCombo_->addItem("Local", static_cast<int>(GameSource::Local));
139 sourceCombo_->addItem("Steam", static_cast<int>(GameSource::Steam));
140 sourceCombo_->addItem("GOG", static_cast<int>(GameSource::GOG));
141 sourceCombo_->addItem("Epic", static_cast<int>(GameSource::Epic));
142 sourceCombo_->addItem("Lutris", static_cast<int>(GameSource::Lutris));
143 layout->addRow("Source:", sourceCombo_);
144
145 // Runner
146 runnerCombo_ = new QComboBox();
147 runnerCombo_->addItem("Native Linux", static_cast<int>(GameRunner::Native));
148 runnerCombo_->addItem("Wine", static_cast<int>(GameRunner::Wine));
149 runnerCombo_->addItem("Proton", static_cast<int>(GameRunner::Proton));
150 runnerCombo_->addItem("DOSBox", static_cast<int>(GameRunner::DOSBox));
151 runnerCombo_->addItem("ScummVM", static_cast<int>(GameRunner::ScummVM));
152 connect(runnerCombo_, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
153 &AddGameDialog::onRunnerChanged);
154 layout->addRow("Runner:", runnerCombo_);
155}
156
157void AddGameDialog::setupRunnerTab(QWidget* tab)
158{
159 QFormLayout* layout = new QFormLayout(tab);
160 layout->setSpacing(10);
161
162 // Wine Prefix
163 QHBoxLayout* prefixLayout = new QHBoxLayout();
164 winePrefixEdit_ = new QLineEdit();
165 winePrefixEdit_->setPlaceholderText("~/.wine or custom prefix path");
166 QPushButton* browsePrefixBtn = new QPushButton("Browse...");
167 connect(browsePrefixBtn, &QPushButton::clicked, this, &AddGameDialog::onBrowseWinePrefix);
168 prefixLayout->addWidget(winePrefixEdit_);
169 prefixLayout->addWidget(browsePrefixBtn);
170 layout->addRow("Wine Prefix:", prefixLayout);
171
172 // Proton Version
173 protonVersionCombo_ = new QComboBox();
174 protonVersionCombo_->setEditable(true);
175 protonVersionCombo_->addItem("(Auto-detect)");
176 protonVersionCombo_->addItem("proton-cachyos-slr");
177 protonVersionCombo_->addItem("GE-Proton");
178 protonVersionCombo_->addItem("Proton Experimental");
179 protonVersionCombo_->addItem("Proton 9.0");
180 protonVersionCombo_->addItem("Proton 8.0");
181 layout->addRow("Proton Version:", protonVersionCombo_);
182
183 // Virtual Desktop
184 vdGroup_ = new QGroupBox("Virtual Desktop");
185 QVBoxLayout* vdLayout = new QVBoxLayout(vdGroup_);
186
187 virtualDesktopCheck_ = new QCheckBox("Enable virtual desktop");
188 vdLayout->addWidget(virtualDesktopCheck_);
189
190 QHBoxLayout* resLayout = new QHBoxLayout();
191 resLayout->addWidget(new QLabel("Resolution:"));
192 desktopWidthSpin_ = new QSpinBox();
193 desktopWidthSpin_->setRange(640, 7680);
194 desktopWidthSpin_->setValue(1920);
195 resLayout->addWidget(desktopWidthSpin_);
196 resLayout->addWidget(new QLabel("x"));
197 desktopHeightSpin_ = new QSpinBox();
198 desktopHeightSpin_->setRange(480, 4320);
199 desktopHeightSpin_->setValue(1080);
200 resLayout->addWidget(desktopHeightSpin_);
201 resLayout->addStretch();
202 vdLayout->addLayout(resLayout);
203
204 layout->addRow(vdGroup_);
205
206 // Initially disable Wine/Proton options for native games
207 onRunnerChanged(runnerCombo_->currentIndex());
208}
209
210void AddGameDialog::setupAdvancedTab(QWidget* tab)
211{
212 QFormLayout* layout = new QFormLayout(tab);
213 layout->setSpacing(10);
214
215 // Cover Art
216 QHBoxLayout* coverLayout = new QHBoxLayout();
217 coverArtEdit_ = new QLineEdit();
218 coverArtEdit_->setPlaceholderText("Path to cover art image");
219 QPushButton* browseCoverBtn = new QPushButton("Browse...");
220 connect(browseCoverBtn, &QPushButton::clicked, this, &AddGameDialog::onBrowseCoverArt);
221 coverLayout->addWidget(coverArtEdit_);
222 coverLayout->addWidget(browseCoverBtn);
223 layout->addRow("Cover Art:", coverLayout);
224
225 // Description
226 descriptionEdit_ = new QLineEdit();
227 descriptionEdit_->setPlaceholderText("Game description (optional)");
228 layout->addRow("Description:", descriptionEdit_);
229}
230
231void AddGameDialog::onBrowseExecutable()
232{
233 QString filter = "All Files (*);;Executables (*.exe *.sh *.AppImage)";
234 QString path =
235 QFileDialog::getOpenFileName(this, "Select Game Executable", QDir::homePath(), filter);
236 if (!path.isEmpty())
237 {
238 executableEdit_->setText(path);
239 }
240}
241
242void AddGameDialog::onBrowseWorkingDir()
243{
244 QString path =
245 QFileDialog::getExistingDirectory(this, "Select Working Directory", QDir::homePath());
246 if (!path.isEmpty())
247 {
248 workingDirEdit_->setText(path);
249 }
250}
251
252void AddGameDialog::onBrowseWinePrefix()
253{
254 QString path = QFileDialog::getExistingDirectory(this, "Select Wine Prefix", QDir::homePath());
255 if (!path.isEmpty())
256 {
257 winePrefixEdit_->setText(path);
258 }
259}
260
261void AddGameDialog::onBrowseCoverArt()
262{
263 QString filter = "Images (*.png *.jpg *.jpeg *.webp);;All Files (*)";
264 QString path = QFileDialog::getOpenFileName(this, "Select Cover Art", QDir::homePath(), filter);
265 if (!path.isEmpty())
266 {
267 coverArtEdit_->setText(path);
268 }
269}
270
271void AddGameDialog::onExecutableChanged(const QString& path)
272{
273 if (path.isEmpty())
274 return;
275
276 // Auto-fill name from filename if name is empty
277 if (nameEdit_->text().isEmpty())
278 {
279 QFileInfo info(path);
280 QString name = info.completeBaseName();
281 // Clean up common suffixes
282 name.remove(QRegularExpression("[-_](x64|x86|win64|win32|launcher)$",
283 QRegularExpression::CaseInsensitiveOption));
284 nameEdit_->setText(name);
285 }
286
287 // Auto-detect runner from extension
288 if (path.endsWith(".exe", Qt::CaseInsensitive))
289 {
290 // Default to Proton for .exe files
291 int protonIndex = runnerCombo_->findData(static_cast<int>(GameRunner::Proton));
292 if (protonIndex >= 0)
293 {
294 runnerCombo_->setCurrentIndex(protonIndex);
295 }
296 }
297}
298
299void AddGameDialog::onRunnerChanged(int index)
300{
301 GameRunner runner = static_cast<GameRunner>(runnerCombo_->itemData(index).toInt());
302 bool isWineProton = (runner == GameRunner::Wine || runner == GameRunner::Proton);
303
304 winePrefixEdit_->setEnabled(isWineProton);
305 protonVersionCombo_->setEnabled(runner == GameRunner::Proton);
306 virtualDesktopCheck_->setEnabled(isWineProton);
307 desktopWidthSpin_->setEnabled(isWineProton && virtualDesktopCheck_->isChecked());
308 desktopHeightSpin_->setEnabled(isWineProton && virtualDesktopCheck_->isChecked());
309}
310
311void AddGameDialog::onAutoDetect()
312{
313 QString path = executableEdit_->text();
314 if (path.isEmpty())
315 {
316 QMessageBox::information(this, "Auto-Detect", "Please select an executable first.");
317 return;
318 }
319
320 autoDetectFromPath(path);
321}
322
323void AddGameDialog::autoDetectFromPath(const QString& path)
324{
325 QFileInfo info(path);
326
327 // Try to detect Wine prefix
328 QString dir = info.absolutePath();
329 QString prefix;
330
331 // Walk up looking for drive_c
332 QDir current(dir);
333 for (int i = 0; i < 10; ++i)
334 {
335 if (QDir(current.filePath("drive_c")).exists())
336 {
337 prefix = current.absolutePath();
338 break;
339 }
340 if (!current.cdUp())
341 break;
342 }
343
344 if (!prefix.isEmpty())
345 {
346 winePrefixEdit_->setText(prefix);
347 }
348
349 // Auto-fill working directory
350 if (workingDirEdit_->text().isEmpty())
351 {
352 workingDirEdit_->setText(info.absolutePath());
353 }
354}
355
356void AddGameDialog::onAccept()
357{
358 // Validate
359 if (nameEdit_->text().trimmed().isEmpty())
360 {
361 QMessageBox::warning(this, "Validation Error", "Please enter a game name.");
362 nameEdit_->setFocus();
363 return;
364 }
365
366 if (executableEdit_->text().trimmed().isEmpty())
367 {
368 QMessageBox::warning(this, "Validation Error", "Please select an executable.");
369 executableEdit_->setFocus();
370 return;
371 }
372
373 updateGameFromUI();
374 accept();
375}
376
378{
379 game_ = game;
380 updateUIFromGame();
381}
382
384{
385 editMode_ = editing;
386 setWindowTitle(editing ? "Edit Game" : "Add Game");
387 autoDetectButton_->setVisible(!editing);
388}
389
391{
392 return game_;
393}
394
395void AddGameDialog::updateUIFromGame()
396{
397 nameEdit_->setText(QString::fromStdString(game_.name));
398 executableEdit_->setText(QString::fromStdString(game_.runnerConfig.executablePath));
399 workingDirEdit_->setText(QString::fromStdString(game_.runnerConfig.workingDirectory));
400 argumentsEdit_->setText(QString::fromStdString(game_.runnerConfig.arguments));
401
402 // Source
403 int sourceIndex = sourceCombo_->findData(static_cast<int>(game_.source));
404 if (sourceIndex >= 0)
405 sourceCombo_->setCurrentIndex(sourceIndex);
406
407 // Runner
408 int runnerIndex = runnerCombo_->findData(static_cast<int>(game_.runner));
409 if (runnerIndex >= 0)
410 runnerCombo_->setCurrentIndex(runnerIndex);
411
412 // Wine/Proton settings
413 winePrefixEdit_->setText(QString::fromStdString(game_.runnerConfig.winePrefix));
414 protonVersionCombo_->setCurrentText(QString::fromStdString(game_.runnerConfig.protonVersion));
415
416 // Virtual desktop
417 virtualDesktopCheck_->setChecked(game_.runnerConfig.useVirtualDesktop);
418 desktopWidthSpin_->setValue(game_.runnerConfig.virtualDesktopWidth);
419 desktopHeightSpin_->setValue(game_.runnerConfig.virtualDesktopHeight);
420
421 // Advanced
422 coverArtEdit_->setText(QString::fromStdString(game_.metadata.coverArtPath));
423 descriptionEdit_->setText(QString::fromStdString(game_.metadata.description));
424}
425
426void AddGameDialog::updateGameFromUI()
427{
428 game_.name = nameEdit_->text().trimmed().toStdString();
429 game_.runnerConfig.executablePath = executableEdit_->text().trimmed().toStdString();
430 game_.runnerConfig.workingDirectory = workingDirEdit_->text().trimmed().toStdString();
431 game_.runnerConfig.arguments = argumentsEdit_->text().trimmed().toStdString();
432
433 game_.source = static_cast<GameSource>(sourceCombo_->currentData().toInt());
434 game_.runner = static_cast<GameRunner>(runnerCombo_->currentData().toInt());
435
436 // Wine/Proton
437 game_.runnerConfig.winePrefix = winePrefixEdit_->text().trimmed().toStdString();
438 QString protonVersion = protonVersionCombo_->currentText().trimmed();
439 if (protonVersion != "(Auto-detect)")
440 {
441 game_.runnerConfig.protonVersion = protonVersion.toStdString();
442 }
443
444 game_.runnerConfig.useVirtualDesktop = virtualDesktopCheck_->isChecked();
445 game_.runnerConfig.virtualDesktopWidth = desktopWidthSpin_->value();
446 game_.runnerConfig.virtualDesktopHeight = desktopHeightSpin_->value();
447
448 // Advanced
449 game_.metadata.coverArtPath = coverArtEdit_->text().trimmed().toStdString();
450 game_.metadata.description = descriptionEdit_->text().trimmed().toStdString();
451
452 // Set status based on executable existence
453 QFileInfo exeInfo(QString::fromStdString(game_.runnerConfig.executablePath));
454 game_.status = exeInfo.exists() ? GameStatus::Installed : GameStatus::NotInstalled;
455
456 // Generate ID if new game
457 if (game_.id.empty())
458 {
459 game_.id = std::to_string(std::time(nullptr)) + "_" +
460 std::to_string(std::hash<std::string>{}(game_.name));
461 }
462
463 // Set date added
464 if (game_.statistics.dateAdded == 0)
465 {
466 game_.statistics.dateAdded = std::time(nullptr);
467 }
468}
469
470// ==============================================================================
471// Theme Support
472// ==============================================================================
473
474void AddGameDialog::applyTheme()
475{
476 const auto& c = themeManager_->colors();
477
478 // Style tab widget pane — let global stylesheet handle tab bar styling
479 if (tabWidget_)
480 {
481 tabWidget_->setStyleSheet(QString("QTabWidget::pane {"
482 " border: 1px solid %1;"
483 " border-top: none;"
484 " background: %2;"
485 "}")
486 .arg(c.borderLight.name(), c.panelBackground.name()));
487 }
488
489 // Style group boxes
490 if (vdGroup_)
491 {
492 applyGroupStyle(vdGroup_, c, false);
493 }
494
495 // Style primary action button
496 if (okButton_)
497 {
498 okButton_->setStyleSheet(
499 QString("QPushButton {"
500 " background-color: %1;"
501 " color: %2;"
502 " border: none;"
503 " border-radius: 4px;"
504 " padding: 6px 16px;"
505 " font-weight: bold;"
506 "}"
507 "QPushButton:hover { background-color: %3; }")
508 .arg(c.accent.name(), c.textOnAccent.name(), c.accentHover.name()));
509 }
510
511 // Style auto-detect button with accent outline
512 if (autoDetectButton_)
513 {
514 autoDetectButton_->setStyleSheet(QString("QPushButton {"
515 " background-color: transparent;"
516 " color: %1;"
517 " border: 1px solid %1;"
518 " border-radius: 4px;"
519 " padding: 6px 16px;"
520 "}"
521 "QPushButton:hover { background-color: %2; }")
522 .arg(c.accent.name(), c.hoverBackground.name()));
523 }
524}
static void applyGroupStyle(QGroupBox *group, const ThemeColors &c, bool highlight=false)
Game getGame() const
void setGame(const Game &game)
AddGameDialog(QWidget *parent=nullptr, ThemeManager *themeManager=nullptr)
void setEditMode(bool editing)
Complete representation of a game in the library.
Definition game.h:767
GameMetadata metadata
Descriptive information and media.
Definition game.h:785
std::string name
Display name shown in UI.
Definition game.h:774
GameRunner runner
Runner/compatibility layer to use (Native, Proton, Wine, etc.).
Definition game.h:777
GameRunnerConfig runnerConfig
How to launch and run the game.
Definition game.h:784
GameSource source
Where the game was acquired from (Steam, GOG, Local, etc.).
Definition game.h:775
void themeChanged()
Emitted when theme or accent color changes.
GameSource
Where the game was acquired from (store/service/local).
Definition game.h:39
@ Steam
Steam store/platform.
Definition game.h:41
@ Local
Manually added game (not from a store).
Definition game.h:40
@ Lutris
Lutris community scripts.
Definition game.h:45
@ GOG
GOG.com (DRM-free games).
Definition game.h:42
@ Epic
Epic Games Store.
Definition game.h:43
@ NotInstalled
Game is in library but not installed.
Definition game.h:98
@ Installed
Game is installed and ready to play.
Definition game.h:99
GameRunner
Runner/compatibility layer for executing the game.
Definition game.h:81
@ ScummVM
ScummVM engine.
Definition game.h:86
@ Proton
Proton (Steam's Wine fork).
Definition game.h:84
@ Native
Native Linux executable (no compatibility layer).
Definition game.h:82
@ Wine
Wine compatibility layer.
Definition game.h:83
@ DOSBox
DOSBox emulator.
Definition game.h:85
std::string coverArtPath
Path to cover art image file (DEPRECATED - use imagePaths.verticalCover).
Definition game.h:559
std::string description
Game description text.
Definition game.h:565
std::string executablePath
Full path to game executable.
Definition game.h:141
int virtualDesktopWidth
Virtual desktop width in pixels.
Definition game.h:177
std::string workingDirectory
Working directory for game execution.
Definition game.h:142
bool useVirtualDesktop
Launch game in Wine virtual desktop.
Definition game.h:176
std::string arguments
Command line arguments to pass to game.
Definition game.h:143
std::string winePrefix
WINEPREFIX path (Wine install location).
Definition game.h:167
int virtualDesktopHeight
Virtual desktop height in pixels.
Definition game.h:178
std::string protonVersion
Proton version (e.g., "proton-cachyos-slr").
Definition game.h:170
QColor textPrimary