当前位置:网站首页>List of QT players [easy to understand]

List of QT players [easy to understand]

2022-07-01 12:28:00 Full stack programmer webmaster

Hello everyone , I meet you again , I'm your friend, Quan Jun .

The list only needs to display the string . Choose to use QListView

Now that it's used View There must be a need for Model. The Model Need to store

  • Current playback index
  • Added media link
  • Play mode

First step , rewrite QAbstractItemModel The virtual function

    // QAbstractItemModel interface
public:
    QVariant data(const QModelIndex &index, int role) const;
    int rowCount(const QModelIndex &parent=QModelIndex()) const;
    int columnCount(const QModelIndex &parent=QModelIndex()) const;

    QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const;
    QModelIndex parent(const QModelIndex &child=QModelIndex()) const;
    bool setData(const QModelIndex &index, const QVariant &value, int role);
    bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex());

private:
    QList<QMediaContent> m_data;
QVariant MediaListModel::data(const QModelIndex &index, int role) const
{
    if(index.isValid() && role == Qt::DisplayRole)
    {
        if(index.row()>=0 && index.row()<m_data.size() && index.column()==0)
        {
            return  QFileInfo(m_data.at(index.row()).canonicalUrl().path()).fileName();
        }
    }
    return QVariant();
}

int MediaListModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return m_data.size();
}


int MediaListModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return 1;
}

bool MediaListModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    Q_UNUSED(role);
    if(index.isValid() && value.isValid() && index.row()>=0 &&index.row()<m_data.size() && index.column()==0){
        m_data.replace(index.row(),value.toUrl());
        emit dataChanged(index,index);
        return true;
    }
    return false;
}

QModelIndex MediaListModel::index(int row, int column, const QModelIndex &parent) const
{
    return row>=0 &&
            row<m_data.size() &&
            column==0 &&
            !parent.isValid()
            ?createIndex(row,column)
           :QModelIndex();
}

QModelIndex MediaListModel::parent(const QModelIndex &child) const
{
    Q_UNUSED(child);
    return QModelIndex();
}

bool MediaListModel::removeRows(int row, int count, const QModelIndex &parent)
{
    Q_UNUSED(parent);
    if(row+count<=m_data.size() && row>=0 &&count>=0)
    {
        for(int i=0;i<count;i++)
        {
            m_data.removeAt(row);
        }
        return true;
    }
    return false;
}

Add data and query data

    void insert(const QUrl &url);
    QMediaContent media(const QModelIndex &index)const;

void MediaListModel::insert(const QUrl &url)
{
    if(url.isValid()){
        m_data.append(url);
        QModelIndex index=createIndex(m_data.size()-1,0);
        emit dataChanged(index,index);
    }
}

QMediaContent MediaListModel::media(const QModelIndex &index) const
{
    if(index.isValid() && index.row()>=0&&index.row()<m_data.size())
    {
        return m_data[index.row()];
    }
    return QMediaContent();
}

Then set the playback index and query index

    QModelIndex currentIndex()const;
    void setPlayIndex(int index);

QModelIndex MediaListModel::currentIndex() const
{
    return createIndex(m_index,0);
}

void MediaListModel::setPlayIndex(int index)
{
    m_index=index;
}

Set playback mode and query playback mode

enum PlaybackMode { CurrentItemOnce, CurrentItemInLoop, Sequential, Loop, Random };


void setPlaybackMode(PlaybackMode mode);
PlaybackMode playbackMode()const;


void MediaListModel::setPlaybackMode(MediaListModel::PlaybackMode mode)
{
    if(m_mode!=mode)
    {
        m_mode=mode;
    }
}

MediaListModel::PlaybackMode MediaListModel::playbackMode() const
{
    return m_mode;
}

Complete code :

#ifndef MEDIALISTMODEL_H
#define MEDIALISTMODEL_H

#include <QAbstractItemModel>
#include <QMediaContent>

class MediaListModel : public QAbstractItemModel
{
    Q_OBJECT
public:

    enum PlaybackMode { CurrentItemOnce, CurrentItemInLoop, Sequential, Loop, Random };

    explicit MediaListModel(QObject *parent = nullptr);
    void insert(const QUrl &url);
    QMediaContent media(const QModelIndex &index)const;
    void setPlaybackMode(PlaybackMode mode);
    PlaybackMode playbackMode()const;
    QModelIndex currentIndex()const;
    void setPlayIndex(int index);

    // QAbstractItemModel interface
public:
    QVariant data(const QModelIndex &index, int role) const;
    int rowCount(const QModelIndex &parent=QModelIndex()) const;
    int columnCount(const QModelIndex &parent=QModelIndex()) const;

    QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const;
    QModelIndex parent(const QModelIndex &child=QModelIndex()) const;
    bool setData(const QModelIndex &index, const QVariant &value, int role);
    bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex());


private:
    int m_index;
    PlaybackMode m_mode=CurrentItemOnce;
    QList<QMediaContent> m_data;

};

#endif // MEDIALISTMODEL_H
#include "medialistmodel.h"

#include <QFileInfo>
#include <QDebug>

MediaListModel::MediaListModel(QObject *parent) : QAbstractItemModel(parent)
{
    m_index=-1;
}

void MediaListModel::insert(const QUrl &url)
{
    if(url.isValid()){
        m_data.append(url);
        QModelIndex index=createIndex(m_data.size()-1,0);
        emit dataChanged(index,index);
    }
}

QMediaContent MediaListModel::media(const QModelIndex &index) const
{
    if(index.isValid() && index.row()>=0&&index.row()<m_data.size())
    {
        return m_data[index.row()];
    }
    return QMediaContent();
}

void MediaListModel::setPlaybackMode(MediaListModel::PlaybackMode mode)
{
    if(m_mode!=mode)
    {
        m_mode=mode;
    }
}

MediaListModel::PlaybackMode MediaListModel::playbackMode() const
{
    return m_mode;
}

QModelIndex MediaListModel::currentIndex() const
{
    return createIndex(m_index,0);
}

void MediaListModel::setPlayIndex(int index)
{
    m_index=index;
}

QVariant MediaListModel::data(const QModelIndex &index, int role) const
{
    if(index.isValid() && role == Qt::DisplayRole)
    {
        if(index.row()>=0 && index.row()<m_data.size() && index.column()==0)
        {
            return  QFileInfo(m_data.at(index.row()).canonicalUrl().path()).fileName();
        }
    }
    return QVariant();
}

int MediaListModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return m_data.size();
}


int MediaListModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return 1;
}

bool MediaListModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    Q_UNUSED(role);
    if(index.isValid() && value.isValid() && index.row()>=0 &&index.row()<m_data.size() && index.column()==0){
        m_data.replace(index.row(),value.toUrl());
        emit dataChanged(index,index);
        return true;
    }
    return false;
}

QModelIndex MediaListModel::index(int row, int column, const QModelIndex &parent) const
{
    return row>=0 &&
            row<m_data.size() &&
            column==0 &&
            !parent.isValid()
            ?createIndex(row,column)
           :QModelIndex();
}

QModelIndex MediaListModel::parent(const QModelIndex &child) const
{
    Q_UNUSED(child);
    return QModelIndex();
}

bool MediaListModel::removeRows(int row, int count, const QModelIndex &parent)
{
    Q_UNUSED(parent);
    if(row+count<=m_data.size() && row>=0 &&count>=0)
    {
        for(int i=0;i<count;i++)
        {
            m_data.removeAt(row);
        }
        return true;
    }
    return false;
}

The list requires the following functions

  • Double click to play the video
  • Right click to play the video
  • Add files
  • Add folder
  • Load playlist
  • Save playlist
  • Delete the selection
  • Clear playlist
  • Empty invalid files
  • Set playback mode
  • Open the file directory

Set the menu , The playback mode menu is mutually exclusive ~

    QMenu *m_menu;
    QString m_playModeKey[5];
    MediaListModel::PlaybackMode m_playModeValue[5];
    QAction *m_play;
    QAction *m_deleteSelect;
    QAction *m_openFolder;


    setEditTriggers(QAbstractItemView::NoEditTriggers);
    setSelectionMode(QAbstractItemView::ExtendedSelection);
    setContextMenuPolicy(Qt::CustomContextMenu);

    m_playModeKey[0] = QStringLiteral(" Order of play ");
    m_playModeKey[1] = QStringLiteral(" Single play ");
    m_playModeKey[2] = QStringLiteral(" Random broadcast ");
    m_playModeKey[3] = QStringLiteral(" Single cycle ");
    m_playModeKey[4] = QStringLiteral(" List loop ");

    m_playModeValue[0]=MediaListModel::Sequential;
    m_playModeValue[1]=MediaListModel::CurrentItemOnce;
    m_playModeValue[2]=MediaListModel::Random;
    m_playModeValue[3]=MediaListModel::CurrentItemInLoop;
    m_playModeValue[4]=MediaListModel::Loop;

    m_menu = new QMenu();
    m_play = m_menu->addAction(QStringLiteral(" Play "));
    auto addFile = m_menu->addAction(QStringLiteral(" Add files "));
    auto addFolder = m_menu->addAction(QStringLiteral(" Add folder "));
    auto load = m_menu->addAction(QStringLiteral(" Load playlist "));
    auto save = m_menu->addAction(QStringLiteral(" Save playlist "));
    m_deleteSelect = m_menu->addAction(QStringLiteral(" Delete the selection "));
    auto clearPlayList = m_menu->addAction(QStringLiteral(" Clear playlist "));
    auto clearInvalidfiles = m_menu->addAction(QStringLiteral(" Empty invalid files "));
    auto loopMode = m_menu->addMenu(QStringLiteral(" Circulation patterns "));
    m_openFolder = m_menu->addAction(QStringLiteral(" Open the file directory "));

    QAction* loopActions[5];
    loopActions[0] = loopMode->addAction(m_playModeKey[0]);
    loopActions[1] =loopMode->addAction(m_playModeKey[1]);
    loopActions[2] =loopMode->addAction(m_playModeKey[2]);
    loopActions[3] =loopMode->addAction(m_playModeKey[3]);
    loopActions[4] =loopMode->addAction(m_playModeKey[4]);

    QActionGroup *group = new QActionGroup(this);
    for(int i=0;i<5;i++)
    {
        loopActions[i]->setCheckable(true);
        group->addAction(loopActions[i]);
    }

    QSettings setting("./Setting.ini",QSettings::IniFormat);
    int index = setting.value("PlaybackModel",1).toInt();
    loopActions[index]->setChecked(true);

    loadPlayList("default.list");

    connect(m_play,&QAction::triggered,this,&MediaListView::play);
    connect(addFile,&QAction::triggered,this,&MediaListView::addFile);
    connect(addFolder,&QAction::triggered,this,&MediaListView::addFolder);
    connect(load,&QAction::triggered,this,&MediaListView::loadPlaylist);
    connect(save,&QAction::triggered,this,&MediaListView::savePlaylist);
    connect(m_deleteSelect,&QAction::triggered,this,&MediaListView::deleteSelect);
    connect(clearPlayList,&QAction::triggered,this,&MediaListView::clearPlaylist);
    connect(clearInvalidfiles,&QAction::triggered,this,&MediaListView::clearInvalidFiles);
    connect(loopMode,&QMenu::triggered,this,&MediaListView::loopMode);
    connect(m_openFolder,&QAction::triggered,this,&MediaListView::openFolder);
    connect(this,&MediaListView::customContextMenuRequested,this,&MediaListView::customContext);

Rewrite the custom menu function , If no index is selected when right clicking , Need to play , Delete , The open directory is set to non clickable

void MediaListView::customContext(const QPoint &pos)
{
    bool isEnable=!selectionModel()->selectedIndexes().isEmpty();

    m_play->setEnabled(isEnable);
    m_deleteSelect->setEnabled(isEnable);
    m_openFolder->setEnabled(isEnable);

    m_menu->exec(QCursor::pos());
}

insert data , be used for VideoWidget Button insert

void MediaListView::insert(const QUrl &url)
{
    m_model->insert(url);
    auto index = QModelIndex();
    index = m_model->index(m_model->rowCount(index)-1,0,index);
    setCurrentIndex(index);
}

Select the folder , All multimedia files under the convenience folder , Insert the path into the list

void MediaListView::addFolder()
{
    QString path = QFileDialog::getExistingDirectory(nullptr,"Select Video Directory");
    QDir dir(path);
    QStringList filter = m_fileFilter?m_fileFilter->getFilterAll():QStringList();
    auto list = dir.entryInfoList(filter,QDir::Files|QDir::Readable, QDir::Name);
    foreach (auto p, list)
    {
       m_model->insert(QUrl::fromLocalFile(p.filePath()));
    }
}

Read list , Simple file reading

void MediaListView::loadPlaylist()
{
    auto fileName = QFileDialog::getOpenFileName(nullptr,"Select list","","*.list");
    if(fileName.isEmpty())
    {
        return;
    }
    loadPlayList(fileName);
}

void MediaListView::loadPlayList(QString fileName)
{
    QFile file(fileName);
    if(!file.open(QIODevice::Text|QIODevice::ReadOnly))
    {
        qDebug()<<QString("not open listInfo file ");
        return;
    }
    QTextStream ts(&file);
    while(!ts.atEnd())
    {
       m_model->insert(ts.readLine());
    }
    file.close();
}

Save list , Simple file writing

void MediaListView::savePlaylist()
{
    auto fileName = QFileDialog::getSaveFileName(nullptr,"save list","","*.list");
    if(fileName.isEmpty())
    {
        return;
    }
    savePlayList(fileName);
}

void MediaListView::savePlayList(QString fileName)
{
    QFile file(fileName);
    if(!file.open(QIODevice::Text|QIODevice::WriteOnly))
    {
        qDebug()<<QString("not create listInfo file ");
       return;
    }
    QTextStream ts(&file);
    auto modelIndex = QModelIndex();
    int count=m_model->rowCount(modelIndex);
    int index=0;

    while(index<count)
    {
        ts<<m_model->media(m_model->index(index,0,modelIndex)).canonicalUrl().toString()<<endl;
        index++;
    }
    file.close();
}

Delete the selection , When deleting, you need to delete from the following index ,3,2,1 , If you delete the currently playing , Need to end the play

bool modelSort(const QModelIndex &a, const QModelIndex &b)
{
    return a.row()> b.row();
}
void MediaListView::deleteSelect()
{
    auto indexs = selectedIndexes();
    qSort(indexs.begin(),indexs.end(),modelSort);
    foreach (auto index, indexs) {
        model()->removeRow(index.row());
        if(index ==m_model->currentIndex())
        {
            emit deleteIndex();
        }
    }
}

clear list , Delete all items directly , And notify the end of playing

void MediaListView::clearPlaylist()
{
    model()->removeRows(0,model()->rowCount(QModelIndex()),QModelIndex());
    emit deleteIndex();
}

Empty useless items , Simply judge whether the file exists

void MediaListView::clearInvalidFiles()
{
    auto index = QModelIndex();
    int count =m_model->rowCount(index);

    for(int i=count-1;i>=0;i--)
    {
        auto url =m_model->media(m_model->index(i,0,index)).canonicalUrl();
        if(url.isLocalFile())
        {
            auto fileName = url.toString();
            fileName.replace("file:///","");
            QFile file(fileName);
            if(!file.exists())
            {
               m_model->removeRow(i);
            }
        }
    }
}

Open the file directory , Use QDesktopServices Open Explorer

void MediaListView::openFolder()
{
    auto url =m_model->media(currentIndex()).canonicalUrl();
    if(url.isLocalFile())
    {
        QFileInfo info(url.path());
        QDesktopServices::openUrl(QUrl::fromLocalFile( info.dir().path()));
    }
}

Set playback mode , Save the settings to Setting.ini file

void MediaListView::loopMode(QAction *action)
{
    auto text = action->text();

    for(int i=0;i<5;i++)
    {
        if(text == m_playModeKey[i])
        {
            m_model->setPlaybackMode(m_playModeValue[i]);
            QSettings setting("./Setting.ini",QSettings::IniFormat);
            setting.setValue("PlaybackModel",i);
            break;
        }
    }
}

Load when the window is initialized Setting.ini file

    QSettings setting("./Setting.ini",QSettings::IniFormat);
    int index = setting.value("PlaybackModel",1).toInt();
    loopActions[index]->setChecked(true);

Complete code

#ifndef MEDIALISTVIEW_H
#define MEDIALISTVIEW_H

#include <QListView>
#include <QMediaPlayer>
#include "medialistmodel.h"

QT_BEGIN_NAMESPACE
class QAction;
class FileFilter;
class MediaListModel;
QT_END_NAMESPACE

class MediaListView : public QListView
{
    Q_OBJECT
public:
    explicit MediaListView(QWidget *parent = nullptr);
    void setFileFilter(FileFilter *filter);
    void insert(const QUrl &url);
    void setPlayIndex(const QModelIndex &index);
    QMediaContent media(const QModelIndex &index)const;
signals:
    void playIndex(const QModelIndex &index);
    void deleteIndex();

public slots:
    void next();
    void previous();
    void mediaStatusChanged(QMediaPlayer::MediaStatus status);

private slots:
    void customContext(const QPoint &pos);
    void play();
    void addFile();
    void addFolder();
    void loadPlaylist();
    void savePlaylist();
    void deleteSelect();
    void clearPlaylist();
    void clearInvalidFiles();
    void loopMode(QAction *action);
    void openFolder();
    void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int>());

    // QWidget interface
protected:
    void mousePressEvent(QMouseEvent *event);

private:
    void loadPlayList(QString fileName);
    void savePlayList(QString fileName);

private:
    QMenu *m_menu;
    QString m_playModeKey[5];
    MediaListModel::PlaybackMode m_playModeValue[5];
    QAction *m_play;
    QAction *m_deleteSelect;
    QAction *m_openFolder;

    MediaListModel *m_model;
    FileFilter *m_fileFilter;
};

#endif // MEDIALISTVIEW_H
#include "medialistview.h"
#include <QMenu>
#include <QMouseEvent>
#include <QDebug>
#include <QAction>
#include <QFileDialog>
#include <QDesktopServices>
#include <QSettings>
#include "filefilter.h"

MediaListView::MediaListView(QWidget *parent) : QListView(parent)
{
    m_model = new MediaListModel(this);
    this->setModel(m_model);

    setEditTriggers(QAbstractItemView::NoEditTriggers);
    setSelectionMode(QAbstractItemView::ExtendedSelection);
    setContextMenuPolicy(Qt::CustomContextMenu);

    m_playModeKey[0] = QStringLiteral(" Order of play ");
    m_playModeKey[1] = QStringLiteral(" Single play ");
    m_playModeKey[2] = QStringLiteral(" Random broadcast ");
    m_playModeKey[3] = QStringLiteral(" Single cycle ");
    m_playModeKey[4] = QStringLiteral(" List loop ");

    m_playModeValue[0]=MediaListModel::Sequential;
    m_playModeValue[1]=MediaListModel::CurrentItemOnce;
    m_playModeValue[2]=MediaListModel::Random;
    m_playModeValue[3]=MediaListModel::CurrentItemInLoop;
    m_playModeValue[4]=MediaListModel::Loop;

    m_menu = new QMenu();
    m_play = m_menu->addAction(QStringLiteral(" Play "));
    auto addFile = m_menu->addAction(QStringLiteral(" Add files "));
    auto addFolder = m_menu->addAction(QStringLiteral(" Add folder "));
    auto load = m_menu->addAction(QStringLiteral(" Load playlist "));
    auto save = m_menu->addAction(QStringLiteral(" Save playlist "));
    m_deleteSelect = m_menu->addAction(QStringLiteral(" Delete the selection "));
    auto clearPlayList = m_menu->addAction(QStringLiteral(" Clear playlist "));
    auto clearInvalidfiles = m_menu->addAction(QStringLiteral(" Empty invalid files "));
    auto loopMode = m_menu->addMenu(QStringLiteral(" Circulation patterns "));
    m_openFolder = m_menu->addAction(QStringLiteral(" Open the file directory "));

    QAction* loopActions[5];
    loopActions[0] = loopMode->addAction(m_playModeKey[0]);
    loopActions[1] =loopMode->addAction(m_playModeKey[1]);
    loopActions[2] =loopMode->addAction(m_playModeKey[2]);
    loopActions[3] =loopMode->addAction(m_playModeKey[3]);
    loopActions[4] =loopMode->addAction(m_playModeKey[4]);

    QActionGroup *group = new QActionGroup(this);
    for(int i=0;i<5;i++)
    {
        loopActions[i]->setCheckable(true);
        group->addAction(loopActions[i]);
    }

    QSettings setting("./Setting.ini",QSettings::IniFormat);
    int index = setting.value("PlaybackModel",1).toInt();
    loopActions[index]->setChecked(true);

    loadPlayList("default.list");

    connect(m_play,&QAction::triggered,this,&MediaListView::play);
    connect(addFile,&QAction::triggered,this,&MediaListView::addFile);
    connect(addFolder,&QAction::triggered,this,&MediaListView::addFolder);
    connect(load,&QAction::triggered,this,&MediaListView::loadPlaylist);
    connect(save,&QAction::triggered,this,&MediaListView::savePlaylist);
    connect(m_deleteSelect,&QAction::triggered,this,&MediaListView::deleteSelect);
    connect(clearPlayList,&QAction::triggered,this,&MediaListView::clearPlaylist);
    connect(clearInvalidfiles,&QAction::triggered,this,&MediaListView::clearInvalidFiles);
    connect(loopMode,&QMenu::triggered,this,&MediaListView::loopMode);
    connect(m_openFolder,&QAction::triggered,this,&MediaListView::openFolder);
    connect(this,&MediaListView::customContextMenuRequested,this,&MediaListView::customContext);
    connect(m_model,&MediaListModel::dataChanged,this,&MediaListView::dataChanged);
}

void MediaListView::setFileFilter(FileFilter *filter)
{
    m_fileFilter = filter;
}

void MediaListView::insert(const QUrl &url)
{
    m_model->insert(url);
    auto index = QModelIndex();
    index = m_model->index(m_model->rowCount(index)-1,0,index);
    setCurrentIndex(index);
}

void MediaListView::setPlayIndex(const QModelIndex &index)
{
    m_model->setPlayIndex(index.row());
}

QMediaContent MediaListView::media(const QModelIndex &index) const
{
    return m_model->media(index);
}

void MediaListView::next()
{
    auto index =m_model->currentIndex();
    index = model()->index(index.row()+1,0);

    if(index.row()==-1 &&m_model->playbackMode() == MediaListModel::Loop)
    {
        index=m_model->index(0,0);
    }

    setCurrentIndex(index);
    emit playIndex(index);
}

void MediaListView::previous()
{
    auto index =m_model->currentIndex();
    index = model()->index(index.row()-1,0);

    if(index.row()==-1 &&m_model->playbackMode() == MediaListModel::Loop)
    {
        index=model()->index(model()->rowCount(QModelIndex())-1,0);
    }

    setCurrentIndex(index);
    emit playIndex(index);
}

void MediaListView::mediaStatusChanged(QMediaPlayer::MediaStatus status)
{
    if(status != QMediaPlayer::EndOfMedia)
    {
        return;
    }
    QModelIndex index =m_model->currentIndex();
    switch(m_model->playbackMode())
    {
    case MediaListModel::Random:
        do
        {
            index =m_model->index(rand()%m_model->rowCount(),0);
        }while(index ==m_model->currentIndex());
    case MediaListModel::CurrentItemInLoop:
        setCurrentIndex(index);
        emit playIndex(index);
        break;
    case MediaListModel::Sequential:
    case MediaListModel::Loop:
        next();
        break;
    }
}

void MediaListView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles)
{
    Q_UNUSED(topLeft);
    Q_UNUSED(bottomRight);
    Q_UNUSED(roles);
    savePlayList("default.list");
}

void MediaListView::customContext(const QPoint &pos)
{
    bool isEnable=!selectionModel()->selectedIndexes().isEmpty();

    m_play->setEnabled(isEnable);
    m_deleteSelect->setEnabled(isEnable);
    m_openFolder->setEnabled(isEnable);

    m_menu->exec(QCursor::pos());
}

void MediaListView::play()
{
    emit playIndex(currentIndex());
}

void MediaListView::addFile()
{
    QString filter=m_fileFilter?m_fileFilter->getFilterString():QString();
    QUrl url = QFileDialog::getOpenFileUrl(nullptr,"select video file",QUrl(),filter);

    if(url.isLocalFile())
    {
        m_model->insert(url);
    }
}

void MediaListView::addFolder()
{
    QString path = QFileDialog::getExistingDirectory(nullptr,"Select Video Directory");
    QDir dir(path);
    QStringList filter = m_fileFilter?m_fileFilter->getFilterAll():QStringList();
    auto list = dir.entryInfoList(filter,QDir::Files|QDir::Readable, QDir::Name);
    foreach (auto p, list)
    {
        m_model->insert(QUrl::fromLocalFile(p.filePath()));
    }
}

void MediaListView::loadPlaylist()
{
    auto fileName = QFileDialog::getOpenFileName(nullptr,"Select list","","*.list");
    if(fileName.isEmpty())
    {
        return;
    }
    loadPlayList(fileName);
}

void MediaListView::savePlaylist()
{
    auto fileName = QFileDialog::getSaveFileName(nullptr,"save list","","*.list");
    if(fileName.isEmpty())
    {
        return;
    }
    savePlayList(fileName);
}

bool modelSort(const QModelIndex &a, const QModelIndex &b)
{
    return a.row()> b.row();
}
void MediaListView::deleteSelect()
{
    auto indexs = selectedIndexes();
    qSort(indexs.begin(),indexs.end(),modelSort);
    foreach (auto index, indexs) {
        model()->removeRow(index.row());
        if(index ==m_model->currentIndex())
        {
            emit deleteIndex();
        }
    }
}

void MediaListView::clearPlaylist()
{
    model()->removeRows(0,model()->rowCount(QModelIndex()),QModelIndex());
    emit deleteIndex();
}

void MediaListView::clearInvalidFiles()
{
    auto index = QModelIndex();
    int count =m_model->rowCount(index);

    for(int i=count-1;i>=0;i--)
    {
        auto url =m_model->media(m_model->index(i,0,index)).canonicalUrl();
        if(url.isLocalFile())
        {
            auto fileName = url.toString();
            fileName.replace("file:///","");
            QFile file(fileName);
            if(!file.exists())
            {
                m_model->removeRow(i);
            }
        }
    }
}

void MediaListView::loopMode(QAction *action)
{
    auto text = action->text();

    for(int i=0;i<5;i++)
    {
        if(text == m_playModeKey[i])
        {
            m_model->setPlaybackMode(m_playModeValue[i]);
            QSettings setting("./Setting.ini",QSettings::IniFormat);
            setting.setValue("PlaybackModel",i);
            break;
        }
    }
}

void MediaListView::openFolder()
{
    auto url =m_model->media(currentIndex()).canonicalUrl();
    if(url.isLocalFile())
    {
        QFileInfo info(url.path());
        QDesktopServices::openUrl(QUrl::fromLocalFile( info.dir().path()));
    }
}

void MediaListView::mousePressEvent(QMouseEvent *event)
{
    auto index = indexAt(event->pos());
    if(index.row()==-1)
    {
        setCurrentIndex(index);
    }
    QListView::mousePressEvent(event);
}

void MediaListView::loadPlayList(QString fileName)
{
    QFile file(fileName);
    if(!file.open(QIODevice::Text|QIODevice::ReadOnly))
    {
        qDebug()<<QString("not open listInfo file ");
        return;
    }
    QTextStream ts(&file);
    while(!ts.atEnd())
    {
        m_model->insert(ts.readLine());
    }
    file.close();
}

void MediaListView::savePlayList(QString fileName)
{
    QFile file(fileName);
    if(!file.open(QIODevice::Text|QIODevice::WriteOnly))
    {
        qDebug()<<QString("not create listInfo file ");
        return;
    }
    QTextStream ts(&file);
    auto modelIndex = QModelIndex();
    int count=m_model->rowCount(modelIndex);
    int index=0;

    while(index<count)
    {
        ts<<m_model->media(m_model->index(index,0,modelIndex)).canonicalUrl().toString()<<endl;
        index++;
    }
    file.close();
}

Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/131513.html Link to the original text :https://javaforall.cn

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202207011222149241.html