| 1 | /* |
| 2 | Copyright (C) 2009 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 "inspector.h" |
| 24 | #include "types.h" |
| 25 | #include <chrono> |
| 26 | #include <QtGui> |
| 27 | |
| 28 | Inspector::Inspector(QWidget *parent) |
| 29 | : QDockWidget(parent) |
| 30 | { |
| 31 | setWindowTitle(tr("Inspector")); |
| 32 | m_treeWidget = new QTreeWidget(this); |
| 33 | // m_treeWidget->setHeaderLabel(tr("Inspector")); |
| 34 | m_treeWidget->setHeaderHidden(true); |
| 35 | m_treeWidget->show(); |
| 36 | setWidget(m_treeWidget); |
| 37 | // connect(m_treeWidget, SIGNAL(itemClicked(QTreeWidgetItem*,int)), |
| 38 | // this, SLOT(itemActivated(QTreeWidgetItem*,int))); |
| 39 | connect(m_treeWidget, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), |
| 40 | this, SLOT(itemChanged(QTreeWidgetItem*, QTreeWidgetItem*))); |
| 41 | // connect(m_treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)), |
| 42 | // this, SLOT(itemActivated(QTreeWidgetItem*,int))); |
| 43 | treeItem1 = new TreeItem(m_treeWidget, QStringList(tr("Opcodes"))); |
| 44 | treeItem2 = new TreeItem(m_treeWidget, QStringList(tr("Macros"))); |
| 45 | treeItem3 = new TreeItem(m_treeWidget, QStringList(tr("Instruments"))); |
| 46 | treeItem4 = new TreeItem(m_treeWidget, QStringList(tr("F-tables"))); |
| 47 | treeItem5 = new TreeItem(m_treeWidget, QStringList(tr("Score"))); |
| 48 | |
| 49 | m_treeWidget->expandItem(treeItem1); // opcodes |
| 50 | m_treeWidget->collapseItem(treeItem2); // macros |
| 51 | m_treeWidget->expandItem(treeItem3); // instruments |
| 52 | m_treeWidget->collapseItem(treeItem4); // ftables |
| 53 | m_treeWidget->collapseItem(treeItem5); // score |
| 54 | rxOpcode.setPattern("\\bopcode\\s+(\\w+),\\s*\\w+\\s*,\\s*\\w+"); |
| 55 | xinRx.setPattern("\\bxin\\b"); |
| 56 | // xinRx.setPattern("[akiS]\\w+[,\\w\\d\\s_]+\\bxin\\s*$"); |
| 57 | xoutRx.setPattern("\\bxout\\s+"); |
| 58 | // ftableRx1.setPattern("^f\\s*\\d"); |
| 59 | ftableRx2.setPattern("^\\w*\\s*ftgen"); |
| 60 | inspectLabels = false; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | Inspector::~Inspector() |
| 65 | { |
| 66 | delete m_treeWidget; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | void Inspector::parseText(const QString &text) |
| 71 | { |
| 72 | udosMap.clear(); |
| 73 | inspectorMutex.lock(); |
| 74 | |
| 75 | auto t1 = std::chrono::high_resolution_clock::now(); |
| 76 | |
| 77 | bool treeItem1Expanded = true; |
| 78 | bool treeItem2Expanded = true; |
| 79 | bool treeItem3Expanded = true; |
| 80 | bool treeItem4Expanded = true; |
| 81 | bool treeItem5Expanded = true; |
| 82 | bool insideOrc = false; |
| 83 | if (treeItem1 != 0) { |
| 84 | treeItem1Expanded = treeItem1->isExpanded(); |
| 85 | treeItem2Expanded = treeItem2->isExpanded(); |
| 86 | treeItem3Expanded = treeItem3->isExpanded(); |
| 87 | treeItem4Expanded = (treeItem4 != 0 ? treeItem4->isExpanded() : false); |
| 88 | treeItem5Expanded = (treeItem5 != 0 ? treeItem5->isExpanded() : false); |
| 89 | } |
| 90 | QHash<QString, bool> instrumentExpanded; // Remember if instrument is expanded in menu |
| 91 | for (int i = 0; i < treeItem3->childCount(); i++) { |
| 92 | QTreeWidgetItem * instr = treeItem3->child(i); |
| 93 | Q_ASSERT(instr->columnCount() > 0); |
| 94 | instrumentExpanded[instr->text(0)] = instr->isExpanded(); |
| 95 | } |
| 96 | |
| 97 | m_treeWidget->clear(); |
| 98 | treeItem1 = new TreeItem(m_treeWidget, QStringList(tr("Opcodes"))); |
| 99 | treeItem1->setLine(-1); |
| 100 | treeItem2 = new TreeItem(m_treeWidget, QStringList(tr("Preprocessor"))); |
| 101 | treeItem2->setLine(-1); |
| 102 | treeItem3 = new TreeItem(m_treeWidget, QStringList(tr("Instruments"))); |
| 103 | treeItem3->setLine(-1); |
| 104 | treeItem4 = new TreeItem(m_treeWidget, QStringList(tr("F-tables"))); |
| 105 | treeItem4->setLine(-1); |
| 106 | treeItem5 = new TreeItem(m_treeWidget, QStringList(tr("Score"))); |
| 107 | treeItem5->setLine(-1); // This might be overridden below |
| 108 | TreeItem *currentInstrument = nullptr; |
| 109 | TreeItem *currentItem = treeItem3; |
| 110 | Opcode *currentOpcode = nullptr; |
| 111 | int commentIndex = 0; |
| 112 | bool partOfComment = false; |
| 113 | int i = 0; |
| 114 | auto lines = text.splitRef('\n'); |
| 115 | QRegularExpressionMatch match; |
| 116 | static QRegularExpression orcStartRx("^\\s*<CsInstruments>"); |
| 117 | |
| 118 | for(; i<lines.size(); i++) { |
| 119 | if(orcStartRx.match(lines[i]).hasMatch()) { |
| 120 | i++; |
| 121 | treeItem3->setLine(i); |
| 122 | insideOrc = true; |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // Parsing orchestra |
| 128 | if(insideOrc) { |
| 129 | for (; i< lines.size(); i++) { |
| 130 | auto line = lines[i].trimmed(); |
| 131 | if (line.isEmpty() || line[0] == ';' || (line[0] == '/' && line[1] == '/')) |
| 132 | continue; |
| 133 | |
| 134 | if (partOfComment) { |
| 135 | if (line.indexOf("*/") != -1) |
| 136 | partOfComment = false; |
| 137 | continue; |
| 138 | } |
| 139 | if (!partOfComment && (commentIndex=line.indexOf("/*")) != -1) { |
| 140 | partOfComment = line.indexOf("*/", commentIndex) == -1; |
| 141 | continue; |
| 142 | } |
| 143 | if (line[0] == '<') { |
| 144 | if (!line.startsWith("</CsInstruments>")) |
| 145 | QDEBUG << "Malformed orchestra, tag" << line << "is invalid"; |
| 146 | break; |
| 147 | } |
| 148 | if(currentOpcode == nullptr && currentInstrument == nullptr) { |
| 149 | // we are at instr 0 |
| 150 | if (line.startsWith("instr ")) { |
| 151 | auto instrline = line.mid(6).trimmed().toString(); |
| 152 | auto newItem = new TreeItem(treeItem3, QStringList(instrline)); |
| 153 | newItem->setLine(i + 1); |
| 154 | currentInstrument = newItem; |
| 155 | currentItem = newItem; |
| 156 | } |
| 157 | else if(line.startsWith("opcode ") && (match=rxOpcode.match(line)).hasMatch()) { |
| 158 | auto opcodeName = match.captured(1); |
| 159 | auto itemtext = line.mid(7).toString(); |
| 160 | QStringList columnslist(itemtext); |
| 161 | if (treeItem1->childCount() == 0) { // set line for element to the first one found |
| 162 | treeItem1->setLine(i + 1); |
| 163 | } |
| 164 | TreeItem *newItem = new TreeItem(treeItem1, columnslist); |
| 165 | newItem->setLine(i + 1); |
| 166 | currentItem = newItem; |
| 167 | udosMap.insert(opcodeName, Opcode(opcodeName)); |
| 168 | currentOpcode = &udosMap[opcodeName]; |
| 169 | } |
| 170 | else if (line[0] == '#') { |
| 171 | if (line.startsWith("#define")) { |
| 172 | QString item = line.mid(8).toString(); |
| 173 | if (treeItem2->childCount() == 0) { // set line for element to the first one found |
| 174 | treeItem2->setLine(i + 1); |
| 175 | } |
| 176 | TreeItem *newItem = new TreeItem(treeItem2, QStringList(item)); |
| 177 | newItem->setLine(i + 1); |
| 178 | } else if (line.startsWith("#include")) { |
| 179 | // TODO: parse include file |
| 180 | if (treeItem2->childCount() == 0) { // set line for element to the first one found |
| 181 | treeItem2->setLine(i + 1); |
| 182 | } |
| 183 | TreeItem *newItem = new TreeItem(treeItem2, QStringList(line.toString())); |
| 184 | newItem->setLine(i + 1); |
| 185 | |
| 186 | } |
| 187 | } |
| 188 | else if(ftableRx2.match(line).hasMatch()) { |
| 189 | QStringList columnslist(line.toString()); |
| 190 | if (treeItem4->childCount() == 0) { // set line for element to the first one found |
| 191 | treeItem4->setLine(i + 1); |
| 192 | } |
| 193 | TreeItem *newItem = new TreeItem(treeItem4, columnslist); |
| 194 | newItem->setLine(i + 1); |
| 195 | } |
| 196 | } |
| 197 | else if(currentOpcode != nullptr) { |
| 198 | if (line == "endop") { |
| 199 | if(currentOpcode != nullptr) { |
| 200 | currentOpcode = nullptr; |
| 201 | currentItem = treeItem3; |
| 202 | } |
| 203 | else { |
| 204 | qDebug() << "endop found outside opcode definition, line" << (i+1); |
| 205 | } |
| 206 | } |
| 207 | else if(currentOpcode->inArgs.isEmpty() && (match=xinRx.match(line)).hasMatch()) { |
| 208 | currentOpcode->inArgs = line.mid(0, match.capturedStart()).toString().simplified(); |
| 209 | QStringList columnslist(currentOpcode->inArgs + " xin"); |
| 210 | TreeItem *newItem = new TreeItem(currentItem, columnslist); |
| 211 | newItem->setLine(i + 1); |
| 212 | } |
| 213 | else if(currentOpcode->outArgs.isEmpty() && (match=xoutRx.match(line)).hasMatch()) { |
| 214 | currentOpcode->outArgs = line.mid(match.capturedEnd()).toString().simplified(); |
| 215 | QStringList columnslist("xout " + currentOpcode->outArgs); |
| 216 | TreeItem *newItem = new TreeItem(currentItem, columnslist); |
| 217 | newItem->setLine(i + 1); |
| 218 | } |
| 219 | } |
| 220 | else if (line == "endin") { |
| 221 | // everything between instruments is placed in the main instrument menu |
| 222 | currentInstrument = nullptr; |
| 223 | currentItem = treeItem3; |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | |
| 229 | static QRegularExpression rxCsScore("^\\s*<CsScore>"); |
| 230 | for(; i< lines.size(); i++) { |
| 231 | if (rxCsScore.match(lines[i]).hasMatch()) { |
| 232 | treeItem5->setLine(i + 1); |
| 233 | currentItem = treeItem5; |
| 234 | break; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | treeItem1->setExpanded(treeItem1Expanded); |
| 239 | treeItem2->setExpanded(treeItem2Expanded); |
| 240 | treeItem3->setExpanded(treeItem3Expanded); |
| 241 | treeItem4->setExpanded(treeItem4Expanded); |
| 242 | treeItem5->setExpanded(treeItem5Expanded); |
| 243 | |
| 244 | for (int i = 0; i < treeItem3->childCount(); i++) { |
| 245 | QTreeWidgetItem * instr = treeItem3->child(i); |
| 246 | if (instrumentExpanded.contains(instr->text(0))) { |
| 247 | instr->setExpanded(instrumentExpanded[instr->text(0)]); |
| 248 | } |
| 249 | } |
| 250 | std::chrono::duration<double, std::milli> ms_double = std::chrono::high_resolution_clock::now() - t1; |
| 251 | qDebug() << "Inspector::parseText: "<< ms_double.count() << "ms\n"; |
| 252 | inspectorMutex.unlock(); |
| 253 | |
| 254 | } |
| 255 | |
| 256 | void Inspector::parsePythonText(const QString &text) |
| 257 | { |
| 258 | // qDebug() << "Inspector:parseText"; |
| 259 | inspectorMutex.lock(); |
| 260 | m_treeWidget->clear(); |
| 261 | treeItem1 = 0; |
| 262 | treeItem1 = new TreeItem(m_treeWidget, QStringList(tr("Imports"))); |
| 263 | treeItem1->setLine(-1); |
| 264 | treeItem2 = new TreeItem(m_treeWidget, QStringList(tr("Classes"))); |
| 265 | treeItem2->setLine(-1); |
| 266 | treeItem3 = new TreeItem(m_treeWidget, QStringList(tr("Functions"))); |
| 267 | treeItem3->setLine(-1); |
| 268 | treeItem4 = 0; |
| 269 | treeItem5 = 0; |
| 270 | TreeItem *currentParent = 0; |
| 271 | QStringList lines = text.split(QRegExp("[\\n\\r]")); |
| 272 | for (int i = 0; i< lines.size(); i++) { |
| 273 | if (lines[i].trimmed().startsWith("class ")) { |
| 274 | QStringList columnslist(lines[i].simplified()); |
| 275 | TreeItem *newItem = new TreeItem(treeItem2, columnslist); |
| 276 | currentParent = newItem; |
| 277 | m_treeWidget->expandItem(newItem); |
| 278 | QFont itemFont = newItem->font(0); |
| 279 | itemFont.setBold(true); |
| 280 | newItem->setFont(1, itemFont); |
| 281 | newItem->setLine(i + 1); |
| 282 | } |
| 283 | else if (lines[i].contains(QRegExp("[\\s]+def "))) { |
| 284 | QStringList columnslist(lines[i].simplified()); |
| 285 | if (currentParent != 0) { |
| 286 | TreeItem *newItem = new TreeItem(currentParent, columnslist); |
| 287 | newItem->setLine(i + 1); |
| 288 | } |
| 289 | } |
| 290 | else if (lines[i].trimmed().contains(QRegExp("\\bimport\\b"))) { |
| 291 | QStringList columnslist(lines[i].simplified()); |
| 292 | TreeItem *newItem = new TreeItem(treeItem1, columnslist); |
| 293 | newItem->setLine(i + 1); |
| 294 | } |
| 295 | else if (lines[i].trimmed().startsWith("def ")) { |
| 296 | QStringList columnslist(lines[i].simplified()); |
| 297 | TreeItem *newItem = new TreeItem(treeItem3, columnslist); |
| 298 | newItem->setLine(i + 1); |
| 299 | } |
| 300 | else if (lines[i].contains("##")) { |
| 301 | QStringList columnslist(lines[i].simplified()); |
| 302 | TreeItem *newItem = new TreeItem(treeItem3, columnslist); |
| 303 | // newItem->setForeground (0, QBrush(Qt::darkGreen) ); |
| 304 | newItem->setLine(i + 1); |
| 305 | } |
| 306 | } |
| 307 | m_treeWidget->expandItem(treeItem1); |
| 308 | m_treeWidget->expandItem(treeItem2); |
| 309 | m_treeWidget->expandItem(treeItem3); |
| 310 | inspectorMutex.unlock(); |
| 311 | } |
| 312 | |
| 313 | void Inspector::focusInEvent (QFocusEvent * event) |
| 314 | { |
| 315 | QWidget::focusInEvent(event); |
| 316 | // parseText(); |
| 317 | } |
| 318 | |
| 319 | void Inspector::closeEvent(QCloseEvent * /*event*/) |
| 320 | { |
| 321 | // qDebug() << "Inspector::closeEvent"; |
| 322 | emit Close(false); |
| 323 | } |
| 324 | |
| 325 | void Inspector::itemActivated(QTreeWidgetItem * item, int /*column*/) |
| 326 | { |
| 327 | int line = static_cast<TreeItem *>(item)->getLine(); |
| 328 | if (line >= 0) { |
| 329 | emit jumpToLine(line); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | void Inspector::itemChanged(QTreeWidgetItem * newItem, QTreeWidgetItem * /*oldItem*/) |
| 334 | { |
| 335 | if (newItem == 0) { |
| 336 | return; |
| 337 | } |
| 338 | int line = static_cast<TreeItem *>(newItem)->getLine(); |
| 339 | if (line >= 0) { |
| 340 | emit jumpToLine(line); |
| 341 | } |
| 342 | } |