1 /*
2  * %kadu copyright begin%
3  * Copyright 2015 Rafał Przemysław Malinowski (rafal.przemyslaw.malinowski@gmail.com)
4  * %kadu copyright end%
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 as
8  * published by the Free Software Foundation; either version 2 of
9  * 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 
20 #include "collapse-action.h"
21 
22 #include "gui/actions/action-context.h"
23 #include "gui/actions/action.h"
24 
25 #include <QtWidgets/QTreeView>
26 
CollapseAction(QObject * parent)27 CollapseAction::CollapseAction(QObject *parent) :
28 		// using C++ initializers breaks Qt's lupdate
29 		ActionDescription(parent)
30 {
31 	setType(ActionDescription::TypeUserList);
32 	setName("collapseAction");
33 	setText(tr("Collapse"));
34 }
35 
~CollapseAction()36 CollapseAction::~CollapseAction()
37 {
38 }
39 
triggered(QWidget * widget,ActionContext * context,bool toggled)40 void CollapseAction::triggered(QWidget *widget, ActionContext *context, bool toggled)
41 {
42 	Q_UNUSED(widget)
43 	Q_UNUSED(toggled)
44 
45 	auto treeViewWidget = qobject_cast<QTreeView *>(context->widget());
46 	if (!treeViewWidget)
47 		return;
48 
49 	auto selectedIndexes = treeViewWidget->selectionModel()->selectedIndexes();
50 	for (auto &&selectedIndex : selectedIndexes)
51 		treeViewWidget->collapse(selectedIndex);
52 }
53 
updateActionState(Action * action)54 void CollapseAction::updateActionState(Action *action)
55 {
56 	action->setEnabled(false);
57 
58 	auto treeViewWidget = qobject_cast<QTreeView *>(action->context()->widget());
59 	if (!treeViewWidget)
60 		return;
61 
62 	auto selectedIndexes = treeViewWidget->selectionModel()->selectedIndexes();
63 	for (auto &&selectedIndex : selectedIndexes)
64 		if (treeViewWidget->model()->rowCount(selectedIndex) > 0)
65 		{
66 			action->setEnabled(true);
67 			return;
68 		}
69 }
70 
71 #include "moc_collapse-action.cpp"
72