File: action.cpp

package info (click to toggle)
cantata 3.3.1.ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 38,300 kB
  • sloc: cpp: 105,773; perl: 1,366; xml: 723; python: 139; lex: 110; sh: 105; yacc: 78; makefile: 8
file content (158 lines) | stat: -rw-r--r-- 4,843 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/***************************************************************************
 *   Copyright (C) 2005-09 by the Quassel Project                          *
 *   devel@quassel-irc.org                                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************
 * Parts of this implementation are taken from KDE's kaction.cpp           *
 ***************************************************************************/

#include "action.h"
#include "config.h"
#include "gtkstyle.h"
#include "utils.h"
#include <QApplication>
#include <QKeySequence>

Action::Action(QObject* parent)
	: QAction(parent)
{
	init();
}

Action::Action(const QString& text, QObject* parent, const QObject* receiver, const char* slot, const QKeySequence& shortcut)
	: QAction(parent)
{
	init();
	setText(text);
	setShortcut(shortcut);
	if (receiver && slot)
		connect(this, SIGNAL(triggered()), receiver, slot);
}

Action::Action(const QIcon& icon, const QString& text, QObject* parent, const QObject* receiver, const char* slot, const QKeySequence& shortcut)
	: QAction(parent)
{
	init();
	setIcon(icon);
	setText(text);
	setShortcut(shortcut);
	if (receiver && slot)
		connect(this, SIGNAL(triggered()), receiver, slot);
}

void Action::initIcon(QAction* act)
{
	if (GtkStyle::isActive() && act) {
		act->setIconVisibleInMenu(false);
	}
}

static const char* constPlainToolTipProperty = "plain-tt";

void Action::updateToolTip(QAction* act)
{
	if (!act) {
		return;
	}
	QKeySequence sc = act->shortcut();
	if (sc.isEmpty()) {
		QString tt = act->property(constPlainToolTipProperty).toString();
		if (!tt.isEmpty()) {
			act->setToolTip(tt);
			act->setProperty(constPlainToolTipProperty, QString());
		}
	}
	else {
		QString tt = act->property(constPlainToolTipProperty).toString();
		if (tt.isEmpty()) {
			tt = act->toolTip();
			act->setProperty(constPlainToolTipProperty, tt);
		}
		act->setToolTip(QString::fromLatin1("%1 <span style=\"color: gray; font-size: small\">%2</span>")
		                        .arg(tt)
		                        .arg(sc.toString(QKeySequence::NativeText)));
	}
}

static const char* constSettingsText = "tt-for-settings";

QString Action::settingsText(QAction* act)
{
	return act->property(constSettingsText).isValid()
			? act->property(constSettingsText).toString()
			: Utils::stripAcceleratorMarkers(act->text());
}

void Action::init()
{
	connect(this, SIGNAL(triggered(bool)), this, SLOT(slotTriggered()));

	setProperty("isShortcutConfigurable", true);
}

void Action::slotTriggered()
{
	emit triggered(QApplication::mouseButtons(), QApplication::keyboardModifiers());
}

bool Action::isShortcutConfigurable() const
{
	return property("isShortcutConfigurable").toBool();
}

void Action::setShortcutConfigurable(bool b)
{
	setProperty("isShortcutConfigurable", b);
}

void Action::setSettingsText(const QString& text)
{
	setProperty(constSettingsText, text);
}

void Action::setSettingsText(Action* parent)
{
	setSettingsText(Utils::strippedText(parent->text()) + QLatin1String(" / ") + Utils::strippedText(text()));
}

QKeySequence Action::shortcut(ShortcutTypes type) const
{
	Q_ASSERT(type);
	if (type == DefaultShortcut)
		return property("defaultShortcut").value<QKeySequence>();

	if (shortcuts().count()) return shortcuts().value(0);
	return QKeySequence();
}

void Action::setShortcut(const QShortcut& shortcut, ShortcutTypes type)
{
	setShortcut(shortcut.key(), type);
}

void Action::setShortcut(const QKeySequence& key, ShortcutTypes type)
{
	Q_ASSERT(type);

	if (type & DefaultShortcut)
		setProperty("defaultShortcut", key);

	if (type & ActiveShortcut)
		QAction::setShortcut(key);
}

#include "moc_action.cpp"