|
| 1 | +#include "ExtendedScintilla.h" |
| 2 | +#include "FindReplaceDialog.h" |
| 3 | +#include "Settings.h" |
| 4 | +#include "Qsci/qscilexer.h" |
| 5 | + |
| 6 | +#include <QFile> |
| 7 | +#include <QDropEvent> |
| 8 | +#include <QUrl> |
| 9 | +#include <QMimeData> |
| 10 | +#include <QShortcut> |
| 11 | +#include <QAction> |
| 12 | +#include <QMenu> |
| 13 | +#include <cmath> |
| 14 | + |
| 15 | + |
| 16 | +ExtendedScintilla::ExtendedScintilla(QWidget* parent) : |
| 17 | + QsciScintilla(parent), |
| 18 | + findReplaceDialog(new FindReplaceDialog(this)) |
| 19 | +{ |
| 20 | + // This class does not set any lexer, that must be done in the child classes. |
| 21 | + |
| 22 | + // Enable UTF8 |
| 23 | + setUtf8(true); |
| 24 | + |
| 25 | + // Enable brace matching |
| 26 | + setBraceMatching(QsciScintilla::SloppyBraceMatch); |
| 27 | + |
| 28 | + // Enable auto indentation |
| 29 | + setAutoIndent(true); |
| 30 | + |
| 31 | + // Enable folding |
| 32 | + setFolding(QsciScintilla::BoxedTreeFoldStyle); |
| 33 | + |
| 34 | + // Create error indicator |
| 35 | + errorIndicatorNumber = indicatorDefine(QsciScintilla::SquiggleIndicator); |
| 36 | + setIndicatorForegroundColor(Qt::red, errorIndicatorNumber); |
| 37 | + |
| 38 | + // Set a sensible scroll width, so the scroll bar is avoided in |
| 39 | + // most cases. |
| 40 | + setScrollWidth(80); |
| 41 | + |
| 42 | + // Scroll width is adjusted to ensure that all of the lines |
| 43 | + // currently displayed can be completely scrolled. This mode never |
| 44 | + // adjusts the scroll width to be narrower. |
| 45 | + setScrollWidthTracking(true); |
| 46 | + |
| 47 | + // Connect signals |
| 48 | + connect(this, SIGNAL(linesChanged()), this, SLOT(updateLineNumberAreaWidth())); |
| 49 | + |
| 50 | + // The shortcut is constrained to the Widget context so it does not conflict with other SqlTextEdit widgets in the Main Window. |
| 51 | + QShortcut* shortcutFindReplace = new QShortcut(QKeySequence(tr("Ctrl+H")), this, nullptr, nullptr, Qt::WidgetShortcut); |
| 52 | + connect(shortcutFindReplace, SIGNAL(activated()), this, SLOT(openFindReplaceDialog())); |
| 53 | + |
| 54 | + // Prepare for adding the find/replace option to the QScintilla context menu |
| 55 | + setContextMenuPolicy(Qt::CustomContextMenu); |
| 56 | + connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showContextMenu(const QPoint &))); |
| 57 | +} |
| 58 | + |
| 59 | +ExtendedScintilla::~ExtendedScintilla() |
| 60 | +{ |
| 61 | +} |
| 62 | + |
| 63 | +void ExtendedScintilla::updateLineNumberAreaWidth() |
| 64 | +{ |
| 65 | + // Calculate number of digits of the current number of lines |
| 66 | + int digits = std::floor(std::log10(lines())) + 1; |
| 67 | + |
| 68 | + // Calculate the width of this number if it was all zeros (this is because a 1 might require less space than a 0 and this could |
| 69 | + // cause some flickering depending on the font) and set the new margin width. |
| 70 | + QFont font = lexer()->defaultFont(); |
| 71 | + setMarginWidth(0, QFontMetrics(font).width(QString("0").repeated(digits)) + 5); |
| 72 | +} |
| 73 | + |
| 74 | +void ExtendedScintilla::dropEvent(QDropEvent* e) |
| 75 | +{ |
| 76 | + QList<QUrl> urls = e->mimeData()->urls(); |
| 77 | + if(urls.isEmpty()) |
| 78 | + return QsciScintilla::dropEvent(e); |
| 79 | + |
| 80 | + QString file = urls.first().toLocalFile(); |
| 81 | + if(!QFile::exists(file)) |
| 82 | + return; |
| 83 | + |
| 84 | + QFile f(file); |
| 85 | + f.open(QIODevice::ReadOnly); |
| 86 | + setText(f.readAll()); |
| 87 | + f.close(); |
| 88 | +} |
| 89 | + |
| 90 | +void ExtendedScintilla::setupSyntaxHighlightingFormat(const QString& settings_name, int style) |
| 91 | +{ |
| 92 | + lexer()->setColor(QColor(Settings::getValue("syntaxhighlighter", settings_name + "_colour").toString()), style); |
| 93 | + |
| 94 | + QFont font(Settings::getValue("editor", "font").toString()); |
| 95 | + font.setPointSize(Settings::getValue("editor", "fontsize").toInt()); |
| 96 | + font.setBold(Settings::getValue("syntaxhighlighter", settings_name + "_bold").toBool()); |
| 97 | + font.setItalic(Settings::getValue("syntaxhighlighter", settings_name + "_italic").toBool()); |
| 98 | + font.setUnderline(Settings::getValue("syntaxhighlighter", settings_name + "_underline").toBool()); |
| 99 | + lexer()->setFont(font, style); |
| 100 | +} |
| 101 | + |
| 102 | +void ExtendedScintilla::reloadKeywords() |
| 103 | +{ |
| 104 | + // Set lexer again to reload the updated keywords list |
| 105 | + setLexer(lexer()); |
| 106 | +} |
| 107 | + |
| 108 | +void ExtendedScintilla::reloadSettings() |
| 109 | +{ |
| 110 | + // Enable auto completion if it hasn't been disabled |
| 111 | + if(Settings::getValue("editor", "auto_completion").toBool()) |
| 112 | + { |
| 113 | + setAutoCompletionThreshold(3); |
| 114 | + setAutoCompletionCaseSensitivity(true); |
| 115 | + setAutoCompletionShowSingle(true); |
| 116 | + setAutoCompletionSource(QsciScintilla::AcsAPIs); |
| 117 | + } else { |
| 118 | + setAutoCompletionThreshold(0); |
| 119 | + } |
| 120 | + |
| 121 | + // Set syntax highlighting settings |
| 122 | + QFont defaultfont(Settings::getValue("editor", "font").toString()); |
| 123 | + defaultfont.setStyleHint(QFont::TypeWriter); |
| 124 | + defaultfont.setPointSize(Settings::getValue("editor", "fontsize").toInt()); |
| 125 | + lexer()->setDefaultColor(Qt::black); |
| 126 | + lexer()->setFont(defaultfont); |
| 127 | + |
| 128 | + // Set font |
| 129 | + QFont font(Settings::getValue("editor", "font").toString()); |
| 130 | + font.setStyleHint(QFont::TypeWriter); |
| 131 | + font.setPointSize(Settings::getValue("editor", "fontsize").toInt()); |
| 132 | + setFont(font); |
| 133 | + |
| 134 | + // Show line numbers |
| 135 | + QFont marginsfont(QFont(Settings::getValue("editor", "font").toString())); |
| 136 | + marginsfont.setPointSize(font.pointSize()); |
| 137 | + setMarginsFont(marginsfont); |
| 138 | + setMarginLineNumbers(0, true); |
| 139 | + setMarginsBackgroundColor(Qt::lightGray); |
| 140 | + updateLineNumberAreaWidth(); |
| 141 | + |
| 142 | + // Highlight current line |
| 143 | + setCaretLineVisible(true); |
| 144 | + setCaretLineBackgroundColor(QColor(Settings::getValue("syntaxhighlighter", "currentline_colour").toString())); |
| 145 | + |
| 146 | + // Set tab width |
| 147 | + setTabWidth(Settings::getValue("editor", "tabsize").toInt()); |
| 148 | + lexer()->refreshProperties(); |
| 149 | + |
| 150 | + // Check if error indicators are enabled and clear them if they just got disabled |
| 151 | + showErrorIndicators = Settings::getValue("editor", "error_indicators").toBool(); |
| 152 | + if(!showErrorIndicators) |
| 153 | + clearErrorIndicators(); |
| 154 | + |
| 155 | +} |
| 156 | + |
| 157 | +void ExtendedScintilla::clearErrorIndicators() |
| 158 | +{ |
| 159 | + // Clear any error indicators from position (0,0) to the last column of the last line |
| 160 | + clearIndicatorRange(0, 0, lines(), lineLength(lines()), errorIndicatorNumber); |
| 161 | +} |
| 162 | + |
| 163 | +void ExtendedScintilla::setErrorIndicator(int fromRow, int fromIndex, int toRow, int toIndex) |
| 164 | +{ |
| 165 | + // Set error indicator for the specified range but only if they're enabled |
| 166 | + if(showErrorIndicators) |
| 167 | + fillIndicatorRange(fromRow, fromIndex, toRow, toIndex, errorIndicatorNumber); |
| 168 | +} |
| 169 | + |
| 170 | +void ExtendedScintilla::setErrorIndicator(int position) |
| 171 | +{ |
| 172 | + // Set error indicator for the position until end of line, but only if they're enabled |
| 173 | + if(showErrorIndicators) { |
| 174 | + |
| 175 | + int column = SendScintilla(QsciScintillaBase::SCI_GETCOLUMN, position); |
| 176 | + int line = SendScintilla(QsciScintillaBase::SCI_LINEFROMPOSITION, position); |
| 177 | + |
| 178 | + fillIndicatorRange(line, column, line+1, 0, errorIndicatorNumber); |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +bool ExtendedScintilla::findText(QString text, bool regexp, bool caseSensitive, bool words, bool wrap, bool forward) { |
| 183 | + |
| 184 | + // For finding the previous occurrence, we need to skip the current |
| 185 | + // selection, otherwise we'd always found the same occurrence. |
| 186 | + if (!forward && hasSelectedText()) { |
| 187 | + int lineFrom, indexFrom; |
| 188 | + int lineTo, indexTo; |
| 189 | + getSelection(&lineFrom, &indexFrom, &lineTo, &indexTo); |
| 190 | + setCursorPosition(lineFrom, indexFrom); |
| 191 | + } |
| 192 | + |
| 193 | + return findFirst(text, regexp, caseSensitive, words, wrap, forward); |
| 194 | +} |
| 195 | + |
| 196 | +void ExtendedScintilla::clearSelection() |
| 197 | +{ |
| 198 | + setSelection(-1,-1,-1,-1); |
| 199 | +} |
| 200 | + |
| 201 | +void ExtendedScintilla::openFindReplaceDialog() |
| 202 | +{ |
| 203 | + findReplaceDialog->setExtendedScintilla(this); |
| 204 | + findReplaceDialog->show(); |
| 205 | +} |
| 206 | + |
| 207 | +void ExtendedScintilla::showContextMenu(const QPoint &pos) |
| 208 | +{ |
| 209 | + |
| 210 | + QAction* findReplaceAction = new QAction(QIcon(":/icons/text_replace"), tr("Find and Replace..."), this); |
| 211 | + findReplaceAction->setShortcut(QKeySequence(tr("Ctrl+H"))); |
| 212 | + connect(findReplaceAction, &QAction::triggered, [&]() { |
| 213 | + openFindReplaceDialog(); |
| 214 | + }); |
| 215 | + |
| 216 | + // This has to be created here, otherwise the set of enabled options would not update accordingly. |
| 217 | + QMenu* editContextMenu = createStandardContextMenu(); |
| 218 | + editContextMenu->addSeparator(); |
| 219 | + editContextMenu->addAction(findReplaceAction); |
| 220 | + |
| 221 | + editContextMenu->exec(mapToGlobal(pos)); |
| 222 | +} |
0 commit comments