~mrp/aerodramus

a5a5950f4ee8fced16965fa5c0ac035fdcf4ced6 — Mark Penner 7 months ago eff88e9
start implementing a model to keep track of open files (not functional yet)
M src/CMakeLists.txt => src/CMakeLists.txt +1 -0
@@ 11,6 11,7 @@ qt_add_qml_module(aerodramus-gui URI mrp.aerodramus NO_RESOURCE_TARGET_PATH
    SOURCES
        app.h
        audiofile.cpp
        filelistmodel.cpp
)

add_library(aerodramus STATIC

M src/audiofile.cpp => src/audiofile.cpp +32 -7
@@ 7,11 7,42 @@
#include <QList>
#include <QPointF>

QLineSeries *AudioFile::getWaveSeries() const
{
    return m_waveSeries;
}
void AudioFile::setWaveSeries(QLineSeries *series)
{
    m_waveSeries = series;
    Q_EMIT waveSeriesChanged();
}

int AudioFile::getLength() const
{
    return m_samples;
}

QUrl AudioFile::getFileUrl() const
{
    return m_fileUrl;
}
void AudioFile::setFileUrl(QUrl fileUrl)
{
    m_fileUrl = fileUrl;
    m_name = m_fileUrl.path();
}

QString AudioFile::getName() const
{
    return m_name;
}

void AudioFile::readFile()
{
    m_data = Aerodramus::read_file(m_fileName.path().toStdString());
    m_data = Aerodramus::read_file(m_fileUrl.path().toStdString());
    m_samples = m_data.size();
}

void AudioFile::graphData()
{
    readFile();


@@ 24,9 55,3 @@ void AudioFile::graphData()
    m_waveSeries->replace(line);
    Q_EMIT waveSeriesChanged();
}

void AudioFile::setWaveSeries(QLineSeries *series)
{
    m_waveSeries = series;
    Q_EMIT waveSeriesChanged();
}

M src/audiofile.h => src/audiofile.h +18 -23
@@ 7,43 7,38 @@
#include <QLineSeries>
#include <QObject>
#include <QUrl>
#include <qqmlintegration.h>
#include <QtQmlIntegration>

class AudioFile : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QLineSeries *waveSeries READ getWaveSeries WRITE setWaveSeries NOTIFY waveSeriesChanged)
    Q_PROPERTY(int length READ getLength NOTIFY waveSeriesChanged)
    Q_PROPERTY(QUrl fileName READ getFileName WRITE setFileName)
    Q_PROPERTY(int length READ getLength)
    Q_PROPERTY(QUrl fileUrl READ getFileUrl WRITE setFileUrl)
    Q_PROPERTY(QString name READ getName)
    QML_ELEMENT
public:
    QLineSeries *getWaveSeries() const
    {
        return m_waveSeries;
    }
    int getLength() const
    {
        return m_samples;
    }
    QUrl getFileName() const
    {
        return m_fileName;
    }
    void setFileName(QUrl fileName)
    {
        m_fileName = fileName;
    }
    void readFile();
    QLineSeries *getWaveSeries() const;
    void setWaveSeries(QLineSeries *series);

public Q_SLOTS:
    void graphData();
    int getLength() const;

    QUrl getFileUrl() const;
    void setFileUrl(QUrl fileUrl);

    QString getName() const;

    void readFile();

    Q_INVOKABLE void graphData();

Q_SIGNALS:
    void waveSeriesChanged();

private:
    std::vector<float> m_data{};
    QUrl m_fileName{};
    QUrl m_fileUrl{};
    QString m_name;
    QLineSeries *m_waveSeries{};
    int m_samples{};
};

A src/filelistmodel.cpp => src/filelistmodel.cpp +48 -0
@@ 0,0 1,48 @@
// SPDX-FileCopyrightText: 2023 Mark Penner <mrpenner@mailbox.org>
// SPDX-License-Identifier: GPL-3.0-or-later

#include "filelistmodel.h"

FileListModel::FileListModel(QObject *parent)
    : QAbstractListModel::QAbstractListModel(parent)
{
}

QVariant FileListModel::data(const QModelIndex &index, int role) const
{
    switch (role) {
    case Qt::DisplayRole: {
        return m_data.at(index.row());
        break;
    }
    case NameRole: {
        return m_data.at(index.row());
        break;
    }
    default:
        return QVariant();
    }
}

QHash<int, QByteArray> FileListModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[NameRole] = "name";
    return roles;
}

int FileListModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid()) {
        return 0;
    }
    return m_data.length();
}

bool FileListModel::append(QString string)
{
    Q_EMIT beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_data.append(string);
    Q_EMIT endInsertRows();
    return true;
}

A src/filelistmodel.h => src/filelistmodel.h +29 -0
@@ 0,0 1,29 @@
// SPDX-FileCopyrightText: 2023 Mark Penner <mrpenner@mailbox.org>
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef AERODRAMUS_FILELISTMODEL_H
#define AERODRAMUS_FILELISTMODEL_H

#include <QAbstractListModel>
#include <QObject>
#include <QtQmlIntegration>

class FileListModel : public QAbstractListModel
{
    Q_OBJECT
    QML_ELEMENT
public:
    FileListModel(QObject *parent = nullptr);

    enum Roles { NameRole = Qt::UserRole };

    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    QHash<int, QByteArray> roleNames() const override;
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;

    Q_INVOKABLE bool append(QString string);

private:
    QList<QString> m_data{};
};
#endif

M src/qml/AudioFile.qml => src/qml/AudioFile.qml +3 -2
@@ 9,7 9,8 @@ import mrp.aerodramus

Kirigami.Page {

    property alias fileName: aFile.fileName
    property alias fileUrl: aFile.fileUrl
    property alias name: aFile.name

    ChartView {
        id: chart


@@ 41,7 42,7 @@ Kirigami.Page {
    }

    Component.onCompleted: {
        console.log("graphing ", fileName);
        console.log("graphing ", name);
        aFile.graphData();
    }
}

M src/qml/main.qml => src/qml/main.qml +10 -3
@@ 49,6 49,9 @@ Kirigami.ApplicationWindow {
        ]
    }

    FileListModel {
        id: fileListModel
    }
    pageStack.initialPage: navPage
    Kirigami.ScrollablePage {
        id: navPage


@@ 61,8 64,11 @@ Kirigami.ApplicationWindow {
                text: i18n("No views yet")
                helpfulAction: openAction
            }
            model: 0
            delegate: QQC.Label{text: model.index}
            model: fileListModel
            delegate: QQC.Label{
                required property string name
                text: name
            }
        }
    }
    function openFilePage(fileUrl) {


@@ 70,6 76,7 @@ Kirigami.ApplicationWindow {
            pageStack.currentIndex = 1;
            pageStack.pop();
        }
        root.mainPage = pageStack.push("qrc:/qml/AudioFile.qml", {fileName: fileUrl});
        root.mainPage = pageStack.push("qrc:/qml/AudioFile.qml", {fileUrl: fileUrl});
        fileListModel.append(root.mainPage.name);
    }
}