not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kcontrol / kxkb / kxkbwidget.cpp
blob35da60be63c1569eecc5b0742cd325e5beee3e7e
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>
28 #include <khelpmenu.h>
29 #include <kcmdlineargs.h>
30 #include <kmenu.h>
32 #include "pixmap.h"
33 #include "rules.h"
34 #include "kxkbconfig.h"
36 #include "kxkbwidget.h"
38 #include "kxkbwidget.moc"
41 KxkbWidget::KxkbWidget(int controlType):
42 m_controlType(controlType),
43 m_configSeparator(NULL)
47 void KxkbWidget::setCurrentLayout(const LayoutUnit& layoutUnit)
49 QString tip = m_descriptionMap[layoutUnit.toPair()];
50 if( tip == NULL || tip.length() == 0 )
51 tip = layoutUnit.toPair();
52 setToolTip(tip);
53 const QPixmap& icon =
54 LayoutIcon::getInstance().findPixmap(layoutUnit.layout, m_showFlag, layoutUnit.getDisplayName());
55 // kDebug() << "setting pixmap: " << icon.width();
56 setPixmap( icon );
57 kDebug() << "setting text: " << layoutUnit.layout;
58 setText(layoutUnit.layout);
61 void KxkbWidget::setError(const QString& layoutInfo)
63 QString msg = i18n("Error changing keyboard layout to '%1'", layoutInfo);
64 setToolTip(msg);
65 setPixmap(LayoutIcon::getInstance().findPixmap("error", false));
69 void KxkbWidget::initLayoutList(const QList<LayoutUnit>& layouts, const XkbRules& rules)
71 if( m_controlType <= NO_MENU ) {
72 kDebug() << "indicator with no menu requested";
73 return;
76 QMenu* menu = contextMenu();
78 m_descriptionMap.clear();
80 // menu->clear();
81 // menu->addTitle( qApp->windowIcon(), KGlobal::caption() );
82 // menu->setTitle( KGlobal::mainComponent().aboutData()->programName() );
84 for(QList<QAction*>::Iterator it=m_actions.begin(); it != m_actions.end(); it++ )
85 menu->removeAction(*it);
86 m_actions.clear();
88 int cnt = 0;
89 QList<LayoutUnit>::ConstIterator it;
90 for (it=layouts.begin(); it != layouts.end(); ++it)
92 const QString layoutName = (*it).layout;
93 const QString variantName = (*it).variant;
95 const QPixmap& layoutPixmap =
96 LayoutIcon::getInstance().findPixmap(layoutName, m_showFlag, (*it).getDisplayName());
97 // const QPixmap pix = iconeffect.apply(layoutPixmap, KIcon::Small, KIcon::DefaultState);
99 QString layoutString = rules.layouts()[layoutName];
100 QString fullName = layoutString;
101 if( variantName.isEmpty() == false )
102 fullName += " (" + variantName + ')';
103 // menu->insertItem(pix, fullName, START_MENU_ID + cnt, m_menuStartIndex + cnt);
105 QAction* action = new QAction(layoutPixmap, fullName, menu);
106 action->setData(START_MENU_ID + cnt);
107 m_actions.append(action);
108 m_descriptionMap.insert((*it).toPair(), fullName);
110 // kDebug() << "added" << (*it).toPair() << "to context menu";
112 cnt++;
114 menu->insertActions(m_configSeparator, m_actions);
116 // if show config, if show help
117 // if( menu->indexOf(CONFIG_MENU_ID) == -1 ) {
118 if( m_configSeparator == NULL && m_controlType >= MENU_FULL ) { // first call
119 m_configSeparator = menu->addSeparator();
121 QAction* configAction = new QAction(SmallIcon("configure"), i18n("Configure..."), menu);
122 configAction->setData(CONFIG_MENU_ID);
123 menu->addAction(configAction);
125 KHelpMenu* helpmenu = new KHelpMenu( NULL, KCmdLineArgs::aboutData(), false );
127 menu->addMenu(helpmenu->menu());
129 // else {
130 // kDebug() << "indicator with menu 'layouts only' requested";
131 // }
133 // menu->update();
135 /* if( index != -1 ) { //not first start
136 menu->addSeparator();
137 KAction* quitAction = KStdAction::quit(this, SIGNAL(quitSelected()), actionCollection());
138 if (quitAction)
139 quitAction->plug(menu);
144 // ----------------------------
145 // QSysTrayIcon implementation
147 KxkbSysTrayIcon::KxkbSysTrayIcon(int controlType):
148 KxkbWidget(controlType)
150 m_indicatorWidget = new KSystemTrayIcon();
152 connect(contextMenu(), SIGNAL(triggered(QAction*)), this, SIGNAL(menuTriggered(QAction*)));
153 connect(m_indicatorWidget, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
154 this, SLOT(trayActivated(QSystemTrayIcon::ActivationReason)));
157 void KxkbSysTrayIcon::trayActivated(QSystemTrayIcon::ActivationReason reason)
159 if( reason == QSystemTrayIcon::Trigger )
160 emit iconToggled();
163 void KxkbSysTrayIcon::setPixmap(const QPixmap& pixmap)
165 m_indicatorWidget->setIcon( pixmap );
168 // ----------------------------
170 KxkbLabel::KxkbLabel(int controlType, QWidget* parent):
171 KxkbWidget(controlType),
172 m_displayMode(ICON)
174 m_indicatorWidget = new QToolButton(parent);
176 m_indicatorWidget->setAutoRaise(true);
177 m_indicatorWidget->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum));
179 connect( m_indicatorWidget, SIGNAL(clicked(bool)), this, SIGNAL(iconToggled()) );
180 // connect( m_indicatorWidget, SIGNAL(toggled(bool)), this, SIGNAL(iconToggled()) );
182 m_menu = new QMenu(m_indicatorWidget);
183 if( m_controlType >= KxkbWidget::MENU_LAYOUTS_ONLY ) {
184 m_indicatorWidget->setContextMenuPolicy(Qt::CustomContextMenu);
185 connect(m_indicatorWidget, SIGNAL(customContextMenuRequested(const QPoint &)),
186 this, SLOT(contextMenuEvent(const QPoint&)));
190 void KxkbLabel::contextMenuEvent(const QPoint& pos)
192 QMenu* menu = contextMenu();
193 menu->exec(pos);
196 void KxkbLabel::setPixmap(const QPixmap& pixmap)
198 m_indicatorWidget->setIconSize(QSize(24,24));
199 m_indicatorWidget->setIcon( pixmap );