| 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 "documentview.h" |
| 24 | #include "findreplace.h" |
| 25 | #include "opentryparser.h" |
| 26 | #include "types.h" |
| 27 | |
| 28 | #include "highlighter.h" |
| 29 | #include "texteditor.h" |
| 30 | |
| 31 | |
| 32 | static const QStringList tagWords = {"CsInstruments", "CsScore", "CsoundSynthesizer", "CsOptions"}; |
| 33 | |
| 34 | DocumentView::DocumentView(QWidget * parent, OpEntryParser *opcodeTree) : |
| 35 | BaseView(parent,opcodeTree) |
| 36 | { |
| 37 | m_autoComplete = true; |
| 38 | |
| 39 | m_hoverWidget = new HoverWidget(m_mainEditor); |
| 40 | m_hoverText= new QLabel("Hello", m_hoverWidget); |
| 41 | m_hoverText->show(); |
| 42 | m_hoverText->setStyleSheet( |
| 43 | "QLabel {" |
| 44 | " border: 1px solid #505050;" |
| 45 | " border-radius: 4px;" |
| 46 | " padding: 2px;" |
| 47 | " background-color: #eaeac5;" |
| 48 | " color: #202020; " |
| 49 | "};"); |
| 50 | m_hoverText->setWordWrap(true); |
| 51 | m_hoverText->setMinimumWidth(300); |
| 52 | m_hoverWidget->hide(); |
| 53 | |
| 54 | for (int i = 0; i < editors.size(); i++) { |
| 55 | if (editors[i]!=m_filebEditor && editors[i] != m_mainEditor) { // FilebEditor does not have this slot |
| 56 | // to fix always marked as modified, comment it out and handle setModified in this->textChanged slot |
| 57 | // connect(editors[i], SIGNAL(textChanged()), this, SLOT(setModified())); |
| 58 | } |
| 59 | splitter->addWidget(editors[i]); |
| 60 | editors[i]->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
| 61 | editors[i]->setContextMenuPolicy(Qt::CustomContextMenu); |
| 62 | connect(editors[i], SIGNAL(customContextMenuRequested(QPoint)), |
| 63 | this, SLOT(createContextMenu(QPoint))); |
| 64 | } |
| 65 | connect(m_orcEditor, SIGNAL(textChanged()), this, SLOT(textChanged())); |
| 66 | connect(m_orcEditor, SIGNAL(cursorPositionChanged()), |
| 67 | this, SLOT(syntaxCheck())); |
| 68 | setFocusProxy(m_mainEditor); // for comment action from main application |
| 69 | internalChange = false; |
| 70 | |
| 71 | // m_highlighter = new Highlighter(); |
| 72 | connect(m_mainEditor, SIGNAL(textChanged()), |
| 73 | this, SLOT(textChanged())); |
| 74 | // connect(m_mainEditor, SIGNAL(cursorPositionChanged()), |
| 75 | // this, SLOT(updateContext())); |
| 76 | connect(m_mainEditor, SIGNAL(cursorPositionChanged()), |
| 77 | this, SLOT(syntaxCheck())); |
| 78 | connect(m_mainEditor, SIGNAL(escapePressed()), |
| 79 | this, SLOT(escapePressed())); |
| 80 | connect(m_mainEditor, SIGNAL(newLine()), |
| 81 | this, SLOT(indentNewLine())); |
| 82 | connect(m_mainEditor, SIGNAL(requestIndent()), |
| 83 | this, SLOT(indent())); |
| 84 | connect(m_mainEditor, SIGNAL(requestUnindent()), |
| 85 | this, SLOT(unindent())); |
| 86 | connect(m_mainEditor, SIGNAL(tabPressed()), |
| 87 | this, SLOT(nextParameter())); |
| 88 | connect(m_mainEditor, SIGNAL(backtabPressed()), |
| 89 | this, SLOT(prevParameter())); |
| 90 | connect(m_mainEditor, SIGNAL(arrowPressed()), |
| 91 | this, SLOT(exitParameterMode())); |
| 92 | connect(m_mainEditor, SIGNAL(enterPressed()), |
| 93 | this, SLOT(finishParameterMode())); |
| 94 | connect(m_mainEditor, SIGNAL(showParameterInfo()), |
| 95 | this, SLOT(showHoverText())); |
| 96 | connect(m_mainEditor, SIGNAL(requestParameterModeExit()), |
| 97 | this, SLOT(exitParameterMode())); |
| 98 | connect(m_mainEditor,SIGNAL(arrowPressed()), this, SLOT(restoreCursorPosition()) ); |
| 99 | |
| 100 | //TODO put this for line reporting for score editor |
| 101 | // connect(scoreEditor, SIGNAL(textChanged()), |
| 102 | // this, SLOT(syntaxCheck()) // connect(scoreEditor, SIGNAL(cursorPositionChanged()), |
| 103 | // this, SLOT(syntaxCheck())); |
| 104 | |
| 105 | errorMarked = false; |
| 106 | m_isModified = false; |
| 107 | |
| 108 | syntaxMenu = new MySyntaxMenu(m_mainEditor); |
| 109 | // syntaxMenu->setFocusPolicy(Qt::NoFocus); |
| 110 | syntaxMenu->setAutoFillBackground(true); |
| 111 | connect(syntaxMenu,SIGNAL(keyPressed(QString)), |
| 112 | m_mainEditor, SLOT(insertPlainText(QString))); |
| 113 | |
| 114 | setViewMode(1); |
| 115 | setViewMode(0); // To force a change |
| 116 | setAcceptDrops(true); |
| 117 | |
| 118 | m_oldCursorPosition = -1; // 0 or positive, if cursor needs to be moved there |
| 119 | markCurrentPosition(); |
| 120 | |
| 121 | } |
| 122 | |
| 123 | DocumentView::~DocumentView() |
| 124 | { |
| 125 | disconnect(this, 0,0,0); |
| 126 | } |
| 127 | |
| 128 | bool DocumentView::isModified() |
| 129 | { |
| 130 | return m_isModified; |
| 131 | } |
| 132 | |
| 133 | void DocumentView::markCurrentPosition() { |
| 134 | int pos = this->currentLineNumber(); |
| 135 | cursorPositions.push_back(pos); |
| 136 | if(cursorPositions.size() > 1000) { |
| 137 | cursorPositions.pop_front(); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | bool DocumentView::childHasFocus() |
| 142 | { |
| 143 | if (this->hasFocus() || m_scoreEditor->hasFocus() |
| 144 | || m_optionsEditor->hasFocus() |
| 145 | || m_otherEditor->hasFocus() |
| 146 | || m_otherCsdEditor->hasFocus() |
| 147 | || m_widgetEditor->hasFocus()) { |
| 148 | return true; |
| 149 | } |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | void DocumentView::print(QPrinter *printer) |
| 154 | { |
| 155 | m_mainEditor->print(printer); |
| 156 | } |
| 157 | |
| 158 | void DocumentView::updateContext() |
| 159 | { |
| 160 | // this code is too expensive to be run at EACH keystroke. We need to think of something better |
| 161 | // Until then I am disabling it (EM, Nov. 21) |
| 162 | return; |
| 163 | QStringList contextStart; |
| 164 | contextStart << "<CsInstruments>" << "<CsScore" << "<CsOptions>"; |
| 165 | QTextCursor cursor = m_mainEditor->textCursor(); |
| 166 | cursor.select(QTextCursor::LineUnderCursor); |
| 167 | QString line = cursor.selection().toPlainText().simplified(); |
| 168 | while (!cursor.atStart()) { |
| 169 | foreach(QString startText, contextStart) { |
| 170 | if (line.startsWith(startText)) { |
| 171 | m_currentContext = contextStart.indexOf(startText) + 1; |
| 172 | break; |
| 173 | } |
| 174 | } |
| 175 | cursor.movePosition(QTextCursor::Up); |
| 176 | cursor.select(QTextCursor::BlockUnderCursor); |
| 177 | line = cursor.selection().toPlainText(); |
| 178 | cursor.movePosition(QTextCursor::StartOfBlock); |
| 179 | } |
| 180 | if (m_currentContext == DocumentView::ORC_CONTEXT) { // Instrument section |
| 181 | QString endText = "</CsInstruments>"; |
| 182 | QTextCursor linecursor = cursor; |
| 183 | linecursor.select(QTextCursor::LineUnderCursor); |
| 184 | line = linecursor.selection().toPlainText(); |
| 185 | linecursor.movePosition(QTextCursor::EndOfLine); |
| 186 | while (!linecursor.atEnd() && !line.contains(endText)) { |
| 187 | cursor.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor); |
| 188 | cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor); |
| 189 | linecursor = cursor; |
| 190 | linecursor.select(QTextCursor::LineUnderCursor); |
| 191 | line = linecursor.selection().toPlainText(); |
| 192 | linecursor.movePosition(QTextCursor::EndOfLine); |
| 193 | } |
| 194 | QString orc = cursor.selection().toPlainText(); |
| 195 | updateOrcContext(orc); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | void DocumentView::updateOrcContext(QString orc) |
| 200 | { |
| 201 | // This is disabled at the moment since updateContext is itself disabled |
| 202 | (void) orc; |
| 203 | // TODO: add string as context |
| 204 | QStringList innerContextStart; |
| 205 | innerContextStart << "instr" << "opcode"; |
| 206 | QTextCursor cursor = m_mainEditor->textCursor(); |
| 207 | cursor.select(QTextCursor::LineUnderCursor); |
| 208 | QString line = cursor.selection().toPlainText().trimmed(); |
| 209 | while (!cursor.atStart()) { |
| 210 | foreach(QString startText, innerContextStart) { |
| 211 | if (line.startsWith(startText)) { |
| 212 | break; |
| 213 | } |
| 214 | } |
| 215 | cursor.movePosition(QTextCursor::Up); |
| 216 | cursor.select(QTextCursor::BlockUnderCursor); |
| 217 | line = cursor.selection().toPlainText().trimmed(); |
| 218 | cursor.movePosition(QTextCursor::StartOfBlock); |
| 219 | } |
| 220 | cursor.movePosition(QTextCursor::NextBlock); |
| 221 | QString endText = "endin"; |
| 222 | QTextCursor linecursor = cursor; |
| 223 | linecursor.select(QTextCursor::LineUnderCursor); |
| 224 | line = linecursor.selection().toPlainText().trimmed(); |
| 225 | linecursor.movePosition(QTextCursor::EndOfLine); |
| 226 | while (!linecursor.atEnd() && !line.startsWith(endText)) { |
| 227 | cursor.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor); |
| 228 | cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor); |
| 229 | linecursor = cursor; |
| 230 | linecursor.select(QTextCursor::LineUnderCursor); |
| 231 | line = linecursor.selection().toPlainText().trimmed(); |
| 232 | linecursor.movePosition(QTextCursor::EndOfLine); |
| 233 | } |
| 234 | QString instr = cursor.selection().toPlainText(); |
| 235 | auto lines = instr.splitRef("\n", SKIP_EMPTY_PARTS); |
| 236 | |
| 237 | auto rxWordSplit = QRegularExpression("[\\s,+-/\\*\\.\\^\\(\\)\\[\\]$]"); |
| 238 | m_localVariables.clear(); |
| 239 | QSet<QString> seen; |
| 240 | for(auto line: lines) { |
| 241 | line = line.trimmed(); |
| 242 | if (line.isEmpty() || line.startsWith(";")) { |
| 243 | continue; |
| 244 | } |
| 245 | auto words = line.toString().split(rxWordSplit, SKIP_EMPTY_PARTS); |
| 246 | for(auto word: words) { |
| 247 | if(!seen.contains(word) && !m_opcodeTree->isOpcode(word)) { |
| 248 | seen.insert(word); |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | //m_localVariables = QStringList(seen.begin(), seen.end()); |
| 253 | m_localVariables = seen.values(); |
| 254 | } |
| 255 | |
| 256 | void DocumentView::nextParameter() |
| 257 | { |
| 258 | QChar character = m_mainEditor->document()->characterAt(m_mainEditor->textCursor().position()); |
| 259 | // First move forward a word |
| 260 | while(!m_mainEditor->textCursor().atBlockEnd() && |
| 261 | !(character.isSpace() || character == ',' || character == '(' || character == ')' |
| 262 | || character == '[' || character == ']' || character == '\\')) { |
| 263 | m_mainEditor->moveCursor(QTextCursor::NextCharacter); |
| 264 | character = m_mainEditor->document()->characterAt(m_mainEditor->textCursor().position()); |
| 265 | } |
| 266 | // Then move forward all white space |
| 267 | while(!m_mainEditor->textCursor().atBlockEnd() && |
| 268 | (character.isSpace() || character == ',' || character == '(' || character == ')' |
| 269 | || character == '[' || character == ']' || character == '\\')) { |
| 270 | m_mainEditor->moveCursor(QTextCursor::NextCharacter); |
| 271 | character = m_mainEditor->document()->characterAt(m_mainEditor->textCursor().position()); |
| 272 | } |
| 273 | if (m_mainEditor->textCursor().atBlockStart()) { |
| 274 | m_mainEditor->moveCursor(QTextCursor::PreviousCharacter); |
| 275 | } |
| 276 | QTextCursor cursor = m_mainEditor->textCursor(); |
| 277 | cursor.select(QTextCursor::WordUnderCursor); |
| 278 | if (cursor.selectedText() == "\"") { |
| 279 | cursor.movePosition(QTextCursor::NextCharacter); |
| 280 | while (!(cursor.selectedText().endsWith("\"") || cursor.atBlockEnd())) { |
| 281 | cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor); |
| 282 | } |
| 283 | if (cursor.selectedText().endsWith("\"")) { |
| 284 | cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor); |
| 285 | } |
| 286 | } |
| 287 | m_mainEditor->blockSignals(true); // To avoid triggering the position changed action which hides the text cursor and disables parameter mode |
| 288 | m_mainEditor->setTextCursor(cursor); |
| 289 | m_mainEditor->blockSignals(false); |
| 290 | showHoverText(); |
| 291 | } |
| 292 | |
| 293 | void DocumentView::prevParameter() |
| 294 | { |
| 295 | if (!m_mainEditor->textCursor().atBlockStart()) { |
| 296 | m_mainEditor->moveCursor(QTextCursor::PreviousCharacter); |
| 297 | } |
| 298 | QChar character = m_mainEditor->document()->characterAt(m_mainEditor->textCursor().position()); |
| 299 | while(!m_mainEditor->textCursor().atBlockStart() && |
| 300 | !(character.isSpace() || character == ',' || character == '(' || character == ')' |
| 301 | || character == '[' || character == ']' || character == '\\')) { |
| 302 | m_mainEditor->moveCursor(QTextCursor::PreviousCharacter); |
| 303 | character = m_mainEditor->document()->characterAt(m_mainEditor->textCursor().position()); |
| 304 | } |
| 305 | while(!m_mainEditor->textCursor().atBlockStart() && |
| 306 | (character.isSpace() || character == ',' || character == '(' || character == ')' |
| 307 | || character == '[' || character == ']' || character == '\\')) { |
| 308 | m_mainEditor->moveCursor(QTextCursor::PreviousCharacter); |
| 309 | character = m_mainEditor->document()->characterAt(m_mainEditor->textCursor().position()); |
| 310 | } |
| 311 | QTextCursor cursor = m_mainEditor->textCursor(); |
| 312 | cursor.select(QTextCursor::WordUnderCursor); |
| 313 | if (cursor.selectedText().startsWith("\"")) { |
| 314 | cursor.movePosition(QTextCursor::PreviousCharacter); |
| 315 | while (!(cursor.selectedText().startsWith("\"") || cursor.atBlockEnd())) { |
| 316 | cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor); |
| 317 | } |
| 318 | if (cursor.selectedText().startsWith("\"")) { |
| 319 | cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor); |
| 320 | } |
| 321 | } |
| 322 | m_mainEditor->blockSignals(true); // To avoid triggering the position changed action which hides the text cursor and disables parameter mode |
| 323 | m_mainEditor->setTextCursor(cursor); |
| 324 | m_mainEditor->blockSignals(false); |
| 325 | showHoverText(); |
| 326 | } |
| 327 | |
| 328 | void DocumentView::showHoverText() |
| 329 | { |
| 330 | QTextCursor cursor = m_mainEditor->textCursor(); |
| 331 | QRect cursorRect = m_mainEditor->cursorRect(); |
| 332 | updateHoverText(cursorRect.x(), cursorRect.y(), cursor.selectedText()); |
| 333 | } |
| 334 | |
| 335 | void DocumentView::hideHoverText() |
| 336 | { |
| 337 | m_hoverWidget->hide(); |
| 338 | } |
| 339 | |
| 340 | void DocumentView::updateHoverText(int x, int y, QString text) |
| 341 | { |
| 342 | QString displayText = m_currentOpcodeText; |
| 343 | if (!text.isEmpty()) { |
| 344 | displayText.replace(text, "<b>" + text + "</b>"); |
| 345 | } |
| 346 | m_hoverText->setText(displayText); |
| 347 | m_hoverText->adjustSize(); |
| 348 | QRect textRect = m_hoverText->contentsRect(); |
| 349 | int textWidth = textRect.width() + 10; |
| 350 | if (textWidth > this->width()) { |
| 351 | textRect.setWidth(this->width()); |
| 352 | textWidth = this->width(); |
| 353 | } |
| 354 | int xoffset = 0; |
| 355 | if (m_currentOpcodeText.contains(text)) { |
| 356 | xoffset = 10 + textRect.width() * (m_currentOpcodeText.indexOf(text)/(float) (m_currentOpcodeText.indexOf("<br />") + 5)); |
| 357 | } |
| 358 | if (xoffset > x) { |
| 359 | xoffset = x; |
| 360 | } else if (textWidth + x - xoffset > this->width()) { |
| 361 | xoffset = - (this->width() - (textWidth + x - xoffset)); |
| 362 | } |
| 363 | m_hoverWidget->setGeometry(x - xoffset, y-textRect.height() - 10, textWidth, textRect.height() + 10); |
| 364 | |
| 365 | m_hoverWidget->show(); |
| 366 | } |
| 367 | |
| 368 | void DocumentView::setModified(bool mod) |
| 369 | { |
| 370 | // auto sender = static_cast<QTextEdit*>(this->sender()); |
| 371 | // auto senderName = sender != nullptr ? sender->property("name").toString() : ""; |
| 372 | // qDebug() << "sender: " << senderName << "modified:"<<mod; |
| 373 | if (mod) { |
| 374 | emit contentsChanged(); |
| 375 | } |
| 376 | m_isModified = mod; |
| 377 | } |
| 378 | |
| 379 | void DocumentView::insertText(QString text, int section) |
| 380 | { |
| 381 | if (section == -1 || section < 0) { |
| 382 | section = 0; // TODO implment for multiple views |
| 383 | } |
| 384 | QTextCursor cursor; |
| 385 | switch(section) { |
| 386 | case 0: |
| 387 | cursor = m_mainEditor->textCursor(); |
| 388 | cursor.insertText(text); |
| 389 | m_mainEditor->setTextCursor(cursor); |
| 390 | break; |
| 391 | // case 1: |
| 392 | // orcEditor; |
| 393 | // break; |
| 394 | // case 2: |
| 395 | // scoreEditor; |
| 396 | // break; |
| 397 | // case 3: |
| 398 | // optionsEditor; |
| 399 | // break; |
| 400 | // case 4: |
| 401 | // filebEditor; |
| 402 | // break; |
| 403 | // case 5: |
| 404 | // versionEditor; |
| 405 | // break; |
| 406 | // case 6: |
| 407 | // licenceEditor; |
| 408 | // break; |
| 409 | // case 7: |
| 410 | // otherEditor; |
| 411 | // break; |
| 412 | // case 8: |
| 413 | // widgetEditor; |
| 414 | // break; |
| 415 | // case 9: |
| 416 | // ladspaEditor; |
| 417 | // break; |
| 418 | // case 10: |
| 419 | // break; |
| 420 | default: |
| 421 | qDebug() <<"Section " << section << " not implemented."; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | void DocumentView::setAutoComplete(bool autoComplete, int delay) |
| 426 | { |
| 427 | m_autoComplete = autoComplete; |
| 428 | if(delay > 0) |
| 429 | m_autoCompleteDelay = delay; |
| 430 | } |
| 431 | |
| 432 | void DocumentView::setAutoParameterMode(bool autoParameterMode) |
| 433 | { |
| 434 | m_autoParameterMode = autoParameterMode; |
| 435 | } |
| 436 | |
| 437 | void DocumentView::setViewMode(int mode) |
| 438 | { |
| 439 | if (m_viewMode == mode) |
| 440 | return; |
| 441 | QString fullText = getFullText(); |
| 442 | m_viewMode = mode; |
| 443 | setFullText(fullText); // Update text for new mode |
| 444 | hideAllEditors(); |
| 445 | |
| 446 | // TODO implement modes properly |
| 447 | switch (m_viewMode) { |
| 448 | case 0: // csd without extra sections |
| 449 | m_mainEditor->show(); |
| 450 | m_highlighter.setDocument(m_mainEditor->document()); |
| 451 | break; |
| 452 | case 1: // full plain text |
| 453 | m_mainEditor->show(); |
| 454 | m_highlighter.setDocument(m_mainEditor->document()); |
| 455 | |
| 456 | break; |
| 457 | default: |
| 458 | m_highlighter.setDocument(m_orcEditor->document()); |
| 459 | m_orcEditor->setVisible(m_viewMode & 2); |
| 460 | m_scoreEditor->setVisible(m_viewMode & 4); |
| 461 | m_optionsEditor->setVisible(m_viewMode & 8); |
| 462 | m_filebEditor->setVisible(m_viewMode & 16); |
| 463 | m_otherEditor->setVisible(m_viewMode & 32); |
| 464 | m_otherCsdEditor->setVisible(m_viewMode & 64); |
| 465 | m_widgetEditor->setVisible(m_viewMode & 128); |
| 466 | m_appEditor->setVisible(m_viewMode & 256); |
| 467 | auto defaultFormat = m_highlighter.getFormat("default"); |
| 468 | auto sheet = QString("QTextEdit { color: %1; background-color: %2}") |
| 469 | .arg(defaultFormat.foreground().color().name()) |
| 470 | .arg(defaultFormat.background().color().name()); |
| 471 | if(m_viewMode & 2) { |
| 472 | m_orcEditor->setStyleSheet(sheet); |
| 473 | } |
| 474 | |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | QString DocumentView::getSelectedText(int section) |
| 479 | { |
| 480 | if (section < 0) { |
| 481 | section = 0; // TODO implment for multiple views |
| 482 | } |
| 483 | QString text; |
| 484 | switch(section) { |
| 485 | case 0: |
| 486 | text = m_mainEditor->textCursor().selectedText(); |
| 487 | break; |
| 488 | case 1: |
| 489 | text = m_orcEditor->textCursor().selectedText(); |
| 490 | break; |
| 491 | case 2: |
| 492 | text = m_scoreEditor->getSelection(); |
| 493 | break; |
| 494 | case 3: |
| 495 | text = m_optionsEditor->textCursor().selectedText(); |
| 496 | // ; |
| 497 | break; |
| 498 | case 4: |
| 499 | // filebEditor; |
| 500 | break; |
| 501 | case 5: |
| 502 | text = m_otherEditor->textCursor().selectedText(); |
| 503 | break; |
| 504 | case 6: |
| 505 | text = m_otherCsdEditor->textCursor().selectedText(); |
| 506 | break; |
| 507 | case 7: |
| 508 | text = m_widgetEditor->textCursor().selectedText(); |
| 509 | break; |
| 510 | case 8: |
| 511 | text = m_appEditor->textCursor().selectedText(); |
| 512 | break; |
| 513 | default: |
| 514 | qDebug() << "Section " << section << " not implemented."; |
| 515 | } |
| 516 | return text; |
| 517 | } |
| 518 | |
| 519 | QString DocumentView::getMacWidgetsText() |
| 520 | { |
| 521 | // With tags including presets. For text that is being edited in the text editor |
| 522 | // Includes presets text |
| 523 | qDebug() << "Not implemented and will crash!"; |
| 524 | return "Not implemented."; |
| 525 | } |
| 526 | |
| 527 | QString DocumentView::getWidgetsText() |
| 528 | { |
| 529 | // With tags including presets, in new xml format. For text that is being edited in the text editor |
| 530 | // Includes presets text |
| 531 | qDebug() << "Not implemented and will crash!"; |
| 532 | return "Not implemented."; |
| 533 | } |
| 534 | |
| 535 | int DocumentView::getViewMode() |
| 536 | { |
| 537 | return m_viewMode; |
| 538 | } |
| 539 | |
| 540 | int DocumentView::currentLineNumber() |
| 541 | { |
| 542 | // Returns text without tags |
| 543 | int line = -1; |
| 544 | if (m_viewMode < 2) {// A single editor (orc and sco are not split) |
| 545 | QTextCursor cursor = m_mainEditor->textCursor(); |
| 546 | line = cursor.blockNumber() + 1; |
| 547 | } |
| 548 | else { // Split view |
| 549 | // TODO check properly for line number also from other editors |
| 550 | QWidget *w = this->focusWidget(); // Gives last child of this widget that has had focus. |
| 551 | if (w == m_scoreEditor || w == m_filebEditor) { |
| 552 | qDebug() << "Not implemented for score and fileb editor."; |
| 553 | } |
| 554 | else if (w != 0 && editors.contains(w)) { // Somehow this widget can sometimes be invalid... so must check if it is one of the editors |
| 555 | QTextCursor cursor = static_cast<TextEditor *>(w)->textCursor(); |
| 556 | line = cursor.blockNumber() + 1; |
| 557 | } |
| 558 | } |
| 559 | return line; |
| 560 | } |
| 561 | |
| 562 | QString DocumentView::lineUnderCursor() { |
| 563 | if(m_viewMode >= 2) { |
| 564 | QDEBUG << "Not implemented"; |
| 565 | return ""; |
| 566 | } |
| 567 | QTextCursor cursor = m_mainEditor->textCursor(); |
| 568 | cursor.select(QTextCursor::LineUnderCursor); |
| 569 | auto line = cursor.selectedText(); |
| 570 | return line; |
| 571 | } |
| 572 | |
| 573 | QString DocumentView::wordUnderCursor() |
| 574 | { |
| 575 | QString word; |
| 576 | if (m_viewMode < 2) {// A single editor (orc and sco are not split) |
| 577 | QTextCursor cursor = m_mainEditor->textCursor(); |
| 578 | word = cursor.selectedText(); |
| 579 | if (word.isEmpty()) { |
| 580 | cursor.select(QTextCursor::WordUnderCursor); |
| 581 | word = cursor.selectedText(); |
| 582 | } |
| 583 | } |
| 584 | else { // Split view |
| 585 | // TODO check properly for line number also from other editors |
| 586 | qDebug() << "Not implemented for split view."; |
| 587 | } |
| 588 | return word; |
| 589 | } |
| 590 | |
| 591 | QString DocumentView::getActiveSection() |
| 592 | { |
| 593 | // Will return all document if there are no ## boundaries (for any kind of file) |
| 594 | QString section; |
| 595 | if (m_viewMode < 2) { |
| 596 | if (m_mode == EDIT_PYTHON_MODE) { |
| 597 | QTextCursor cursor = m_mainEditor->textCursor(); |
| 598 | m_oldCursorPosition = cursor.position(); // to move back there on next keypress |
| 599 | cursor.select(QTextCursor::LineUnderCursor); |
| 600 | bool sectionStart = cursor.selectedText().simplified().startsWith("##"); |
| 601 | while (!sectionStart && !cursor.anchor() == 0) { |
| 602 | cursor.movePosition(QTextCursor::PreviousBlock); |
| 603 | cursor.select(QTextCursor::LineUnderCursor); |
| 604 | sectionStart = cursor.selectedText().simplified().startsWith("##"); |
| 605 | } |
| 606 | int start = cursor.anchor(); |
| 607 | cursor = m_mainEditor->textCursor(); |
| 608 | cursor.movePosition(QTextCursor::NextBlock); |
| 609 | cursor.select(QTextCursor::LineUnderCursor); |
| 610 | bool sectionEnd = cursor.selectedText().simplified().startsWith("##"); |
| 611 | while (!sectionEnd && !cursor.atEnd()) { |
| 612 | cursor.movePosition(QTextCursor::NextBlock); |
| 613 | cursor.select(QTextCursor::LineUnderCursor); |
| 614 | sectionEnd = cursor.selectedText().simplified().startsWith("##"); |
| 615 | } |
| 616 | cursor.movePosition(QTextCursor::EndOfLine); |
| 617 | cursor.setPosition(start, QTextCursor::KeepAnchor); |
| 618 | m_mainEditor->setTextCursor(cursor); |
| 619 | section = cursor.selectedText(); |
| 620 | section.replace(QChar(0x2029), QChar('\n')); |
| 621 | } else if (m_mode == EDIT_CSOUND_MODE || m_mode == EDIT_ORC_MODE) { |
| 622 | QTextCursor cursor = m_mainEditor->textCursor(); |
| 623 | m_oldCursorPosition = cursor.position(); // to move back there on next keypress |
| 624 | cursor.select(QTextCursor::LineUnderCursor); |
| 625 | QString text = cursor.selectedText().simplified(); |
| 626 | bool sectionStart = text.startsWith("instr") || text.startsWith(";;"); |
| 627 | while (!sectionStart && !cursor.anchor() == 0) { |
| 628 | cursor.movePosition(QTextCursor::PreviousBlock); |
| 629 | cursor.select(QTextCursor::LineUnderCursor); |
| 630 | text = cursor.selectedText().simplified(); |
| 631 | sectionStart = text.startsWith("instr") || text.startsWith(";;"); |
| 632 | } |
| 633 | int start = cursor.anchor(); |
| 634 | cursor = m_mainEditor->textCursor(); |
| 635 | cursor.movePosition(QTextCursor::NextBlock); |
| 636 | cursor.select(QTextCursor::LineUnderCursor); |
| 637 | text = cursor.selectedText().simplified(); |
| 638 | bool sectionEnd = text.startsWith("endin") || text.startsWith(";;"); |
| 639 | while (!sectionEnd && !cursor.atEnd()) { |
| 640 | cursor.movePosition(QTextCursor::NextBlock); |
| 641 | cursor.select(QTextCursor::LineUnderCursor); |
| 642 | text = cursor.selectedText().simplified(); |
| 643 | sectionEnd = text.startsWith("endin") || text.startsWith(";;"); |
| 644 | } |
| 645 | cursor.movePosition(QTextCursor::EndOfLine); |
| 646 | cursor.setPosition(start, QTextCursor::KeepAnchor); |
| 647 | m_mainEditor->setTextCursor(cursor); |
| 648 | section = cursor.selectedText(); |
| 649 | section.replace(QChar(0x2029), QChar('\n')); |
| 650 | } |
| 651 | } |
| 652 | else { // Split view |
| 653 | // TODO check properly for line number also from other editors |
| 654 | qDebug() << "Not implemented for split view."; |
| 655 | } |
| 656 | return section; |
| 657 | } |
| 658 | |
| 659 | QString DocumentView::getActiveText() |
| 660 | { |
| 661 | QString selection; |
| 662 | if (m_viewMode < 2) { |
| 663 | QTextCursor cursor = m_mainEditor->textCursor(); |
| 664 | selection = cursor.selectedText(); |
| 665 | if (selection == "") { |
| 666 | cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor); |
| 667 | cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor); |
| 668 | selection = cursor.selectedText(); |
| 669 | } |
| 670 | selection.replace(QChar(0x2029), QChar('\n')); |
| 671 | if (!selection.endsWith("\n")) { |
| 672 | selection.append("\n"); |
| 673 | } |
| 674 | } |
| 675 | else { // Split view |
| 676 | // TODO check properly for line number also from other editors |
| 677 | qDebug() << "Not implemented for split view."; |
| 678 | } |
| 679 | return selection; |
| 680 | } |
| 681 | |
| 682 | void DocumentView::syntaxCheck() |
| 683 | { |
| 684 | // TODO implment for multiple views |
| 685 | |
| 686 | TextEditor *editor; |
| 687 | editor = m_viewMode < 2 ? m_mainEditor : (TextEditor *) sender(); |
| 688 | m_currentEditor = editor ; // for parenthesis functions |
| 689 | |
| 690 | QTextCursor cursor = editor->textCursor(); |
| 691 | |
| 692 | // send cursor position |
| 693 | emit newLineAndColumn(cursor.blockNumber()+1 , cursor.positionInBlock()+1); |
| 694 | |
| 695 | |
| 696 | // matchparenthesis. Code by Geir Vatterkar see https://doc.qt.io/archives/qq/QtQuarterly31.pdf |
| 697 | // some corrections by Heinz van Saanen http://qt-apps.org/content/show.php/CLedit?content=125532 ; comments: http://www.qtcentre.org/archive/index.php/t-31084.html |
| 698 | |
| 699 | QList<QTextEdit::ExtraSelection> selections; |
| 700 | editor->setExtraSelections(selections); |
| 701 | |
| 702 | TextBlockData *data = static_cast<TextBlockData *>(editor->textCursor().block().userData()); |
| 703 | |
| 704 | if (data) { |
| 705 | QVector<ParenthesisInfo *> infos = data->parentheses(); |
| 706 | |
| 707 | int pos = editor->textCursor().block().position(); |
| 708 | for (int i = 0; i < infos.size(); ++i) { |
| 709 | ParenthesisInfo *info = infos.at(i); |
| 710 | |
| 711 | int curPos = editor->textCursor().position() - editor->textCursor().block().position(); |
| 712 | if (info->position == curPos && info->character == '(') { |
| 713 | if (matchLeftParenthesis(editor->textCursor().block(), i + 1, 0)) |
| 714 | createParenthesisSelection(pos + info->position, true); // found, mark as pair |
| 715 | else |
| 716 | createParenthesisSelection(pos + info->position, false); // no pair, mark as single |
| 717 | } else if (info->position == curPos - 1 && info->character == ')') { |
| 718 | if (matchRightParenthesis(editor->textCursor().block(), i - 1, 0)) |
| 719 | createParenthesisSelection(pos + info->position, true); |
| 720 | else |
| 721 | createParenthesisSelection(pos + info->position, false); |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | // syntax check |
| 727 | cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::MoveAnchor); |
| 728 | cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor); |
| 729 | auto words = cursor.selectedText().split(QRegExp("\\b"), |
| 730 | SKIP_EMPTY_PARTS); |
| 731 | bool showHover = false; |
| 732 | for(int i = 0; i < words.size(); i++) { |
| 733 | auto word = words[words.size() - i - 1]; |
| 734 | if (m_opcodeTree->isOpcode(word)) { |
| 735 | QString syntax = m_opcodeTree->getSyntax(word); |
| 736 | if(!syntax.isEmpty()) { |
| 737 | emit(opcodeSyntaxSignal(syntax)); |
| 738 | m_currentOpcodeText = syntax; |
| 739 | showHover = i == 0 && editor->textCursor().hasSelection(); |
| 740 | break; |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | if (showHover) { |
| 745 | showHoverText(); |
| 746 | } else { |
| 747 | hideHoverText(); |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | // parentheses matching functions ------------- |
| 752 | |
| 753 | bool DocumentView::matchLeftParenthesis(QTextBlock currentBlock, int i, int numLeftParentheses) |
| 754 | { |
| 755 | TextBlockData *data = static_cast<TextBlockData *>(currentBlock.userData()); |
| 756 | QVector<ParenthesisInfo *> infos = data->parentheses(); |
| 757 | |
| 758 | int docPos = currentBlock.position(); |
| 759 | for (; i < infos.size(); ++i) { |
| 760 | ParenthesisInfo *info = infos.at(i); |
| 761 | |
| 762 | if (info->character == '(') { |
| 763 | ++numLeftParentheses; |
| 764 | continue; |
| 765 | } |
| 766 | |
| 767 | if (info->character == ')' && numLeftParentheses == 0) { |
| 768 | createParenthesisSelection(docPos + info->position); |
| 769 | return true; |
| 770 | } else |
| 771 | --numLeftParentheses; |
| 772 | } |
| 773 | |
| 774 | currentBlock = currentBlock.next(); |
| 775 | if (currentBlock.isValid()) |
| 776 | return matchLeftParenthesis(currentBlock, 0, numLeftParentheses); |
| 777 | |
| 778 | return false; |
| 779 | } |
| 780 | |
| 781 | |
| 782 | bool DocumentView::matchRightParenthesis(QTextBlock currentBlock, int index, int numRightParentheses) |
| 783 | { |
| 784 | TextBlockData *data = static_cast<TextBlockData *>(currentBlock.userData()); |
| 785 | QVector<ParenthesisInfo *> parentheses = data->parentheses(); |
| 786 | |
| 787 | // Match in same line? |
| 788 | int docPos = currentBlock.position(); |
| 789 | for (int j=index; j>=0; --j ) { |
| 790 | ParenthesisInfo *info = parentheses.at(j); |
| 791 | if (info->character == ')') { |
| 792 | ++numRightParentheses; |
| 793 | continue; |
| 794 | } |
| 795 | if (info->character == '(' && numRightParentheses == 0) { |
| 796 | createParenthesisSelection(docPos + info->position); |
| 797 | return true; |
| 798 | } else |
| 799 | --numRightParentheses; |
| 800 | } |
| 801 | |
| 802 | // No match yet? Then try previous block |
| 803 | currentBlock = currentBlock.previous(); |
| 804 | if (currentBlock.isValid()) { |
| 805 | // Recalculate correct index first |
| 806 | TextBlockData *data = static_cast<TextBlockData *>( currentBlock.userData() ); |
| 807 | QVector<ParenthesisInfo *> infos = data->parentheses(); |
| 808 | |
| 809 | return matchRightParenthesis(currentBlock, infos.size()-1, numRightParentheses); |
| 810 | } |
| 811 | |
| 812 | // No match at all |
| 813 | return false; |
| 814 | } |
| 815 | |
| 816 | void DocumentView::createParenthesisSelection(int pos, bool paired) |
| 817 | { |
| 818 | //TODO: get editor from parameters |
| 819 | QTextEdit * editor = (!m_currentEditor) ? m_mainEditor : m_currentEditor ; // if currentEditor not set, use mainEditor |
| 820 | |
| 821 | QList<QTextEdit::ExtraSelection> selections = editor->extraSelections(); |
| 822 | |
| 823 | QTextEdit::ExtraSelection selection; |
| 824 | QTextCharFormat format = selection.format; |
| 825 | auto palette = qApp->palette(); |
| 826 | auto bgcolor = (paired) ? palette.highlight() : palette.midlight(); |
| 827 | // format.setBackground( (paired) ? Qt::lightGray: Qt::magenta); // if single parenthesis, mark with magenta |
| 828 | format.setBackground(bgcolor); |
| 829 | selection.format = format; |
| 830 | |
| 831 | QTextCursor cursor = editor->textCursor(); |
| 832 | cursor.setPosition(pos); |
| 833 | cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor); |
| 834 | selection.cursor = cursor; |
| 835 | |
| 836 | selections.append(selection); |
| 837 | |
| 838 | editor->setExtraSelections(selections); |
| 839 | } |
| 840 | |
| 841 | |
| 842 | const QStringList DocumentView::getAllWords() { |
| 843 | // All words are parsed at max. each 4 seconds |
| 844 | const int minMsecsFromLastParsing = 4000; |
| 845 | auto now = QTime::currentTime(); |
| 846 | int msecs = m_lastWordsUpdate.msecsTo(now); |
| 847 | if(m_allWords.isEmpty() || msecs > minMsecsFromLastParsing) { |
| 848 | QDEBUG << "Updating all words, time since last update: " << msecs << "msecs"; |
| 849 | TextEditor *editor = m_mainEditor; |
| 850 | QString wholeText = editor->toPlainText(); |
| 851 | auto allWords = wholeText.split(QRegularExpression("[" + QRegularExpression::escape("+-*/=#&,\"\'|[]()<>.;:^") + "\\s]"), SKIP_EMPTY_PARTS); |
| 852 | allWords.removeDuplicates(); |
| 853 | m_lastWordsUpdate = now; |
| 854 | m_allWords = allWords; |
| 855 | } |
| 856 | return m_allWords; |
| 857 | } |
| 858 | |
| 859 | |
| 860 | void DocumentView::autoCompleteAtCursor() { |
| 861 | TextEditor *editor = m_mainEditor; |
| 862 | // TODO: replace all this with QCompleter |
| 863 | QTextCursor cursor = editor->textCursor(); |
| 864 | int curIndex = cursor.position(); |
| 865 | |
| 866 | // Only use the word written until cursor, not the entire word under the cursor |
| 867 | // This is the behaviour used in all text editors |
| 868 | cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor); |
| 869 | cursor.setPosition(curIndex, QTextCursor::KeepAnchor); |
| 870 | QString word = cursor.selectedText(); |
| 871 | |
| 872 | if(word.isEmpty()) { |
| 873 | // This handles the special case where the cursor is at a ")" or a "," |
| 874 | // which work as separators so the word to the left appears empty |
| 875 | cursor.select(QTextCursor::WordUnderCursor); |
| 876 | QString wordUnderCursor = cursor.selectedText(); |
| 877 | if(wordUnderCursor != "," && wordUnderCursor != ")") |
| 878 | return; |
| 879 | |
| 880 | cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::MoveAnchor, 2); |
| 881 | cursor.movePosition(QTextCursor::StartOfWord); |
| 882 | cursor.select(QTextCursor::WordUnderCursor); |
| 883 | word = cursor.selectedText(); |
| 884 | if(word.isEmpty()) { |
| 885 | return; |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | if(word.size() < 3) |
| 890 | // only start completion after three characters |
| 891 | return; |
| 892 | |
| 893 | QTextCursor lineCursor = editor->textCursor(); |
| 894 | lineCursor.select(QTextCursor::LineUnderCursor); |
| 895 | QString line = lineCursor.selectedText(); |
| 896 | |
| 897 | int lineCommentIndex; |
| 898 | |
| 899 | if((lineCommentIndex = line.indexOf(";")) >= 0) { |
| 900 | if(lineCursor.position() - line.length() + lineCommentIndex < curIndex) |
| 901 | return; |
| 902 | } |
| 903 | if((lineCommentIndex = line.indexOf("//")) >= 0) { |
| 904 | if(lineCursor.position() - line.length() + lineCommentIndex < curIndex) |
| 905 | return; |
| 906 | } |
| 907 | |
| 908 | static QRegularExpression rxOpcodeOrInstrAtStart("^\\s*(opcode|instr)"); |
| 909 | if(rxOpcodeOrInstrAtStart.match(line).hasMatch()) { |
| 910 | return; |
| 911 | } |
| 912 | static QRegularExpression rxTwoWords("\\s*\\w+\\s+\\w+"); |
| 913 | bool useFunction = false; |
| 914 | if(!rxTwoWords.match(line).hasMatch()) { |
| 915 | useFunction = true; |
| 916 | } |
| 917 | // matches long command line options, like --nosound, -+rtaudio, etc. |
| 918 | static QRegularExpression rxCommandLineOption("\\s*-([+-])(\\w*)"); |
| 919 | |
| 920 | bool showSyntaxMenu = false; |
| 921 | QSet<QString>menuWordsSeen; |
| 922 | QRegularExpressionMatch rxmatch; |
| 923 | if((rxmatch = rxCommandLineOption.match(line)).hasMatch()) { |
| 924 | syntaxMenu->clear(); |
| 925 | auto capt = rxmatch.captured(2); |
| 926 | auto options = rxmatch.captured(1)=="-"? m_longOptions: m_longOptions2; |
| 927 | for(auto option: options) { |
| 928 | if(option.startsWith(capt)) { |
| 929 | QAction *a = syntaxMenu->addAction(option, this, |
| 930 | SLOT(insertAutoCompleteText())); // was: insertParameterText that does not exist any more |
| 931 | a->setData(option); |
| 932 | showSyntaxMenu = true; |
| 933 | } |
| 934 | } |
| 935 | } |
| 936 | else if (cursor.position() > cursor.anchor() && word.size() > 2 && !word.startsWith("\"")) { // Only at the end of the word |
| 937 | syntaxMenu->clear(); |
| 938 | foreach(QString var, m_localVariables) { |
| 939 | if (var.endsWith(',')) { |
| 940 | var.chop(1); |
| 941 | } |
| 942 | if (var.startsWith(word) && word != var) { |
| 943 | QAction *a = syntaxMenu->addAction(var, this, |
| 944 | SLOT(insertAutoCompleteText())); // was: insertParameterText that does not exist any more |
| 945 | a->setData(var); |
| 946 | showSyntaxMenu = true; |
| 947 | } |
| 948 | } |
| 949 | // opcodes and parameters |
| 950 | auto opcodedefs = m_opcodeTree->getMatchingOpcodes(word); |
| 951 | bool allEqual = true; |
| 952 | for(int i = 0; i < opcodedefs.size(); i++) { |
| 953 | if (opcodedefs[i].opcodeName != word) { |
| 954 | allEqual = false; |
| 955 | break; |
| 956 | } |
| 957 | } |
| 958 | if (opcodedefs.size() > 0 && !allEqual) { |
| 959 | for(int i = 0; i < opcodedefs.size(); i++) { |
| 960 | QString opcodeName = opcodedefs[i].opcodeName; |
| 961 | QString text = opcodeName.size() < 14 ? |
| 962 | opcodeName.leftJustified(14) : |
| 963 | opcodeName + " "; |
| 964 | |
| 965 | QString outArgs = opcodedefs[i].outArgs; |
| 966 | auto inArgs = opcodedefs[i].inArgs; |
| 967 | if(!inArgs.isEmpty()) { |
| 968 | text += inArgs.size()<=28 ? inArgs : inArgs.mid(0, 27)+"…"; |
| 969 | } |
| 970 | if(text.size() < 42) { |
| 971 | text = text.leftJustified(42); |
| 972 | } |
| 973 | switch(outArgs[0].toLatin1()) { |
| 974 | case 'a': |
| 975 | text += " :a"; break; |
| 976 | case 'k': |
| 977 | text += " :k"; break; |
| 978 | case 'i': |
| 979 | text += " :i"; break; |
| 980 | case 'x': |
| 981 | text += " :x"; break; |
| 982 | case 'S': |
| 983 | text += " :S"; break; |
| 984 | case 'f': |
| 985 | text += " :pvs"; break; |
| 986 | } |
| 987 | |
| 988 | QString syntaxText; |
| 989 | if (useFunction && !opcodedefs[i].inArgs.isEmpty()) { |
| 990 | syntaxText = QString("%1(%2)").arg( |
| 991 | opcodedefs[i].opcodeName.simplified(), |
| 992 | opcodedefs[i].inArgs.simplified()); |
| 993 | } else { |
| 994 | syntaxText= opcodedefs[i].outArgs.simplified(); |
| 995 | if (!opcodedefs[i].outArgs.isEmpty()) |
| 996 | syntaxText += " "; |
| 997 | syntaxText += opcodedefs[i].opcodeName.simplified(); |
| 998 | if (!opcodedefs[i].inArgs.isEmpty()) { |
| 999 | if (opcodedefs[i].inArgs.contains("(x)") ) { |
| 1000 | syntaxText += "(x)"; // avoid other text like (with no rate restriction) |
| 1001 | } else { |
| 1002 | syntaxText += " " + opcodedefs[i].inArgs.simplified(); |
| 1003 | } |
| 1004 | } |
| 1005 | } |
| 1006 | auto a = syntaxMenu->addAction(text, this, SLOT(insertAutoCompleteText())); |
| 1007 | a->setData(syntaxText); |
| 1008 | a->setToolTip(syntaxText); |
| 1009 | showSyntaxMenu = true; |
| 1010 | menuWordsSeen.insert(opcodeName); |
| 1011 | } |
| 1012 | syntaxMenu->addSeparator(); |
| 1013 | } |
| 1014 | // check for autcompletion from ALL words in text editor |
| 1015 | auto allWords = getAllWords(); |
| 1016 | QStringList menuWords; |
| 1017 | QString wordlow = word.toLower(); // this must be AFTER the word is corrected |
| 1018 | |
| 1019 | // any word [was: variables ] |
| 1020 | //QRegularExpression rxVariables("\\b(g)?[iakS][a-zA-Z0-9_]+"); // this only matches variable names |
| 1021 | static QRegularExpression rxAnyWord("\\b[A-Za-z][a-zA-Z0-9_]*\\b" ); // any word that starts with a letter |
| 1022 | QRegularExpressionMatch match = rxAnyWord.match(word); |
| 1023 | for(auto theWord: allWords) { |
| 1024 | if (word != theWord && |
| 1025 | theWord.toLower().startsWith(wordlow) && |
| 1026 | !menuWordsSeen.contains(theWord) && |
| 1027 | /* rxVariables.match(word).hasMatch() */ |
| 1028 | match.hasMatch() ) { |
| 1029 | auto a = syntaxMenu->addAction(theWord, this, SLOT(insertAutoCompleteText())); |
| 1030 | a->setData(theWord); |
| 1031 | showSyntaxMenu = true; |
| 1032 | menuWordsSeen.insert(theWord); |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | for(auto tag: tagWords) { |
| 1037 | if(tag.toLower().startsWith(wordlow) && word != tag) { |
| 1038 | auto a = syntaxMenu->addAction(tag, this, SLOT(insertAutoCompleteText())); |
| 1039 | a->setData(tag); |
| 1040 | showSyntaxMenu = true; |
| 1041 | } |
| 1042 | } |
| 1043 | } |
| 1044 | else { |
| 1045 | destroySyntaxMenu(); |
| 1046 | } |
| 1047 | if(showSyntaxMenu && !syntaxMenu->actions().isEmpty()) { |
| 1048 | QRect r = editor->cursorRect(); |
| 1049 | QPoint p = QPoint(r.x() + r.width(), r.y() + r.height()); |
| 1050 | syntaxMenu->setDefaultAction(syntaxMenu->actions()[0]); |
| 1051 | syntaxMenu->move(editor->mapToGlobal(p)); |
| 1052 | syntaxMenu->show(); |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | void DocumentView::textChanged() { |
| 1057 | if (internalChange) { |
| 1058 | internalChange = false; |
| 1059 | return; |
| 1060 | } |
| 1061 | |
| 1062 | //test: seems to do the trick and solve "always modified bug |
| 1063 | bool modified = m_mainEditor->document()->isModified(); |
| 1064 | setModified(modified); |
| 1065 | if(!modified) { |
| 1066 | return; |
| 1067 | } |
| 1068 | |
| 1069 | // This should go somewhere else, maybe when escape is pressed? |
| 1070 | // unmarkErrorLines(); |
| 1071 | |
| 1072 | if(!(m_mode == EDIT_CSOUND_MODE || m_mode == EDIT_ORC_MODE)) |
| 1073 | return; |
| 1074 | syntaxCheck(); |
| 1075 | if(m_autoComplete) { |
| 1076 | if(m_autoCompleteTimer != nullptr && m_autoCompleteTimer->isActive()) { |
| 1077 | m_autoCompleteTimer->stop(); |
| 1078 | } |
| 1079 | auto key = m_mainEditor->getLastKey(); |
| 1080 | if(key != Qt::Key_Backspace && key != Qt::Key_Delete && key != Qt::Key_Space && key != Qt::Key_Comma && key != Qt::Key_BraceLeft && key != Qt::Key_Colon) { |
| 1081 | // Only perform autocomplete if the change was not generated by deleting |
| 1082 | // Also pressing any word separator would never lead to autocomplete |
| 1083 | auto autoCompleteTask = new QTimer(this); |
| 1084 | autoCompleteTask->setInterval(m_autoCompleteDelay); |
| 1085 | autoCompleteTask->setSingleShot(true); |
| 1086 | connect(autoCompleteTask, &QTimer::timeout, this, &DocumentView::autoCompleteAtCursor); |
| 1087 | autoCompleteTask->start(); |
| 1088 | m_autoCompleteTimer = autoCompleteTask; |
| 1089 | } |
| 1090 | |
| 1091 | // this->autoCompleteAtCursor(); |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | void DocumentView::escapePressed() |
| 1096 | { |
| 1097 | // TODO implment for multiple views |
| 1098 | if (m_viewMode < 2) { |
| 1099 | if(errorMarked) |
| 1100 | unmarkErrorLines(); |
| 1101 | |
| 1102 | if (m_mainEditor->getParameterMode()) { |
| 1103 | // Force unselecting |
| 1104 | m_mainEditor->moveCursor(QTextCursor::NextCharacter); |
| 1105 | m_mainEditor->moveCursor(QTextCursor::PreviousCharacter); |
| 1106 | exitParameterMode(); |
| 1107 | } else if (m_hoverText->isVisible()){ |
| 1108 | hideHoverText(); |
| 1109 | } else { |
| 1110 | qDebug()<<"closeExtraPanels now omitted - let know, if it should be brought back."; |
| 1111 | //emit closeExtraPanels(); |
| 1112 | } |
| 1113 | } |
| 1114 | else { |
| 1115 | qDebug() << "Not implemented for split view"; |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | void DocumentView::finishParameterMode() |
| 1120 | { |
| 1121 | // TODO implment for multiple views |
| 1122 | if (m_viewMode < 2) { |
| 1123 | if (m_mainEditor->getParameterMode()) { |
| 1124 | killToEnd(); |
| 1125 | exitParameterMode(); |
| 1126 | } else { |
| 1127 | hideHoverText(); |
| 1128 | } |
| 1129 | } else { |
| 1130 | qDebug() << "Not implemented for split view"; |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | void DocumentView::exitParameterMode() |
| 1135 | { |
| 1136 | // TODO implment for multiple views |
| 1137 | if (m_viewMode < 2) { |
| 1138 | m_mainEditor->setParameterMode(false); |
| 1139 | } else { |
| 1140 | qDebug() << "Not implemented for split view"; |
| 1141 | } |
| 1142 | hideHoverText(); |
| 1143 | // parameterButton->setVisible(false); |
| 1144 | } |
| 1145 | |
| 1146 | void DocumentView::indentNewLine() |
| 1147 | { |
| 1148 | QTextCursor linecursor = m_mainEditor->textCursor(); |
| 1149 | if (m_mode == EDIT_PYTHON_MODE){ |
| 1150 | linecursor.movePosition(QTextCursor::PreviousCharacter); |
| 1151 | linecursor.select(QTextCursor::LineUnderCursor); |
| 1152 | QString line = linecursor.selectedText(); |
| 1153 | if (line.endsWith(":")) { |
| 1154 | m_mainEditor->insertPlainText(" "); |
| 1155 | } |
| 1156 | } else if (m_mode == EDIT_CSOUND_MODE || m_mode == EDIT_ORC_MODE |
| 1157 | || m_mode == EDIT_SCO_MODE || m_mode == EDIT_INC_MODE) { |
| 1158 | linecursor.movePosition(QTextCursor::PreviousBlock); |
| 1159 | linecursor.select(QTextCursor::LineUnderCursor); |
| 1160 | QString line = linecursor.selectedText(); |
| 1161 | QRegExp regex = QRegExp("\\s+"); |
| 1162 | if (line.indexOf(regex) == 0) { |
| 1163 | m_mainEditor->insertPlainText(regex.cap()); |
| 1164 | } |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | void DocumentView::findReplace() |
| 1169 | { |
| 1170 | // TODO implment for multiple views |
| 1171 | internalChange = true; |
| 1172 | if (m_viewMode < 2) { |
| 1173 | QTextCursor cursor = m_mainEditor->textCursor(); |
| 1174 | QString word = cursor.selectedText(); |
| 1175 | cursor.select(QTextCursor::WordUnderCursor); |
| 1176 | QString word2 = cursor.selectedText(); |
| 1177 | if (word == word2 && word!= "") { |
| 1178 | lastSearch = word; |
| 1179 | } |
| 1180 | FindReplace *dialog = new FindReplace(this, |
| 1181 | m_mainEditor, |
| 1182 | &lastSearch, |
| 1183 | &lastReplace, |
| 1184 | &lastCaseSensitive); |
| 1185 | // lastSearch and lastReplace are passed by reference to be |
| 1186 | // updated by FindReplace dialog |
| 1187 | connect(dialog, SIGNAL(findString(QString)), this, SLOT(findString(QString))); |
| 1188 | dialog->show(); |
| 1189 | } |
| 1190 | else { // Split view |
| 1191 | // TODO check properly for line number also from other editors |
| 1192 | qDebug() << "Not implemented for split view."; |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | void DocumentView::gotoLineDialog() |
| 1197 | { |
| 1198 | auto dialog = new QDialog(this); |
| 1199 | dialog->resize(300, 120); |
| 1200 | dialog->setModal(true); |
| 1201 | dialog->setWindowTitle(tr("Go to Line")); |
| 1202 | |
| 1203 | auto layout = new QGridLayout(dialog); |
| 1204 | |
| 1205 | auto label = new QLabel(tr("Go To Line: ")); |
| 1206 | layout->addWidget(label, 0, 0, Qt::AlignRight|Qt::AlignVCenter); |
| 1207 | |
| 1208 | auto lineSpinBox = new QSpinBox(dialog); |
| 1209 | lineSpinBox->unsetLocale(); // for chinease numbers to work |
| 1210 | lineSpinBox->setRange(0, 99999999); |
| 1211 | layout->addWidget(lineSpinBox, 0, 1, Qt::AlignLeft|Qt::AlignVCenter); |
| 1212 | |
| 1213 | auto okButton = new QPushButton(tr("Ok")); |
| 1214 | layout->addWidget(okButton, 10, 1, Qt::AlignCenter|Qt::AlignVCenter); |
| 1215 | connect(okButton, &QPushButton::clicked, |
| 1216 | [this, lineSpinBox](){ this->jumpToLine(lineSpinBox->value()); }); |
| 1217 | connect(okButton, &QPushButton::clicked, [dialog](){ dialog->close(); }); |
| 1218 | |
| 1219 | lineSpinBox->setFocus(); |
| 1220 | lineSpinBox->selectAll(); |
| 1221 | dialog->show(); |
| 1222 | } |
| 1223 | |
| 1224 | void DocumentView::getToIn() |
| 1225 | { |
| 1226 | // Change chnget/chnset to invalue/outvalue |
| 1227 | // TODO implment for multiple views |
| 1228 | if (m_viewMode < 2) { |
| 1229 | internalChange = true; |
| 1230 | m_mainEditor->setPlainText(changeToInvalue(m_mainEditor->toPlainText())); |
| 1231 | m_mainEditor->document()->setModified(true); // Necessary, or is setting it locally enough? |
| 1232 | } |
| 1233 | else { // Split view |
| 1234 | qDebug() << "Not implemented for split view."; |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | void DocumentView::inToGet() |
| 1239 | { |
| 1240 | // TODO implment for multiple views |
| 1241 | if (m_viewMode < 2) { |
| 1242 | internalChange = true; |
| 1243 | m_mainEditor->setPlainText(changeToChnget(m_mainEditor->toPlainText())); |
| 1244 | m_mainEditor->document()->setModified(true); |
| 1245 | } |
| 1246 | else { // Split view |
| 1247 | // TODO check properly for line number also from other editors |
| 1248 | qDebug() << "Not implemented for split view."; |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | void DocumentView::insertAutoCompleteText() |
| 1253 | { |
| 1254 | TextEditor *editor; |
| 1255 | if (m_viewMode < 2) { |
| 1256 | editor = m_mainEditor; |
| 1257 | } |
| 1258 | else { // Split view |
| 1259 | editor = (TextEditor *) focusWidget(); |
| 1260 | // TODO check properly for line number also from other editors |
| 1261 | qDebug() << "Not implemented for split view."; |
| 1262 | } |
| 1263 | if (editor != 0) { |
| 1264 | internalChange = true; |
| 1265 | QAction *action = static_cast<QAction *>(QObject::sender()); |
| 1266 | bool insertComplete = static_cast<MySyntaxMenu *>(action->parent())->insertComplete; |
| 1267 | |
| 1268 | QTextCursor cursor = editor->textCursor(); |
| 1269 | cursor.select(QTextCursor::WordUnderCursor); |
| 1270 | while ((cursor.selectedText() == "" || cursor.selectedText() == "," || cursor.selectedText()==")") && !cursor.atBlockStart()) { |
| 1271 | cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::MoveAnchor, 2); |
| 1272 | cursor.select(QTextCursor::WordUnderCursor); |
| 1273 | } |
| 1274 | |
| 1275 | cursor.insertText(""); |
| 1276 | editor->setTextCursor(cursor); |
| 1277 | |
| 1278 | QTextCursor cursor2 = editor->textCursor(); |
| 1279 | cursor2.movePosition(QTextCursor::StartOfLine,QTextCursor::KeepAnchor); |
| 1280 | bool noOutargs = false; |
| 1281 | if (!cursor2.selectedText().simplified().isEmpty()) { // Text before cursor, don't put outargs |
| 1282 | noOutargs = true; |
| 1283 | } |
| 1284 | internalChange = true; |
| 1285 | if (insertComplete) { |
| 1286 | if (noOutargs) { |
| 1287 | QString syntaxText = action->data().toString(); |
| 1288 | int index =syntaxText.indexOf(QRegExp("\\w\\s+\\w")); |
| 1289 | editor->insertPlainText(syntaxText.mid(index + 1).trimmed()); // right returns the whole string if index < 0 |
| 1290 | } |
| 1291 | else { |
| 1292 | editor->insertPlainText(action->data().toString()); |
| 1293 | } |
| 1294 | QString syntaxText = action->data().toString(); |
| 1295 | QStringList syntaxSections = syntaxText.simplified().split(" "); // was split("") |
| 1296 | QString actionText = action->text(); |
| 1297 | actionText = actionText.split(" ").at(0); |
| 1298 | if (m_autoParameterMode && m_opcodeTree->isOpcode(actionText) && syntaxSections.size() > 1) { |
| 1299 | m_mainEditor->moveCursor(QTextCursor::StartOfBlock); |
| 1300 | m_mainEditor->setParameterMode(true); |
| 1301 | prevParameter(); |
| 1302 | } |
| 1303 | } else { |
| 1304 | int index = action->text().indexOf(" "); |
| 1305 | if (index > 0) { |
| 1306 | editor->insertPlainText(action->text().left(index)); |
| 1307 | } |
| 1308 | else { |
| 1309 | editor->insertPlainText(action->text()); |
| 1310 | } |
| 1311 | } |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | void DocumentView::findString(QString query) |
| 1316 | { |
| 1317 | // TODO search across all editors |
| 1318 | if (m_viewMode < 2) { |
| 1319 | if (query == "") { |
| 1320 | query = lastSearch; |
| 1321 | } |
| 1322 | bool found = false; |
| 1323 | if (lastCaseSensitive) { |
| 1324 | found = m_mainEditor->find(query, |
| 1325 | QTextDocument::FindCaseSensitively); |
| 1326 | } |
| 1327 | else |
| 1328 | found = m_mainEditor->find(query); |
| 1329 | |
| 1330 | if (!found) { |
| 1331 | int ret = QMessageBox::question(this, tr("Find and replace"), |
| 1332 | tr("The string was not found.\n" |
| 1333 | "Would you like to start from the top?"), |
| 1334 | QMessageBox::Yes | QMessageBox::No, |
| 1335 | QMessageBox::No |
| 1336 | ); |
| 1337 | if (ret == QMessageBox::Yes) { |
| 1338 | m_mainEditor->moveCursor(QTextCursor::Start); |
| 1339 | findString(); |
| 1340 | } |
| 1341 | } |
| 1342 | } |
| 1343 | else { // Split view |
| 1344 | // TODO check properly for line number also from other editors |
| 1345 | qDebug() << "Not implemented for split view."; |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | void DocumentView::evaluate() |
| 1350 | { |
| 1351 | emit evaluate(getActiveText()); |
| 1352 | } |
| 1353 | |
| 1354 | void DocumentView::createContextMenu(QPoint pos) |
| 1355 | { |
| 1356 | if (m_viewMode < 2) { |
| 1357 | QMenu *menu = m_mainEditor->createStandardContextMenu(); |
| 1358 | menu->addSeparator(); |
| 1359 | menu->addAction(tr("Evaluate Selection"), this, SLOT(evaluate())); |
| 1360 | menu->addAction(tr("Opcode Help"), this, SLOT(opcodeHelp())); |
| 1361 | menu->addAction(tr("Show/hide line numbers"), this, SLOT(toggleLineArea())); |
| 1362 | menu->addSeparator(); |
| 1363 | QMenu *opcodeMenu = menu->addMenu("Opcodes"); |
| 1364 | QMenu *mainMenu = 0; |
| 1365 | QMenu *subMenu; |
| 1366 | QString currentMain = ""; |
| 1367 | for (int i = 0; i < m_opcodeTree->getCategoryCount(); i++) { |
| 1368 | QString category = m_opcodeTree->getCategory(i); |
| 1369 | QStringList categorySplit = category.split(":"); |
| 1370 | if (!categorySplit.isEmpty() && categorySplit[0] != currentMain) { |
| 1371 | mainMenu = opcodeMenu->addMenu(categorySplit[0]); |
| 1372 | currentMain = categorySplit[0]; |
| 1373 | } |
| 1374 | if (categorySplit.size() < 2) { |
| 1375 | subMenu = mainMenu; |
| 1376 | } |
| 1377 | else { |
| 1378 | subMenu = mainMenu->addMenu(categorySplit[1]); |
| 1379 | } |
| 1380 | foreach(Opcode opcode, m_opcodeTree->getOpcodeList(i)) { |
| 1381 | QAction *action = subMenu->addAction(opcode.opcodeName, this, SLOT(opcodeFromMenu())); |
| 1382 | QString opcodeText = opcode.outArgs; |
| 1383 | opcodeText += (!opcode.outArgs.isEmpty() |
| 1384 | && !opcode.outArgs.endsWith(" ") |
| 1385 | && !opcode.opcodeName.startsWith(" ") ? |
| 1386 | " " : ""); |
| 1387 | opcodeText += opcode.opcodeName; |
| 1388 | opcodeText += (!opcode.inArgs.isEmpty() |
| 1389 | && !opcode.inArgs.startsWith(" ") |
| 1390 | && !opcode.opcodeName.endsWith(" ") ? |
| 1391 | " " : ""); |
| 1392 | opcodeText += opcode.inArgs; |
| 1393 | action->setData(opcodeText); |
| 1394 | } |
| 1395 | } |
| 1396 | menu->exec(m_mainEditor->mapToGlobal(pos)); |
| 1397 | delete menu; |
| 1398 | } |
| 1399 | else { // Split view |
| 1400 | // TODO check properly for line number also from other editors |
| 1401 | qDebug() << "Not implemented for split view."; |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | |
| 1406 | void DocumentView::showOrc(bool show) |
| 1407 | { |
| 1408 | // FIXME set m_viewmode |
| 1409 | if (m_viewMode >= 2) { |
| 1410 | m_orcEditor->setVisible(show); |
| 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | void DocumentView::showScore(bool show) |
| 1415 | { |
| 1416 | // FIXME set m_viewmode |
| 1417 | if (m_viewMode >= 2) { |
| 1418 | m_scoreEditor->setVisible(show); |
| 1419 | } |
| 1420 | } |
| 1421 | |
| 1422 | void DocumentView::showOptions(bool show) |
| 1423 | { |
| 1424 | // FIXME set m_viewmode |
| 1425 | if (m_viewMode >= 2) { |
| 1426 | m_optionsEditor->setVisible(show); |
| 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | void DocumentView::showFileB(bool show) |
| 1431 | { |
| 1432 | // FIXME set m_viewmode |
| 1433 | if (m_viewMode >= 2) { |
| 1434 | m_filebEditor->setVisible(show); |
| 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | void DocumentView::showOther(bool show) |
| 1439 | { |
| 1440 | // FIXME set m_viewmode |
| 1441 | if (m_viewMode >= 2) { |
| 1442 | m_otherEditor->setVisible(show); |
| 1443 | } |
| 1444 | } |
| 1445 | |
| 1446 | void DocumentView::showOtherCsd(bool show) |
| 1447 | { |
| 1448 | // FIXME set m_viewmode |
| 1449 | if (m_viewMode >= 2) { |
| 1450 | m_otherCsdEditor->setVisible(show); |
| 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | void DocumentView::showWidgetEdit(bool show) |
| 1455 | { |
| 1456 | // FIXME set m_viewmode |
| 1457 | if (m_viewMode >= 2) { |
| 1458 | m_widgetEditor->setVisible(show); |
| 1459 | } |
| 1460 | } |
| 1461 | |
| 1462 | void DocumentView::showAppEdit(bool show) |
| 1463 | { |
| 1464 | // FIXME set m_viewmode |
| 1465 | if (m_viewMode >= 2) { |
| 1466 | m_appEditor->setVisible(show); |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | void DocumentView::cut() |
| 1471 | { |
| 1472 | if (m_viewMode < 2) { |
| 1473 | m_mainEditor->cut(); |
| 1474 | } |
| 1475 | else { |
| 1476 | if (m_scoreEditor->hasFocus()) { |
| 1477 | m_scoreEditor->cut(); |
| 1478 | } |
| 1479 | else if (m_optionsEditor->hasFocus()) { |
| 1480 | m_optionsEditor->cut(); |
| 1481 | } |
| 1482 | else if (m_otherEditor->hasFocus() ) { |
| 1483 | m_otherEditor->cut(); |
| 1484 | } |
| 1485 | else if (m_otherCsdEditor->hasFocus()) { |
| 1486 | m_otherCsdEditor->cut(); |
| 1487 | } |
| 1488 | else if (m_widgetEditor->hasFocus()) { |
| 1489 | m_widgetEditor->cut(); |
| 1490 | } |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | void DocumentView::copy() |
| 1495 | { |
| 1496 | if (m_viewMode < 2) { |
| 1497 | m_mainEditor->copy(); |
| 1498 | } |
| 1499 | else { |
| 1500 | if (m_scoreEditor->hasFocus()) { |
| 1501 | m_scoreEditor->copy(); |
| 1502 | } |
| 1503 | else if (m_optionsEditor->hasFocus()) { |
| 1504 | m_optionsEditor->copy(); |
| 1505 | } |
| 1506 | else if (m_otherEditor->hasFocus() ) { |
| 1507 | m_otherEditor->copy(); |
| 1508 | } |
| 1509 | else if (m_otherCsdEditor->hasFocus()) { |
| 1510 | m_otherCsdEditor->copy(); |
| 1511 | } |
| 1512 | else if (m_widgetEditor->hasFocus()) { |
| 1513 | m_widgetEditor->copy(); |
| 1514 | } |
| 1515 | } |
| 1516 | } |
| 1517 | |
| 1518 | void DocumentView::paste() |
| 1519 | { |
| 1520 | if (m_viewMode < 2) { |
| 1521 | m_mainEditor->paste(); |
| 1522 | } |
| 1523 | else { |
| 1524 | if (m_scoreEditor->hasFocus()) { |
| 1525 | m_scoreEditor->paste(); |
| 1526 | } |
| 1527 | else if (m_optionsEditor->hasFocus()) { |
| 1528 | m_optionsEditor->paste(); |
| 1529 | } |
| 1530 | else if (m_otherEditor->hasFocus() ) { |
| 1531 | m_otherEditor->paste(); |
| 1532 | } |
| 1533 | else if (m_otherCsdEditor->hasFocus()) { |
| 1534 | m_otherCsdEditor->paste(); |
| 1535 | } |
| 1536 | else if (m_widgetEditor->hasFocus()) { |
| 1537 | m_widgetEditor->paste(); |
| 1538 | } |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | void DocumentView::undo() |
| 1543 | { |
| 1544 | if (m_viewMode < 2) { |
| 1545 | m_mainEditor->undo(); |
| 1546 | } |
| 1547 | else { |
| 1548 | if (m_scoreEditor->hasFocus()) { |
| 1549 | m_scoreEditor->undo(); |
| 1550 | } |
| 1551 | else if (m_optionsEditor->hasFocus()) { |
| 1552 | m_optionsEditor->undo(); |
| 1553 | } |
| 1554 | else if (m_otherEditor->hasFocus() ) { |
| 1555 | m_otherEditor->undo(); |
| 1556 | } |
| 1557 | else if (m_otherCsdEditor->hasFocus()) { |
| 1558 | m_otherCsdEditor->undo(); |
| 1559 | } |
| 1560 | else if (m_widgetEditor->hasFocus()) { |
| 1561 | m_widgetEditor->undo(); |
| 1562 | } |
| 1563 | } |
| 1564 | escapePressed(); |
| 1565 | } |
| 1566 | |
| 1567 | void DocumentView::redo() |
| 1568 | { |
| 1569 | if (m_viewMode < 2) { |
| 1570 | m_mainEditor->redo(); |
| 1571 | } |
| 1572 | else { |
| 1573 | if (m_scoreEditor->hasFocus()) { |
| 1574 | m_scoreEditor->redo(); |
| 1575 | } |
| 1576 | else if (m_optionsEditor->hasFocus()) { |
| 1577 | m_optionsEditor->redo(); |
| 1578 | } |
| 1579 | else if (m_otherEditor->hasFocus() ) { |
| 1580 | m_otherEditor->redo(); |
| 1581 | } |
| 1582 | else if (m_otherCsdEditor->hasFocus()) { |
| 1583 | m_otherCsdEditor->redo(); |
| 1584 | } |
| 1585 | else if (m_widgetEditor->hasFocus()) { |
| 1586 | m_widgetEditor->redo(); |
| 1587 | } |
| 1588 | } |
| 1589 | } |
| 1590 | |
| 1591 | void DocumentView::comment() |
| 1592 | { |
| 1593 | // TODO implment for multiple views |
| 1594 | // qDebug() << "DocumentView::comment()"; |
| 1595 | if (m_viewMode < 2) { |
| 1596 | internalChange = true; |
| 1597 | QString commentChar = ""; |
| 1598 | if (m_mode == EDIT_CSOUND_MODE || m_mode == EDIT_ORC_MODE |
| 1599 | || m_mode == EDIT_SCO_MODE || m_mode == EDIT_INC_MODE) { |
| 1600 | commentChar = ";"; |
| 1601 | } |
| 1602 | else if (m_mode == EDIT_PYTHON_MODE) { // Python Mode |
| 1603 | commentChar = "#"; |
| 1604 | } |
| 1605 | QTextCursor cursor = m_mainEditor->textCursor(); |
| 1606 | if (cursor.position() > cursor.anchor()) { |
| 1607 | int temp = cursor.anchor(); |
| 1608 | cursor.setPosition(cursor.position()); |
| 1609 | cursor.setPosition(temp, QTextCursor::KeepAnchor); |
| 1610 | } |
| 1611 | if (!cursor.atBlockStart()) { |
| 1612 | cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor); |
| 1613 | } |
| 1614 | int start = cursor.selectionStart(); |
| 1615 | QString text = cursor.selectedText(); |
| 1616 | if (text.startsWith(commentChar)) { |
| 1617 | uncomment(); |
| 1618 | return; |
| 1619 | } |
| 1620 | text.prepend(commentChar); |
| 1621 | text.replace(QChar(QChar::ParagraphSeparator), QString("\n" + commentChar)); |
| 1622 | if (text.endsWith("\n" + commentChar) ) { |
| 1623 | text.chop(1); |
| 1624 | } |
| 1625 | cursor.insertText(text); |
| 1626 | cursor.setPosition(start); |
| 1627 | cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, text.size()); |
| 1628 | m_mainEditor->setTextCursor(cursor); |
| 1629 | } |
| 1630 | else { |
| 1631 | qDebug() << "Not implemented for split view"; |
| 1632 | } |
| 1633 | } |
| 1634 | |
| 1635 | void DocumentView::uncomment() |
| 1636 | { |
| 1637 | // TODO implment for multiple views |
| 1638 | if (m_viewMode < 2) { |
| 1639 | internalChange = true; |
| 1640 | QString commentChar = ""; |
| 1641 | if (m_mode == EDIT_CSOUND_MODE || m_mode == EDIT_ORC_MODE |
| 1642 | || m_mode == EDIT_SCO_MODE || m_mode == EDIT_INC_MODE) { |
| 1643 | commentChar = ";"; |
| 1644 | } |
| 1645 | else if (m_mode == EDIT_PYTHON_MODE) { // Python Mode |
| 1646 | commentChar = "#"; |
| 1647 | } |
| 1648 | QTextCursor cursor = m_mainEditor->textCursor(); |
| 1649 | if (cursor.position() > cursor.anchor()) { |
| 1650 | int temp = cursor.anchor(); |
| 1651 | cursor.setPosition(cursor.position()); |
| 1652 | cursor.setPosition(temp, QTextCursor::KeepAnchor); |
| 1653 | } |
| 1654 | QString text = cursor.selectedText(); |
| 1655 | if (!cursor.atBlockStart() && !text.startsWith(commentChar)) { |
| 1656 | cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor); |
| 1657 | text = cursor.selectedText(); |
| 1658 | } |
| 1659 | if (text.startsWith(commentChar)) { |
| 1660 | text.remove(0,1); |
| 1661 | } |
| 1662 | int start = cursor.selectionStart(); |
| 1663 | text.replace(QChar(QChar::ParagraphSeparator), QString("\n")); |
| 1664 | text.replace(QString("\n" + commentChar), QString("\n")); //TODO make more robust |
| 1665 | cursor.insertText(text); |
| 1666 | cursor.setPosition(start); |
| 1667 | cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, text.size()); |
| 1668 | m_mainEditor->setTextCursor(cursor); |
| 1669 | } |
| 1670 | else { |
| 1671 | qDebug() << "Not implemented for split view"; |
| 1672 | } |
| 1673 | } |
| 1674 | |
| 1675 | void DocumentView::indent() |
| 1676 | { |
| 1677 | // TODO implment for multiple views |
| 1678 | if (m_viewMode < 2) { |
| 1679 | // qDebug("DocumentPage::indent"); |
| 1680 | internalChange = true; |
| 1681 | QString indentChar = ""; |
| 1682 | if (m_mode == EDIT_CSOUND_MODE || m_mode == EDIT_ORC_MODE |
| 1683 | || m_mode == EDIT_SCO_MODE || m_mode == EDIT_INC_MODE) { |
| 1684 | indentChar = "\t"; |
| 1685 | } |
| 1686 | else if (m_mode == EDIT_PYTHON_MODE) { // Python Mode |
| 1687 | indentChar = " "; |
| 1688 | } |
| 1689 | QTextCursor cursor = m_mainEditor->textCursor(); |
| 1690 | QTextCursor::MoveMode moveMode = cursor.selectedText().isEmpty() ? QTextCursor::MoveAnchor : QTextCursor::KeepAnchor; |
| 1691 | if (cursor.position() > cursor.anchor()) { |
| 1692 | int temp = cursor.anchor(); |
| 1693 | cursor.setPosition(cursor.position()); |
| 1694 | cursor.setPosition(temp, QTextCursor::KeepAnchor); |
| 1695 | } |
| 1696 | if (!cursor.atBlockStart()) { |
| 1697 | cursor.movePosition(QTextCursor::StartOfLine, moveMode); |
| 1698 | } |
| 1699 | int start = cursor.selectionStart(); |
| 1700 | QString text = cursor.selectedText(); |
| 1701 | text.prepend(indentChar); |
| 1702 | text.replace(QChar(QChar::ParagraphSeparator), "\n" + indentChar); |
| 1703 | if (text.endsWith("\n" + indentChar) ) { |
| 1704 | text.chop(1); |
| 1705 | } |
| 1706 | cursor.insertText(text); |
| 1707 | cursor.setPosition(start); |
| 1708 | cursor.movePosition(QTextCursor::NextCharacter, moveMode, text.size()); |
| 1709 | m_mainEditor->setTextCursor(cursor); |
| 1710 | } |
| 1711 | else { |
| 1712 | qDebug() << "Not implemented for split view"; |
| 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | void DocumentView::unindent() |
| 1717 | { |
| 1718 | // TODO implment for multiple views |
| 1719 | if (m_viewMode < 2) { |
| 1720 | internalChange = true; |
| 1721 | QString indentChar = ""; |
| 1722 | if (m_mode == EDIT_CSOUND_MODE || m_mode == EDIT_ORC_MODE |
| 1723 | || m_mode == EDIT_SCO_MODE || m_mode == EDIT_INC_MODE) { |
| 1724 | indentChar = "\t"; |
| 1725 | } |
| 1726 | else if (m_mode == EDIT_PYTHON_MODE) { // Python Mode |
| 1727 | indentChar = " "; |
| 1728 | } |
| 1729 | QTextCursor cursor = m_mainEditor->textCursor(); |
| 1730 | if (cursor.position() > cursor.anchor()) { |
| 1731 | int temp = cursor.anchor(); |
| 1732 | cursor.setPosition(cursor.position()); |
| 1733 | cursor.setPosition(temp, QTextCursor::KeepAnchor); |
| 1734 | } |
| 1735 | if (!cursor.atBlockStart()) { |
| 1736 | cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor); |
| 1737 | } |
| 1738 | int start = cursor.selectionStart(); |
| 1739 | QString text = cursor.selectedText(); |
| 1740 | while (indentChar == " " && text.startsWith("\t")) { |
| 1741 | text.remove(0,1); |
| 1742 | } |
| 1743 | if (text.startsWith(indentChar)) { |
| 1744 | text.remove(0,indentChar.size()); |
| 1745 | } |
| 1746 | text.replace(QChar(QChar::ParagraphSeparator), QString("\n")); |
| 1747 | text.replace("\n" + indentChar, QString("\n")); //TODO make more robust |
| 1748 | cursor.insertText(text); |
| 1749 | cursor.setPosition(start); |
| 1750 | cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, text.size()); |
| 1751 | m_mainEditor->setTextCursor(cursor); |
| 1752 | } |
| 1753 | else { |
| 1754 | qDebug() << "Not implemented for split view"; |
| 1755 | } |
| 1756 | } |
| 1757 | |
| 1758 | void DocumentView::killLine() |
| 1759 | { |
| 1760 | // TODO implment for multiple views |
| 1761 | if (m_viewMode < 2) { |
| 1762 | QTextCursor cursor = m_mainEditor->textCursor(); |
| 1763 | if (!cursor.atBlockStart()) { |
| 1764 | cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor); |
| 1765 | } |
| 1766 | cursor.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor); |
| 1767 | cursor.insertText(""); |
| 1768 | } |
| 1769 | else { |
| 1770 | qDebug() << "Not implemented for split view"; |
| 1771 | } |
| 1772 | } |
| 1773 | |
| 1774 | void DocumentView::killToEnd() |
| 1775 | { |
| 1776 | // TODO implment for multiple views |
| 1777 | // internalChange = true; |
| 1778 | if (m_viewMode < 2) { |
| 1779 | QTextCursor cursor = m_mainEditor->textCursor(); |
| 1780 | if (!cursor.atBlockEnd()) { |
| 1781 | cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor); |
| 1782 | } |
| 1783 | cursor.insertText(""); |
| 1784 | } |
| 1785 | else { |
| 1786 | qDebug() << "Not implemented for split view"; |
| 1787 | } |
| 1788 | } |
| 1789 | |
| 1790 | void DocumentView::markErrorLines(QList<QPair<int, QString> > lines) |
| 1791 | { |
| 1792 | // TODO implement for multiple views |
| 1793 | if (m_viewMode < 2) { |
| 1794 | auto fmt = m_highlighter.getFormat("default"); |
| 1795 | auto fg = fmt.foreground().color(); |
| 1796 | auto bg = fmt.background().color(); |
| 1797 | bool originallyMod = m_mainEditor->document()->isModified(); |
| 1798 | internalChange = true; |
| 1799 | QTextCharFormat errorFormat; |
| 1800 | // make error background depend on color theme |
| 1801 | QColor errbg = fg.lightness() < bg.lightness() ? bg.darker(200) : bg.lighter(200); |
| 1802 | errbg.setRed(errbg.blue() * 2.5); |
| 1803 | errorFormat.setBackground(QBrush(errbg)); |
| 1804 | QTextCursor cur = m_mainEditor->textCursor(); |
| 1805 | cur.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor); |
| 1806 | for(int i = 0; i < lines.size(); i++) { |
| 1807 | int line = lines[i].first; |
| 1808 | cur.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, line-1); |
| 1809 | cur.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor); |
| 1810 | cur.mergeCharFormat(errorFormat); |
| 1811 | internalChange = true; |
| 1812 | m_mainEditor->setTextCursor(cur); |
| 1813 | errorMarked = true; |
| 1814 | if (!originallyMod) { |
| 1815 | m_mainEditor->document()->setModified(false); |
| 1816 | } |
| 1817 | } |
| 1818 | } |
| 1819 | else { |
| 1820 | qDebug() << "Not implemented for split view"; |
| 1821 | } |
| 1822 | } |
| 1823 | |
| 1824 | void DocumentView::unmarkErrorLines() |
| 1825 | { |
| 1826 | // TODO implment for multiple views |
| 1827 | if (!errorMarked) |
| 1828 | return; |
| 1829 | // qDebug("DocumentPage::unmarkErrorLines()"); |
| 1830 | if (m_viewMode < 2) { |
| 1831 | int position = m_mainEditor->verticalScrollBar()->value(); |
| 1832 | QTextCursor currentCursor = m_mainEditor->textCursor(); |
| 1833 | errorMarked = false; |
| 1834 | m_mainEditor->selectAll(); |
| 1835 | internalChange = true; |
| 1836 | QTextCursor cur = m_mainEditor->textCursor(); |
| 1837 | QTextCharFormat format = cur.blockCharFormat(); |
| 1838 | format.clearBackground(); |
| 1839 | cur.setCharFormat(format); |
| 1840 | internalChange = true; |
| 1841 | m_mainEditor->setTextCursor(cur); //sets format |
| 1842 | internalChange = true; |
| 1843 | m_mainEditor->setTextCursor(currentCursor); //returns cursor to initial position |
| 1844 | m_mainEditor->verticalScrollBar()->setValue(position); //return document display to initial position |
| 1845 | } |
| 1846 | else { |
| 1847 | qDebug() << "Not implemented for split view"; |
| 1848 | } |
| 1849 | } |
| 1850 | |
| 1851 | |
| 1852 | void editorJumpToLine(TextEditor *editor, int line) { |
| 1853 | int maxlines = editor->document()->blockCount(); |
| 1854 | if(line > maxlines) |
| 1855 | line = maxlines; |
| 1856 | QTextCursor cursor(editor->document()->findBlockByLineNumber(line-1)); |
| 1857 | editor->moveCursor(QTextCursor::End); |
| 1858 | editor->setTextCursor(cursor); |
| 1859 | } |
| 1860 | |
| 1861 | void DocumentView::jumpToLine(int line, bool mark) |
| 1862 | { |
| 1863 | if(mark) |
| 1864 | markCurrentPosition(); |
| 1865 | // TODO implment for multiple views |
| 1866 | if (m_viewMode < 2) { |
| 1867 | editorJumpToLine(m_mainEditor, line); |
| 1868 | } |
| 1869 | else { |
| 1870 | editorJumpToLine(m_orcEditor, line); |
| 1871 | } |
| 1872 | } |
| 1873 | |
| 1874 | void DocumentView::goBackToPreviousPosition() { |
| 1875 | if(cursorPositions.isEmpty()) |
| 1876 | return; |
| 1877 | int lastPos = cursorPositions.takeLast(); |
| 1878 | this->jumpToLine(lastPos, false); |
| 1879 | } |
| 1880 | |
| 1881 | void DocumentView::gotoNextLine() |
| 1882 | { |
| 1883 | m_mainEditor->moveCursor(QTextCursor::Down); |
| 1884 | } |
| 1885 | |
| 1886 | void DocumentView::opcodeFromMenu() |
| 1887 | { |
| 1888 | QAction *action = (QAction *) QObject::sender(); |
| 1889 | if (m_viewMode < 2) { |
| 1890 | QTextCursor cursor = m_mainEditor->textCursor(); |
| 1891 | QString text = action->data().toString(); |
| 1892 | cursor.insertText(text); |
| 1893 | } |
| 1894 | else { |
| 1895 | qDebug() << "Not implemented for split view"; |
| 1896 | } |
| 1897 | } |
| 1898 | |
| 1899 | void DocumentView::insertChn_k(QString channel) |
| 1900 | { |
| 1901 | QTextCursor cursor; |
| 1902 | cursor = m_mainEditor->textCursor(); |
| 1903 | m_mainEditor->moveCursor(QTextCursor::Start); |
| 1904 | if (m_mainEditor->find(";;channels")) { |
| 1905 | m_mainEditor->moveCursor(QTextCursor::NextBlock); |
| 1906 | } else if (m_mainEditor->find("0dbfs")) { // if no ;;channels try to put under 0dbfs line in options |
| 1907 | m_mainEditor->moveCursor(QTextCursor::NextBlock); |
| 1908 | m_mainEditor->insertPlainText("\n;;channels\n"); |
| 1909 | } else if (m_mainEditor->find("instr ")) { // or before last instrument |
| 1910 | m_mainEditor->moveCursor(QTextCursor::PreviousBlock); |
| 1911 | m_mainEditor->insertPlainText("\n"); |
| 1912 | m_mainEditor->moveCursor(QTextCursor::PreviousBlock); |
| 1913 | m_mainEditor->insertPlainText("\n;;channels\n"); |
| 1914 | } else { // or ask if current cursor position is OK |
| 1915 | int response = QMessageBox::question(this, tr("Where to insert?"),tr("Could find section ;;channels\nIs it OK to insert ;;channels and chn_k declaration before in the current position?")); |
| 1916 | if (response==QMessageBox::Yes) { |
| 1917 | m_mainEditor->setTextCursor(cursor); |
| 1918 | m_mainEditor->moveCursor(QTextCursor::StartOfLine); |
| 1919 | m_mainEditor->insertPlainText("\n;;channels\n"); |
| 1920 | } else { |
| 1921 | m_mainEditor->setTextCursor(cursor); |
| 1922 | return; |
| 1923 | } |
| 1924 | } |
| 1925 | |
| 1926 | if (getBasicText().contains("chn_k \""+channel)) { |
| 1927 | QMessageBox::information(this, tr("chn_kdeclaration"),tr("This channel is already declared.")); |
| 1928 | } else { |
| 1929 | m_mainEditor->insertPlainText(QString("chn_k \"%1\",3\n").arg(channel)); |
| 1930 | } |
| 1931 | |
| 1932 | m_mainEditor->setTextCursor(cursor); |
| 1933 | } |
| 1934 | |
| 1935 | void DocumentView::contextMenuEvent(QContextMenuEvent *event) |
| 1936 | { |
| 1937 | qDebug() << "DocumentView::contextMenuEvent"; |
| 1938 | createContextMenu(event->globalPos()); |
| 1939 | } |
| 1940 | |
| 1941 | QString DocumentView::changeToChnget(QString text) |
| 1942 | { |
| 1943 | QStringList lines = text.split("\n"); |
| 1944 | QString newText = ""; |
| 1945 | foreach (QString line, lines) { |
| 1946 | if (line.contains("invalue")) { |
| 1947 | line.replace("invalue", "chnget"); |
| 1948 | } |
| 1949 | else if (line.contains("outvalue")) { |
| 1950 | line.replace("outvalue", "chnset"); |
| 1951 | int arg1Index = line.indexOf("chnset") + 7; |
| 1952 | int arg2Index = line.indexOf(",") + 1; |
| 1953 | int arg2EndIndex = line.indexOf(QRegExp("[\\s]*[;]"), arg2Index); |
| 1954 | QString arg1 = line.mid(arg1Index, arg2Index-arg1Index - 1).trimmed(); |
| 1955 | QString arg2 = line.mid(arg2Index, arg2EndIndex-arg2Index).trimmed(); |
| 1956 | QString comment = line.mid(arg2EndIndex); |
| 1957 | qDebug() << arg1 << arg2 << arg2EndIndex; |
| 1958 | line = line.mid(0, arg1Index) + " " + arg2 + ", " + arg1; |
| 1959 | if (arg2EndIndex > 0) |
| 1960 | line += " " + comment; |
| 1961 | } |
| 1962 | newText += line + "\n"; |
| 1963 | } |
| 1964 | return newText; |
| 1965 | } |
| 1966 | |
| 1967 | QString DocumentView::changeToInvalue(QString text) |
| 1968 | { |
| 1969 | QStringList lines = text.split("\n"); |
| 1970 | QString newText = ""; |
| 1971 | foreach (QString line, lines) { |
| 1972 | if (line.contains("chnget")) { |
| 1973 | line.replace("chnget", "invalue"); |
| 1974 | } |
| 1975 | else if (line.contains("chnset")) { |
| 1976 | line.replace("chnset", "outvalue"); |
| 1977 | int arg1Index = line.indexOf("outvalue") + 8; |
| 1978 | int arg2Index = line.indexOf(",") + 1; |
| 1979 | int arg2EndIndex = line.indexOf(QRegExp("[\\s]*[;]"), arg2Index); |
| 1980 | QString arg1 = line.mid(arg1Index, arg2Index-arg1Index - 1).trimmed(); |
| 1981 | QString arg2 = line.mid(arg2Index, arg2EndIndex-arg2Index).trimmed(); |
| 1982 | QString comment = line.mid(arg2EndIndex); |
| 1983 | qDebug() << arg1 << arg2 << arg2EndIndex; |
| 1984 | line = line.mid(0, arg1Index) + " " + arg2 + ", " + arg1; |
| 1985 | if (arg2EndIndex > 0) |
| 1986 | line += " " + comment; |
| 1987 | } |
| 1988 | newText += line + "\n"; |
| 1989 | } |
| 1990 | return newText; |
| 1991 | } |
| 1992 | |
| 1993 | void DocumentView::destroySyntaxMenu() |
| 1994 | { |
| 1995 | syntaxMenu->clear(); |
| 1996 | syntaxMenu->hide(); |
| 1997 | // syntaxMenu = 0; |
| 1998 | } |
| 1999 | |
| 2000 | void DocumentView::opcodeHelp() |
| 2001 | { |
| 2002 | emit setHelp(); |
| 2003 | } |
| 2004 | |
| 2005 | void DocumentView::restoreCursorPosition() |
| 2006 | { |
| 2007 | // check if cursor must be moved to old position (after evaluateSection) |
| 2008 | if (m_oldCursorPosition>=0) { |
| 2009 | qDebug()<<"Restoring cursor position to "<<m_oldCursorPosition; |
| 2010 | QTextCursor cursor = m_mainEditor->textCursor(); |
| 2011 | cursor.setPosition(m_oldCursorPosition); // TODO: check also for pyuthonEditor |
| 2012 | m_mainEditor->setTextCursor(cursor); |
| 2013 | m_oldCursorPosition = -1; |
| 2014 | } |
| 2015 | } |
| 2016 | |
| 2017 | void DocumentView::setParsedUDOs(QStringList udos) { |
| 2018 | m_highlighter.setUDOs(udos); |
| 2019 | } |
| 2020 | |
| 2021 | MySyntaxMenu::MySyntaxMenu(QWidget * parent) : |
| 2022 | QMenu(parent) |
| 2023 | |
| 2024 | { |
| 2025 | this->setStyleSheet( |
| 2026 | "QMenu {" |
| 2027 | " background-color: #f8f8f8; " |
| 2028 | " color: #5050A0 !important; " |
| 2029 | " border-radius: 4px; " |
| 2030 | " menu-scrollable: 1; " |
| 2031 | " padding: 4px;" |
| 2032 | "} " |
| 2033 | "QMenu::item { " |
| 2034 | " font-family: monospace; " |
| 2035 | " font-size: 12px; " |
| 2036 | " padding-left: 4px;" |
| 2037 | " padding-top: 0px; " |
| 2038 | " padding-bottom: 0px;" |
| 2039 | " padding-right: 4px;" |
| 2040 | "}" |
| 2041 | "QMenu::item:selected { " |
| 2042 | " color: #f8f8f8; " |
| 2043 | " background-color: #3daee9; " |
| 2044 | "}" |
| 2045 | "QMenu::separator {" |
| 2046 | " background: 7070c0;" |
| 2047 | "}"); |
| 2048 | |
| 2049 | } |
| 2050 | |
| 2051 | MySyntaxMenu::~MySyntaxMenu() |
| 2052 | { |
| 2053 | |
| 2054 | } |
| 2055 | |
| 2056 | void MySyntaxMenu::keyPressEvent(QKeyEvent * event) |
| 2057 | { |
| 2058 | if (event->key() == Qt::Key_Escape) { |
| 2059 | this->hide(); |
| 2060 | } |
| 2061 | else if (event->key() == Qt::Key_Tab) { |
| 2062 | QAction * a = activeAction(); |
| 2063 | insertComplete = false; |
| 2064 | this->close(); |
| 2065 | if (a != 0) { |
| 2066 | a->trigger(); |
| 2067 | return; |
| 2068 | } |
| 2069 | else { |
| 2070 | Q_ASSERT(defaultAction() != NULL); |
| 2071 | defaultAction()->trigger(); |
| 2072 | return; |
| 2073 | } |
| 2074 | } else if (event->key() == Qt::Key_Return) { |
| 2075 | QAction * a = activeAction(); |
| 2076 | insertComplete = true; |
| 2077 | this->close(); |
| 2078 | if (a != 0) { |
| 2079 | a->trigger(); |
| 2080 | return; |
| 2081 | } |
| 2082 | else { |
| 2083 | Q_ASSERT(defaultAction() != NULL); |
| 2084 | defaultAction()->trigger(); |
| 2085 | return; |
| 2086 | } |
| 2087 | } else if (event->key() == Qt::Key_Up || event->key() == Qt::Key_Down) { |
| 2088 | QMenu::keyPressEvent(event); |
| 2089 | insertComplete = false; |
| 2090 | return; |
| 2091 | } else { |
| 2092 | this->close(); |
| 2093 | if (event->key() != Qt::Key_Backspace) { |
| 2094 | emit keyPressed(event->text()); |
| 2095 | } |
| 2096 | else { |
| 2097 | QObject *par = parent(); |
| 2098 | if (par) |
| 2099 | par->event(event); |
| 2100 | } |
| 2101 | } |
| 2102 | // insertComplete = false; |
| 2103 | // QMenu::keyPressEvent(event); |
| 2104 | } |
| 2105 | |
| 2106 | HoverWidget::HoverWidget(QWidget *parent) : |
| 2107 | QWidget(parent) |
| 2108 | { |
| 2109 | |
| 2110 | } |
| 2111 | |
| 2112 | void HoverWidget::mousePressEvent(QMouseEvent *ev) |
| 2113 | { |
| 2114 | (void) ev; |
| 2115 | this->hide(); |
| 2116 | } |
| 2117 |