Skip to content

Commit

Permalink
[ModTabWidget] Now has progress bars and a sidebar.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed May 7, 2020
1 parent 41e812e commit 8668ef9
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 133 deletions.
4 changes: 4 additions & 0 deletions source/DatabaseLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ void DatabaseLoader::update() const {
emit updateStarted();

const auto &repositoryList = m_data.repositoryList();
if (repositoryList.empty()) {
// TODO: Create default repositories here
}

for (auto &it : repositoryList) {
emit updateProgressed(0);
emit stateChanged("Loading users from repository '" + it.second.name() + "'...");
Expand Down
46 changes: 3 additions & 43 deletions source/EngineVersionTabWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
#include <QProgressBar>
#include <QTreeWidgetItemIterator>

#include <quazip/quazipfile.h>

#include "ContentData.hpp"
#include "EngineVersionTabWidget.hpp"
#include "PathUtils.hpp"
Expand Down Expand Up @@ -101,6 +99,8 @@ void EngineVersionTabWidget::update() {

for (int i = 0 ; i < 5 ; ++i)
m_versionListWidget.resizeColumnToContents(i);

toggleButtons();
}

void EngineVersionTabWidget::downloadActionTriggered() {
Expand Down Expand Up @@ -162,47 +162,7 @@ void EngineVersionTabWidget::unzipFile(QNetworkReply *reply) {
return;
}

QuaZip archive{path + "/content.zip"};
if (!archive.open(QuaZip::mdUnzip)) {
qDebug() << "Failed to open archive. Error code:" << archive.getZipError();
return;
}

for(bool f = archive.goToFirstFile(); f; f = archive.goToNextFile()) {
QString filePath = archive.getCurrentFileName();

if (filePath.at(filePath.size() - 1) == '/') {
QDir dir;
dir.mkpath(path + QDir::separator() + filePath);
}
}

for(bool f = archive.goToFirstFile(); f; f = archive.goToNextFile()) {
QString filePath = archive.getCurrentFileName();

if (filePath.at(filePath.size() - 1) != '/') {
QuaZipFile file(archive.getZipName(), filePath);
file.open(QIODevice::ReadOnly);

QuaZipFileInfo info;
file.getFileInfo(&info);

QByteArray data = file.readAll();

file.close();

QFile dstFile(path + QDir::separator() + filePath);
dstFile.open(QIODevice::WriteOnly);
dstFile.write(data);
dstFile.setPermissions(info.getPermissions());
dstFile.close();
}
}

archive.close();

QFile file{path + "/content.zip"};
file.remove();
PathUtils::unzipFile(path + "/content.zip", true);

engineVersion->setState(ContentEngineVersion::State::Downloaded);

Expand Down
183 changes: 95 additions & 88 deletions source/ModTabWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,43 @@
*/
#include <QDir>
#include <QMenu>
#include <QNetworkReply>
#include <QProgressBar>
#include <QVBoxLayout>

#include <quazip/quazipfile.h>

#include "ContentData.hpp"
#include "ModTabWidget.hpp"
#include "PathUtils.hpp"

ModTabWidget::ModTabWidget(ContentData &data, QWidget *parent) : QWidget(parent), m_data(data) {
m_modListWidget.setHeaderLabels({"", tr("ID"), tr("Name"), tr("Author"), tr("Latest installed"), tr("Latest version"), tr("Repository"), tr("Creation date"), tr("Updated")});
// m_modListWidget.setRootIsDecorated(false);
m_modListWidget.setHeaderLabels({"", tr("ID"), tr("Name"), tr("Author"), tr("Latest installed"), tr("Latest version"), tr("Repository"), tr("Creation date"), tr("Updated"), "", ""});
m_modListWidget.setSortingEnabled(true);
m_modListWidget.setContextMenuPolicy(Qt::CustomContextMenu);
m_modListWidget.sortItems(2, Qt::AscendingOrder);
m_modListWidget.setColumnWidth(0, 64);
m_modListWidget.setColumnWidth(9, 32);
m_modListWidget.hideColumn(1);
m_modListWidget.setSelectionMode(QAbstractItemView::NoSelection);
m_modListWidget.setFocusPolicy(Qt::NoFocus);

connect(&m_modListWidget, &QTreeWidget::customContextMenuRequested, this, &ModTabWidget::showContextMenu);
connect(&m_modListWidget, &QTreeWidget::itemSelectionChanged, this, &ModTabWidget::toggleButtons);

m_installButton = new QPushButton{tr("Install")};
m_removeButton = new QPushButton{tr("Remove")};

m_installButton->setDisabled(true);
m_removeButton->setDisabled(true);

QVBoxLayout *layout = new QVBoxLayout{this};
connect(m_installButton, &QPushButton::clicked, this, &ModTabWidget::downloadActionTriggered);
connect(m_removeButton, &QPushButton::clicked, this, &ModTabWidget::removeActionTriggered);

auto *sideBar = new QWidget;
auto *buttonLayout = new QVBoxLayout{sideBar};
buttonLayout->addWidget(m_installButton);
buttonLayout->addWidget(m_removeButton);
buttonLayout->addWidget(new QWidget, 1);

QHBoxLayout *layout = new QHBoxLayout{this};
layout->addWidget(&m_modListWidget);
layout->addWidget(sideBar);
}

void ModTabWidget::update() {
Expand All @@ -56,7 +70,6 @@ void ModTabWidget::update() {
auto &modList = m_data.modList();
for (auto &it : modList) {
auto *item = new QTreeWidgetItem(&m_modListWidget);
// item->setText(0, " 0");
item->setText(1, QString::number(it.second.id()));
item->setText(2, it.second.name());
item->setText(7, it.second.date().toString());
Expand All @@ -73,6 +86,10 @@ void ModTabWidget::update() {
if (user)
item->setText(3, user->name());

auto *progressBar = new QProgressBar;
progressBar->setRange(0, 100);
m_modListWidget.setItemWidget(item, 9, progressBar);

ContentModVersion *latestVersion = nullptr;
ContentModVersion *latestInstalledVersion = nullptr;
for (auto &it : it.second.versions()) {
Expand All @@ -83,12 +100,19 @@ void ModTabWidget::update() {
child->setIcon(0, QIcon(":/checkbox_off"));
else if (version->state() == ContentModVersion::State::Downloaded)
child->setIcon(0, QIcon(":/checkbox_on"));
else
child->setIcon(0, QIcon(":/ask"));

child->setText(1, " " + QString::number(version->id()));
child->setText(1, " " + QString::number(version->id()));
child->setText(2, " " + version->name());
child->setText(7, " " + version->date().toString());
child->setText(8, " " + QString(version->hasBeenUpdated() ? "true" : "false"));

auto *progressBar = new QProgressBar;
progressBar->setRange(0, 100);
m_modListWidget.setItemWidget(child, 9, progressBar);

if (!latestVersion || latestVersion->id() < version->id())
latestVersion = version;

Expand All @@ -107,6 +131,11 @@ void ModTabWidget::update() {
if (latestVersion)
item->setText(5, latestVersion->name());
}

for (int i = 1 ; i < 9 ; ++i)
m_modListWidget.resizeColumnToContents(i);

toggleButtons();
}

ContentModVersion *ModTabWidget::getModVersionFromItem(QTreeWidgetItem *item) {
Expand All @@ -132,106 +161,84 @@ ContentModVersion *ModTabWidget::getModVersionFromItem(QTreeWidgetItem *item) {
return modVersion;
}

void ModTabWidget::showContextMenu(const QPoint &pos) {
QTreeWidgetItem *item = m_modListWidget.itemAt(pos);
if (!item) return;

m_currentItem = item;

ContentModVersion *modVersion = getModVersionFromItem(m_currentItem);
if (modVersion) {
QMenu menu{this};

if (modVersion->state() == ContentModVersion::State::Available) {
QAction *downloadAction = new QAction(tr("&Download"), this);
downloadAction->setStatusTip(tr("Download mod"));
connect(downloadAction, &QAction::triggered, this, &ModTabWidget::downloadActionTriggered);
menu.addAction(downloadAction);
}
else if (modVersion->state() == ContentModVersion::State::Downloaded) {
QAction *removeAction = new QAction(tr("&Remove"), this);
removeAction->setStatusTip(tr("Remove mod from disk"));
connect(removeAction, &QAction::triggered, this, &ModTabWidget::removeActionTriggered);
menu.addAction(removeAction);
void ModTabWidget::downloadActionTriggered() {
QList<QTreeWidgetItem *> selectedItems = m_modListWidget.selectedItems();
if (selectedItems.size() == 1) {
ContentModVersion *modVersion = getModVersionFromItem(selectedItems.at(0));

if (modVersion) {
QNetworkReply *reply = m_session.downloadRequest(modVersion->doc());
connect(reply, &QNetworkReply::finished, [this, reply]() { unzipFile(reply); });
connect(reply, &QNetworkReply::downloadProgress, this, [this, reply](qint64 bytesReceived, qint64 bytesTotal) {
updateProgressBar(reply, bytesReceived, bytesTotal);
});

m_downloads.emplace(reply, selectedItems.at(0));
}

menu.exec(m_modListWidget.mapToGlobal(pos));
}
}

void ModTabWidget::downloadActionTriggered() {
ContentModVersion *modVersion = getModVersionFromItem(m_currentItem);
ContentMod *mod = m_data.getMod(modVersion->modID());
if (modVersion) {
QString path = PathUtils::getModVersionPath(*mod, *modVersion);

QDir dir;
if (!dir.exists(path))
dir.mkpath(path);
void ModTabWidget::removeActionTriggered() {
QList<QTreeWidgetItem *> selectedItems = m_modListWidget.selectedItems();
if (selectedItems.size() == 1) {
ContentModVersion *modVersion = getModVersionFromItem(selectedItems.at(0));

if (m_session.download(modVersion->doc(), path + "/content.zip")) {
QuaZip archive{path + "/content.zip"};
if (!archive.open(QuaZip::mdUnzip)) {
qDebug() << "ERROR: Failed to open archive at" << path + "/content.zip";
return;
}
if (modVersion) {
QString path = PathUtils::getModVersionPath(modVersion->mod(), *modVersion);

for(bool f = archive.goToFirstFile(); f; f = archive.goToNextFile()) {
QString filePath = archive.getCurrentFileName();
QDir dir{path};
if (dir.exists())
dir.removeRecursively();

if (filePath.at(filePath.size() - 1) == '/') {
QDir dir;
dir.mkpath(path + QDir::separator() + filePath);
}
}
modVersion->setState(ContentModVersion::State::Available);

for(bool f = archive.goToFirstFile(); f; f = archive.goToNextFile()) {
QString filePath = archive.getCurrentFileName();
update();
}
}
}

if (filePath.at(filePath.size() - 1) != '/') {
QuaZipFile file(archive.getZipName(), filePath);
file.open(QIODevice::ReadOnly);
void ModTabWidget::updateProgressBar(QNetworkReply *reply, qint64 bytesReceived, qint64 bytesTotal) {
QProgressBar *progressBar = (QProgressBar *)m_modListWidget.itemWidget(m_downloads.at(reply), 5);
if (progressBar)
progressBar->setValue((float)bytesReceived / bytesTotal * 100.f);
}

QuaZipFileInfo info;
file.getFileInfo(&info);
void ModTabWidget::unzipFile(QNetworkReply *reply) {
ContentModVersion *modVersion = getModVersionFromItem(m_downloads.at(reply));

QByteArray data = file.readAll();
if (modVersion) {
QString path = PathUtils::getModVersionPath(modVersion->mod(), *modVersion);

file.close();
QDir dir;
if (!dir.exists(path))
dir.mkpath(path);

QFile dstFile(path + QDir::separator() + filePath);
dstFile.open(QIODevice::WriteOnly);
dstFile.write(data);
dstFile.setPermissions(info.getPermissions());
dstFile.close();
}
}
if (!m_session.saveFileToDisk(reply, path + "/content.zip")) {
qDebug() << "Failed to save" << path + "/content.zip";
return;
}

archive.close();
PathUtils::unzipFile(path + "/content.zip", true);

QFile file{path + "/content.zip"};
file.remove();
modVersion->setState(ContentModVersion::State::Downloaded);

modVersion->setState(ContentModVersion::State::Downloaded);
update();

update();
}
m_downloads.erase(reply);
}
}

void ModTabWidget::removeActionTriggered() {
ContentModVersion *modVersion = getModVersionFromItem(m_currentItem);
ContentMod *mod = m_data.getMod(modVersion->modID());
if (modVersion) {
QString path = PathUtils::getModVersionPath(*mod, *modVersion);
void ModTabWidget::toggleButtons() {
QList<QTreeWidgetItem *> selectedItems = m_modListWidget.selectedItems();
if (selectedItems.size() == 1) {
ContentModVersion *modVersion = getModVersionFromItem(selectedItems.at(0));

QDir dir{path};
if (dir.exists())
dir.removeRecursively();

modVersion->setState(ContentModVersion::State::Available);

update();
m_installButton->setEnabled(modVersion->state() != ContentModVersion::State::Downloaded);
m_removeButton->setEnabled(modVersion->state() == ContentModVersion::State::Downloaded);
}
else {
m_installButton->setEnabled(false);
m_removeButton->setEnabled(false);
}
}

15 changes: 13 additions & 2 deletions source/ModTabWidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
#ifndef MODTABWIDGET_HPP_
#define MODTABWIDGET_HPP_

#include <unordered_map>

#include <QPushButton>
#include <QTreeWidget>

#include "Session.hpp"
Expand All @@ -45,20 +48,28 @@ class ModTabWidget : public QWidget {
void windowRefeshRequested();

private:
void showContextMenu(const QPoint &pos);

ContentModVersion *getModVersionFromItem(QTreeWidgetItem *item);

void downloadActionTriggered();
void removeActionTriggered();

void updateProgressBar(QNetworkReply *reply, qint64 bytesReceived, qint64 bytesTotal);
void unzipFile(QNetworkReply *reply);

void toggleButtons();

ContentData &m_data;

QTreeWidget m_modListWidget;

QTreeWidgetItem *m_currentItem = nullptr;

Session m_session;

QPushButton *m_installButton = nullptr;
QPushButton *m_removeButton = nullptr;

std::unordered_map<QNetworkReply *, QTreeWidgetItem *> m_downloads;
};

#endif // MODTABWIDGET_HPP_
Loading

0 comments on commit 8668ef9

Please sign in to comment.