[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    playground/base/plasma/applets/desktop
From:       Matt Broadstone <mbroadst () gmail ! com>
Date:       2007-08-08 8:35:14
Message-ID: 1186562114.643883.4142.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 697627 by mbroadst:

Cleaned up and added a patch by Matias Costa providing a reference implementation of \
vertical organizing, also implements grid alignment. Yay desktop

 M  +1 -0      CMakeLists.txt  
 M  +110 -4    desktop.cpp  
 M  +58 -3     desktop.h  
 M  +63 -7     desktoporganizer.cpp  
 M  +30 -7     desktoporganizer.h  
 M  +11 -2     launcher.cpp  
 M  +7 -2      launcher.h  


--- trunk/playground/base/plasma/applets/desktop/CMakeLists.txt #697626:697627
@@ -3,6 +3,7 @@
 set(desktop_SRCS
     desktop.cpp
     launcher.cpp
+    desktoporganizer.cpp
 )
 
 
--- trunk/playground/base/plasma/applets/desktop/desktop.cpp #697626:697627
@@ -1,5 +1,6 @@
 /*
- *   Copyright (C) 2007 Matt Broadstone <mbroadst@gmail.com>
+ *   Copyright (C) 2007 Matt Broadstone <mbroadst@kde.org>
+ *   Copyright (C) 2007 Matias Costa <m.costacano@gmail.com>
  *
  *   This program is free software; you can redistribute it and/or modify
  *   it under the terms of the GNU Library General Public License version 2 as
@@ -19,6 +20,9 @@
 #include <QFileSystemWatcher>
 #include <QApplication>
 #include <QPainter>
+#include <QGraphicsSceneMouseEvent>
+#include <QGraphicsScene>
+#include <QTimer>
 
 #include <KDebug>
 #include <KGlobalSettings>
@@ -32,6 +36,8 @@
 
 #include "desktop.h"
 #include "desktop.moc"
+#include "launcher.h"
+#include "desktoporganizer.h"
 
 StandardDesktop::StandardDesktop(QObject *parent, const QStringList &args)
     : Plasma::Applet(parent, args),
@@ -60,7 +66,12 @@
     connect(m_solidEngine, SIGNAL(newSource(const QString&)),
                      this, SLOT(sourceAdded(const QString&)));
 
+    // Organization
+    m_organizer = new VerticalOrganizer(this);
 
+    // Grid
+    m_gridAlign = true;
+    setGridSize(QSizeF(100, 100));
 }
 
 StandardDesktop::~StandardDesktop()
@@ -69,18 +80,81 @@
     delete m_dirLister;
 }
 
+bool StandardDesktop::isGridAligned()
+{
+    return m_gridAlign;
+}
+
+void StandardDesktop::setGridAligned(bool align)
+{
+    m_gridAlign = align;
+}
+
+void StandardDesktop::alignToGrid(Launcher *item)
+{
+    QPointF currentPos = mapToParent(item->pos());
+    QPointF currentGridPos = mapToGrid(currentPos);
+    alignToGrid(item, currentGridPos);
+}
+
+void StandardDesktop::alignToGrid(Launcher *item, const QPointF &pos)
+{
+    qreal width = item->geometry().width();
+    qreal height = item->geometry().height();
+
+    QPointF scenePos = mapFromGrid(pos.toPoint());
+    scenePos.rx() -= width/2;
+    scenePos.ry() -= height/2;
+    item->setPos(scenePos);
+}
+
+QSizeF StandardDesktop::gridSize() const
+{
+    return m_gridSize;
+}
+
+QSize StandardDesktop::gridDimensions() const
+{
+    QSizeF thisSize = contentSize();
+    return QSize( int(thisSize.width() / m_gridSize.width()),
+                  int(thisSize.height() / m_gridSize.height()) );
+}
+
+void StandardDesktop::setGridSize(QSizeF gridSize)
+{
+    QSizeF desktopSize = contentSize();
+    qreal bestMachWidth = desktopSize.width()
+                        / qRound(desktopSize.width()/gridSize.width());
+    qreal bestMachHeight = desktopSize.height()
+                        / qRound(desktopSize.height()/gridSize.height());
+    m_gridSize = QSizeF(bestMachWidth, bestMachHeight);
+    if (m_gridAlign) {
+        foreach(Launcher *item, m_desktopItems) {
+            alignToGrid(item);
+        }
+    }
+}
+
 QSizeF StandardDesktop::contentSize() const
 {
     // This should probably use KWin, this is easy right now..
     // TODO fix for multihead
-    return QSizeF(QApplication::desktop()->screenGeometry().size());
+     return QSizeF(QApplication::desktop()->screenGeometry().size());
 }
 
 void StandardDesktop::paint(QPainter *painter, const QStyleOptionGraphicsItem \
*option,  const QRect &rect)
 {
+    Q_UNUSED(option);
     painter->setPen(Qt::red);
     painter->drawRect(rect);
+/*    QSizeF desktopSize = contentSize();
+    for (qreal i=m_gridSize.width(); i<desktopSize.width(); i+=m_gridSize.width()) {
+        painter->drawLine(QPointF(i, 0), QPointF(i, desktopSize.height()));
+    }
+    for (qreal i=m_gridSize.height(); i<desktopSize.height(); \
i+=m_gridSize.height()) { +        painter->drawLine(QPointF(0, i), \
QPointF(desktopSize.width(), i)); +    }*/
 }
 
 void StandardDesktop::clearDesktop()
@@ -102,6 +176,7 @@
 
 void StandardDesktop::newItemsFound(const KFileItemList &items)
 {
+    QList<Launcher*> newLaunchers;
     foreach (KFileItem *item, items)
     {
         if ( (item->isDir() && (item->name() == "." || item->name() == "..")) ||
@@ -112,12 +187,16 @@
             continue;
 
         FileLauncher *tmp = new FileLauncher(item, this);
+        tmp->installSceneEventFilter(this);
 
+        // kDebug() << "New launcher: " << tmp->size() << tmp->boundingRect() << \
endl; +        newLaunchers << tmp;
         m_desktopItems << tmp;
         m_fileItems[item] = tmp;
 
-        tmp->moveBy(100, 200);
+        tmp->setPos(100, 100);
     }
+    m_organizer->organize(newLaunchers);
 }
 
 void StandardDesktop::itemDeleted(KFileItem *file)
@@ -128,6 +207,7 @@
 
 void StandardDesktop::updated(const QString &source, Plasma::DataEngine::Data data)
 {
+    Q_UNUSED(source);
     QString deviceUdi = data["udi"].toString();
     bool newDevice = data["added"].toBool();
 
@@ -138,7 +218,9 @@
         if (!m_solidDevices[deviceUdi])
         {
             DeviceLauncher *launcher = new DeviceLauncher(deviceUdi, this);
-            launcher->setPos(300, 300);
+            launcher->installSceneEventFilter(this);
+
+            launcher->setPos(100, 100);
         }
         else
         {
@@ -159,5 +241,29 @@
     m_solidEngine->connectSource(source, this);
 }
 
+bool StandardDesktop::sceneEventFilter(QGraphicsItem *target, QEvent *event)
+{
+    switch (event->type())
+    {
+        case QEvent::GraphicsSceneMouseRelease:
+            {
+                if (m_gridAlign)
+                {
+                    foreach (QGraphicsItem *item, scene()->selectedItems())
+                    {
+                        Launcher *launcher = qgraphicsitem_cast<Launcher*>(item);
+                        if (launcher)
+                            alignToGrid(launcher);
+                    }
+                }
+            }
+            break;
 
+        default:
+            break;
+    }
 
+    return QGraphicsItem::sceneEventFilter(target, event);
+}
+
+K_EXPORT_PLASMA_APPLET(desktop, StandardDesktop)
--- trunk/playground/base/plasma/applets/desktop/desktop.h #697626:697627
@@ -1,5 +1,6 @@
 /*
- *   Copyright (C) 2007 Matt Broadstone <mbroadst@gmail.com>
+ *   Copyright (C) 2007 Matt Broadstone <mbroadst@kde.org>
+ *   Copyright (C) 2007 Matias Costa <m.costacano@gmail.com>
  *
  *   This program is free software; you can redistribute it and/or modify
  *   it under the terms of the GNU Library General Public License version 2 as
@@ -21,14 +22,21 @@
 #include <QHash>
 
 #include <plasma/applet.h>
-#include "launcher.h"
 
+
 namespace Plasma
 {
     class DataEngine;
 }
 
 class KDirLister;
+class Launcher;
+class FileLauncher;
+class DeviceLauncher;
+class KFileItemList;
+class KFileItem;
+class DesktopOrganizer;
+
 class StandardDesktop : public Plasma::Applet
 {
     Q_OBJECT
@@ -40,6 +48,35 @@
     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
                const QRect& rect);
 
+    bool isGridAligned();
+    void setGridAligned(bool align);
+
+    QSizeF gridSize() const;
+
+    // Temporarily public
+    void alignToGrid(Launcher *item);
+    void alignToGrid(Launcher *item, const QPointF &pos);
+
+    inline QPoint mapToGrid(const QPointF &pos) const;
+    inline QPointF mapFromGrid(const QPoint &pos) const;
+
+    /**
+        Sets the grid size to the nearest size which results in
+        a integer number of grid squares. If the desktop width is 1024
+        and the desired grid width is 100 the resulting gridSize will be
+        102.4 because it's the nearest width with a integer number of
+        divisions (10)
+
+        @arg size The desired gridSize to be set.
+     */
+
+    void setGridSize(QSizeF gridSize);
+    /**
+        The width and height of the size returned by this method represents
+        the grid places a Launcher can be placed.
+     */
+    QSize gridDimensions() const;
+
 public Q_SLOTS:
     void updated(const QString &, QHash<QString, QVariant> );
 
@@ -51,6 +88,9 @@
 
     void sourceAdded(const QString &source);
 
+protected:
+    bool sceneEventFilter(QGraphicsItem *watched, QEvent *event);
+
 private:
     KDirLister *m_dirLister;
     Plasma::DataEngine *m_solidEngine;
@@ -60,9 +100,24 @@
     QHash<KFileItem*, FileLauncher*> m_fileItems;
     QHash<QString, DeviceLauncher*> m_solidDevices;
 
+    DesktopOrganizer *m_organizer;
+
+    bool m_gridAlign;
+    QSizeF m_gridSize;
+    Launcher *m_target;
 };
 
-K_EXPORT_PLASMA_APPLET(desktop, StandardDesktop)
+QPoint StandardDesktop::mapToGrid(const QPointF &pos) const
+{
+    return QPoint(int(pos.x()/m_gridSize.width()),
+                  int(pos.y()/m_gridSize.height()));
+}
 
+QPointF StandardDesktop::mapFromGrid(const QPoint &pos) const
+{
+    return QPointF(pos.x()*m_gridSize.width() +m_gridSize.width()/2,
+                  pos.y()*m_gridSize.height() +m_gridSize.height()/2);
+}
+
 #endif
 
--- trunk/playground/base/plasma/applets/desktop/desktoporganizer.cpp #697626:697627
@@ -1,6 +1,40 @@
+/*
+ *   Copyright (C) 2007 Matt Broadstone <mbroadst@kde.org>
+ *   Copyright (C) 2007 Matias Costa <m.costacano@gmail.com>
+ *
+ *   This program is free software; you can redistribute it and/or modify
+ *   it under the terms of the GNU Library General Public License version 2 as
+ *   published by the Free Software Foundation
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details
+ *
+ *   You should have received a copy of the GNU Library General Public
+ *   License along with this program; if not, write to the
+ *   Free Software Foundation, Inc.,
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#include <QGraphicsItem>
+#include <QGraphicsScene>
+#include <QListIterator>
+
+#include "launcher.h"
+#include "desktop.h"
 #include "desktoporganizer.h"
 
-VerticalOrganizer::VerticalOrganizer(QGraphicsItem *parent)
+DesktopOrganizer::DesktopOrganizer(StandardDesktop *parent)
+    : m_desktop(parent)
+{
+}
+
+DesktopOrganizer::~DesktopOrganizer()
+{
+}
+
+VerticalOrganizer::VerticalOrganizer(StandardDesktop *parent)
     : DesktopOrganizer(parent)
 {
 }
@@ -11,17 +45,39 @@
 
 QString VerticalOrganizer::name() const
 {
-    return QString("Vertically"):
+    return QString("Vertically");
 }
 
-void VerticalOrganizer::organize(const QList<QGraphicsItem*> items)
+void VerticalOrganizer::organize(const QList<Launcher*> &items)
 {
-    int x = 0;
-    int y = 0;
+    if (items.isEmpty())
+    {
+        kDebug() << "Warning: VerticalOrganizer::organize call with empty \
list"<<endl; +        return;
+    }
 
-    foreach (QGraphicsItem *item, items)
+    Launcher *icon;
+    QListIterator<Launcher*> it(items);
+    icon = it.next();
+
+    QSize gridDim = m_desktop->gridDimensions();
+    for(int x=0; x < gridDim.width(); ++x)
     {
-
+        for(int y=0; y < gridDim.height(); ++y)
+        {
+            QPointF newGridPos = m_desktop->mapFromGrid(QPoint(x,y));
+            QGraphicsItem *existing = m_desktop->scene()->itemAt(newGridPos);
+            if (!existing || existing == m_desktop)
+            {
+                m_desktop->alignToGrid(icon, QPointF(x, y));
+                if (it.hasNext()) {
+                    icon = it.next();
+                } else {
+                    /* all items located */
+                    return;
+                }
+            }
+        }
     }
 }
 
--- trunk/playground/base/plasma/applets/desktop/desktoporganizer.h #697626:697627
@@ -1,3 +1,21 @@
+/*
+ *   Copyright (C) 2007 Matt Broadstone <mbroadst@kde.org>
+ *   Copyright (C) 2007 Matias Costa <m.costacano@gmail.com>
+ *
+ *   This program is free software; you can redistribute it and/or modify
+ *   it under the terms of the GNU Library General Public License version 2 as
+ *   published by the Free Software Foundation
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details
+ *
+ *   You should have received a copy of the GNU Library General Public
+ *   License along with this program; if not, write to the
+ *   Free Software Foundation, Inc.,
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
 #ifndef DESKTOPORGANIZER_H
 #define DESKTOPORGANIZER_H
 
@@ -5,24 +23,29 @@
 #include <QGraphicsItem>
 
 class StandardDesktop;
+class Launcher;
+
 class DesktopOrganizer
 {
 public:
     DesktopOrganizer(StandardDesktop *desktop);
-    ~DesktopOrganizer();
+    virtual ~DesktopOrganizer();
 
-    QString name() const = 0;
-    void organize(const QList<QGraphicsItem*> items) = 0;
+    virtual QString name() const = 0;
+    virtual void organize(const QList<Launcher*> &items) = 0;
+
+protected:
+    StandardDesktop *m_desktop;
 };
 
-class VerticalNameOrganizer : public DesktopOrganizer
+class VerticalOrganizer : public DesktopOrganizer
 {
 public:
-    VerticalNameOrganizer(StandardDesktop *desktop);
-    ~VerticalNameOrganizer();
+    VerticalOrganizer(StandardDesktop *desktop);
+    ~VerticalOrganizer();
 
     QString name() const;
-    void organize(const QList<QGraphicsItem*> items);
+    void organize(const QList<Launcher*> &items);
 };
 
 
--- trunk/playground/base/plasma/applets/desktop/launcher.cpp #697626:697627
@@ -1,5 +1,6 @@
 /*
- *   Copyright (C) 2007 Matt Broadstone <mbroadst@gmail.com>
+ *   Copyright (C) 2007 Matt Broadstone <mbroadst@kde.org>
+ *   Copyright (C) 2007 Matias Costa <m.costacano@gmail.com>
  *
  *   This program is free software; you can redistribute it and/or modify
  *   it under the terms of the GNU Library General Public License version 2 as
@@ -43,7 +44,7 @@
     setFlag(QGraphicsItem::ItemIsSelectable, true);
     setFlag(QGraphicsItem::ItemIsFocusable, true);
     int sz = KIconLoader::global()->currentSize( K3Icon::Desktop );
-    setIconSize(sz, sz); 
+    setIconSize(sz, sz);
 }
 
 Launcher::~Launcher()
@@ -57,6 +58,10 @@
 
     Icon::mouseDoubleClickEvent(event);
 }
+void Launcher::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
+{
+    Icon::mouseMoveEvent(event);
+}
 
 void Launcher::mousePressEvent(QGraphicsSceneMouseEvent *event)
 {
@@ -66,6 +71,10 @@
     Icon::mousePressEvent(event);
 }
 
+void Launcher::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
+{
+    Icon::mouseReleaseEvent(event);
+}
 
 // Silly little helper
 QString fileName(const QString &name)
--- trunk/playground/base/plasma/applets/desktop/launcher.h #697626:697627
@@ -1,5 +1,6 @@
 /*
- *   Copyright (C) 2007 Matt Broadstone <mbroadst@gmail.com>
+ *   Copyright (C) 2007 Matt Broadstone <mbroadst@kde.org>
+ *   Copyright (C) 2007 Matias Costa <m.costacano@gmail.com>
  *
  *   This program is free software; you can redistribute it and/or modify
  *   it under the terms of the GNU Library General Public License version 2 as
@@ -18,6 +19,8 @@
 #ifndef LAUNCHERBUTTON_H
 #define LAUNCHERBUTTON_H
 
+#include <KFileItem>
+
 #include <plasma/widgets/icon.h>
 #include <solid/device.h>
 
@@ -28,9 +31,11 @@
     Launcher(QGraphicsItem * parent = 0);
     virtual ~Launcher();
 
-protected:
+// protected:
     void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
+    void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
     void mousePressEvent(QGraphicsSceneMouseEvent *event);
+    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
 
     virtual void launch() = 0;
 


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic