1 /*
2  outline_presenter.h     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 #ifndef M8RUI_ORLOJ_PRESENTER_H
20 #define M8RUI_ORLOJ_PRESENTER_H
21 
22 #include <iostream>
23 
24 #include "../../lib/src/debug.h"
25 #include "../../lib/src/mind/mind.h"
26 
27 #include <QtWidgets>
28 
29 #include "orloj_view.h"
30 #include "dashboard_presenter.h"
31 #include "organizer_presenter.h"
32 #include "tags_table_presenter.h"
33 #include "recent_notes_table_presenter.h"
34 #include "main_window_presenter.h"
35 #include "outlines_table_presenter.h"
36 #include "notes_table_presenter.h"
37 #include "outline_view_presenter.h"
38 #include "outline_header_view_presenter.h"
39 #include "outline_header_edit_presenter.h"
40 #include "note_view_presenter.h"
41 #include "note_edit_presenter.h"
42 #include "navigator_presenter.h"
43 
44 namespace m8r {
45 
46 class DashboardPresenter;
47 class OrganizerPresenter;
48 class TagCloudPresenter;
49 class NotePresenter;
50 class NoteViewPresenter;
51 class NoteEditPresenter;
52 class OutlineHeaderViewPresenter;
53 class OutlineHeaderEditPresenter;
54 class OutlineViewPresenter;
55 class OrlojView;
56 
57 enum OrlojPresenterFacets {
58     FACET_NONE,                   // 0
59     FACET_LIST_OUTLINES,          // 1
60     FACET_VIEW_OUTLINE,           // 2
61     FACET_VIEW_OUTLINE_HEADER,    // 3
62     FACET_EDIT_OUTLINE_HEADER,    // 4
63     FACET_VIEW_NOTE,              // 5
64     FACET_EDIT_NOTE,              // 6
65     FACET_FTS_VIEW_NOTE,          // 7
66     FACET_ORGANIZER,              // 8
67     FACET_TAG_CLOUD,              // 9
68     FACET_RECENT_NOTES,           // 10
69     FACET_NAVIGATOR,              // 11
70     FACET_DASHBOARD               // 12
71 };
72 
73 // aspect modifies facet
74 enum OrlojPresenterFacetAspect {
75     ASPECT_NONE,
76     ASPECT_LIVE_PREVIEW
77 };
78 
79 enum OrlojButtonRoles {
80     AUTOSAVE_ROLE,
81     DISCARD_ROLE,
82     EDIT_ROLE,
83     SAVE_ROLE,
84     INVALID_ROLE = -1
85 };
86 
87 class OrlojPresenter : public QObject
88 {
89     Q_OBJECT
90 
91 private:
92     MainWindowPresenter* mainPresenter;
93 
94     OrlojPresenterFacets activeFacet;
95     OrlojPresenterFacetAspect activeAspect;
96 
97     OrlojView* view;
98     Configuration& config;
99     Mind* mind;
100 
101     DashboardPresenter* dashboardPresenter;
102     OrganizerPresenter* organizerPresenter;
103     TagsTablePresenter* tagCloudPresenter;
104     OutlinesTablePresenter* outlinesTablePresenter;
105     RecentNotesTablePresenter* recentNotesTablePresenter;
106     OutlineViewPresenter* outlineViewPresenter;
107     OutlineHeaderViewPresenter* outlineHeaderViewPresenter;
108     OutlineHeaderEditPresenter* outlineHeaderEditPresenter;
109     NoteViewPresenter* noteViewPresenter;
110     NoteEditPresenter* noteEditPresenter;
111     NavigatorPresenter* navigatorPresenter;
112 
113     bool skipEditNoteCheck;
114 
115 public:
116     explicit OrlojPresenter(MainWindowPresenter* mainPresenter,
117                    OrlojView* view,
118                    Mind* mind);
119 
getMind()120     Mind* getMind() { return mind; }
121 
getView()122     OrlojView* getView() const { return view; }
getDashboard()123     DashboardPresenter* getDashboard() const { return dashboardPresenter; }
getOrganizer()124     OrganizerPresenter* getOrganizer() const { return organizerPresenter; }
getNavigator()125     NavigatorPresenter* getNavigator() const { return navigatorPresenter; }
getMainPresenter()126     MainWindowPresenter* getMainPresenter() const { return mainPresenter; }
getOutlinesTable()127     OutlinesTablePresenter* getOutlinesTable() const { return outlinesTablePresenter; }
getRecentNotesTable()128     RecentNotesTablePresenter* getRecentNotesTable() const { return recentNotesTablePresenter; }
getOutlineView()129     OutlineViewPresenter* getOutlineView() const { return outlineViewPresenter; }
getOutlineHeaderView()130     OutlineHeaderViewPresenter* getOutlineHeaderView() const { return outlineHeaderViewPresenter; }
getOutlineHeaderEdit()131     OutlineHeaderEditPresenter* getOutlineHeaderEdit() const { return outlineHeaderEditPresenter; }
getNoteView()132     NoteViewPresenter* getNoteView() const { return noteViewPresenter; }
getNoteEdit()133     NoteEditPresenter* getNoteEdit() const { return noteEditPresenter; }
134 
isFacetActive(const OrlojPresenterFacets facet)135     bool isFacetActive(const OrlojPresenterFacets facet) const { return activeFacet==facet;}
isFacetActiveOutlineOrNoteView()136     bool isFacetActiveOutlineOrNoteView() {
137         if(isFacetActive(OrlojPresenterFacets::FACET_VIEW_OUTLINE)
138              ||
139            isFacetActive(OrlojPresenterFacets::FACET_VIEW_NOTE)
140              ||
141            isFacetActive(OrlojPresenterFacets::FACET_VIEW_OUTLINE_HEADER))
142         {
143             return true;
144         } else {
145             return false;
146         }
147     }
isFacetActiveOutlineOrNoteEdit()148     bool isFacetActiveOutlineOrNoteEdit() {
149         if(isFacetActive(OrlojPresenterFacets::FACET_EDIT_NOTE)
150              ||
151            isFacetActive(OrlojPresenterFacets::FACET_EDIT_OUTLINE_HEADER))
152         {
153             return true;
154         } else {
155             return false;
156         }
157     }
158 
getFacet()159     OrlojPresenterFacets getFacet() const { return activeFacet; }
setFacet(OrlojPresenterFacets facet)160     void setFacet(OrlojPresenterFacets facet) {
161         onFacetChange(facet);
162         activeFacet = facet;
163     }
164 
getAspect()165     OrlojPresenterFacetAspect getAspect() const { return activeAspect; }
setAspect(OrlojPresenterFacetAspect aspect)166     void setAspect(OrlojPresenterFacetAspect aspect) { activeAspect = aspect; }
isAspectActive(OrlojPresenterFacetAspect aspect)167     bool isAspectActive(OrlojPresenterFacetAspect aspect) { return activeAspect == aspect; }
168 
169     /**
170      * @brief This method is invoked whenever a facet is changed i.e. it allows to perform desired actions.
171      */
172     void onFacetChange(const OrlojPresenterFacets targetFacet) const;
173 
174     void showFacetDashboard();
175     void showFacetOrganizer(const std::vector<Outline*>& outlines);
176     void showFacetTagCloud();
177     void showFacetOutlineList(const std::vector<Outline*>& outlines);
178     void showFacetRecentNotes(const std::vector<Note*>& notes);
179     void showFacetKnowledgeGraphNavigator();
180     void showFacetFtsResult(std::vector<Note*>* result);
181     void showFacetOutline(Outline* outline);
182     void showFacetNoteView();
183     void showFacetNoteView(Note* note);
184     void showFacetNoteEdit(Note* note);
185     void showFacetOutlineHeaderEdit(Outline* outline);
186 
187     bool applyFacetHoisting();
188 
189     void refreshLiveNotePreview();
190 
191     void fromOutlineHeaderEditBackToView(Outline* outline);
192     void fromNoteEditBackToView(Note* note);
193 
194 public slots:
195     void slotShowOutlines();
196     void slotShowSelectedOutline();
197     void slotShowOutline(const QItemSelection& selected, const QItemSelection& deselected);
198     void slotShowOutlineHeader();
199     void slotShowNote(const QItemSelection& selected, const QItemSelection& deselected);
200     void slotShowSelectedRecentNote();
201     void slotShowRecentNote(const QItemSelection& selected, const QItemSelection& deselected);
202     void slotShowSelectedTagRecallDialog();
203     void slotShowTagRecallDialog(const QItemSelection& selected, const QItemSelection& deselected);
204     void slotShowNavigator();
205     void slotShowNoteNavigator(Note* note);
206     void slotShowOutlineNavigator(Outline* outline);
207     void slotGetLinksForPattern(const QString& pattern);
208     void slotRefreshCurrentNotePreview();
209     void slotOutlinesTableSorted(int column);
210     void slotToggleFullOutlinePreview();
211     void slotEditStartLinkCompletion();
212 
213 signals:
214     void signalLinksForPattern(const QString& completionPrefix, std::vector<std::string>* links);
215     void signalLinksForHeaderPattern(const QString& completionPrefix, std::vector<std::string>* links);
216 
217 private:
218     bool avoidDataLossOnNoteEdit();
219 };
220 
221 } // m8r namespace
222 
223 #endif // M8RUI_ORLOJ_PRESENTER_H
224