Skip to content

Commit

Permalink
GUI: Use original resource info system
Browse files Browse the repository at this point in the history
  • Loading branch information
fdde authored and DrMcCoy committed Dec 27, 2017
1 parent 3643b5a commit 5cfb8db
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 48 deletions.
4 changes: 4 additions & 0 deletions src/common/ustring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ const char *UString::c_str() const {
return _string.c_str();
}

QString UString::toQString() const {
return QString::fromStdString(_string);
}

UString::iterator UString::begin() const {
return iterator(_string.begin(), _string.begin(), _string.end());
}
Expand Down
4 changes: 4 additions & 0 deletions src/common/ustring.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#ifndef COMMON_USTRING_H
#define COMMON_USTRING_H

#include <QString>

#include <string>
#include <sstream>
#include <vector>
Expand Down Expand Up @@ -131,6 +133,8 @@ class UString {
/** Return the (utf8 encoded) string data. */
const char *c_str() const;

QString toQString() const;

iterator begin() const;
iterator end() const;

Expand Down
144 changes: 99 additions & 45 deletions src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*
* Phaethon is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MERCHANTABILIT Y or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
Expand All @@ -31,17 +31,22 @@
#include "mainwindow.h"
#include "ui/ui_mainwindow.h"

#include "src/common/filepath.h"
#include "src/common/strutil.h"
#include "src/gui/mainwindow.h"
#include "src/aurora/util.h"

namespace GUI {

W_OBJECT_IMPL(MainWindow)

MainWindow::MainWindow(QWidget *parent) :
MainWindow::MainWindow(QWidget *parent, const char *version, QSize size, QString path) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

connect(this, &MainWindow::open, this, &MainWindow::setTreeViewModel);
connect(this, &MainWindow::openDir, this, &MainWindow::setTreeViewModel);
connect(ui->actionOpen_directory, &QAction::triggered, this, &MainWindow::on_actionOpen_directory_triggered);
connect(ui->actionClose, &QAction::triggered, this, &MainWindow::on_actionClose_triggered);
connect(ui->actionQuit, &QAction::triggered, this, &MainWindow::on_actionQuit_triggered);
Expand All @@ -50,6 +55,11 @@ MainWindow::MainWindow(QWidget *parent) :
connect(ui->pushButton_3, &QPushButton::clicked, this, &MainWindow::on_pushButton_3_clicked);
connect(ui->pushButton_4, &QPushButton::clicked, this, &MainWindow::on_pushButton_4_clicked);

if (size != QSize())
resize(size);

connect(this, &MainWindow::openDir, this, &MainWindow::setTreeViewModel);

statusLabel = new QLabel(this);
statusLabel->setText("None");
statusLabel->setAlignment(Qt::AlignLeft);
Expand All @@ -68,28 +78,26 @@ MainWindow::MainWindow(QWidget *parent) :
resLabels << ui->resLabel2;
resLabels << ui->resLabel3;
resLabels << ui->resLabel4;
resLabels[0]->setText("Resource name: -");
resLabels[1]->setText("Size: -");
resLabels[2]->setText("File type: -");
resLabels[3]->setText("Resource type: -");
resLabels[0]->setText("Resource name:");
resLabels[1]->setText("Size:");
resLabels[2]->setText("File type:");
resLabels[3]->setText("Resource type:");

if (!path.isEmpty())
emit openDir(path);
}

MainWindow::~MainWindow() {
delete ui;
}

void MainWindow::setTreeViewModel(const QString &path) {
QString cPath;

cPath = QDir(path).canonicalPath();
QString cPath = QDir(path).canonicalPath();
fsModel.setRootPath(cPath);

ui->treeView->setModel(&fsModel);

connect(ui->treeView->selectionModel(),
&QItemSelectionModel::selectionChanged,
this,
&MainWindow::selection);
connect(ui->treeView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &MainWindow::selection);

for (int i = 1; i < fsModel.columnCount(); i++)
ui->treeView->hideColumn(i);
Expand All @@ -110,18 +118,20 @@ void MainWindow::on_actionOpen_directory_triggered() {
QString(QStandardPaths::HomeLocation),
QFileDialog::ShowDirsOnly);
if (!dir.isEmpty())
emit open(dir);
emit openDir(dir);
}

void MainWindow::on_actionClose_triggered() {
ui->treeView->setModel(nullptr);

resLabels[0]->setText("Resource name: -");
resLabels[1]->setText("Size: -");
resLabels[2]->setText("File type: -");
resLabels[3]->setText("Resource type: -");
resLabels[0]->setText("Resource name:");
resLabels[1]->setText("Size:");
resLabels[2]->setText("File type:");
resLabels[3]->setText("Resource type:");

statusLabel->setText("None");

clearLabels();
}

void MainWindow::on_actionQuit_triggered() {
Expand All @@ -133,7 +143,7 @@ void MainWindow::on_pushButton_1_clicked() {
QString myKotorPath("/home/mike/kotor");
QDir dir(myKotorPath);
if (dir.exists())
emit open(myKotorPath);
emit openDir(myKotorPath);
}

void MainWindow::on_pushButton_2_clicked() {
Expand All @@ -146,39 +156,83 @@ void MainWindow::on_pushButton_3_clicked() {
void MainWindow::on_pushButton_4_clicked() {
}

// ty stackoverflow
QString size_human(qint64 size)
{
float num = size;
QStringList list;
list << "KB" << "MB" << "GB" << "TB";
QString getSizeLabel(size_t size) {
if (size == Common::kFileInvalid)
return "-";

QStringListIterator i(list);
QString unit("bytes");
if (size < 1024)
return QString("%1").arg(size);

while(num >= 1024.0 && i.hasNext())
{
unit = i.next();
num /= 1024.0;
}
return QString().setNum(num,'f',2)+" "+unit;
QString humanRead = Common::FilePath::getHumanReadableSize(size).toQString();

return QString("%1 (%2)").arg(humanRead).arg(size);
}

void MainWindow::selection(const QItemSelection &selected) {
QModelIndex index = selected.indexes().at(0);
QString getFileTypeLabel(Aurora::FileType type) {
QString label = QString("%1").arg(type);

resLabels[0]->setText(tr("Resource name: %1").arg(fsModel.fileName(index)));
if (type != Aurora::kFileTypeNone) {
QString temp = TypeMan.getExtension(type).toQString();
label += QString(" (%1)").arg(temp);
}

if (fsModel.isDir(index)) {
resLabels[1]->setText("Size: -");
resLabels[2]->setText("File type: Directory");
resLabels[3]->setText("Resource type: Directory");
return label;
}

QString getResTypeLabel(Aurora::ResourceType type) {
QString label = QString("%1").arg(type);

if (type != Aurora::kResourceNone) {
QString temp = getResourceTypeDescription(type).toQString();
label += QString(" (%1)").arg(temp);
}
else {
resLabels[1]->setText(tr("Size: %1").arg(size_human(fsModel.size(index))));
resLabels[2]->setText("File type: -"); // TODO
resLabels[3]->setText("Resource type: -"); // TODO

return label;
}

void MainWindow::setLabels() {
QString labelName = "Resource name: ";
QString labelSize = "Size: ";
QString labelFileType = "File type: ";
QString labelResType = "Resource type: ";

labelName += _currentSelection.fileName();

if (_currentSelection.getSource() == Source::kSourceDirectory) {

labelSize += "-";
labelFileType += "Directory";
labelResType += "Directory";

} else if ((_currentSelection.getSource() == Source::kSourceFile) ||
(_currentSelection.getSource() == Source::kSourceArchiveFile)) {

Aurora::FileType fileType = _currentSelection.getFileType();
Aurora::ResourceType resType = _currentSelection.getResourceType();

labelSize += getSizeLabel(_currentSelection.size());
labelFileType += getFileTypeLabel(fileType);
labelResType += getResTypeLabel(resType);
}

resLabels[0]->setText(labelName);
resLabels[1]->setText(labelSize);
resLabels[2]->setText(labelFileType);
resLabels[3]->setText(labelResType);
}


void MainWindow::clearLabels() {
resLabels[0]->setText("Resource name:");
resLabels[1]->setText("Size:");
resLabels[2]->setText("File type:");
resLabels[3]->setText("Resource type:");
}

void MainWindow::selection(const QItemSelection &selected) {
const QModelIndex index = selected.indexes().at(0);
_currentSelection = Selection(fsModel.fileInfo(index));
setLabels();
}

} // End of namespace GUI
12 changes: 9 additions & 3 deletions src/gui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

#include "verdigris/wobjectdefs.h"

#include "src/gui/selection.h"

namespace Ui {
class MainWindow;
}
Expand All @@ -43,12 +45,12 @@ class MainWindow : public QMainWindow {
W_OBJECT(MainWindow)

public:
explicit MainWindow(QWidget *parent = 0);
explicit MainWindow(QWidget *parent = 0, const char *version = "", QSize size = QSize(), QString path = QString());
~MainWindow();

// signals:
void open(const QString &path)
W_SIGNAL(open, path)
void openDir(const QString &path)
W_SIGNAL(openDir, path)

// public slots:
void setTreeViewModel(const QString &path);
Expand All @@ -65,12 +67,16 @@ class MainWindow : public QMainWindow {

void selection(const QItemSelection &selected);

void setLabels();
void clearLabels();

private:
Ui::MainWindow *ui;
QFileSystemModel fsModel;
QLabel *statusLabel;
QProgressBar *statusProgressBar;
QList<QLabel*> resLabels;
Selection _currentSelection;
};

} // End of namespace GUI
Expand Down
2 changes: 2 additions & 0 deletions src/gui/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ src_gui_libgui_la_SOURCES += \
src/gui/panelpreviewempty.h \
src/gui/panelpreviewsound.h \
src/gui/panelpreviewimage.h \
src/gui/selection.h \
$(QT_UI_FORMS_BUILT) \
$(EMPTY)

Expand All @@ -49,6 +50,7 @@ src_gui_libgui_la_SOURCES += \
src/gui/panelpreviewempty.cpp \
src/gui/panelpreviewsound.cpp \
src/gui/panelpreviewimage.cpp \
src/gui/selection.cpp \
$(EMPTY)

EXTRA_src_gui_libgui_la_DEPENDENCIES = \
Expand Down

0 comments on commit 5cfb8db

Please sign in to comment.