Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / kcontrol / kxkb / kxkbwidget.cpp
blobacff5995691c2143bdef17a0f2d0eef8dca991ef
1 /*
2 * Copyright (C) 2006 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.
20 #include <QSystemTrayIcon>
21 #include <QMenu>
22 #include <QMouseEvent>
24 #include <kdebug.h>
25 #include <klocale.h>
26 #include <kiconloader.h>
27 #include <kaction.h>
29 #include "pixmap.h"
30 #include "rules.h"
31 #include "kxkbconfig.h"
33 #include "kxkbwidget.h"
35 #include "kxkbwidget.moc"
38 KxkbWidget::KxkbWidget(int controlType):
39 m_controlType(controlType),
40 m_configSeparator(NULL)
44 void KxkbWidget::setCurrentLayout(const LayoutUnit& layoutUnit)
46 setToolTip(m_descriptionMap[layoutUnit.toPair()]);
47 const QPixmap& icon =
48 LayoutIcon::getInstance().findPixmap(layoutUnit.layout, m_showFlag, layoutUnit.getDisplayName());
49 // kDebug() << "setting pixmap: " << icon.width();
50 setPixmap( icon );
51 kDebug() << "setting text: " << layoutUnit.layout;
52 setText(layoutUnit.layout);
55 void KxkbWidget::setError(const QString& layoutInfo)
57 QString msg = i18n("Error changing keyboard layout to '%1'", layoutInfo);
58 setToolTip(msg);
59 setPixmap(LayoutIcon::getInstance().findPixmap("error", false));
63 void KxkbWidget::initLayoutList(const QList<LayoutUnit>& layouts, const XkbRules& rules)
65 if( m_controlType <= NO_MENU ) {
66 kDebug() << "indicator with no menu requested";
67 return;
70 QMenu* menu = contextMenu();
72 m_descriptionMap.clear();
74 // menu->clear();
75 // menu->addTitle( qApp->windowIcon(), KGlobal::caption() );
76 // menu->setTitle( KGlobal::mainComponent().aboutData()->programName() );
78 for(QList<QAction*>::Iterator it=m_actions.begin(); it != m_actions.end(); it++ )
79 menu->removeAction(*it);
80 m_actions.clear();
82 int cnt = 0;
83 QList<LayoutUnit>::ConstIterator it;
84 for (it=layouts.begin(); it != layouts.end(); ++it)
86 const QString layoutName = (*it).layout;
87 const QString variantName = (*it).variant;
89 const QPixmap& layoutPixmap =
90 LayoutIcon::getInstance().findPixmap(layoutName, m_showFlag, (*it).getDisplayName());
91 // const QPixmap pix = iconeffect.apply(layoutPixmap, KIcon::Small, KIcon::DefaultState);
93 QString layoutString = rules.layouts()[layoutName];
94 QString fullName = layoutString;
95 if( variantName.isEmpty() == false )
96 fullName += " (" + variantName + ')';
97 // menu->insertItem(pix, fullName, START_MENU_ID + cnt, m_menuStartIndex + cnt);
99 QAction* action = new QAction(layoutPixmap, fullName, menu);
100 action->setData(START_MENU_ID + cnt);
101 m_actions.append(action);
102 m_descriptionMap.insert((*it).toPair(), fullName);
104 // kDebug() << "added" << (*it).toPair() << "to context menu";
106 cnt++;
108 menu->insertActions(m_configSeparator, m_actions);
110 // if show config, if show help
111 // if( menu->indexOf(CONFIG_MENU_ID) == -1 ) {
112 if( m_configSeparator == NULL && m_controlType >= MENU_FULL ) { // first call
113 m_configSeparator = menu->addSeparator();
115 QAction* configAction = new QAction(SmallIcon("configure"), i18n("Configure..."), menu);
116 configAction->setData(CONFIG_MENU_ID);
117 menu->addAction(configAction);
119 // if( menu->indexOf(HELP_MENU_ID) == -1 )
120 QAction* helpAction = new QAction(SmallIcon("help-contents"), i18n("Help"), menu);
121 helpAction->setData(HELP_MENU_ID);
122 menu->addAction(helpAction);
124 // else {
125 // kDebug() << "indicator with menu 'layouts only' requested";
126 // }
128 // menu->update();
130 /* if( index != -1 ) { //not first start
131 menu->addSeparator();
132 KAction* quitAction = KStdAction::quit(this, SIGNAL(quitSelected()), actionCollection());
133 if (quitAction)
134 quitAction->plug(menu);
139 // ----------------------------
140 // QSysTrayIcon implementation
142 KxkbSysTrayIcon::KxkbSysTrayIcon(int controlType):
143 KxkbWidget(controlType)
145 m_indicatorWidget = new KSystemTrayIcon();
147 connect(contextMenu(), SIGNAL(triggered(QAction*)), this, SIGNAL(menuTriggered(QAction*)));
148 connect(m_indicatorWidget, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
149 this, SLOT(trayActivated(QSystemTrayIcon::ActivationReason)));
152 void KxkbSysTrayIcon::trayActivated(QSystemTrayIcon::ActivationReason reason)
154 if( reason == QSystemTrayIcon::Trigger )
155 emit iconToggled();
158 void KxkbSysTrayIcon::setPixmap(const QPixmap& pixmap)
160 m_indicatorWidget->setIcon( pixmap );
163 // ----------------------------
165 KxkbLabel::KxkbLabel(int controlType, QWidget* parent):
166 KxkbWidget(controlType),
167 m_displayMode(ICON)
169 m_indicatorWidget = new QToolButton(parent);
171 m_indicatorWidget->setAutoRaise(true);
172 m_indicatorWidget->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum));
174 connect( m_indicatorWidget, SIGNAL(clicked(bool)), this, SIGNAL(iconToggled()) );
175 // connect( m_indicatorWidget, SIGNAL(toggled(bool)), this, SIGNAL(iconToggled()) );
177 m_menu = new QMenu(m_indicatorWidget);
178 if( m_controlType >= KxkbWidget::MENU_LAYOUTS_ONLY ) {
179 m_indicatorWidget->setContextMenuPolicy(Qt::CustomContextMenu);
180 connect(m_indicatorWidget, SIGNAL(customContextMenuRequested(const QPoint &)),
181 this, SLOT(contextMenuEvent(const QPoint&)));
185 void KxkbLabel::contextMenuEvent(const QPoint& pos)
187 QMenu* menu = contextMenu();
188 menu->exec(pos);
191 void KxkbLabel::setPixmap(const QPixmap& pixmap)
193 m_indicatorWidget->setIconSize(QSize(24,24));
194 m_indicatorWidget->setIcon( pixmap );