| 1 | /* |
| 2 | Copyright (C) 2013 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 "debugpanel.h" |
| 24 | #include "ui_debugpanel.h" |
| 25 | |
| 26 | #include <QInputDialog> |
| 27 | #include <QModelIndex> |
| 28 | #include <QTableWidget> |
| 29 | #include <QCheckBox> |
| 30 | |
| 31 | #include <QDebug> |
| 32 | |
| 33 | #include "newbreakpointdialog.h" |
| 34 | |
| 35 | DebugPanel::DebugPanel(QWidget *parent) : |
| 36 | QDockWidget(parent), |
| 37 | ui(new Ui::DebugPanel) |
| 38 | { |
| 39 | ui->setupUi(this); |
| 40 | |
| 41 | ui->breakpointTableWidget->setShowGrid(false); |
| 42 | |
| 43 | connect(ui->runButton, SIGNAL(toggled(bool)), |
| 44 | this, SLOT(runToggle(bool))); |
| 45 | connect(ui->pauseButton, SIGNAL(released()), |
| 46 | this, SLOT(pause())); |
| 47 | connect(ui->continueButton, SIGNAL(released()), |
| 48 | this, SLOT(continueDebug())); |
| 49 | connect(ui->nextButton, SIGNAL(released()), |
| 50 | this, SLOT(next())); |
| 51 | connect(ui->newBreakpointToolButton, SIGNAL(released()), |
| 52 | this, SLOT(newBreakpoint())); |
| 53 | connect(ui->deleteBreakpointToolButton, SIGNAL(released()), |
| 54 | this, SLOT(deleteBreakpoint())); |
| 55 | |
| 56 | connect(ui->breakpointTableWidget, SIGNAL(cellChanged(int,int)), |
| 57 | this, SLOT(cellChanged(int,int))); |
| 58 | connect(ui->breakpointTableWidget, SIGNAL(currentCellChanged(int,int,int,int)), |
| 59 | this, SLOT(currentCellChanged(int,int,int,int))); |
| 60 | } |
| 61 | |
| 62 | DebugPanel::~DebugPanel() |
| 63 | { |
| 64 | delete ui; |
| 65 | } |
| 66 | |
| 67 | QVector<QVariantList> DebugPanel::getBreakpoints() |
| 68 | { |
| 69 | QTableWidget *table = ui->breakpointTableWidget; |
| 70 | QVector<QVariantList> breakpoints; |
| 71 | for (int row = 0; row < table->rowCount(); row++) { |
| 72 | QVariantList bp; |
| 73 | QCheckBox *cb = static_cast<QCheckBox *>(table->cellWidget(row,0)); |
| 74 | if (cb->isChecked()) { |
| 75 | QTableWidgetItem *item = table->item(row,1); |
| 76 | bp << item->data(Qt::DisplayRole).toString(); |
| 77 | if (bp.back().toString() == "instr" ) { |
| 78 | item = table->item(row,2); |
| 79 | bp << item->data(Qt::DisplayRole).toDouble(); |
| 80 | item = table->item(row,3); |
| 81 | bp << item->data(Qt::DisplayRole).toInt(); |
| 82 | breakpoints.append(bp); |
| 83 | } else { |
| 84 | item = table->item(row,2); |
| 85 | QStringList parts = item->data(Qt::DisplayRole).toString().split(":"); |
| 86 | Q_ASSERT(parts.size() == 2); |
| 87 | bp << parts[0].toInt(); |
| 88 | bp << parts[1].toInt(); |
| 89 | item = table->item(row,3); |
| 90 | bp << item->data(Qt::DisplayRole).toInt(); |
| 91 | breakpoints.append(bp); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | return breakpoints; |
| 96 | } |
| 97 | |
| 98 | void DebugPanel::setDebugFilename(QString filename) |
| 99 | { |
| 100 | ui->statusLabel->setText(QString("File: %1").arg(filename)); |
| 101 | } |
| 102 | |
| 103 | void DebugPanel::setVariableList(QVector<QVariantList> varList) |
| 104 | { |
| 105 | QTableWidget *table = ui->stackTableWidget; |
| 106 | table->setRowCount(0); |
| 107 | int row = 0, column = 0; |
| 108 | foreach(QVariantList var, varList) { |
| 109 | table->insertRow(table->rowCount()); |
| 110 | column = 0; |
| 111 | foreach(QVariant part, var) { |
| 112 | QTableWidgetItem *item = new QTableWidgetItem(part.toString()); |
| 113 | table->setItem(row, column, item); |
| 114 | column++; |
| 115 | } |
| 116 | row++; |
| 117 | } |
| 118 | table->setEnabled(true); |
| 119 | } |
| 120 | |
| 121 | void DebugPanel::setInstrumentList(QVector<QVariantList> instrumentList) |
| 122 | { |
| 123 | QTableWidget *table = ui->instrumentTableWidget; |
| 124 | table->setRowCount(0); |
| 125 | int row = 0, column = 0; |
| 126 | foreach(QVariantList var, instrumentList) { |
| 127 | if (var[0].toDouble() == 0) { |
| 128 | continue; |
| 129 | } |
| 130 | table->insertRow(table->rowCount()); |
| 131 | column = 0; |
| 132 | foreach(QVariant part, var) { |
| 133 | QTableWidgetItem *item = new QTableWidgetItem(part.toString()); |
| 134 | table->setItem(row, column, item); |
| 135 | column++; |
| 136 | } |
| 137 | row++; |
| 138 | } |
| 139 | table->setEnabled(true); |
| 140 | } |
| 141 | |
| 142 | void DebugPanel::stopDebug() |
| 143 | { |
| 144 | ui->statusLabel->setText(QString("Debugger stopped")); |
| 145 | ui->runButton->setChecked(false); |
| 146 | ui->stackTableWidget->setRowCount(0); |
| 147 | ui->instrumentTableWidget->setRowCount(0); |
| 148 | emit stopSignal(); |
| 149 | } |
| 150 | |
| 151 | void DebugPanel::runToggle(bool run_) |
| 152 | { |
| 153 | if (run_) { |
| 154 | run(); |
| 155 | } else { |
| 156 | stopDebug(); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void DebugPanel::run() |
| 161 | { |
| 162 | ui->stackTableWidget->setEnabled(false); |
| 163 | ui->instrumentTableWidget->setEnabled(false); |
| 164 | emit runSignal(); |
| 165 | } |
| 166 | |
| 167 | void DebugPanel::pause() |
| 168 | { |
| 169 | emit pauseSignal(); |
| 170 | } |
| 171 | |
| 172 | void DebugPanel::continueDebug() |
| 173 | { |
| 174 | ui->stackTableWidget->setEnabled(false); |
| 175 | ui->instrumentTableWidget->setEnabled(false); |
| 176 | emit continueSignal(); |
| 177 | } |
| 178 | |
| 179 | void DebugPanel::next() |
| 180 | { |
| 181 | emit nextSignal(); |
| 182 | } |
| 183 | |
| 184 | void DebugPanel::newBreakpoint() |
| 185 | { |
| 186 | NewBreakpointDialog newbp(this); |
| 187 | newbp.setModal(true); |
| 188 | newbp.exec(); |
| 189 | |
| 190 | if (newbp.result() == QDialog::Accepted) { |
| 191 | ui->breakpointTableWidget->blockSignals(true); // To avoid triggering the changed slots |
| 192 | int row = ui->breakpointTableWidget->rowCount(); |
| 193 | ui->breakpointTableWidget->insertRow(row); |
| 194 | QCheckBox *checkbox = new QCheckBox; |
| 195 | checkbox->setChecked(true); |
| 196 | checkbox->setEnabled(false); |
| 197 | ui->breakpointTableWidget->setCellWidget(row, 0, checkbox); |
| 198 | QTableWidgetItem *item = new QTableWidgetItem; |
| 199 | item->setData(Qt::DisplayRole, newbp.type); |
| 200 | item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable); // not editable |
| 201 | ui->breakpointTableWidget->setItem(row, 1, item); |
| 202 | item = new QTableWidgetItem; |
| 203 | item->setData(Qt::DisplayRole, newbp.instrline); |
| 204 | ui->breakpointTableWidget->setItem(row, 2, item); |
| 205 | item = new QTableWidgetItem; |
| 206 | item->setData(Qt::DisplayRole, newbp.skip); |
| 207 | ui->breakpointTableWidget->setItem(row, 3, item); |
| 208 | ui->breakpointTableWidget->blockSignals(false); |
| 209 | if (newbp.type == "instr") { |
| 210 | emit addInstrumentBreakpoint(newbp.instr, newbp.skip); |
| 211 | } else { |
| 212 | emit addBreakpoint(newbp.line, newbp.instr, newbp.skip); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | void DebugPanel::deleteBreakpoint() |
| 218 | { |
| 219 | int row = ui->breakpointTableWidget->currentRow(); |
| 220 | if (row < 0) { |
| 221 | return; |
| 222 | } |
| 223 | if (ui->breakpointTableWidget->item(row, 1)->data(Qt::DisplayRole).toString() == "instr") { |
| 224 | double d = ui->breakpointTableWidget->item(row, 2)->data(Qt::DisplayRole).toDouble(); |
| 225 | emit removeInstrumentBreakpoint(d); |
| 226 | } |
| 227 | if (ui->breakpointTableWidget->item(row, 1)->data(Qt::DisplayRole).toString() == "line") { |
| 228 | QString line = ui->breakpointTableWidget->item(row, 2)->data(Qt::DisplayRole).toString(); |
| 229 | int linenum = 0, instr = 0; |
| 230 | if (line.contains(":")) { |
| 231 | QStringList parts = line.split(":"); |
| 232 | Q_ASSERT(parts.size()==2); |
| 233 | instr = parts[0].toInt(); |
| 234 | linenum = parts[1].toInt(); |
| 235 | Q_ASSERT(linenum >= 0); |
| 236 | Q_ASSERT(instr >= 0); |
| 237 | } |
| 238 | emit removeBreakpoint(linenum, instr); |
| 239 | } |
| 240 | ui->breakpointTableWidget->removeRow(row); |
| 241 | } |
| 242 | |
| 243 | void DebugPanel::cellChanged(int row, int column) |
| 244 | { |
| 245 | switch(column) { |
| 246 | case 0: |
| 247 | // TODO enable/disable toggle |
| 248 | break; |
| 249 | case 2: |
| 250 | case 3: |
| 251 | if (ui->breakpointTableWidget->item(row, 1)->data(Qt::DisplayRole).toString() == "instr") { |
| 252 | QTableWidgetItem *item = ui->breakpointTableWidget->item(row, 2); |
| 253 | double d = item->data(Qt::DisplayRole).toDouble(); |
| 254 | int skip = item->data(Qt::DisplayRole).toInt(); |
| 255 | emit removeInstrumentBreakpoint(item->data(Qt::UserRole).toDouble()); |
| 256 | emit addInstrumentBreakpoint(d, skip); |
| 257 | } else if (ui->breakpointTableWidget->item(row, 1)->data(Qt::DisplayRole).toString() == "line") { |
| 258 | // Remove existing breakpoint |
| 259 | QTableWidgetItem *item = ui->breakpointTableWidget->item(row, 2); |
| 260 | QString oldInstrline = item->data(Qt::UserRole).toString(); |
| 261 | int line = 0, instr = 0; |
| 262 | if (oldInstrline.contains(":")) { |
| 263 | QStringList parts = oldInstrline.split(":"); |
| 264 | Q_ASSERT(parts.size() == 2); |
| 265 | line = parts[1].toInt(); |
| 266 | instr = parts[0].toInt(); |
| 267 | } else { |
| 268 | line = oldInstrline.toInt(); |
| 269 | } |
| 270 | emit removeBreakpoint(line, instr); |
| 271 | // Register new breakpoint |
| 272 | QString instrline = item->data(Qt::DisplayRole).toString(); |
| 273 | line = 0; instr = 0; |
| 274 | if (instrline.contains(":")) { |
| 275 | QStringList parts = instrline.split(":"); |
| 276 | Q_ASSERT(parts.size() == 2); |
| 277 | line = parts[1].toInt(); |
| 278 | instr = parts[0].toInt(); |
| 279 | } else { |
| 280 | line = instrline.toInt(); |
| 281 | } |
| 282 | int skip = item->data(Qt::DisplayRole).toInt(); |
| 283 | emit addBreakpoint(line, instr, skip); |
| 284 | } else { |
| 285 | qDebug() << "Other breakpoint types not supported."; |
| 286 | } |
| 287 | break; |
| 288 | |
| 289 | default: |
| 290 | case 1: |
| 291 | // Do nothing |
| 292 | break; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | void DebugPanel::currentCellChanged(int currentRow, int currentColumn, int previousRow, int previousColumn) |
| 297 | { |
| 298 | Q_UNUSED(previousRow); |
| 299 | Q_UNUSED(previousColumn); |
| 300 | QTableWidgetItem *item; |
| 301 | switch(currentColumn) { |
| 302 | case 2: |
| 303 | case 3: |
| 304 | item = ui->breakpointTableWidget->item(currentRow, currentColumn); |
| 305 | item->setData(Qt::UserRole, item->text()); // Store old value in UserRole of cell |
| 306 | break; |
| 307 | default: |
| 308 | case 0: |
| 309 | case 1: |
| 310 | // Do nothing here |
| 311 | break; |
| 312 | } |
| 313 | } |
| 314 |