Created
March 24, 2015 04:50
-
-
Save shaina7837/a172752b717e7fe7ec14 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QApplication> | |
#include "mainwindow.h" | |
int main(int argc, char *argv[]) | |
{ | |
QApplication a(argc, argv); | |
MainWindow w; | |
w.show(); | |
return a.exec(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "mainwindow.h" | |
#include "scadlexer.h" | |
MainWindow::MainWindow(QWidget *parent) | |
: QMainWindow(parent) | |
{ | |
qsci = new QsciScintilla; | |
setCentralWidget(qsci); | |
ScadLexer * lexer = new ScadLexer(this); | |
qsci->setLexer(lexer); | |
lexer->setFont( QFont("Courier New") ); | |
qsci->setCaretLineVisible(true); | |
qsci->setCaretLineBackgroundColor(QColor("gainsboro")); | |
qsci->setAutoIndent(true); | |
qsci->setIndentationWidth(4); | |
qsci->setMarginsBackgroundColor(QColor("gainsboro")); | |
qsci->setMarginLineNumbers(1, true); | |
qsci->setMarginWidth(1, 50); | |
qsci->setBraceMatching(QsciScintilla::SloppyBraceMatch); | |
qsci->setMatchedBraceBackgroundColor(Qt::yellow); | |
qsci->setUnmatchedBraceForegroundColor(Qt::blue); | |
} | |
MainWindow::~MainWindow() | |
{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef MAINWINDOW_H | |
#define MAINWINDOW_H | |
#include <QMainWindow> | |
#include <Qsci/qsciscintilla.h> | |
class MainWindow : public QMainWindow | |
{ | |
Q_OBJECT | |
public: | |
MainWindow(QWidget *parent = 0); | |
~MainWindow(); | |
private: | |
QsciScintilla *qsci; | |
}; | |
#endif | |
~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
QT += core gui | |
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets | |
TARGET = scadlexer-demo | |
TEMPLATE = app | |
SOURCES += main.cpp\ | |
mainwindow.cpp \ | |
scadlexer.cpp | |
HEADERS += mainwindow.h \ | |
scadlexer.h | |
LIBS += -lqscintilla2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "scadlexer.h" | |
#include <Qsci/qsciscintilla.h> | |
ScadLexer::ScadLexer(QObject *parent) : | |
QsciLexerCustom(parent) | |
{ | |
keywordsList << "if" << "else" << "cirlce" << "sphere" << | |
"function" << "module" << "use" << "cylinder" << | |
"include"; | |
} | |
ScadLexer::~ScadLexer() | |
{; | |
} | |
void ScadLexer::styleText(int start, int end) | |
{ | |
if(!editor()) | |
return; | |
char * data = new char[end - start + 1]; | |
editor()->SendScintilla(QsciScintilla::SCI_GETTEXTRANGE, start, end, data); | |
QString source(data); | |
delete [] data; | |
if(source.isEmpty()) | |
return; | |
highlightKeywords(source, start); | |
} | |
void ScadLexer::highlightKeywords(const QString &source, int start) | |
{ | |
foreach(QString word, keywordsList) { | |
if(source.contains(word)) { | |
int p = source.count(word); | |
int index = 0; | |
while(p != 0) { | |
int begin = source.indexOf(word, index); | |
index = begin+1; | |
startStyling(start + begin); | |
setStyling(word.length(), Keyword); | |
startStyling(start + begin); | |
p--; | |
} | |
} | |
} | |
} | |
QColor ScadLexer::defaultColor(int style) const | |
{ | |
switch(style) { | |
case Keyword: | |
return Qt::blue; | |
} | |
return Qt::black; | |
} | |
QString ScadLexer::description(int style) const | |
{ | |
switch(style) { | |
case Default: | |
return "Default"; | |
case Keyword: | |
return "Keyword"; | |
} | |
return QString(style); | |
} | |
const char * ScadLexer::language() const | |
{ | |
return "SCAD"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef QSCILEXERASM_H | |
#define QSCILEXERASM_H | |
#include<Qsci/qscilexercustom.h> | |
class ScadLexer : public QsciLexerCustom | |
{ | |
Q_OBJECT | |
public: | |
explicit ScadLexer(QObject *parent = 0); | |
~ScadLexer(); | |
void styleText(int start, int end); | |
void highlightKeywords(const QString &source, int start); | |
const char * language() const; | |
QColor defaultColor(int style) const; | |
QString description(int style) const; | |
enum { | |
Default = 0, | |
Comment = 1, | |
Keyword = 2 | |
}; | |
private: | |
ScadLexer(const ScadLexer &); | |
ScadLexer &operator=(const ScadLexer &); | |
QStringList keywordsList; | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment