1 //=========================================================
2 // MusE
3 // Linux Music Editor
4 // Copyright (C) 1999-2011 by Werner Schweer and others
5 //
6 // compact_patch_edit.cpp
7 // (C) Copyright 2015-2016 Tim E. Real (terminator356 on sourceforge)
8 //
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License
11 // as published by the Free Software Foundation; version 2 of
12 // the License, or (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 //=========================================================
24
25 #include <QVBoxLayout>
26 #include <QMouseEvent>
27 #include <QStyle>
28
29 #include "compact_patch_edit.h"
30 #include "lcd_widgets.h"
31 #include "elided_label.h"
32 #include "gconfig.h"
33
34 namespace MusEGui {
35
CompactPatchEdit(QWidget * parent,const char * name,QColor)36 CompactPatchEdit::CompactPatchEdit(QWidget *parent,
37 const char *name,
38 QColor /*fillColor*/)
39 : QFrame(parent)
40
41 {
42 setObjectName(name);
43
44 _orient = ReadoutHorizontal;
45 _showPatchLabel = true;
46
47 _maxAliasedPointSize = -1;
48 _id = -1;
49 _currentPatch = 0;
50
51 _style3d = true;
52 _radius = 2;
53
54 _patchNameLabel = new ElidedLabel(nullptr, Qt::ElideNone);
55 _patchNameLabel->setObjectName("CompactPatchEditLabel");
56 _patchNameLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Minimum);
57 _patchNameLabel->setHasOffMode(true);
58 _patchNameLabel->setToolTip(tr("Patch name"));
59 _patchNameLabel->setContentsMargins(0, 0, 0, 0);
60
61 // _patchNameLabel->setBgColor(MusEGlobal::config.midiInstrumentBackgroundColor);
62 // _patchNameLabel->setBgActiveColor(MusEGlobal::config.midiInstrumentBgActiveColor);
63 // _patchNameLabel->setBorderColor(MusEGlobal::config.midiInstrumentBorderColor);
64
65 _patchEdit = new LCDPatchEdit();
66
67 QVBoxLayout* layout = new QVBoxLayout(this);
68 layout->setSpacing(0);
69 layout->setContentsMargins(0, 0, 0, 0);
70
71 layout->addWidget(_patchNameLabel);
72 layout->addWidget(_patchEdit);
73
74 connect(_patchNameLabel, SIGNAL(pressed(QPoint,int,Qt::MouseButtons,Qt::KeyboardModifiers)),
75 SLOT(patchNamePressed(QPoint,int,Qt::MouseButtons,Qt::KeyboardModifiers)));
76 connect(_patchNameLabel, SIGNAL(returnPressed(QPoint,int,Qt::KeyboardModifiers)),
77 SLOT(patchNameReturnPressed(QPoint,int,Qt::KeyboardModifiers)));
78
79 connect(_patchEdit, SIGNAL(valueChanged(int,int)),
80 SLOT(patchEditValueChanged(int,int)));
81
82 connect(_patchEdit, SIGNAL(rightClicked(QPoint,int)),
83 SLOT(patchEditRightClicked(QPoint,int)));
84 }
85
~CompactPatchEdit()86 CompactPatchEdit::~CompactPatchEdit()
87 {
88 if(_patchEdit)
89 delete _patchEdit;
90 }
91
92 // Static.
getMinimumSizeHint(const QFontMetrics & fm,Qt::Orientation orient,int xMargin,int yMargin)93 QSize CompactPatchEdit::getMinimumSizeHint(const QFontMetrics& fm,
94 Qt::Orientation orient,
95 int xMargin,
96 int yMargin)
97 {
98 const QSize ctrl_sz = LCDPatchEdit::getMinimumSizeHint(
99 fm,
100 xMargin,
101 yMargin,
102 orient == Qt::Horizontal ? LCDPatchEdit::PatchHorizontal : LCDPatchEdit::PatchVertical
103 );
104
105 // HACK Try to find the size of a label
106 // QLabel* dummyLabel = new QLabel("WWW", this);
107 // dummyLabel->setMargin(yMargin);
108 // const QSize lbl_sz = dummyLabel->sizeHint();
109 const int lbl_h = fm.height() + 2 * yMargin;
110
111 const int h = ctrl_sz.height() + lbl_h;
112
113 switch(orient) {
114 case Qt::Horizontal:
115 return QSize(ctrl_sz.width(), h); // Patch edit is dominant.
116 break;
117 case Qt::Vertical:
118 return QSize(16, h);
119 break;
120 }
121 return QSize(10, 10);
122 }
123
setupComponentTabbing(QWidget * previousWidget)124 QWidget* CompactPatchEdit::setupComponentTabbing(QWidget* previousWidget)
125 {
126 QWidget* prev = previousWidget;
127 if(_patchNameLabel)
128 {
129 if(prev)
130 QWidget::setTabOrder(prev, _patchNameLabel);
131 prev = _patchNameLabel;
132 }
133 if(_patchEdit)
134 {
135 if(prev)
136 QWidget::setTabOrder(prev, _patchEdit);
137 prev = _patchEdit;
138 }
139 return prev;
140 }
141
142 // void CompactPatchEdit::keyPressEvent(QKeyEvent* e)
143 // {
144 // switch (e->key())
145 // {
146 // case Qt::Key_Escape:
147 // break;
148 //
149 // default:
150 // break;
151 // }
152 //
153 // e->ignore();
154 // return QFrame::keyPressEvent(e);
155 // }
156
setReadoutColor(const QColor & c)157 void CompactPatchEdit::setReadoutColor(const QColor& c)
158 {
159 _patchEdit->setReadoutColor(c);
160 // Colour was not updating with stylesheet colour.
161 // This trick recommended by help. Tested OK.
162 // Good demonstration of how to apply a stylesheet after a property has been changed.
163 // if(style())
164 // {
165 // style()->unpolish(_patchEdit);
166 // style()->polish(_patchEdit);
167 // }
168
169 // update();
170 }
171
setBgColor(const QColor & c)172 void CompactPatchEdit::setBgColor(const QColor& c)
173 {
174 _patchNameLabel->setBgColor(c);
175 _patchEdit->setBgColor(c);
176 }
setBgActiveColor(const QColor & c)177 void CompactPatchEdit::setBgActiveColor(const QColor& c)
178 {
179 _patchNameLabel->setBgActiveColor(c);
180 _patchEdit->setBgActiveColor(c);
181 }
setBorderColor(const QColor & c)182 void CompactPatchEdit::setBorderColor(const QColor& c)
183 {
184 _patchNameLabel->setBorderColor(c);
185 _patchEdit->setBorderColor(c);
186 }
setBorderColorPatchEdit(const QColor & c)187 void CompactPatchEdit::setBorderColorPatchEdit(const QColor &c)
188 {
189 _patchEdit->setBorderColor(c);
190 }
setFontColor(const QColor & c)191 void CompactPatchEdit::setFontColor(const QColor& c)
192 {
193 _patchNameLabel->setFontColor(c);
194 }
setFontActiveColor(const QColor & c)195 void CompactPatchEdit::setFontActiveColor(const QColor& c)
196 {
197 _patchNameLabel->setFontActiveColor(c);
198 }
199
setStyle3d(const bool s)200 void CompactPatchEdit::setStyle3d(const bool s)
201 {
202 _style3d = s;
203 _patchNameLabel->setStyle3d(s);
204 _patchEdit->setStyle3d(s);
205 }
setRadius(const int r)206 void CompactPatchEdit::setRadius(const int r)
207 {
208 _radius = r;
209 _patchNameLabel->setRadius(r);
210 _patchEdit->setRadius(r);
211 }
212
setReadoutOrientation(ReadoutOrientation orient)213 void CompactPatchEdit::setReadoutOrientation(ReadoutOrientation orient)
214 {
215 _orient = orient;
216 }
217
setShowPatchLabel(bool v)218 void CompactPatchEdit::setShowPatchLabel(bool v)
219 {
220 _showPatchLabel = v;
221 }
222
setMaxAliasedPointSize(int sz)223 void CompactPatchEdit::setMaxAliasedPointSize(int sz)
224 {
225 _maxAliasedPointSize = sz;
226 _patchEdit->setMaxAliasedPointSize(sz);
227 }
228
229 // QSize CompactPatchEdit::sizeHint() const
230 // {
231 // return getMinimumSizeHint(fontMetrics(),
232 // _orient == ReadoutHorizontal ? Qt::Horizontal : Qt::Vertical,
233 // frameWidth(), //_xMargin,
234 // frameWidth() //_yMargin
235 // );
236 // }
237
value() const238 int CompactPatchEdit::value() const
239 {
240 return _currentPatch;
241 }
242
setValue(int v)243 void CompactPatchEdit::setValue(int v)
244 {
245 if(_currentPatch != v)
246 {
247 _currentPatch = v;
248 //update();
249 }
250 _patchEdit->setValue(v);
251 }
252
setLastValidValue(int v)253 void CompactPatchEdit::setLastValidValue(int v)
254 {
255 _patchEdit->setLastValidPatch(v);
256 }
257
setLastValidBytes(int hbank,int lbank,int prog)258 void CompactPatchEdit::setLastValidBytes(int hbank, int lbank, int prog)
259 {
260 _patchEdit->setLastValidBytes(hbank, lbank, prog);
261 }
262
patchName() const263 QString CompactPatchEdit::patchName() const
264 {
265 return _patchNameLabel->text();
266 }
267
setPatchName(const QString & patchName)268 void CompactPatchEdit::setPatchName(const QString& patchName)
269 {
270 _patchNameLabel->setText(patchName);
271 }
272
setPatchNameOff(bool v)273 void CompactPatchEdit::setPatchNameOff(bool v)
274 {
275 _patchNameLabel->setOff(v);
276 }
277
patchEditValueChanged(int val,int)278 void CompactPatchEdit::patchEditValueChanged(int val, int /*id*/)
279 {
280 _currentPatch = val;
281 emit valueChanged(_currentPatch, _id);
282 }
283
patchEditDoubleClicked(QPoint,int,Qt::MouseButtons buttons,Qt::KeyboardModifiers keys)284 void CompactPatchEdit::patchEditDoubleClicked(QPoint /*p*/, int /*id*/, Qt::MouseButtons buttons, Qt::KeyboardModifiers keys)
285 {
286 if(buttons == Qt::LeftButton && keys == (Qt::ControlModifier | Qt::ShiftModifier))
287 {
288
289 }
290 }
291
patchEditRightClicked(QPoint p,int)292 void CompactPatchEdit::patchEditRightClicked(QPoint p, int /*id*/)
293 {
294 emit patchValueRightClicked(p, _id);
295 }
296
patchNamePressed(QPoint p,int,Qt::MouseButtons buttons,Qt::KeyboardModifiers)297 void CompactPatchEdit::patchNamePressed(QPoint p, int /*id*/, Qt::MouseButtons buttons, Qt::KeyboardModifiers /*keys*/)
298 {
299 if(buttons == Qt::LeftButton)
300 emit patchNameClicked(p, _id);
301 else if(buttons == Qt::RightButton)
302 emit patchNameRightClicked(mapToGlobal(p), _id);
303 }
304
patchNameReturnPressed(QPoint p,int,Qt::KeyboardModifiers)305 void CompactPatchEdit::patchNameReturnPressed(QPoint p, int /*id*/, Qt::KeyboardModifiers /*keys*/)
306 {
307 emit patchNameClicked(p, _id);
308 }
309
310
311 } // namespace MusEGui
312