1 /***************************************************************************
2 * Copyright (C) 2007 by Robert Knight <robertknight@gmail.com> *
3 * Copyright (C) 2008 by Alexis Ménard <darktears31@gmail.com> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
19 ***************************************************************************/
23 #include "windowtaskitem.h"
24 #include "taskgroupitem.h"
25 #include "ui_tasksConfig.h"
28 #include <taskmanager/taskgroup.h>
29 #include <taskmanager/taskitem.h>
32 #include <KConfigDialog>
33 #include <KWindowSystem>
37 #include <QGraphicsScene>
38 #include <QGraphicsLinearLayout>
42 #include <Plasma/Containment>
43 #include <Plasma/FrameSvg>
44 #include <Plasma/Theme>
46 Tasks::Tasks(QObject
* parent
, const QVariantList
&arguments
)
47 : Plasma::Applet(parent
, arguments
),
48 m_taskItemBackground(0),
56 m_groupModifierKey(Qt::AltModifier
)
58 setHasConfigurationInterface(true);
59 setAspectRatioMode(Plasma::IgnoreAspectRatio
);
60 m_screenTimer
.setSingleShot(true);
61 m_screenTimer
.setInterval(300);
71 delete m_groupManager
;
78 m_groupManager
= new TaskManager::GroupManager(this);
79 Plasma::Containment
* appletContainment
= containment();
80 if (appletContainment
) {
81 m_groupManager
->setScreen(appletContainment
->screen());
84 //FIXME: the order of creation and setting of items in this method is both fragile (from
85 // personal experience tinking with it) and convoluted. It should be possible to
86 // set up the GroupManager firt, and *then* create the root TaskGroupItem.
88 // connect(m_groupManager, SIGNAL(reload()), this, SLOT(reload()));
89 connect(this, SIGNAL(settingsChanged()), m_groupManager
, SLOT(reconnect()));
91 m_rootGroupItem
= new TaskGroupItem(this, this, false);
92 m_rootGroupItem
->expand();
93 m_rootGroupItem
->setGroup(m_groupManager
->rootGroup());
96 foreach (TaskManager::AbstractGroupableItem *item, m_groupManager->rootGroup()->members()) {
97 kDebug() << item->name();
101 connect(m_rootGroupItem
, SIGNAL(sizeHintChanged(Qt::SizeHint
)), this, SLOT(changeSizeHint(Qt::SizeHint
)));
103 setSizePolicy(QSizePolicy(QSizePolicy::Expanding
,QSizePolicy::Expanding
));
104 //like in Qt's designer
106 setMaximumSize(INT_MAX
,INT_MAX
);
108 layout
= new QGraphicsLinearLayout(this);
109 layout
->setContentsMargins(0,0,0,0);
110 layout
->setSizePolicy(QSizePolicy(QSizePolicy::Expanding
,QSizePolicy::Expanding
));
112 layout
->setMaximumSize(INT_MAX
,INT_MAX
);
113 layout
->setOrientation(Qt::Vertical
);
114 layout
->addItem(m_rootGroupItem
);
119 KConfigGroup cg
= config();
121 m_groupManager
->setShowOnlyCurrentDesktop( cg
.readEntry("showOnlyCurrentDesktop", false));
122 m_groupManager
->setShowOnlyCurrentScreen( cg
.readEntry("showOnlyCurrentScreen", false));
123 m_groupManager
->setShowOnlyMinimized( cg
.readEntry("showOnlyMinimized", false));
124 m_groupManager
->setOnlyGroupWhenFull(cg
.readEntry("groupWhenFull", true));
125 m_showTooltip
= cg
.readEntry("showTooltip", true);
127 m_groupManager
->setGroupingStrategy( static_cast<TaskManager::GroupManager::TaskGroupingStrategy
>(cg
.readEntry("groupingStrategy", static_cast<int>(TaskManager::GroupManager::ProgramGrouping
))));
129 m_groupManager
->setSortingStrategy( static_cast<TaskManager::GroupManager::TaskSortingStrategy
>(cg
.readEntry("sortingStrategy", static_cast<int>(TaskManager::GroupManager::AlphaSorting
))));
130 m_rootGroupItem
->setMaxRows( cg
.readEntry("maxRows", 2));
131 m_rootGroupItem
->setForceRows( cg
.readEntry("forceRows", false));
133 emit
settingsChanged();
138 m_rootGroupItem
->reload();
141 TaskManager::GroupManager
&Tasks::groupManager() const
143 return *m_groupManager
;
146 Qt::KeyboardModifiers
Tasks::groupModifierKey() const
148 return m_groupModifierKey
;
153 void Tasks::constraintsEvent(Plasma::Constraints constraints
)
156 if (m_groupManager
&& constraints
& Plasma::ScreenConstraint
) {
157 Plasma::Containment
* appletContainment
= containment();
158 if (appletContainment
) {
159 m_groupManager
->setScreen(appletContainment
->screen());
163 if (constraints
& Plasma::SizeConstraint
) {
164 adjustGroupingStrategy();
167 emit
constraintsChanged(constraints
);
170 Plasma::FrameSvg
* Tasks::itemBackground()
172 if (!m_taskItemBackground
) {
173 m_taskItemBackground
= new Plasma::FrameSvg(this);
174 m_taskItemBackground
->setImagePath("widgets/tasks");
175 m_taskItemBackground
->setCacheAllRenderedFrames(true);
178 return m_taskItemBackground
;
181 void Tasks::resizeItemBackground(const QSizeF
&size
)
184 if (!m_taskItemBackground
) {
187 if (!m_taskItemBackground
) {
188 //kDebug() << "Error1";
193 if (m_taskItemBackground
->frameSize() == size
) {
194 //kDebug() << "Error2";
198 m_taskItemBackground
->resizeFrame(size
);
200 QString oldPrefix
= m_taskItemBackground
->prefix();
201 m_taskItemBackground
->setElementPrefix("normal");
202 //get the margins now
203 m_taskItemBackground
->getMargins(m_leftMargin
, m_topMargin
, m_rightMargin
, m_bottomMargin
);
204 //if the task height is too little shrink the top and bottom margins
205 if (size
.height() - m_topMargin
- m_bottomMargin
< KIconLoader::SizeSmall
) {
206 m_topMargin
= m_bottomMargin
= qMax(1, int((size
.height() - KIconLoader::SizeSmall
)/2));
208 m_taskItemBackground
->setElementPrefix(oldPrefix
);
211 KColorScheme
*Tasks::colorScheme()
213 if (!m_colorScheme
) {
214 m_colorScheme
= new KColorScheme(QPalette::Active
, KColorScheme::View
, Plasma::Theme::defaultTheme()->colorScheme());
217 return m_colorScheme
;
221 QSizeF
Tasks::sizeHint(Qt::SizeHint which
, const QSizeF
&constraint
) const
223 if (m_rootGroupItem
&& which
== Qt::PreferredSize
) {
224 return m_rootGroupItem
->preferredSize();
226 return Plasma::Applet::sizeHint(which
, constraint
);
230 void Tasks::adjustGroupingStrategy()
232 //FIXME: should use AbstractTaskItem::basicPreferredSize() but it seems to cause crashes
233 //QSize itemSize = QSize(300, 30);
234 //m_groupManager->setFullLimit(((size().width()*size().height()) / (itemSize.width()*itemSize.height())));
235 //kDebug() << ((size().width()*size().height()) / (itemSize.width()*itemSize.height()));
237 m_groupManager
->setFullLimit(rootGroupItem()->optimumCapacity());
240 void Tasks::changeSizeHint(Qt::SizeHint which
)
242 emit
sizeHintChanged(which
);
243 adjustGroupingStrategy();
246 void Tasks::createConfigurationInterface(KConfigDialog
*parent
)
248 QWidget
*widget
= new QWidget
;
249 m_ui
.setupUi(widget
);
250 connect(parent
, SIGNAL(applyClicked()), this, SLOT(configAccepted()));
251 connect(parent
, SIGNAL(okClicked()), this, SLOT(configAccepted()));
252 parent
->addPage(widget
, i18n("General"), icon());
254 m_ui
.showTooltip
->setChecked(m_showTooltip
);
255 m_ui
.showOnlyCurrentDesktop
->setChecked(m_groupManager
->showOnlyCurrentDesktop());
256 m_ui
.showOnlyCurrentScreen
->setChecked(m_groupManager
->showOnlyCurrentScreen());
257 m_ui
.showOnlyMinimized
->setChecked(m_groupManager
->showOnlyMinimized());
258 m_ui
.fillRows
->setChecked(m_rootGroupItem
->forceRows());
260 m_ui
.groupingStrategy
->addItem(i18n("Do Not Group"),QVariant(TaskManager::GroupManager::NoGrouping
));
261 m_ui
.groupingStrategy
->addItem(i18n("Manually"),QVariant(TaskManager::GroupManager::ManualGrouping
));
262 m_ui
.groupingStrategy
->addItem(i18n("By Program Name"),QVariant(TaskManager::GroupManager::ProgramGrouping
));
264 connect(m_ui
.groupingStrategy
, SIGNAL(currentIndexChanged(int)), this, SLOT(dialogGroupingChanged(int)));
266 switch (m_groupManager
->groupingStrategy()) {
267 case TaskManager::GroupManager::NoGrouping
:
268 m_ui
.groupingStrategy
->setCurrentIndex(0);
270 case TaskManager::GroupManager::ManualGrouping
:
271 m_ui
.groupingStrategy
->setCurrentIndex(1);
273 case TaskManager::GroupManager::ProgramGrouping
:
274 m_ui
.groupingStrategy
->setCurrentIndex(2);
277 m_ui
.groupingStrategy
->setCurrentIndex(-1);
279 kDebug() << m_groupManager
->groupingStrategy();
281 m_ui
.groupWhenFull
->setChecked(m_groupManager
->onlyGroupWhenFull());
284 m_ui
.sortingStrategy
->addItem(i18n("Do Not Sort"),QVariant(TaskManager::GroupManager::NoSorting
));
285 m_ui
.sortingStrategy
->addItem(i18n("Manually"),QVariant(TaskManager::GroupManager::ManualSorting
));
286 m_ui
.sortingStrategy
->addItem(i18n("Alphabetically"),QVariant(TaskManager::GroupManager::AlphaSorting
));
287 m_ui
.sortingStrategy
->addItem(i18n("By Desktop"),QVariant(TaskManager::GroupManager::DesktopSorting
));
290 switch (m_groupManager
->sortingStrategy()) {
291 case TaskManager::GroupManager::NoSorting
:
292 m_ui
.sortingStrategy
->setCurrentIndex(0);
294 case TaskManager::GroupManager::ManualSorting
:
295 m_ui
.sortingStrategy
->setCurrentIndex(1);
297 case TaskManager::GroupManager::AlphaSorting
:
298 m_ui
.sortingStrategy
->setCurrentIndex(2);
300 case TaskManager::GroupManager::DesktopSorting
:
301 m_ui
.sortingStrategy
->setCurrentIndex(3);
304 m_ui
.sortingStrategy
->setCurrentIndex(-1);
306 // kDebug() << m_groupManager->sortingStrategy();
307 m_ui
.maxRows
->setValue(m_rootGroupItem
->maxRows());
310 void Tasks::dialogGroupingChanged(int index
)
312 m_ui
.groupWhenFull
->setEnabled(static_cast<TaskManager::GroupManager::TaskGroupingStrategy
>(m_ui
.groupingStrategy
->itemData(index
).toInt()) == TaskManager::GroupManager::ProgramGrouping
);
315 void Tasks::configAccepted()
318 bool changed
= false;
320 if (m_groupManager
->showOnlyCurrentDesktop() != (m_ui
.showOnlyCurrentDesktop
->isChecked())) {
321 m_groupManager
->setShowOnlyCurrentDesktop(!m_groupManager
->showOnlyCurrentDesktop());
322 KConfigGroup cg
= config();
323 cg
.writeEntry("showOnlyCurrentDesktop", m_groupManager
->showOnlyCurrentDesktop());
326 if (m_groupManager
->showOnlyCurrentScreen() != (m_ui
.showOnlyCurrentScreen
->isChecked())) {
327 m_groupManager
->setShowOnlyCurrentScreen(!m_groupManager
->showOnlyCurrentScreen());
328 KConfigGroup cg
= config();
329 cg
.writeEntry("showOnlyCurrentScreen", m_groupManager
->showOnlyCurrentScreen());
332 if (m_groupManager
->showOnlyMinimized() != (m_ui
.showOnlyMinimized
->isChecked())) {
333 m_groupManager
->setShowOnlyMinimized(!m_groupManager
->showOnlyMinimized());
334 KConfigGroup cg
= config();
335 cg
.writeEntry("showOnlyMinimized", m_groupManager
->showOnlyMinimized());
339 if (m_groupManager
->groupingStrategy() != (m_ui
.groupingStrategy
->currentIndex())) {
340 m_groupManager
->setGroupingStrategy(static_cast<TaskManager::GroupManager::TaskGroupingStrategy
>(m_ui
.groupingStrategy
->itemData(m_ui
.groupingStrategy
->currentIndex()).toInt()));
341 KConfigGroup cg
= config();
342 cg
.writeEntry("groupingStrategy", static_cast<int>(m_groupManager
->groupingStrategy()));
346 if (m_groupManager
->onlyGroupWhenFull() != m_ui
.groupWhenFull
->isChecked()) {
347 adjustGroupingStrategy();
348 m_groupManager
->setOnlyGroupWhenFull(m_ui
.groupWhenFull
->isChecked());
349 KConfigGroup cg
= config();
350 cg
.writeEntry("groupWhenFull", m_groupManager
->onlyGroupWhenFull());
354 if (m_groupManager
->sortingStrategy() != (m_ui
.sortingStrategy
->currentIndex())) {
355 m_groupManager
->setSortingStrategy(static_cast<TaskManager::GroupManager::TaskSortingStrategy
>(m_ui
.sortingStrategy
->itemData(m_ui
.sortingStrategy
->currentIndex()).toInt()));
356 KConfigGroup cg
= config();
357 cg
.writeEntry("sortingStrategy", static_cast<int>(m_groupManager
->sortingStrategy()));
361 if (m_rootGroupItem
->maxRows() != (m_ui
.maxRows
->value())) {
362 m_rootGroupItem
->setMaxRows(m_ui
.maxRows
->value());
363 KConfigGroup cg
= config();
364 cg
.writeEntry("maxRows", m_rootGroupItem
->maxRows());
368 if (m_rootGroupItem
->forceRows() != m_ui
.fillRows
->isChecked()) {
369 m_rootGroupItem
->setForceRows(m_ui
.fillRows
->isChecked());
370 KConfigGroup cg
= config();
371 cg
.writeEntry("forceRows", m_rootGroupItem
->forceRows());
375 if (m_showTooltip
!= (m_ui
.showTooltip
->checkState() == Qt::Checked
)) {
376 m_showTooltip
= !m_showTooltip
;
377 KConfigGroup cg
= config();
378 cg
.writeEntry("showTooltip", m_showTooltip
);
383 emit
settingsChanged();
384 emit
configNeedsSaving();
389 bool Tasks::showTooltip() const
391 return m_showTooltip
;
396 void Tasks::themeRefresh()
398 delete m_taskItemBackground
;
399 m_taskItemBackground
= 0;
401 delete m_colorScheme
;
407 TaskGroupItem
* Tasks::rootGroupItem()
409 return m_rootGroupItem
;
413 K_EXPORT_PLASMA_APPLET(tasks
, Tasks
)