Statistics
| Branch: | Tag: | Revision:

root / kadu-core / gui / actions / action.cpp @ f38188d2

History | View | Annotate | Download (3.6 kB)

1
/*
2
 * %kadu copyright begin%
3
 * Copyright 2009, 2010 RafaƂ 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 "accounts/account-manager.h"
21
#include "buddies/buddy.h"
22
#include "buddies/buddy-set.h"
23
#include "gui/hot-key.h"
24
#include "gui/windows/main-window.h"
25
#include "icons-manager.h"
26
27
#include "action.h"
28
29
Action::Action(ActionDescription *description, MainWindow *parent) :
30
                QAction(parent), Description(description)
31
{
32
        setText(Description->Text);
33
34
        if (Description->Checkable)
35
        {
36
                OnText = Description->CheckedText;
37
                OffText = Description->Text;
38
        }
39
40
        if (!Description->IconName.isEmpty())
41
        {
42
                connect(IconsManager::instance(), SIGNAL(themeChanged()), this, SLOT(updateIcon()));
43
44
                if (Description->Checkable)
45
                {
46
                        OnIcon = IconsManager::instance()->loadIcon(Description->IconName);
47
                        OffIcon = IconsManager::instance()->loadIcon(Description->IconName + "_off");
48
49
                        setIcon(OffIcon);
50
                }
51
                else
52
                        setIcon(IconsManager::instance()->loadIcon(Description->IconName));
53
        }
54
55
        setCheckable(Description->Checkable);
56
57
        connect(this, SIGNAL(changed()), this, SLOT(changedSlot()));
58
        connect(this, SIGNAL(hovered()), this, SLOT(hoveredSlot()));
59
        connect(this, SIGNAL(toggled(bool)), this, SLOT(toggledSlot(bool)));
60
        connect(this, SIGNAL(triggered(bool)), this, SLOT(triggeredSlot(bool)));
61
62
        checkState();
63
}
64
65
Action::~Action()
66
{
67
}
68
69
Contact Action::contact()
70
{
71
        ContactSet contactSet = contacts();
72
        if (1 != contactSet.count())
73
                return Contact::null;
74
        else
75
                return *contactSet.begin();
76
}
77
78
ContactSet Action::contacts()
79
{
80
        MainWindow *kaduMainWindow = dynamic_cast<MainWindow *>(parent());
81
        if (kaduMainWindow)
82
                return kaduMainWindow->contacts();
83
        else
84
                return ContactSet();
85
}
86
87
Buddy Action::buddy()
88
{
89
        BuddySet buddySet = buddies();
90
        if (1 != buddySet.count())
91
                return Buddy::null;
92
        else
93
                return *buddySet.begin();
94
}
95
96
BuddySet Action::buddies()
97
{
98
        MainWindow *kaduMainWindow = dynamic_cast<MainWindow *>(parent());
99
        if (kaduMainWindow)
100
                return kaduMainWindow->buddies();
101
        else
102
                return BuddySet();
103
}
104
105
void Action::changedSlot()
106
{
107
        emit changed(this);
108
}
109
110
void Action::hoveredSlot()
111
{
112
        emit hovered(this);
113
}
114
115
void Action::toggledSlot(bool checked)
116
{
117
        if (checked)
118
        {
119
                if (!OnText.isEmpty())
120
                        setText(OnText);
121
                if (!OnIcon.isNull())
122
                        setIcon(OnIcon);
123
        }
124
        else
125
        {
126
                if (!OffText.isEmpty())
127
                        setText(OffText);
128
                if (!OffIcon.isNull())
129
                        setIcon(OffIcon);
130
        }
131
}
132
133
void Action::triggeredSlot(bool checked)
134
{
135
        emit triggered(this, checked);
136
}
137
138
void Action::checkState()
139
{
140
        if (Description->EnableCallback)
141
                (*Description->EnableCallback)(this);
142
}
143
144
void Action::updateIcon()
145
{
146
        if (Description->Checkable)
147
        {
148
                OnIcon = IconsManager::instance()->loadIcon(Description->IconName);
149
                OffIcon = IconsManager::instance()->loadIcon(Description->IconName + "_off");
150
151
                toggledSlot(isChecked());
152
        }
153
        else
154
                setIcon(IconsManager::instance()->loadIcon(Description->IconName));
155
}
156
157
void disableEmptyContacts(Action *action)
158
{
159
        action->setEnabled(!action->contacts().isEmpty());
160
}