Skip to content

Commit

Permalink
Add CustomDirectoryBrowser
Browse files Browse the repository at this point in the history
A simple class that extends from FileBrowser that allows
for adding and removing directories.
  • Loading branch information
sakertooth committed Sep 19, 2023
1 parent c9402b4 commit d16a236
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 9 deletions.
43 changes: 43 additions & 0 deletions include/CustomDirectoryBrowser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* CustomDirectoryBrowser.h
*
* Copyright (c) 2023 saker <sakertooth@gmail.com>
*
* This file is part of LMMS - https://lmms.io
*
* 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 (see COPYING); if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
*/

#ifndef LMMS_CUSTOM_DIRECTORY_BROWSER_H
#define LMMS_CUSTOM_DIRECTORY_BROWSER_H

#include "FileBrowser.h"
namespace lmms::gui {

class CustomDirectoryBrowser : public FileBrowser
{
Q_OBJECT
public:
CustomDirectoryBrowser(const QString& directories, const QString& filter, const QString& title, const QPixmap& pm,
QWidget* parent, bool dirs_as_items = false, bool recurse = false, const QString& userDir = "",
const QString& factoryDir = "");

void addCustomDirectory();
void removeCustomDirectory();
};
} // namespace lmms::gui
#endif // LMMS_CUSTOM_DIRECTORY_BROWSER_H
8 changes: 5 additions & 3 deletions include/FileBrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,16 @@ class FileBrowser : public SideBarWidget

~FileBrowser() override = default;

private slots:
protected slots:
void reloadTree();
void expandItems( QTreeWidgetItem * item=nullptr, QList<QString> expandedDirs = QList<QString>() );
bool filterAndExpandItems(const QString & filter, QTreeWidgetItem * item = nullptr);
void giveFocusToFilter();

protected:
QString m_directories; //!< Directories to search, split with '*'
FileBrowserTreeWidget* m_fileBrowserTreeWidget;

private:
void keyPressEvent( QKeyEvent * ke ) override;

Expand All @@ -86,11 +90,9 @@ private slots:
void saveDirectoriesStates();
void restoreDirectoriesStates();

FileBrowserTreeWidget * m_fileBrowserTreeWidget;

QLineEdit * m_filterEdit;

QString m_directories; //!< Directories to search, split with '*'
QString m_filter; //!< Filter as used in QDir::match()

bool m_dirsAsItems;
Expand Down
1 change: 1 addition & 0 deletions src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ SET(LMMS_SRCS
gui/ControllerRackView.cpp
gui/ControllerView.cpp
gui/Controls.cpp
gui/CustomDirectoryBrowser.cpp
gui/EffectControlDialog.cpp
gui/EffectRackView.cpp
gui/EffectView.cpp
Expand Down
77 changes: 77 additions & 0 deletions src/gui/CustomDirectoryBrowser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* CustomDirectoryBrowser.h
*
* Copyright (c) 2023 saker <sakertooth@gmail.com>
*
* This file is part of LMMS - https://lmms.io
*
* 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 (see COPYING); if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
*/

#include "CustomDirectoryBrowser.h"

#include <QFileDialog>
#include <QPushButton>
#include <QWidget>

#include "ConfigManager.h"
#include "FileBrowser.h"
#include "embed.h"

namespace lmms::gui {
CustomDirectoryBrowser::CustomDirectoryBrowser(const QString& directories, const QString& filter, const QString& title,
const QPixmap& pm, QWidget* parent, bool dirs_as_items, bool recurse, const QString& userDir,
const QString& factoryDir)
: FileBrowser(directories, filter, title, pm, parent, dirs_as_items, recurse, userDir, factoryDir)
{
auto buttons = new QWidget(contentParent());
auto buttonsLayout = new QHBoxLayout(buttons);
buttons->setContentsMargins(0, 0, 0, 0);
buttonsLayout->setSpacing(0);

auto addButton = new QPushButton(embed::getIconPixmap("add"), QString{}, buttons);
auto removeButton = new QPushButton(embed::getIconPixmap("discard"), QString{}, buttons);
connect(addButton, &QPushButton::clicked, this, &CustomDirectoryBrowser::addCustomDirectory);
connect(removeButton, &QPushButton::clicked, this, &CustomDirectoryBrowser::removeCustomDirectory);

buttonsLayout->addWidget(addButton, 0, Qt::AlignCenter);
buttonsLayout->addWidget(removeButton, 0, Qt::AlignCenter);
addContentWidget(buttons);
}

void CustomDirectoryBrowser::addCustomDirectory()
{
auto dir = QFileDialog::getExistingDirectory(this, tr("Add custom directory"), "", QFileDialog::ShowDirsOnly);
if (dir.isEmpty()) { return; }
ConfigManager::inst()->addCustomDirectory(dir);
m_directories += dir + "*";
reloadTree();
}

void CustomDirectoryBrowser::removeCustomDirectory()
{
auto selectedItems = m_fileBrowserTreeWidget->selectedItems();
for (auto& item : selectedItems)
{
auto directory = dynamic_cast<Directory*>(item);
ConfigManager::inst()->removeCustomDirectory(directory->text(0));
m_directories.remove(directory->text(0));
}
reloadTree();
}

} // namespace lmms::gui
6 changes: 3 additions & 3 deletions src/gui/FileBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,15 +321,15 @@ void FileBrowser::giveFocusToFilter()

void FileBrowser::addItems(const QString & path )
{
// Do not add the current working directory if the path is empty
if (path.isEmpty()) { return; }

if( m_dirsAsItems )
{
m_fileBrowserTreeWidget->addTopLevelItem( new Directory( path, QString(), m_filter ) );
return;
}

// Do not add the current working directory if the path is empty
if (path.isEmpty()) { return; }

// try to add all directories from file system alphabetically into the tree
QDir cdir( path );
QStringList files = cdir.entryList( QDir::Dirs, QDir::Name );
Expand Down
6 changes: 3 additions & 3 deletions src/gui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "AboutDialog.h"
#include "AutomationEditor.h"
#include "ControllerRackView.h"
#include "CustomDirectoryBrowser.h"
#include "embed.h"
#include "Engine.h"
#include "ExportProjectDialog.h"
Expand Down Expand Up @@ -139,10 +140,9 @@ MainWindow::MainWindow() :
auto customDirectories = QString{};
for (const auto& directory : confMgr->customDirectories())
{
customDirectories.append(directory);
if (directory != confMgr->customDirectories().back()) { customDirectories.append("*"); }
customDirectories.append(directory + "*");
}
sideBar->appendTab(new FileBrowser(customDirectories, "*", tr("Custom Directories"),
sideBar->appendTab(new CustomDirectoryBrowser(customDirectories, "*", tr("Custom Directories"),
embed::getIconPixmap("folder").transformed(QTransform().rotate(90)), splitter, true, true));

m_workspace = new QMdiArea(splitter);
Expand Down

0 comments on commit d16a236

Please sign in to comment.