Qt/GameList: Fix weird column resizing

This commit is contained in:
spycrab 2018-06-24 16:34:35 +02:00
parent 7e881288a2
commit d746a8dae3
2 changed files with 121 additions and 11 deletions

View File

@ -4,6 +4,9 @@
#include "DolphinQt2/GameList/GameList.h"
#include <algorithm>
#include <cmath>
#include <QDesktopServices>
#include <QDir>
#include <QErrorMessage>
@ -16,6 +19,7 @@
#include <QMenu>
#include <QMessageBox>
#include <QProgressDialog>
#include <QScrollBar>
#include <QUrl>
#include "Common/FileUtil.h"
@ -90,30 +94,32 @@ void GameList::MakeListView()
QHeaderView* hor_header = m_list->horizontalHeader();
hor_header->restoreState(
Settings::GetQSettings().value(QStringLiteral("tableheader/state")).toByteArray());
connect(hor_header, &QHeaderView::sortIndicatorChanged, this, &GameList::OnHeaderViewChanged);
connect(hor_header, &QHeaderView::sectionResized, this, &GameList::OnHeaderViewChanged);
connect(hor_header, &QHeaderView::sectionCountChanged, this, &GameList::OnHeaderViewChanged);
connect(hor_header, &QHeaderView::sectionMoved, this, &GameList::OnHeaderViewChanged);
connect(hor_header, &QHeaderView::sectionResized, this, &GameList::OnSectionResized);
if (!Settings::GetQSettings().contains(QStringLiteral("tableheader/state")))
m_list->sortByColumn(GameListModel::COL_TITLE, Qt::AscendingOrder);
hor_header->restoreState(
Settings::GetQSettings().value(QStringLiteral("tableheader/state")).toByteArray());
hor_header->setSectionResizeMode(GameListModel::COL_PLATFORM, QHeaderView::Fixed);
hor_header->setSectionResizeMode(GameListModel::COL_BANNER, QHeaderView::Fixed);
hor_header->setSectionResizeMode(GameListModel::COL_TITLE, QHeaderView::Stretch);
hor_header->setSectionResizeMode(GameListModel::COL_TITLE, QHeaderView::Interactive);
hor_header->setSectionResizeMode(GameListModel::COL_DESCRIPTION, QHeaderView::Interactive);
hor_header->setSectionResizeMode(GameListModel::COL_MAKER, QHeaderView::Interactive);
hor_header->setSectionResizeMode(GameListModel::COL_ID, QHeaderView::Interactive);
hor_header->setSectionResizeMode(GameListModel::COL_ID, QHeaderView::Fixed);
hor_header->setSectionResizeMode(GameListModel::COL_COUNTRY, QHeaderView::Fixed);
hor_header->setSectionResizeMode(GameListModel::COL_SIZE, QHeaderView::Fixed);
hor_header->setSectionResizeMode(GameListModel::COL_FILE_NAME, QHeaderView::Interactive);
m_list->setColumnWidth(GameListModel::COL_PLATFORM, Resources::GetPlatform(0).width());
m_list->setColumnWidth(GameListModel::COL_COUNTRY, Resources::GetCountry(0).width());
m_list->setColumnWidth(GameListModel::COL_SIZE, 70);
m_list->setColumnWidth(GameListModel::COL_BANNER, 100);
m_list->setColumnWidth(GameListModel::COL_PLATFORM, 40);
m_list->setColumnWidth(GameListModel::COL_COUNTRY, 40);
m_list->setColumnWidth(GameListModel::COL_SIZE, 85);
m_list->setColumnWidth(GameListModel::COL_ID, 70);
m_list->setColumnHidden(GameListModel::COL_PLATFORM, !SConfig::GetInstance().m_showSystemColumn);
m_list->setColumnHidden(GameListModel::COL_BANNER, !SConfig::GetInstance().m_showBannerColumn);
@ -128,12 +134,19 @@ void GameList::MakeListView()
!SConfig::GetInstance().m_showFileNameColumn);
m_list->verticalHeader()->hide();
m_list->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_list->setFrameStyle(QFrame::NoFrame);
hor_header->setSectionsMovable(true);
hor_header->setHighlightSections(false);
}
GameList::~GameList()
{
Settings::GetQSettings().setValue(QStringLiteral("tableheader/state"),
m_list->horizontalHeader()->saveState());
}
void GameList::MakeEmptyView()
{
m_empty = new QLabel(this);
@ -151,6 +164,11 @@ void GameList::MakeEmptyView()
});
}
void GameList::resizeEvent(QResizeEvent* event)
{
OnHeaderViewChanged();
}
void GameList::MakeGridView()
{
m_grid = new QListView(this);
@ -531,16 +549,102 @@ static bool CompressCB(const std::string& text, float percent, void* ptr)
{
if (ptr == nullptr)
return false;
auto* progress_dialog = static_cast<QProgressDialog*>(ptr);
progress_dialog->setValue(percent * 100);
return !progress_dialog->wasCanceled();
}
void GameList::OnSectionResized(int index, int, int)
{
auto* hor_header = m_list->horizontalHeader();
std::vector<int> sections;
const int vis_index = hor_header->visualIndex(index);
const int col_count = hor_header->count() - hor_header->hiddenSectionCount();
bool last = true;
for (int i = vis_index + 1; i < col_count; i++)
{
if (hor_header->sectionResizeMode(i) != QHeaderView::Interactive)
continue;
last = false;
break;
}
if (!last)
{
for (int i = 0; i < vis_index; i++)
{
if (hor_header->sectionResizeMode(i) != QHeaderView::Interactive)
continue;
hor_header->setSectionResizeMode(hor_header->logicalIndex(i), QHeaderView::Fixed);
sections.push_back(i);
}
OnHeaderViewChanged();
for (int i : sections)
{
hor_header->setSectionResizeMode(hor_header->logicalIndex(i), QHeaderView::Interactive);
}
}
else
{
OnHeaderViewChanged();
}
}
void GameList::OnHeaderViewChanged()
{
Settings::GetQSettings().setValue(QStringLiteral("tableheader/state"),
m_list->horizontalHeader()->saveState());
static bool block = false;
if (block)
return;
block = true;
// So here's the deal: Qt's way of resizing stuff around stretched columns sucks ass
// That's why instead of using Stretch, we'll just make resizable columns take all the available
// space ourselves!
int available_width = width() - style()->pixelMetric(QStyle::PM_ScrollBarExtent);
int previous_width = 0;
std::vector<int> candidate_columns;
// Iterate through all columns
for (int i = 0; i < GameListModel::NUM_COLS; i++)
{
if (m_list->isColumnHidden(i))
continue;
if (m_list->horizontalHeader()->sectionResizeMode(i) == QHeaderView::Fixed)
{
available_width -= m_list->columnWidth(i);
}
else
{
candidate_columns.push_back(i);
previous_width += m_list->columnWidth(i);
}
}
for (int column : candidate_columns)
{
int column_width = static_cast<int>(
std::max(5.f, std::ceil(available_width * (static_cast<float>(m_list->columnWidth(column)) /
previous_width))));
m_list->setColumnWidth(column, column_width);
}
block = false;
}
void GameList::SetSearchTerm(const QString& term)

View File

@ -22,6 +22,8 @@ class GameList final : public QStackedWidget
public:
explicit GameList(QWidget* parent = nullptr);
~GameList();
std::shared_ptr<const UICommon::GameFile> GetSelectedGame() const;
void SetListView() { SetPreferredView(true); }
@ -31,6 +33,9 @@ public:
void OnColumnVisibilityToggled(const QString& row, bool visible);
void OnGameListVisibilityChanged();
void resizeEvent(QResizeEvent* event) override;
signals:
void GameSelected();
void NetPlayHost(const QString& game_id);
@ -51,6 +56,7 @@ private:
void CompressISO();
void ChangeDisc();
void OnHeaderViewChanged();
void OnSectionResized(int index, int, int);
void MakeListView();
void MakeGridView();