1
/***************************************************************************
2
kbswitchtrayicon.cpp - description
5
copyright : (C) 2001 by Leonid Zeitlin
7
***************************************************************************/
9
/***************************************************************************
11
* This program is free software; you can redistribute it and/or modify *
12
* it under the terms of the GNU General Public License as published by *
13
* the Free Software Foundation; either version 2 of the License, or *
14
* (at your option) any later version. *
16
***************************************************************************/
18
#include <kdeversion.h>
20
#undef USE_BOLD_MENUITEM
22
#include "kbswitchtrayicon.h"
23
#include "xkeyboard.h"
24
#ifdef USE_BOLD_MENUITEM
25
#include "boldmenuitem.h"
27
#include <kpopupmenu.h>
28
#if KDE_VERSION_MAJOR >= 3
29
#include <kstandarddirs.h>
34
#include <kstdaction.h>
36
#include <kaboutapplication.h>
37
#if KDE_VERSION_MAJOR >= 3
38
#include <kapplication.h>
42
#include <kiconloader.h>
44
#include <kdeversion.h>
49
#ifdef USE_BOLD_MENUITEM
50
static QColor getActiveTextColor(KPopupMenu *menu)
52
int id = menu->insertItem("test text");
53
QMenuItem *item = menu->findItem(id);
54
QStyleOption styleopt = QStyleOption(item);
55
QPainter painter(menu);
56
QColorGroup &cg = menu->colorGroup();
57
KApplication::style().drawControl(QStyle::CE_PopupMenuItem, &painter, menu,
58
menu->contentsRect(), cg, QStyle::Style_Enabled | QStyle::Style_Active,
61
return painter.pen().color();
65
KBSwitchTrayIcon::KBSwitchTrayIcon(KBConfig *conf){
67
#if KDE_VERSION_MAJOR >= 3
68
KActionCollection *actions = new KActionCollection(this);
69
#define ACTION_PARENT actions
71
#define ACTION_PARENT this
75
//QObject::connect(conf, SIGNAL(changed()), this, SLOT(updateMenuIcons()));
76
KPopupMenu * menu = contextMenu();
77
addLayoutItems(menu, false);
78
QObject::connect(menu, SIGNAL(activated(int)), this, SLOT(slotMenuActivated(int)));
80
menu->insertSeparator();
81
KAction *pref = KStdAction::preferences(this, SIGNAL(preferencesSelected()), ACTION_PARENT);
83
KAction *help = KStdAction::help(this, SLOT(slotHelp()), ACTION_PARENT);
85
KAction *about = KStdAction::aboutApp(this, SLOT(slotAbout()), ACTION_PARENT);
88
/*QString path = locate("icon", "hicolor/16x16/apps/locale.png");
89
if (!path.isEmpty()) pix.load(path);*/
90
pix = kapp->iconLoader()->loadIcon("locale", KIcon::Small);
91
menu->changeTitle(menu->idAt(0), pix, i18n("Keyboard Switch"));
92
setAlignment(Qt::AlignHCenter | Qt::AlignCenter);
95
KBSwitchTrayIcon::~KBSwitchTrayIcon(){
98
/** No descriptions */
99
void KBSwitchTrayIcon::slotMenuActivated(int id){
100
if (id >= 0 && id < m_kbconf->groupCount()) emit groupSelected(id);
103
/** No descriptions */
104
void KBSwitchTrayIcon::setActiveGroup(int groupno){
106
KPopupMenu *menu = contextMenu();
107
for (i = 0; i < m_kbconf->groupCount(); i++)
108
menu->setItemChecked(i, false);
109
menu->setItemChecked(groupno, true);
112
/** No descriptions */
113
void KBSwitchTrayIcon::setToggleGroups(int group1, int group2){
116
KPopupMenu *menu = contextMenu();
118
for (i = 0; i < m_kbconf->groupCount(); i++) {
119
toggling = (i == group1 || i == group2);
120
#ifdef USE_BOLD_MENUITEM
121
QMenuItem *item = menu->findItem(i);
122
BoldMenuItem *bolditem = dynamic_cast<BoldMenuItem*>(item->custom());
123
bolditem->setBold(toggling);
126
menu->changeItem(i, m_kbconf->getGroup(i)->getName() + "*");
127
else menu->changeItem(i, m_kbconf->getGroup(i)->getName());
132
/** No descriptions */
133
void KBSwitchTrayIcon::mouseReleaseEvent(QMouseEvent *event){
134
if (event->button() == LeftButton) {
139
/** No descriptions */
140
void KBSwitchTrayIcon::slotAbout(){
141
KAboutApplication about;
145
/** No descriptions */
146
void KBSwitchTrayIcon::updateMenuIcons(){
147
KPopupMenu *menu = contextMenu();
148
for (int i = 0; i < m_kbconf->groupCount(); i++) {
149
menu->changeItem(i, m_kbconf->getGroup(i)->getPixmap(), menu->text(i));
153
/** No descriptions */
154
void KBSwitchTrayIcon::addLayoutItems(KPopupMenu *menu, bool clearOld) {
157
#ifdef USE_BOLD_MENUITEM
158
QColor active_text_color = getActiveTextColor(menu);
162
for (int i = 0; i < XKeyboard::MaxNumKbdGroups; i++)
163
if ((index = menu->indexOf(i)) >= 0) menu->removeItemAt(index);
165
for (int i = 0; i < m_kbconf->groupCount(); i++) {
166
group = m_kbconf->getGroup(i);
167
#ifdef USE_BOLD_MENUITEM
168
/* the work on BoldMenuItems suspended: see comments in boldmenuitem.h */
169
menu->insertItem(group->getPixmap(),
170
new BoldMenuItem(group->getName(), active_text_color, false), i);
172
menu->insertItem(group->getPixmap(), group->getName(), i, i + 1);
177
/** No descriptions */
178
void KBSwitchTrayIcon::reconfigure(){
179
addLayoutItems(contextMenu(), true);
182
/** Update the tray icon display for the given group */
183
void KBSwitchTrayIcon::updateTrayIcon(int groupno){
184
const QPixmap& pix = m_kbconf->getGroup(groupno)->getPixmap();
186
setActiveGroup(groupno);
187
QToolTip::remove(this);
188
QToolTip::add(this, m_kbconf->getGroup(groupno)->getName());
191
/** Update menu and tray icons after configuration has changed */
192
void KBSwitchTrayIcon::slotUpdateIcons(){
193
updateTrayIcon(XKeyboard::self()->getGroupNo());
198
void KBSwitchTrayIcon::slotHelp(){