NOISSUE convert accounts page to use a toolbar for the side menu

This commit is contained in:
Petr Mrázek 2019-07-19 08:29:31 +02:00
parent 3d5869e1cf
commit 81fdde6fdd
5 changed files with 173 additions and 184 deletions

View File

@ -34,10 +34,14 @@
#include "MultiMC.h" #include "MultiMC.h"
AccountListPage::AccountListPage(QWidget *parent) AccountListPage::AccountListPage(QWidget *parent)
: QWidget(parent), ui(new Ui::AccountListPage) : QMainWindow(parent), ui(new Ui::AccountListPage)
{ {
ui->setupUi(this); ui->setupUi(this);
ui->tabWidget->tabBar()->hide(); ui->listView->setEmptyString(tr(
"Welcome!\n"
"If you're new here, you can click the \"Add\" button to add your Mojang or Minecraft account."
));
ui->listView->setEmptyMode(VersionListView::String);
m_accounts = MMC->accounts(); m_accounts = MMC->accounts();
@ -64,18 +68,27 @@ AccountListPage::~AccountListPage()
delete ui; delete ui;
} }
void AccountListPage::changeEvent(QEvent* event)
{
if (event->type() == QEvent::LanguageChange)
{
ui->retranslateUi(this);
}
QMainWindow::changeEvent(event);
}
void AccountListPage::listChanged() void AccountListPage::listChanged()
{ {
updateButtonStates(); updateButtonStates();
} }
void AccountListPage::on_addAccountBtn_clicked() void AccountListPage::on_actionAdd_triggered()
{ {
addAccount(tr("Please enter your Mojang or Minecraft account username and password to add " addAccount(tr("Please enter your Mojang or Minecraft account username and password to add "
"your account.")); "your account."));
} }
void AccountListPage::on_rmAccountBtn_clicked() void AccountListPage::on_actionRemove_triggered()
{ {
QModelIndexList selection = ui->listView->selectionModel()->selectedIndexes(); QModelIndexList selection = ui->listView->selectionModel()->selectedIndexes();
if (selection.size() > 0) if (selection.size() > 0)
@ -85,7 +98,7 @@ void AccountListPage::on_rmAccountBtn_clicked()
} }
} }
void AccountListPage::on_setDefaultBtn_clicked() void AccountListPage::on_actionSetDefault_triggered()
{ {
QModelIndexList selection = ui->listView->selectionModel()->selectedIndexes(); QModelIndexList selection = ui->listView->selectionModel()->selectedIndexes();
if (selection.size() > 0) if (selection.size() > 0)
@ -97,7 +110,7 @@ void AccountListPage::on_setDefaultBtn_clicked()
} }
} }
void AccountListPage::on_noDefaultBtn_clicked() void AccountListPage::on_actionNoDefault_triggered()
{ {
m_accounts->setActiveAccount(""); m_accounts->setActiveAccount("");
} }
@ -107,11 +120,19 @@ void AccountListPage::updateButtonStates()
// If there is no selection, disable buttons that require something selected. // If there is no selection, disable buttons that require something selected.
QModelIndexList selection = ui->listView->selectionModel()->selectedIndexes(); QModelIndexList selection = ui->listView->selectionModel()->selectedIndexes();
ui->rmAccountBtn->setEnabled(selection.size() > 0); ui->actionRemove->setEnabled(selection.size() > 0);
ui->setDefaultBtn->setEnabled(selection.size() > 0); ui->actionSetDefault->setEnabled(selection.size() > 0);
ui->uploadSkinBtn->setEnabled(selection.size() > 0); ui->actionUploadSkin->setEnabled(selection.size() > 0);
if(m_accounts->activeAccount().get() == nullptr) {
ui->actionNoDefault->setEnabled(false);
ui->actionNoDefault->setChecked(true);
}
else {
ui->actionNoDefault->setEnabled(true);
ui->actionNoDefault->setChecked(false);
}
ui->noDefaultBtn->setDown(m_accounts->activeAccount().get() == nullptr);
} }
void AccountListPage::addAccount(const QString &errMsg) void AccountListPage::addAccount(const QString &errMsg)
@ -140,7 +161,7 @@ void AccountListPage::addAccount(const QString &errMsg)
} }
} }
void AccountListPage::on_uploadSkinBtn_clicked() void AccountListPage::on_actionUploadSkin_triggered()
{ {
QModelIndexList selection = ui->listView->selectionModel()->selectedIndexes(); QModelIndexList selection = ui->listView->selectionModel()->selectedIndexes();
if (selection.size() > 0) if (selection.size() > 0)

View File

@ -15,7 +15,7 @@
#pragma once #pragma once
#include <QDialog> #include <QMainWindow>
#include <memory> #include <memory>
#include "pages/BasePage.h" #include "pages/BasePage.h"
@ -30,7 +30,7 @@ class AccountListPage;
class AuthenticateTask; class AuthenticateTask;
class AccountListPage : public QWidget, public BasePage class AccountListPage : public QMainWindow, public BasePage
{ {
Q_OBJECT Q_OBJECT
public: public:
@ -59,17 +59,20 @@ public:
return "Getting-Started#adding-an-account"; return "Getting-Started#adding-an-account";
} }
private:
void changeEvent(QEvent * event) override;
public public
slots: slots:
void on_addAccountBtn_clicked(); void on_actionAdd_triggered();
void on_rmAccountBtn_clicked(); void on_actionRemove_triggered();
void on_setDefaultBtn_clicked(); void on_actionSetDefault_triggered();
void on_noDefaultBtn_clicked(); void on_actionNoDefault_triggered();
void on_uploadSkinBtn_clicked(); void on_actionUploadSkin_triggered();
void listChanged(); void listChanged();

View File

@ -1,122 +1,101 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"> <ui version="4.0">
<class>AccountListPage</class> <class>AccountListPage</class>
<widget class="QWidget" name="AccountListPage"> <widget class="QMainWindow" name="AccountListPage">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>694</width> <width>800</width>
<height>609</height> <height>600</height>
</rect> </rect>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_2"> <property name="windowTitle">
<property name="leftMargin"> <string>MainWindow</string>
<number>0</number> </property>
<widget class="QWidget" name="centralwidget">
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="VersionListView" name="listView"/>
</item>
</layout>
</widget>
<widget class="QToolBar" name="toolBar">
<property name="windowTitle">
<string>toolBar</string>
</property> </property>
<property name="topMargin"> <property name="movable">
<number>0</number> <bool>true</bool>
</property> </property>
<property name="rightMargin"> <property name="allowedAreas">
<number>0</number> <set>Qt::LeftToolBarArea|Qt::RightToolBarArea</set>
</property> </property>
<property name="bottomMargin"> <property name="toolButtonStyle">
<number>0</number> <enum>Qt::ToolButtonTextOnly</enum>
</property> </property>
<item> <property name="floatable">
<widget class="QTabWidget" name="tabWidget"> <bool>false</bool>
<property name="currentIndex"> </property>
<number>0</number> <attribute name="toolBarArea">
</property> <enum>RightToolBarArea</enum>
<widget class="QWidget" name="tab"> </attribute>
<attribute name="title"> <attribute name="toolBarBreak">
<string notr="true">Tab 1</string> <bool>false</bool>
</attribute> </attribute>
<layout class="QVBoxLayout" name="verticalLayout"> <addaction name="actionAdd"/>
<item> <addaction name="actionRemove"/>
<widget class="QLabel" name="welcomeLabel"> <addaction name="actionSetDefault"/>
<property name="text"> <addaction name="actionNoDefault"/>
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Welcome! If you're new here, you can click the &amp;quot;Add&amp;quot; button to add your Mojang or Minecraft account.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string> <addaction name="actionUploadSkin"/>
</property> </widget>
<property name="wordWrap"> <action name="actionAdd">
<bool>true</bool> <property name="text">
</property> <string>Add</string>
</widget> </property>
</item> </action>
<item> <action name="actionRemove">
<layout class="QHBoxLayout" name="horizontalLayout"> <property name="text">
<item> <string>Remove</string>
<widget class="QTreeView" name="listView"/> </property>
</item> </action>
<item> <action name="actionSetDefault">
<layout class="QVBoxLayout" name="manageAcctsBtnBox"> <property name="text">
<item> <string>Set Default</string>
<widget class="QPushButton" name="addAccountBtn"> </property>
<property name="text"> </action>
<string>&amp;Add</string> <action name="actionNoDefault">
</property> <property name="checkable">
</widget> <bool>true</bool>
</item> </property>
<item> <property name="text">
<widget class="QPushButton" name="rmAccountBtn"> <string>No Default</string>
<property name="text"> </property>
<string>&amp;Remove</string> </action>
</property> <action name="actionUploadSkin">
</widget> <property name="text">
</item> <string>Upload Skin</string>
<item> </property>
<spacer name="buttonSpacer"> </action>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="setDefaultBtn">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Set the currently selected account as the active account. The active account is the account that is used to log in (unless it is overridden in an instance-specific setting).&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>&amp;Set Default</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="noDefaultBtn">
<property name="toolTip">
<string>Set no default account. This will cause MultiMC to prompt you to select an account every time you launch an instance that doesn't have its own default set.</string>
</property>
<property name="text">
<string>&amp;No Default</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="uploadSkinBtn">
<property name="toolTip">
<string>Opens a dialog to select and upload a skin image to the selected account.</string>
</property>
<property name="text">
<string>&amp;Upload Skin</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget> </widget>
<customwidgets>
<customwidget>
<class>VersionListView</class>
<extends>QTreeView</extends>
<header>widgets/VersionListView.h</header>
</customwidget>
</customwidgets>
<resources/> <resources/>
<connections/> <connections/>
</ui> </ui>

View File

@ -29,9 +29,8 @@ VersionListView::VersionListView(QWidget *parent)
void VersionListView::rowsInserted(const QModelIndex &parent, int start, int end) void VersionListView::rowsInserted(const QModelIndex &parent, int start, int end)
{ {
if(!m_itemCount)
viewport()->update();
m_itemCount += end-start+1; m_itemCount += end-start+1;
updateEmptyViewPort();
QTreeView::rowsInserted(parent, start, end); QTreeView::rowsInserted(parent, start, end);
} }
@ -39,16 +38,14 @@ void VersionListView::rowsInserted(const QModelIndex &parent, int start, int end
void VersionListView::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) void VersionListView::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
{ {
m_itemCount -= end-start+1; m_itemCount -= end-start+1;
if(!m_itemCount) updateEmptyViewPort();
viewport()->update();
QTreeView::rowsInserted(parent, start, end); QTreeView::rowsInserted(parent, start, end);
} }
void VersionListView::setModel(QAbstractItemModel *model) void VersionListView::setModel(QAbstractItemModel *model)
{ {
m_itemCount = model->rowCount(); m_itemCount = model->rowCount();
if(!m_itemCount) updateEmptyViewPort();
viewport()->update();
QTreeView::setModel(model); QTreeView::setModel(model);
} }
@ -58,7 +55,10 @@ void VersionListView::reset()
{ {
m_itemCount = model()->rowCount(); m_itemCount = model()->rowCount();
} }
viewport()->update(); else {
m_itemCount = 0;
}
updateEmptyViewPort();
QTreeView::reset(); QTreeView::reset();
} }
@ -82,6 +82,8 @@ void VersionListView::setEmptyMode(VersionListView::EmptyMode mode)
void VersionListView::updateEmptyViewPort() void VersionListView::updateEmptyViewPort()
{ {
setAccessibleDescription(currentEmptyString());
if(!m_itemCount) if(!m_itemCount)
{ {
viewport()->update(); viewport()->update();
@ -100,77 +102,60 @@ void VersionListView::paintEvent(QPaintEvent *event)
} }
} }
void VersionListView::paintInfoLabel(QPaintEvent *event) QString VersionListView::currentEmptyString() const
{ {
QString emptyString; if(m_itemCount) {
return QString();
}
switch(m_emptyMode) switch(m_emptyMode)
{ {
default:
case VersionListView::Empty: case VersionListView::Empty:
return; return QString();
case VersionListView::String: case VersionListView::String:
emptyString = m_emptyString; return m_emptyString;
break;
case VersionListView::ErrorString: case VersionListView::ErrorString:
emptyString = m_emptyErrorString; return m_emptyErrorString;
break;
} }
}
void VersionListView::paintInfoLabel(QPaintEvent *event) const
{
QString emptyString = currentEmptyString();
//calculate the rect for the overlay //calculate the rect for the overlay
QPainter painter(viewport()); QPainter painter(viewport());
painter.setRenderHint(QPainter::Antialiasing, true); painter.setRenderHint(QPainter::Antialiasing, true);
QFont font("sans", 20); QFont font("sans", 20);
font.setBold(true); font.setBold(true);
QRect bounds = viewport()->geometry(); QRect bounds = viewport()->geometry();
bounds.moveTop(0); bounds.moveTop(0);
QTextLayout layout(emptyString, font); auto innerBounds = bounds;
qreal height = 0.0; innerBounds.adjust(10, 10, -10, -10);
qreal widthUsed = 0.0;
QStringList lines = viewItemTextLayout(layout, bounds.width() - 20, height, widthUsed);
QRect rect (0,0, widthUsed, height);
rect.setWidth(rect.width()+20);
rect.setHeight(rect.height()+20);
rect.moveCenter(bounds.center());
//check if we are allowed to draw in our area
if (!event->rect().intersects(rect)) {
return;
}
//draw the letter of the topmost item semitransparent in the middle
QColor background = QApplication::palette().color(QPalette::Foreground); QColor background = QApplication::palette().color(QPalette::Foreground);
QColor foreground = QApplication::palette().color(QPalette::Base); QColor foreground = QApplication::palette().color(QPalette::Base);
/* foreground.setAlpha(190);
background.setAlpha(128 - scrollFade); painter.setFont(font);
foreground.setAlpha(128 - scrollFade); auto fontMetrics = painter.fontMetrics();
*/ auto textRect = fontMetrics.boundingRect(innerBounds, Qt::AlignHCenter | Qt::TextWordWrap, emptyString);
textRect.moveCenter(bounds.center());
auto wrapRect = textRect;
wrapRect.adjust(-10, -10, 10, 10);
//check if we are allowed to draw in our area
if (!event->rect().intersects(wrapRect)) {
return;
}
painter.setBrush(QBrush(background)); painter.setBrush(QBrush(background));
painter.setPen(foreground); painter.setPen(foreground);
painter.drawRoundedRect(rect, 5.0, 5.0); painter.drawRoundedRect(wrapRect, 5.0, 5.0);
foreground.setAlpha(190);
painter.setPen(foreground); painter.setPen(foreground);
painter.setFont(font); painter.setFont(font);
painter.drawText(rect, Qt::AlignCenter, lines.join("\n")); painter.drawText(textRect, Qt::AlignHCenter | Qt::TextWordWrap, emptyString);
} }
/*
void ModListView::setModel ( QAbstractItemModel* model )
{
QTreeView::setModel ( model );
auto head = header();
head->setStretchLastSection(false);
// HACK: this is true for the checkbox column of mod lists
auto string = model->headerData(0,head->orientation()).toString();
if(!string.size())
{
head->setSectionResizeMode(0, QHeaderView::ResizeToContents);
head->setSectionResizeMode(1, QHeaderView::Stretch);
for(int i = 2; i < head->count(); i++)
head->setSectionResizeMode(i, QHeaderView::ResizeToContents);
}
else
{
head->setSectionResizeMode(0, QHeaderView::Stretch);
for(int i = 1; i < head->count(); i++)
head->setSectionResizeMode(i, QHeaderView::ResizeToContents);
}
}
*/

View File

@ -46,8 +46,9 @@ protected slots:
virtual void rowsInserted(const QModelIndex &parent, int start, int end) override; virtual void rowsInserted(const QModelIndex &parent, int start, int end) override;
private: /* methods */ private: /* methods */
void paintInfoLabel(QPaintEvent *event); void paintInfoLabel(QPaintEvent *event) const;
void updateEmptyViewPort(); void updateEmptyViewPort();
QString currentEmptyString() const;
private: /* variables */ private: /* variables */
int m_itemCount = 0; int m_itemCount = 0;