Qt: also repaint trophy game list background colors

And refactor a bit
This commit is contained in:
Megamouse 2018-07-07 20:23:08 +02:00 committed by Ivan
parent f0a9799b4b
commit 0d72889c52
4 changed files with 153 additions and 85 deletions

View File

@ -1082,6 +1082,8 @@ void main_window::RepaintGui()
RepaintToolBarIcons();
RepaintThumbnailIcons();
Q_EMIT RequestTrophyManagerRepaint();
}
void main_window::CreateActions()
@ -1211,6 +1213,7 @@ void main_window::CreateConnects()
connect(ui->actionManage_Trophy_Data, &QAction::triggered, [=]
{
trophy_manager_dialog* trop_manager = new trophy_manager_dialog(guiSettings);
connect(this, &main_window::RequestTrophyManagerRepaint, trop_manager, &trophy_manager_dialog::RepaintUI);
trop_manager->show();
});

View File

@ -75,6 +75,7 @@ public:
Q_SIGNALS:
void RequestGlobalStylesheetChange(const QString& sheetFilePath);
void RequestTrophyManagerRepaint();
public Q_SLOTS:
void OnEmuStop();

View File

@ -66,6 +66,7 @@ trophy_manager_dialog::trophy_manager_dialog(std::shared_ptr<gui_settings> gui_s
// Games Table
m_game_table = new QTableWidget();
m_game_table->setObjectName("trophy_manager_game_table");
m_game_table->setShowGrid(false);
m_game_table->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
m_game_table->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
@ -87,6 +88,7 @@ trophy_manager_dialog::trophy_manager_dialog(std::shared_ptr<gui_settings> gui_s
// Trophy Table
m_trophy_table = new QTableWidget();
m_trophy_table->setObjectName("trophy_manager_trophy_table");
m_trophy_table->setShowGrid(false);
m_trophy_table->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
m_trophy_table->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
@ -110,51 +112,8 @@ trophy_manager_dialog::trophy_manager_dialog(std::shared_ptr<gui_settings> gui_s
m_splitter->addWidget(m_game_table);
m_splitter->addWidget(m_trophy_table);
// Populate the trophy database
QDirIterator dir_iter(qstr(vfs::get(m_TROPHY_DIR)), QDir::Dirs | QDir::NoDotAndDotDot);
while (dir_iter.hasNext())
{
dir_iter.next();
std::string dirName = sstr(dir_iter.fileName());
LOG_TRACE(GENERAL, "Loading trophy dir: %s", dirName);
LoadTrophyFolderToDB(dirName);
}
m_game_table->setRowCount(m_trophies_db.size());
m_game_icon_size = gui_settings::SizeFromSlider(m_game_icon_size_index);
for (int i = 0; i < m_trophies_db.size(); ++i)
{
const int all_trophies = m_trophies_db[i]->trop_usr->GetTrophiesCount();
const int unlocked_trophies = m_trophies_db[i]->trop_usr->GetUnlockedTrophiesCount();
const int percentage = 100 * unlocked_trophies / all_trophies;
const std::string icon_path = m_trophies_db[i]->path + "/ICON0.PNG";
const QString name = qstr(m_trophies_db[i]->game_name).simplified();
const QString progress = QString("%0% (%1/%2)").arg(percentage).arg(unlocked_trophies).arg(all_trophies);
m_game_combo->addItem(name, i);
// Load game icon
QPixmap icon;
if (!icon.load(qstr(icon_path)))
{
LOG_WARNING(GENERAL, "Could not load trophy game icon from path %s", icon_path);
}
custom_table_widget_item* icon_item = new custom_table_widget_item;
icon_item->setData(Qt::UserRole, icon);
icon_item->setData(Qt::DecorationRole, icon.scaled(m_game_icon_size, Qt::KeepAspectRatio, Qt::TransformationMode::SmoothTransformation));
m_game_table->setItem(i, GameColumns::GameIcon, icon_item);
m_game_table->setItem(i, GameColumns::GameName, new custom_table_widget_item(name));
m_game_table->setItem(i, GameColumns::GameProgress, new custom_table_widget_item(progress, Qt::UserRole, percentage));
}
gui::utils::resize_combo_box_view(m_game_combo);
m_game_table->setSortingEnabled(true); // Enable sorting only after using setItem calls
// Checkboxes to control dialog
QCheckBox* check_lock_trophy = new QCheckBox(tr("Show Locked Trophies"));
check_lock_trophy->setCheckable(true);
@ -241,36 +200,6 @@ trophy_manager_dialog::trophy_manager_dialog(std::shared_ptr<gui_settings> gui_s
all_layout->setStretch(1, 1);
setLayout(all_layout);
if (!restoreGeometry(m_gui_settings->GetValue(gui::tr_geometry).toByteArray()))
resize(QDesktopWidget().availableGeometry().size() * 0.7);
if (!m_splitter->restoreState(m_gui_settings->GetValue(gui::tr_splitterState).toByteArray()))
{
const int width_left = m_splitter->width() * 0.4;
const int width_right = m_splitter->width() - width_left;
m_splitter->setSizes({ width_left, width_right });
}
PopulateUI();
QByteArray game_table_state = m_gui_settings->GetValue(gui::tr_games_state).toByteArray();
if (!m_game_table->horizontalHeader()->restoreState(game_table_state) && m_game_table->rowCount())
{
// If no settings exist, resize to contents. (disabled)
//m_game_table->verticalHeader()->resizeSections(QHeaderView::ResizeMode::ResizeToContents);
//m_game_table->horizontalHeader()->resizeSections(QHeaderView::ResizeMode::ResizeToContents);
}
QByteArray trophy_table_state = m_gui_settings->GetValue(gui::tr_trophy_state).toByteArray();
if (!m_trophy_table->horizontalHeader()->restoreState(trophy_table_state) && m_trophy_table->rowCount())
{
// If no settings exist, resize to contents. (disabled)
//m_trophy_table->verticalHeader()->resizeSections(QHeaderView::ResizeMode::ResizeToContents);
//m_trophy_table->horizontalHeader()->resizeSections(QHeaderView::ResizeMode::ResizeToContents);
}
ApplyFilter();
// Make connects
connect(m_icon_slider, &QSlider::valueChanged, this, [=](int val)
{
@ -376,7 +305,7 @@ trophy_manager_dialog::trophy_manager_dialog(std::shared_ptr<gui_settings> gui_s
connect(m_game_combo, &QComboBox::currentTextChanged, [this]
{
PopulateUI();
PopulateTrophyTable();
ApplyFilter();
});
@ -385,10 +314,7 @@ trophy_manager_dialog::trophy_manager_dialog(std::shared_ptr<gui_settings> gui_s
m_game_combo->setCurrentText(m_game_table->item(m_game_table->currentRow(), GameColumns::GameName)->text());
});
// Show dialog and then paint gui in order to adjust headers correctly
show();
ReadjustGameTable();
ReadjustTrophyTable();
RepaintUI();
}
bool trophy_manager_dialog::LoadTrophyFolderToDB(const std::string& trop_name)
@ -462,6 +388,80 @@ bool trophy_manager_dialog::LoadTrophyFolderToDB(const std::string& trop_name)
return true;
}
void trophy_manager_dialog::RepaintUI()
{
if (m_gui_settings->GetValue(gui::m_enableUIColors).toBool())
{
m_game_icon_color = m_gui_settings->GetValue(gui::gl_iconColor).value<QColor>();
}
else
{
m_game_icon_color = gui::utils::get_label_color("gamelist_icon_background_color");
}
PopulateTrophyDB();
PopulateGameTable();
if (!restoreGeometry(m_gui_settings->GetValue(gui::tr_geometry).toByteArray()))
{
resize(QDesktopWidget().availableGeometry().size() * 0.7);
}
if (!m_splitter->restoreState(m_gui_settings->GetValue(gui::tr_splitterState).toByteArray()))
{
const int width_left = m_splitter->width() * 0.4;
const int width_right = m_splitter->width() - width_left;
m_splitter->setSizes({ width_left, width_right });
}
PopulateTrophyTable();
QByteArray game_table_state = m_gui_settings->GetValue(gui::tr_games_state).toByteArray();
if (!m_game_table->horizontalHeader()->restoreState(game_table_state) && m_game_table->rowCount())
{
// If no settings exist, resize to contents. (disabled)
//m_game_table->verticalHeader()->resizeSections(QHeaderView::ResizeMode::ResizeToContents);
//m_game_table->horizontalHeader()->resizeSections(QHeaderView::ResizeMode::ResizeToContents);
}
QByteArray trophy_table_state = m_gui_settings->GetValue(gui::tr_trophy_state).toByteArray();
if (!m_trophy_table->horizontalHeader()->restoreState(trophy_table_state) && m_trophy_table->rowCount())
{
// If no settings exist, resize to contents. (disabled)
//m_trophy_table->verticalHeader()->resizeSections(QHeaderView::ResizeMode::ResizeToContents);
//m_trophy_table->horizontalHeader()->resizeSections(QHeaderView::ResizeMode::ResizeToContents);
}
ApplyFilter();
// Show dialog and then paint gui in order to adjust headers correctly
show();
ReadjustGameTable();
ReadjustTrophyTable();
}
void trophy_manager_dialog::ResizeGameIcon(int index)
{
QTableWidgetItem* item = m_game_table->item(index, GameColumns::GameIcon);
const QPixmap pixmap = item->data(Qt::UserRole).value<QPixmap>();
const QSize original_size = pixmap.size();
QPixmap new_pixmap = QPixmap(original_size);
new_pixmap.fill(m_game_icon_color);
QPainter painter(&new_pixmap);
if (!pixmap.isNull())
{
painter.drawPixmap(QPoint(0, 0), pixmap);
}
painter.end();
const QPixmap scaled = new_pixmap.scaled(m_game_icon_size, Qt::KeepAspectRatio, Qt::TransformationMode::SmoothTransformation);
item->setData(Qt::DecorationRole, scaled);
}
void trophy_manager_dialog::ResizeGameIcons()
{
if (m_game_combo->count() <= 0)
@ -469,10 +469,7 @@ void trophy_manager_dialog::ResizeGameIcons()
for (int i = 0; i < m_game_table->rowCount(); ++i)
{
QTableWidgetItem* item = m_game_table->item(i, GameColumns::GameIcon);
QPixmap pixmap = item->data(Qt::UserRole).value<QPixmap>();
QPixmap scaled = pixmap.scaled(m_game_icon_size, Qt::KeepAspectRatio, Qt::TransformationMode::SmoothTransformation);
item->setData(Qt::DecorationRole, scaled);
ResizeGameIcon(i);
}
ReadjustGameTable();
@ -568,7 +565,61 @@ void trophy_manager_dialog::ShowContextMenu(const QPoint& loc)
menu->exec(globalPos);
}
void trophy_manager_dialog::PopulateUI()
void trophy_manager_dialog::PopulateTrophyDB()
{
QDirIterator dir_iter(qstr(vfs::get(m_TROPHY_DIR)), QDir::Dirs | QDir::NoDotAndDotDot);
while (dir_iter.hasNext())
{
dir_iter.next();
std::string dirName = sstr(dir_iter.fileName());
LOG_TRACE(GENERAL, "Loading trophy dir: %s", dirName);
LoadTrophyFolderToDB(dirName);
}
}
void trophy_manager_dialog::PopulateGameTable()
{
m_game_table->setSortingEnabled(false); // Disable sorting before using setItem calls
m_game_table->clearContents();
m_game_table->setRowCount(m_trophies_db.size());
m_game_combo->clear();
for (int i = 0; i < m_trophies_db.size(); ++i)
{
const int all_trophies = m_trophies_db[i]->trop_usr->GetTrophiesCount();
const int unlocked_trophies = m_trophies_db[i]->trop_usr->GetUnlockedTrophiesCount();
const int percentage = 100 * unlocked_trophies / all_trophies;
const std::string icon_path = m_trophies_db[i]->path + "/ICON0.PNG";
const QString name = qstr(m_trophies_db[i]->game_name).simplified();
const QString progress = QString("%0% (%1/%2)").arg(percentage).arg(unlocked_trophies).arg(all_trophies);
m_game_combo->addItem(name, i);
// Load game icon
QPixmap icon;
if (!icon.load(qstr(icon_path)))
{
LOG_WARNING(GENERAL, "Could not load trophy game icon from path %s", icon_path);
}
custom_table_widget_item* icon_item = new custom_table_widget_item;
icon_item->setData(Qt::UserRole, icon);
m_game_table->setItem(i, GameColumns::GameIcon, icon_item);
m_game_table->setItem(i, GameColumns::GameName, new custom_table_widget_item(name));
m_game_table->setItem(i, GameColumns::GameProgress, new custom_table_widget_item(progress, Qt::UserRole, percentage));
ResizeGameIcon(i);
}
m_game_table->setSortingEnabled(true); // Enable sorting only after using setItem calls
gui::utils::resize_combo_box_view(m_game_combo);
}
void trophy_manager_dialog::PopulateTrophyTable()
{
if (m_game_combo->count() <= 0)
return;

View File

@ -54,7 +54,11 @@ class trophy_manager_dialog : public QWidget
public:
explicit trophy_manager_dialog(std::shared_ptr<gui_settings> gui_settings);
public Q_SLOTS:
void RepaintUI();
private Q_SLOTS:
void ResizeGameIcon(int index);
void ResizeGameIcons();
void ResizeTrophyIcons();
void ApplyFilter();
@ -66,10 +70,18 @@ private:
*/
bool LoadTrophyFolderToDB(const std::string& trop_name);
/** Fills UI with information.
/** Populate the trophy database */
void PopulateTrophyDB();
/** Fills game table with information.
Takes results from LoadTrophyFolderToDB and puts it into the UI.
*/
void PopulateUI();
void PopulateGameTable();
/** Fills trophy table with information.
Takes results from LoadTrophyFolderToDB and puts it into the UI.
*/
void PopulateTrophyTable();
void ReadjustGameTable();
void ReadjustTrophyTable();
@ -102,4 +114,5 @@ private:
QSize m_game_icon_size = QSize(m_game_icon_size_index, m_game_icon_size_index);
bool m_save_game_icon_size = false;
QSlider* m_game_icon_slider = nullptr;
QColor m_game_icon_color;
};