| 1 | /* |
| 2 | Copyright (C) 2010 Andres Cabrera |
| 3 | [email protected] |
| 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 "liveeventcontrol.h" |
| 24 | #include "ui_liveeventcontrol.h" |
| 25 | |
| 26 | LiveEventControl::LiveEventControl(QWidget *parent) : |
| 27 | QWidget(parent), |
| 28 | m_ui(new Ui::LiveEventControl) |
| 29 | { |
| 30 | this->setWindowFlags(Qt::Window); |
| 31 | m_ui->setupUi(this); |
| 32 | m_ui->panelTableWidget->setColumnWidth(0,40); |
| 33 | m_ui->panelTableWidget->setColumnWidth(1,40); |
| 34 | m_ui->panelTableWidget->setColumnWidth(2,40); |
| 35 | m_ui->panelTableWidget->setColumnWidth(3,40); |
| 36 | m_ui->panelTableWidget->setColumnWidth(4,280); |
| 37 | m_ui->panelTableWidget->setColumnWidth(5,80); |
| 38 | m_ui->panelTableWidget->setColumnWidth(6,80); |
| 39 | m_ui->panelTableWidget->setColumnWidth(7,50); |
| 40 | m_ui->stopAllButton->hide(); // TODO implement stop all |
| 41 | connect(m_ui->newButton,SIGNAL(released()), this, SLOT(newButtonReleased())); |
| 42 | connect(m_ui->panelTableWidget, SIGNAL(cellChanged ( int , int )), this, SLOT(cellChangedSlot( int , int ))); |
| 43 | connect(m_ui->panelTableWidget, SIGNAL(cellClicked ( int , int )), this, SLOT(cellClickedSlot( int , int ))); |
| 44 | } |
| 45 | |
| 46 | LiveEventControl::~LiveEventControl() |
| 47 | { |
| 48 | delete m_ui; |
| 49 | } |
| 50 | |
| 51 | void LiveEventControl::renamePanel(int index, QString newName) |
| 52 | { |
| 53 | QTableWidgetItem * item = getItem(index, 4); |
| 54 | item->setText(newName); |
| 55 | } |
| 56 | |
| 57 | void LiveEventControl::setPanelLoopRange(int index, double start, double end) |
| 58 | { |
| 59 | QTableWidgetItem * item = getItem(index, 6); |
| 60 | item->setText(QString::number(start + 1) + "-" + QString::number(end + 1)); |
| 61 | } |
| 62 | |
| 63 | void LiveEventControl::setPanelLoopLength(int index, double length) |
| 64 | { |
| 65 | QTableWidgetItem * item = getItem(index, 5); |
| 66 | item->setText(QString::number(length)); |
| 67 | } |
| 68 | |
| 69 | void LiveEventControl::setPanelLoopEnabled(int index, bool enabled) |
| 70 | { |
| 71 | QTableWidgetItem * item = getItem(index, 2); |
| 72 | item->setCheckState(enabled ? Qt::Checked : Qt::Unchecked); |
| 73 | } |
| 74 | |
| 75 | void LiveEventControl::setPanelTempo(int index, double tempo) |
| 76 | { |
| 77 | QTableWidgetItem * item = getItem(index, 7); |
| 78 | item->setText(QString::number(tempo)); |
| 79 | } |
| 80 | |
| 81 | void LiveEventControl::removePanel(int index) |
| 82 | { |
| 83 | qDebug() << "LiveEventControl::removePanel " << index; |
| 84 | m_ui->panelTableWidget->removeRow(index); |
| 85 | } |
| 86 | |
| 87 | void LiveEventControl::appendPanel(LiveEventFrame *e) |
| 88 | { |
| 89 | this->blockSignals(true); // To avoid showing the panels when adding them (e.g. when starting up) |
| 90 | int newRow = m_ui->panelTableWidget->rowCount(); |
| 91 | // qDebug() << "LiveEventControl::appendPanel " << newRow; |
| 92 | m_ui->panelTableWidget->insertRow(newRow); |
| 93 | m_ui->panelTableWidget->setRowHeight(newRow, 20); |
| 94 | QTableWidgetItem *visibleItem = getItem(newRow, 0); |
| 95 | // visibleItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable); |
| 96 | visibleItem->setCheckState(e->getVisibleEnabled() ? Qt::Checked : Qt::Unchecked); |
| 97 | connect(e, SIGNAL(closed(LiveEventFrame *)), this, SLOT(frameClosed(LiveEventFrame *)) ); |
| 98 | QTableWidgetItem *playItem = getItem(newRow, 1); |
| 99 | playItem->setIcon(QIcon(":/themes/boring/gtk-media-play-ltr.png")); |
| 100 | // playItem->setCheckState(visible ? Qt::Checked : Qt::Unchecked); |
| 101 | QTableWidgetItem *loopItem = getItem(newRow, 2); |
| 102 | loopItem->setCheckState(Qt::Unchecked); |
| 103 | // QTableWidgetItem *syncItem = getItem(newRow, 3); |
| 104 | // loopItem->setCheckState(visible ? Qt::Checked : Qt::Unchecked); |
| 105 | QTableWidgetItem *nameItem = getItem(newRow, 4); |
| 106 | nameItem->setData(Qt::DisplayRole, QVariant(e->getName())); |
| 107 | QTableWidgetItem *loopLengthItem = getItem(newRow, 5); |
| 108 | loopLengthItem->setData(Qt::DisplayRole, QVariant(e->getLoopLength())); |
| 109 | |
| 110 | QTableWidgetItem *loopRangeItem = getItem(newRow, 6); |
| 111 | loopRangeItem->setText(QString::number(e->getLoopStart() + 1) + "-" |
| 112 | + QString::number(e->getLoopEnd() + 1) ); |
| 113 | // loopRangeItem->setCheckState(visible ? Qt::Checked : Qt::Unchecked); |
| 114 | QTableWidgetItem *tempoItem = getItem(newRow, 7); |
| 115 | tempoItem->setData(Qt::DisplayRole, QVariant(e->getTempo())); |
| 116 | |
| 117 | this->blockSignals(false); |
| 118 | // setPanelProperty(newRow, "LE_visible", visible); |
| 119 | // setPanelProperty(newRow, "LE_play", play); |
| 120 | // setPanelProperty(newRow, "LE_loop", loop); |
| 121 | // setPanelProperty(newRow, "LE_sync", sync); |
| 122 | // setPanelProperty(newRow, "LE_name", name); |
| 123 | // setPanelProperty(newRow, "LE_loopLength", loopLength); |
| 124 | // setPanelProperty(newRow, "LE_loopRange", loopRange); |
| 125 | // setPanelProperty(newRow, "LE_tempo", tempo); |
| 126 | } |
| 127 | |
| 128 | void LiveEventControl::setPanelProperty(int index, QString property, QVariant value) |
| 129 | { |
| 130 | if (index < 0 || index >= m_ui->panelTableWidget->rowCount()) { |
| 131 | qDebug() << "LiveEventControl::setPanelProperty invalid index " << index; |
| 132 | return; |
| 133 | } |
| 134 | if (property == "LE_visible") { |
| 135 | emit setPanelVisible(index, value.toBool()); |
| 136 | } |
| 137 | // else if (property == "LE_play") { |
| 138 | // if (value.toBool()) { |
| 139 | // emit playPanel(index); |
| 140 | // } |
| 141 | // else { |
| 142 | // emit stopPanel(index); |
| 143 | // } |
| 144 | // } |
| 145 | // else if (property == "LE_loop") { |
| 146 | // emit loopPanel(index, value.toBool()); |
| 147 | // } |
| 148 | else if (property == "LE_sync") { |
| 149 | emit setPanelSync(index, value.toInt()); |
| 150 | } |
| 151 | else if (property == "LE_name") { |
| 152 | emit setPanelNameSignal(index, value.toString()); |
| 153 | } |
| 154 | else { |
| 155 | qDebug() << "LiveEventControl::setPanelProperty unknown property " << property; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | QTableWidgetItem * LiveEventControl::getItem(int row, int column) |
| 160 | { |
| 161 | QTableWidgetItem * item = m_ui->panelTableWidget->item(row, column); |
| 162 | if (item == 0) { |
| 163 | item = new QTableWidgetItem(QTableWidgetItem::Type); |
| 164 | m_ui->panelTableWidget->setItem(row, column, item); |
| 165 | } |
| 166 | if (column == 0 || column == 2) { |
| 167 | item->setFlags(Qt::ItemIsUserCheckable |Qt::ItemIsEnabled); |
| 168 | } |
| 169 | else if (column == 4) { |
| 170 | item->setFlags(Qt::ItemIsEditable | Qt::ItemIsEnabled); |
| 171 | } |
| 172 | else { |
| 173 | item->setFlags(Qt::ItemIsEnabled); |
| 174 | } |
| 175 | return item; |
| 176 | } |
| 177 | |
| 178 | void LiveEventControl::openLoopRangeDialog(int row) |
| 179 | { |
| 180 | QDialog d; |
| 181 | QVBoxLayout l(&d); |
| 182 | QLabel ls(tr("Loop start"),&d); |
| 183 | QSpinBox ss(&d); |
| 184 | QLabel le(tr("Loop end"),&d); |
| 185 | QSpinBox se(&d); |
| 186 | QPushButton okButton(tr("Ok"), &d); |
| 187 | QPushButton cancelButton(tr("Cancel"), &d); |
| 188 | connect(&okButton,SIGNAL(released()),&d,SLOT(accept())); |
| 189 | connect(&cancelButton,SIGNAL(released()),&d,SLOT(reject())); |
| 190 | l.addWidget(&ls); |
| 191 | l.addWidget(&ss); |
| 192 | l.addWidget(&le); |
| 193 | l.addWidget(&se); |
| 194 | l.addWidget(&okButton); |
| 195 | l.addWidget(&cancelButton); |
| 196 | QTableWidgetItem *item = getItem(row,6); |
| 197 | QStringList bounds(item->text().split("-")); |
| 198 | if (bounds.size() > 1) { |
| 199 | if (bounds[0].toInt() >= 0) { |
| 200 | ss.setValue(bounds[0].toInt()); |
| 201 | } |
| 202 | if (bounds[1].toInt() >= 0) { |
| 203 | se.setValue(bounds[1].toInt()); |
| 204 | } |
| 205 | } |
| 206 | int ret = d.exec(); |
| 207 | if (ret == QDialog::Accepted) { |
| 208 | QString range = QString::number(ss.value()) + "-" + QString::number(se.value()); |
| 209 | qDebug() << "LiveEventControl::openLoopRangeDialog " << row << range; |
| 210 | QTableWidgetItem *item = m_ui->panelTableWidget->item(row,6); |
| 211 | item->setText(range); |
| 212 | emit setPanelLoopRangeSignal(row, ss.value() - 1,se.value() - 1); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | void LiveEventControl::closeEvent(QCloseEvent * event) |
| 217 | { |
| 218 | qDebug() << "LiveEventControl::closeEvent"; |
| 219 | this->hide(); |
| 220 | emit closed(); |
| 221 | event->accept(); |
| 222 | } |
| 223 | |
| 224 | void LiveEventControl::changeEvent(QEvent *e) |
| 225 | { |
| 226 | QWidget::changeEvent(e); |
| 227 | switch (e->type()) { |
| 228 | case QEvent::LanguageChange: |
| 229 | m_ui->retranslateUi(this); |
| 230 | break; |
| 231 | default: |
| 232 | break; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | void LiveEventControl::newButtonReleased() |
| 237 | { |
| 238 | emit newPanel(); |
| 239 | } |
| 240 | |
| 241 | void LiveEventControl::cellChangedSlot(int row, int column) |
| 242 | { |
| 243 | QTableWidgetItem *item = m_ui->panelTableWidget->item(row, column); |
| 244 | if (column == 0) { // Visible |
| 245 | emit setPanelVisible(row, item->checkState() == Qt::Checked); |
| 246 | } |
| 247 | else if (column == 2) { // Loop |
| 248 | emit loopPanel(row, item->checkState() == Qt::Checked); |
| 249 | } |
| 250 | else if (column == 4) { // Name |
| 251 | emit setPanelNameSignal(row, item->data(Qt::DisplayRole).toString()); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | void LiveEventControl::cellClickedSlot(int row, int column) |
| 256 | { |
| 257 | if (column == 1) { // Play |
| 258 | qDebug() << "LiveEventControl::cellChangedSlot play"; |
| 259 | emit playPanel(row); |
| 260 | } |
| 261 | else if (column == 3) { // Sync |
| 262 | qDebug() << "Sync not implemented yet."; |
| 263 | } |
| 264 | else if (column == 6) { // Loop Range |
| 265 | openLoopRangeDialog(row); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | void LiveEventControl::frameClosed(LiveEventFrame *e) |
| 270 | { |
| 271 | (void) e; |
| 272 | qDebug() << "Not implemented."; |
| 273 | } |
| 274 | |
| 275 | void LiveEventControl::on_closeAllButton_clicked() |
| 276 | { |
| 277 | emit hidePanels(); |
| 278 | } |