Skip to content

Commit

Permalink
Now accepts CLI arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
maurictg committed Apr 2, 2020
1 parent 996da9b commit b9cbd9a
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 33 deletions.
16 changes: 11 additions & 5 deletions EasyNotepad/etab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
#include <QTextListFormat>
#include <QTextList>

ETab::ETab(MainWindow *mainwindow, QWidget *parent) :
QWidget(parent),
ui(new Ui::ETab)
ETab::ETab(MainWindow *mainwindow, QWidget *parent) : QWidget(parent), ui(new Ui::ETab)
{
ui->setupUi(this);
this->main = mainwindow;
Expand Down Expand Up @@ -73,11 +71,15 @@ void ETab::timerTick(){
* Logic
*/

void ETab::setText(QString text, bool doSave) {
void ETab::setContent(QString text, bool doSave) {
ui->textEdit->setHtml(text);
this->dontSave = !doSave;
}

QString ETab::getContent() {
return ui->textEdit->toHtml();
}

//Set font format on selected tab
void ETab::setFontFormat(const QTextCharFormat &format){
//Get cursor and set charFormat
Expand Down Expand Up @@ -157,7 +159,7 @@ void ETab::useFile(bool write){

void ETab::openFile() { this->useFile(false);}
void ETab::saveFile() {
if(changes) //Maybe exist check? QFile(Info) as private class variable?
if(changes)
this->useFile(true);
}

Expand Down Expand Up @@ -331,3 +333,7 @@ void ETab::setAlign(int type){
break;
}
}

bool ETab::fileExists() {
return file->exists();
}
4 changes: 3 additions & 1 deletion EasyNotepad/etab.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ class ETab : public QWidget
void setStyle(int type);
void setAlign(int type);
void focus();
void setText(QString text, bool doSave = true);
void setContent(QString text, bool doSave = true);
QString getContent();
bool fileExists();

private slots:
void timerTick();
Expand Down
13 changes: 12 additions & 1 deletion EasyNotepad/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,18 @@ int main(int argc, char *argv[])
}
#endif

MainWindow w;
//Get parameters from CLI
QStringList params;
for(int i = 0; i < argc; i++){
if(i == 0)
continue;

std::cout << argv[i] << std::endl;
params << QString::fromLocal8Bit(argv[i]);
}


MainWindow w(&params);
w.setWindowTitle("EasyNotepad");
w.show();
return a.exec();
Expand Down
91 changes: 67 additions & 24 deletions EasyNotepad/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <QJsonDocument>
#include <QJsonArray>

MainWindow::MainWindow(QWidget *parent)
MainWindow::MainWindow(QStringList* params, QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
Expand Down Expand Up @@ -53,10 +53,15 @@ MainWindow::MainWindow(QWidget *parent)
this->donotload = false;

tempfile = QDir::cleanPath(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) + QDir::separator() + "temp.enff");

this->params = params;
}

MainWindow::~MainWindow()
{
if(params != nullptr) {
params->clear();
}
delete ui;
}

Expand Down Expand Up @@ -149,7 +154,7 @@ void MainWindow::on_actionSave_as_triggered()
//Get selected tab
ETab *selected = ui->tabs->findChild<ETab *>(ui->tabs->currentWidget()->objectName());
if(selected == NULL){
std::cout << "Error: selected tab is NULL" << std::endl;
std::cerr << "Error: selected tab is NULL" << std::endl;
return;
}

Expand Down Expand Up @@ -183,21 +188,33 @@ void MainWindow::showEvent(QShowEvent *event){

//Try to load temp file
this->index = 0;
this->loadTempFile();
if(remember){
ui->actionRemeber_opened_files->setChecked(true);
} else{
//Open new file
this->openTab("New file");

if(params->size() > 0) {
for(int i = 0; i < params->size(); i++) {
QString name = params->at(i);
if(name != "#Close") {
openTab(name);
}
}
delete params;
} else {
this->loadTempFile();
if(remember){
ui->actionRemeber_opened_files->setChecked(true);
} else{
//Open new file
this->openTab("New file");
}
}

statusBar()->clearMessage();
this->donotload = true;
}

//Event thats triggered when user quits
void MainWindow::closeEvent(QCloseEvent *event){
on_action_Exit_triggered();
event->accept();
event->ignore();
}

//Other menu items
Expand Down Expand Up @@ -242,6 +259,7 @@ void MainWindow::on_action_Exit_triggered()
}

on_actionClose_all_triggered();

std::cout << "Bye!" << std::endl;
QApplication::exit(0);
}
Expand Down Expand Up @@ -304,9 +322,6 @@ void MainWindow::dropEvent(QDropEvent *event){
void MainWindow::loadTempFile(){
remember = false;

//Create list
QJsonArray files;

//Load temp file
QFile f(tempfile);
if(f.exists()){
Expand All @@ -324,7 +339,14 @@ void MainWindow::loadTempFile(){
//Read document to object
QJsonObject json = doc.object();
if(json.contains("files") && json["files"].isArray()) {
files = json["files"].toArray();
QJsonArray files = json["files"].toArray();
if(files.count() > 0){
for (int i = 0; i < files.size(); i++) {
openTab(files[i].toString());
}

remember = true;
}
}

//Screen resolution and other settings. Maybe object "settings"
Expand All @@ -339,15 +361,23 @@ void MainWindow::loadTempFile(){
MainWindow::setMaximumSize(16777215,16777215);
}
}
}
}

if(files.count() > 0){
for (int i = 0; i < files.size(); i++) {
openTab(files[i].toString());
}
if(json.contains("editors") && json["editors"].isArray()) {
QJsonArray arr = json["editors"].toArray();
for(int i = 0; i < arr.size(); i++) {
if(arr[i].isObject()) {
QJsonObject o = arr[i].toObject();
openTab(QString("Quick note #%1").arg(i+1));
ETab *selected = ui->tabs->findChild<ETab *>(ui->tabs->currentWidget()->objectName());
selected->setContent(o["content"].toString());
}
}

remember = true;
if(arr.size() > 0) {
ui->actionRemember_quick_notes->setChecked(true);
}
}
}
}
}

Expand All @@ -362,10 +392,22 @@ void MainWindow::saveTempFile(){
return;
}

QJsonArray files;
QJsonArray files; //Files to be reloaded from disk
QJsonArray editors; //Open editors that will be saved

QList<ETab*> tabs = ui->tabs->findChildren<ETab*>() ;
for (ETab* t : tabs) {
files.append(t->getFileName());
if(t->fileExists()) {
files.append(t->getFileName());
} else {
if(ui->actionRemember_quick_notes->isChecked() && t->getFileName() != "#About") {
QJsonObject editor;
editor["content"] = t->getContent();
editors.append(editor);
t->setContent("", false); //Trick to not save file
delete t; //Close tab
}
}
}

QJsonArray resolution;
Expand All @@ -375,6 +417,7 @@ void MainWindow::saveTempFile(){
QJsonObject object;
object["files"] = files;
object["resolution"] = resolution;
object["editors"] = editors;

QJsonDocument doc(object);
QString res = doc.toJson();
Expand Down Expand Up @@ -538,8 +581,8 @@ void MainWindow::changeTab(ACTION action, int argument){

void MainWindow::on_actionAbout_triggered()
{
openTab("About");
openTab("#About");
QString hardCodedAboutPage = "<h1>EasyNotepad++</h1><p>EasyNotepad++ is an richtext editor for Windows and Linux. It supports HTML, Markdown, and plain text files. It is also able to export a file to an odf file. It is made with QT and C++</p><h1>Licence</h1><p>EasyNotepad is licenced under the MIT-licence.</p><br/><h4>&rarr;&nbsp;More info, see <a href=\"https://github.com/maurictg/EasyNotepadPlusPlus\">https://github.com/maurictg/EasyNotepadPlusPlus</a></h4>";
ETab *selected = ui->tabs->findChild<ETab *>(ui->tabs->currentWidget()->objectName());
selected->setText(hardCodedAboutPage, false);
selected->setContent(hardCodedAboutPage, false);
}
4 changes: 2 additions & 2 deletions EasyNotepad/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class MainWindow : public QMainWindow
Q_OBJECT

public:
MainWindow(QWidget *parent = nullptr);
MainWindow(QStringList* params, QWidget *parent = nullptr);
~MainWindow();
void updateStatusLabel(int line, int col);
void updateActions(const QTextCharFormat &format);
Expand Down Expand Up @@ -74,14 +74,14 @@ private slots:
void on_actionRight_triggered();
void on_actionCenter_triggered();
void on_actionJustify_triggered();

void on_actionAbout_triggered();

private:
Ui::MainWindow *ui;
QLabel *lblStatus;
QLabel *lblClock;
QString tempfile;
QStringList *params;
void setFontOnSelected(const QTextCharFormat &format);
void openTab(QString title);
void updateActions();
Expand Down
13 changes: 13 additions & 0 deletions EasyNotepad/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@
<addaction name="actionStay_topmost"/>
<addaction name="separator"/>
<addaction name="actionRemeber_opened_files"/>
<addaction name="actionRemember_quick_notes"/>
<addaction name="separator"/>
<addaction name="action_Exit"/>
<addaction name="actionForce_Quit"/>
Expand Down Expand Up @@ -638,6 +639,18 @@
<string>About</string>
</property>
</action>
<action name="actionRemember_quick_notes">
<property name="checkable">
<bool>true</bool>
</property>
<property name="icon">
<iconset resource="resources.qrc">
<normaloff>:/icons/icons/idea_48px.png</normaloff>:/icons/icons/idea_48px.png</iconset>
</property>
<property name="text">
<string>Remember quick notes</string>
</property>
</action>
</widget>
<resources>
<include location="resources.qrc"/>
Expand Down

0 comments on commit b9cbd9a

Please sign in to comment.