1 /*
2  assoc_leaderboard_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 "assoc_leaderboard_presenter.h"
20 
21 namespace m8r {
22 
AssocLeaderboardPresenter(AssocLeaderboardView * view,OrlojPresenter * orloj)23 AssocLeaderboardPresenter::AssocLeaderboardPresenter(AssocLeaderboardView* view, OrlojPresenter* orloj)
24 {
25     this->view = view;
26     this->model = new AssocLeaderboardModel(this);
27     this->view->setModel(this->model);
28 
29     this->orloj = orloj;
30 
31     // ensure HTML cells rendering
32     HtmlDelegate* delegate = new HtmlDelegate();
33     this->view->setItemDelegate(delegate);
34 
35     QObject::connect(
36         view->selectionModel(),
37         SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
38         this,
39         SLOT(slotShowNote(const QItemSelection&, const QItemSelection&)));
40 
41     lastAssociations = nullptr;
42 }
43 
~AssocLeaderboardPresenter()44 AssocLeaderboardPresenter::~AssocLeaderboardPresenter()
45 {
46     if(lastAssociations) delete lastAssociations;
47 }
48 
slotShowNote(const QItemSelection & selected,const QItemSelection & deselected)49 void AssocLeaderboardPresenter::slotShowNote(const QItemSelection& selected, const QItemSelection& deselected)
50 {
51     Q_UNUSED(deselected);
52 
53     QModelIndexList indices = selected.indexes();
54     if(indices.size()) {
55         const QModelIndex& index = indices.at(0);
56         QStandardItem* item
57             = model->itemFromIndex(index);
58         // IMPROVE make my role constant
59         Note* note = item->data(Qt::UserRole + 1).value<Note*>();
60 
61         note->incReads();
62         note->makeDirty();
63 
64         orloj->showFacetNoteView(note);
65     } // else do nothing
66 }
67 
refresh(AssociatedNotes * associations)68 void AssocLeaderboardPresenter::refresh(AssociatedNotes* associations)
69 {
70     if(associations->getAssociations()->size()) {
71 
72         lastAssociations = associations;
73 
74         switch(associations->getSourceType()) {
75         case OUTLINE:
76             model->setTitle(QString::fromStdString(associations->getOutline()->getName()));
77             break;
78         case NOTE:
79             model->setTitle(QString::fromStdString(associations->getNote()->getName()));
80             break;
81         case WORD:
82             model->setTitle(QString::fromStdString(associations->getWord()));
83             break;
84         default:
85             model->setTitle(tr("Associations"));
86         }
87 
88         model->removeAllRows();
89         for(auto& i:*lastAssociations->getAssociations()) {
90             model->addRow(i.first, i.second);
91         }
92     } else {
93         model->removeAllRows();
94     }
95 
96     view->setVisible(true);
97     // do NOT hide leaderboard using view->setVisible(false); as it kills usability e.g. selected N in the tree can be hidden
98 }
99 
100 } // m8r namespace
101