~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kcontrol/keyboard/layouts_menu.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2010 Andriy Rysin (rysin@kde.org)
 
3
 *
 
4
 *  This program is free software; you can redistribute it and/or modify
 
5
 *  it under the terms of the GNU General Public License as published by
 
6
 *  the Free Software Foundation; either version 2 of the License, or
 
7
 *  (at your option) any later version.
 
8
 *
 
9
 *  This program is distributed in the hope that it will be useful,
 
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 *  GNU General Public License for more details.
 
13
 *
 
14
 *  You should have received a copy of the GNU General Public License
 
15
 *  along with this program; if not, write to the Free Software
 
16
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
17
 */
 
18
 
 
19
#include "layouts_menu.h"
 
20
 
 
21
#include <kmenu.h>
 
22
#include <ktoolinvocation.h>
 
23
#include <klocalizedstring.h>
 
24
 
 
25
#include <QtGui/QAction>
 
26
#include <QtGui/QMenu>
 
27
 
 
28
#include "keyboard_config.h"
 
29
#include "x11_helper.h"
 
30
#include "xkb_helper.h"
 
31
#include "flags.h"
 
32
 
 
33
 
 
34
LayoutsMenu::LayoutsMenu(const KeyboardConfig& keyboardConfig_, const Rules& rules_, Flags& flags_):
 
35
        keyboardConfig(keyboardConfig_),
 
36
        rules(rules_),
 
37
        flags(flags_),
 
38
        actionGroup(NULL)
 
39
{
 
40
}
 
41
 
 
42
LayoutsMenu::~LayoutsMenu()
 
43
{
 
44
        delete actionGroup;
 
45
}
 
46
 
 
47
const QIcon LayoutsMenu::getFlag(const QString& layout) const
 
48
{
 
49
        return keyboardConfig.showFlag ? flags.getIcon(layout) : QIcon();
 
50
}
 
51
 
 
52
void LayoutsMenu::actionTriggered(QAction* action)
 
53
{
 
54
        QString data = action->data().toString();
 
55
        if( data == "config" ) {
 
56
                QStringList args;
 
57
                args << "--args=--tab=layouts";
 
58
                args << "kcm_keyboard";
 
59
                KToolInvocation::kdeinitExec("kcmshell4", args);
 
60
        }
 
61
        else {
 
62
                LayoutUnit layoutUnit(LayoutUnit(action->data().toString()));
 
63
                if( X11Helper::getCurrentLayouts().layouts.contains(layoutUnit) ) {
 
64
                        X11Helper::setLayout(layoutUnit);
 
65
                }
 
66
                else {
 
67
                        QList<LayoutUnit> layouts(keyboardConfig.getDefaultLayouts());
 
68
                        layouts.removeLast();
 
69
                        layouts.append(layoutUnit);
 
70
                        XkbHelper::initializeKeyboardLayouts(layouts);
 
71
                        X11Helper::setLayout(layoutUnit);
 
72
                }
 
73
        }
 
74
}
 
75
 
 
76
QAction* LayoutsMenu::createAction(const LayoutUnit& layoutUnit) const
 
77
{
 
78
        QString menuText = Flags::getFullText(layoutUnit, keyboardConfig, &rules);
 
79
        QAction* action = new QAction(getFlag(layoutUnit.layout), menuText, actionGroup);
 
80
        action->setData(layoutUnit.toString());
 
81
        //FIXME: tooltips don't work on dbusmenus???
 
82
//      if( ! layoutUnit.getShortcut().isEmpty() ) {
 
83
//              action->setToolTip(layoutUnit.getShortcut().toString());
 
84
//      }
 
85
        return action;
 
86
}
 
87
 
 
88
QList<QAction*> LayoutsMenu::contextualActions()
 
89
{
 
90
        if( actionGroup ) {
 
91
                disconnect(actionGroup, SIGNAL(triggered(QAction*)), this, SLOT(actionTriggered(QAction*)));
 
92
                delete actionGroup;
 
93
        }
 
94
        actionGroup = new QActionGroup(this);
 
95
 
 
96
        X11Helper::getLayoutsList(); //UGLY: seems to be more reliable with extra call
 
97
        QList<LayoutUnit> currentLayouts = X11Helper::getLayoutsList();
 
98
        foreach(const LayoutUnit& layoutUnit, currentLayouts) {
 
99
                QAction* action = createAction(layoutUnit);
 
100
                actionGroup->addAction(action);
 
101
        }
 
102
 
 
103
        if( keyboardConfig.configureLayouts ) {
 
104
                QList<LayoutUnit> extraLayouts = keyboardConfig.layouts;
 
105
                foreach(const LayoutUnit& layoutUnit, currentLayouts) {
 
106
                        extraLayouts.removeOne(layoutUnit);
 
107
                }
 
108
                if( extraLayouts.size() > 0 ) {
 
109
                        QAction* separator = new QAction(actionGroup);
 
110
                        separator->setSeparator(true);
 
111
                        actionGroup->addAction(separator);
 
112
 
 
113
                        foreach(const LayoutUnit& layoutUnit, extraLayouts) {
 
114
                                QAction* action = createAction(layoutUnit);
 
115
                                actionGroup->addAction(action);
 
116
                        }
 
117
                }
 
118
        }
 
119
 
 
120
        QAction* separator = new QAction(actionGroup);
 
121
        separator->setSeparator(true);
 
122
        actionGroup->addAction(separator);
 
123
        QAction* configAction = new QAction(i18n("Configure..."), actionGroup);
 
124
        actionGroup->addAction(configAction);
 
125
        configAction->setData("config");
 
126
        connect(actionGroup, SIGNAL(triggered(QAction*)), this, SLOT(actionTriggered(QAction*)));
 
127
        return actionGroup->actions();
 
128
}