1 /*
2 * %kadu copyright begin%
3 * Copyright 2010, 2011 Piotr Galiszewski (piotr.galiszewski@kadu.im)
4 * Copyright 2010 Wojciech Treter (juzefwt@gmail.com)
5 * Copyright 2010 Tomasz Rostański (rozteck@interia.pl)
6 * Copyright 2011, 2013, 2014 Bartosz Brachaczek (b.brachaczek@gmail.com)
7 * Copyright 2009, 2010, 2011, 2012, 2013, 2014 Rafał Przemysław Malinowski (rafal.przemyslaw.malinowski@gmail.com)
8 * %kadu copyright end%
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include <QtWidgets/QMenu>
25
26 #include "accounts/account-manager.h"
27 #include "buddies/buddy-set.h"
28 #include "buddies/buddy.h"
29 #include "gui/actions/action-context.h"
30 #include "gui/actions/action-description.h"
31 #include "gui/hot-key.h"
32 #include "icons/icons-manager.h"
33 #include "icons/kadu-icon.h"
34
35 #include "action.h"
36
Action(ActionDescription * description,ActionContext * context,QObject * parent)37 Action::Action(ActionDescription *description, ActionContext *context, QObject *parent) :
38 QAction(parent), Description(description), Context(context)
39 {
40 setText(Description->Text);
41 setCheckable(Description->Checkable);
42
43 connect(this, SIGNAL(changed()), this, SLOT(changedSlot()));
44 connect(this, SIGNAL(hovered()), this, SLOT(hoveredSlot()));
45 connect(this, SIGNAL(triggered(bool)), this, SLOT(triggeredSlot(bool)));
46
47 connect(context, SIGNAL(changed()), this, SLOT(checkState()));
48 }
49
~Action()50 Action::~Action()
51 {
52 emit aboutToBeDestroyed(this);
53
54 // we are real owner of this menu
55 if (menu())
56 {
57 delete menu();
58 setMenu(0);
59 }
60 }
61
setIconsManager(IconsManager * iconsManager)62 void Action::setIconsManager(IconsManager *iconsManager)
63 {
64 m_iconsManager = iconsManager;
65 }
66
init()67 void Action::init()
68 {
69 if (!Description->icon().isNull())
70 {
71 connect(m_iconsManager, SIGNAL(themeChanged()), this, SLOT(updateIcon()));
72 setIcon(Description->icon());
73 }
74
75 checkState();
76 }
77
context()78 ActionContext * Action::context()
79 {
80 return Context;
81 }
82
changedSlot()83 void Action::changedSlot()
84 {
85 if (isCheckable() && isChecked() && !isEnabled())
86 setChecked(false);
87
88 emit changed(this);
89 }
90
hoveredSlot()91 void Action::hoveredSlot()
92 {
93 emit hovered(this);
94 }
95
triggeredSlot(bool checked)96 void Action::triggeredSlot(bool checked)
97 {
98 emit triggered(this, checked);
99 }
100
checkState()101 void Action::checkState()
102 {
103 Description->updateActionState(this);
104 }
105
updateIcon()106 void Action::updateIcon()
107 {
108 setIcon(Description->icon());
109 }
110
setIcon(const KaduIcon & icon)111 void Action::setIcon(const KaduIcon &icon)
112 {
113 QAction::setIcon(m_iconsManager->iconByPath(icon));
114 }
115
disableEmptyContacts(Action * action)116 void disableEmptyContacts(Action *action)
117 {
118 action->setEnabled(!action->context()->contacts().isEmpty());
119 }
120
disableNoChat(Action * action)121 void disableNoChat(Action *action)
122 {
123 action->setEnabled(action->context()->chat() && !action->context()->buddies().isAnyTemporary());
124 }
125
126 #include "moc_action.cpp"
127