| 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 | |
| 24 | #include <QtWidgets> |
| 25 | |
| 26 | #include "texteditor.h" |
| 27 | |
| 28 | |
| 29 | TextEditor::TextEditor(QWidget *parent) : |
| 30 | QTextEdit(parent) |
| 31 | { |
| 32 | setAcceptDrops(true); |
| 33 | setAcceptRichText(false); |
| 34 | m_parameterMode = false; |
| 35 | m_commaTyped = false; |
| 36 | // qDebug() << "TextEditor::TextEditor" << acceptDrops(); |
| 37 | } |
| 38 | |
| 39 | void TextEditor::keyPressEvent (QKeyEvent * event) |
| 40 | { |
| 41 | m_lastkey = event->key(); |
| 42 | if (m_commaTyped && event->key() != Qt::Key_Space) { |
| 43 | m_commaTyped = false; |
| 44 | } |
| 45 | switch(event->key()) { |
| 46 | case Qt::Key_Tab: |
| 47 | if (m_parameterMode) { |
| 48 | emit tabPressed(); |
| 49 | } else if(m_tabIndents) { |
| 50 | // this blocks default behaviour and only tab does not move any further |
| 51 | emit requestIndent(); |
| 52 | } else { |
| 53 | QTextEdit::keyPressEvent(event); |
| 54 | } |
| 55 | return; |
| 56 | case Qt::Key_Backtab: |
| 57 | if (m_parameterMode) { |
| 58 | emit backtabPressed(); |
| 59 | } else if(m_tabIndents) { |
| 60 | emit requestUnindent(); |
| 61 | } |
| 62 | return; |
| 63 | case Qt::Key_Return: |
| 64 | case Qt::Key_Enter: |
| 65 | emit enterPressed(); |
| 66 | break; |
| 67 | case Qt::Key_Space: |
| 68 | if (m_commaTyped) { |
| 69 | emit showParameterInfo(); |
| 70 | } |
| 71 | m_commaTyped = false; |
| 72 | break; |
| 73 | case Qt::Key_Comma: |
| 74 | m_commaTyped = true; |
| 75 | break; |
| 76 | } |
| 77 | QTextEdit::keyPressEvent(event); // Process key events in the rest of the application |
| 78 | if (event->key() == Qt::Key_Up || event->key() == Qt::Key_Down |
| 79 | || event->key() == Qt::Key_Left || event->key() == Qt::Key_Right) { |
| 80 | emit arrowPressed(); |
| 81 | } else if (event->key() == Qt::Key_Escape) { |
| 82 | emit escapePressed(); |
| 83 | } else if(event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) { |
| 84 | emit newLine(); |
| 85 | } |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | void TextEditor::mouseReleaseEvent(QMouseEvent *e) |
| 90 | { |
| 91 | QTextEdit::mouseReleaseEvent(e); // and do all the necessary qt job |
| 92 | if (m_parameterMode) { |
| 93 | emit requestParameterModeExit(); |
| 94 | } |
| 95 | |
| 96 | } |
| 97 | |
| 98 | //The following makes the editor almost accept drop events on OS X, but breaks all dragging on the same document on linux |
| 99 | //void TextEditor::dropEvent(QDropEvent *event) |
| 100 | //{ |
| 101 | // qDebug() << "TextEditor::dropEvent" << event->format(); |
| 102 | //// QString fileName = QString(event->encodedData("text/uri-list")).remove("file://"); |
| 103 | //// event->; |
| 104 | // |
| 105 | // event->acceptProposedAction(); |
| 106 | //} |
| 107 | // |
| 108 | //void TextEditor::dragEnterEvent(QDragEnterEvent *event) |
| 109 | //{ |
| 110 | //// qDebug() << "TextEditor::dragEnterEvent" << event->format(); |
| 111 | // // TODO remove file:// text from text |
| 112 | // event->acceptProposedAction(); |
| 113 | //} |
| 114 | // |
| 115 | //void TextEditor::dragMoveEvent(QDragMoveEvent *event) |
| 116 | //{ |
| 117 | //// qDebug() << "TextEditor::dragMoveEvent" << event->mimeData()->text(); |
| 118 | // event->acceptProposedAction(); |
| 119 | //} |
| 120 | |
| 121 | TextEditLineNumbers::TextEditLineNumbers(QWidget *parent) |
| 122 | : TextEditor(parent) |
| 123 | { |
| 124 | setLineAreaVisible(false); |
| 125 | lineNumberArea = new LineNumberArea(this); |
| 126 | connect(this->document(),SIGNAL(blockCountChanged(int)),this,SLOT(updateLineArea(int))); |
| 127 | connect(this->verticalScrollBar(),SIGNAL(valueChanged(int)),this,SLOT(updateLineArea(int))); |
| 128 | connect(this,SIGNAL(cursorPositionChanged()),this,SLOT(updateLineArea())); |
| 129 | lineNumberArea->setLineNumberSizeScaling(0.8); |
| 130 | m_editorPadding = 6; |
| 131 | this->setStyleSheet("QTextEdit { border: none !important; }"); |
| 132 | |
| 133 | } |
| 134 | |
| 135 | int TextEditLineNumbers::getAreaWidth() |
| 136 | { |
| 137 | qreal scaling = lineNumberArea->lineNumberSizeScaling(); |
| 138 | int right_padding = lineNumberArea->padding(); |
| 139 | int left_padding = lineNumberArea->padding(); |
| 140 | int numDigits = 4; |
| 141 | int oneDigit = this->fontMetrics().width(QLatin1Char('9')); |
| 142 | return oneDigit * scaling * numDigits + left_padding + right_padding; |
| 143 | } |
| 144 | |
| 145 | void TextEditLineNumbers::resizeEvent(QResizeEvent *e) |
| 146 | { |
| 147 | QTextEdit::resizeEvent(e); |
| 148 | if (m_lineAreaVisble) { |
| 149 | setViewportMargins(getAreaWidth()+m_editorPadding, 0, 0, 0); // since font might have changed |
| 150 | QRect cr = contentsRect(); |
| 151 | lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), getAreaWidth(), cr.height())); |
| 152 | } else |
| 153 | setViewportMargins(m_editorPadding, 0, 0, 0); // since font might have changed |
| 154 | |
| 155 | } |
| 156 | |
| 157 | void TextEditLineNumbers::setLineAreaVisible(bool visible) |
| 158 | { |
| 159 | m_lineAreaVisble = visible; |
| 160 | if (m_lineAreaVisble) { |
| 161 | setViewportMargins(getAreaWidth() + m_editorPadding, 0, 0, 0); |
| 162 | } |
| 163 | else { |
| 164 | setViewportMargins(m_editorPadding, 0, 0, 0); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | void TextEditLineNumbers::markDebugLine(int line) |
| 169 | { |
| 170 | m_debugLines.append(line); |
| 171 | lineNumberArea->setDebugLines(m_debugLines); |
| 172 | } |
| 173 | |
| 174 | void TextEditLineNumbers::unmarkDebugLine(int line) |
| 175 | { |
| 176 | int index = m_debugLines.indexOf(line); |
| 177 | if (index >= 0) { |
| 178 | m_debugLines.remove(index); |
| 179 | } else { |
| 180 | qDebug() << "TextEditLineNumbers::unmarkDebugLine no marker at line " << line; |
| 181 | } |
| 182 | lineNumberArea->setDebugLines(m_debugLines); |
| 183 | updateLineArea(); |
| 184 | } |
| 185 | |
| 186 | void TextEditLineNumbers::setCurrentDebugLine(int line) |
| 187 | { |
| 188 | lineNumberArea->setCurrentDebugLine(line); |
| 189 | updateLineArea(); |
| 190 | } |
| 191 | |
| 192 | void TextEditLineNumbers::updateLineArea(int) |
| 193 | { |
| 194 | lineNumberArea->update(); |
| 195 | } |
| 196 | |
| 197 | void TextEditLineNumbers::updateLineArea() |
| 198 | { |
| 199 | lineNumberArea->update(); |
| 200 | } |
| 201 | |
| 202 | void TextEditLineNumbers::setLineAreaColors(QColor foreground, QColor background) |
| 203 | { |
| 204 | lineNumberArea->setColors(foreground, background); |
| 205 | } |
| 206 | |
| 207 | void LineNumberArea::paintEvent(QPaintEvent *) |
| 208 | { |
| 209 | if ( !codeEditor->lineAreaVisble() ) |
| 210 | return; |
| 211 | QPainter painter(this); |
| 212 | painter.fillRect(rect(), m_background); |
| 213 | //code based partly on: http://john.nachtimwald.com/2009/08/15/qtextedit-with-line-numbers/ |
| 214 | QTextBlock block = codeEditor->document()->begin(); |
| 215 | int contents_y = codeEditor->verticalScrollBar()->value(); |
| 216 | int page_bottom = contents_y + codeEditor->viewport()->height(); |
| 217 | QTextBlock current_block = codeEditor->document()->findBlock(codeEditor->textCursor().position()); |
| 218 | |
| 219 | int line_count = 0; |
| 220 | painter.setPen(m_foreground); // not exactly black |
| 221 | |
| 222 | bool bold; |
| 223 | QFont font = painter.font(); |
| 224 | font.setPointSizeF(font.pointSizeF() * m_lineNumbersSizeFactor); |
| 225 | painter.setFont(font); |
| 226 | auto width = codeEditor->getAreaWidth(); |
| 227 | auto metrics = codeEditor->fontMetrics(); |
| 228 | |
| 229 | while (block.isValid()) { |
| 230 | line_count += 1; |
| 231 | QString number = QString::number(line_count); |
| 232 | QPointF position = codeEditor->document()->documentLayout()->blockBoundingRect(block).topLeft(); |
| 233 | if (position.y() > page_bottom) |
| 234 | break; |
| 235 | int y = round(position.y()) - contents_y + metrics.ascent(); |
| 236 | if (y>=0) { // the line is visible |
| 237 | if (block == current_block) { |
| 238 | bold = true; |
| 239 | font.setBold(true); |
| 240 | painter.setFont(font); |
| 241 | } |
| 242 | int x = width - metrics.width(number)*m_lineNumbersSizeFactor - m_padding; |
| 243 | painter.drawText(x, y, number); |
| 244 | if (bold) { |
| 245 | font.setBold(false); |
| 246 | painter.setFont(font); |
| 247 | } |
| 248 | if (m_debugLines.indexOf(line_count) >= 0) { |
| 249 | painter.setPen(QColor(Qt::red)); |
| 250 | painter.setBrush(QColor(Qt::red)); |
| 251 | int height = codeEditor->fontMetrics().ascent() ; |
| 252 | painter.drawEllipse(position.x(), y - height, height, height); |
| 253 | painter.setBrush(QBrush()); |
| 254 | painter.setPen(QColor(Qt::darkGray).darker()); // not exactly black |
| 255 | } |
| 256 | if (m_currentDebugLine >= 0 && line_count == m_currentDebugLine) { |
| 257 | QImage image("://themes/boring/gtk-media-play-ltr.png"); |
| 258 | painter.drawImage(position.x(), y, image); |
| 259 | } |
| 260 | } |
| 261 | block = block.next(); |
| 262 | } |
| 263 | } |