not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / applets / tasks / tasks.cpp
blobe7068af0187bc3774938f733b16056152b77949e
1 /***************************************************************************
2 * Copyright (C) 2007 by Robert Knight <robertknight@gmail.com> *
3 * Copyright (C) 2008 by Alexis Ménard <darktears31@gmail.com> *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
21 // Own
22 #include "tasks.h"
23 #include "windowtaskitem.h"
24 #include "taskgroupitem.h"
25 #include "ui_tasksConfig.h"
27 //Taskmanager
28 #include <taskmanager/taskgroup.h>
29 #include <taskmanager/taskitem.h>
31 // KDE
32 #include <KConfigDialog>
33 #include <KWindowSystem>
35 // Qt
36 #include <QTimeLine>
37 #include <QGraphicsScene>
38 #include <QGraphicsLinearLayout>
39 #include <QVariant>
41 // Plasma
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),
49 m_colorScheme(0),
50 m_leftMargin(0),
51 m_topMargin(0),
52 m_rightMargin(0),
53 m_bottomMargin(0),
54 m_rootGroupItem(0),
55 m_groupManager(0),
56 m_groupModifierKey(Qt::AltModifier)
58 setHasConfigurationInterface(true);
59 setAspectRatioMode(Plasma::IgnoreAspectRatio);
60 m_screenTimer.setSingleShot(true);
61 m_screenTimer.setInterval(300);
62 resize(500, 58);
64 setAcceptDrops(true);
68 Tasks::~Tasks()
70 delete m_colorScheme;
71 delete m_groupManager;
74 void Tasks::init()
76 //kDebug();
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
105 //TODO : Qt's bug??
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));
111 //TODO : Qt's bug??
112 layout->setMaximumSize(INT_MAX,INT_MAX);
113 layout->setOrientation(Qt::Vertical);
114 layout->addItem(m_rootGroupItem);
117 setLayout(layout);
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();
136 void Tasks::reload()
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)
155 //kDebug();
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)
183 //kDebug();
184 if (!m_taskItemBackground) {
185 itemBackground();
187 if (!m_taskItemBackground) {
188 //kDebug() << "Error1";
189 return;
193 if (m_taskItemBackground->frameSize() == size) {
194 //kDebug() << "Error2";
195 return;
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();
225 } else {
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);
269 break;
270 case TaskManager::GroupManager::ManualGrouping:
271 m_ui.groupingStrategy->setCurrentIndex(1);
272 break;
273 case TaskManager::GroupManager::ProgramGrouping:
274 m_ui.groupingStrategy->setCurrentIndex(2);
275 break;
276 default:
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);
293 break;
294 case TaskManager::GroupManager::ManualSorting:
295 m_ui.sortingStrategy->setCurrentIndex(1);
296 break;
297 case TaskManager::GroupManager::AlphaSorting:
298 m_ui.sortingStrategy->setCurrentIndex(2);
299 break;
300 case TaskManager::GroupManager::DesktopSorting:
301 m_ui.sortingStrategy->setCurrentIndex(3);
302 break;
303 default:
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()
317 kDebug();
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());
324 changed = true;
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());
330 changed = true;
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());
336 changed = true;
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()));
343 changed = true;
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());
351 changed = true;
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()));
358 changed = true;
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());
365 changed = true;
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());
372 changed = true;
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);
379 changed = true;
382 if (changed) {
383 emit settingsChanged();
384 emit configNeedsSaving();
385 update();
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;
402 m_colorScheme = 0;
407 TaskGroupItem* Tasks::rootGroupItem()
409 return m_rootGroupItem;
413 K_EXPORT_PLASMA_APPLET(tasks, Tasks)
415 #include "tasks.moc"