not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kcontrol / kxkb / layoutmap.cpp
blob33198cbcab42fdcfa2b3c4b5f58ff39c41d11173
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 <QX11Info>
21 #include <kwindowsystem.h>
23 #include "layoutmap.h"
25 #include "x11helper.h"
28 LayoutMap::LayoutMap(const KxkbConfig& kxkbConfig_):
29 m_kxkbConfig(kxkbConfig_),
30 m_currentWinId( X11Helper::UNKNOWN_WINDOW_ID )
34 // private
35 void LayoutMap::clearMaps()
37 m_appLayouts.clear();
38 m_winLayouts.clear();
39 m_globalLayouts.clear();
40 //setCurrentWindow( -1 );
43 void LayoutMap::reset()
45 clearMaps();
46 ownerChanged();
47 m_currentWinId = X11Helper::UNKNOWN_WINDOW_ID;
51 void LayoutMap::ownerChanged()
53 if( m_kxkbConfig.m_switchingPolicy == SWITCH_POLICY_DESKTOP ) {
54 m_currentDesktop = KWindowSystem::currentDesktop();
56 else {
57 m_currentWinId = KWindowSystem::activeWindow();
58 if( m_kxkbConfig.m_switchingPolicy == SWITCH_POLICY_WIN_CLASS )
59 m_currentWinClass = X11Helper::getWindowClass(m_currentWinId, QX11Info::display());
63 // private
64 QQueue<int>& LayoutMap::getCurrentLayoutQueueInternal()
66 if( m_currentWinId == X11Helper::UNKNOWN_WINDOW_ID )
67 return m_globalLayouts;
69 switch( m_kxkbConfig.m_switchingPolicy ) {
70 case SWITCH_POLICY_WIN_CLASS:
71 return m_appLayouts[ m_currentWinClass ];
72 case SWITCH_POLICY_WINDOW:
73 return m_winLayouts[ m_currentWinId ];
74 case SWITCH_POLICY_DESKTOP:
75 return m_winLayouts[ m_currentDesktop ];
76 default:
77 return m_globalLayouts;
81 //private
82 QString LayoutMap::getOwner()
84 switch( m_kxkbConfig.m_switchingPolicy ) {
85 case SWITCH_POLICY_WIN_CLASS:
86 return QString("winclass: %1").arg(m_currentWinClass);
87 case SWITCH_POLICY_WINDOW:
88 return QString("window: %1").arg(m_currentWinId);
89 case SWITCH_POLICY_DESKTOP:
90 return QString("desktop: %1").arg(m_currentDesktop);
91 default:
92 return "global";
96 // private
97 QQueue<int>& LayoutMap::getCurrentLayoutQueue()
99 QQueue<int>& layoutQueue = getCurrentLayoutQueueInternal();
100 if( layoutQueue.count() == 0 ) {
101 initLayoutQueue(layoutQueue);
102 kDebug() << "Created queue for " << getOwner() << " size: " << layoutQueue.count();
105 return layoutQueue;
108 int LayoutMap::getCurrentLayout() {
109 return getCurrentLayoutQueue().head();
112 int LayoutMap::getNextLayout() {
113 LayoutQueue& layoutQueue = getCurrentLayoutQueue(/*m_currentWinId*/);
114 int layoutState = layoutQueue.dequeue();
115 layoutQueue.enqueue(layoutState);
117 kDebug() << "map: Next layout: " << layoutQueue.head() << " for " << getOwner();
119 return layoutQueue.head();
122 void LayoutMap::setCurrentLayout(int layoutUnit) {
123 LayoutQueue& layoutQueue = getCurrentLayoutQueue();
124 kDebug() << "map: Storing layout: " << layoutUnit << " for " << getOwner();
126 int queueSize = (int)layoutQueue.count();
127 for(int ii=0; ii<queueSize; ii++) {
128 if( layoutQueue.head() == layoutUnit )
129 return; // if present return when it's in head
131 int layoutState = layoutQueue.dequeue();
132 if( ii < queueSize - 1 ) {
133 layoutQueue.enqueue(layoutState);
135 else {
136 layoutQueue.enqueue(layoutUnit);
139 for(int ii=0; ii<queueSize - 1; ii++) {
140 int layoutState = layoutQueue.dequeue();
141 layoutQueue.enqueue(layoutState);
145 // private
146 void LayoutMap::initLayoutQueue(LayoutQueue& layoutQueue) {
147 int queueSize = ( m_kxkbConfig.m_stickySwitching )
148 ? m_kxkbConfig.m_stickySwitchingDepth : m_kxkbConfig.m_layouts.count();
149 for(int ii=0; ii<queueSize; ii++) {
150 layoutQueue.enqueue( ii );