game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
sidebar_manager.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// ui/qt/sidebar_manager.cpp
4// ==============================================================================
5// Sidebar widget tree owner. Originally extracted from MainWindow
6// (2026-04-19, see memory/toolbar_extraction_2026_04.md for context), then
7// migrated from QListWidget to QListView + QAbstractListModel in the Qt 6
8// modernization pass later the same day (see
9// memory/qt_modernization_audit_2026_04.md).
10// ==============================================================================
11
13
18#include "models/game.h"
21#include "theme_manager.h"
24
25#include <QAbstractScrollArea>
26#include <QAction>
27#include <QColor>
28#include <QFrame>
29#include <QHBoxLayout>
30#include <QInputDialog>
31#include <QItemSelectionModel>
32#include <QListView>
33#include <QMenu>
34#include <QMessageBox>
35#include <QPalette>
36#include <QPushButton>
37#include <QScrollArea>
38#include <QVBoxLayout>
39#include <QWidget>
40
41#include <algorithm>
42#include <memory>
43#include <string>
44#include <vector>
45
46namespace
47{
48// GameLibraryController::getAllGames() returns std::vector<Game*>, but the
49// sidebar models take std::vector<const Game*> (read-only view). Convert
50// at the call site — a few tens of pointer-copies per sidebar refresh.
51std::vector<const Game*> toConstPtrs(const std::vector<Game*>& games)
52{
53 std::vector<const Game*> out;
54 out.reserve(games.size());
55 for (Game* g : games)
56 out.push_back(g);
57 return out;
58}
59} // namespace
60
61// ==============================================================================
62// Impl
63// ==============================================================================
64
66{
67 public:
69 core::LaunchController* launchCtrl, core::SettingsController* settingsCtrl,
70 QtNotificationService* notifSvc, QWidget* parentWidget, ThemeManager* themeMgr)
71 : q(owner), libraryController(libCtrl), launchController(launchCtrl),
72 settingsController(settingsCtrl), notificationService(notifSvc), parent(parentWidget),
73 themeManager(themeMgr)
74 {
75 }
76
77 SidebarManager* q = nullptr;
82 QWidget* parent = nullptr;
84
85 QWidget* sidebar = nullptr;
86
87 // Views + models. Each view is owned by the Qt parent chain; each model
88 // is a child of the manager (we give them q as parent in setupSidebar
89 // so destruction order is deterministic).
90 QListView* categoryView = nullptr;
91 QListView* sourceView = nullptr;
92 QListView* runnerView = nullptr;
93 QListView* collectionsView = nullptr;
94
99
100 // Build the per-list stylesheet. Used by setupSidebar and applyTheme.
101 // The same sheet applies to QListView as to QListWidget (both inherit
102 // QAbstractItemView selectors like QListView::item).
103 QString buildSidebarListStyle() const
104 {
105 const ThemeColors& c = themeManager->colors();
106 QColor selectedBg = c.accent;
107 selectedBg.setAlpha(30);
108 QColor selectedHoverBg = c.accent;
109 selectedHoverBg.setAlpha(45);
110 return QString("QListView { background-color: transparent; border: none; outline: none; }"
111 "QListView::item { padding: 7px 12px; border-radius: 4px; margin: 1px 6px; "
112 "color: %1; }"
113 "QListView::item:hover { background-color: %2; color: %3; }"
114 "QListView::item:selected { background-color: %4; color: %5; "
115 " border-left: 3px solid %5; font-weight: bold; }"
116 "QListView::item:selected:hover { background-color: %6; color: %5; "
117 " border-left: 3px solid %5; }")
118 .arg(c.textSecondary.name())
119 .arg(c.hoverBackground.name())
120 .arg(c.textPrimary.name())
121 .arg(selectedBg.name(QColor::HexArgb))
122 .arg(c.accent.name())
123 .arg(selectedHoverBg.name(QColor::HexArgb));
124 }
125
126 QListView* makeListView(const QString& styleSheet)
127 {
128 auto* view = new QListView();
129 view->setStyleSheet(styleSheet);
130 view->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
131 view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
132 view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
133 view->setFrameShape(QFrame::NoFrame);
134 view->setIconSize(QSize(16, 16));
135 view->setUniformItemSizes(true);
136 view->setSelectionMode(QAbstractItemView::SingleSelection);
137 view->setEditTriggers(QAbstractItemView::NoEditTriggers);
138 return view;
139 }
140};
141
142// ==============================================================================
143// Ctor / dtor
144// ==============================================================================
145
147 core::LaunchController* launchController,
148 core::SettingsController* settingsController,
149 QtNotificationService* notificationService,
150 ThemeManager* themeManager, QWidget* parent, QObject* qparent)
151 : QObject(qparent),
152 impl_(std::make_unique<Impl>(this, libraryController, launchController, settingsController,
153 notificationService, parent, themeManager))
154{
155}
156
158
159// ==============================================================================
160// setupSidebar
161// ==============================================================================
162
164{
165 auto& I = *impl_;
166 const ThemeColors& c = impl_->themeManager->colors();
167
168 // Models — parented to `this` (the manager). QListView::setModel does
169 // not take ownership, so we need explicit parenting for deterministic
170 // destruction. ThemeManager is injected (singleton today, replaceable
171 // in tests).
172 ThemeManager* themeMgr = impl_->themeManager;
173 I.categoryModel = new CategoryModel(themeMgr, this);
174 I.sourceModel = new SourceFilterModel(themeMgr, this);
175 I.runnerModel = new RunnerFilterModel(themeMgr, this);
176 I.collectionsModel = new CollectionsModel(themeMgr, this);
177
178 I.sidebar = new QWidget(I.parent);
179 // Palette-backed (windowBackground = QPalette::Window via
180 // ThemeManager::applyToApplication).
181 I.sidebar->setAutoFillBackground(true);
182 I.sidebar->setBackgroundRole(QPalette::Window);
183 auto* sidebarLayout = new QVBoxLayout(I.sidebar);
184 sidebarLayout->setContentsMargins(0, 0, 0, 0);
185 sidebarLayout->setSpacing(0);
186
187 auto* scrollArea = new QScrollArea(I.sidebar);
188 scrollArea->setWidgetResizable(true);
189 scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
190 scrollArea->setFrameShape(QFrame::NoFrame);
191 scrollArea->setStyleSheet("QScrollArea { background-color: transparent; }");
192
193 auto* scrollContent = new QWidget();
194 // Palette-backed.
195 scrollContent->setAutoFillBackground(true);
196 scrollContent->setBackgroundRole(QPalette::Window);
197 auto* sectionsLayout = new QVBoxLayout(scrollContent);
198 sectionsLayout->setContentsMargins(0, 0, 0, 0);
199 sectionsLayout->setSpacing(0);
200
201 const QString sidebarListStyle = I.buildSidebarListStyle();
202
203 // ------------------------------------------------------------------------
204 // Library section
205 // ------------------------------------------------------------------------
206 auto* librarySection =
207 new CollapsibleSection("Library", "sidebar/library_collapsed", false, scrollContent);
208
209 I.categoryView = I.makeListView(sidebarListStyle);
210 I.categoryView->setModel(I.categoryModel);
211 I.categoryView->setCurrentIndex(I.categoryModel->index(0));
212
213 connect(I.categoryView->selectionModel(), &QItemSelectionModel::currentRowChanged, this,
214 [this](const QModelIndex& current, const QModelIndex&)
215 { emit categoryChanged(current.row()); });
216
217 librarySection->addWidget(I.categoryView);
218 sectionsLayout->addWidget(librarySection);
219 connect(impl_->themeManager, &ThemeManager::themeChanged, librarySection,
221 connect(impl_->themeManager, &ThemeManager::themeChanged, this,
222 [this]() { impl_->categoryModel->rebuildIcons(); });
223
224 // ------------------------------------------------------------------------
225 // Sources section
226 // ------------------------------------------------------------------------
227 auto* sourceSection =
228 new CollapsibleSection("Sources", "sidebar/sources_collapsed", false, scrollContent);
229
230 I.sourceView = I.makeListView(sidebarListStyle);
231 I.sourceView->setModel(I.sourceModel);
232
233 connect(I.sourceView->selectionModel(), &QItemSelectionModel::currentRowChanged, this,
234 [this](const QModelIndex&, const QModelIndex&) { emit sourceChanged(); });
235
236 sourceSection->addWidget(I.sourceView);
237 sectionsLayout->addWidget(sourceSection);
238 connect(impl_->themeManager, &ThemeManager::themeChanged, sourceSection,
240
241 // ------------------------------------------------------------------------
242 // Runner section (collapsed by default)
243 // ------------------------------------------------------------------------
244 auto* runnerSection =
245 new CollapsibleSection("Runner", "sidebar/runner_collapsed", true, scrollContent);
246
247 I.runnerView = I.makeListView(sidebarListStyle);
248 I.runnerView->setModel(I.runnerModel);
249
250 // Populate the filter models before any selection restore logic runs.
254
255 // Select "All" for the runner filter after the model is populated.
256 if (I.runnerModel->rowCount() > 0)
257 I.runnerView->setCurrentIndex(I.runnerModel->index(0));
258
259 connect(I.runnerView->selectionModel(), &QItemSelectionModel::currentRowChanged, this,
260 [this](const QModelIndex&, const QModelIndex&) { emit runnerChanged(); });
261
262 runnerSection->addWidget(I.runnerView);
263 sectionsLayout->addWidget(runnerSection);
264 connect(impl_->themeManager, &ThemeManager::themeChanged, runnerSection,
266
267 // ------------------------------------------------------------------------
268 // Collections section
269 // ------------------------------------------------------------------------
270 auto* collectionsSection = new CollapsibleSection(
271 "Collections", "sidebar/collections_collapsed", false, scrollContent);
272
273 auto* collectionsToolbar = new QWidget();
274 auto* collectionsToolbarLayout = new QHBoxLayout(collectionsToolbar);
275 collectionsToolbarLayout->setContentsMargins(10, 2, 10, 2);
276 collectionsToolbarLayout->setSpacing(4);
277 collectionsToolbarLayout->addStretch();
278
279 auto* addCollectionBtn = new QPushButton(QStringLiteral("+"));
280 addCollectionBtn->setFixedSize(20, 20);
281 addCollectionBtn->setToolTip(QStringLiteral("Create new collection"));
282 addCollectionBtn->setCursor(Qt::PointingHandCursor);
283 addCollectionBtn->setStyleSheet(
284 QString("QPushButton { background-color: %1; color: %2; border: 1px solid %3; "
285 "border-radius: 4px; font-weight: bold; }"
286 "QPushButton:hover { background-color: %4; }")
287 .arg(c.inputBackground.name())
288 .arg(c.textPrimary.name())
289 .arg(c.border.name())
290 .arg(c.accent.name()));
291 connect(addCollectionBtn, &QPushButton::clicked, this,
292 [this]() { showCreateCollectionDialog(impl_->parent); });
293 collectionsToolbarLayout->addWidget(addCollectionBtn);
294
295 collectionsSection->addWidget(collectionsToolbar);
296
297 I.collectionsView = I.makeListView(sidebarListStyle);
298 I.collectionsView->setModel(I.collectionsModel);
299 I.collectionsView->setContextMenuPolicy(Qt::CustomContextMenu);
300
301 connect(I.collectionsView, &QListView::customContextMenuRequested, this,
302 [this](const QPoint& pos)
303 {
304 auto& II = *impl_;
305 QModelIndex idx = II.collectionsView->indexAt(pos);
306 if (!idx.isValid())
307 return;
308 QString collectionName = II.collectionsModel->data(idx, Qt::UserRole).toString();
309 if (collectionName.isEmpty())
310 return; // "All" row — no delete action
311
312 QMenu contextMenu(II.parent);
313 impl_->themeManager->styleMenu(&contextMenu);
314 QAction* deleteAction = contextMenu.addAction(QStringLiteral("Delete Collection"));
315 connect(
316 deleteAction, &QAction::triggered, this,
317 [this, collectionName]()
318 {
319 auto& III = *impl_;
320 auto reply =
321 QMessageBox::question(III.parent, QStringLiteral("Delete Collection"),
322 QString("Are you sure you want to delete the "
323 "collection '%1'?\n\nGames will not be "
324 "deleted, only the collection.")
325 .arg(collectionName),
326 QMessageBox::Yes | QMessageBox::No);
327 if (reply == QMessageBox::Yes)
328 {
329 III.libraryController->removeCollection(collectionName.toStdString());
330 updateCollectionsList();
331 emit collectionDeleted(collectionName);
332 }
333 });
334 contextMenu.exec(II.collectionsView->mapToGlobal(pos));
335 });
336
337 connect(I.collectionsView->selectionModel(), &QItemSelectionModel::currentRowChanged, this,
338 [this](const QModelIndex& current, const QModelIndex&)
339 {
340 if (!current.isValid())
341 {
342 emit collectionChanged(QString());
343 return;
344 }
345 emit collectionChanged(
346 impl_->collectionsModel->data(current, Qt::UserRole).toString());
347 });
348
349 updateCollectionsList();
350 collectionsSection->addWidget(I.collectionsView);
351 sectionsLayout->addWidget(collectionsSection);
352 connect(impl_->themeManager, &ThemeManager::themeChanged, collectionsSection,
354
355 sectionsLayout->addStretch();
356 scrollArea->setWidget(scrollContent);
357 sidebarLayout->addWidget(scrollArea);
358}
359
360// ==============================================================================
361// applyTheme
362// ==============================================================================
363
365{
366 auto& I = *impl_;
367 const ThemeColors& c = impl_->themeManager->colors();
368 if (I.sidebar)
369 {
370 // Palette already set in setupSidebar; nothing to do here.
371 (void)c;
372 }
373
374 const QString sidebarListStyle = I.buildSidebarListStyle();
375 if (I.categoryView)
376 I.categoryView->setStyleSheet(sidebarListStyle);
377 if (I.sourceView)
378 I.sourceView->setStyleSheet(sidebarListStyle);
379 if (I.runnerView)
380 I.runnerView->setStyleSheet(sidebarListStyle);
381 if (I.collectionsView)
382 I.collectionsView->setStyleSheet(sidebarListStyle);
383
384 if (I.sidebar)
385 {
386 for (QFrame* frame : I.sidebar->findChildren<QFrame*>())
387 {
388 if (frame->frameShape() == QFrame::HLine)
389 {
390 frame->setStyleSheet(QString("background-color: %1; max-height: 1px; "
391 "margin: 8px 4px;")
392 .arg(c.border.name()));
393 }
394 }
395 }
396}
397
398// ==============================================================================
399// Accessors + actions
400// ==============================================================================
401
403{
404 return impl_->sidebar;
405}
406
408{
409 auto& I = *impl_;
410 if (!I.categoryView)
411 return -1;
412 const QModelIndex idx = I.categoryView->currentIndex();
413 return idx.isValid() ? idx.row() : -1;
414}
415
417{
418 auto& I = *impl_;
419 if (!I.sourceView || !I.sourceModel)
420 return -1;
421 const QModelIndex idx = I.sourceView->currentIndex();
422 if (!idx.isValid())
423 return -1;
424 return I.sourceModel->data(idx, Qt::UserRole).toInt();
425}
426
428{
429 auto& I = *impl_;
430 if (!I.runnerView || !I.runnerModel)
431 return {};
432 const QModelIndex idx = I.runnerView->currentIndex();
433 if (!idx.isValid())
434 return {};
435 return I.runnerModel->data(idx, Qt::UserRole).toString();
436}
437
439{
440 auto& I = *impl_;
441 if (!I.collectionsView || !I.collectionsModel)
442 return {};
443 const QModelIndex idx = I.collectionsView->currentIndex();
444 if (!idx.isValid())
445 return {};
446 return I.collectionsModel->data(idx, Qt::UserRole).toString();
447}
448
450{
451 auto& I = *impl_;
452 if (!I.categoryView || !I.categoryModel)
453 return;
454 if (row < 0 || row >= I.categoryModel->rowCount())
455 return;
456 I.categoryView->setCurrentIndex(I.categoryModel->index(row));
457}
458
459void SidebarManager::selectCategoryByText(const QString& partialText)
460{
461 auto& I = *impl_;
462 if (!I.categoryView || !I.categoryModel)
463 return;
464 const int row = I.categoryModel->rowContaining(partialText);
465 if (row >= 0)
466 I.categoryView->setCurrentIndex(I.categoryModel->index(row));
467}
468
470{
471 auto& I = *impl_;
472 if (I.collectionsView && I.collectionsView->selectionModel())
473 I.collectionsView->selectionModel()->clearCurrentIndex();
474}
475
476// ==============================================================================
477// populateRunnerVersions
478// ==============================================================================
479
481{
482 auto& I = *impl_;
483 if (!I.runnerModel)
484 return;
485
486 // Preserve the current selection across the rebuild so the user-visible
487 // row doesn't jump when the counts change.
488 QString currentVersion = selectedRunnerVersion();
489
490 I.runnerModel->rebuild(toConstPtrs(I.libraryController->getAllGames()));
491
492 // Restore selection if still present, otherwise select "All".
493 int row = I.runnerModel->rowForVersion(currentVersion);
494 if (row < 0)
495 row = 0;
496 if (I.runnerView && I.runnerModel->rowCount() > 0)
497 I.runnerView->setCurrentIndex(I.runnerModel->index(row));
498}
499
500// ==============================================================================
501// updateSourceFilters
502// ==============================================================================
503
505{
506 auto& I = *impl_;
507 if (!I.sourceModel)
508 return;
509
510 int currentValue = selectedSourceValue();
511 I.sourceModel->rebuild(toConstPtrs(I.libraryController->getAllGames()));
512
513 int row = I.sourceModel->rowForSourceValue(currentValue);
514 if (row < 0)
515 row = 0;
516 if (I.sourceView && I.sourceModel->rowCount() > 0)
517 I.sourceView->setCurrentIndex(I.sourceModel->index(row));
518}
519
520// ==============================================================================
521// updateCategoryCounts
522// ==============================================================================
523
525{
526 auto& I = *impl_;
527 if (!I.categoryModel || !I.libraryController)
528 return;
529
530 auto games = I.libraryController->getAllGames();
531 int allCount = static_cast<int>(games.size());
532 int favoritesCount = 0;
533 int recentlyPlayedCount = 0;
534 int installedCount = 0;
535 int notInstalledCount = 0;
536 int updatesAvailableCount = 0;
537 int runningCount = 0;
538
539 for (const auto* game : games)
540 {
541 if (!game)
542 continue;
543 if (game->isFavorite)
544 favoritesCount++;
545 if (game->statistics.lastPlayed > 0)
546 recentlyPlayedCount++;
547 if (game->isInstalled())
548 installedCount++;
549 else
550 notInstalledCount++;
551
552 if (game->source == GameSource::Steam && game->metadata.steamAppId > 0)
553 {
555 static_cast<uint32_t>(game->metadata.steamAppId));
556 if (SteamStateFlags::needsUpdate(progress.stateFlags))
557 updatesAvailableCount++;
558 }
559 if (I.launchController && I.launchController->isGameRunning(game->id))
560 runningCount++;
561 }
562
563 I.categoryModel->setCounts(allCount, favoritesCount, recentlyPlayedCount, installedCount,
564 notInstalledCount, updatesAvailableCount, runningCount);
565}
566
567// ==============================================================================
568// updateCollectionsList
569// ==============================================================================
570
572{
573 auto& I = *impl_;
574 if (!I.collectionsModel)
575 return;
576
577 QString currentSelection = selectedCollection();
578 int total = static_cast<int>(I.libraryController->getGameCount());
579
580 I.collectionsModel->rebuild(I.libraryController->getCollections(),
581 toConstPtrs(I.libraryController->getAllGames()), total);
582
583 int row = I.collectionsModel->rowForCollection(currentSelection);
584 if (row < 0)
585 row = 0;
586 if (I.collectionsView && I.collectionsModel->rowCount() > 0)
587 I.collectionsView->setCurrentIndex(I.collectionsModel->index(row));
588}
589
590// ==============================================================================
591// showCreateCollectionDialog
592// ==============================================================================
593
595{
596 auto& I = *impl_;
597 bool ok = false;
598 QString name = QInputDialog::getText(dialogParent, QStringLiteral("Create Collection"),
599 QStringLiteral("Enter collection name:"),
600 QLineEdit::Normal, QString(), &ok);
601 if (!ok || name.isEmpty())
602 return;
603
604 auto collections = I.libraryController->getCollections();
605 std::string nameStr = name.toStdString();
606 if (std::find(collections.begin(), collections.end(), nameStr) != collections.end())
607 {
608 QMessageBox::warning(dialogParent, QStringLiteral("Collection Exists"),
609 QString("A collection named '%1' already exists.").arg(name));
610 return;
611 }
612
613 I.libraryController->addCollection(nameStr);
615
616 if (I.notificationService)
617 {
618 I.notificationService->success("Created collection: " + nameStr);
619 }
620}
Complete representation of a game in the library.
Definition game.h:767
Impl(SidebarManager *owner, core::GameLibraryController *libCtrl, core::LaunchController *launchCtrl, core::SettingsController *settingsCtrl, QtNotificationService *notifSvc, QWidget *parentWidget, ThemeManager *themeMgr)
core::SettingsController * settingsController
SourceFilterModel * sourceModel
QListView * makeListView(const QString &styleSheet)
core::GameLibraryController * libraryController
QString buildSidebarListStyle() const
ThemeManager * themeManager
core::LaunchController * launchController
RunnerFilterModel * runnerModel
QtNotificationService * notificationService
CollectionsModel * collectionsModel
CategoryModel * categoryModel
QString selectedCollection() const
"" = "All"
void setupSidebar()
Build the sidebar widget tree. Call once after construction.
int selectedSourceValue() const
GameSource enum int, -1 = "All".
void selectCategoryByRow(int row)
void applyTheme()
Re-apply theme colours (called from MainWindow::applyTheme()).
void showCreateCollectionDialog(QWidget *dialogParent)
int selectedCategoryIndex() const
QWidget * widget() const
The root widget — slot into MainWindow's splitter.
void selectCategoryByText(const QString &partialText)
void clearCollectionsSelection()
SidebarManager(core::GameLibraryController *libraryController, core::LaunchController *launchController, core::SettingsController *settingsController, QtNotificationService *notificationService, ThemeManager *themeManager, QWidget *parent, QObject *qparent=nullptr)
QString selectedRunnerVersion() const
"" = "All", "native" = Native
~SidebarManager() override
static SteamDownloadProgress readGameState(uint32_t appId)
Read current state of a game directly from its appmanifest.
void themeChanged()
Emitted when theme or accent color changes.
@ Steam
Steam store/platform.
Definition game.h:41
bool needsUpdate(uint32_t flags)
Check if a game needs an update.
std::vector< const Game * > toConstPtrs(const std::vector< Game * > &games)
QColor inputBackground
QColor textSecondary
QColor textPrimary
QColor hoverBackground