1 /***************************************************************************
2 * Copyright (C) 2007 by Robert Knight <robertknight@gmail.com> *
3 * Copyright (C) 2008 by Alexis Ménard <darktears31@gmail.com> *
4 * Copyright (C) 2008 by Marco Martin <notmart@gmail.com> *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
20 ***************************************************************************/
23 #ifndef ABSTRACTTASKITEM_H
24 #define ABSTRACTTASKITEM_H
27 #include <KColorScheme>
29 #include <taskmanager/taskgroup.h>
34 #include <QGraphicsWidget>
42 #include <Plasma/Animator>
49 * A baseclass for a task
51 class AbstractTaskItem
: public QGraphicsWidget
56 /** Constructs a new representation for an abstract task. */
57 AbstractTaskItem(QGraphicsWidget
*parent
, Tasks
*applet
, const bool showTooltip
);
59 /** Destruct the representation for an abstract task. */
62 /** Switch on/off tooltips above tasks */
63 void setShowTooltip(const bool showit
);
65 /** Sets the text for this task item. */
66 void setText(const QString
&text
);
68 /** Sets the icon for this task item. */
69 void setIcon(const QIcon
&icon
);
72 * This enum describes the generic flags which are currently
78 * This flag is set by the task to indicate that it wants
79 * the user's attention.
81 TaskWantsAttention
= 1,
83 * Indicates that the task's window has the focus
87 * Indicates that the task is iconified
91 Q_DECLARE_FLAGS(TaskFlags
, TaskFlag
)
93 /** Sets the task flags for this item. */
94 void setTaskFlags(const TaskFlags flags
);
96 /** Returns the task's current flags. */
97 TaskFlags
taskFlags() const;
99 /** Returns current text for this task. */
100 QString
text() const;
102 /** Returns the current icon for this task. */
105 virtual void close() = 0;
107 /** Tells the window manager the minimized task's geometry. */
108 virtual void publishIconGeometry() const;
109 virtual void publishIconGeometry(const QRect
&rect
) const;
110 QRect
iconGeometry() const; // helper for above
112 /** Overridden from LayoutItem */
113 void setGeometry(const QRectF
& geometry
);
115 /** Convenience Functions to get information about Grouping */
116 /** Only true if the task is not only member of rootGroup */
117 bool isGrouped() const;
118 bool isGroupMember(const TaskGroupItem
*group
) const;
119 TaskGroupItem
*parentGroup() const;
121 virtual bool isWindowItem() const = 0;
122 virtual bool isActive() const = 0;
124 virtual void setAdditionalMimeData(QMimeData
* mimeData
) = 0;
126 void setLayoutWidget(LayoutWidget
* widget
);
127 TaskManager::AbstractItemPtr
abstractItem();
129 /** Returns the preferred size calculated on base of the fontsize and the iconsize*/
130 QSize
basicPreferredSize() const;
133 void activated(AbstractTaskItem
*);
136 virtual void activate() = 0;
137 void toolTipAboutToShow();
138 void toolTipHidden();
141 void dragEnterEvent(QGraphicsSceneDragDropEvent
*event
);
142 void dragMoveEvent(QGraphicsSceneDragDropEvent
*event
);
143 void dragLeaveEvent(QGraphicsSceneDragDropEvent
*event
);
146 void hoverEnterEvent(QGraphicsSceneHoverEvent
*event
);
147 void hoverLeaveEvent(QGraphicsSceneHoverEvent
*event
);
148 void mousePressEvent(QGraphicsSceneMouseEvent
*event
);
149 void mouseMoveEvent(QGraphicsSceneMouseEvent
*event
);
150 void mouseReleaseEvent(QGraphicsSceneMouseEvent
*event
);
151 void timerEvent(QTimerEvent
*event
);
152 void paint(QPainter
*painter
,const QStyleOptionGraphicsItem
*option
,QWidget
*widget
);
154 /** Draws the background for the task item. */
155 virtual void drawBackground(QPainter
*painter
,const QStyleOptionGraphicsItem
*option
,
157 /** Draws the icon and text which represent the task item. */
158 virtual void drawTask(QPainter
*painter
,const QStyleOptionGraphicsItem
*option
,
161 /** Returns a QTextOption object for the icon label QTtextLayout.*/
162 QTextOption
textOption() const;
165 * Lays the text out in the text layout using the constraints, and returns the actual
166 * size required. The returned size may be wider than the constraints if the text
167 * contains a non-breakable word that is wider than the maximum width.
168 * If more height is needed than what's available, the last line that will fit will be
169 * extended to hold the remainder of the text.
171 QSize
layoutText(QTextLayout
&layout
, const QString
&text
, const QSize
&constraints
) const;
174 * Draws the text layout (which must already have the text laid out) in the rect using
175 * the supplied painter. If the layout contains text lines that are longer than the rect
176 * is wide, they will be elided by fading the text out.
178 void drawTextLayout(QPainter
*painter
, const QTextLayout
&layout
, const QRect
&rect
) const;
180 virtual void updateTask(::TaskManager::TaskChanges changes
) = 0; // pure virtual function
181 virtual void updateToolTip() = 0; // pure virtual function
182 QString
expanderElement() const;
185 /** Event compression **/
188 void animationUpdate(qreal progress
);
189 void syncActiveRect();
190 void checkSettings();
193 // area of item occupied by task's icon
194 QRectF
iconRect(const QRectF
&bounds
) const;
195 // area for the expander arrow for group items
196 QRectF
expanderRect(const QRectF
&b
) const;
197 // area of item occupied by task's text
198 QRectF
textRect(const QRectF
&bounds
) const;
199 // start an animation to chnge the task background
200 void fadeBackground(const QString
&newBackground
, int duration
, bool fadeIn
);
201 // text color, use this because it could be animated
202 QColor
textColor() const;
204 TaskManager::AbstractItemPtr m_abstractItem
;
205 LayoutWidget
*m_layoutWidget
;
208 LayoutWidget
*m_parentWidget
;
209 QTimer
* m_activateTimer
;
219 QString m_oldBackgroundPrefix
;
220 QString m_backgroundPrefix
;
226 int m_attentionTimerId
;
227 int m_attentionTicks
;
230 bool m_showTooltip
: 1;
231 bool m_showingTooltip
: 1;
232 // distance (in pixels) between a task's icon and its text
233 static const int IconTextSpacing
= 4;
235 static const int TaskItemHorizontalMargin
= 4;
236 static const int TaskItemVerticalMargin
= 4;