Explorar el Código

Initial commit

Nicolas Winkler hace 6 años
commit
e4655305a3
Se han modificado 7 ficheros con 386 adiciones y 0 borrados
  1. 36 0
      brainy.pro
  2. 59 0
      brainywindow.cpp
  3. 31 0
      brainywindow.h
  4. 120 0
      brainywindow.ui
  5. 11 0
      main.cpp
  6. 92 0
      syntaxhighlighter.cpp
  7. 37 0
      syntaxhighlighter.h

+ 36 - 0
brainy.pro

@@ -0,0 +1,36 @@
+#-------------------------------------------------
+#
+# Project created by QtCreator 2017-07-10T21:29:24
+#
+#-------------------------------------------------
+
+QT       += core gui
+
+greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
+
+TARGET = brainy
+TEMPLATE = app
+
+# The following define makes your compiler emit warnings if you use
+# any feature of Qt which as been marked as deprecated (the exact warnings
+# depend on your compiler). Please consult the documentation of the
+# deprecated API in order to know how to port your code away from it.
+DEFINES += QT_DEPRECATED_WARNINGS
+
+# You can also make your code fail to compile if you use deprecated APIs.
+# In order to do so, uncomment the following line.
+# You can also select to disable deprecated APIs only up to a certain version of Qt.
+#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
+
+
+SOURCES += \
+        main.cpp \
+    syntaxhighlighter.cpp \
+    brainywindow.cpp
+
+HEADERS += \
+    syntaxhighlighter.h \
+    brainywindow.h
+
+FORMS += \
+    brainywindow.ui

+ 59 - 0
brainywindow.cpp

@@ -0,0 +1,59 @@
+#include "brainywindow.h"
+#include "ui_editorwindow.h"
+#include <QFileDialog>
+#include <QFontDialog>
+#include <QFile>
+#include <QTextStream>
+#include <QFontMetrics>
+#include <iostream>
+
+EditorWindow::EditorWindow(QWidget *parent) :
+    QMainWindow(parent),
+    ui(new Ui::EditorWindow)
+{
+    ui->setupUi(this);
+    sh = new SyntaxHighlighter(ui->textEdit->document());
+
+    ui->actionOpen_2->connect(ui->actionOpen_2, &QAction::triggered, this, openFile);
+    ui->actionSave_2->connect(ui->actionSave_2, &QAction::triggered, this, saveFile);
+    ui->actionEditor_Font->connect(ui->actionEditor_Font, &QAction::triggered, this, [this] () {
+        bool ok;
+        QFont font = QFontDialog::getFont(&ok, ui->textEdit->font(), this);
+        if (ok)
+            ui->textEdit->setFont(font);
+    });
+}
+
+EditorWindow::~EditorWindow()
+{
+    delete ui;
+}
+
+
+void EditorWindow::openFile()
+{
+    auto filename = QFileDialog::getOpenFileName(this,
+        tr("Open File"), "", tr("Brainfuck File (*.bf);; Any File (*.*)"));
+    openedFile = new QFile(filename);
+    if (!openedFile->open(QIODevice::ReadWrite | QFile::Text)) return;
+    QTextStream in(openedFile);
+    ui->textEdit->setText(in.readAll());
+}
+
+
+void EditorWindow::saveFile()
+{
+    if (!openedFile->isOpen()) {
+        openedFile->open(QIODevice::WriteOnly | QFile::Text | QFile::Truncate);
+    }
+    if (openedFile->isOpen()) {
+        openedFile->write(ui->textEdit->document()->toPlainText().toUtf8().data());
+        openedFile->close();
+    }
+}
+
+
+bool EditorWindow::changesToSave() const
+{
+    return changedAnything;
+}

+ 31 - 0
brainywindow.h

@@ -0,0 +1,31 @@
+#ifndef EDITORWINDOW_H
+#define EDITORWINDOW_H
+
+#include <QMainWindow>
+#include "syntaxhighlighter.h"
+
+namespace Ui
+{
+    class EditorWindow;
+}
+
+class EditorWindow : public QMainWindow
+{
+    Q_OBJECT
+
+public:
+    explicit EditorWindow(QWidget *parent = 0);
+    ~EditorWindow();
+
+    bool changedAnything;
+    QFile* openedFile;
+    void openFile();
+    void saveFile();
+    bool changesToSave() const;
+
+private:
+    Ui::EditorWindow* ui;
+    SyntaxHighlighter* sh;
+};
+
+#endif // EDITORWINDOW_H

+ 120 - 0
brainywindow.ui

@@ -0,0 +1,120 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>BrainyWindow</class>
+ <widget class="QMainWindow" name="BrainyWindow">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>1091</width>
+    <height>560</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>Brainy</string>
+  </property>
+  <property name="windowIcon">
+   <iconset theme="document-new"/>
+  </property>
+  <widget class="QWidget" name="centralWidget">
+   <layout class="QVBoxLayout" name="verticalLayout">
+    <item>
+     <widget class="QTextEdit" name="textEdit">
+      <property name="font">
+       <font>
+        <family>Courier New</family>
+        <pointsize>10</pointsize>
+       </font>
+      </property>
+      <property name="layoutDirection">
+       <enum>Qt::LeftToRight</enum>
+      </property>
+      <property name="html">
+       <string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
+&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
+p, li { white-space: pre-wrap; }
+&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'Courier New'; font-size:10pt; font-weight:400; font-style:normal;&quot;&gt;
+&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
+      </property>
+      <property name="acceptRichText">
+       <bool>false</bool>
+      </property>
+     </widget>
+    </item>
+   </layout>
+  </widget>
+  <widget class="QMenuBar" name="menuBar">
+   <property name="geometry">
+    <rect>
+     <x>0</x>
+     <y>0</y>
+     <width>1091</width>
+     <height>21</height>
+    </rect>
+   </property>
+   <widget class="QMenu" name="menuFile">
+    <property name="title">
+     <string>File</string>
+    </property>
+    <addaction name="actionNew_File"/>
+    <addaction name="separator"/>
+    <addaction name="actionOpen_2"/>
+    <addaction name="actionSave_2"/>
+   </widget>
+   <widget class="QMenu" name="menuOptions">
+    <property name="title">
+     <string>Options</string>
+    </property>
+    <addaction name="actionEditor_Font"/>
+   </widget>
+   <addaction name="menuFile"/>
+   <addaction name="menuOptions"/>
+  </widget>
+  <widget class="QStatusBar" name="statusBar"/>
+  <action name="actionNew_File">
+   <property name="text">
+    <string>New File</string>
+   </property>
+   <property name="statusTip">
+    <string>Creates a new file</string>
+   </property>
+   <property name="shortcut">
+    <string>Ctrl+N</string>
+   </property>
+  </action>
+  <action name="actionOpen">
+   <property name="text">
+    <string>Open</string>
+   </property>
+  </action>
+  <action name="actionSave">
+   <property name="text">
+    <string>Save</string>
+   </property>
+  </action>
+  <action name="actionOpen_2">
+   <property name="text">
+    <string>&amp;Open</string>
+   </property>
+   <property name="shortcut">
+    <string>Ctrl+O</string>
+   </property>
+  </action>
+  <action name="actionSave_2">
+   <property name="text">
+    <string>&amp;Save</string>
+   </property>
+   <property name="shortcut">
+    <string>Ctrl+S</string>
+   </property>
+  </action>
+  <action name="actionEditor_Font">
+   <property name="text">
+    <string>Editor Font</string>
+   </property>
+  </action>
+ </widget>
+ <layoutdefault spacing="6" margin="11"/>
+ <resources/>
+ <connections/>
+</ui>

+ 11 - 0
main.cpp

@@ -0,0 +1,11 @@
+#include "brainywindow.h"
+#include <QApplication>
+
+int main(int argc, char *argv[])
+{
+    QApplication a(argc, argv);
+    EditorWindow w;
+    w.show();
+
+    return a.exec();
+}

+ 92 - 0
syntaxhighlighter.cpp

@@ -0,0 +1,92 @@
+#include "syntaxhighlighter.h"
+#include <set>
+
+SyntaxHighlighter::SyntaxHighlighter(QTextDocument *parent) :
+    QSyntaxHighlighter(parent)
+{
+    levelColor.push_back(QTextCharFormat());
+    levelColor.push_back(QTextCharFormat());
+    levelColor.push_back(QTextCharFormat());
+    levelColor.push_back(QTextCharFormat());
+    levelColor.push_back(QTextCharFormat());
+    levelColor.push_back(QTextCharFormat());
+    levelColor[0].setForeground(Qt::blue);
+    levelColor[1].setForeground(Qt::red);
+    levelColor[2].setForeground(Qt::darkGreen);
+    levelColor[3].setForeground(Qt::darkRed);
+    levelColor[4].setForeground(Qt::darkYellow);
+    levelColor[5].setForeground(Qt::green);
+    for (auto& tcf : levelColor) {
+        tcf.setFontWeight(99);
+    }
+    comment.setForeground(Qt::gray);
+}
+
+
+void SyntaxHighlighter::highlightBlock(const QString &text)
+{
+    struct FormatState {
+        QTextCharFormat* currentFormat;
+        int startIndex = 0;
+        int length = 0;
+        SyntaxHighlighter* sh;
+
+        FormatState(SyntaxHighlighter* sh) : currentFormat{ &sh->comment }, sh{ sh } {}
+
+        void upateFormat(int len, QTextCharFormat* format) {
+            if (format == currentFormat) {
+                length++;
+            }
+            else {
+                sh->setFormat(startIndex, length, *currentFormat);
+                currentFormat = format;
+                startIndex += length;
+                length = 1;
+            }
+        }
+
+        void finish() {
+            sh->setFormat(startIndex, length, *currentFormat);
+        }
+    };
+
+    int level = previousBlockState();
+    if (level < 0) level = 0;
+    FormatState fs(this);
+    for (int i = 0; i < text.length(); i++) {
+        auto& c = text[i];
+        if (c == '[') {
+            level++;
+        }
+        if (isBfChar(c)) {
+            fs.upateFormat(1, &levelColor[level % levelColor.size()]);
+        }
+        else {
+            fs.upateFormat(1, &comment);
+        }
+        if (c == ']') {
+            level--;
+        }
+        /*if (c == '[') {
+            level++;
+        }
+        if (isBfChar(c)) {
+            setFormat(i, 1, levelColor[level % levelColor.size()]);
+        }
+        else {
+            setFormat(i, 1, comment);
+        }
+        if (c == ']') {
+            level--;
+        }*/
+    }
+    fs.finish();
+    setCurrentBlockState(level);
+}
+
+
+bool SyntaxHighlighter::isBfChar(const QChar& c)
+{
+    static const std::set<QChar> x{'+', '-', '<', '>', ',', '.', '[', ']'};
+    return x.find(c) != x.end();
+}

+ 37 - 0
syntaxhighlighter.h

@@ -0,0 +1,37 @@
+#ifndef SYNTAXHIGHLIGHTER_H
+#define SYNTAXHIGHLIGHTER_H
+
+#include <QSyntaxHighlighter>
+
+class SyntaxHighlighter :
+        public QSyntaxHighlighter
+{
+    Q_OBJECT
+
+public:
+    SyntaxHighlighter(QTextDocument *parent = 0);
+
+protected:
+    void highlightBlock(const QString &text) override;
+
+private:
+    /*struct HighlightingRule
+    {
+        QRegularExpression pattern;
+        QTextCharFormat format;
+    };
+    QVector<HighlightingRule> highlightingRules;
+
+    QRegularExpression commentStartExpression;
+    QRegularExpression commentEndExpression;*/
+
+    /**
+     * @brief color per loop level
+     */
+    std::vector<QTextCharFormat> levelColor;
+    QTextCharFormat comment;
+
+    bool isBfChar(const QChar& c);
+};
+
+#endif // SYNTAXHIGHLIGHTER_H