Selaa lähdekoodia

began to implement proper ui

Zoadian 10 vuotta sitten
vanhempi
sitoutus
70d6acafc7
13 muutettua tiedostoa jossa 470 lisäystä ja 7 poistoa
  1. 23 0
      ccsim.cpp
  2. 22 0
      ccsim.h
  3. 12 3
      ccsim.pro
  4. 1 1
      ccsim.pro.user
  5. 116 0
      ccsim.ui
  6. 5 2
      main.cpp
  7. 3 1
      mainwindow.cpp
  8. 77 0
      syntaxhighlighter.cpp
  9. 33 0
      syntaxhighlighter.h
  10. 112 0
      wgtcodeeditor.cpp
  11. 28 0
      wgtcodeeditor.h
  12. 15 0
      wgtlinenumberarea.cpp
  13. 23 0
      wgtlinenumberarea.h

+ 23 - 0
ccsim.cpp

@@ -0,0 +1,23 @@
+#include "ccsim.h"
+#include "ui_ccsim.h"
+
+CCSim::CCSim(QWidget *parent) :
+    QMainWindow(parent),
+    ui(new Ui::CCSim)
+{
+    ui->setupUi(this);
+
+
+   auto path =  "C:\\Users\\zoadian\\Desktop\\worldcup\\aftermath_03.rob";
+
+
+   QFile file(path);
+   if (file.open(QFile::ReadOnly | QFile::Text))
+       ui->pteEditor->setPlainText(file.readAll());
+
+}
+
+CCSim::~CCSim()
+{
+    delete ui;
+}

+ 22 - 0
ccsim.h

@@ -0,0 +1,22 @@
+#ifndef CCSIM_H
+#define CCSIM_H
+
+#include <QMainWindow>
+
+namespace Ui {
+class CCSim;
+}
+
+class CCSim : public QMainWindow
+{
+    Q_OBJECT
+
+public:
+    explicit CCSim(QWidget *parent = 0);
+    ~CCSim();
+
+private:
+    Ui::CCSim *ui;
+};
+
+#endif // CCSIM_H

+ 12 - 3
ccsim.pro

@@ -19,7 +19,11 @@ SOURCES += main.cpp\
     program.cpp \
     simulator.cpp \
     instruction.cpp \
-    parse.cpp
+    parse.cpp \
+    ccsim.cpp \
+    wgtcodeeditor.cpp \
+    wgtlinenumberarea.cpp \
+    syntaxhighlighter.cpp
 
 HEADERS  += mainwindow.h \
     simulator.h \
@@ -27,7 +31,12 @@ HEADERS  += mainwindow.h \
     debugwidget.h \
     program.h \
     instruction.h \
-    parse.h
+    parse.h \
+    ccsim.h \
+    wgtcodeeditor.h \
+    wgtlinenumberarea.h \
+    syntaxhighlighter.h
 
 FORMS    += mainwindow.ui \
-    debugwidget.ui
+    debugwidget.ui \
+    ccsim.ui

+ 1 - 1
ccsim.pro.user

@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE QtCreatorProject>
-<!-- Written by QtCreator 3.6.0, 2016-01-24T13:12:47. -->
+<!-- Written by QtCreator 3.6.0, 2016-01-24T21:36:35. -->
 <qtcreator>
  <data>
   <variable>EnvironmentId</variable>

+ 116 - 0
ccsim.ui

@@ -0,0 +1,116 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>CCSim</class>
+ <widget class="QMainWindow" name="CCSim">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>1024</width>
+    <height>768</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>MainWindow</string>
+  </property>
+  <widget class="QWidget" name="centralwidget">
+   <layout class="QVBoxLayout" name="verticalLayout">
+    <item>
+     <layout class="QHBoxLayout" name="horizontalLayout">
+      <item>
+       <widget class="QPushButton" name="btnEditor">
+        <property name="text">
+         <string>Editor</string>
+        </property>
+       </widget>
+      </item>
+      <item>
+       <widget class="QPushButton" name="btnSimulator">
+        <property name="text">
+         <string>Simulator</string>
+        </property>
+       </widget>
+      </item>
+      <item>
+       <widget class="QPushButton" name="btnTournament">
+        <property name="text">
+         <string>Tournament</string>
+        </property>
+       </widget>
+      </item>
+      <item>
+       <widget class="QPushButton" name="btnOptionSet">
+        <property name="text">
+         <string>Option Set</string>
+        </property>
+       </widget>
+      </item>
+      <item>
+       <spacer name="horizontalSpacer">
+        <property name="orientation">
+         <enum>Qt::Horizontal</enum>
+        </property>
+        <property name="sizeHint" stdset="0">
+         <size>
+          <width>40</width>
+          <height>20</height>
+         </size>
+        </property>
+       </spacer>
+      </item>
+      <item>
+       <widget class="QPushButton" name="btnPreferences">
+        <property name="text">
+         <string>Preferences</string>
+        </property>
+       </widget>
+      </item>
+      <item>
+       <widget class="QPushButton" name="btnHelp">
+        <property name="text">
+         <string>Help</string>
+        </property>
+       </widget>
+      </item>
+     </layout>
+    </item>
+    <item>
+     <widget class="QStackedWidget" name="stackedWidget">
+      <widget class="QWidget" name="pgEditor">
+       <layout class="QVBoxLayout" name="verticalLayout_2">
+        <item>
+         <widget class="WgtCodeEditor" name="pteEditor"/>
+        </item>
+       </layout>
+      </widget>
+      <widget class="QWidget" name="pgSimulator"/>
+      <widget class="QWidget" name="pgTournament"/>
+      <widget class="QWidget" name="pgOptionSet"/>
+      <widget class="QWidget" name="pgPreferences"/>
+      <widget class="QWidget" name="pgHelp"/>
+     </widget>
+    </item>
+   </layout>
+  </widget>
+  <widget class="QMenuBar" name="menubar">
+   <property name="geometry">
+    <rect>
+     <x>0</x>
+     <y>0</y>
+     <width>1024</width>
+     <height>21</height>
+    </rect>
+   </property>
+  </widget>
+  <widget class="QStatusBar" name="statusbar"/>
+ </widget>
+ <customwidgets>
+  <customwidget>
+   <class>WgtCodeEditor</class>
+   <extends>QPlainTextEdit</extends>
+   <header>wgtcodeeditor.h</header>
+  </customwidget>
+ </customwidgets>
+ <resources/>
+ <connections/>
+</ui>

+ 5 - 2
main.cpp

@@ -1,10 +1,13 @@
-#include "mainwindow.h"
+#include "ccsim.h"
 #include <QApplication>
+#include <QStyleFactory>
 
 int main(int argc, char *argv[])
 {
     QApplication a(argc, argv);
-    MainWindow w;
+    QApplication::setStyle(QStyleFactory::create("Fusion"));
+
+    CCSim w;
     w.show();
 
     return a.exec();

+ 3 - 1
mainwindow.cpp

@@ -36,7 +36,9 @@ MainWindow::MainWindow(QWidget *parent)
 
     connect(ui->actionLoadProgram, &QAction::triggered, this, [this](){
         auto fileName = QFileDialog::getOpenFileName(this, tr("Open Program"), "/c/", tr("Code Contest Program Files (*.ccp)"));
-        loadProgramFromFile(fileName, Qt::red, 10, 10);
+        if(!fileName.isEmpty()) {
+            loadProgramFromFile(fileName, Qt::red, 10, 10);
+        }
     });
 
     connect(ui->actionStart, &QAction::triggered, this, [this](){

+ 77 - 0
syntaxhighlighter.cpp

@@ -0,0 +1,77 @@
+#include "syntaxhighlighter.h"
+
+SyntaxHighlighter::SyntaxHighlighter(QTextDocument *parent)
+    : QSyntaxHighlighter(parent)
+{
+    HighlightingRule rule;
+
+    keywordFormat.setForeground(Qt::darkBlue);
+    keywordFormat.setFontWeight(QFont::Bold);
+    QStringList keywordPatterns;
+
+    keywordPatterns << "\\bCREATE\\b" << "\\bMOVE\\b" << "\\bDIE\\b" << "\\bTRANS\\b" << "\\bRTRANS\\b" << "\\bTURN\\b"
+                    << "\\bJUMP\\b" << "\\bAJUMP\\b" << "\\bBJUMP\\b"
+                    << "\\bSCAN\\b" << "\\bFARSCAN\\b"
+                    << "\\bSET\\b" << "\\bADD\\b" << "\\bSUB\\b" << "\\bMUL\\b" << "\\bDIV\\b" << "\\bMOD\\b" << "\\bMIN\\b" << "\\bMAX\\b" << "\\bRANDOM\\b"
+                    << "\\bIF\\b" << "\\bIFN\\b" << "\\bIFG\\b" << "\\bIFL\\b" << "\\bIFGE\\b" << "\\bIFLE\\b"
+                    << "\\bINIT\\b" << "\\bBREAK\\b" << "\\bRESUME\\b" << "\\bSEIZE\\b" << "\\bSLEEP\\b" << "\\bQUIT\\b"
+                       ;
+
+    foreach (const QString &pattern, keywordPatterns) {
+        rule.pattern = QRegExp(pattern);
+        rule.pattern.setCaseSensitivity(Qt::CaseInsensitive);
+        rule.format = keywordFormat;
+        highlightingRules.append(rule);
+    }
+
+    bankFormat.setFontWeight(QFont::Bold);
+    bankFormat.setForeground(Qt::white);
+    bankFormat.setBackground(Qt::darkGray);
+    rule.pattern = QRegExp("\\s*BANK.*");
+    rule.pattern.setCaseSensitivity(Qt::CaseInsensitive);
+    rule.format = bankFormat;
+    highlightingRules.append(rule);
+
+    singleLineCommentFormat.setForeground(Qt::darkGreen);
+    rule.pattern = QRegExp("//[^\n]*");
+    rule.format = singleLineCommentFormat;
+    highlightingRules.append(rule);
+
+    multiLineCommentFormat.setForeground(Qt::darkGreen);
+
+    commentStartExpression = QRegExp("/\\*");
+    commentEndExpression = QRegExp("\\*/");
+}
+
+void SyntaxHighlighter::highlightBlock(const QString &text)
+{
+    foreach (const HighlightingRule &rule, highlightingRules) {
+        QRegExp expression(rule.pattern);
+        int index = expression.indexIn(text);
+        while (index >= 0) {
+            int length = expression.matchedLength();
+            setFormat(index, length, rule.format);
+            index = expression.indexIn(text, index + length);
+        }
+    }
+
+    setCurrentBlockState(0);
+
+    int startIndex = 0;
+    if (previousBlockState() != 1)
+        startIndex = commentStartExpression.indexIn(text);
+
+    while (startIndex >= 0) {
+        int endIndex = commentEndExpression.indexIn(text, startIndex);
+        int commentLength;
+        if (endIndex == -1) {
+            setCurrentBlockState(1);
+            commentLength = text.length() - startIndex;
+        } else {
+            commentLength = endIndex - startIndex
+                            + commentEndExpression.matchedLength();
+        }
+        setFormat(startIndex, commentLength, multiLineCommentFormat);
+        startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
+    }
+}

+ 33 - 0
syntaxhighlighter.h

@@ -0,0 +1,33 @@
+#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) Q_DECL_OVERRIDE;
+
+private:
+    struct HighlightingRule
+    {
+        QRegExp pattern;
+        QTextCharFormat format;
+    };
+    QVector<HighlightingRule> highlightingRules;
+
+    QRegExp commentStartExpression;
+    QRegExp commentEndExpression;
+
+    QTextCharFormat keywordFormat;
+    QTextCharFormat bankFormat;
+    QTextCharFormat singleLineCommentFormat;
+    QTextCharFormat multiLineCommentFormat;
+};
+
+#endif // SYNTAXHIGHLIGHTER_H

+ 112 - 0
wgtcodeeditor.cpp

@@ -0,0 +1,112 @@
+#include "wgtcodeeditor.h"
+
+#include <QPainter>
+#include <QTextBlock>
+#include "wgtlinenumberarea.h"
+#include "syntaxhighlighter.h"
+
+WgtCodeEditor::WgtCodeEditor(QWidget *parent)
+    : QPlainTextEdit(parent)
+{
+    QFont font;
+    font.setFamily("Courier");
+    font.setStyleHint(QFont::Monospace);
+    font.setFixedPitch(true);
+    font.setPointSize(10);
+    setFont(font);
+
+    const int tabStop = 4;  // 4 characters
+    QFontMetrics metrics(font);
+    setTabStopWidth(tabStop * metrics.width(' '));
+
+    lineNumberArea = new WgtLineNumberArea(this);
+
+    new SyntaxHighlighter(document());
+
+    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();
+}
+
+int WgtCodeEditor::lineNumberAreaWidth()
+{
+    int digits = 1;
+    int max = qMax(1, blockCount());
+    while (max >= 10) {
+        max /= 10;
+        ++digits;
+    }
+
+    int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits;
+
+    return space;
+}
+
+void WgtCodeEditor::updateLineNumberAreaWidth(int /* newBlockCount */)
+{
+    setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
+}
+
+void WgtCodeEditor::updateLineNumberArea(const QRect &rect, int dy)
+{
+    if (dy)
+        lineNumberArea->scroll(0, dy);
+    else
+        lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
+
+    if (rect.contains(viewport()->rect()))
+        updateLineNumberAreaWidth(0);
+}
+
+void WgtCodeEditor::resizeEvent(QResizeEvent *e)
+{
+    QPlainTextEdit::resizeEvent(e);
+
+    QRect cr = contentsRect();
+    lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
+}
+
+void WgtCodeEditor::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 WgtCodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
+{
+    QPainter painter(lineNumberArea);
+    painter.fillRect(event->rect(), Qt::lightGray);
+    QTextBlock block = firstVisibleBlock();
+    int blockNumber = block.blockNumber();
+    int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
+    int bottom = top + (int) blockBoundingRect(block).height();
+    while (block.isValid() && top <= event->rect().bottom()) {
+        if (block.isVisible() && bottom >= event->rect().top()) {
+            QString number = QString::number(blockNumber + 1);
+            painter.setPen(Qt::black);
+            painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),
+                             Qt::AlignRight, number);
+        }
+
+        block = block.next();
+        top = bottom;
+        bottom = top + (int) blockBoundingRect(block).height();
+        ++blockNumber;
+    }
+}

+ 28 - 0
wgtcodeeditor.h

@@ -0,0 +1,28 @@
+#ifndef WGTCODEEDITOR_H
+#define WGTCODEEDITOR_H
+
+#include <QPlainTextEdit>
+
+class WgtCodeEditor : public QPlainTextEdit
+{
+    Q_OBJECT
+
+public:
+    WgtCodeEditor(QWidget *parent = 0);
+
+    void lineNumberAreaPaintEvent(QPaintEvent *event);
+    int lineNumberAreaWidth();
+
+protected:
+    void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE;
+
+private slots:
+    void updateLineNumberAreaWidth(int newBlockCount);
+    void highlightCurrentLine();
+    void updateLineNumberArea(const QRect &, int);
+
+private:
+    QWidget *lineNumberArea;
+};
+
+#endif // WGTCODEEDITOR_H

+ 15 - 0
wgtlinenumberarea.cpp

@@ -0,0 +1,15 @@
+#include "wgtlinenumberarea.h"
+
+WgtLineNumberArea::WgtLineNumberArea(WgtCodeEditor *editor)
+    : QWidget(editor)
+{
+    codeEditor = editor;
+}
+
+QSize WgtLineNumberArea::sizeHint() const {
+    return QSize(codeEditor->lineNumberAreaWidth(), 0);
+}
+
+void WgtLineNumberArea::paintEvent(QPaintEvent *event) {
+    codeEditor->lineNumberAreaPaintEvent(event);
+}

+ 23 - 0
wgtlinenumberarea.h

@@ -0,0 +1,23 @@
+#ifndef WGTLINENUMBERAREA_H
+#define WGTLINENUMBERAREA_H
+
+#include <QWidget>
+#include "wgtcodeeditor.h"
+
+class WgtLineNumberArea : public QWidget
+{
+    Q_OBJECT
+
+public:
+    WgtLineNumberArea(WgtCodeEditor *editor);
+
+    QSize sizeHint() const Q_DECL_OVERRIDE;
+
+protected:
+    void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
+
+private:
+    WgtCodeEditor *codeEditor;
+};
+
+#endif // WGTLINENUMBERAREA_H