diff --git a/EasyNotepad/etab.cpp b/EasyNotepad/etab.cpp index 3840e71..61e3d8d 100644 --- a/EasyNotepad/etab.cpp +++ b/EasyNotepad/etab.cpp @@ -13,9 +13,7 @@ #include #include -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; @@ -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 @@ -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); } @@ -331,3 +333,7 @@ void ETab::setAlign(int type){ break; } } + +bool ETab::fileExists() { + return file->exists(); +} diff --git a/EasyNotepad/etab.h b/EasyNotepad/etab.h index cf3cf80..0839e4e 100644 --- a/EasyNotepad/etab.h +++ b/EasyNotepad/etab.h @@ -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(); diff --git a/EasyNotepad/main.cpp b/EasyNotepad/main.cpp index 5f13838..9b20bfe 100644 --- a/EasyNotepad/main.cpp +++ b/EasyNotepad/main.cpp @@ -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(¶ms); w.setWindowTitle("EasyNotepad"); w.show(); return a.exec(); diff --git a/EasyNotepad/mainwindow.cpp b/EasyNotepad/mainwindow.cpp index 455b926..4a65311 100644 --- a/EasyNotepad/mainwindow.cpp +++ b/EasyNotepad/mainwindow.cpp @@ -20,7 +20,7 @@ #include #include -MainWindow::MainWindow(QWidget *parent) +MainWindow::MainWindow(QStringList* params, QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { @@ -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; } @@ -149,7 +154,7 @@ void MainWindow::on_actionSave_as_triggered() //Get selected tab ETab *selected = ui->tabs->findChild(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; } @@ -183,13 +188,25 @@ 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; } @@ -197,7 +214,7 @@ void MainWindow::showEvent(QShowEvent *event){ //Event thats triggered when user quits void MainWindow::closeEvent(QCloseEvent *event){ on_action_Exit_triggered(); - event->accept(); + event->ignore(); } //Other menu items @@ -242,6 +259,7 @@ void MainWindow::on_action_Exit_triggered() } on_actionClose_all_triggered(); + std::cout << "Bye!" << std::endl; QApplication::exit(0); } @@ -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()){ @@ -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" @@ -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(ui->tabs->currentWidget()->objectName()); + selected->setContent(o["content"].toString()); + } + } - remember = true; + if(arr.size() > 0) { + ui->actionRemember_quick_notes->setChecked(true); + } + } + } } } @@ -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 tabs = ui->tabs->findChildren() ; 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; @@ -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(); @@ -538,8 +581,8 @@ void MainWindow::changeTab(ACTION action, int argument){ void MainWindow::on_actionAbout_triggered() { - openTab("About"); + openTab("#About"); QString hardCodedAboutPage = "

EasyNotepad++

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++

Licence

EasyNotepad is licenced under the MIT-licence.


→ More info, see https://github.com/maurictg/EasyNotepadPlusPlus

"; ETab *selected = ui->tabs->findChild(ui->tabs->currentWidget()->objectName()); - selected->setText(hardCodedAboutPage, false); + selected->setContent(hardCodedAboutPage, false); } diff --git a/EasyNotepad/mainwindow.h b/EasyNotepad/mainwindow.h index 1eca702..7f4b073 100644 --- a/EasyNotepad/mainwindow.h +++ b/EasyNotepad/mainwindow.h @@ -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); @@ -74,7 +74,6 @@ private slots: void on_actionRight_triggered(); void on_actionCenter_triggered(); void on_actionJustify_triggered(); - void on_actionAbout_triggered(); private: @@ -82,6 +81,7 @@ private slots: QLabel *lblStatus; QLabel *lblClock; QString tempfile; + QStringList *params; void setFontOnSelected(const QTextCharFormat &format); void openTab(QString title); void updateActions(); diff --git a/EasyNotepad/mainwindow.ui b/EasyNotepad/mainwindow.ui index 4badcb6..65e2f82 100644 --- a/EasyNotepad/mainwindow.ui +++ b/EasyNotepad/mainwindow.ui @@ -175,6 +175,7 @@ + @@ -638,6 +639,18 @@ About + + + true + + + + :/icons/icons/idea_48px.png:/icons/icons/idea_48px.png + + + Remember quick notes + +