Skip to content

Commit

Permalink
add talcs::Widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
CrSjimo committed Oct 26, 2024
1 parent eba4c49 commit 647af58
Show file tree
Hide file tree
Showing 21 changed files with 781 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ option(TALCS_REMOTE "Build talcs::Remote library" OFF)
option(TALCS_JUCE_ADAPTER "Build talcs::JuceAdapter library" OFF)
option(TALCS_DSPX "Build talcs::Dspx library" OFF)
option(TALCS_GUI "Build talcs::Gui library" OFF)
option(TALCS_WIDGETS "Build talcs::Widgets library" OFF)


# ----------------------------------
Expand Down Expand Up @@ -118,6 +119,9 @@ if(TALCS_BUILD_DOCUMENTATIONS)
if(TALCS_GUI)
list(APPEND _doc_targets TalcsGui)
endif()
if(TALCS_WIDGETS)
list(APPEND _doc_targets TalcsWidgets)
endif()

qm_import(Doxygen)
qm_setup_doxygen(talcs_RunDoxygen
Expand Down
4 changes: 4 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ if(TALCS_GUI)
add_subdirectory(gui)
endif()

if(TALCS_WIDGETS)
add_subdirectory(widgets)
endif()

if(TALCS_INSTALL)
# Add install target
set(_install_dir ${CMAKE_INSTALL_LIBDIR}/cmake/${TALCS_INSTALL_NAME})
Expand Down
9 changes: 6 additions & 3 deletions src/format/io/AudioFormatIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,14 @@ namespace talcs {
*
* @see AudioFormatIO()
*/
void AudioFormatIO::setStream(QIODevice *stream) {
void AudioFormatIO::setStream(QIODevice *stream, qint64 offset) {
Q_D(AudioFormatIO);
if (d->openMode) {
qWarning() << "AudioFormatIO: Cannot set stream when AudioFormatIO is open.";
return;
}
d->stream = stream;
d->streamOffset = offset;
}

/**
Expand All @@ -95,13 +96,15 @@ namespace talcs {
}

int64_t AudioFormatIOPrivate::sfVioGetFilelen() const {
return stream->size();
return stream->size() - streamOffset;
}
int64_t AudioFormatIOPrivate::sfVioSeek(int64_t offset, int whence) const {
if (whence == SF_SEEK_CUR)
offset += stream->pos();
else if (whence == SF_SEEK_END)
offset += stream->size();
else if (whence == SF_SEEK_SET)
offset += streamOffset;
if (offset != stream->pos()) {
if (!stream->seek(offset))
return -1;
Expand All @@ -115,7 +118,7 @@ namespace talcs {
return stream->write(static_cast<const char *>(ptr), count);
}
int64_t AudioFormatIOPrivate::sfVioTell() const {
return stream->pos();
return stream->pos() - streamOffset;
}
static SF_VIRTUAL_IO sfVio = {
[](void *d) { return static_cast<AudioFormatIOPrivate *>(d)->sfVioGetFilelen(); },
Expand Down
2 changes: 1 addition & 1 deletion src/format/io/AudioFormatIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace talcs {

~AudioFormatIO() override;

void setStream(QIODevice *stream);
void setStream(QIODevice *stream, qint64 offset = 0);
QIODevice *stream() const;

enum MajorFormat {
Expand Down
2 changes: 2 additions & 0 deletions src/format/io/AudioFormatIO_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ namespace talcs {

double compressionLevel = 0.0;

qint64 streamOffset = 0;

int64_t sfVioGetFilelen() const;
int64_t sfVioSeek(int64_t offset, int whence) const;
int64_t sfVioRead(void *ptr, int64_t count) const;
Expand Down
2 changes: 2 additions & 0 deletions src/format/io/WavpackAudioFormatIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ namespace talcs {
auto ctx = WavpackOpenFileInputEx64(&wpStreamReader, d->stream, d->corrStream, err, flags, 0);
if (!ctx) {
qWarning() << "WavpackAudioFormatIO: Failed to open" << err;
setErrorString(err);
return false;
}
d->context = ctx;
Expand All @@ -174,6 +175,7 @@ namespace talcs {
d->context = nullptr;
}
d->openMode = NotOpen;
clearErrorString();
}

int WavpackAudioFormatIO::format() const {
Expand Down
3 changes: 2 additions & 1 deletion src/format/io/WavpackAudioFormatIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <QScopedPointer>

#include <TalcsCore/ErrorStringProvider.h>
#include <TalcsFormat/AbstractAudioFormatIO.h>

class QFileDevice;
Expand All @@ -30,7 +31,7 @@ namespace talcs {

class WavpackAudioFormatIOPrivate;

class TALCSFORMAT_EXPORT WavpackAudioFormatIO : public AbstractAudioFormatIO {
class TALCSFORMAT_EXPORT WavpackAudioFormatIO : public AbstractAudioFormatIO, public ErrorStringProvider {
Q_DECLARE_PRIVATE(WavpackAudioFormatIO)
public:
explicit WavpackAudioFormatIO(QFileDevice *stream = nullptr, QFileDevice *corrStream = nullptr);
Expand Down
32 changes: 23 additions & 9 deletions src/format/manager/FormatManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
******************************************************************************/

#include "FormatManager.h"

#include <QFileInfo>

#include "FormatManager_p.h"

#include <set>
Expand Down Expand Up @@ -70,17 +73,28 @@ namespace talcs {
return d->extensionHintDict.value(extension);
}

talcs::AbstractAudioFormatIO *FormatManager::getFormatLoad(const QString &filename, const QVariant &userData) const {
talcs::AbstractAudioFormatIO *FormatManager::getFormatLoad(const QString &filename, const QVariant &userData, const QString &entryClassName) const {
Q_D(const FormatManager);
auto extension = (filename + ".").split(".").last();
auto hintedEntry = extension.isEmpty() ? nullptr : hintFromExtension(extension);
if (hintedEntry) {
if (auto io = hintedEntry->getFormatLoad(filename, userData))
return io;
auto entries = d->entries;
{
auto extension = QFileInfo(filename).suffix();
auto hintedEntry = extension.isEmpty() ? nullptr : hintFromExtension(extension);
if (hintedEntry) {
entries.removeOne(hintedEntry);
entries.prepend(hintedEntry);
}
}
if (!entryClassName.isEmpty()) {
auto specifiedEntry = std::find_if(d->entries.cbegin(), d->entries.cend(), [entryClassName](FormatEntry *entry) {
return entry->metaObject()->className() == entryClassName;
});
if (specifiedEntry != d->entries.cend()) {
auto entry = *specifiedEntry;
entries.removeOne(entry);
entries.prepend(entry);
}
}
for (auto entry : entries()) {
if (entry == hintedEntry)
continue;
for (auto entry : entries) {
if (auto io = entry->getFormatLoad(filename, userData))
return io;
}
Expand Down
3 changes: 2 additions & 1 deletion src/format/manager/FormatManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <TalcsFormat/TalcsFormatGlobal.h>

#include <QObject>
#include <QVariant>

namespace talcs {

Expand All @@ -46,7 +47,7 @@ namespace talcs {

FormatEntry *hintFromExtension(const QString &extension) const;

talcs::AbstractAudioFormatIO *getFormatLoad(const QString &filename, const QVariant &userData) const;
talcs::AbstractAudioFormatIO *getFormatLoad(const QString &filename, const QVariant &userData = {}, const QString &entryClassName = {}) const;

private:
QScopedPointer<FormatManagerPrivate> d_ptr;
Expand Down
10 changes: 10 additions & 0 deletions src/widgets/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
project(TalcsWidgets VERSION ${TALCS_VERSION} LANGUAGES CXX)

file(GLOB_RECURSE _src *.h *.cpp)

talcs_add_library(${PROJECT_NAME} AUTOGEN
SOURCES ${_src}
QT_LINKS Core Gui Widgets
LINKS talcs::Core talcs::Format talcs::Device
QT_INCLUDE_PRIVATE Core Gui Widgets
)
37 changes: 37 additions & 0 deletions src/widgets/TalcsWidgetsGlobal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/******************************************************************************
* Copyright (c) 2024 CrSjimo *
* *
* This file is part of TALCS. *
* *
* TALCS is free software: you can redistribute it and/or modify it under the *
* terms of the GNU Lesser General Public License as published by the Free *
* Software Foundation, either version 3 of the License, or (at your option) *
* any later version. *
* *
* TALCS 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 GNU Lesser General Public License for *
* more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with TALCS. If not, see <https://www.gnu.org/licenses/>. *
******************************************************************************/

#ifndef TALCS_TALCSWIDGETSGLOBAL_H
#define TALCS_TALCSWIDGETSGLOBAL_H

#include <QtGlobal>

#ifndef TALCSWIDGETS_EXPORT
# ifdef TALCSWIDGETS_STATIC
# define TALCSWIDGETS_EXPORT
# else
# ifdef TALCSWIDGETS_LIBRARY
# define TALCSWIDGETS_EXPORT Q_DECL_EXPORT
# else
# define TALCSWIDGETS_EXPORT Q_DECL_IMPORT
# endif
# endif
#endif

#endif //TALCS_TALCSWIDGETSGLOBAL_H
76 changes: 76 additions & 0 deletions src/widgets/dialogs/AudioFileDialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/******************************************************************************
* Copyright (c) 2024 CrSjimo *
* *
* This file is part of TALCS. *
* *
* TALCS is free software: you can redistribute it and/or modify it under the *
* terms of the GNU Lesser General Public License as published by the Free *
* Software Foundation, either version 3 of the License, or (at your option) *
* any later version. *
* *
* TALCS 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 GNU Lesser General Public License for *
* more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with TALCS. If not, see <https://www.gnu.org/licenses/>. *
******************************************************************************/

#include "AudioFileDialog.h"

#include <algorithm>

#include <QFileDialog>

#include <TalcsFormat/FormatManager.h>
#include <TalcsFormat/FormatEntry.h>

#include <TalcsWidgets/StandardFormatEntry.h>

namespace talcs {
AbstractAudioFormatIO *AudioFileDialog::getOpenAudioFileIO(const FormatManager *formatManager,
QString &fileName, QVariant &userData, QString &entryClassName,
QWidget *parent, const QString &caption, const QString &dir) {
QStringList extensionList = formatManager->extensionHints();
std::transform(extensionList.cbegin(), extensionList.cend(), extensionList.begin(), [](const QString &s) {
return "*." + s;
});
auto allSupportedFilesFilter = tr("All Supported Files (%1)").arg(extensionList.join(" "));
auto rawDataFilter = tr("As Raw Data (*)");
auto allFilesFilter = tr("All Files (*)");

auto filters = QStringList{allSupportedFilesFilter} + formatManager->filters() + QStringList{rawDataFilter, allFilesFilter};

QString selectedFilter;
fileName = QFileDialog::getOpenFileName(parent, caption, dir, filters.join(";;"), &selectedFilter);
if (fileName.isEmpty()) {
return nullptr;
}
if (selectedFilter == rawDataFilter) {
StandardFormatEntry entry;
userData = QVariantMap({{"raw", true}});
if (auto io = entry.getFormatOpen(fileName, userData, parent)) {
entryClassName = StandardFormatEntry::staticMetaObject.className();
return io;
}
return nullptr;
}
auto entries = formatManager->entries();
{
auto extension = QFileInfo(fileName).suffix();
auto hintedEntry = extension.isEmpty() ? nullptr : formatManager->hintFromExtension(extension);
if (hintedEntry) {
entries.removeOne(hintedEntry);
entries.prepend(hintedEntry);
}
}
for (auto entry : entries) {
if (auto io = entry->getFormatOpen(fileName, userData, parent)) {
entryClassName = entry->metaObject()->className();
return io;
}
}
return nullptr;
}
}
44 changes: 44 additions & 0 deletions src/widgets/dialogs/AudioFileDialog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/******************************************************************************
* Copyright (c) 2024 CrSjimo *
* *
* This file is part of TALCS. *
* *
* TALCS is free software: you can redistribute it and/or modify it under the *
* terms of the GNU Lesser General Public License as published by the Free *
* Software Foundation, either version 3 of the License, or (at your option) *
* any later version. *
* *
* TALCS 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 GNU Lesser General Public License for *
* more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with TALCS. If not, see <https://www.gnu.org/licenses/>. *
******************************************************************************/

#ifndef TALCS_AUDIOFILEDIALOG_H
#define TALCS_AUDIOFILEDIALOG_H

#include <QVariant>
#include <QObject>

#include <TalcsWidgets/TalcsWidgetsGlobal.h>

namespace talcs {

class AbstractAudioFormatIO;

class FormatManager;

class TALCSWIDGETS_EXPORT AudioFileDialog : public QObject {
Q_OBJECT
public:
static AbstractAudioFormatIO *getOpenAudioFileIO(const FormatManager *formatManager,
QString &fileName, QVariant &userData, QString &entryClassName,
QWidget *parent = nullptr, const QString &caption = {}, const QString &dir = {});
};

} // talcs

#endif //TALCS_AUDIOFILEDIALOG_H
Loading

0 comments on commit 647af58

Please sign in to comment.