From 69d8ab16c58509bafb98694f9aa998ae51ff4b7f Mon Sep 17 00:00:00 2001 From: Sky Date: Wed, 27 Nov 2013 22:39:49 +0000 Subject: [PATCH 1/3] Dropdown for account switching --- gui/MainWindow.cpp | 110 +++++++++++++++++++++++++++--- gui/MainWindow.h | 8 ++- gui/dialogs/AccountListDialog.cpp | 3 + 3 files changed, 110 insertions(+), 11 deletions(-) diff --git a/gui/MainWindow.cpp b/gui/MainWindow.cpp index 3fdbed44d..5295a8a5b 100644 --- a/gui/MainWindow.cpp +++ b/gui/MainWindow.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include "osutils.h" #include "userutils.h" @@ -171,12 +172,24 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); ui->mainToolBar->addWidget(spacer); - actionManageAccounts = new QToolButton(this); - actionManageAccounts->setToolTip(tr("Manage your Mojang or Minecraft accounts.")); - actionManageAccounts->setObjectName("actionManageAccounts"); - actionManageAccounts->setText(tr("Manage accounts")); - actionManageAccounts->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); - actionManageAccounts->setLayoutDirection(Qt::RightToLeft); + accountMenu = new QMenu(this); + manageAccountsAction = new QAction(tr("Manage accounts"), this); + manageAccountsAction->setCheckable(false); + connect(manageAccountsAction, SIGNAL(triggered(bool)), this, SLOT(on_actionManageAccounts_triggered())); + + repopulateAccountsMenu(); + + accountMenuButton = new QToolButton(this); + accountMenuButton->setText(tr("Accounts")); + accountMenuButton->setMenu(accountMenu); + accountMenuButton->setPopupMode(QToolButton::InstantPopup); + accountMenuButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); + accountMenuButton->setLayoutDirection(Qt::RightToLeft); + + QWidgetAction *accountMenuButtonAction = new QWidgetAction(this); + accountMenuButtonAction->setDefaultWidget(accountMenuButton); + + ui->mainToolBar->addAction(accountMenuButtonAction); MojangAccountPtr account = MMC->accounts()->activeAccount(); if(account != nullptr) @@ -197,9 +210,6 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi job->start(); } - connect(actionManageAccounts, SIGNAL(clicked()), this, SLOT(on_actionManageAccounts_triggered())); - ui->mainToolBar->addWidget(actionManageAccounts); - // run the things that load and download other things... FIXME: this is NOT the place // FIXME: invisible actions in the background = NOPE. { @@ -232,8 +242,85 @@ MainWindow::~MainWindow() delete assets_downloader; } + +void MainWindow::repopulateAccountsMenu() +{ + accountMenu->clear(); + + std::shared_ptr accounts = MMC->accounts(); + + QString active_username; + if(accounts->activeAccount() != nullptr) + { + active_username = accounts->activeAccount()->username(); + } + + if(accounts->count() <= 0) + { + QAction *action = new QAction(tr("No accounts added!"), this); + action->setEnabled(false); + accountMenu->addAction(action); + + accountMenu->addSeparator(); + } + else + { + // TODO: Nicer way to iterate? + for(int i = 0; i < accounts->count(); i++) + { + MojangAccountPtr account = accounts->at(i); + + // Styling hack + QAction *section = new QAction(account->username(), this); + section->setEnabled(false); + accountMenu->addAction(section); + + for(AccountProfile profile : account->profiles()) + { + QAction *action = new QAction(profile.name(), this); + action->setData(account->username()); + action->setCheckable(true); + if(active_username == account->username()) + { + action->setChecked(true); + } + + accountMenu->addAction(action); + connect(action, SIGNAL(triggered(bool)), SLOT(changeActiveAccount())); + } + + accountMenu->addSeparator(); + } + } + + accountMenu->addAction(manageAccountsAction); +} + +/* + * Assumes the sender is a QAction + */ +void MainWindow::changeActiveAccount() +{ + QAction* sAction = (QAction*) sender(); + + // Profile's associated Mojang username + // Will need to change when profiles are properly implemented + if(sAction->data().type() != QVariant::Type::String) return; + + QString id = sAction->data().toString(); + + if(id != nullptr && !id.isEmpty()) + { + MMC->accounts()->setActiveAccount(id); + } + + activeAccountChanged(); +} + void MainWindow::activeAccountChanged() { + repopulateAccountsMenu(); + MojangAccountPtr account = MMC->accounts()->activeAccount(); if(account != nullptr) @@ -241,9 +328,12 @@ void MainWindow::activeAccountChanged() const AccountProfile *profile = account->currentProfile(); if(profile != nullptr) { - actionManageAccounts->setIcon(SkinUtils::getFaceFromCache(profile->name())); + accountMenuButton->setIcon(SkinUtils::getFaceFromCache(profile->name())); + return; } } + + accountMenuButton->setIcon(QIcon()); } bool MainWindow::eventFilter(QObject *obj, QEvent *ev) diff --git a/gui/MainWindow.h b/gui/MainWindow.h index 4e20d7cbb..4191e5902 100644 --- a/gui/MainWindow.h +++ b/gui/MainWindow.h @@ -148,6 +148,10 @@ slots: void activeAccountChanged(); + void changeActiveAccount(); + + void repopulateAccountsMenu(); + protected: bool eventFilter(QObject *obj, QEvent *ev); void setCatBackground(bool enabled); @@ -170,5 +174,7 @@ private: QLabel *m_statusLeft; QLabel *m_statusRight; - QToolButton *actionManageAccounts; + QMenu *accountMenu; + QToolButton *accountMenuButton; + QAction *manageAccountsAction; }; diff --git a/gui/dialogs/AccountListDialog.cpp b/gui/dialogs/AccountListDialog.cpp index ea6861c68..0ce146385 100644 --- a/gui/dialogs/AccountListDialog.cpp +++ b/gui/dialogs/AccountListDialog.cpp @@ -112,6 +112,9 @@ void AccountListDialog::onLoginComplete() // Add the authenticated account to the accounts list. MojangAccountPtr account = m_authTask->getMojangAccount(); m_accounts->addAccount(account); + + emit activeAccountChanged(); + //ui->listView->update(); // Grab associated player skins From 70973d111a35c65072b9431420d3d854bd82e8f5 Mon Sep 17 00:00:00 2001 From: Sky Date: Wed, 27 Nov 2013 22:47:15 +0000 Subject: [PATCH 2/3] Use player face in accounts dropdown, grab all valid account skins on startup --- gui/MainWindow.cpp | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/gui/MainWindow.cpp b/gui/MainWindow.cpp index 5295a8a5b..2917bf953 100644 --- a/gui/MainWindow.cpp +++ b/gui/MainWindow.cpp @@ -191,23 +191,29 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi ui->mainToolBar->addAction(accountMenuButtonAction); - MojangAccountPtr account = MMC->accounts()->activeAccount(); - if(account != nullptr) + std::shared_ptr accounts = MMC->accounts(); + + // TODO: Nicer way to iterate? + for(int i = 0; i < accounts->count(); i++) { - auto job = new NetJob("Startup player skins: " + account->username()); - - for(AccountProfile profile : account->profiles()) + MojangAccountPtr account = accounts->at(i); + if(account != nullptr) { - auto meta = MMC->metacache()->resolveEntry("skins", profile.name() + ".png"); - auto action = CacheDownload::make( - QUrl("http://skins.minecraft.net/MinecraftSkins/" + profile.name() + ".png"), - meta); - job->addNetAction(action); - meta->stale = true; - } + auto job = new NetJob("Startup player skins: " + account->username()); - connect(job, SIGNAL(succeeded()), SLOT(activeAccountChanged())); - job->start(); + for(AccountProfile profile : account->profiles()) + { + auto meta = MMC->metacache()->resolveEntry("skins", profile.name() + ".png"); + auto action = CacheDownload::make( + QUrl("http://skins.minecraft.net/MinecraftSkins/" + profile.name() + ".png"), + meta); + job->addNetAction(action); + meta->stale = true; + } + + connect(job, SIGNAL(succeeded()), SLOT(activeAccountChanged())); + job->start(); + } } // run the things that load and download other things... FIXME: this is NOT the place @@ -285,6 +291,7 @@ void MainWindow::repopulateAccountsMenu() action->setChecked(true); } + action->setIcon(SkinUtils::getFaceFromCache(profile.name())); accountMenu->addAction(action); connect(action, SIGNAL(triggered(bool)), SLOT(changeActiveAccount())); } From 191e850cf14666302c3338d69073d1f417925a45 Mon Sep 17 00:00:00 2001 From: Sky Date: Wed, 27 Nov 2013 23:39:36 +0000 Subject: [PATCH 3/3] Add support for "no default account" --- gui/MainWindow.cpp | 30 ++++++++++++++++++++++-------- gui/dialogs/AccountListDialog.cpp | 2 ++ logic/lists/MojangAccountList.cpp | 6 +++++- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/gui/MainWindow.cpp b/gui/MainWindow.cpp index a9f29d86d..f8904267a 100644 --- a/gui/MainWindow.cpp +++ b/gui/MainWindow.cpp @@ -255,9 +255,10 @@ void MainWindow::repopulateAccountsMenu() accountMenu->clear(); std::shared_ptr accounts = MMC->accounts(); + MojangAccountPtr active_account = accounts->activeAccount(); - QString active_username; - if(accounts->activeAccount() != nullptr) + QString active_username = ""; + if(active_account != nullptr) { active_username = accounts->activeAccount()->username(); } @@ -301,6 +302,18 @@ void MainWindow::repopulateAccountsMenu() } } + QAction *action = new QAction(tr("No default"), this); + action->setCheckable(true); + action->setData(""); + if(active_username.isEmpty()) + { + action->setChecked(true); + } + + accountMenu->addAction(action); + connect(action, SIGNAL(triggered(bool)), SLOT(changeActiveAccount())); + + accountMenu->addSeparator(); accountMenu->addAction(manageAccountsAction); } @@ -310,18 +323,19 @@ void MainWindow::repopulateAccountsMenu() void MainWindow::changeActiveAccount() { QAction* sAction = (QAction*) sender(); - // Profile's associated Mojang username // Will need to change when profiles are properly implemented if(sAction->data().type() != QVariant::Type::String) return; - QString id = sAction->data().toString(); - - if(id != nullptr && !id.isEmpty()) + QVariant data = sAction->data(); + QString id = ""; + if(!data.isNull()) { - MMC->accounts()->setActiveAccount(id); + id = data.toString(); } + MMC->accounts()->setActiveAccount(id); + activeAccountChanged(); } @@ -331,7 +345,7 @@ void MainWindow::activeAccountChanged() MojangAccountPtr account = MMC->accounts()->activeAccount(); - if(account != nullptr) + if(account != nullptr && account->username() != "") { const AccountProfile *profile = account->currentProfile(); if(profile != nullptr) diff --git a/gui/dialogs/AccountListDialog.cpp b/gui/dialogs/AccountListDialog.cpp index 97dc05645..29f3838dc 100644 --- a/gui/dialogs/AccountListDialog.cpp +++ b/gui/dialogs/AccountListDialog.cpp @@ -92,6 +92,8 @@ void AccountListDialog::on_setDefaultBtn_clicked() void AccountListDialog::on_noDefaultBtn_clicked() { m_accounts->setActiveAccount(""); + + emit activeAccountChanged(); } void AccountListDialog::on_closeBtnBox_rejected() diff --git a/logic/lists/MojangAccountList.cpp b/logic/lists/MojangAccountList.cpp index a30ef4abc..c6dce8363 100644 --- a/logic/lists/MojangAccountList.cpp +++ b/logic/lists/MojangAccountList.cpp @@ -100,8 +100,10 @@ void MojangAccountList::setActiveAccount(const QString& username) else { for (MojangAccountPtr account : m_accounts) + { if (account->username() == username) m_activeAccount = username; + } } endResetModel(); onListChanged(); @@ -113,7 +115,9 @@ void MojangAccountList::onListChanged() if (m_autosave) // TODO: Alert the user if this fails. saveList(); - emit listChanged(); + + // TODO: stop this getting called from setActiveAccount + //emit listChanged(); }