Skip to content

Instantly share code, notes, and snippets.

@shaina7837
Created March 24, 2015 04:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shaina7837/a172752b717e7fe7ec14 to your computer and use it in GitHub Desktop.
Save shaina7837/a172752b717e7fe7ec14 to your computer and use it in GitHub Desktop.
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#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()
{
}
#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
~
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
#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";
}
#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