not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / applets / tasks / abstracttaskitem.h
blob4bde58f1155adde8699cd0c44e74138ea26abfa1
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> *
5 * *
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. *
10 * *
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. *
15 * *
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
26 // KDE
27 #include <KColorScheme>
28 // Own
29 #include <taskmanager/taskgroup.h>
31 // Qt
32 #include <QTime>
33 #include <QIcon>
34 #include <QGraphicsWidget>
36 class QTextOption;
37 class QTextLayout;
38 class QString;
41 // Plasma
42 #include <Plasma/Animator>
44 class Tasks;
45 class TaskGroupItem;
46 class LayoutWidget;
48 /**
49 * A baseclass for a task
51 class AbstractTaskItem : public QGraphicsWidget
53 Q_OBJECT
55 public:
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. */
60 ~AbstractTaskItem();
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);
71 /**
72 * This enum describes the generic flags which are currently
73 * set by the task.
75 enum TaskFlag
77 /**
78 * This flag is set by the task to indicate that it wants
79 * the user's attention.
81 TaskWantsAttention = 1,
82 /**
83 * Indicates that the task's window has the focus
85 TaskHasFocus = 2,
86 /**
87 * Indicates that the task is iconified
89 TaskIsMinimized = 4
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. */
103 QIcon icon() const;
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;
132 Q_SIGNALS:
133 void activated(AbstractTaskItem *);
135 public Q_SLOTS:
136 virtual void activate() = 0;
137 void toolTipAboutToShow();
138 void toolTipHidden();
140 protected:
141 void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
142 void dragMoveEvent(QGraphicsSceneDragDropEvent *event);
143 void dragLeaveEvent(QGraphicsSceneDragDropEvent *event);
145 // reimplemented
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,
156 QWidget *widget);
157 /** Draws the icon and text which represent the task item. */
158 virtual void drawTask(QPainter *painter,const QStyleOptionGraphicsItem *option,
159 QWidget *widget);
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;
184 protected Q_SLOTS:
185 /** Event compression **/
186 void queueUpdate();
188 void animationUpdate(qreal progress);
189 void syncActiveRect();
190 void checkSettings();
192 protected:
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;
207 Tasks *m_applet;
208 LayoutWidget *m_parentWidget;
209 QTimer* m_activateTimer;
212 TaskFlags m_flags;
214 QIcon m_icon;
215 QString m_text;
217 int m_animId;
218 qreal m_alpha;
219 QString m_oldBackgroundPrefix;
220 QString m_backgroundPrefix;
221 QRectF m_activeRect;
223 QPointF _dragOffset;
224 QTime m_lastUpdate;
225 int m_updateTimerId;
226 int m_attentionTimerId;
227 int m_attentionTicks;
229 bool m_fadeIn : 1;
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;
239 #endif