1 /***************************************************************************
2  *   Copyright (C) 2021 by Abderrahman Taha                                *
3  *                                                                         *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
19  ***************************************************************************/
20 #include "editor.h"
21 #include <QFile>
22 #include <QFileDialog>
23 #include <QMessageBox>
24 #include <QTextStream>
25 
editor(QWidget * parent)26 editor::editor(QWidget *parent) : QMainWindow(parent), ui(new Ui::editor)
27 {
28     this->setWindowFlags(Qt::WindowStaysOnTopHint);
29     ui->setupUi(this);
30 }
~editor()31 editor::~editor()
32 {
33     delete ui;
34 }
about()35 void editor::about()
36 {
37     QMessageBox::about(this, tr("About Syntax Highlighter"),
38                        tr("<p>The <b>Syntax Highlighter</b> example shows how "
39                           "to perform simple syntax highlighting by subclassing "
40                           "the QSyntaxHighlighter class and describing "
41                           "highlighting rules using regular expressions.</p>"));
42 }
newFile()43 void editor::newFile()
44 {
45     ui->textEdit->clear();
46 }
openFile(const QString & path)47 void editor::openFile(const QString &path)
48 {
49     QString fileName = path;
50     // if (fileName.isNull())
51     fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "",
52                                             "Files (*.js *.*)");
53     if (!fileName.isEmpty())
54     {
55         QFile file(fileName);
56         if (file.open(QFile::ReadOnly | QFile::Text))
57             ui->textEdit->setPlainText(file.readAll());
58     }
59 }
setupEditor()60 void editor::setupEditor()
61 {
62     QFont font;
63     font.setFamily("Courier");
64     font.setFixedPitch(true);
65     font.setPointSize(10);
66 
67     ui->textEdit = new QTextEdit;
68     ui->textEdit->setFont(font);
69     QFile file("mainwindow.h");
70     if (file.open(QFile::ReadOnly | QFile::Text))
71         ui->textEdit->setPlainText(file.readAll());
72 }
on_actionOpen_triggered()73 void editor::on_actionOpen_triggered()
74 {
75     openFile("");
76 }
save()77 void editor::save()
78 {
79     if (filename.isEmpty())
80     {
81         saveAs();
82         return;
83     }
84     QString text = ui->textEdit->toPlainText();
85     QFile f(filename);
86     if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
87     {
88         statusBar()->showMessage(tr("Could not write to %1").arg(filename), 2000);
89         return;
90     }
91 
92     QTextStream t(&f);
93     t << text;
94     f.close();
95     statusBar()->showMessage(tr("File %1 saved").arg(filename), 2000);
96 }
saveAs()97 void editor::saveAs()
98 {
99     QString fn = QFileDialog::getSaveFileName(this, tr("Save file"), "",
100                  tr("All Files (*.*)"));
101 
102     if (!fn.isEmpty())
103     {
104         filename = fn;
105         save();
106     }
107     else
108     {
109         statusBar()->showMessage(tr("Saving aborted"), 2000);
110     }
111     workfile = fn;
112 }
on_actionNew_triggered()113 void editor::on_actionNew_triggered()
114 {
115     newFile();
116 }
on_actionAbout_triggered()117 void editor::on_actionAbout_triggered()
118 {
119     about();
120 }
on_actionSave_triggered()121 void editor::on_actionSave_triggered()
122 {
123     save();
124 }
on_actionSave_As_triggered()125 void editor::on_actionSave_As_triggered()
126 {
127     saveAs();
128 }
on_actionExit_triggered()129 void editor::on_actionExit_triggered()
130 {
131     this->close();
132 }
on_actionUndo_triggered()133 void editor::on_actionUndo_triggered()
134 {
135     ui->textEdit->undo();
136 }
on_actionRedo_triggered()137 void editor::on_actionRedo_triggered()
138 {
139     ui->textEdit->redo();
140 }
on_actionCut_triggered()141 void editor::on_actionCut_triggered()
142 {
143     ui->textEdit->cut();
144 }
on_actionCopy_triggered()145 void editor::on_actionCopy_triggered()
146 {
147     ui->textEdit->copy();
148 }
149 
on_actionAbout_MathMod_triggered()150 void editor::on_actionAbout_MathMod_triggered()
151 {
152     ab.show();
153 }
on_actionPaste_triggered()154 void editor::on_actionPaste_triggered()
155 {
156     ui->textEdit->paste();
157 }
158