1 /*
2  recent_notes_table_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 "recent_notes_table_presenter.h"
20 
21 namespace m8r {
22 
23 using namespace std;
24 
RecentNotesTablePresenter(RecentNotesTableView * view,HtmlOutlineRepresentation * htmlRepresentation)25 RecentNotesTablePresenter::RecentNotesTablePresenter(RecentNotesTableView* view, HtmlOutlineRepresentation* htmlRepresentation)
26 {
27     this->view = view;
28     this->model = new RecentNotesTableModel(this, htmlRepresentation);
29     this->view->setModel(this->model);
30 
31     // ensure HTML cells rendering
32     HtmlDelegate* delegate = new HtmlDelegate();
33     // IMPROVE implement delegates by type e.g. timestamp an reuse them across views
34     //this->view->setItemDelegateForColumn(delegate);
35     this->view->setItemDelegate(delegate);
36 }
37 
~RecentNotesTablePresenter()38 RecentNotesTablePresenter::~RecentNotesTablePresenter()
39 {
40 }
41 
refresh(const vector<Note * > & notes)42 void RecentNotesTablePresenter::refresh(const vector<Note*>& notes)
43 {
44     model->removeAllRows();
45     if(notes.size()) {
46         int uiLimit = Configuration::getInstance().getRecentNotesUiLimit();
47         for(Note* n:notes) {
48             if(uiLimit) uiLimit--; else break;
49             model->addRow(n);
50         }
51     }
52 
53     // order by read timestamp
54     view->sortByColumn(4, Qt::SortOrder::DescendingOrder);
55 
56     this->view->setCurrentIndex(this->model->index(0, 0));
57     this->view->setFocus();
58 }
59 
60 
getCurrentRow() const61 int RecentNotesTablePresenter::getCurrentRow() const
62 {
63     QModelIndexList indexes = view->selectionModel()->selection().indexes();
64     for(int i=0; i<indexes.count(); i++) {
65         return indexes.at(i).row();
66     }
67     return NO_ROW;
68 }
69 
70 } // m8r namespace
71