Skip to content

Commit

Permalink
Dark Theme
Browse files Browse the repository at this point in the history
  • Loading branch information
JaDogg committed Nov 26, 2014
1 parent 43b9656 commit f5ee8e7
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 85 deletions.
37 changes: 11 additions & 26 deletions CodeEditor/codeeditor.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/****************************************************************************
**
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
Expand Down Expand Up @@ -36,6 +37,7 @@
**
** $QT_END_LICENSE$
**
** -----------Modified By Bhathiya Perera-------------
****************************************************************************/

#include <QtWidgets>
Expand All @@ -48,10 +50,13 @@ CodeEditor::CodeEditor(QWidget* parent)

connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
connect(this, SIGNAL(updateRequest(QRect, int)), this, SLOT(updateLineNumberArea(QRect, int)));
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));

updateLineNumberAreaWidth(0);
highlightCurrentLine();

QPalette p = this->palette();
p.setColor(QPalette::Base, Qt::black);
p.setColor(QPalette::Text, Qt::white);
this->setPalette(p);
}

int CodeEditor::lineNumberAreaWidth()
Expand Down Expand Up @@ -94,42 +99,22 @@ void CodeEditor::resizeEvent(QResizeEvent* e)

void CodeEditor::keyPressEvent(QKeyEvent* e)
{
//TODO handle automatical indentation
//TODO backspace auto unindent
//TODO indent selected text
switch (e->key()) {
case Qt::Key_Tab:
QPlainTextEdit::insertPlainText(" ");
break;
// TODO Indentation
// case Qt::Key_Enter:
// case Qt::Key_Return:
// break;
default:
QPlainTextEdit::keyPressEvent(e);
}
}

void CodeEditor::highlightCurrentLine()
{
QList<QTextEdit::ExtraSelection> extraSelections;

if (!isReadOnly()) {
QTextEdit::ExtraSelection selection;

QColor lineColor = QColor(Qt::yellow).lighter(160);

selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
selection.cursor = textCursor();
selection.cursor.clearSelection();
extraSelections.append(selection);
}

setExtraSelections(extraSelections);
}

void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent* event)
{
QPainter painter(lineNumberArea);
painter.fillRect(event->rect(), Qt::lightGray);
painter.fillRect(event->rect(), Qt::darkGray);

QTextBlock block = firstVisibleBlock();
int blockNumber = block.blockNumber();
Expand Down
1 change: 0 additions & 1 deletion CodeEditor/codeeditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ class CodeEditor : public QPlainTextEdit {

private slots:
void updateLineNumberAreaWidth(int newBlockCount);
void highlightCurrentLine();
void updateLineNumberArea(const QRect&, int);

private:
Expand Down
107 changes: 52 additions & 55 deletions CodeEditor/pythonsyntaxhighlighter.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
/*
$Id: PythonSyntaxHighlighter.cpp 167 2013-11-03 17:01:22Z oliver $
This is a C++ port of the following PyQt example
http://diotavelli.net/PyQtWiki/Python%20syntax%20highlighting
C++ port by Frankie Simon (www.kickdrive.de, www.fuh-edv.de)
The following free software license applies for this file ("X11 license"):
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
/* $Id: PythonSyntaxHighlighter.cpp 167 2013-11-03 17:01:22Z oliver $
*
* This is a C++ port of the following PyQt example
* http://diotavelli.net/PyQtWiki/Python%20syntax%20highlighting
* C++ port by Frankie Simon (www.kickdrive.de, www.fuh-edv.de)
*
* The following free software license applies for this file ("X11 license"):
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* -----------Modified By Bhathiya Perera-------------
*/

#include "CodeEditor/PythonSyntaxHighlighter.h"
Expand Down Expand Up @@ -60,30 +62,26 @@ PythonSyntaxHighlighter::PythonSyntaxHighlighter(QTextDocument* parent)
<< "True"
<< "False";

operators = QStringList() << "=" <<
// Comparison
"=="
operators = QStringList() << "="
<< "=="
<< "!="
<< "<"
<< "<="
<< ">"
<< ">=" <<
// Arithmetic
"\\+"
<< ">="
<< "\\+"
<< "-"
<< "\\*"
<< "/"
<< "//"
<< "%"
<< "\\*\\*" <<
// In-place
"\\+="
<< "\\*\\*"
<< "\\+="
<< "-="
<< "\\*="
<< "/="
<< "%=" <<
// Bitwise
"\\^"
<< "%="
<< "\\^"
<< "\\|"
<< "&"
<< "~"
Expand All @@ -97,11 +95,6 @@ PythonSyntaxHighlighter::PythonSyntaxHighlighter(QTextDocument* parent)
<< "\\["
<< "]";

QFont* font = new QFont();
font->setFamily("Courier New");
font->setFixedPitch(true);
font->setPointSize(10);

setStyles();

triSingleQuote.setPattern("'''");
Expand All @@ -112,16 +105,16 @@ PythonSyntaxHighlighter::PythonSyntaxHighlighter(QTextDocument* parent)

void PythonSyntaxHighlighter::setStyles()
{
basicStyles.insert("keyword", getTextCharFormat("blue"));
basicStyles.insert("keyword", getTextCharFormat("orange", "bold"));
basicStyles.insert("operator", getTextCharFormat("red"));
basicStyles.insert("brace", getTextCharFormat("darkGray"));
basicStyles.insert("defclass", getTextCharFormat("black", "bold"));
basicStyles.insert("brace", getTextCharFormat("darkGray"));
basicStyles.insert("brace", getTextCharFormat("red", "bold"));
basicStyles.insert("defclass", getTextCharFormat("white", "bold"));
basicStyles.insert("string", getTextCharFormat("magenta"));
basicStyles.insert("string2", getTextCharFormat("darkMagenta"));
basicStyles.insert("comment", getTextCharFormat("darkGreen", "italic"));
basicStyles.insert("self", getTextCharFormat("black", "italic"));
basicStyles.insert("numbers", getTextCharFormat("brown"));
basicStyles.insert("comment", getTextCharFormat("darkGreen", "bold"));
basicStyles.insert("self", getTextCharFormat("white", "bold"));
basicStyles.insert("numbers", getTextCharFormat("cyan"));
basicStyles.insert("bugs", getTextCharFormat("yellow", "bold", "red"));
}

void PythonSyntaxHighlighter::initializeRules()
Expand All @@ -139,27 +132,25 @@ void PythonSyntaxHighlighter::initializeRules()
rules.append(HighlightingRule("\\bself\\b", 0, basicStyles.value("self")));

// Double-quoted string, possibly containing escape sequences
// FF: originally in python : r'"[^"\\]*(\\.[^"\\]*)*"'
rules.append(HighlightingRule("\"[^\"\\\\]*(\\\\.[^\"\\\\]*)*\"", 0, basicStyles.value("string")));
// Single-quoted string, possibly containing escape sequences
// FF: originally in python : r"'[^'\\]*(\\.[^'\\]*)*'"
rules.append(HighlightingRule("'[^'\\\\]*(\\\\.[^'\\\\]*)*'", 0, basicStyles.value("string")));

// 'def' followed by an identifier
// FF: originally: r'\bdef\b\s*(\w+)'
rules.append(HighlightingRule("\\bdef\\b\\s*(\\w+)", 1, basicStyles.value("defclass")));
// 'class' followed by an identifier
// FF: originally: r'\bclass\b\s*(\w+)'
rules.append(HighlightingRule("\\bclass\\b\\s*(\\w+)", 1, basicStyles.value("defclass")));

// From '#' until a newline
// FF: originally: r'#[^\\n]*'
rules.append(HighlightingRule("#[^\\n]*", 0, basicStyles.value("comment")));

// Numeric literals
rules.append(HighlightingRule("\\b[+-]?[0-9]+[lL]?\\b", 0, basicStyles.value("numbers"))); // r'\b[+-]?[0-9]+[lL]?\b'
rules.append(HighlightingRule("\\b[+-]?0[xX][0-9A-Fa-f]+[lL]?\\b", 0, basicStyles.value("numbers"))); // r'\b[+-]?0[xX][0-9A-Fa-f]+[lL]?\b'
rules.append(HighlightingRule("\\b[+-]?[0-9]+(?:\\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\\b", 0, basicStyles.value("numbers"))); // r'\b[+-]?[0-9]+(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\b'
rules.append(HighlightingRule("\\b[+-]?[0-9]+[lL]?\\b", 0, basicStyles.value("numbers")));
rules.append(HighlightingRule("\\b[+-]?0[xX][0-9A-Fa-f]+[lL]?\\b", 0, basicStyles.value("numbers")));
rules.append(HighlightingRule("\\b[+-]?[0-9]+(?:\\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\\b", 0, basicStyles.value("numbers")));

// tab and space mixed
rules.append(HighlightingRule("[^\\n]*(?:\\t | \\t)[^\\n]*", 0, basicStyles.value("bugs")));
}

void PythonSyntaxHighlighter::highlightBlock(const QString& text)
Expand Down Expand Up @@ -227,11 +218,17 @@ bool PythonSyntaxHighlighter::matchMultiline(const QString& text, const QRegExp&
return false;
}

const QTextCharFormat PythonSyntaxHighlighter::getTextCharFormat(const QString& colorName, const QString& style)
const QTextCharFormat PythonSyntaxHighlighter::getTextCharFormat(
const QString& colorName, const QString& style, const QString& backColorName)
{
QTextCharFormat charFormat;
QColor color(colorName);
charFormat.setForeground(color);

if (!backColorName.isEmpty()) {
QColor backColor(backColorName);
charFormat.setBackground(backColor);
}
if (style.contains("bold", Qt::CaseInsensitive))
charFormat.setFontWeight(QFont::Bold);
if (style.contains("italic", Qt::CaseInsensitive))
Expand Down
3 changes: 2 additions & 1 deletion CodeEditor/pythonsyntaxhighlighter.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ class PythonSyntaxHighlighter : public QSyntaxHighlighter {
void initializeRules();
//! Highlighst multi-line strings, returns true if after processing we are still within the multi-line section.
bool matchMultiline(const QString& text, const QRegExp& delimiter, const int inState, const QTextCharFormat& style);
const QTextCharFormat getTextCharFormat(const QString& colorName, const QString& style = QString());
const QTextCharFormat getTextCharFormat(const QString& colorName, const QString& style = QString(),
const QString& backColorName = QString());
QList<HighlightingRule> rules;
QRegExp triSingleQuote;
QRegExp triDoubleQuote;
Expand Down
6 changes: 4 additions & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ static MainView* mainView;
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
wchar_t c_s[] = L"expressPython";
Py_SetProgramName(c_s);

wchar_t name[] = L"expressPython";
Py_SetProgramName(name);

snip = new Snippets();
mainView = new MainView();
mainView->SetSnippets(snip);
Expand Down

0 comments on commit f5ee8e7

Please sign in to comment.