Allow moving accounts in list (#4647)

This commit is contained in:
Alexandru Ionut Tripon 2026-01-17 02:08:19 +02:00 committed by GitHub
commit 78bc6657f4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 59 additions and 0 deletions

View file

@ -168,6 +168,24 @@ void AccountList::removeAccount(QModelIndex index)
}
}
void AccountList::moveAccount(QModelIndex index, int delta) {
const int row = index.row();
const int newRow = row + delta;
if (index.isValid() && row < m_accounts.size() && newRow >= 0 && newRow < m_accounts.size()) {
// Qt is stupid, https://doc.qt.io/qt-6/qabstractitemmodel.html#beginMoveRows
const int modelDestinationRow = (newRow > row) ? newRow + 1 : newRow;
if (beginMoveRows(QModelIndex(), row, row, QModelIndex(), modelDestinationRow)) {
m_accounts.move(row, newRow);
endMoveRows();
onListChanged();
} else {
qCritical().noquote() << "AccountList: failed to move account from" << row << "to" << newRow << QString("(%1 accounts in total)").arg(this->count());
}
}
}
MinecraftAccountPtr AccountList::defaultAccount() const
{
return m_defaultAccount;

View file

@ -78,6 +78,7 @@ class AccountList : public QAbstractListModel {
void addAccount(MinecraftAccountPtr account);
void removeAccount(QModelIndex index);
void moveAccount(QModelIndex index, int delta);
int findAccountByProfileId(const QString& profileId) const;
MinecraftAccountPtr getAccountByProfileName(const QString& profileName) const;
QStringList profileNames() const;

View file

@ -210,11 +210,17 @@ void AccountListPage::updateButtonStates()
bool hasSelection = !selection.empty();
bool accountIsReady = false;
bool accountIsOnline = false;
bool accountCanMoveUp = false;
bool accountCanMoveDown = false;
if (hasSelection) {
QModelIndex selected = selection.first();
MinecraftAccountPtr account = selected.data(AccountList::PointerRole).value<MinecraftAccountPtr>();
accountIsReady = !account->isActive();
accountIsOnline = account->accountType() != AccountType::Offline;
accountCanMoveUp = selected.row() > 0;
int indexOfLast = m_accounts->count() - 1;
accountCanMoveDown = selected.row() < indexOfLast;
}
ui->actionRemove->setEnabled(accountIsReady);
ui->actionSetDefault->setEnabled(accountIsReady);
@ -228,6 +234,8 @@ void AccountListPage::updateButtonStates()
ui->actionNoDefault->setEnabled(true);
ui->actionNoDefault->setChecked(false);
}
ui->actionMoveUp->setEnabled(accountCanMoveUp);
ui->actionMoveDown->setEnabled(accountCanMoveDown);
ui->listView->resizeColumnToContents(3);
}
@ -241,3 +249,21 @@ void AccountListPage::on_actionManageSkins_triggered()
dialog.exec();
}
}
void AccountListPage::on_actionMoveUp_triggered()
{
QModelIndexList selection = ui->listView->selectionModel()->selectedIndexes();
if (selection.size() > 0) {
QModelIndex selected = selection.first();
m_accounts->moveAccount(selected, -1);
}
}
void AccountListPage::on_actionMoveDown_triggered()
{
QModelIndexList selection = ui->listView->selectionModel()->selectedIndexes();
if (selection.size() > 0) {
QModelIndex selected = selection.first();
m_accounts->moveAccount(selected, 1);
}
}

View file

@ -76,6 +76,8 @@ class AccountListPage : public QMainWindow, public BasePage {
void on_actionSetDefault_triggered();
void on_actionNoDefault_triggered();
void on_actionManageSkins_triggered();
void on_actionMoveUp_triggered();
void on_actionMoveDown_triggered();
void listChanged();

View file

@ -58,6 +58,8 @@
<addaction name="actionRemove"/>
<addaction name="actionSetDefault"/>
<addaction name="actionNoDefault"/>
<addaction name="actionMoveUp"/>
<addaction name="actionMoveDown"/>
<addaction name="separator"/>
<addaction name="actionManageSkins"/>
</widget>
@ -105,6 +107,16 @@
<string>Remo&amp;ve</string>
</property>
</action>
<action name="actionMoveUp">
<property name="text">
<string>Move &amp;Up</string>
</property>
</action>
<action name="actionMoveDown">
<property name="text">
<string>Move &amp;Down</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>