• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Applets

windowtaskitem.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by Robert Knight <robertknight@gmail.com>          *
00003  *   Copyright (C) 2008 by Alexis Ménard <darktears31@gmail.com>           *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
00019  ***************************************************************************/
00020 
00021 // Own
00022 #include "windowtaskitem.h"
00023 
00024 // Qt
00025 #include <QGraphicsSceneContextMenuEvent>
00026 #include <QStyleOptionGraphicsItem>
00027 #include <QGraphicsView>
00028 #include <QTimer>
00029 #include <QApplication>
00030 
00031 // KDE
00032 #include <KAuthorized>
00033 #include <KDebug>
00034 #include <KIcon>
00035 #include <KLocalizedString>
00036 #include <KGlobalSettings>
00037 #include <KIconLoader>
00038 
00039 #include <taskmanager/taskrmbmenu.h>
00040 
00041 #include "plasma/theme.h"
00042 #include "plasma/paintutils.h"
00043 #include "plasma/panelsvg.h"
00044 
00045 #include "tasks.h"
00046 
00047 WindowTaskItem::WindowTaskItem(Tasks *parent, const bool showTooltip)
00048     : QGraphicsWidget(parent),
00049       m_applet(parent),
00050       m_activateTimer(0),
00051       m_flags(0),
00052       m_animId(0),
00053       m_alpha(1),
00054       m_fadeIn(true),
00055       m_updateTimerId(0),
00056       m_attentionTimerId(0),
00057       m_attentionTicks(0)
00058 {
00059     m_showTooltip = showTooltip;
00060     setSizePolicy(QSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding));
00061     setAcceptsHoverEvents(true);
00062     setAcceptDrops(true);
00063 
00064     QFontMetrics fm(KGlobalSettings::taskbarFont());
00065     QSize mSize = fm.size(0, "M");
00066     setPreferredSize(QSize(mSize.width()*15 + m_applet->itemLeftMargin() + m_applet->itemRightMargin() + IconSize(KIconLoader::Panel),
00067                            mSize.height()*3 + m_applet->itemTopMargin() + m_applet->itemBottomMargin()));
00068 }
00069 
00070 void WindowTaskItem::activate()
00071 {
00072     // the Task class has a method called activateRaiseOrIconify() which
00073     // should perform the required action here.
00074     //
00075     // however it currently does not minimize the task's window if the item
00076     // is clicked whilst the window is active probably because the active window by
00077     // the time the mouse is released over the window task button is not the
00078     // task's window but instead the desktop window
00079     //
00080     // TODO: the Kicker panel in KDE 3.x has a feature whereby clicking on it
00081     // does not take away the focus from the active window (unless clicking
00082     // in a widget such as a line edit which does accept the focus)
00083     // this needs to be implemented for Plasma's own panels.
00084     if (m_task) {
00085         m_task->activateRaiseOrIconify();
00086         emit windowSelected(this);
00087     }
00088 }
00089 
00090 void WindowTaskItem::close()
00091 {
00092     if (m_task) {
00093         m_task->close();
00094     }
00095 }
00096 
00097 void WindowTaskItem::setShowTooltip(const bool showit)
00098 {
00099     m_showTooltip = showit;
00100     updateTask();
00101 }
00102 
00103 void WindowTaskItem::setText(const QString &text)
00104 {
00105     m_text = text;
00106 }
00107 
00108 void WindowTaskItem::setIcon(const QIcon &icon)
00109 {
00110     m_icon = icon; //icon.pixmap(MinTaskIconSize);
00111 }
00112 
00113 void WindowTaskItem::setTaskFlags(TaskFlags flags)
00114 {
00115     if ((m_flags & TaskWantsAttention) != (flags & TaskWantsAttention)) {
00116         //kDebug() << "task attention state changed" << m_attentionTimerId;
00117         if (flags & TaskWantsAttention) {
00118             // start attention getting
00119             if (!m_attentionTimerId) {
00120                 m_attentionTimerId = startTimer(500);
00121             }
00122         } else if (m_attentionTimerId) {
00123             killTimer(m_attentionTimerId);
00124             m_attentionTimerId = 0;
00125             // stop attention getting
00126         }
00127     }
00128 
00129     m_flags = flags;
00130 }
00131 
00132 WindowTaskItem::TaskFlags WindowTaskItem::taskFlags() const
00133 {
00134     return m_flags;
00135 }
00136 
00137 void WindowTaskItem::queueUpdate()
00138 {
00139     if (m_updateTimerId || m_attentionTimerId) {
00140         return;
00141     }
00142 
00143     if (m_lastUpdate.elapsed() < 200) {
00144         m_updateTimerId = startTimer(200);
00145         return;
00146     }
00147 
00148     update();
00149     m_lastUpdate.restart();
00150 }
00151 
00152 void WindowTaskItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
00153 {
00154     const int FadeInDuration = 75;
00155 
00156     if (m_animId) {
00157         Plasma::Animator::self()->stopCustomAnimation(m_animId);
00158     }
00159 
00160     m_fadeIn = true;
00161     m_animId = Plasma::Animator::self()->customAnimation(40 / (1000 / FadeInDuration), FadeInDuration,Plasma::Animator::LinearCurve, this, "animationUpdate");
00162 
00163     QGraphicsWidget::hoverEnterEvent(event);
00164 }
00165 
00166 void WindowTaskItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
00167 {
00168     const int FadeOutDuration = 150;
00169 
00170     if (m_animId) {
00171         Plasma::Animator::self()->stopCustomAnimation(m_animId);
00172     }
00173 
00174     m_fadeIn = false;
00175     m_animId = Plasma::Animator::self()->customAnimation(40 / (1000 / FadeOutDuration), FadeOutDuration,Plasma::Animator::LinearCurve, this, "animationUpdate");
00176 
00177     QGraphicsWidget::hoverLeaveEvent(event);
00178 }
00179 
00180 void WindowTaskItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *)
00181 {
00182     activate();
00183 }
00184 
00185 void WindowTaskItem::mousePressEvent(QGraphicsSceneMouseEvent *)
00186 {
00187     update();
00188 }
00189 
00190 void WindowTaskItem::timerEvent(QTimerEvent *event)
00191 {
00192     if (event->timerId() == m_updateTimerId) {
00193         killTimer(m_updateTimerId);
00194         update();
00195         m_updateTimerId = 0;
00196     } else if (event->timerId() == m_attentionTimerId) {
00197         ++m_attentionTicks;
00198         if (m_attentionTicks > 6) {
00199             killTimer(m_attentionTimerId);
00200             m_attentionTimerId = 0;
00201             m_attentionTicks = 0;
00202         }
00203 
00204         update();
00205     }
00206 }
00207 
00208 void WindowTaskItem::resizeEvent(QGraphicsSceneResizeEvent *event)
00209 {
00210     // we need to lose precision here because all our drawing later on
00211     // is done with ints, not floats
00212     m_applet->resizeItemBackground(event->newSize().toSize());
00213 }
00214 
00215 void WindowTaskItem::paint(QPainter *painter,
00216                            const QStyleOptionGraphicsItem *option,
00217                            QWidget *widget)
00218 {
00219     painter->setRenderHint(QPainter::Antialiasing);
00220 
00221     // draw background
00222     drawBackground(painter, option, widget);
00223 
00224     // draw icon and text
00225     drawTask(painter, option, widget);
00226 }
00227 
00228 void WindowTaskItem::drawBackground(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
00229 {
00230     // FIXME  Check the usage of KColorScheme here with various color schemes
00231 
00232     //Don't paint with invalid sizes, the happens when the layout i's being initialized
00233     if (!option->rect.isValid()) {
00234         return;
00235     }
00236 
00237     const qreal hoverAlpha = 0.4;
00238     bool hasSvg = false;
00239 
00240     Plasma::PanelSvg *itemBackground = m_applet->itemBackground();
00241 
00242     if ((m_flags & TaskWantsAttention) && !(m_attentionTicks % 2)) {
00243         if (itemBackground && itemBackground->hasElementPrefix("attention")) {
00244             //Draw task background from theme svg "attention" element
00245             itemBackground->setElementPrefix("attention");
00246             hasSvg = true;
00247         } else {
00248             //Draw task background without svg theming
00249             QColor background = m_applet->colorScheme()->background(KColorScheme::ActiveBackground).color();
00250             background.setAlphaF(hoverAlpha+0.2);
00251             painter->setBrush(QBrush(background));
00252             painter->drawPath(Plasma::PaintUtils::roundedRectangle(option->rect, 6));
00253         }
00254     } else if (m_flags & TaskIsMinimized) {
00255         if (itemBackground && itemBackground->hasElementPrefix("minimized")) {
00256             //Draw task background from theme svg "attention" element
00257             itemBackground->setElementPrefix("minimized");
00258             hasSvg = true;
00259         } else {
00260             //Not painting anything by default
00261             painter->setBrush(QBrush());
00262         }
00263     } else if (m_flags & TaskHasFocus) {
00264             if (itemBackground && itemBackground->hasElementPrefix("focus")) {
00265                 //Draw task background from theme svg "focus" element
00266                 itemBackground->setElementPrefix("focus");
00267                 hasSvg = true;
00268             } else {
00269                 //Draw task background without svg theming
00270                 QLinearGradient background(boundingRect().topLeft(), boundingRect().bottomLeft());
00271 
00272                 QColor startColor = m_applet->colorScheme()->background(KColorScheme::NormalBackground).color();
00273                 QColor endColor = m_applet->colorScheme()->shade(startColor,KColorScheme::DarkShade);
00274 
00275                 endColor.setAlphaF(qMin(0.8,startColor.alphaF()+0.2));
00276                 startColor.setAlphaF(0);
00277 
00278                 background.setColorAt(0, startColor);
00279                 background.setColorAt(1, endColor);
00280 
00281                 painter->setBrush(background);
00282                 painter->setPen(Plasma::Theme::defaultTheme()->color(Plasma::Theme::BackgroundColor));
00283 
00284                 painter->drawPath(Plasma::PaintUtils::roundedRectangle(option->rect, 6));
00285             }
00286     //Default is a normal task
00287     } else {
00288         if (itemBackground && itemBackground->hasElementPrefix("normal")) {
00289             //Draw task background from theme svg "normal" element
00290             itemBackground->setElementPrefix("normal");
00291             hasSvg = true;
00292         } else {
00293             //Draw task background without svg theming
00294             KColorScheme *colorScheme = m_applet->colorScheme();
00295             QColor background = colorScheme->shade(colorScheme->background(KColorScheme::AlternateBackground).color(),
00296                                                    KColorScheme::DarkShade);
00297             background.setAlphaF(0.2);
00298             painter->setBrush(QBrush(background));
00299             painter->setPen(Plasma::Theme::defaultTheme()->color(Plasma::Theme::BackgroundColor));
00300 
00301             painter->drawPath(Plasma::PaintUtils::roundedRectangle(option->rect, 6));
00302         }
00303     }
00304 
00305     //Draw task background fading away if needed
00306     if (hasSvg) {
00307         if (!m_animId) {
00308              if (~option->state & QStyle::State_MouseOver) {
00309                  itemBackground->paintPanel(painter, rect().toRect());
00310              }
00311         } else {
00312             QPixmap *alphaPixmap = m_applet->taskAlphaPixmap(itemBackground->panelSize().toSize());
00313             //kDebug() << (QObject*)this << "setting alpha to" << (255 * (1.0 - m_alpha)) << m_alpha;
00314             if (m_alpha < 0.95) {
00315                 alphaPixmap->fill(QColor(0, 0, 0, 255 * (1.0 - m_alpha)));
00316             } else {
00317                 alphaPixmap->fill(Qt::transparent);
00318             }
00319 
00320             {
00321                 QPainter buffPainter(alphaPixmap);
00322                 buffPainter.setCompositionMode(QPainter::CompositionMode_SourceIn);
00323                 itemBackground->paintPanel(&buffPainter, alphaPixmap->rect());
00324             }
00325 
00326             painter->drawPixmap(QPoint(0, 0), *alphaPixmap);
00327         }
00328     }
00329 
00330     if (option->state & QStyle::State_MouseOver || m_animId) {
00331         if (itemBackground && itemBackground->hasElementPrefix("hover")) {
00332             if ((!m_animId || m_alpha == 1) && (~option->state & QStyle::State_Sunken)) {
00333                 itemBackground->setElementPrefix("hover");
00334                 itemBackground->paintPanel(painter, rect().toRect());
00335             } else {
00336                 //Draw task background from theme svg "hover" element
00337                 QPixmap *alphaPixmap = m_applet->taskAlphaPixmap(itemBackground->panelSize().toSize());
00338 
00339                 if (option->state & QStyle::State_Sunken) {
00340                     alphaPixmap->fill(QColor(0, 0, 0, 50));
00341                 } else if (m_alpha < 0.9) {
00342                     alphaPixmap->fill(QColor(0, 0, 0, 255 * m_alpha));
00343                 } else {
00344                     alphaPixmap->fill(Qt::transparent);
00345                 }
00346 
00347                 {
00348                     QPainter buffPainter(alphaPixmap);
00349                     buffPainter.setCompositionMode(QPainter::CompositionMode_SourceIn);
00350                     itemBackground->setElementPrefix("hover");
00351                     itemBackground->paintPanel(&buffPainter, alphaPixmap->rect());
00352                 }
00353 
00354                 painter->drawPixmap(QPoint(0, 0), *alphaPixmap);
00355             }
00356         } else {
00357             //Draw task background without svg theming
00358             QLinearGradient background(boundingRect().topLeft(),
00359                                        boundingRect().bottomLeft());
00360 
00361             QColor startColor = m_applet->colorScheme()->background(KColorScheme::AlternateBackground).color();
00362             QColor endColor = m_applet->colorScheme()->shade(startColor,KColorScheme::DarkShade);
00363 
00364             const qreal pressedAlpha = 0.2;
00365 
00366             qreal alpha = 0;
00367 
00368             if (option->state & QStyle::State_Sunken) {
00369                 alpha = pressedAlpha;
00370             } else {
00371                 alpha = hoverAlpha;
00372             }
00373 
00374             startColor.setAlphaF(alpha);
00375             endColor.setAlphaF(m_alpha);
00376 
00377             background.setColorAt(0, startColor);
00378             background.setColorAt(1, endColor);
00379 
00380             painter->setBrush(background);
00381             painter->setPen(Plasma::Theme::defaultTheme()->color(Plasma::Theme::BackgroundColor));
00382 
00383             painter->drawPath(Plasma::PaintUtils::roundedRectangle(option->rect, 6));
00384         }
00385     }
00386 }
00387 
00388 void WindowTaskItem::drawTask(QPainter *painter,const QStyleOptionGraphicsItem *option,QWidget *)
00389 {
00390     Q_UNUSED(option)
00391 
00392     QRectF bounds = boundingRect().adjusted(m_applet->itemLeftMargin(), m_applet->itemTopMargin(), -m_applet->itemRightMargin(), -m_applet->itemBottomMargin());
00393     m_icon.paint(painter, iconRect(bounds).toRect());
00394 
00395     painter->setPen(QPen(Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor), 1.0));
00396 
00397     QRect rect = textRect(bounds).toRect();
00398     if (rect.height() > 20) {
00399         rect.adjust(2, 2, -2, -2); // Create a text margin
00400     }
00401     QTextLayout layout;
00402     layout.setFont(KGlobalSettings::taskbarFont());
00403     layout.setTextOption(textOption());
00404 
00405     layoutText(layout, m_text, rect.size());
00406     drawTextLayout(painter, layout, rect);
00407 }
00408 
00409 QTextOption WindowTaskItem::textOption() const
00410 {
00411     Qt::LayoutDirection direction = QApplication::layoutDirection();
00412     Qt::Alignment alignment = QStyle::visualAlignment(direction, Qt::AlignLeft | Qt::AlignVCenter);
00413 
00414     QTextOption option;
00415     option.setTextDirection(direction);
00416     option.setAlignment(alignment);
00417 
00418     return option;
00419 }
00420 
00421 QSize WindowTaskItem::layoutText(QTextLayout &layout, const QString &text,
00422                                    const QSize &constraints) const
00423 {
00424     QFontMetrics metrics(layout.font());
00425     int leading     = metrics.leading();
00426     int height      = 0;
00427     int maxWidth    = constraints.width();
00428     int widthUsed   = 0;
00429     int lineSpacing = metrics.lineSpacing();
00430     QTextLine line;
00431 
00432     layout.setText(text);
00433 
00434     layout.beginLayout();
00435     while ((line = layout.createLine()).isValid())
00436     {
00437         height += leading;
00438 
00439         // Make the last line that will fit infinitely long.
00440         // drawTextLayout() will handle this by fading the line out
00441         // if it won't fit in the contraints.
00442         if (height + 2 * lineSpacing > constraints.height()) {
00443             line.setPosition(QPoint(0, height));
00444             break;
00445         }
00446 
00447         line.setLineWidth(maxWidth);
00448         line.setPosition(QPoint(0, height));
00449 
00450         height += int(line.height());
00451         widthUsed = int(qMax(qreal(widthUsed), line.naturalTextWidth()));
00452     }
00453     layout.endLayout();
00454 
00455     return QSize(widthUsed, height);
00456 }
00457 
00458 void WindowTaskItem::drawTextLayout(QPainter *painter, const QTextLayout &layout, const QRect &rect) const
00459 {
00460     if (rect.width() < 1 || rect.height() < 1) {
00461         return;
00462     }
00463 
00464     QPixmap pixmap(rect.size());
00465     pixmap.fill(Qt::transparent);
00466 
00467     QPainter p(&pixmap);
00468     p.setPen(painter->pen());
00469 
00470     // Create the alpha gradient for the fade out effect
00471     QLinearGradient alphaGradient(0, 0, 1, 0);
00472     alphaGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
00473     if (layout.textOption().textDirection() == Qt::LeftToRight)
00474     {
00475         alphaGradient.setColorAt(0, QColor(0, 0, 0, 255));
00476         alphaGradient.setColorAt(1, QColor(0, 0, 0, 0));
00477     } else
00478     {
00479         alphaGradient.setColorAt(0, QColor(0, 0, 0, 0));
00480         alphaGradient.setColorAt(1, QColor(0, 0, 0, 255));
00481     }
00482 
00483     QFontMetrics fm(layout.font());
00484     int textHeight = layout.lineCount() * fm.lineSpacing();
00485     QPointF position = textHeight < rect.height() ?
00486             QPointF(0, (rect.height() - textHeight) / 2) : QPointF(0, 0);
00487     QList<QRect> fadeRects;
00488     int fadeWidth = 30;
00489 
00490     // Draw each line in the layout
00491     for (int i = 0; i < layout.lineCount(); i++)
00492     {
00493         QTextLine line = layout.lineAt(i);
00494         line.draw(&p, position);
00495 
00496         // Add a fade out rect to the list if the line is too long
00497         if (line.naturalTextWidth() > rect.width())
00498         {
00499             int x = int(qMin(line.naturalTextWidth(), (qreal)pixmap.width())) - fadeWidth;
00500             int y = int(line.position().y() + position.y());
00501             QRect r = QStyle::visualRect(layout.textOption().textDirection(), pixmap.rect(),
00502                                          QRect(x, y, fadeWidth, int(line.height())));
00503             fadeRects.append(r);
00504         }
00505     }
00506 
00507     // Reduce the alpha in each fade out rect using the alpha gradient
00508     if (!fadeRects.isEmpty())
00509     {
00510         p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
00511         foreach (const QRect &rect, fadeRects) {
00512             p.fillRect(rect, alphaGradient);
00513         }
00514     }
00515 
00516     p.end();
00517 
00518     painter->drawPixmap(rect.topLeft(), pixmap);
00519 }
00520 
00521 void WindowTaskItem::updateTask()
00522 {
00523     Q_ASSERT(m_task);
00524 
00525     // task flags
00526     TaskFlags flags = m_flags;
00527     if (m_task->isActive()) {
00528         flags |= TaskHasFocus;
00529         emit activated(this);
00530     } else {
00531         flags &= ~TaskHasFocus;
00532     }
00533 
00534     if (m_task->demandsAttention()) {
00535         flags |= TaskWantsAttention;
00536     } else {
00537         flags &= ~TaskWantsAttention;
00538     }
00539 
00540     if (m_task->isMinimized()) {
00541         flags |= TaskIsMinimized;
00542     } else {
00543         flags &= ~TaskIsMinimized;
00544     }
00545 
00546     setTaskFlags(flags);
00547 
00548     // basic title and icon
00549     QIcon taskIcon;
00550     taskIcon.addPixmap(m_task->icon(KIconLoader::SizeSmall, KIconLoader::SizeSmall, false));
00551     taskIcon.addPixmap(m_task->icon(KIconLoader::SizeSmallMedium, KIconLoader::SizeSmallMedium, false));
00552     taskIcon.addPixmap(m_task->icon(KIconLoader::SizeMedium, KIconLoader::SizeMedium, false));
00553     taskIcon.addPixmap(m_task->icon(KIconLoader::SizeLarge, KIconLoader::SizeLarge, false));
00554 
00555 #ifdef TOOLTIP_MANAGER
00556     if (m_showTooltip) {
00557       Plasma::ToolTipData data;
00558       data.mainText = m_task->visibleName();
00559       data.subText = i18nc("Which virtual desktop a window is currently on", "On %1", KWindowSystem::desktopName(m_task->desktop()));
00560       data.image = iconPixmap;
00561       data.windowToPreview = m_task->window();
00562       setToolTip(data);
00563     } else {
00564         Plasma::ToolTipData data;
00565         setToolTip(data); // Clear
00566     }
00567 #endif
00568     setIcon(taskIcon);
00569     setText(m_task->visibleName());
00570     //redraw
00571     queueUpdate();
00572 }
00573 
00574 void WindowTaskItem::animationUpdate(qreal progress)
00575 {
00576     if (progress == 1) {
00577         m_animId = 0;
00578         m_fadeIn = true;
00579     }
00580 
00581     m_alpha = m_fadeIn ? progress : 1 - progress;
00582 
00583     // explicit update
00584     update();
00585 }
00586 
00587 void WindowTaskItem::setStartupTask(TaskManager::StartupPtr task)
00588 {
00589     setText(task->text());
00590     setIcon(KIcon(task->icon()));
00591 #ifdef TOOLTIP_MANAGER
00592     if (m_showTooltip) {
00593         Plasma::ToolTipData tip;
00594         tip.mainText = task->text();
00595         tip.image = task->icon();
00596         setToolTip(tip);
00597     }
00598 #endif
00599 }
00600 
00601 void WindowTaskItem::setWindowTask(TaskManager::TaskPtr task)
00602 {
00603     if (m_task) {
00604         disconnect(m_task.constData(), 0, this, 0);
00605     }
00606 
00607     m_task = task;
00608 
00609     connect(task.constData(), SIGNAL(changed()),
00610             this, SLOT(updateTask()));
00611     connect(task.constData(), SIGNAL(iconChanged()),
00612             this, SLOT(updateTask()));
00613 
00614     updateTask();
00615     publishIconGeometry();
00616 
00617     //kDebug() << "Task added, isActive = " << task->isActive();
00618 }
00619 
00620 TaskManager::TaskPtr WindowTaskItem::windowTask() const
00621 {
00622     return m_task;
00623 }
00624 
00625 void WindowTaskItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *e)
00626 {
00627     if (!KAuthorized::authorizeKAction("kwin_rmb") || m_task.isNull()) {
00628         QGraphicsWidget::contextMenuEvent(e);
00629         return;
00630     }
00631 
00632     TaskManager::TaskRMBMenu menu(m_task);
00633     menu.exec(e->screenPos());
00634 }
00635 
00636 void WindowTaskItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
00637 {
00638     event->accept();
00639     if (!m_activateTimer) {
00640         m_activateTimer = new QTimer(this);
00641         m_activateTimer->setSingleShot(true);
00642         m_activateTimer->setInterval(300);
00643         connect(m_activateTimer, SIGNAL(timeout()), this, SLOT(activate()));
00644     }
00645     m_activateTimer->start();
00646 }
00647 
00648 void WindowTaskItem::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
00649 {
00650     Q_UNUSED(event);
00651 
00652     // restart the timer so that activate() is only called after the mouse
00653     // stops moving
00654     m_activateTimer->start();
00655 }
00656 
00657 void WindowTaskItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
00658 {
00659     Q_UNUSED(event);
00660 
00661     delete m_activateTimer;
00662     m_activateTimer = 0;
00663 }
00664 
00665 void WindowTaskItem::setGeometry(const QRectF& geometry)
00666 {
00667     QGraphicsWidget::setGeometry(geometry);
00668     publishIconGeometry();
00669 }
00670 
00671 void WindowTaskItem::publishIconGeometry()
00672 {
00673     if (!scene()) {
00674         return;
00675     }
00676 
00677     QGraphicsView *parentView = 0L;
00678     // The following was taken from Plasma::Applet, it doesn't make sense to make the item an applet, and this was the easiest way around it.
00679     foreach (QGraphicsView *view, scene()->views()) {
00680         if (view->sceneRect().intersects(sceneBoundingRect()) ||
00681             view->sceneRect().contains(scenePos())) {
00682             parentView = view;
00683         }
00684     }
00685     if (!parentView || !m_task) {
00686         return;
00687     }
00688     if( !boundingRect().isValid() )
00689         return;
00690 
00691     QRect rect = parentView->mapFromScene(mapToScene(boundingRect())).boundingRect().adjusted(0, 0, 1, 1);
00692     rect.moveTopLeft(parentView->mapToGlobal(rect.topLeft()));
00693     if (m_task) {
00694         m_task->publishIconGeometry(rect);
00695     }
00696 }
00697 
00698 QRectF WindowTaskItem::iconRect(const QRectF &b) const
00699 {
00700     QRectF bounds(b);
00701     const int right = bounds.right();
00702     //leave enough space for the text. useful in vertical panel
00703     bounds.setWidth(qMax(bounds.width() / 3, qMin(minimumSize().height(), bounds.width())));
00704 
00705     //restore right position if the layout is RTL
00706     if (QApplication::layoutDirection() == Qt::RightToLeft) {
00707         bounds.moveRight(right);
00708     }
00709 
00710     QSize iconSize = m_icon.actualSize(bounds.size().toSize());
00711 
00712     return QStyle::alignedRect(QApplication::layoutDirection(), Qt::AlignLeft | Qt::AlignVCenter,
00713                                iconSize, bounds.toRect());
00714 }
00715 
00716 QRectF WindowTaskItem::textRect(const QRectF &bounds) const
00717 {
00718     QSize size(bounds.size().toSize());
00719     size.rwidth() -= int(iconRect(bounds).width()) + qMax(0, IconTextSpacing - 2);
00720 
00721     return QStyle::alignedRect(QApplication::layoutDirection(), Qt::AlignRight | Qt::AlignVCenter,
00722                                      size, bounds.toRect());
00723 }
00724 
00725 #include "windowtaskitem.moc"

Applets

Skip menu "Applets"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal