diff --git a/include/CustomDirectoryBrowser.h b/include/CustomDirectoryBrowser.h new file mode 100644 index 00000000000..8d435ab9397 --- /dev/null +++ b/include/CustomDirectoryBrowser.h @@ -0,0 +1,43 @@ +/* + * CustomDirectoryBrowser.h + * + * Copyright (c) 2023 saker + * + * 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 \ No newline at end of file diff --git a/include/FileBrowser.h b/include/FileBrowser.h index eafb827dac5..565d9ae8489 100644 --- a/include/FileBrowser.h +++ b/include/FileBrowser.h @@ -72,12 +72,16 @@ class FileBrowser : public SideBarWidget ~FileBrowser() override = default; -private slots: +protected slots: void reloadTree(); void expandItems( QTreeWidgetItem * item=nullptr, QList expandedDirs = QList() ); 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; @@ -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; diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index afed153f928..2ab8d700f75 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -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 diff --git a/src/gui/CustomDirectoryBrowser.cpp b/src/gui/CustomDirectoryBrowser.cpp new file mode 100644 index 00000000000..0b3341a18b8 --- /dev/null +++ b/src/gui/CustomDirectoryBrowser.cpp @@ -0,0 +1,77 @@ +/* + * CustomDirectoryBrowser.h + * + * Copyright (c) 2023 saker + * + * 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 +#include +#include + +#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(item); + ConfigManager::inst()->removeCustomDirectory(directory->text(0)); + m_directories.remove(directory->text(0)); + } + reloadTree(); +} + +} // namespace lmms::gui \ No newline at end of file diff --git a/src/gui/FileBrowser.cpp b/src/gui/FileBrowser.cpp index ae41abf2c11..0f74678a551 100644 --- a/src/gui/FileBrowser.cpp +++ b/src/gui/FileBrowser.cpp @@ -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 ); diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 7019b71ce0b..ff599f3ea23 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -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" @@ -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);