feat: add some Qt C++ codes

Just archived code to be removed immediately.
merge-requests/8/head
Nefo Fortressia 2021-10-15 13:36:26 +07:00
parent 1586a798d9
commit 628bfd13ac
5 changed files with 250 additions and 0 deletions

75
src/met/core-editor.cpp Normal file
View File

@ -0,0 +1,75 @@
#include "core-editor.h"
EchidnaCoreEditor::EchidnaCoreEditor(QWidget *parent) : QPlainTextEdit(parent) {
LineNumberArea *lineNumber = new LineNumberArea(this);
QObject::connect(this, &EchidnaCoreEditor::cursorPositionChanged, &EchidnaCoreEditor::highlightCurrentLine);
QObject::connect(this, &EchidnaCoreEditor::updateRequest, &EchidnaCoreEditor::updateLineNumberArea);
QObject::connect(this, &EchidnaCoreEditor::blockCountChanged, &EchidnaCoreEditor::updateLineNumberAreaWidth);
this->highlightCurrentLine();
this->updateLineNumberAreaWidth(0);
}
EchidnaCoreEditor::~EchidnaCoreEditor(){
}
int EchidnaCoreEditor::lineNumberAreaWidth(){
}
void EchidnaCoreEditor::lineNumberAreaPaintEvent(QPaintEvent *event){
}
void EchidnaCoreEditor::resizeEvent(QResizeEvent *e){
QPlainTextEdit::resizeEvent(e);
QRect contentsRect = this->contentsRect();
}
/**
*
* This function implements the functionaly that highlight the current line your cursor is currently on.
*
* TODO: Implement support for multi-line editing.
*
*/
void EchidnaCoreEditor::highlightCurrentLine(){
if(this->isReadOnly()){
return;
} else {
QList<QTextEdit::ExtraSelection> selections;
QTextEdit::ExtraSelection selection;
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
QBrush selectionFormatBrush = selection.format.foreground();
}
}
/*
the method `set_menubar` exists for reference `&components::echidna_editor::imp::EchidnaEditor`, but its trait bounds were not satisfied
method cannot be called on `&components::echidna_editor::imp::EchidnaEditor` due to unsatisfied trait bounds
note: the following trait bounds were not satisfied:
`components::echidna_editor::imp::EchidnaEditor: glib::IsA<gtk4::Application>`
which is required by `components::echidna_editor::imp::EchidnaEditor: gtk4::prelude::GtkApplicationExt`
`&components::echidna_editor::imp::EchidnaEditor: glib::IsA<gtk4::Application>`
which is required by `&components::echidna_editor::imp::EchidnaEditor: gtk4::prelude::GtkApplicationExt`
*/

58
src/met/core-editor.h Normal file
View File

@ -0,0 +1,58 @@
/*
Echidna Core Text Editor. This is the Monaco replacement for Echidna.
A lot of the code are ~~stolen~~ inspired from [Qt's Code Editor example](https://doc.qt.io/qt-5/qtwidgets-widgets-codeeditor-example.html), with added features to replicate the Visual Studio Code experience.
*/
#include <QtWidgets>
class LineNumberArea : public QWidget
{
public:
LineNumberArea(EchidnaCoreEditor *editor) : QWidget(editor), coreEditor(editor)
{
}
QSize sizeHint() const override
{
return QSize(coreEditor->lineNumberAreaWidth(), 0);
}
protected:
void paintEvent(QPaintEvent *event) override
{
coreEditor->lineNumberAreaPaintEvent(event);
}
EchidnaCoreEditor *coreEditor;
};
class EchidnaCoreEditor : public QPlainTextEdit
{
Q_OBJECT
public:
EchidnaCoreEditor(QWidget *parent = nullptr);
~EchidnaCoreEditor();
void lineNumberAreaPaintEvent(QPaintEvent *event);
int lineNumberAreaWidth();
protected:
void resizeEvent(QResizeEvent *event) override;
private slots:
void highlightCurrentLine();
void updateLineNumberAreaWidth(int newBlockCount);
void updateLineNumberArea(const QRect &rect, int dy);
LineNumberArea *lineNumber;
};

64
src/met/editor.cpp Normal file
View File

@ -0,0 +1,64 @@
#include "editor.h"
#include <ui_echidna.h>
#include <QtCore>
#include <algorithm>
#include <QtWidgets>
EchidnaEditor::EchidnaEditor(QWidget *parent) : QMainWindow(parent),
ui(new Ui::EchidnaEditor)
{
ui->setupUi(this);
this->setCentralWidget(ui->centralWidget);
QObject::connect(this, &EchidnaEditor::newFileOpened, &EchidnaEditor::handleNewFileOpened);
}
EchidnaEditor::~EchidnaEditor()
{
delete ui;
};
void EchidnaEditor::actionOpenFolder()
{
QString dir = QFileDialog::getExistingDirectory(this,
"Add a Folder to Workspace",
QDir::homePath(),
QFileDialog::ShowDirsOnly);
std::vector<QFileSystemModel>::iterator foundExistingOpenedFolders = std::find_if(this->folders->begin(), this->folders->end(),
[&](QFileSystemModel *iterator)
{
return iterator->rootPath() == dir;
});
if (this->folders->end() != foundExistingOpenedFolders)
{
}
else
{
QFileSystemModel *model = new QFileSystemModel;
model->setRootPath(dir);
}
}
void EchidnaEditor::actionOpenFile(){
QStringList files = QFileDialog::getOpenFileNames(this,
"Open Files",
QDir::homePath()
);
for(int i = 0; i < files.size(); ++i){
emit this->newFileOpened(files.at(i));
}
}
void EchidnaEditor::handleNewFileOpened(QString filename){
QFile file(filename);
}

39
src/met/editor.h Normal file
View File

@ -0,0 +1,39 @@
#include <QMainWindow>
#include <QtWidgets>
#include <vector>
namespace Ui
{
class EchidnaEditor;
}
class EchidnaEditor : public QMainWindow
{
Q_OBJECT
public:
explicit EchidnaEditor(QWidget *parent = nullptr);
~EchidnaEditor();
private slots:
void actionOpenFolder();
void actionAddFolderToWorkspace(QString folder);
void actionOpenFile();
void handleNewFileOpened(QString file);
signals:
void newFileOpened(QString file);
void newFolderOpened();
private:
Ui::EchidnaEditor *ui;
std::vector<QFileSystemModel> *folders;
std::vector<QFile> *files;
};

14
src/met/main.cpp Normal file
View File

@ -0,0 +1,14 @@
#include <QApplication>
#include "editor.h"
int main(int argc, char *argv[]){
QApplication EchidnaEditorApp(argc, argv);
EchidnaEditor Editor;
Editor.show();
EchidnaEditorApp.exec();
}