1 /*
2  note_edit_presenter.cpp     MindForger thinking notebook
3 
4  Copyright (C) 2016-2020 Martin Dvorak <martin.dvorak@mindforger.com>
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License
8  as published by the Free Software Foundation; either version 2
9  of the License, or (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #include "note_edit_presenter.h"
20 
21 #include "look_n_feel.h"
22 
23 using namespace std;
24 
25 namespace m8r {
26 
NoteEditPresenter(NoteEditView * view,MainWindowPresenter * mwp,QObject * parent)27 NoteEditPresenter::NoteEditPresenter(
28         NoteEditView* view,
29         MainWindowPresenter* mwp,
30         QObject* parent) : QObject(parent)
31 {
32     this->view = view;
33     this->mwp = mwp;
34 
35     view->setEditorShowLineNumbers(Configuration::getInstance().isUiEditorShowLineNumbers());
36     view->setStatusBar(mwp->getStatusBar()->getView());
37 
38     noteEditDialog
39         = new NoteEditDialog{mwp->getMind()->remind().getOntology(), view};
40     this->view->setNoteEditDialog(noteEditDialog);
41 
42     // signals
43     QObject::connect(
44         view, SIGNAL(signalSaveNote()),
45         this, SLOT(slotSaveNote()));
46     QObject::connect(
47         view, SIGNAL(signalCloseEditor()),
48         this, SLOT(slotCloseEditor()));
49     QObject::connect(
50         view, SIGNAL(signalSaveAndCloseEditor()),
51         this, SLOT(slotSaveAndCloseEditor()));
52 }
53 
~NoteEditPresenter()54 NoteEditPresenter::~NoteEditPresenter()
55 {
56 }
57 
setNote(Note * note)58 void NoteEditPresenter::setNote(Note* note)
59 {
60     mwp->getOrloj()->getOutlineView()->getAssocLeaderboard()->getView()->setVisible(false);
61 
62     this->currentNote = note;
63     string mdDescription{};
64     mwp->getMarkdownRepresentation()->toDescription(note, &mdDescription);
65 
66     view->setNote(note, mdDescription);
67 }
68 
slotKeyPressed()69 void NoteEditPresenter::slotKeyPressed()
70 {
71     mwp->getMind()->associate();
72 }
73 
slotCloseEditor()74 void NoteEditPresenter::slotCloseEditor()
75 {
76     mwp->getOrloj()->fromNoteEditBackToView(currentNote);
77 }
78 
slotSaveAndCloseEditor()79 void NoteEditPresenter::slotSaveAndCloseEditor()
80 {
81     slotSaveNote();
82     mwp->getOrloj()->fromNoteEditBackToView(currentNote);
83 }
84 
slotSaveNote()85 void NoteEditPresenter::slotSaveNote()
86 {
87     // set UI data to current note
88     if(currentNote) {
89         string name{"Note"};
90         if(!view->getName().isEmpty()) {
91             name.assign(view->getName().toStdString());
92         }
93 
94         // ensure autolinking indices are updated on N rename
95         mwp->getMind()->autolinkUpdate(currentNote->getName(), view->getName().toStdString());
96 
97         currentNote->setName(name);
98 
99         if(!view->isDescriptionEmpty()) {
100             string s{view->getDescription().toStdString()};
101             //MF_DEBUG("- BEGIN N description -" << endl << s << "- END N description -" << endl);
102             vector<string*> d{};
103             mwp->getMarkdownRepresentation()->description(&s, d);
104             currentNote->setDescription(d);
105         } else {
106             currentNote->clearDescription();
107         }
108 
109         // Note metada (type, tags, progress, deadline) are set by Note edit dialog on it's close
110         // (if user doesn't open dialog, nothing is blindly saved there & here)
111 
112         currentNote->makeModified();
113 
114         // remember
115         mwp->getMind()->remember(currentNote->getOutlineKey());
116         mwp->getStatusBar()->showInfo(tr("Note '%1' saved").arg(QString::fromStdString(currentNote->getName())));
117     } else {
118         mwp->getStatusBar()->showError(tr("Attempt to save data from UI to Note, but no Note is set."));
119     }
120 }
121 
122 } // namespace m8r
123