Linked model groups (#4964)

Add labeled controls for different types with a common base class

Implement a container for multiple equal groups of linked models and
suiting views. Such groups are suited for representing mono effects where each
Model occurs twice. A group provides Models for one mono processor and is
visually represented with a group box.

This concept is common for LADSPA and Lv2, and useful for any mono effect.
This commit is contained in:
Johannes Lorenz
2020-02-21 19:26:29 +01:00
committed by GitHub
parent 3410db4d99
commit eebdc0f4be
11 changed files with 1351 additions and 0 deletions

133
include/ControlLayout.h Normal file
View File

@@ -0,0 +1,133 @@
/*
* ControlLayout.h - layout for controls
*
* Copyright (c) 2019-2019 Johannes Lorenz <j.git$$$lorenz-ho.me, $$$=@>
*
* 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.
*
*/
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef CONTROLLAYOUT_H
#define CONTROLLAYOUT_H
#include <QLayout>
#include <QMultiMap>
#include <QStyle>
class QLayoutItem;
class QRect;
class QString;
/**
Layout for controls (models)
Originally token from Qt's FlowLayout example. Modified.
Features a search bar, as well as looking up widgets with string keys
Keys have to be provided in the widgets' objectNames
*/
class ControlLayout : public QLayout
{
Q_OBJECT
public:
explicit ControlLayout(QWidget *parent,
int margin = -1, int hSpacing = -1, int vSpacing = -1);
~ControlLayout() override;
void addItem(QLayoutItem *item) override;
int horizontalSpacing() const;
int verticalSpacing() const;
Qt::Orientations expandingDirections() const override;
bool hasHeightForWidth() const override;
int heightForWidth(int) const override;
int count() const override;
QLayoutItem *itemAt(int index) const override;
QLayoutItem *itemByString(const QString& key) const;
QSize minimumSize() const override;
void setGeometry(const QRect &rect) override;
QSize sizeHint() const override;
QLayoutItem *takeAt(int index) override;
private slots:
void onTextChanged(const QString&);
private:
int doLayout(const QRect &rect, bool testOnly) const;
int smartSpacing(QStyle::PixelMetric pm) const;
QMap<QString, QLayoutItem *>::const_iterator pairAt(int index) const;
QMultiMap<QString, QLayoutItem *> m_itemMap;
int m_hSpace;
int m_vSpace;
// relevant dimension is width, as later, heightForWidth() will be called
// 400 looks good and is ~4 knobs in a row
constexpr const static int m_minWidth = 400;
class QLineEdit* m_searchBar;
//! name of search bar, must be ASCII sorted before any alpha numerics
static constexpr const char* s_searchBarName = "!!searchBar!!";
};
#endif // CONTROLLAYOUT_H

134
include/Controls.h Normal file
View File

@@ -0,0 +1,134 @@
/*
* Controls.h - labeled control widgets
*
* Copyright (c) 2019-2019 Johannes Lorenz <j.git$$$lorenz-ho.me, $$$=@>
*
* 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 CONTROLS_H
#define CONTROLS_H
#include "Model.h"
// headers only required for covariance
#include "AutomatableModel.h"
#include "ComboBoxModel.h"
class QString;
class QWidget;
class AutomatableModel;
/**
These classes provide
- a control with a text label
- a type safe way to set a model
(justification: setting the wrong typed model to a widget will cause
hard-to-find runtime errors)
*/
class Control
{
public:
virtual QWidget* topWidget() = 0;
virtual void setText(const QString& text) = 0;
virtual void setModel(AutomatableModel* model) = 0;
virtual AutomatableModel* model() = 0;
virtual class AutomatableModelView* modelView() = 0;
virtual ~Control();
};
class KnobControl : public Control
{
class Knob* m_knob;
public:
void setText(const QString& text) override;
QWidget* topWidget() override;
void setModel(AutomatableModel* model) override;
FloatModel* model() override;
class AutomatableModelView* modelView() override;
KnobControl(QWidget* parent = nullptr);
~KnobControl() override;
};
class ComboControl : public Control
{
QWidget* m_widget;
class ComboBox* m_combo;
class QLabel* m_label;
public:
void setText(const QString& text) override;
QWidget* topWidget() override { return m_widget; }
void setModel(AutomatableModel* model) override;
ComboBoxModel* model() override;
class AutomatableModelView* modelView() override;
ComboControl(QWidget* parent = nullptr);
~ComboControl() override;
};
class LcdControl : public Control
{
class LcdSpinBox* m_lcd;
public:
void setText(const QString& text) override;
QWidget* topWidget() override;
void setModel(AutomatableModel* model) override;
IntModel* model() override;
class AutomatableModelView* modelView() override;
LcdControl(int numDigits, QWidget* parent = nullptr);
~LcdControl() override;
};
class CheckControl : public Control
{
QWidget* m_widget;
class LedCheckBox* m_checkBox;
QLabel* m_label;
public:
void setText(const QString& text) override;
QWidget* topWidget() override;
void setModel(AutomatableModel* model) override;
BoolModel *model() override;
class AutomatableModelView* modelView() override;
CheckControl(QWidget* parent = nullptr);
~CheckControl() override;
};
#endif // CONTROLS_H

View File

@@ -0,0 +1,105 @@
/*
* LinkedModelGroupViews.h - view for groups of linkable models
*
* Copyright (c) 2019-2019 Johannes Lorenz <j.git$$$lorenz-ho.me, $$$=@>
*
* 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 LINKEDMODELGROUPVIEWS_H
#define LINKEDMODELGROUPVIEWS_H
#include <cstddef>
#include <memory>
#include <vector>
#include <QWidget>
/**
@file LinkedModelGroupViews.h
See Lv2ViewBase.h for example usage
*/
/**
View for a representative processor
Features:
* Remove button for removable models
* Simple handling of adding, removing and model changing
@note Neither this class, nor any inheriting classes, shall inherit
ModelView. The "view" in the name is just for consistency
with LinkedModelGroupsView.
*/
class LinkedModelGroupView : public QWidget
{
public:
/**
@param colNum numbers of columns for the controls
(link LEDs not counted)
*/
LinkedModelGroupView(QWidget *parent, class LinkedModelGroup* model,
std::size_t colNum);
~LinkedModelGroupView();
//! Reconnect models if model changed
void modelChanged(class LinkedModelGroup *linkedModelGroup);
protected:
//! Add a control to this widget
//! @warning This widget will own this control, do not free it
void addControl(class Control *ctrl, const std::string &id,
const std::string& display, bool removable);
void removeControl(const QString &key);
private:
class LinkedModelGroup* m_model;
//! column number in surrounding grid in LinkedModelGroupsView
std::size_t m_colNum;
class ControlLayout* m_layout;
std::map<std::string, std::unique_ptr<class Control>> m_widgets;
};
/**
Container class for one LinkedModelGroupView
@note It's intended this class does not inherit from ModelView.
Inheriting classes need to do that, see e.g. Lv2Instrument.h
*/
class LinkedModelGroupsView
{
protected:
~LinkedModelGroupsView() = default;
//! Reconnect models if model changed; to be called by child virtuals
void modelChanged(class LinkedModelGroups* ctrlBase);
private:
//! The base class must return the adressed group view,
//! which has the same value as "this"
virtual LinkedModelGroupView* getGroupView() = 0;
};
#endif // LINKEDMODELGROUPVIEWS_H

175
include/LinkedModelGroups.h Normal file
View File

@@ -0,0 +1,175 @@
/*
* LinkedModelGroups.h - base classes for groups of linked models
*
* Copyright (c) 2019-2019 Johannes Lorenz <j.git$$$lorenz-ho.me, $$$=@>
*
* 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 LINKEDMODELGROUPS_H
#define LINKEDMODELGROUPS_H
#include <cstddef>
#include <memory>
#include <vector>
#include "Model.h"
/**
@file LinkedModelGroups.h
See Lv2ControlBase.h and Lv2Proc.h for example usage
*/
/**
Base class for a group of linked models
See the LinkedModelGroup class for explenations
Features:
* Models are stored by their QObject::objectName
* Models are linked automatically
*/
class LinkedModelGroup : public Model
{
Q_OBJECT
public:
/*
Initialization
*/
//! @param parent model of the LinkedModelGroups class
LinkedModelGroup(Model* parent) : Model(parent) {}
/*
Linking (initially only)
*/
void linkControls(LinkedModelGroup *other);
/*
Models
*/
struct ModelInfo
{
QString m_name;
class AutomatableModel* m_model;
ModelInfo() { /* hopefully no one will use this */ } // TODO: remove?
ModelInfo(const QString& name, AutomatableModel* model)
: m_name(name), m_model(model) {}
};
// TODO: refactor those 2
template<class Functor>
void foreach_model(const Functor& ftor)
{
for (auto itr = m_models.begin(); itr != m_models.end(); ++itr)
{
ftor(itr->first, itr->second);
}
}
template<class Functor>
void foreach_model(const Functor& ftor) const
{
for (auto itr = m_models.cbegin(); itr != m_models.cend(); ++itr)
{
ftor(itr->first, itr->second);
}
}
std::size_t modelNum() const { return m_models.size(); }
bool containsModel(const QString& name) const;
void removeControl(AutomatableModel *);
/*
Load/Save
*/
void saveValues(class QDomDocument& doc, class QDomElement& that);
void loadValues(const class QDomElement& that);
signals:
// NOTE: when separating core from UI, this will need to be removed
// (who would kno if the client is Qt, i.e. it may not have slots at all)
// In this case you'd e.g. send the UI something like
// "/added <model meta info>"
void modelAdded(AutomatableModel* added);
void modelRemoved(AutomatableModel* removed);
public:
AutomatableModel* getModel(const std::string& s)
{
auto itr = m_models.find(s);
return (itr == m_models.end()) ? nullptr : itr->second.m_model;
}
//! Register a further model
void addModel(class AutomatableModel* model, const QString& name);
//! Unregister a model, return true if a model was erased
bool eraseModel(const QString& name);
//! Remove all models
void clearModels();
private:
//! models for the controls
std::map<std::string, ModelInfo> m_models;
};
/**
Container for a group of linked models
Each group contains the same models and model types. The models are
numbered, and equal numbered models are associated and always linked.
A typical application are two mono plugins making a stereo plugin.
@note Though this class can contain multiple model groups, a corresponding
view ("LinkedModelGroupViews") will only display one group, as they all have
the same values
@note Though called "container", this class does not contain, but only
know the single groups. The inheriting classes are responsible for storage.
*/
class LinkedModelGroups
{
public:
virtual ~LinkedModelGroups();
void linkAllModels();
/*
Load/Save
*/
void saveSettings(class QDomDocument& doc, class QDomElement& that);
void loadSettings(const class QDomElement& that);
/*
General
*/
//! Derived classes must return the group with index @p idx,
//! or nullptr if @p is out of range
virtual LinkedModelGroup* getGroup(std::size_t idx) = 0;
//! @see getGroup
virtual const LinkedModelGroup* getGroup(std::size_t idx) const = 0;
};
#endif // LINKEDMODELGROUPS_H

View File

@@ -21,6 +21,13 @@ std::unique_ptr<T> make_unique(Args&&... args)
{
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
//! Overload for the case a deleter should be specified
template<typename T, typename Deleter, typename... Args>
std::unique_ptr<T, Deleter> make_unique(Args&&... args)
{
return std::unique_ptr<T, Deleter>(new T(std::forward<Args>(args)...));
}
#endif
#endif // include guard