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>
22 #include <QMouseEvent>
26 #include <kiconloader.h>
28 #include <khelpmenu.h>
29 #include <kcmdlineargs.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();
54 LayoutIcon::getInstance().findPixmap(layoutUnit
.layout
, m_showFlag
, layoutUnit
.getDisplayName());
55 // kDebug() << "setting pixmap: " << icon.width();
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
);
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";
76 QMenu
* menu
= contextMenu();
78 m_descriptionMap
.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
);
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";
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());
130 // kDebug() << "indicator with menu 'layouts only' requested";
135 /* if( index != -1 ) { //not first start
136 menu->addSeparator();
137 KAction* quitAction = KStdAction::quit(this, SIGNAL(quitSelected()), actionCollection());
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
)
163 void KxkbSysTrayIcon::setPixmap(const QPixmap
& pixmap
)
165 m_indicatorWidget
->setIcon( pixmap
);
168 // ----------------------------
170 KxkbLabel::KxkbLabel(int controlType
, QWidget
* parent
):
171 KxkbWidget(controlType
),
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();
196 void KxkbLabel::setPixmap(const QPixmap
& pixmap
)
198 m_indicatorWidget
->setIconSize(QSize(24,24));
199 m_indicatorWidget
->setIcon( pixmap
);