00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "activitybar.h"
00021
00022 #include <QGraphicsLinearLayout>
00023
00024 #include <KWindowSystem>
00025
00026 #include <Plasma/View>
00027 #include <Plasma/Corona>
00028 #include <Plasma/Context>
00029 #include <Plasma/Containment>
00030 #include <Plasma/TabBar>
00031
00032
00033 ActivityBar::ActivityBar(QObject *parent, const QVariantList &args)
00034 : Plasma::Applet(parent, args),
00035 m_activeContainment(-1)
00036 {
00037 resize(200, 60);
00038 setAspectRatioMode(Plasma::IgnoreAspectRatio);
00039 setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding));
00040 }
00041
00042
00043 ActivityBar::~ActivityBar()
00044 {
00045 }
00046
00047 void ActivityBar::init()
00048 {
00049 QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(this);
00050 m_tabBar = new Plasma::TabBar(this);
00051 layout->addItem(m_tabBar);
00052 layout->setContentsMargins(0,0,0,0);
00053 layout->setSizePolicy(QSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding));
00054
00055 if (containment()) {
00056 Plasma::Corona *c = containment()->corona();
00057
00058 if (!c) {
00059 kDebug() << "No corona, can't happen";
00060 setFailedToLaunch(true);
00061 return;
00062 }
00063
00064 int myScreen = containment()->screen();
00065
00066 QList<Plasma::Containment*> containments = c->containments();
00067 foreach (Plasma::Containment *cont, containments) {
00068 if (cont->containmentType() == Plasma::Containment::PanelContainment) {
00069 continue;
00070 }
00071
00072 m_containments.append(cont);
00073
00074 if (cont->activity().isNull()) {
00075 m_tabBar->addTab(cont->name());
00076 } else {
00077 m_tabBar->addTab(cont->activity());
00078 }
00079
00080 if (cont->screen() != -1 &&
00081 cont->screen() == myScreen &&
00082 (cont->desktop() == -1 || cont->desktop() == KWindowSystem::currentDesktop()-1)) {
00083 m_view = qobject_cast<Plasma::View *>(cont->view());
00084 m_activeContainment = m_containments.count() - 1;
00085 m_tabBar->setCurrentIndex(m_activeContainment);
00086 }
00087
00088 connect(cont, SIGNAL(destroyed(QObject *)), this, SLOT(containmentDestroyed(QObject *)));
00089 connect(cont, SIGNAL(screenChanged(int, int, Plasma::Containment *)), this, SLOT(screenChanged(int, int, Plasma::Containment *)));
00090 connect(cont, SIGNAL(contextChanged(Plasma::Context *)), this, SLOT(contextChanged(Plasma::Context *)));
00091 }
00092
00093 connect(c, SIGNAL(containmentAdded(Plasma::Containment *)), this, SLOT(containmentAdded(Plasma::Containment *)));
00094 }
00095
00096 if (m_containments.count() > 1) {
00097 connect(m_tabBar, SIGNAL(currentChanged(int)), this, SLOT(switchContainment(int)));
00098 }
00099
00100 connect(KWindowSystem::self(), SIGNAL(currentDesktopChanged(int)), this, SLOT(currentDesktopChanged(int)));
00101
00102 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
00103 setPreferredSize(m_tabBar->nativeWidget()->sizeHint());
00104 emit sizeHintChanged(Qt::PreferredSize);
00105 }
00106
00107 void ActivityBar::constraintsEvent(Plasma::Constraints constraints)
00108 {
00109 if (constraints & Plasma::FormFactorConstraint ) {
00110 if (formFactor() == Plasma::Vertical) {
00111 m_tabBar->nativeWidget()->setShape(QTabBar::RoundedWest);
00112 } else {
00113 m_tabBar->nativeWidget()->setShape(QTabBar::RoundedNorth);
00114 }
00115 setPreferredSize(m_tabBar->nativeWidget()->sizeHint());
00116 emit sizeHintChanged(Qt::PreferredSize);
00117 }
00118 if (constraints & Plasma::SizeConstraint) {
00119 setPreferredSize(m_tabBar->nativeWidget()->sizeHint());
00120 setMinimumSize(m_tabBar->nativeWidget()->minimumSize());
00121 }
00122 }
00123
00124 void ActivityBar::switchContainment(int newActive)
00125 {
00126 if (newActive == m_activeContainment || !containment()) {
00127 return;
00128 }
00129
00130 Plasma::Corona *c = containment()->corona();
00131 if (!c) {
00132 return;
00133 }
00134
00135 const int myScreen = containment()->screen();
00136
00137
00138 if (!m_view || m_view->screen() != myScreen) {
00139 Plasma::Containment *cont = c->containmentForScreen(containment()->screen(), containment()->desktop() - 1);
00140 if (cont) {
00141 m_view = qobject_cast<Plasma::View *>(cont->view());
00142 }
00143 }
00144
00145 if (m_view && m_view->screen() != myScreen) {
00146 m_view = 0;
00147 return;
00148 }
00149
00150 if (m_view) {
00151 m_activeContainment = newActive;
00152 Plasma::Containment *cont = m_containments[newActive];
00153
00154 m_view->setContainment(cont);
00155 }
00156 }
00157
00158 void ActivityBar::currentDesktopChanged(const int currentDesktop)
00159 {
00160 Plasma::Corona *c = containment()->corona();
00161 if (!c) {
00162 return;
00163 }
00164
00165
00166 Plasma::Containment *cont = c->containmentForScreen(containment()->screen(), currentDesktop - 1);
00167
00168 if (!cont) {
00169 return;
00170 }
00171
00172 int index = m_containments.indexOf(cont);
00173
00174 if (index != -1 &&
00175 index != m_activeContainment) {
00176 m_activeContainment = index;
00177 m_tabBar->setCurrentIndex(index);
00178 m_view = qobject_cast<Plasma::View *>(cont->view());
00179 }
00180 }
00181
00182 void ActivityBar::containmentAdded(Plasma::Containment *cont)
00183 {
00184 if (cont->containmentType() == Plasma::Containment::PanelContainment || m_containments.contains(cont)) {
00185 return;
00186 }
00187
00188 m_containments.append(cont);
00189 if (cont->activity().isNull()) {
00190 m_tabBar->addTab(cont->name());
00191 } else {
00192 m_tabBar->addTab(cont->activity());
00193 }
00194
00195 if (m_containments.count() > 1) {
00196 connect(m_tabBar, SIGNAL(currentChanged(int)), this, SLOT(switchContainment(int)));
00197 }
00198
00199 connect(cont, SIGNAL(destroyed(QObject *)), this, SLOT(containmentDestroyed(QObject *)));
00200 connect(cont, SIGNAL(screenChanged(int, int, Plasma::Containment *)), this, SLOT(screenChanged(int, int, Plasma::Containment *)));
00201 connect(cont, SIGNAL(contextChanged(Plasma::Context *)), this, SLOT(contextChanged(Plasma::Context *)));
00202
00203 setPreferredSize(m_tabBar->nativeWidget()->sizeHint());
00204 emit sizeHintChanged(Qt::PreferredSize);
00205 }
00206
00207 void ActivityBar::containmentDestroyed(QObject *obj)
00208 {
00209 Plasma::Containment *containment = static_cast<Plasma::Containment *>(obj);
00210
00211 int index = m_containments.indexOf(containment);
00212 if (index != -1) {
00213 if (index < m_activeContainment) {
00214 --m_activeContainment;
00215 }
00216
00217 if (m_containments.count() == 1) {
00218 disconnect(m_tabBar, SIGNAL(currentChanged(int)), this, SLOT(switchContainment(int)));
00219 }
00220
00221 m_containments.removeAt(index);
00222 m_tabBar->removeTab(index);
00223 }
00224
00225 setPreferredSize(m_tabBar->nativeWidget()->sizeHint());
00226 emit sizeHintChanged(Qt::PreferredSize);
00227 }
00228
00229 void ActivityBar::screenChanged(int wasScreen, int isScreen, Plasma::Containment *cont)
00230 {
00231 Q_UNUSED(wasScreen)
00232
00233
00234 int index = m_containments.indexOf(cont);
00235
00236
00237 if (index != -1 &&
00238 index != m_activeContainment &&
00239 containment()->screen() == isScreen &&
00240 (cont->desktop() == -1 || cont->desktop() == KWindowSystem::currentDesktop()-1)) {
00241 m_activeContainment = index;
00242 m_tabBar->setCurrentIndex(index);
00243 m_view = qobject_cast<Plasma::View *>(cont->view());
00244 }
00245 }
00246
00247 void ActivityBar::contextChanged(Plasma::Context *context)
00248 {
00249 Plasma::Containment *cont = qobject_cast<Plasma::Containment *>(sender());
00250
00251 if (!cont) {
00252 return;
00253 }
00254
00255 int index = m_containments.indexOf(cont);
00256 if (index != -1) {
00257 m_tabBar->setTabText(index, context->currentActivity());
00258 }
00259 }
00260
00261 #include "activitybar.moc"