1 /*
2 	Copyright (C) 2009 Andres Cabrera
3 	mantaraya36@gmail.com
4 
5 	This file is part of CsoundQt.
6 
7 	CsoundQt is free software; you can redistribute it
8 	and/or modify it under the terms of the GNU Lesser General Public
9 	License as published by the Free Software Foundation; either
10 	version 2.1 of the License, or (at your option) any later version.
11 
12 	CsoundQt 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 Lesser General Public License for more details.
16 
17 	You should have received a copy of the GNU Lesser General Public
18 	License along with Csound; if not, write to the Free Software
19 	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 	02111-1307 USA
21 */
22 
23 #include "liveeventframe.h"
24 #include "eventsheet.h"
25 
26 #include <QTextEdit>
27 #include <QLabel>
28 #include <QVBoxLayout>
29 #include <QLineEdit>
30 #include <QDialog>
31 #include <QDoubleSpinBox>
32 #include <QResizeEvent>
33 #include <QMessageBox>
34 
35 //Only for debug
36 #include <QtCore>
37 
LiveEventFrame(QString csdName,QWidget * parent,Qt::WindowFlags f)38 LiveEventFrame::LiveEventFrame(QString csdName, QWidget *parent, Qt::WindowFlags f) :
39 	QWidget(parent, f), m_csdName(csdName),
40 	m_ui(new Ui::LiveEventFrame)
41 {
42 	m_ui->setupUi(this);
43 
44 	setWindowTitle(m_csdName);
45 	//  setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
46 	m_sheet = new EventSheet(this);
47 	//  m_sheet->show();
48 	m_sheet->setTempo(60.0);
49 	m_sheet->setLoopLength(8.0);
50 	m_sheet->hide();
51 	connect(m_sheet,SIGNAL(modified()), this, SLOT(setModified()));
52 	connect(m_sheet,SIGNAL(setLoopRangeFromSheet(double,double)), this, SLOT(markLoop(double,double)));
53 	connect(m_sheet, SIGNAL(setLoopEnabledFromSheet(bool)), this, SLOT(setLoopEnabled(bool)));
54 
55 	m_ui->scrollArea->setWidget(m_sheet);
56 
57 	m_editor = new QTextEdit(this);
58 	m_editor->hide();
59 
60 	m_modified = false;
61 	m_mode = 0; // Sheet mode by default
62 
63 	m_ui->loopLengthSpinBox->setKeyboardTracking(false);
64 
65 	connect(m_ui->actionComboBox,SIGNAL(activated(int)), this, SLOT(doAction(int)));
66 	connect(m_ui->tempoSpinBox,SIGNAL(valueChanged(double)), this, SLOT(setTempo(double)));
67 	connect(m_ui->modeComboBox,SIGNAL(activated(int)), this, SLOT(setMode(int)));
68 	connect(m_ui->loopLengthSpinBox,SIGNAL(valueChanged(double)), this, SLOT(setLoopLength(double)));
69 
70 	//  setVisibleEnabled(true);
71 	//  this->setVisible(false);
72 	this->hide();
73 }
74 
~LiveEventFrame()75 LiveEventFrame::~LiveEventFrame()
76 {
77 	//  qDebug() << "LiveEventFrame::~LiveEventFrame()";
78 	disconnect(m_ui->actionComboBox, 0,0,0);
79 	disconnect(m_ui->tempoSpinBox, 0,0,0);
80 	disconnect(m_ui->modeComboBox, 0,0,0);
81 	disconnect(m_ui->loopLengthSpinBox, 0,0,0);
82 	disconnect(m_sheet, 0,0,0);
83 	delete m_sheet;
84 	delete m_editor;
85 	delete m_ui;
86 }
87 
getSheet()88 EventSheet * LiveEventFrame::getSheet()
89 {
90 	return m_sheet;
91 }
92 
setMode(int mode)93 void LiveEventFrame::setMode(int mode)
94 {
95 	if (m_mode == mode)
96 		return;
97 	if (mode == 0) {
98 		m_editor->hide();
99 		m_editor = static_cast<QTextEdit *>(m_ui->scrollArea->takeWidget());
100 		m_sheet->setFromText(m_editor->toPlainText(), 0,0, -1, -1);
101 		m_ui->scrollArea->setWidget(m_sheet);
102 		m_sheet->show();
103 	}
104 	else if (mode == 1) {
105 		m_sheet->stopAllEvents();
106 		m_sheet->hide();
107 		m_sheet = static_cast<EventSheet *>(m_ui->scrollArea->takeWidget());
108 		m_editor->setText(m_sheet->getPlainText());
109 		m_ui->scrollArea->setWidget(m_editor);
110 		m_editor->show();
111 	}
112 	m_mode = mode;
113 }
114 
setTempo(double tempo)115 void LiveEventFrame::setTempo(double tempo)
116 {
117 	//  qDebug() << "LiveEventFrame::setTempo";
118 	m_ui->tempoSpinBox->setValue(tempo);
119 	m_sheet->setTempo(tempo);
120 	emit setTempoFromPanel(this, tempo);  // Signal from sheet must go a long way to do this line...
121 	//TODO add sending tempo to other modes here too
122 }
123 
setName(QString name)124 void LiveEventFrame::setName(QString name)
125 {
126 	m_name = name;
127 	setWindowTitle(m_csdName + " -- " + m_name);
128 }
129 
setLoopLength(double length)130 void LiveEventFrame::setLoopLength(double length)
131 {
132 	//  qDebug() << "LiveEventFrame::setLoopLength";
133 	m_ui->loopLengthSpinBox->setValue(length);
134 	m_sheet->setLoopLength(length);
135 	emit setLoopLengthFromPanel(this, length);  // Signal from sheet must go a long way to do this line...
136 	//TODO add sending length to other modes here too
137 }
138 
setLoopRange(double start,double end)139 void LiveEventFrame::setLoopRange(double start, double end)
140 {
141 	m_sheet->markLoop(start,end);
142 	m_loopStart = start;
143 	m_loopEnd = end;
144 }
145 
setLoopEnabled(bool enabled)146 void LiveEventFrame::setLoopEnabled(bool enabled)
147 {
148 	emit setLoopEnabledFromPanel(this, enabled);  // Signal from sheet must go a long way to do this line...
149 }
150 
setModified(bool mod)151 void LiveEventFrame::setModified(bool mod)
152 {
153 	m_modified = mod;
154 }
155 
doAction(int action)156 void LiveEventFrame::doAction(int action)
157 {
158 	// TODO This really should be done with QActions
159 	if (action == 1) {
160 		newFrame();
161 	}
162 	else if (action == 2) {
163 		cloneFrame();
164 	}
165 	else if (action == 3) {
166 		deleteFrame(true);
167 		return;
168 	}
169 	else if (action == 4) {
170 		renameDialog();
171 	}
172 	else if (action == 5) {
173 		markLoop();
174 	}
175 	m_ui->actionComboBox->setCurrentIndex(0);
176 }
177 
newFrame()178 void LiveEventFrame::newFrame()
179 {
180 	emit(newFrameSignal(QString()));
181 }
182 
cloneFrame()183 void LiveEventFrame::cloneFrame()
184 {
185 	emit(newFrameSignal(m_sheet->getPlainText()));
186 }
187 
deleteFrame(bool ask)188 void LiveEventFrame::deleteFrame(bool ask)
189 {
190 	int ret = QMessageBox::Ok;
191 	if (ask) {
192 		ret = QMessageBox::question(this,
193 									tr("Delete Frame"),
194 									tr("Are you sure you want to delete this frame?"),
195 									QMessageBox::Ok | QMessageBox::Cancel,
196 									QMessageBox::Cancel);
197 	}
198 	if (ret == QMessageBox::Ok || !ask)
199 		emit(deleteFrameSignal(this));
200 }
201 
setFromText(QString text)202 void LiveEventFrame::setFromText(QString text)
203 {
204 	if (m_mode == 0) { // Sheet mode
205 		m_sheet->setFromText(text,0,0,0,0,true);
206 		m_sheet->clearHistory();
207 		m_sheet->markHistory();
208 		m_modified = false;
209 	}
210 	else if (m_mode == 1) { // text mode
211 		m_modified = false;
212 	}
213 }
214 
getTempo()215 double LiveEventFrame::getTempo()
216 {
217 	return m_ui->tempoSpinBox->value();
218 }
219 
getName()220 QString LiveEventFrame::getName()
221 {
222 	return m_name;
223 }
224 
getLoopLength()225 double LiveEventFrame::getLoopLength()
226 {
227 	return m_ui->loopLengthSpinBox->value();
228 }
229 
getPlainText()230 QString LiveEventFrame::getPlainText()
231 {
232 	QString panel = "<EventPanel name=\"";
233 	panel += getName() + "\" tempo=\"";
234 	panel += QString::number(getTempo(), 'f', 8) + "\" loop=\"";
235 	panel += QString::number(getLoopLength(), 'f', 8) + "\" x=\"";
236 	panel += QString::number(x()) + "\" y=\"";
237 	panel += QString::number(y()) + "\" width=\"";
238 	panel += QString::number(width()) + "\" height=\"";
239 	panel += QString::number(height()) + "\" visible=\"";
240 	panel += QString(getVisibleEnabled()? "true":"false") + "\" loopStart=\"";
241 	panel += QString::number(m_loopStart) + "\" loopEnd=\"";
242 	panel += QString::number(m_loopEnd) + "\">";
243 	panel += m_sheet->getPlainText();
244 	panel += "</EventPanel>\n";
245 	return panel;
246 }
247 
getEvents(unsigned long,QStringList *)248 void LiveEventFrame::getEvents(unsigned long /*ksmps*/, QStringList */*eventText*/)
249 {
250 	// TODO: implement
251 }
252 
isModified()253 bool LiveEventFrame::isModified()
254 {
255 	return m_modified;
256 }
257 
258 //void LiveEventFrame::forceDestroy()
259 //{
260 //  this->destroy();
261 //}
262 
renameDialog()263 void LiveEventFrame::renameDialog()
264 {
265 	QDialog d;
266 	QVBoxLayout l(&d);
267 	QLabel label(tr("Enter new name", "New name for Live Event panel"));
268 	QLineEdit line;
269 	//  d.resize(300, d.height());
270 	//  line.resize(300, line.height());
271 	line.setText(m_name);
272 	l.addWidget(&label);
273 	l.addWidget(&line);
274 	connect(&line, SIGNAL(editingFinished()), &d, SLOT(accept ()) );
275 	int ret = d.exec();
276 	if (ret == QDialog::Accepted) {
277 		emit renamePanel(this, line.text());
278 	}
279 }
280 
markLoop(double start,double end)281 void LiveEventFrame::markLoop(double start, double end)
282 {
283 	QPair<int, int> range = m_sheet->getSelectedRowsRange();
284 	if (start == -1) {
285 		start = range.first;
286 	}
287 	if (end == -1) {
288 		end = range.second;
289 	}
290 	m_loopStart = start;
291 	m_loopEnd = end;
292 	m_sheet->markLoop(start, end);
293 	emit setLoopRangeFromPanel(this, start, end);  // Signal from sheet must go a long way to do this line...
294 }
295 
loopPanel(bool loop)296 void LiveEventFrame::loopPanel(bool loop)
297 {
298 	m_sheet->setLoopActive(loop);
299 }
300 
301 //void LiveEventFrame::changeEvent(QEvent *e)
302 //{
303 //  QFrame::changeEvent(e);
304 //  switch (e->type()) {
305 //  case QEvent::LanguageChange:
306 //    retranslateUi(this);
307 //    break;
308 //  default:
309 //    break;
310 //  }
311 //}
312 
closeEvent(QCloseEvent * event)313 void LiveEventFrame::closeEvent (QCloseEvent * event)
314 {
315 	emit closed(false);
316 	event->accept();
317 }
318 
getLoopStart()319 double LiveEventFrame::getLoopStart()
320 {
321 	return m_loopStart;
322 }
323 
getLoopEnd()324 double LiveEventFrame::getLoopEnd()
325 {
326 	return m_loopEnd;
327 }
328 
329 //void LiveEventFrame::hideEvent (QHideEvent * event)
330 //{
331 //  qDebug() << "LiveEventFrame::hideEvent";
332 //  setVisibleEnabled(false);
333 //  QFrame::hideEvent(event);
334 //}
335 //
336 //void LiveEventFrame::showEvent (QShowEvent * event)
337 //{
338 //  qDebug() << "LiveEventFrame::showEvent";
339 //  setVisibleEnabled(true);
340 //  QFrame::showEvent(event);
341 //}
342 
343