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

Applets

tasks.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 "tasks.h"
00023 #include "windowtaskitem.h"
00024 #include "taskgroupitem.h"
00025 #include "ui_tasksConfig.h"
00026 
00027 //Taskmanager
00028 #include <taskmanager/taskgroup.h>
00029 #include <taskmanager/taskitem.h>
00030 
00031 // KDE
00032 #include <KConfigDialog>
00033 #include <KWindowSystem>
00034 
00035 // Qt
00036 #include <QTimeLine>
00037 #include <QGraphicsScene>
00038 #include <QGraphicsLinearLayout>
00039 #include <QVariant>
00040 
00041 // Plasma
00042 #include <Plasma/Containment>
00043 #include <Plasma/FrameSvg>
00044 #include <Plasma/Theme>
00045 
00046 Tasks::Tasks(QObject* parent, const QVariantList &arguments)
00047      : Plasma::Applet(parent, arguments),
00048        m_taskItemBackground(0),
00049        m_colorScheme(0),
00050        m_leftMargin(0),
00051        m_topMargin(0),
00052        m_rightMargin(0),
00053        m_bottomMargin(0),
00054        m_rootGroupItem(0),
00055        m_groupManager(0),
00056        m_groupModifierKey(Qt::AltModifier)
00057 {
00058     setHasConfigurationInterface(true);
00059     setAspectRatioMode(Plasma::IgnoreAspectRatio);
00060     m_screenTimer.setSingleShot(true);
00061     m_screenTimer.setInterval(300);
00062     resize(500, 58);
00063 
00064     setAcceptDrops(true);
00065 
00066 }
00067 
00068 Tasks::~Tasks()
00069 {
00070     delete m_colorScheme;
00071     delete m_groupManager;
00072 }
00073 
00074 void Tasks::init()
00075 {
00076     //kDebug();
00077 
00078     m_groupManager = new TaskManager::GroupManager(this);
00079     Plasma::Containment* appletContainment = containment();
00080     if (appletContainment) {
00081         m_groupManager->setScreen(appletContainment->screen());
00082     }
00083 
00084     //FIXME: the order of creation and setting of items in this method is both fragile (from
00085     // personal experience tinking with it) and convoluted. It should be possible to
00086     // set up the GroupManager firt, and *then* create the root TaskGroupItem.
00087 
00088    // connect(m_groupManager, SIGNAL(reload()), this, SLOT(reload()));
00089     connect(this, SIGNAL(settingsChanged()), m_groupManager, SLOT(reconnect()));
00090 
00091     m_rootGroupItem = new TaskGroupItem(this, this, false);
00092     m_rootGroupItem->expand();
00093     m_rootGroupItem->setGroup(m_groupManager->rootGroup());
00094 
00095     /*
00096     foreach (TaskManager::AbstractGroupableItem *item, m_groupManager->rootGroup()->members()) {
00097         kDebug() << item->name();
00098     }
00099     */
00100 
00101     connect(m_rootGroupItem, SIGNAL(sizeHintChanged(Qt::SizeHint)), this, SLOT(changeSizeHint(Qt::SizeHint)));
00102 
00103     setSizePolicy(QSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding));
00104     //like in Qt's designer
00105     //TODO : Qt's bug??
00106     setMaximumSize(INT_MAX,INT_MAX);
00107 
00108     layout = new QGraphicsLinearLayout(this);
00109     layout->setContentsMargins(0,0,0,0);
00110     layout->setSizePolicy(QSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding));
00111     //TODO : Qt's bug??
00112     layout->setMaximumSize(INT_MAX,INT_MAX);
00113     layout->setOrientation(Qt::Vertical);
00114     layout->addItem(m_rootGroupItem);
00115 
00116 
00117     setLayout(layout);
00118 
00119     KConfigGroup cg = config();
00120 
00121     m_groupManager->setShowOnlyCurrentDesktop( cg.readEntry("showOnlyCurrentDesktop", false));
00122     m_groupManager->setShowOnlyCurrentScreen( cg.readEntry("showOnlyCurrentScreen", false));
00123     m_groupManager->setShowOnlyMinimized( cg.readEntry("showOnlyMinimized", false));
00124     m_groupManager->setOnlyGroupWhenFull(cg.readEntry("groupWhenFull", true));
00125     m_showTooltip = cg.readEntry("showTooltip", true);
00126 
00127     m_groupManager->setGroupingStrategy( static_cast<TaskManager::GroupManager::TaskGroupingStrategy>(cg.readEntry("groupingStrategy", static_cast<int>(TaskManager::GroupManager::ProgramGrouping))));
00128 
00129     m_groupManager->setSortingStrategy( static_cast<TaskManager::GroupManager::TaskSortingStrategy>(cg.readEntry("sortingStrategy", static_cast<int>(TaskManager::GroupManager::AlphaSorting))));
00130     m_rootGroupItem->setMaxRows( cg.readEntry("maxRows", 2));
00131     m_rootGroupItem->setForceRows( cg.readEntry("forceRows", false));
00132 
00133     emit settingsChanged();
00134 }
00135 
00136 void Tasks::reload()
00137 {
00138     m_rootGroupItem->reload();
00139 }
00140 
00141 TaskManager::GroupManager &Tasks::groupManager() const
00142 {
00143     return *m_groupManager;
00144 }
00145 
00146 Qt::KeyboardModifiers Tasks::groupModifierKey() const
00147 {
00148     return m_groupModifierKey;
00149 }
00150 
00151 
00152 
00153 void Tasks::constraintsEvent(Plasma::Constraints constraints)
00154 {
00155     //kDebug();
00156     if (m_groupManager && constraints & Plasma::ScreenConstraint) {
00157         Plasma::Containment* appletContainment = containment();
00158         if (appletContainment) {
00159             m_groupManager->setScreen(appletContainment->screen());
00160         }
00161     }
00162 
00163     if (constraints & Plasma::SizeConstraint) {
00164         adjustGroupingStrategy();
00165     }
00166 
00167     emit constraintsChanged(constraints);
00168 }
00169 
00170 Plasma::FrameSvg* Tasks::itemBackground()
00171 {
00172     if (!m_taskItemBackground) {
00173         m_taskItemBackground = new Plasma::FrameSvg(this);
00174         m_taskItemBackground->setImagePath("widgets/tasks");
00175         m_taskItemBackground->setCacheAllRenderedFrames(true);
00176     }
00177 
00178     return m_taskItemBackground;
00179 }
00180 
00181 void Tasks::resizeItemBackground(const QSizeF &size)
00182 {
00183   //kDebug();
00184     if (!m_taskItemBackground) {
00185         itemBackground();
00186 
00187         if (!m_taskItemBackground) {
00188             //kDebug() << "Error1";
00189             return;
00190         }
00191     }
00192 
00193     if (m_taskItemBackground->frameSize() == size) {
00194         //kDebug() << "Error2";
00195         return;
00196     }
00197 
00198     m_taskItemBackground->resizeFrame(size);
00199 
00200     QString oldPrefix = m_taskItemBackground->prefix();
00201     m_taskItemBackground->setElementPrefix("normal");
00202     //get the margins now
00203     m_taskItemBackground->getMargins(m_leftMargin, m_topMargin, m_rightMargin, m_bottomMargin);
00204     //if the task height is too little shrink the top and bottom margins
00205     if (size.height() - m_topMargin - m_bottomMargin < KIconLoader::SizeSmall) {
00206         m_topMargin = m_bottomMargin = qMax(1, int((size.height() - KIconLoader::SizeSmall)/2));
00207     }
00208     m_taskItemBackground->setElementPrefix(oldPrefix);
00209 }
00210 
00211 KColorScheme *Tasks::colorScheme()
00212 {
00213     if (!m_colorScheme) {
00214         m_colorScheme = new KColorScheme(QPalette::Active, KColorScheme::View, Plasma::Theme::defaultTheme()->colorScheme());
00215     }
00216 
00217     return m_colorScheme;
00218 }
00219 
00220 
00221 QSizeF Tasks::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
00222 {
00223     if (m_rootGroupItem && which == Qt::PreferredSize) {
00224         return m_rootGroupItem->preferredSize();
00225     } else {
00226         return Plasma::Applet::sizeHint(which, constraint);
00227     }
00228 }
00229 
00230 void Tasks::adjustGroupingStrategy()
00231 {
00232     //FIXME: should use AbstractTaskItem::basicPreferredSize() but it seems to cause crashes
00233     //QSize itemSize = QSize(300, 30);
00234     //m_groupManager->setFullLimit(((size().width()*size().height()) / (itemSize.width()*itemSize.height())));  
00235     //kDebug() << ((size().width()*size().height()) / (itemSize.width()*itemSize.height()));
00236 
00237     m_groupManager->setFullLimit(rootGroupItem()->optimumCapacity());
00238 }
00239 
00240 void Tasks::changeSizeHint(Qt::SizeHint which)
00241 {
00242     emit sizeHintChanged(which);
00243     adjustGroupingStrategy();
00244 }
00245 
00246 void Tasks::createConfigurationInterface(KConfigDialog *parent)
00247 {
00248      QWidget *widget = new QWidget;
00249      m_ui.setupUi(widget);
00250      connect(parent, SIGNAL(applyClicked()), this, SLOT(configAccepted()));
00251      connect(parent, SIGNAL(okClicked()), this, SLOT(configAccepted()));
00252      parent->addPage(widget, i18n("General"), icon());
00253 
00254     m_ui.showTooltip->setChecked(m_showTooltip);
00255     m_ui.showOnlyCurrentDesktop->setChecked(m_groupManager->showOnlyCurrentDesktop());
00256     m_ui.showOnlyCurrentScreen->setChecked(m_groupManager->showOnlyCurrentScreen());
00257     m_ui.showOnlyMinimized->setChecked(m_groupManager->showOnlyMinimized());
00258     m_ui.fillRows->setChecked(m_rootGroupItem->forceRows());
00259 
00260     m_ui.groupingStrategy->addItem(i18n("Do Not Group"),QVariant(TaskManager::GroupManager::NoGrouping));
00261     m_ui.groupingStrategy->addItem(i18n("Manually"),QVariant(TaskManager::GroupManager::ManualGrouping));
00262     m_ui.groupingStrategy->addItem(i18n("By Program Name"),QVariant(TaskManager::GroupManager::ProgramGrouping));
00263 
00264     connect(m_ui.groupingStrategy, SIGNAL(currentIndexChanged(int)), this, SLOT(dialogGroupingChanged(int)));
00265 
00266     switch (m_groupManager->groupingStrategy()) {
00267         case TaskManager::GroupManager::NoGrouping:
00268             m_ui.groupingStrategy->setCurrentIndex(0);
00269             break;
00270         case TaskManager::GroupManager::ManualGrouping:
00271             m_ui.groupingStrategy->setCurrentIndex(1);
00272             break;
00273         case TaskManager::GroupManager::ProgramGrouping:
00274             m_ui.groupingStrategy->setCurrentIndex(2);
00275             break;
00276         default:
00277              m_ui.groupingStrategy->setCurrentIndex(-1);
00278     }
00279     kDebug() << m_groupManager->groupingStrategy();
00280 
00281     m_ui.groupWhenFull->setChecked(m_groupManager->onlyGroupWhenFull());
00282 
00283 
00284     m_ui.sortingStrategy->addItem(i18n("Do Not Sort"),QVariant(TaskManager::GroupManager::NoSorting));
00285     m_ui.sortingStrategy->addItem(i18n("Manually"),QVariant(TaskManager::GroupManager::ManualSorting));
00286     m_ui.sortingStrategy->addItem(i18n("Alphabetically"),QVariant(TaskManager::GroupManager::AlphaSorting));
00287     m_ui.sortingStrategy->addItem(i18n("By Desktop"),QVariant(TaskManager::GroupManager::DesktopSorting));
00288 
00289 
00290     switch (m_groupManager->sortingStrategy()) {
00291         case TaskManager::GroupManager::NoSorting:
00292             m_ui.sortingStrategy->setCurrentIndex(0);
00293             break;
00294         case TaskManager::GroupManager::ManualSorting:
00295             m_ui.sortingStrategy->setCurrentIndex(1);
00296             break;
00297         case TaskManager::GroupManager::AlphaSorting:
00298             m_ui.sortingStrategy->setCurrentIndex(2);
00299             break;
00300         case TaskManager::GroupManager::DesktopSorting:
00301             m_ui.sortingStrategy->setCurrentIndex(3);
00302             break;
00303         default:
00304              m_ui.sortingStrategy->setCurrentIndex(-1);
00305     }
00306  //   kDebug() << m_groupManager->sortingStrategy();
00307     m_ui.maxRows->setValue(m_rootGroupItem->maxRows());
00308 }
00309 
00310 void Tasks::dialogGroupingChanged(int index)
00311 {
00312      m_ui.groupWhenFull->setEnabled(static_cast<TaskManager::GroupManager::TaskGroupingStrategy>(m_ui.groupingStrategy->itemData(index).toInt()) == TaskManager::GroupManager::ProgramGrouping);
00313 }
00314 
00315 void Tasks::configAccepted()
00316 {
00317     kDebug();
00318     bool changed = false;
00319 
00320     if (m_groupManager->showOnlyCurrentDesktop() != (m_ui.showOnlyCurrentDesktop->isChecked())) {
00321         m_groupManager->setShowOnlyCurrentDesktop(!m_groupManager->showOnlyCurrentDesktop());
00322         KConfigGroup cg = config();
00323         cg.writeEntry("showOnlyCurrentDesktop", m_groupManager->showOnlyCurrentDesktop());
00324         changed = true;
00325     }
00326     if (m_groupManager->showOnlyCurrentScreen() != (m_ui.showOnlyCurrentScreen->isChecked())) {
00327         m_groupManager->setShowOnlyCurrentScreen(!m_groupManager->showOnlyCurrentScreen());
00328         KConfigGroup cg = config();
00329         cg.writeEntry("showOnlyCurrentScreen", m_groupManager->showOnlyCurrentScreen());
00330         changed = true;
00331     }
00332     if (m_groupManager->showOnlyMinimized() != (m_ui.showOnlyMinimized->isChecked())) {
00333         m_groupManager->setShowOnlyMinimized(!m_groupManager->showOnlyMinimized());
00334         KConfigGroup cg = config();
00335         cg.writeEntry("showOnlyMinimized", m_groupManager->showOnlyMinimized());
00336         changed = true;
00337     }
00338 
00339     if (m_groupManager->groupingStrategy() != (m_ui.groupingStrategy->currentIndex())) {
00340         m_groupManager->setGroupingStrategy(static_cast<TaskManager::GroupManager::TaskGroupingStrategy>(m_ui.groupingStrategy->itemData(m_ui.groupingStrategy->currentIndex()).toInt()));
00341         KConfigGroup cg = config();
00342         cg.writeEntry("groupingStrategy", static_cast<int>(m_groupManager->groupingStrategy()));
00343         changed = true;
00344     }
00345 
00346     if (m_groupManager->onlyGroupWhenFull() != m_ui.groupWhenFull->isChecked()) {
00347         adjustGroupingStrategy();
00348         m_groupManager->setOnlyGroupWhenFull(m_ui.groupWhenFull->isChecked());
00349         KConfigGroup cg = config();
00350         cg.writeEntry("groupWhenFull", m_groupManager->onlyGroupWhenFull());
00351         changed = true;
00352     }
00353 
00354     if (m_groupManager->sortingStrategy() != (m_ui.sortingStrategy->currentIndex())) {
00355         m_groupManager->setSortingStrategy(static_cast<TaskManager::GroupManager::TaskSortingStrategy>(m_ui.sortingStrategy->itemData(m_ui.sortingStrategy->currentIndex()).toInt()));
00356         KConfigGroup cg = config();
00357         cg.writeEntry("sortingStrategy", static_cast<int>(m_groupManager->sortingStrategy()));
00358         changed = true;
00359     }
00360 
00361     if (m_rootGroupItem->maxRows() != (m_ui.maxRows->value())) {
00362         m_rootGroupItem->setMaxRows(m_ui.maxRows->value());
00363         KConfigGroup cg = config();
00364         cg.writeEntry("maxRows", m_rootGroupItem->maxRows());
00365         changed = true;
00366     }
00367 
00368     if (m_rootGroupItem->forceRows() != m_ui.fillRows->isChecked()) {
00369         m_rootGroupItem->setForceRows(m_ui.fillRows->isChecked());
00370         KConfigGroup cg = config();
00371         cg.writeEntry("forceRows", m_rootGroupItem->forceRows());
00372         changed = true;
00373     }
00374 
00375     if (m_showTooltip != (m_ui.showTooltip->checkState() == Qt::Checked)) {
00376         m_showTooltip = !m_showTooltip;
00377         KConfigGroup cg = config();
00378         cg.writeEntry("showTooltip", m_showTooltip);
00379         changed = true;
00380     }
00381 
00382     if (changed) {
00383         emit settingsChanged();
00384         emit configNeedsSaving();
00385         update();
00386     }
00387 }
00388 
00389 bool Tasks::showTooltip() const
00390 {
00391     return m_showTooltip;
00392 }
00393 
00394 
00395 
00396 void Tasks::themeRefresh()
00397 {
00398     delete m_taskItemBackground;
00399     m_taskItemBackground = 0;
00400 
00401     delete m_colorScheme;
00402     m_colorScheme = 0;
00403 }
00404 
00405 
00406 
00407 TaskGroupItem* Tasks::rootGroupItem()
00408 {
00409     return m_rootGroupItem;
00410 }
00411 
00412 
00413 K_EXPORT_PLASMA_APPLET(tasks, Tasks)
00414 
00415 #include "tasks.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
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.7
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