1 /*
2  tags_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 "tags_table_presenter.h"
20 
21 namespace m8r {
22 
23 using namespace std;
24 
TagsTablePresenter(TagsTableView * view,HtmlOutlineRepresentation * htmlRepresentation)25 TagsTablePresenter::TagsTablePresenter(TagsTableView* view, HtmlOutlineRepresentation* htmlRepresentation)
26 {
27     this->view = view;
28     this->model = new TagsTableModel(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 
~TagsTablePresenter()38 TagsTablePresenter::~TagsTablePresenter()
39 {
40 }
41 
refresh(const map<const Tag *,int> & tags)42 void TagsTablePresenter::refresh(const map<const Tag*, int>& tags)
43 {
44     model->removeAllRows();
45     if(tags.size()) {
46         for(const auto& t:tags) {
47             model->addRow(t.first, t.second);
48         }
49     }
50 
51     view->sortByColumn(1, Qt::SortOrder::DescendingOrder);
52 
53     this->view->setCurrentIndex(this->model->index(0, 0));
54     this->view->setFocus();
55 }
56 
getCurrentRow() const57 int TagsTablePresenter::getCurrentRow() const
58 {
59     QModelIndexList indexes = view->selectionModel()->selection().indexes();
60     for(int i=0; i<indexes.count(); i++) {
61         return indexes.at(i).row();
62     }
63     return NO_ROW;
64 }
65 
66 } // m8r namespace
67