~ubuntu-branches/ubuntu/quantal/psi/quantal

« back to all changes in this revision

Viewing changes to src/shortcutmanager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-04-14 18:57:30 UTC
  • mfrom: (2.1.9 hardy)
  • Revision ID: james.westby@ubuntu.com-20080414185730-528re3zp0m2hdlhi
Tags: 0.11-8
* added CONFIG -= link_prl to .pro files and removed dependencies
  which are made unnecessary by this change
* Fix segfault when closing last chat tab with qt4.4
  (This is from upstream svn, rev. 1101) (Closes: Bug#476122)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <QAction>
 
2
#include <QCoreApplication>
 
3
 
 
4
#include "psioptions.h"
 
5
#include "shortcutmanager.h"
 
6
#include "globalshortcutmanager.h"
 
7
 
 
8
 
 
9
/**
 
10
 * \brief The Construtor of the Shortcutmanager
 
11
 */
 
12
ShortcutManager::ShortcutManager() : QObject(QCoreApplication::instance())
 
13
{
 
14
        // Make sure that there is at least one shortcut for sending messages
 
15
        if (shortcuts("chat.send").isEmpty()) {
 
16
                qWarning("Restoring chat.send shortcut");
 
17
                QVariantList vl;
 
18
                vl << qVariantFromValue(QKeySequence(Qt::Key_Enter)) << qVariantFromValue(QKeySequence(Qt::Key_Return));
 
19
                PsiOptions::instance()->setOption("options.shortcuts.chat.send",vl);
 
20
        }
 
21
}
 
22
 
 
23
/**
 
24
 * "The one and only instance" of the ShortcutManager
 
25
 */
 
26
ShortcutManager* ShortcutManager::instance_ = NULL;
 
27
 
 
28
/**
 
29
 * \brief The Instantiator of the Shortcutmanager
 
30
 */
 
31
ShortcutManager* ShortcutManager::instance() 
 
32
{
 
33
        if(!instance_)
 
34
                instance_ = new ShortcutManager();
 
35
        return instance_;
 
36
}
 
37
 
 
38
/**
 
39
 * \brief get the QKeySequence associated with the keyname "name"
 
40
 * \param the shortcut name e.g. "misc.sendmessage" which is in the options xml then
 
41
 *        mirrored as options.shortcuts.misc.sendmessage
 
42
 * \return QKeySequence, the Keysequence associated with the keyname, or
 
43
 *   the first key sequence if there are multiple
 
44
 */
 
45
QKeySequence ShortcutManager::shortcut(const QString& name) 
 
46
{
 
47
        QVariant variant = PsiOptions::instance()->getOption(QString("options.shortcuts.%1").arg(name));
 
48
        QString type = variant.typeName();
 
49
        if (type == "QVariantList")
 
50
                variant = variant.toList().first();
 
51
        //qDebug() << "shortcut: " << name << variant.value<QKeySequence>().toString();
 
52
        return variant.value<QKeySequence>();
 
53
}
 
54
 
 
55
/**
 
56
 * \brief get the QVariantList associated with the keyname "name"
 
57
 * \param the shortcut name e.g. "misc.sendmessage" which is in the options xml then
 
58
 *        mirrored as options.shortcuts.misc.sendmessage
 
59
 * \return List of sequences
 
60
 */
 
61
QList<QKeySequence> ShortcutManager::shortcuts(const QString& name)
 
62
{
 
63
        QList<QKeySequence> list;
 
64
        QVariant variant = PsiOptions::instance()->getOption(QString("options.shortcuts.%1").arg(name));
 
65
        QString type = variant.typeName();
 
66
        if (type == "QVariantList") {
 
67
                foreach(QVariant variant, variant.toList()) {
 
68
                        list += variant.value<QKeySequence>();
 
69
                }
 
70
        }
 
71
        else {
 
72
                QKeySequence k = variant.value<QKeySequence>();
 
73
                if (!k.isEmpty())
 
74
                        list += k;
 
75
        }
 
76
        return list;
 
77
}
 
78
 
 
79
 
 
80
/**
 
81
 * \brief this function connects the Key or Keys associated with the keyname "path" with the slot "slot"
 
82
 *        of the Widget "parent"
 
83
 * \param path, the shortcut name e.g. "misc.sendmessage" which is in the options xml then
 
84
 *        mirrored as options.shortcuts.misc.sendmessage 
 
85
 * \param parent, the widget to which the new QAction should be connected to
 
86
 * \param slot, the SLOT() of the parent which should be triggerd if the KeySequence is activated
 
87
 */
 
88
void ShortcutManager::connect(const QString& path, QObject* parent, const char* slot)
 
89
{
 
90
        if (parent == NULL || slot == NULL)
 
91
                return;
 
92
 
 
93
        if (!path.startsWith("global.")) {
 
94
                foreach(QKeySequence sequence, ShortcutManager::instance()->shortcuts(path)) {
 
95
                        if (!sequence.isEmpty()) {
 
96
                                QAction* act = new QAction(parent);
 
97
                                act->setShortcut(sequence);
 
98
                                if (parent->isWidgetType())
 
99
                                        ((QWidget*) parent)->addAction(act);
 
100
                                parent->connect(act, SIGNAL(activated()), slot);
 
101
                        }
 
102
                }
 
103
        }
 
104
        else {
 
105
                foreach(QKeySequence sequence, ShortcutManager::instance()->shortcuts(path)) {
 
106
                        if (!sequence.isEmpty()) {
 
107
                                GlobalShortcutManager::instance()->connect(sequence, parent, slot);
 
108
                        }
 
109
                }
 
110
        }
 
111
}