openscad/src/scintillaeditor.cpp

261 lines
8.3 KiB
C++
Raw Normal View History

#include <algorithm>
2014-06-07 17:29:40 +04:00
#include <QString>
#include <QChar>
2014-06-07 19:19:38 +04:00
#include "scintillaeditor.h"
2014-06-17 19:34:54 +04:00
#include "Preferences.h"
2014-06-07 19:19:38 +04:00
2014-06-07 19:22:14 +04:00
ScintillaEditor::ScintillaEditor(QWidget *parent) : EditorInterface(parent)
2014-06-07 19:19:38 +04:00
{
2014-08-22 21:42:23 +04:00
scintillaLayout = new QVBoxLayout(this);
qsci = new QsciScintilla(this);
scintillaLayout->setContentsMargins(0, 0, 0, 0);
scintillaLayout->addWidget(qsci);
qsci->setBraceMatching (QsciScintilla::SloppyBraceMatch);
qsci->setWrapMode(QsciScintilla::WrapWord);
qsci->setWrapVisualFlags(QsciScintilla::WrapFlagByText, QsciScintilla::WrapFlagByText, 0);
qsci->setAutoIndent(true);
qsci->indicatorDefine(QsciScintilla::RoundBoxIndicator, indicatorNumber);
qsci->markerDefine(QsciScintilla::Circle, markerNumber);
qsci->setUtf8(true);
preferenceEditorOption = Preferences::inst()->getValue("editor/syntaxhighlight").toString();
lexer = new ScadLexer(this);
initLexer();
initMargin();
qsci->setFolding(QsciScintilla::BoxedTreeFoldStyle, 4);
qsci->setCaretLineVisible(true);
this->setHighlightScheme(preferenceEditorOption);
2014-06-20 10:37:40 +04:00
2014-06-07 19:19:38 +04:00
}
2014-06-11 14:29:05 +04:00
2014-06-07 19:22:14 +04:00
void ScintillaEditor::setPlainText(const QString &text)
2014-06-07 17:29:40 +04:00
{
2014-08-22 21:42:23 +04:00
qsci->setText(text);
2014-06-07 17:29:40 +04:00
}
2014-06-13 04:40:47 +04:00
2014-06-07 17:29:40 +04:00
QString ScintillaEditor::toPlainText()
{
2014-08-22 21:42:23 +04:00
return qsci->text();
2014-06-07 17:29:40 +04:00
}
bool ScintillaEditor::isContentModified()
{
2014-07-17 23:10:58 +04:00
return qsci->isModified();
}
2014-06-13 04:40:47 +04:00
2014-06-07 19:22:14 +04:00
void ScintillaEditor::highlightError(int error_pos)
2014-06-11 14:29:05 +04:00
{
2014-08-22 21:42:23 +04:00
int line, index;
qsci->lineIndexFromPosition(error_pos, &line, &index);
qsci->fillIndicatorRange(line, index, line, index+1, indicatorNumber);
qsci->setIndicatorForegroundColor(QColor(255,0,0,100));
qsci->markerAdd(line, markerNumber);
2014-06-11 14:29:05 +04:00
}
2014-06-13 04:40:47 +04:00
2014-06-07 19:22:14 +04:00
void ScintillaEditor::unhighlightLastError()
2014-06-11 14:29:05 +04:00
{
2014-08-22 21:42:23 +04:00
int totalLength = qsci->text().length();
int line, index;
qsci->lineIndexFromPosition(totalLength, &line, &index);
qsci->clearIndicatorRange(0, 0, line, index, indicatorNumber);
qsci->markerDeleteAll(markerNumber);
2014-06-11 14:29:05 +04:00
}
2014-06-13 04:40:47 +04:00
2014-08-18 08:13:43 +04:00
//Editor themes
2014-06-19 21:23:00 +04:00
void ScintillaEditor::forLightBackground()
{
2014-08-22 21:42:23 +04:00
lexer->setPaper("#fff");
lexer->setColor(QColor("#272822")); // -> Style: Default text
lexer->setColor(QColor("Green"), QsciLexerCPP::Keyword); // -> Style: Keyword
lexer->setColor(QColor("Green"), QsciLexerCPP::KeywordSet2); // -> Style: KeywordSet2
lexer->setColor(Qt::blue, QsciLexerCPP::CommentDocKeyword); // -> used in comments only like /*! \cube */
lexer->setColor(QColor("DarkBlue"), QsciLexerCPP::GlobalClass); // -> Style: GlobalClass
lexer->setColor(Qt::blue, QsciLexerCPP::Operator);
lexer->setColor(Qt::darkMagenta, QsciLexerCPP::DoubleQuotedString);
lexer->setColor(Qt::darkCyan, QsciLexerCPP::Comment);
lexer->setColor(Qt::darkCyan, QsciLexerCPP::CommentLine);
lexer->setColor(QColor("DarkRed"), QsciLexerCPP::Number);
qsci->setMarkerBackgroundColor(QColor(255, 0, 0, 100), markerNumber);
qsci->setCaretLineBackgroundColor(QColor("#ffe4e4"));
qsci->setMarginsBackgroundColor(QColor("#ccc"));
qsci->setMarginsForegroundColor(QColor("#111"));
2014-06-19 21:23:00 +04:00
}
void ScintillaEditor::forDarkBackground()
{
2014-08-22 21:42:23 +04:00
lexer->setPaper(QColor("#272822"));
lexer->setColor(QColor(Qt::white));
lexer->setColor(QColor("#f12971"), QsciLexerCPP::Keyword);
lexer->setColor(QColor("#56dbf0"),QsciLexerCPP::KeywordSet2);
lexer->setColor(QColor("#ccdf32"), QsciLexerCPP::CommentDocKeyword);
lexer->setColor(QColor("#56d8f0"), QsciLexerCPP::GlobalClass);
lexer->setColor(QColor("#d8d8d8"), QsciLexerCPP::Operator);
lexer->setColor(QColor("#e6db74"), QsciLexerCPP::DoubleQuotedString);
lexer->setColor(QColor("#e6db74"), QsciLexerCPP::CommentLine);
lexer->setColor(QColor("#af7dff"), QsciLexerCPP::Number);
qsci->setCaretLineBackgroundColor(QColor(104,225,104, 127));
qsci->setMarkerBackgroundColor(QColor(255, 0, 0, 100), markerNumber);
qsci->setMarginsBackgroundColor(QColor("20,20,20,150"));
qsci->setMarginsForegroundColor(QColor("#fff"));
2014-06-19 21:23:00 +04:00
2014-06-20 10:37:40 +04:00
}
void ScintillaEditor::Monokai()
{
2014-08-22 21:42:23 +04:00
lexer->setPaper("#272822");
lexer->setColor(QColor("#f8f8f2")); // -> Style: Default text
lexer->setColor(QColor("#66c3b3"), QsciLexerCPP::Keyword); // -> Style: Keyword
lexer->setColor(QColor("#79abff"), QsciLexerCPP::KeywordSet2); // -> Style: KeywordSet2
lexer->setColor(QColor("#ccdf32"), QsciLexerCPP::CommentDocKeyword); // -> used in comments only like /*! \cube */
lexer->setColor(QColor("#ffffff"), QsciLexerCPP::GlobalClass); // -> Style: GlobalClass
lexer->setColor(QColor("#d8d8d8"), QsciLexerCPP::Operator);
lexer->setColor(QColor("#e6db74"), QsciLexerCPP::DoubleQuotedString);
lexer->setColor(QColor("#75715e"), QsciLexerCPP::CommentLine);
lexer->setColor(QColor("#7fb347"), QsciLexerCPP::Number);
qsci->setMarkerBackgroundColor(QColor(255, 0, 0, 100), markerNumber);
qsci->setCaretLineBackgroundColor(QColor("#3e3d32"));
qsci->setMarginsBackgroundColor(QColor("#757575"));
qsci->setMarginsForegroundColor(QColor("#f8f8f2"));
}
2014-08-05 17:53:10 +04:00
void ScintillaEditor::Solarized_light()
{
2014-08-22 21:42:23 +04:00
lexer->setPaper("#fdf6e3");
lexer->setColor(QColor("#657b83")); // -> Style: Default text
lexer->setColor(QColor("#268ad1"), QsciLexerCPP::Keyword); // -> Style: Keyword
lexer->setColor(QColor("#6c71c4"), QsciLexerCPP::KeywordSet2); // -> Style: KeywordSet2
lexer->setColor(QColor("#b58900"), QsciLexerCPP::CommentDocKeyword); // -> used in comments only like /*! \cube */
lexer->setColor(QColor("#b58800"), QsciLexerCPP::GlobalClass); // -> Style: GlobalClass
lexer->setColor(QColor("#859900"), QsciLexerCPP::Operator);
lexer->setColor(QColor("#2aa198"), QsciLexerCPP::DoubleQuotedString);
lexer->setColor(QColor("#b58800"), QsciLexerCPP::CommentLine);
lexer->setColor(QColor("#cb4b16"), QsciLexerCPP::Number);
qsci->setMarkerBackgroundColor(QColor(255, 0, 0, 100), markerNumber);
qsci->setCaretLineBackgroundColor(QColor("#eeead5"));
qsci->setMarginsBackgroundColor(QColor("#eee8d5"));
qsci->setMarginsForegroundColor(QColor("#93a1a1"));
2014-08-05 17:53:10 +04:00
}
2014-06-20 10:37:40 +04:00
void ScintillaEditor::noColor()
{
2014-08-22 21:42:23 +04:00
lexer->setPaper(Qt::white);
lexer->setColor(Qt::black);
qsci->setMarginsBackgroundColor(QColor("#ccc"));
qsci->setMarginsForegroundColor(QColor("#111"));
2014-06-20 10:37:40 +04:00
}
2014-06-07 19:22:14 +04:00
void ScintillaEditor::setHighlightScheme(const QString &name)
2014-06-11 14:29:05 +04:00
{
2014-06-19 21:23:00 +04:00
2014-08-22 21:42:23 +04:00
if(name == "For Light Background")
{
forLightBackground();
}
else if(name == "For Dark Background")
{
forDarkBackground();
}
else if(name == "Monokai")
{
Monokai();
}
else if(name == "Solarized")
{
Solarized_light();
}
else if(name == "Off")
{
noColor();
}
2014-06-11 14:29:05 +04:00
}
2014-06-13 04:40:47 +04:00
2014-06-07 19:22:14 +04:00
void ScintillaEditor::insertPlainText(const QString &text)
2014-06-07 17:29:40 +04:00
{
2014-08-22 21:42:23 +04:00
qsci->setText(text);
2014-06-07 17:29:40 +04:00
}
2014-06-07 19:22:14 +04:00
void ScintillaEditor::undo()
{
2014-08-22 21:42:23 +04:00
qsci->undo();
2014-06-07 19:22:14 +04:00
}
void ScintillaEditor::redo()
{
2014-08-22 21:42:23 +04:00
qsci->redo();
2014-06-07 19:22:14 +04:00
}
void ScintillaEditor::cut()
{
2014-08-22 21:42:23 +04:00
qsci->cut();
2014-06-07 19:22:14 +04:00
}
void ScintillaEditor::copy()
{
2014-08-22 21:42:23 +04:00
qsci->copy();
2014-06-07 19:22:14 +04:00
}
void ScintillaEditor::paste()
{
2014-08-22 21:42:23 +04:00
qsci->paste();
2014-06-07 19:22:14 +04:00
}
void ScintillaEditor::zoomIn()
{
2014-08-22 21:42:23 +04:00
qsci->zoomIn();
2014-06-07 19:22:14 +04:00
}
void ScintillaEditor::zoomOut()
{
2014-08-22 21:42:23 +04:00
qsci->zoomOut();
2014-06-07 19:22:14 +04:00
}
void ScintillaEditor::initFont(const QString& fontName, uint size)
2014-06-11 14:29:05 +04:00
{
2014-08-22 21:42:23 +04:00
QFont font(fontName, size);
font.setFixedPitch(true);
lexer->setFont(font);
2014-06-11 14:29:05 +04:00
}
2014-06-20 10:37:40 +04:00
void ScintillaEditor::initLexer()
{
2014-08-22 21:42:23 +04:00
qsci->setLexer(lexer);
2014-06-20 10:37:40 +04:00
}
2014-08-18 08:13:43 +04:00
2014-06-11 14:29:05 +04:00
void ScintillaEditor::initMargin()
{
2014-08-22 21:42:23 +04:00
QFontMetrics fontmetrics = QFontMetrics(qsci->font());
qsci->setMarginsFont(qsci->font());
qsci->setMarginWidth(0, fontmetrics.width(QString::number(qsci->lines())) + 6);
qsci->setMarginLineNumbers(0, true);
2014-06-11 14:29:05 +04:00
2014-08-22 21:42:23 +04:00
connect(qsci, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
2014-06-11 14:29:05 +04:00
}
void ScintillaEditor::onTextChanged()
{
2014-08-22 21:42:23 +04:00
QFontMetrics fontmetrics = qsci->fontMetrics();
qsci->setMarginWidth(0, fontmetrics.width(QString::number(qsci->lines())) + 6);
2014-06-11 14:29:05 +04:00
}
bool ScintillaEditor::find(const QString &expr, bool findNext, bool findBackwards)
{
int startline = -1, startindex = -1;
// If findNext, start from the end of the current selection
if (qsci->hasSelectedText()) {
int lineFrom, indexFrom, lineTo, indexTo;
qsci->getSelection(&lineFrom, &indexFrom, &lineTo, &indexTo);
2014-08-14 02:58:59 +04:00
startline = !(findBackwards xor findNext) ? std::min(lineFrom, lineTo) : std::max(lineFrom, lineTo);
startindex = !(findBackwards xor findNext) ? std::min(indexFrom, indexTo) : std::max(indexFrom, indexTo);
}
return qsci->findFirst(expr, false, false, false, true,
!findBackwards, startline, startindex);
}
void ScintillaEditor::replaceSelectedText(QString& newText)
{
2014-08-22 21:42:23 +04:00
qsci->replaceSelectedText(newText);
}