2
* Copyright (C) 2010 Andriy Rysin (rysin@kde.org)
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.
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.
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.
19
#include "layouts_menu.h"
22
#include <ktoolinvocation.h>
23
#include <klocalizedstring.h>
25
#include <QtGui/QAction>
26
#include <QtGui/QMenu>
28
#include "keyboard_config.h"
29
#include "x11_helper.h"
30
#include "xkb_helper.h"
34
LayoutsMenu::LayoutsMenu(const KeyboardConfig& keyboardConfig_, const Rules& rules_, Flags& flags_):
35
keyboardConfig(keyboardConfig_),
42
LayoutsMenu::~LayoutsMenu()
47
const QIcon LayoutsMenu::getFlag(const QString& layout) const
49
return keyboardConfig.showFlag ? flags.getIcon(layout) : QIcon();
52
void LayoutsMenu::actionTriggered(QAction* action)
54
QString data = action->data().toString();
55
if( data == "config" ) {
57
args << "--args=--tab=layouts";
58
args << "kcm_keyboard";
59
KToolInvocation::kdeinitExec("kcmshell4", args);
62
LayoutUnit layoutUnit(LayoutUnit(action->data().toString()));
63
if( X11Helper::getCurrentLayouts().layouts.contains(layoutUnit) ) {
64
X11Helper::setLayout(layoutUnit);
67
QList<LayoutUnit> layouts(keyboardConfig.getDefaultLayouts());
69
layouts.append(layoutUnit);
70
XkbHelper::initializeKeyboardLayouts(layouts);
71
X11Helper::setLayout(layoutUnit);
76
QAction* LayoutsMenu::createAction(const LayoutUnit& layoutUnit) const
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());
88
QList<QAction*> LayoutsMenu::contextualActions()
91
disconnect(actionGroup, SIGNAL(triggered(QAction*)), this, SLOT(actionTriggered(QAction*)));
94
actionGroup = new QActionGroup(this);
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);
103
if( keyboardConfig.configureLayouts ) {
104
QList<LayoutUnit> extraLayouts = keyboardConfig.layouts;
105
foreach(const LayoutUnit& layoutUnit, currentLayouts) {
106
extraLayouts.removeOne(layoutUnit);
108
if( extraLayouts.size() > 0 ) {
109
QAction* separator = new QAction(actionGroup);
110
separator->setSeparator(true);
111
actionGroup->addAction(separator);
113
foreach(const LayoutUnit& layoutUnit, extraLayouts) {
114
QAction* action = createAction(layoutUnit);
115
actionGroup->addAction(action);
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();