Skip to content

Instantly share code, notes, and snippets.

@badosu
Created March 4, 2015 22:18
Show Gist options
  • Save badosu/b8f89c7abafa2063b5ec to your computer and use it in GitHub Desktop.
Save badosu/b8f89c7abafa2063b5ec to your computer and use it in GitHub Desktop.
jalv_qt.cpp
/*
Copyright 2007-2013 David Robillard <http://drobilla.net>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "jalv_internal.h"
#include "lv2/lv2plug.in/ns/ext/patch/patch.h"
#include "lv2/lv2plug.in/ns/ext/port-props/port-props.h"
#include <QApplication>
#include <QDial>
#include <QLabel>
#include <QMainWindow>
#include <QMenuBar>
#include <QPushButton>
#include <QSizePolicy>
#include <QTimer>
#include <QVBoxLayout>
#include <QWidget>
#include <stdio.h>
#define CONTROLS_PER_ROW 5
#define DIAL_STEPS 10000
static QApplication* app = NULL;
class PresetAction : public QAction {
Q_OBJECT
public:
PresetAction(QObject* parent, Jalv* jalv, LilvNode* preset)
: QAction(parent)
, _jalv(jalv)
, _preset(preset)
{
connect(this, SIGNAL(triggered()),
this, SLOT(presetChosen()));
}
Q_SLOT void presetChosen() {
jalv_apply_preset(_jalv, _preset);
}
private:
Jalv* _jalv;
LilvNode* _preset;
};
class Control : public QWidget {
Q_OBJECT
public:
Control(Jalv* jalv, struct Port port, QWidget* parent = 0);
public slots:
void dialChanged(int value);
private:
const LilvPlugin* plugin;
struct Port port;
QDial* dial;
QLabel* label;
std::map<int, const char*> scalePoints;
bool isInteger;
};
#include "jalv_qt4_meta.hpp"
extern "C" {
int
jalv_init(int* argc, char*** argv, JalvOptions* opts)
{
app = new QApplication(*argc, *argv, true);
return 0;
}
const char*
jalv_native_ui_type(Jalv* jalv)
{
return "http://lv2plug.in/ns/extensions/ui#Qt4UI";
}
int
jalv_ui_resize(Jalv* jalv, int width, int height)
{
if (jalv->ui_instance && width > 0 && height > 0) {
QWidget* widget = (QWidget*)suil_instance_get_widget(jalv->ui_instance);
if (widget) {
widget->resize(width, height);
}
}
return 0;
}
void
jalv_ui_port_event(Jalv* jalv,
uint32_t port_index,
uint32_t buffer_size,
uint32_t protocol,
const void* buffer)
{
}
class Timer : public QTimer {
public:
explicit Timer(Jalv* jalv) : _jalv(jalv) {}
void timerEvent(QTimerEvent* e) {
jalv_update(_jalv);
}
private:
Jalv* _jalv;
};
static int
add_preset_to_menu(Jalv* jalv,
const LilvNode* node,
const LilvNode* title,
void* data)
{
QMenu* menu = (QMenu*)data;
const char* label = lilv_node_as_string(title);
QAction* action = new PresetAction(menu, jalv, lilv_node_duplicate(node));
action->setText(label);
menu->addAction(action);
return 0;
}
Control::Control(Jalv* jalv, struct Port _port, QWidget* parent)
: QWidget(parent)
{
plugin = jalv->plugin;
port = _port;
const LilvPort* lilvPort = port.lilv_port;
LilvNode* lv2_integer = lilv_new_uri(jalv->world, LV2_CORE__integer);
LilvNode* nmin;
LilvNode* nmax;
LilvNode* ndef;
lilv_port_get_range(plugin, lilvPort, &ndef, &nmin, &nmax);
isInteger = lilv_port_has_property(plugin, lilvPort, lv2_integer);
LilvNode* name = lilv_port_get_name(plugin, lilvPort);
LilvScalePoints* sp = lilv_port_get_scale_points(plugin, lilvPort);
if (sp) {
LILV_FOREACH(scale_points, s, sp) {
const LilvScalePoint* p = lilv_scale_points_get(sp, s);
int value = (int)lilv_node_as_float(lilv_scale_point_get_value(p));
scalePoints[value] = lilv_node_as_string(lilv_scale_point_get_label(p));
}
lilv_scale_points_free(sp);
}
float min = lilv_node_as_float(nmin);
float max = lilv_node_as_float(nmax);
dial = new QDial();
label = new QLabel();
QVBoxLayout* layout = new QVBoxLayout();
float def;
if (ndef) {
def = lilv_node_as_float(ndef);
}
else {
def = min;
}
if (!isInteger) {
min *= DIAL_STEPS;
max *= DIAL_STEPS;
}
dial->setRange(min, max);
dial->setValue(isInteger ? def : def * DIAL_STEPS);
if (scalePoints[def]) {
label->setText(scalePoints[def]);
} else {
label->setText(QString("%1").arg(def));
}
layout->addWidget(new QLabel(lilv_node_as_string(name)), 0, Qt::AlignHCenter);
layout->addWidget(dial, 0, Qt::AlignHCenter);
layout->addWidget(label, 0, Qt::AlignHCenter);
connect(dial, SIGNAL(valueChanged(int)), this, SLOT(dialChanged(int)));
setLayout(layout);
setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
setMinimumWidth(100);
lilv_node_free(nmin);
lilv_node_free(nmax);
lilv_node_free(ndef);
lilv_node_free(name);
lilv_node_free(lv2_integer);
}
void Control::dialChanged(int dialValue) {
float value = isInteger ? dialValue : (float)dialValue/DIAL_STEPS;
if (scalePoints[value]) {
label->setText(scalePoints[value]);
} else {
label->setText(QString("%1").arg(value));
}
port.control = value;
}
static QWidget*
build_control_widget(Jalv* jalv)
{
const LilvPlugin* plugin = jalv->plugin;
LilvWorld* world = jalv->world;
LilvNode* pprop_notOnGUI = lilv_new_uri(world, LV2_PORT_PROPS__notOnGUI);
QWidget* grid = new QWidget();
QGridLayout* layout = new QGridLayout();
int controlIndex = 0;
for (unsigned i = 0; i < jalv->num_ports; ++i) {
Port port = jalv->ports[i];
if (!jalv->opts.show_hidden &&
lilv_port_has_property(plugin, port.lilv_port, pprop_notOnGUI)) {
continue;
}
if (port.type == TYPE_CONTROL) {
int row = controlIndex / CONTROLS_PER_ROW;
int column = controlIndex % CONTROLS_PER_ROW;
layout->addWidget(new Control(jalv, port), row, column);
++controlIndex;
}
}
grid->setLayout(layout);
lilv_node_free(pprop_notOnGUI);
return grid;
}
int
jalv_open_ui(Jalv* jalv)
{
QMainWindow* win = new QMainWindow();
QMenu* file_menu = win->menuBar()->addMenu("&File");
QMenu* presets_menu = win->menuBar()->addMenu("&Presets");
QAction* quit_action = new QAction("&Quit", win);
QObject::connect(quit_action, SIGNAL(triggered()), win, SLOT(close()));
quit_action->setShortcuts(QKeySequence::Quit);
quit_action->setStatusTip("Quit Jalv");
file_menu->addAction(quit_action);
jalv->has_ui = TRUE;
jalv_load_presets(jalv, add_preset_to_menu, presets_menu);
//if (jalv->ui) {
// jalv_ui_instantiate(jalv, jalv_native_ui_type(jalv), win);
//}
QWidget* widget;
//if (jalv->ui_instance) {
// widget = (QWidget*)suil_instance_get_widget(jalv->ui_instance);
//} else {
widget = build_control_widget(jalv);
//}
win->setCentralWidget(widget);
app->connect(app, SIGNAL(lastWindowClosed()), app, SLOT(quit()));
win->show();
Timer* timer = new Timer(jalv);
timer->start(1000 / jalv->ui_update_hz);
int ret = app->exec();
zix_sem_post(jalv->done);
return ret;
}
int
jalv_close_ui(Jalv* jalv)
{
app->quit();
return 0;
}
} // extern "C"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment