Commit 2152fe55 authored by Prince Gupta's avatar Prince Gupta Committed by Jean-Baptiste Kempf
Browse files

qt: implement MLFoldersEditor widget

can be used to change a MLFoldersModel
parent 3a253b50
......@@ -256,6 +256,8 @@ libqt_plugin_la_SOURCES = \
gui/qt/widgets/native/customwidgets.cpp gui/qt/widgets/native/customwidgets.hpp \
gui/qt/widgets/native/interface_widgets.cpp \
gui/qt/widgets/native/interface_widgets.hpp \
gui/qt/widgets/native/mlfolderseditor.cpp \
gui/qt/widgets/native/mlfolderseditor.hpp \
gui/qt/widgets/native/qvlcframe.cpp \
gui/qt/widgets/native/qvlcframe.hpp \
gui/qt/widgets/native/roundimage.cpp gui/qt/widgets/native/roundimage.hpp \
......@@ -395,6 +397,7 @@ nodist_libqt_plugin_la_SOURCES = \
gui/qt/widgets/native/animators.moc.cpp \
gui/qt/widgets/native/customwidgets.moc.cpp \
gui/qt/widgets/native/interface_widgets.moc.cpp \
gui/qt/widgets/native/mlfolderseditor.moc.cpp \
gui/qt/widgets/native/roundimage.moc.cpp \
gui/qt/widgets/native/searchlineedit.moc.cpp
......
/*****************************************************************************
* roundimage.cpp: Custom widgets
****************************************************************************
* Copyright (C) 2021 the VideoLAN team
*
* Authors: Prince Gupta <guptaprince8832@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "mlfolderseditor.hpp"
#include <QBoxLayout>
#include <QHeaderView>
#include <QMessageBox>
#include <QPushButton>
MLFoldersEditor::MLFoldersEditor(QWidget *parent)
: QTableWidget(0, 2, parent)
{
setHorizontalHeaderLabels({ qtr("Path"), qtr("Remove") });
horizontalHeader()->setMinimumSectionSize( 100 );
horizontalHeader()->setSectionResizeMode( 0 , QHeaderView::Stretch );
horizontalHeader()->setFixedHeight( 24 );
}
void MLFoldersEditor::setMLFoldersModel(MLFoldersBaseModel *foldersModel)
{
if (m_foldersModel)
disconnect(m_foldersModel, nullptr, this, nullptr);
m_foldersModel = foldersModel;
m_newEntries.clear();
m_removeEntries.clear();
resetFolders();
connect(m_foldersModel, &QAbstractItemModel::modelReset, this, &MLFoldersEditor::resetFolders);
connect(m_foldersModel, &QAbstractItemModel::rowsInserted, this, &MLFoldersEditor::resetFolders);
connect(m_foldersModel, &QAbstractItemModel::rowsRemoved, this, &MLFoldersEditor::resetFolders);
connect(m_foldersModel, &QAbstractItemModel::rowsMoved, this, &MLFoldersEditor::resetFolders);
connect(m_foldersModel, &MLFoldersBaseModel::operationFailed, this, &MLFoldersEditor::handleOpFailure );
}
void MLFoldersEditor::add(const QUrl &mrl)
{
m_newEntries.push_back(mrl);
newRow(mrl);
}
void MLFoldersEditor::commit()
{
for ( const auto &removeEntry : m_removeEntries )
m_foldersModel->remove( removeEntry );
for ( const auto &newEntry : m_newEntries )
m_foldersModel->add( newEntry );
m_removeEntries.clear();
m_newEntries.clear();
}
void MLFoldersEditor::handleOpFailure(int operation, const QUrl &url)
{
const QString entryPoint = url.toDisplayString( QUrl::RemovePassword | QUrl::PreferLocalFile | QUrl::NormalizePathSegments );
QString msg;
switch (operation)
{
case MLFoldersBaseModel::Add:
msg = qtr("Failed to add \"%1\"").arg( entryPoint );
break;
case MLFoldersBaseModel::Remove:
msg = qtr("Failed to remove \"%1\"").arg( entryPoint );
break;
case MLFoldersBaseModel::Ban:
msg = qtr("Failed to ban \"%1\"").arg( entryPoint );
break;
case MLFoldersBaseModel::Unban:
msg = qtr("Failed to unban \"%1\"").arg( entryPoint );
break;
}
QMessageBox::warning( this, qtr( "Medialibrary error" ), msg );
}
void MLFoldersEditor::resetFolders()
{
setRowCount(0);
for ( int i = 0; i < m_foldersModel->rowCount(); ++i )
{
const auto url = m_foldersModel->data(m_foldersModel->index(i), MLFoldersBaseModel::MRL).toUrl();
if (!m_removeEntries.contains(url))
newRow(url);
}
for ( const auto &newEntry : m_newEntries )
newRow(newEntry);
}
void MLFoldersEditor::newRow(const QUrl &mrl)
{
const int row = rowCount();
setRowCount(row + 1);
const QString text = mrl.toDisplayString( QUrl::RemovePassword | QUrl::PreferLocalFile | QUrl::NormalizePathSegments );
auto col1 = new QTableWidgetItem( text );
col1->setData(Qt::UserRole, mrl);
col1->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
setItem( row, 0, col1 );
QWidget *wid = new QWidget( this );
QBoxLayout* layout = new QBoxLayout( QBoxLayout::LeftToRight , wid );
QPushButton *pb = new QPushButton( "-" , wid );
pb->setFixedSize( 16 , 16 );
layout->addWidget( pb , Qt::AlignCenter );
wid->setLayout( layout );
connect( pb , &QPushButton::clicked , this, [this, col1]()
{
int row = col1->row();
vlc_assert( row >= 0 );
const QUrl mrl = col1->data( Qt::UserRole ).toUrl();
const auto index = m_newEntries.indexOf( mrl );
if ( index == -1 )
m_removeEntries.push_back( mrl );
else
m_newEntries.remove( index );
removeRow( row );
});
setCellWidget( row, 1, wid );
}
/*****************************************************************************
* roundimage.hpp: Custom widgets
****************************************************************************
* Copyright (C) 2021 the VideoLAN team
*
* Authors: Prince Gupta <guptaprince8832@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifndef VLC_QT_MLFOLDERSEDITOR_HPP
#define VLC_QT_MLFOLDERSEDITOR_HPP
#include "qt.hpp"
#include <QTableWidget>
#include <memory>
#include <medialibrary/mlfoldersmodel.hpp>
class MLFoldersEditor : public QTableWidget
{
Q_OBJECT
public:
MLFoldersEditor( QWidget *parent = nullptr );
void setMLFoldersModel( MLFoldersBaseModel *foldersModel );
void add( const QUrl &mrl );
// call 'commit' to apply changes
void commit();
private slots:
void handleOpFailure( int operation, const QUrl &url );
void resetFolders();
private:
void newRow(const QUrl &mrl);
void removeMrlEntry(const QUrl &mrl);
MLFoldersBaseModel *m_foldersModel = nullptr;
// new entries to add/remove on 'commit' call
QVector<QUrl> m_newEntries;
QVector<QUrl> m_removeEntries;
};
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment