Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Highlight files in FileBrowser #7700

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion include/FileBrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private slots:
} ;



class Directory;

class FileBrowserTreeWidget : public QTreeWidget
{
Expand Down Expand Up @@ -195,7 +195,13 @@ private slots:
bool openInNewSampleTrack( lmms::gui::FileItem* item );
void sendToActiveInstrumentTrack( lmms::gui::FileItem* item );
void updateDirectory( QTreeWidgetItem * item );
void openDirectory( Directory* directory );
void openContainingFolder( lmms::gui::FileItem* item );
static bool supportsSelectOption(const QString &fileManager) {
return fileManager == "nautilus" || fileManager == "dolphin" || fileManager == "thunar" ||
fileManager == "pcmanfm" || fileManager == "nemo" || fileManager == "caja" ||
fileManager == "io.elementary.files" || fileManager == "spacefm" || fileManager == "pcmanfm-qt";
}

} ;

Expand Down
152 changes: 118 additions & 34 deletions src/gui/FileBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <QMdiSubWindow>
#include <QMenu>
#include <QMessageBox>
#include <QProcess>
#include <QPushButton>
#include <QShortcut>
#include <QStringList>
Expand Down Expand Up @@ -67,7 +68,6 @@
#include "TextFloat.h"
#include "ThreadPool.h"
#include "embed.h"

namespace lmms::gui
{

Expand Down Expand Up @@ -629,37 +629,63 @@

void FileBrowserTreeWidget::contextMenuEvent(QContextMenuEvent * e )
{
auto file = dynamic_cast<FileItem*>(itemAt(e->pos()));
if( file != nullptr && file->isTrack() )
{
QMenu contextMenu( this );
#ifdef LMMS_BUILD_APPLE
QString fileManager = tr("Finder");
#elif defined(LMMS_BUILD_WIN32)
QString fileManager = tr("File Explorer");
#else
QString fileManager = tr("file manager");
#endif

contextMenu.addAction(
tr( "Send to active instrument-track" ),
[=, this]{ sendToActiveInstrumentTrack(file); }
);
QTreeWidgetItem* item = itemAt(e->pos());

contextMenu.addSeparator();
auto file = dynamic_cast<FileItem*>(item);

contextMenu.addAction(
QIcon(embed::getIconPixmap("folder")),
tr("Open containing folder"),
[=, this]{ openContainingFolder(file); }
);
QMenu contextMenu( this );

auto songEditorHeader = new QAction(tr("Song Editor"), nullptr);
songEditorHeader->setDisabled(true);
contextMenu.addAction( songEditorHeader );
contextMenu.addActions( getContextActions(file, true) );
auto dir = dynamic_cast<Directory*>(item); // TODO: this might not be a great way to check if it's a directory

auto patternEditorHeader = new QAction(tr("Pattern Editor"), nullptr);
patternEditorHeader->setDisabled(true);
contextMenu.addAction(patternEditorHeader);
contextMenu.addActions( getContextActions(file, false) );

// We should only show the menu if it contains items
if (!contextMenu.isEmpty()) { contextMenu.exec( e->globalPos() ); }
}
if( file != nullptr)
{
if (file->isTrack()) {
contextMenu.addAction(
tr( "Send to active instrument-track" ),
[=, this]{ sendToActiveInstrumentTrack(file); }
);

contextMenu.addSeparator();
}

contextMenu.addAction(
QIcon(embed::getIconPixmap("folder")),

tr("Show in") + " " +fileManager,
[=, this]{ openContainingFolder(file); }
);


auto songEditorHeader = new QAction(tr("Song Editor"), nullptr);
songEditorHeader->setDisabled(true);
contextMenu.addAction( songEditorHeader );
contextMenu.addActions( getContextActions(file, true) );

auto patternEditorHeader = new QAction(tr("Pattern Editor"), nullptr);
patternEditorHeader->setDisabled(true);
contextMenu.addAction(patternEditorHeader);
contextMenu.addActions( getContextActions(file, false) );

} else if (dir) {

contextMenu.addAction(
QIcon(embed::getIconPixmap("folder")),
tr("Open in") + " " + fileManager,
[=, this]{ openDirectory(dir); }
);
}

// We should only show the menu if it contains items
if (!contextMenu.isEmpty()) { contextMenu.exec( e->globalPos() ); }
}


Expand Down Expand Up @@ -918,7 +944,7 @@
case FileItem::FileHandling::NotSupported:
default:
break;

Check notice on line 947 in src/gui/FileBrowser.cpp

View check run for this annotation

codefactor.io / CodeFactor

src/gui/FileBrowser.cpp#L947

Redundant blank line at the end of a code block should be deleted. (whitespace/blank_line)
}
Engine::audioEngine()->doneChangeInModel();
}
Expand Down Expand Up @@ -992,21 +1018,79 @@



void FileBrowserTreeWidget::openDirectory(Directory* directory) {
QDesktopServices::openUrl(QUrl::fromLocalFile(directory->fullName()));
}

void FileBrowserTreeWidget::openContainingFolder(FileItem* item)
{
// Delegate to QDesktopServices::openUrl with the directory of the selected file. Please note that
// this will only open the directory but not select the file as this is much more complicated due
// to different implementations that are needed for different platforms (Linux/Windows/MacOS).

// Using QDesktopServices::openUrl seems to be the most simple cross platform way which uses
// functionality that's already available in Qt.
QFileInfo fileInfo(item->fullName());
QDesktopServices::openUrl(QUrl::fromLocalFile(fileInfo.dir().path()));
QFileInfo fileInfo(item->fullName());
QString path = QDir::toNativeSeparators(fileInfo.canonicalFilePath());
QString directory = QDir::toNativeSeparators(fileInfo.canonicalFilePath());

#ifdef _WIN32
// Windows
QStringList param;
if (!fileInfo.isDir())
param += QLatin1String("/select,");
param += path;
QProcess::startDetached("explorer", param);

#elif __APPLE__
// macOS
QProcess::startDetached("open", {"-R", path});
#else
// Linux & BSD
// there are a lot of potential file managers on these systems so we need to figure out what to use.

// Check user preferences from environment variable
QString fileManager = qgetenv("FILE_MANAGER");
if (fileManager.isEmpty()) {
fileManager = qgetenv("XDG_FILE_MANAGER"); // not actually in the freedesktop spec but is sometimes used
}

// If no preference is set, check the desktop environment
if (fileManager.isEmpty()) {
QString desktopEnv = qgetenv("XDG_CURRENT_DESKTOP");
if (desktopEnv.isEmpty()) {
desktopEnv = qgetenv("DESKTOP_SESSION");
}

if (desktopEnv.contains("GNOME", Qt::CaseInsensitive)) {
fileManager = "nautilus";
} else if (desktopEnv.contains("KDE", Qt::CaseInsensitive)) {
fileManager = "dolphin";
} else if (desktopEnv.contains("XFCE", Qt::CaseInsensitive)) {
fileManager = "thunar";
} else if (desktopEnv.contains("LXDE", Qt::CaseInsensitive)) {
fileManager = "pcmanfm";
} else if (desktopEnv.contains("CINNAMON", Qt::CaseInsensitive)) {
fileManager = "nemo";
} else if (desktopEnv.contains("MATE", Qt::CaseInsensitive)) {
fileManager = "caja";
} else if (desktopEnv.contains("PANTHEON", Qt::CaseInsensitive)) {
fileManager = "io.elementary.files";
} else if (desktopEnv.contains("LXQT", Qt::CaseInsensitive)) {
fileManager = "pcmanfm-qt";
} else {
QDesktopServices::openUrl(QUrl::fromLocalFile(path));
return; // Exit if no suitable file manager found
}
}

// Check if the file manager supports the --select option
if (supportsSelectOption(fileManager)) {
QProcess::startDetached(fileManager, {"--select", path});
} else {
QDesktopServices::openUrl(QUrl::fromLocalFile(directory));
}
#endif
}





void FileBrowserTreeWidget::sendToActiveInstrumentTrack( FileItem* item )
{
// get all windows opened in the workspace
Expand Down
Loading