~ubuntu-branches/ubuntu/lucid/kkbswitch/lucid

« back to all changes in this revision

Viewing changes to kkbswitch/kbswitchtrayicon.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Lior Kaplan
  • Date: 2005-09-07 02:01:14 UTC
  • Revision ID: james.westby@ubuntu.com-20050907020114-2wyo9eu21uihq86n
Tags: upstream-1.4.3
ImportĀ upstreamĀ versionĀ 1.4.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                          kbswitchtrayicon.cpp  -  description
 
3
                             -------------------
 
4
    begin                : Wed Jul 4 2001
 
5
    copyright            : (C) 2001 by Leonid Zeitlin
 
6
    email                : lz@europe.com
 
7
 ***************************************************************************/
 
8
 
 
9
/***************************************************************************
 
10
 *                                                                         *
 
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.                                   *
 
15
 *                                                                         *
 
16
 ***************************************************************************/
 
17
 
 
18
#include <kdeversion.h>
 
19
 
 
20
#undef USE_BOLD_MENUITEM
 
21
 
 
22
#include "kbswitchtrayicon.h"
 
23
#include "xkeyboard.h"
 
24
#ifdef USE_BOLD_MENUITEM
 
25
  #include "boldmenuitem.h"
 
26
#endif  
 
27
#include <kpopupmenu.h>
 
28
#if KDE_VERSION_MAJOR >= 3
 
29
  #include <kstandarddirs.h>
 
30
#else  
 
31
  #include <kstddirs.h>
 
32
#endif  
 
33
#include <klocale.h>
 
34
#include <kstdaction.h>
 
35
#include <kaction.h>
 
36
#include <kaboutapplication.h>
 
37
#if KDE_VERSION_MAJOR >= 3
 
38
  #include <kapplication.h>
 
39
#else
 
40
  #include <kapp.h>
 
41
#endif
 
42
#include <kiconloader.h>
 
43
#include <kdebug.h>
 
44
#include <kdeversion.h>
 
45
#include <qtooltip.h>
 
46
#include <qstyle.h>
 
47
#include <qpainter.h>
 
48
 
 
49
#ifdef USE_BOLD_MENUITEM
 
50
static QColor getActiveTextColor(KPopupMenu *menu)
 
51
{
 
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,
 
59
          styleopt);
 
60
        menu->removeItem(id);
 
61
        return painter.pen().color();  
 
62
}
 
63
#endif
 
64
 
 
65
KBSwitchTrayIcon::KBSwitchTrayIcon(KBConfig *conf){
 
66
  QPixmap pix;
 
67
#if KDE_VERSION_MAJOR >= 3
 
68
  KActionCollection *actions = new KActionCollection(this);
 
69
  #define ACTION_PARENT actions
 
70
#else
 
71
  #define ACTION_PARENT this
 
72
#endif
 
73
 
 
74
  m_kbconf = conf;
 
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)));
 
79
 
 
80
  menu->insertSeparator();
 
81
  KAction *pref = KStdAction::preferences(this, SIGNAL(preferencesSelected()), ACTION_PARENT);
 
82
  pref->plug(menu);
 
83
  KAction *help = KStdAction::help(this, SLOT(slotHelp()), ACTION_PARENT);
 
84
  help->plug(menu);
 
85
  KAction *about = KStdAction::aboutApp(this, SLOT(slotAbout()), ACTION_PARENT);
 
86
  about->plug(menu);
 
87
 
 
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);
 
93
}
 
94
 
 
95
KBSwitchTrayIcon::~KBSwitchTrayIcon(){
 
96
}
 
97
 
 
98
/** No descriptions */
 
99
void KBSwitchTrayIcon::slotMenuActivated(int id){
 
100
  if (id >= 0 && id < m_kbconf->groupCount()) emit groupSelected(id);
 
101
}
 
102
 
 
103
/** No descriptions */
 
104
void KBSwitchTrayIcon::setActiveGroup(int groupno){
 
105
  int i;
 
106
  KPopupMenu *menu = contextMenu();
 
107
  for (i = 0; i < m_kbconf->groupCount(); i++)
 
108
    menu->setItemChecked(i, false);
 
109
  menu->setItemChecked(groupno, true);
 
110
}
 
111
 
 
112
/** No descriptions */
 
113
void KBSwitchTrayIcon::setToggleGroups(int group1, int group2){
 
114
  int i;
 
115
  bool toggling;
 
116
  KPopupMenu *menu = contextMenu();
 
117
 
 
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);
 
124
#else
 
125
    if (toggling)
 
126
      menu->changeItem(i, m_kbconf->getGroup(i)->getName() + "*");
 
127
    else menu->changeItem(i, m_kbconf->getGroup(i)->getName());
 
128
#endif
 
129
  }
 
130
}
 
131
 
 
132
/** No descriptions */
 
133
void KBSwitchTrayIcon::mouseReleaseEvent(QMouseEvent *event){
 
134
  if (event->button() == LeftButton) {
 
135
    emit clicked();
 
136
  }
 
137
}
 
138
 
 
139
/** No descriptions */
 
140
void KBSwitchTrayIcon::slotAbout(){
 
141
  KAboutApplication about;
 
142
  about.exec();
 
143
}
 
144
 
 
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));
 
150
  }
 
151
}
 
152
 
 
153
/** No descriptions */
 
154
void KBSwitchTrayIcon::addLayoutItems(KPopupMenu *menu, bool clearOld) {
 
155
  KBGroup *group;
 
156
  int index;
 
157
#ifdef USE_BOLD_MENUITEM
 
158
  QColor active_text_color = getActiveTextColor(menu);
 
159
#endif  
 
160
 
 
161
  if (clearOld)
 
162
    for (int i = 0; i < XKeyboard::MaxNumKbdGroups; i++)
 
163
      if ((index = menu->indexOf(i)) >= 0) menu->removeItemAt(index);
 
164
 
 
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);
 
171
#else    
 
172
    menu->insertItem(group->getPixmap(), group->getName(), i, i + 1);
 
173
#endif    
 
174
  }
 
175
}
 
176
 
 
177
/** No descriptions */
 
178
void KBSwitchTrayIcon::reconfigure(){
 
179
  addLayoutItems(contextMenu(), true);
 
180
}
 
181
 
 
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();
 
185
  setPixmap(pix);
 
186
  setActiveGroup(groupno);
 
187
  QToolTip::remove(this);
 
188
  QToolTip::add(this, m_kbconf->getGroup(groupno)->getName());
 
189
}
 
190
 
 
191
/** Update menu and tray icons after configuration has changed */
 
192
void KBSwitchTrayIcon::slotUpdateIcons(){
 
193
  updateTrayIcon(XKeyboard::self()->getGroupNo());
 
194
  updateMenuIcons();
 
195
}
 
196
 
 
197
/** Display help */
 
198
void KBSwitchTrayIcon::slotHelp(){
 
199
  kapp->invokeHelp();
 
200
}