not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / applets / activitybar / activitybar.cpp
blobc507ff3eccb9a587107fb3eeefbb7425ef783654
1 /***************************************************************************
2 * Copyright (C) 2008 by Marco Martin <notmart@gmail.com> *
3 * *
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. *
8 * *
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. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
18 ***************************************************************************/
20 #include "activitybar.h"
22 #include <QGraphicsLinearLayout>
24 #include <KWindowSystem>
26 #include <Plasma/View>
27 #include <Plasma/Corona>
28 #include <Plasma/Context>
29 #include <Plasma/Containment>
30 #include <Plasma/TabBar>
33 ActivityBar::ActivityBar(QObject *parent, const QVariantList &args)
34 : Plasma::Applet(parent, args),
35 m_activeContainment(-1)
37 resize(200, 60);
38 setAspectRatioMode(Plasma::IgnoreAspectRatio);
39 setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding));
43 ActivityBar::~ActivityBar()
47 void ActivityBar::init()
49 QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(this);
50 m_tabBar = new Plasma::TabBar(this);
51 layout->addItem(m_tabBar);
52 layout->setContentsMargins(0,0,0,0);
53 layout->setSizePolicy(QSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding));
55 if (containment()) {
56 Plasma::Corona *c = containment()->corona();
58 if (!c) {
59 kDebug() << "No corona, can't happen";
60 setFailedToLaunch(true);
61 return;
64 int myScreen = containment()->screen();
66 QList<Plasma::Containment*> containments = c->containments();
67 foreach (Plasma::Containment *cont, containments) {
68 if (cont->containmentType() == Plasma::Containment::PanelContainment) {
69 continue;
72 m_containments.append(cont);
74 if (cont->activity().isNull()) {
75 m_tabBar->addTab(cont->name());
76 } else {
77 m_tabBar->addTab(cont->activity());
80 if (cont->screen() != -1 &&
81 cont->screen() == myScreen &&
82 (cont->desktop() == -1 || cont->desktop() == KWindowSystem::currentDesktop()-1)) {
83 m_view = qobject_cast<Plasma::View *>(cont->view());
84 m_activeContainment = m_containments.count() - 1;
85 m_tabBar->setCurrentIndex(m_activeContainment);
88 connect(cont, SIGNAL(destroyed(QObject *)), this, SLOT(containmentDestroyed(QObject *)));
89 connect(cont, SIGNAL(screenChanged(int, int, Plasma::Containment *)), this, SLOT(screenChanged(int, int, Plasma::Containment *)));
90 connect(cont, SIGNAL(contextChanged(Plasma::Context *)), this, SLOT(contextChanged(Plasma::Context *)));
93 connect(c, SIGNAL(containmentAdded(Plasma::Containment *)), this, SLOT(containmentAdded(Plasma::Containment *)));
96 if (m_containments.count() > 1) {
97 connect(m_tabBar, SIGNAL(currentChanged(int)), this, SLOT(switchContainment(int)));
100 connect(KWindowSystem::self(), SIGNAL(currentDesktopChanged(int)), this, SLOT(currentDesktopChanged(int)));
102 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
103 setPreferredSize(m_tabBar->nativeWidget()->sizeHint());
104 emit sizeHintChanged(Qt::PreferredSize);
107 void ActivityBar::constraintsEvent(Plasma::Constraints constraints)
109 if (constraints & Plasma::FormFactorConstraint ) {
110 if (formFactor() == Plasma::Vertical) {
111 m_tabBar->nativeWidget()->setShape(QTabBar::RoundedWest);
112 } else {
113 m_tabBar->nativeWidget()->setShape(QTabBar::RoundedNorth);
115 setPreferredSize(m_tabBar->nativeWidget()->sizeHint());
116 emit sizeHintChanged(Qt::PreferredSize);
118 if (constraints & Plasma::SizeConstraint) {
119 setPreferredSize(m_tabBar->nativeWidget()->sizeHint());
120 setMinimumSize(m_tabBar->nativeWidget()->minimumSize());
124 void ActivityBar::switchContainment(int newActive)
126 if (newActive == m_activeContainment || !containment()) {
127 return;
130 Plasma::Corona *c = containment()->corona();
131 if (!c) {
132 return;
135 const int myScreen = containment()->screen();
137 //FIXME: this whole thing sounds like an hack isn't it?
138 if (!m_view || m_view->screen() != myScreen) {
139 Plasma::Containment *cont = c->containmentForScreen(containment()->screen(), containment()->desktop() - 1);
140 if (cont) {
141 m_view = qobject_cast<Plasma::View *>(cont->view());
145 if (m_view && m_view->screen() != myScreen) {
146 m_view = 0;
147 return;
150 if (m_view) {
151 m_activeContainment = newActive;
152 Plasma::Containment *cont = m_containments[newActive];
154 m_view->setContainment(cont);
158 void ActivityBar::currentDesktopChanged(const int currentDesktop)
160 Plasma::Corona *c = containment()->corona();
161 if (!c) {
162 return;
165 //-1 because kwindowsystem counts desktop from 1 :)
166 Plasma::Containment *cont = c->containmentForScreen(containment()->screen(), currentDesktop - 1);
168 if (!cont) {
169 return;
172 int index = m_containments.indexOf(cont);
174 if (index != -1 &&
175 index != m_activeContainment) {
176 m_activeContainment = index;
177 m_tabBar->setCurrentIndex(index);
178 m_view = qobject_cast<Plasma::View *>(cont->view());
182 void ActivityBar::containmentAdded(Plasma::Containment *cont)
184 if (cont->containmentType() == Plasma::Containment::PanelContainment || m_containments.contains(cont)) {
185 return;
188 m_containments.append(cont);
189 if (cont->activity().isNull()) {
190 m_tabBar->addTab(cont->name());
191 } else {
192 m_tabBar->addTab(cont->activity());
195 if (m_containments.count() > 1) {
196 connect(m_tabBar, SIGNAL(currentChanged(int)), this, SLOT(switchContainment(int)));
199 connect(cont, SIGNAL(destroyed(QObject *)), this, SLOT(containmentDestroyed(QObject *)));
200 connect(cont, SIGNAL(screenChanged(int, int, Plasma::Containment *)), this, SLOT(screenChanged(int, int, Plasma::Containment *)));
201 connect(cont, SIGNAL(contextChanged(Plasma::Context *)), this, SLOT(contextChanged(Plasma::Context *)));
203 setPreferredSize(m_tabBar->nativeWidget()->sizeHint());
204 emit sizeHintChanged(Qt::PreferredSize);
207 void ActivityBar::containmentDestroyed(QObject *obj)
209 Plasma::Containment *containment = static_cast<Plasma::Containment *>(obj);
211 int index = m_containments.indexOf(containment);
212 if (index != -1) {
213 if (index < m_activeContainment) {
214 --m_activeContainment;
217 if (m_containments.count() == 1) {
218 disconnect(m_tabBar, SIGNAL(currentChanged(int)), this, SLOT(switchContainment(int)));
221 m_containments.removeAt(index);
222 m_tabBar->removeTab(index);
225 setPreferredSize(m_tabBar->nativeWidget()->sizeHint());
226 emit sizeHintChanged(Qt::PreferredSize);
229 void ActivityBar::screenChanged(int wasScreen, int isScreen, Plasma::Containment *cont)
231 Q_UNUSED(wasScreen)
232 //Q_UNUSED(isScreen)
234 int index = m_containments.indexOf(cont);
236 //FIXME: how is supposed to work containment()->desktop() when the pervirtialthing is off?
237 if (index != -1 &&
238 index != m_activeContainment &&
239 containment()->screen() == isScreen &&
240 (cont->desktop() == -1 || cont->desktop() == KWindowSystem::currentDesktop()-1)) {
241 m_activeContainment = index;
242 m_tabBar->setCurrentIndex(index);
243 m_view = qobject_cast<Plasma::View *>(cont->view());
247 void ActivityBar::contextChanged(Plasma::Context *context)
249 Plasma::Containment *cont = qobject_cast<Plasma::Containment *>(sender());
251 if (!cont) {
252 return;
255 int index = m_containments.indexOf(cont);
256 if (index != -1) {
257 m_tabBar->setTabText(index, context->currentActivity());
261 #include "activitybar.moc"