Applets
taskarea.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "taskarea.h"
00023
00024 #include <QtCore/QSet>
00025 #include <QtGui/QApplication>
00026 #include <QtGui/QGraphicsLinearLayout>
00027 #include <QtGui/QWidget>
00028
00029 #include <KIcon>
00030
00031 #include <Plasma/IconWidget>
00032
00033 #include "../core/manager.h"
00034 #include "../core/task.h"
00035
00036 #include "applet.h"
00037 #include "compactlayout.h"
00038
00039
00040 namespace SystemTray
00041 {
00042
00043
00044 class TaskArea::Private
00045 {
00046 public:
00047 Private(SystemTray::Applet *h)
00048 : host(h),
00049 unhider(0),
00050 topLayout(new QGraphicsLinearLayout(Qt::Horizontal)),
00051 taskLayout(new CompactLayout()),
00052 lastItemMargin(0),
00053 lastItemCount(0),
00054 showingHidden(false),
00055 hasHiddenTasks(false),
00056 hasTasksThatCanHide(false)
00057 {
00058 }
00059
00060 SystemTray::Applet *host;
00061 Plasma::IconWidget *unhider;
00062 QGraphicsLinearLayout *topLayout;
00063 CompactLayout *taskLayout;
00064
00065 QGraphicsWidget *lastItemMargin;
00066
00067 QSet<QString> hiddenTypes;
00068 int lastItemCount;
00069 bool showingHidden : 1;
00070 bool hasHiddenTasks : 1;
00071 bool hasTasksThatCanHide : 1;
00072 };
00073
00074
00075 TaskArea::TaskArea(SystemTray::Applet *parent)
00076 : QGraphicsWidget(parent),
00077 d(new Private(parent))
00078 {
00079 setLayout(d->topLayout);
00080 d->topLayout->addItem(d->taskLayout);
00081 d->topLayout->setContentsMargins(0, 0, 0, 0);
00082 }
00083
00084
00085 TaskArea::~TaskArea()
00086 {
00087 delete d;
00088 }
00089
00090
00091 void TaskArea::setHiddenTypes(const QStringList &hiddenTypes)
00092 {
00093 d->hiddenTypes = QSet<QString>::fromList(hiddenTypes);
00094 }
00095
00096 bool TaskArea::isHiddenType(const QString &typeId, bool always) const
00097 {
00098 if (always) {
00099 return !d->showingHidden && d->hiddenTypes.contains(typeId);
00100 } else {
00101 return d->hiddenTypes.contains(typeId);
00102 }
00103 }
00104
00105 void TaskArea::syncTasks(const QList<SystemTray::Task*> &tasks)
00106 {
00107 d->hasTasksThatCanHide = false;
00108 d->hasHiddenTasks = false;
00109 foreach (Task *task, tasks) {
00110 kDebug() << "checking" << task->name() << d->showingHidden;
00111 addWidgetForTask(task);
00112 }
00113
00114 checkUnhideTool();
00115 d->topLayout->invalidate();
00116 emit sizeHintChanged(Qt::PreferredSize);
00117 }
00118
00119 void TaskArea::addTask(Task *task)
00120 {
00121 addWidgetForTask(task);
00122 checkUnhideTool();
00123 emit sizeHintChanged(Qt::PreferredSize);
00124 }
00125
00126 void TaskArea::addWidgetForTask(SystemTray::Task *task)
00127 {
00128 QGraphicsWidget *widget = findWidget(task);
00129 if (!task->isEmbeddable() && !widget) {
00130 kDebug() << "task is not embeddable, so FAIL" << task->name();
00131 return;
00132 }
00133
00134 d->hasTasksThatCanHide = d->hasTasksThatCanHide || isHiddenType(task->typeId(), false);
00135
00136 if (isHiddenType(task->typeId())) {
00137 kDebug() << "is a hidden type";
00138 d->hasHiddenTasks = true;
00139 if (widget) {
00140 kDebug() << "just hiding the widget";
00141 widget->hide();
00142 }
00143 } else if (widget) {
00144 kDebug() << "widget already exists!";
00145 widget->show();
00146 } else {
00147 widget = task->widget(d->host);
00148
00149 if (widget) {
00150 switch (task->order()) {
00151 case SystemTray::Task::First:
00152 d->taskLayout->insertItem(0, widget);
00153 break;
00154 case SystemTray::Task::Normal:
00155 d->taskLayout->insertItem(d->taskLayout->count() - d->lastItemCount, widget);
00156 break;
00157 case SystemTray::Task::Last:
00158
00159
00160 if (d->lastItemCount == 0) {
00161 QGraphicsWidget *applet = dynamic_cast<QGraphicsWidget *>(parentItem());
00162
00163 if (applet) {
00164 qreal left, top, right, bottom;
00165 applet->getContentsMargins(&left, &top, &right, &bottom);
00166 d->lastItemMargin = new QGraphicsWidget();
00167
00168 d->lastItemMargin->setMinimumSize(right, bottom);
00169 }
00170 }
00171 ++d->lastItemCount;
00172 d->taskLayout->addItem(widget);
00173 break;
00174 }
00175 }
00176 }
00177 }
00178
00179 void TaskArea::checkSizes()
00180 {
00181 d->taskLayout->updateGeometry();
00182 d->topLayout->updateGeometry();
00183
00184
00185 QSizeF s = d->taskLayout->effectiveSizeHint(Qt::PreferredSize);
00186 if (d->unhider) {
00187 if (d->topLayout->orientation() == Qt::Horizontal) {
00188 s.setWidth(s.width() + d->unhider->size().width());
00189 } else {
00190 s.setHeight(s.height() + d->unhider->size().height());
00191 }
00192 }
00193
00194 setPreferredSize(s);
00195 }
00196
00197 void TaskArea::removeTask(Task *task)
00198 {
00199 foreach (QGraphicsWidget *widget, task->associatedWidgets()) {
00200 if (d->taskLayout->containsItem(widget)) {
00201 if (task->order() == Task::Last) {
00202 --d->lastItemCount;
00203
00204 if (d->lastItemCount == 0 && d->lastItemMargin) {
00205 d->taskLayout->removeItem(d->lastItemMargin);
00206 d->lastItemMargin->deleteLater();
00207 d->lastItemMargin = 0;
00208 }
00209 }
00210
00211 d->taskLayout->removeItem(widget);
00212 d->topLayout->invalidate();
00213 emit sizeHintChanged(Qt::PreferredSize);
00214 break;
00215 }
00216 }
00217 }
00218
00219 int TaskArea::leftEasement() const
00220 {
00221 if (d->unhider) {
00222 const int cheat = 6;
00223
00224 if (d->topLayout->orientation() == Qt::Horizontal) {
00225 return d->unhider->size().width() / 2 + cheat;
00226 } else {
00227 return d->unhider->size().height() / 2 + cheat;
00228 }
00229 }
00230
00231 return 0;
00232 }
00233
00234 int TaskArea::rightEasement() const
00235 {
00236 int extraMargin = 0;
00237 if (d->lastItemMargin) {
00238 extraMargin = qMin(d->lastItemMargin->size().width(), d->lastItemMargin->size().height());
00239 }
00240 return d->lastItemCount * 24 + int(qreal(extraMargin)/2.0);
00241 }
00242
00243 bool TaskArea::hasHiddenTasks() const
00244 {
00245 return d->hasHiddenTasks;
00246 }
00247
00248 void TaskArea::setOrientation(Qt::Orientation o)
00249 {
00250 d->topLayout->setOrientation(o);
00251
00252 if (d->unhider) {
00253 d->unhider->setOrientation(o);
00254 if (d->topLayout->orientation() == Qt::Horizontal) {
00255 d->unhider->setMaximumSize(26, QWIDGETSIZE_MAX);
00256 d->unhider->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
00257 } else {
00258 d->unhider->setMaximumSize(QWIDGETSIZE_MAX, 26);
00259 d->unhider->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
00260 }
00261 }
00262 updateUnhideToolIcon();
00263 }
00264
00265 void TaskArea::initUnhideTool()
00266 {
00267 if (d->unhider) {
00268 return;
00269 }
00270
00271 d->unhider = new Plasma::IconWidget(this);
00272 d->unhider->setMinimumSize(16, 16);
00273 updateUnhideToolIcon();
00274
00275 if (d->topLayout->orientation() == Qt::Horizontal) {
00276 d->unhider->setMaximumSize(22, QWIDGETSIZE_MAX);
00277 d->unhider->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
00278 } else {
00279 d->unhider->setMaximumSize(QWIDGETSIZE_MAX, 22);
00280 d->unhider->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
00281 }
00282
00283 d->topLayout->removeItem(d->taskLayout);
00284
00285 d->topLayout->addItem(d->unhider);
00286 d->topLayout->addItem(d->taskLayout);
00287 connect(d->unhider, SIGNAL(clicked()), this, SLOT(toggleHiddenItems()));
00288
00289 emit sizeHintChanged(Qt::PreferredSize);
00290 }
00291
00292 void TaskArea::updateUnhideToolIcon()
00293 {
00294 if (!d->unhider) {
00295 return;
00296 }
00297
00298 if (!d->showingHidden && d->topLayout->orientation() == Qt::Vertical) {
00299 d->unhider->setSvg("widgets/systemtray", "expander-up");
00300 } else if(d->showingHidden && d->topLayout->orientation() == Qt::Vertical){
00301 d->unhider->setSvg("widgets/systemtray", "expander-down");
00302 }else if (d->showingHidden || QApplication::layoutDirection() == Qt::RightToLeft) {
00303 d->unhider->setSvg("widgets/systemtray", "expander-right");
00304 } else {
00305 d->unhider->setSvg("widgets/systemtray", "expander-left");
00306 }
00307 }
00308
00309 void TaskArea::toggleHiddenItems()
00310 {
00311 d->showingHidden = !d->showingHidden;
00312 updateUnhideToolIcon();
00313 syncTasks(d->host->manager()->tasks());
00314 emit sizeHintChanged(Qt::PreferredSize);
00315 }
00316
00317 void TaskArea::checkUnhideTool()
00318 {
00319 if (d->hasTasksThatCanHide) {
00320 initUnhideTool();
00321 } else {
00322
00323 d->topLayout->removeItem(d->unhider);
00324 d->unhider->deleteLater();
00325 d->unhider = 0;
00326 }
00327 }
00328
00329 QGraphicsWidget* TaskArea::findWidget(Task *task)
00330 {
00331 foreach (QGraphicsWidget *widget, task->associatedWidgets()) {
00332 if (d->taskLayout->containsItem(widget)) {
00333 return widget;
00334 }
00335 }
00336
00337 return 0;
00338 }
00339
00340
00341 }
00342
00343 #include "taskarea.moc"