1 // qsamplerFxSendList.cpp
2 //
3 /****************************************************************************
4    Copyright (C) 2010-2019, rncbc aka Rui Nuno Capela. All rights reserved.
5    Copyright (C) 2008, Christian Schoenebeck
6 
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License
9    as published by the Free Software Foundation; either version 2
10    of the License, or (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 
21 *****************************************************************************/
22 
23 #include "qsamplerAbout.h"
24 #include "qsamplerFxSendsModel.h"
25 #include "qsamplerFxSend.h"
26 
27 #include <QBrush>
28 #include <QIcon>
29 #include <QFont>
30 
31 namespace QSampler {
32 
33 
FxSendsModel(int iChannelID,QObject * pParent)34 FxSendsModel::FxSendsModel ( int iChannelID, QObject* pParent )
35 	: QAbstractListModel(pParent)
36 {
37 	m_iChannelID = iChannelID;
38 	cleanRefresh();
39 }
40 
41 
rowCount(const QModelIndex &) const42 int FxSendsModel::rowCount ( const QModelIndex& /*parent*/ ) const
43 {
44 	return m_fxSends.size();
45 }
46 
47 
data(const QModelIndex & index,int role) const48 QVariant FxSendsModel::data ( const QModelIndex& index, int role ) const
49 {
50 	if (!index.isValid())
51 		return QVariant();
52 
53 	switch (role) {
54 		case Qt::DisplayRole:
55 			return m_fxSends[index.row()].name();
56 			break;
57 		case Qt::ToolTipRole:
58 			if (m_fxSends[index.row()].deletion())
59 				return QString(
60 					"Scheduled for deletion. Click on 'Apply' to actually "
61 					"destroy FX Send."
62 				);
63 			return (m_fxSends[index.row()].isNew()) ?
64 						QString(
65 							"New FX send. Click on 'Apply' to actually "
66 							"perform creation."
67 						) :
68 						QString("FX Send ID ") +
69 						QString::number(m_fxSends.at(index.row()).id());
70 			break;
71 		case Qt::ForegroundRole:
72 			if (m_fxSends.at(index.row()).deletion())
73 				return QBrush(Qt::red);
74 			if (m_fxSends.at(index.row()).isNew())
75 				return QBrush(Qt::green);
76 			break;
77 		case Qt::DecorationRole:
78 			if (m_fxSends.at(index.row()).deletion())
79 				return QIcon(":/images/formRemove.png");
80 			if (m_fxSends.at(index.row()).isNew())
81 				return QIcon(":/images/itemNew.png");
82 			if (m_fxSends.at(index.row()).isModified())
83 				return QIcon(":/images/formEdit.png");
84 			return QIcon(":/images/itemFile.png");
85 		case Qt::FontRole: {
86 			if (m_fxSends.at(index.row()).isModified()) {
87 				QFont font;
88 				font.setBold(true);
89 				return font;
90 			}
91 			break;
92 		}
93 		default:
94 			return QVariant();
95 	}
96 
97 	return QVariant();
98 }
99 
100 
setData(const QModelIndex & index,const QVariant & value,int)101 bool FxSendsModel::setData (
102 	const QModelIndex& index, const QVariant& value, int /*role*/ )
103 {
104 	if (!index.isValid())
105 		return false;
106 
107 	m_fxSends[index.row()].setName(value.toString());
108 	emit dataChanged(index, index);
109 	emit fxSendsDirtyChanged(true);
110 
111 	return true;
112 }
113 
114 
headerData(int section,Qt::Orientation,int role) const115 QVariant FxSendsModel::headerData (
116 	int section, Qt::Orientation /*orientation*/, int role ) const
117 {
118 	if (role == Qt::DisplayRole && section == 0)
119 		return QString("FX Send Name");
120 	else
121 		return QVariant();
122 }
123 
124 
flags(const QModelIndex &) const125 Qt::ItemFlags FxSendsModel::flags ( const QModelIndex& /*index*/) const
126 {
127 	return Qt::ItemIsEditable | Qt::ItemIsEnabled;
128 }
129 
130 
addFxSend(void)131 FxSend *FxSendsModel::addFxSend (void)
132 {
133 #if CONFIG_FXSEND
134 	FxSend fxSend(m_iChannelID);
135 	fxSend.setName("New FX Send");
136 	m_fxSends.push_back(fxSend);
137 	createIndex(m_fxSends.size() - 1, 0);
138 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
139 	QAbstractListModel::reset();
140 #else
141 	QAbstractListModel::beginResetModel();
142 	QAbstractListModel::endResetModel();
143 #endif
144 	emit fxSendsDirtyChanged(true);
145 	return &m_fxSends.last();
146 #else
147 	return nullptr;
148 #endif // CONFIG_FXSEND
149 }
150 
151 
fxSend(const QModelIndex & index)152 FxSend *FxSendsModel::fxSend ( const QModelIndex& index )
153 {
154 	if (!index.isValid())
155 		return nullptr;
156 
157 	return &m_fxSends[index.row()];
158 }
159 
160 
removeFxSend(const QModelIndex & index)161 void FxSendsModel::removeFxSend ( const QModelIndex& index )
162 {
163 	FxSend *pFxSend = fxSend(index);
164 	if (!pFxSend) return;
165 	pFxSend->setDeletion(true);
166 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
167 	QAbstractListModel::reset();
168 #else
169 	QAbstractListModel::beginResetModel();
170 	QAbstractListModel::endResetModel();
171 #endif
172 	emit fxSendsDirtyChanged(true);
173 }
174 
175 
cleanRefresh(void)176 void FxSendsModel::cleanRefresh (void)
177 {
178 	m_fxSends.clear();
179 	const QList<int>& sends = FxSend::allFxSendsOfSamplerChannel(m_iChannelID);
180 	for (int i = 0; i < sends.size(); ++i) {
181 		const int iFxSendId = sends.at(i);
182 		FxSend fxSend(m_iChannelID, iFxSendId);
183 		fxSend.getFromSampler();
184 		m_fxSends.push_back(fxSend);
185 	}
186 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
187 	QAbstractListModel::reset();
188 #else
189 	QAbstractListModel::beginResetModel();
190 	QAbstractListModel::endResetModel();
191 #endif
192 	emit fxSendsDirtyChanged(false);
193 }
194 
195 
onExternalModifiication(const QModelIndex & index)196 void FxSendsModel::onExternalModifiication ( const QModelIndex& index )
197 {
198 	if (!index.isValid()) return;
199 	emit dataChanged(index, index);
200 	emit fxSendsDirtyChanged(true);
201 }
202 
203 
applyToSampler(void)204 void FxSendsModel::applyToSampler (void)
205 {
206 	for (int i = 0; i < m_fxSends.size(); ++i)
207 		m_fxSends[i].applyToSampler();
208 
209 	// make a clean refresh
210 	// (throws out all FxSend objects marked for deletion)
211 	cleanRefresh();
212 }
213 
214 
215 } // namespace QSampler
216 
217 
218 // end of qsamplerFxSendList.cpp
219