1 /*
2  outlines_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 "outlines_table_presenter.h"
20 
21 using namespace std;
22 
23 namespace m8r {
24 
OutlinesTablePresenter(OutlinesTableView * view,HtmlOutlineRepresentation * htmlRepresentation)25 OutlinesTablePresenter::OutlinesTablePresenter(OutlinesTableView* view, HtmlOutlineRepresentation* htmlRepresentation)
26 {
27     this->view = view;
28     this->model = new OutlinesTableModel(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 
refresh(const vector<Outline * > & outlines)38 void OutlinesTablePresenter::refresh(const vector<Outline*>& outlines)
39 {
40     model->removeAllRows();
41     if(outlines.size()) {
42         for(Outline* outline:outlines) {
43             model->addRow(outline);
44         }
45 
46         view->sortByColumn(
47             Configuration::getInstance().getUiOsTableSortColumn(),
48             Configuration::getInstance().isUiOsTableSortOrder()?Qt::SortOrder::AscendingOrder:Qt::SortOrder::DescendingOrder
49         );
50 
51         this->view->setCurrentIndex(this->model->index(0, 0));
52         this->view->setFocus();
53     }
54 }
55 
getCurrentRow() const56 int OutlinesTablePresenter::getCurrentRow() const
57 {
58     QModelIndexList indexes = view->selectionModel()->selection().indexes();
59     for(int i=0; i<indexes.count(); i++) {
60         return indexes.at(i).row();
61     }
62     return NO_ROW;
63 }
64 
65 } // m8r namespace
66