Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/rmove info item qstrings #595

Merged
merged 10 commits into from
Oct 15, 2024
26 changes: 22 additions & 4 deletions YUViewLib/src/common/Formatting.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ template <typename T> std::string to_string(const std::pair<T, T> &typePair)
return stream.str();
}

template <typename T> std::ostream &operator<<(std::ostream &stream, const std::vector<T> vec)
template <typename T> std::ostream &operator<<(std::ostream &stream, const std::vector<T> &vec)
{
stream << "[";
for (auto it = vec.begin(); it != vec.end(); it++)
Expand All @@ -64,7 +64,7 @@ template <typename T> std::ostream &operator<<(std::ostream &stream, const std::
return stream;
}

template <typename T> std::string to_string(const std::vector<T> vec)
template <typename T> std::string to_string(const std::vector<T> &vec)
{
std::ostringstream stream;
stream << vec;
Expand All @@ -73,7 +73,7 @@ template <typename T> std::string to_string(const std::vector<T> vec)

static std::ostream &operator<<(std::ostream &stream, const Size &size)
{
stream << "(" << size.width << "x" << size.height << ")";
stream << size.width << "x" << size.height;
return stream;
}

Expand All @@ -89,13 +89,31 @@ inline std::string to_string(const bool b)
return b ? "True" : "False";
}

template <typename T>
static std::ostream &operator<<(std::ostream &stream, const std::optional<T> &opt)
{
if (opt)
stream << opt.value();
else
stream << "NA";
return stream;
}

template <typename T> inline std::string to_string(const std::optional<T> &opt)
{
std::ostringstream stream;
stream << opt;
return stream.str();
}

inline std::string stringReplaceAll(std::string str, char value, char replacement)
{
std::replace(str.begin(), str.end(), value, replacement);
return str;
}

inline std::string stringReplaceAll(std::string str, std::initializer_list<char> values, char replacement)
inline std::string
stringReplaceAll(std::string str, std::initializer_list<char> values, char replacement)
{
std::replace_if(
str.begin(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,25 @@
#include <QString>

/*
* An info item has a name, a text and an optional toolTip. These are used to show them in the
* fileInfoWidget. For example: ["File Name", "file.yuv"] or ["Number Frames", "123"] Another option
* is to show a button. If the user clicks on it, the callback function infoListButtonPressed() for
* the corresponding playlist item is called.
* An info item has a name, a text and an optional description. These are used to show them in the
* fileInfoWidget. For example: ["File Name", "file.yuv"] or ["Number Frames", "123"].
*/
struct InfoItem
{
InfoItem(const QString &name,
const QString &text,
const QString &toolTip = QString(),
bool button = false,
int buttonID = -1)
: name(name), text(text), button(button), buttonID(buttonID), toolTip(toolTip)
std::string name{};
std::string text{};
std::string description{};

InfoItem(std::string &&name, std::string &&text) : name(std::move(name)), text(std::move(text)) {}
InfoItem(std::string &&name, std::string &&text, std::string &&description)
: name(std::move(name)), text(std::move(text)), description(std::move(description))
{
}
InfoItem(std::string_view name, std::string_view text) : name(name), text(text) {}
InfoItem(std::string_view name, std::string_view text, std::string_view description)
: name(name), text(text), description(description)
{
}
QString name{};
QString text{};
bool button{};
int buttonID{};
QString toolTip{};
};

struct InfoData
Expand Down
7 changes: 4 additions & 3 deletions YUViewLib/src/common/Typedef.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ struct Size
{
return this->width != other.width || this->height != other.height;
}
explicit operator bool() const { return this->isValid(); }
constexpr bool isValid() const { return this->width > 0 && this->height > 0; }
unsigned width{};
unsigned height{};
Expand Down Expand Up @@ -345,7 +346,7 @@ Q_DECLARE_METATYPE(recacheIndicator)
template <typename... Args> struct QNonConstOverload
{
template <typename R, typename T>
Q_DECL_CONSTEXPR auto operator()(R (T::*ptr)(Args...)) const Q_DECL_NOTHROW -> decltype(ptr)
Q_DECL_CONSTEXPR auto operator()(R (T::*ptr)(Args...)) const Q_DECL_NOTHROW->decltype(ptr)
{
return ptr;
}
Expand All @@ -358,7 +359,7 @@ template <typename... Args> struct QNonConstOverload
template <typename... Args> struct QConstOverload
{
template <typename R, typename T>
Q_DECL_CONSTEXPR auto operator()(R (T::*ptr)(Args...) const) const Q_DECL_NOTHROW -> decltype(ptr)
Q_DECL_CONSTEXPR auto operator()(R (T::*ptr)(Args...) const) const Q_DECL_NOTHROW->decltype(ptr)
{
return ptr;
}
Expand All @@ -375,7 +376,7 @@ template <typename... Args> struct QOverload : QConstOverload<Args...>, QNonCons
using QNonConstOverload<Args...>::of;
using QNonConstOverload<Args...>::operator();
template <typename R>
Q_DECL_CONSTEXPR auto operator()(R (*ptr)(Args...)) const Q_DECL_NOTHROW -> decltype(ptr)
Q_DECL_CONSTEXPR auto operator()(R (*ptr)(Args...)) const Q_DECL_NOTHROW->decltype(ptr)
{
return ptr;
}
Expand Down
16 changes: 8 additions & 8 deletions YUViewLib/src/decoder/decoderHM.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

#include <QLibrary>

#include <common/FileInfo.h>
#include <common/InfoItemAndData.h>
#include <decoder/decoderBase.h>
#include <decoder/externalHeader/libHMDecoder.h>
#include <video/yuv/videoHandlerYUV.h>
Expand All @@ -51,11 +51,11 @@ struct LibraryFunctionsHM
void (*libHMDec_set_SEI_Check)(libHMDec_context *, bool check_hash){};
void (*libHMDec_set_max_temporal_layer)(libHMDec_context *, int max_layer){};
libHMDec_error (*libHMDec_push_nal_unit)(libHMDec_context *decCtx,
const void * data8,
const void *data8,
int length,
bool eof,
bool & bNewPicture,
bool & checkOutputPictures){};
bool &bNewPicture,
bool &checkOutputPictures){};

// Get a picture and retrive information on the picture
libHMDec_picture *(*libHMDec_get_picture)(libHMDec_context *){};
Expand All @@ -77,8 +77,8 @@ struct LibraryFunctionsHM
libHMDec_BlockValue *(*libHMDEC_get_internal_info)(libHMDec_context *,
libHMDec_picture *pic,
unsigned int typeIdx,
unsigned int & nrValues,
bool & callAgain){};
unsigned int &nrValues,
bool &callAgain){};
libHMDec_error (*libHMDEC_clear_internal_info)(libHMDec_context *decCtx){};
};

Expand Down Expand Up @@ -107,7 +107,7 @@ class decoderHM : public decoderBaseSingleLib
private:
// A private constructor that creates an uninitialized decoder library.
// Used by checkLibraryFile to check if a file can be used as a this decoder.
decoderHM(){};
decoderHM() {};

// Return the possible names of the HM library
QStringList getLibraryNames() const override;
Expand Down Expand Up @@ -150,7 +150,7 @@ class decoderHM : public decoderBaseSingleLib
QByteArray currentOutputBuffer;
void copyImgToByteArray(
libHMDec_picture *src,
QByteArray & dst); // Copy the raw data from the de265_image source *src to the byte array
QByteArray &dst); // Copy the raw data from the de265_image source *src to the byte array
#endif

LibraryFunctionsHM lib;
Expand Down
2 changes: 1 addition & 1 deletion YUViewLib/src/decoder/decoderVTM.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

#include <QLibrary>

#include <common/FileInfo.h>
#include <common/InfoItemAndData.h>

#include "decoderBase.h"
#include "externalHeader/libVTMDecoder.h"
Expand Down
2 changes: 1 addition & 1 deletion YUViewLib/src/decoder/decoderVVDec.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

#include <QLibrary>

#include <common/FileInfo.h>
#include <common/InfoItemAndData.h>
#include <decoder/decoderBase.h>
#include <decoder/externalHeader/vvdec/vvdec.h>
#include <video/yuv/videoHandlerYUV.h>
Expand Down
66 changes: 42 additions & 24 deletions YUViewLib/src/filesource/FileSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "FileSource.h"

#include <common/Formatting.h>
#include <common/Typedef.h>

#include <QDateTime>
Expand All @@ -55,26 +56,21 @@ FileSource::FileSource()
&FileSource::fileSystemWatcherFileChanged);
}

bool FileSource::openFile(const QString &filePath)
bool FileSource::openFile(const std::filesystem::path &filePath)
{
// Check if the file exists
this->fileInfo.setFile(filePath);
if (!this->fileInfo.exists() || !this->fileInfo.isFile())
if (!std::filesystem::is_regular_file(filePath))
return false;

if (this->isFileOpened && this->srcFile.isOpen())
this->srcFile.close();

// open file for reading
this->srcFile.setFileName(filePath);
this->srcFile.setFileName(QString::fromStdString(filePath.string()));
this->isFileOpened = this->srcFile.open(QIODevice::ReadOnly);
if (!this->isFileOpened)
return false;

// Save the full file path
this->fullFilePath = filePath;

// Install a watcher for the file (if file watching is active)
this->updateFileWatchSetting();
this->fileChanged = false;

Expand Down Expand Up @@ -115,30 +111,52 @@ int64_t FileSource::readBytes(QByteArray &targetBuffer, int64_t startPos, int64_
return this->srcFile.read(targetBuffer.data(), nrBytes);
}

QList<InfoItem> FileSource::getFileInfoList() const
std::vector<InfoItem> FileSource::getFileInfoList() const
{
QList<InfoItem> infoList;

if (!this->isFileOpened)
return infoList;
return {};

// For now we still use the QFileInfo. There is no easy cross platform formatting
// for the std::filesystem::file_time_type. This is added in C++ 20.
QFileInfo fileInfo(QString::fromStdString(this->fullFilePath.string()));

infoList.append(InfoItem("File Path", this->fileInfo.absoluteFilePath()));
std::vector<InfoItem> infoList;

infoList.emplace_back("File Path", this->fullFilePath.string());
#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
auto createdtime = this->fileInfo.created().toString("yyyy-MM-dd hh:mm:ss");
const auto createdtime = this->fileInfo.created().toString("yyyy-MM-dd hh:mm:ss");
#else
auto createdtime = this->fileInfo.birthTime().toString("yyyy-MM-dd hh:mm:ss");
const auto createdtime = fileInfo.birthTime().toString("yyyy-MM-dd hh:mm:ss");
#endif
infoList.append(InfoItem("Time Created", createdtime));
infoList.append(
InfoItem("Time Modified", this->fileInfo.lastModified().toString("yyyy-MM-dd hh:mm:ss")));
infoList.append(InfoItem("Nr Bytes", QString("%1").arg(this->fileInfo.size())));
infoList.emplace_back("Time Created", createdtime.toStdString());
infoList.emplace_back("Time Modified",
fileInfo.lastModified().toString("yyyy-MM-dd hh:mm:ss").toStdString());

if (const auto size = this->getFileSize())
infoList.emplace_back("Nr Bytes", to_string(this->getFileSize()));

return infoList;
}

QString FileSource::getAbsoluteFilePath() const
std::optional<int64_t> FileSource::getFileSize() const
{
if (!this->isFileOpened)
return {};

try
{
const auto size = std::filesystem::file_size(this->fullFilePath);
return static_cast<int64_t>(size);
}
catch (const std::filesystem::filesystem_error &e)
{
return {};
}
}

std::string FileSource::getAbsoluteFilePath() const
{
return this->isFileOpened ? this->fileInfo.absoluteFilePath() : QString();
return this->isFileOpened ? this->fullFilePath.string() : "";
}

// If you are loading a playlist and you have an absolute path and a relative path, this function
Expand Down Expand Up @@ -177,9 +195,9 @@ void FileSource::updateFileWatchSetting()
// The addPath/removePath functions will do nothing if called twice for the same file.
QSettings settings;
if (settings.value("WatchFiles", true).toBool())
fileWatcher.addPath(this->fullFilePath);
fileWatcher.addPath(QString::fromStdString(this->fullFilePath.string()));
else
fileWatcher.removePath(this->fullFilePath);
fileWatcher.removePath(QString::fromStdString(this->fullFilePath.string()));
}

void FileSource::clearFileCache()
Expand All @@ -195,7 +213,7 @@ void FileSource::clearFileCache()
QMutexLocker locker(&this->readMutex);
this->srcFile.close();

LPCWSTR file = (const wchar_t *)this->fullFilePath.utf16();
LPCWSTR file = this->fullFilePath.wstring().c_str();
HANDLE hFile =
CreateFile(file, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, NULL);
CloseHandle(hFile);
Expand Down
24 changes: 12 additions & 12 deletions YUViewLib/src/filesource/FileSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@
#include <QMutexLocker>
#include <QString>

#include <common/FileInfo.h>
#include <common/EnumMapper.h>
#include <common/InfoItemAndData.h>
#include <common/Typedef.h>

#include <filesystem>

enum class InputFormat
{
Invalid = -1,
Expand Down Expand Up @@ -70,14 +72,13 @@ class FileSource : public QObject
public:
FileSource();

virtual bool openFile(const QString &filePath);
virtual bool openFile(const std::filesystem::path &filePath);

virtual QList<InfoItem> getFileInfoList() const;
int64_t getFileSize() const { return !isFileOpened ? -1 : fileInfo.size(); }
QString getAbsoluteFilePath() const;
QFileInfo getFileInfo() const { return this->fileInfo; }
QFile *getQFile() { return &this->srcFile; }
bool getAndResetFileChangedFlag();
virtual std::vector<InfoItem> getFileInfoList() const;
std::optional<int64_t> getFileSize() const;
std::string getAbsoluteFilePath() const;
QFile *getQFile() { return &this->srcFile; }
bool getAndResetFileChangedFlag();

// Return true if the file could be opened and is ready for use.
bool isOk() const { return this->isFileOpened; }
Expand Down Expand Up @@ -107,10 +108,9 @@ private slots:
void fileSystemWatcherFileChanged(const QString &) { fileChanged = true; }

protected:
QString fullFilePath{};
QFileInfo fileInfo;
QFile srcFile;
bool isFileOpened{};
std::filesystem::path fullFilePath{};
QFile srcFile;
bool isFileOpened{};

private:
QFileSystemWatcher fileWatcher{};
Expand Down
Loading
Loading