Skip to content

Commit

Permalink
Enable/Disable Menu items based on plugin loaded
Browse files Browse the repository at this point in the history
Per @Trinitou suggestions, a plugin specific menu item should only be enabled (available to execute by user)

This fixes the underlying cause of the crashes reported in free-audio#40 and free-audio#42

Therefore this also reverts the now obsolete change in previous merged pull request free-audio#43

Upon load change we update the menu items

Tab indentation set to 3 to be consistent with existing code

Note: Follows the QT memory ownership model, and the QAction pointers lifetime is handled by their parents

Testing:
Launch with no plugin loaded, see menu items are greyed out (disabled), user cannot select these items (as intended) therefore code is not executed and app doesn't crash

Launch with plugin loaded, see menu items are available and executed when user clicks, and confirm still no crashes

Images taken for testing
  • Loading branch information
NatureIsFrequency committed Feb 9, 2024
1 parent 4c4c7f1 commit 1410bb3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
35 changes: 30 additions & 5 deletions host/main-window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ void MainWindow::createMenu() {

QMenu *fileMenu = menuBar->addMenu(tr("File"));
// TODO: fileMenu->addAction(tr("Load plugin"));
connect(fileMenu->addAction(tr("Load Native Plugin Preset")),

_loadPluginPresetAction = fileMenu->addAction(tr("Load Native Plugin Preset"));
connect(_loadPluginPresetAction,
&QAction::triggered,
this,
&MainWindow::loadNativePluginPreset);
Expand All @@ -62,11 +64,15 @@ void MainWindow::createMenu() {
&Application::quit);

auto windowsMenu = menuBar->addMenu("Windows");
connect(windowsMenu->addAction(tr("Show Parameters")),

_showPluginParametersAction = windowsMenu->addAction(tr("Show Parameters"));
connect(_showPluginParametersAction,
&QAction::triggered,
this,
&MainWindow::showPluginParametersWindow);
connect(windowsMenu->addAction(tr("Show Quick Controls")),

_showPluginQuickControlsAction = windowsMenu->addAction(tr("Show Quick Controls"));
connect(_showPluginQuickControlsAction,
&QAction::triggered,
this,
&MainWindow::showPluginQuickControlsWindow);
Expand All @@ -76,18 +82,32 @@ void MainWindow::createMenu() {
dialog.exec();
});
menuBar->addSeparator();
connect(windowsMenu->addAction(tr("Toggle Plugin Window Visibility")),

_togglePluginWindowVisibilityAction = windowsMenu->addAction(tr("Toggle Plugin Window Visibility"));
connect(_togglePluginWindowVisibilityAction,
&QAction::triggered,
this,
&MainWindow::togglePluginWindowVisibility);
connect(windowsMenu->addAction(tr("Recreate Plugin Window")),

_recreatePluginWindowAction = windowsMenu->addAction(tr("Recreate Plugin Window"));
connect(_recreatePluginWindowAction,
&QAction::triggered,
this,
&MainWindow::recreatePluginWindow);

QMenu *helpMenu = menuBar->addMenu(tr("Help"));
connect(
helpMenu->addAction(tr("About")), &QAction::triggered, this, &MainWindow::showAboutDialog);

updateMenuItems();
}

void MainWindow::updateMenuItems() {
_loadPluginPresetAction->setEnabled(_pluginLoaded);
_showPluginParametersAction->setEnabled(_pluginLoaded);
_showPluginQuickControlsAction->setEnabled(_pluginLoaded);
_togglePluginWindowVisibilityAction->setEnabled(_pluginLoaded);
_recreatePluginWindowAction->setEnabled(_pluginLoaded);
}

void MainWindow::showSettingsDialog() {
Expand Down Expand Up @@ -117,6 +137,11 @@ void MainWindow::resizePluginView(int width, int height) {
adjustSize();
}

void MainWindow::onPluginLoadChange(bool const i_pluginLoaded) {
_pluginLoaded = i_pluginLoaded;
updateMenuItems();
}

void MainWindow::loadNativePluginPreset() {
auto file = QFileDialog::getOpenFileName(this, tr("Load Plugin Native Preset"));
if (file.isEmpty())
Expand Down
10 changes: 10 additions & 0 deletions host/main-window.hh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public:
void showPluginParametersWindow();
void showPluginQuickControlsWindow();
void resizePluginView(int width, int height);
void onPluginLoadChange(bool i_pluginLoaded);

void showPluginWindow() { _pluginViewWidget->show(); }

Expand All @@ -41,11 +42,20 @@ private:
void togglePluginWindowVisibility();
void recreatePluginWindow();
void showAboutDialog();
void updateMenuItems();

Application &_application;
QWindow *_pluginViewWindow = nullptr;
QWidget *_pluginViewWidget = nullptr;

QAction *_loadPluginPresetAction = nullptr;
QAction *_showPluginParametersAction = nullptr;
QAction *_showPluginQuickControlsAction = nullptr;
QAction *_togglePluginWindowVisibilityAction = nullptr;
QAction *_recreatePluginWindowAction = nullptr;

bool _pluginLoaded = false;

PluginParametersWidget *_pluginParametersWidget = nullptr;
PluginQuickControlsWidget *_pluginRemoteControlsWidget = nullptr;
};
11 changes: 5 additions & 6 deletions host/plugin-host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,17 @@ bool PluginHost::load(const QString &path, int pluginIndex) {

scanParams();
scanQuickControls();

Application::instance().mainWindow()->onPluginLoadChange(true);

return true;
}

void PluginHost::unload() {
checkForMainThread();

Application::instance().mainWindow()->onPluginLoadChange(false);

if (!_library.isLoaded())
return;

Expand Down Expand Up @@ -1171,12 +1176,6 @@ void PluginHost::remoteControlsSuggestPage(clap_id page_id) noexcept {
bool PluginHost::loadNativePluginPreset(const std::string &path) {
checkForMainThread();

if(!_plugin)
{
std::cerr << "called with a null clap_plugin pointer!" << std::endl;
return false;
}

if (!_plugin->canUsePresetLoad())
return false;

Expand Down

0 comments on commit 1410bb3

Please sign in to comment.