forked from LMMS/lmms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedModelGroupViews.cpp
More file actions
166 lines (130 loc) · 4.05 KB
/
LinkedModelGroupViews.cpp
File metadata and controls
166 lines (130 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* 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.
*
*/
#include "LinkedModelGroupViews.h"
#include <QGridLayout>
#include "Controls.h"
#include "LedCheckbox.h"
#include "LinkedModelGroupLayout.h"
#include "LinkedModelGroups.h"
#include "stdshims.h"
/*
LinkedModelGroupViewBase
*/
LinkedModelGroupView::LinkedModelGroupView(QWidget* parent,
LinkedModelGroup *model, std::size_t colNum) :
QWidget(parent),
m_model(model),
m_colNum(colNum),
m_layout(new LinkedModelGroupLayout(this))
{
// make viewable: if there are no knobs, the user should at least see
// a rectangle to drop controls in
setMinimumSize(QSize(50, 50));
}
LinkedModelGroupView::~LinkedModelGroupView() {}
void LinkedModelGroupView::modelChanged(LinkedModelGroup *group)
{
// reconnect models
group->foreach_model([this](const std::string& str,
const LinkedModelGroup::ModelInfo& minf)
{
auto itr = m_widgets.find(str);
// in case there are new or deleted widgets, the subclass has already
// modified m_widgets, so this will go into the else case
if(itr == m_widgets.end())
{
// no widget? this can happen when the whole view is being destroyed
// (for some strange reasons)
}
else
{
itr->second->setModel(minf.m_model);
}
});
m_model = group;
}
void LinkedModelGroupView::addControl(Control* ctrl, const std::string& id,
const std::string &display, bool removable)
{
int wdgNum = static_cast<int>(m_widgets.size());
if (ctrl)
{
QWidget* box = new QWidget(this);
QHBoxLayout* boxLayout = new QHBoxLayout(box);
boxLayout->addWidget(ctrl->topWidget());
if (removable)
{
QPushButton* removeBtn = new QPushButton;
removeBtn->setIcon( embed::getIconPixmap( "discard" ) );
QObject::connect(removeBtn, &QPushButton::clicked,
this, [this,ctrl](bool){
AutomatableModel* controlModel = ctrl->model();
// remove control out of model group
// (will also remove it from the UI)
m_model->removeControl(controlModel);
// delete model (includes disconnecting all connections)
delete controlModel;
},
Qt::DirectConnection);
boxLayout->addWidget(removeBtn);
}
// required, so the Layout knows how to sort/filter widgets by string
box->setObjectName(QString::fromStdString(display));
m_layout->addWidget(box);
m_widgets.emplace(id, ctrl); // TODO: use set?
++wdgNum;
}
if(isHidden()) { setHidden(false); } // TODO: can be removed?
}
void LinkedModelGroupView::removeControl(const QString& key)
{
auto itr = m_widgets.find(key.toStdString());
if(itr != m_widgets.end())
{
QLayoutItem* item = m_layout->itemByString(key);
Q_ASSERT(!!item);
QWidget* wdg = item->widget();
Q_ASSERT(!!wdg);
// remove item from layout
m_layout->removeItem(item);
// the widget still exists and is visible - remove it now
delete wdg;
// erase widget pointer from dictionary
m_widgets.erase(itr);
// repaint immediately, so we don't have dangling model views
m_layout->update();
}
}
/*
LinkedModelGroupsViewBase
*/
void LinkedModelGroupsView::modelChanged(LinkedModelGroups *groups)
{
LinkedModelGroupView* groupView = getGroupView();
LinkedModelGroup* group0 = groups->getGroup(0);
if(group0 && groupView)
{
groupView->modelChanged(group0);
}
}