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

List:       kde-commits
Subject:    KDE/kdeplasma-addons/applets/nowplaying
From:       Alex Merry <kde () randomguy3 ! me ! uk>
Date:       2008-11-22 1:21:34
Message-ID: 1227316894.684094.19090.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 887470 by alexmerry:

A first attempt at making it play nicely on panels.  I really need to find out how \
layouting works these days.  It seems insanely complicated at first glance.



 M  +53 -9     controls.cpp  
 M  +18 -1     controls.h  
 M  +96 -10    nowplaying.cpp  
 M  +13 -3     nowplaying.h  


--- trunk/KDE/kdeplasma-addons/applets/nowplaying/controls.cpp #887469:887470
@@ -26,16 +26,10 @@
     connect(m_next, SIGNAL(clicked()), this, SIGNAL(next()));
     m_next->setMinimumSize(m_next->sizeFromIconSize(16));
 
-    QGraphicsLinearLayout* m_layout = new QGraphicsLinearLayout(Qt::Horizontal);
-    // adding stretches -> segfault
-    //m_layout->addStretch(1);
-    m_layout->addItem(m_prev);
-    m_layout->addItem(m_playpause);
-    m_layout->addItem(m_stop);
-    m_layout->addItem(m_next);
-    //m_layout->addStretch(1);
+    m_layout = new QGraphicsLinearLayout(Qt::Horizontal);
+    setLayout(m_layout);
 
-    setLayout(m_layout);
+    setDisplayedButtons(AllButtons);
 }
 
 Controls::~Controls()
@@ -95,4 +89,54 @@
     }
 }
 
+Controls::Buttons Controls::displayedButtons() const
+{
+    Buttons result;
+    if (m_prev->layout() == m_layout)
+    {
+        result |= PreviousButton;
+    }
+    if (m_next->layout() == m_layout)
+    {
+        result |= NextButton;
+    }
+    if (m_playpause->layout() == m_layout)
+    {
+        result |= PlayPauseButton;
+    }
+    if (m_stop->layout() == m_layout)
+    {
+        result |= StopButton;
+    }
+    return result;
+}
+
+static void showHideButton(QGraphicsLinearLayout* layout,
+                           QGraphicsWidget* button,
+                           bool show)
+{
+    if (show)
+    {
+        button->show();
+        layout->addItem(button);
+    }
+    else
+    {
+        button->hide();
+    }
+}
+
+void Controls::setDisplayedButtons(Buttons buttons)
+{
+    while (m_layout->count() != 0)
+    {
+        m_layout->removeAt(0);
+    }
+    showHideButton(m_layout, m_prev, (buttons & PreviousButton));
+    showHideButton(m_layout, m_playpause, (buttons & PlayPauseButton));
+    showHideButton(m_layout, m_stop, (buttons & StopButton));
+    showHideButton(m_layout, m_next, (buttons & NextButton));
+    m_layout->invalidate();
+}
+
 // vim: sw=4 sts=4 et tw=100
--- trunk/KDE/kdeplasma-addons/applets/nowplaying/controls.h #887469:887470
@@ -20,22 +20,37 @@
 #define CONTROLS_H
 
 #include <QGraphicsWidget>
+#include <QFlags>
 
 #include "nowplaying.h"
 
 namespace Plasma {
     class IconWidget;
 }
-class QGraphicsLayout;
+class QGraphicsLinearLayout;
 
 class Controls : public QGraphicsWidget
 {
     Q_OBJECT
 
 public:
+    enum Button
+    {
+        NoButtons = 0,
+        PlayPauseButton = 1,
+        StopButton = 2,
+        PreviousButton = 4,
+        NextButton = 8,
+        AllButtons = 15
+    };
+    Q_DECLARE_FLAGS(Buttons, Button);
+
     Controls(QGraphicsWidget *parent = 0);
     ~Controls();
 
+    Buttons displayedButtons() const;
+    void setDisplayedButtons(Buttons buttons);
+
 public slots:
     void stateChanged(State state);
     void setController(Plasma::Service* controller);
@@ -62,4 +77,6 @@
     Plasma::Service* m_controller;
 };
 
+Q_DECLARE_OPERATORS_FOR_FLAGS(Controls::Buttons);
+
 #endif // CONTROLS_H
--- trunk/KDE/kdeplasma-addons/applets/nowplaying/nowplaying.cpp #887469:887470
@@ -37,6 +37,7 @@
     : Plasma::Applet(parent, args),
       m_controller(0),
       m_state(NoPlayer),
+      m_currentLayout(NoLayout),
       m_volume(0),
       m_length(0),
       m_textPanel(new InfoPanel),
@@ -90,14 +91,18 @@
 
 void NowPlaying::init()
 {
-    m_layout = new QGraphicsGridLayout();
-    m_layout->addItem(m_textPanel, 0, 0);
-    m_layout->addItem(m_buttonPanel, 1, 0);
-    m_layout->addItem(m_positionSlider, 2, 0);
-    m_layout->addItem(m_volumeSlider, 0, 1, 3, 1); // rowspan, colspan
-
-    setLayout(m_layout);
-
+    switch (formFactor())
+    {
+        case Plasma::Horizontal:
+            layoutHorizontal();
+            break;
+        case Plasma::Vertical:
+            layoutHorizontal(); // FIXME
+            break;
+        default:
+            layoutPlanar();
+            break;
+    }
     Plasma::DataEngine* nowPlayingEngine = dataEngine("nowplaying");
     if ( nowPlayingEngine )
     {
@@ -111,10 +116,91 @@
     findPlayer();
 }
 
+void NowPlaying::layoutPlanar()
+{
+    if (m_currentLayout != PlanarLayout)
+    {
+        setMinimumSize(300, 200);
+        setAspectRatioMode(Plasma::IgnoreAspectRatio);
+
+        QGraphicsGridLayout* layout = new QGraphicsGridLayout();
+        m_textPanel->show();
+        layout->addItem(m_textPanel, 0, 0);
+        m_buttonPanel->show();
+        m_buttonPanel->setDisplayedButtons(Controls::AllButtons);
+        layout->addItem(m_buttonPanel, 1, 0);
+        m_positionSlider->show();
+        layout->addItem(m_positionSlider, 2, 0);
+        m_volumeSlider->show();
+        layout->addItem(m_volumeSlider, 0, 1, 3, 1); // rowspan, colspan
+
+        QGraphicsLayout* oldLayout = this->layout();
+        setLayout(layout);
+        delete oldLayout;
+
+        m_currentLayout = PlanarLayout;
+    }
+}
+
+void NowPlaying::layoutHorizontal()
+{
+    if (m_currentLayout != HorizontalLayout)
+    {
+        setMinimumSize(20, 10);
+        resize(preferredHeight() * 2, preferredHeight());
+        kDebug() << "preferredHeight():" << preferredHeight();
+        kDebug() << "preferredSize():" << preferredSize();
+        setAspectRatioMode(Plasma::KeepAspectRatio);
+
+        m_textPanel->hide();
+        m_positionSlider->hide();
+        m_volumeSlider->hide();
+
+        QGraphicsLinearLayout* layout = new QGraphicsLinearLayout();
+        m_buttonPanel->show();
+        kDebug() << "m_buttonPanel->preferredSize():" << \
m_buttonPanel->preferredSize(); +        \
m_buttonPanel->setDisplayedButtons(Controls::PlayPauseButton | Controls::NextButton); \
+        layout->addItem(m_buttonPanel); +
+        QGraphicsLayout* oldLayout = this->layout();
+        setLayout(layout);
+        delete oldLayout;
+
+        m_currentLayout = HorizontalLayout;
+    }
+}
+
+QSizeF NowPlaying::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
+{
+    kDebug() << "Asked for size hint" << which << "with constraint" << constraint;
+    return Applet::sizeHint(which, constraint);
+}
+
 void NowPlaying::constraintsUpdated(Plasma::Constraints constraints)
 {
-    // FIXME: we should probably change to everything in one
-    // or two lines if we're horizonal
+    kDebug() << "Constraints:" << constraints;
+    kDebug() << "Maximum size:" << maximumSize();
+    kDebug() << "Preferred size:" << preferredSize();
+    if (constraints & Plasma::FormFactorConstraint)
+    {
+        switch (formFactor())
+        {
+            case Plasma::Horizontal:
+                layoutHorizontal();
+                break;
+            case Plasma::Vertical:
+                layoutHorizontal(); // FIXME
+                break;
+            default:
+                layoutPlanar();
+                break;
+        }
+    }
+    if (constraints & Plasma::SizeConstraint && formFactor() == Plasma::Horizontal)
+    {
+        resize(preferredHeight() * 2, preferredHeight());
+        setAspectRatioMode(Plasma::KeepAspectRatio);
+    }
 }
 
 void NowPlaying::dataUpdated(const QString &name,
--- trunk/KDE/kdeplasma-addons/applets/nowplaying/nowplaying.h #887469:887470
@@ -26,8 +26,7 @@
 
 #include <QPixmap>
 
-class QGraphicsGridLayout;
-class QGraphicsLinearLayout;
+class QGraphicsLayout;
 namespace Plasma {
     class Slider;
 }
@@ -79,6 +78,9 @@
     void playerAdded(const QString &name);
     void playerRemoved(const QString &name);
 
+protected:
+    QSizeF sizeHint(Qt::SizeHint which, const QSizeF& constraint = QSizeF()) const;
+
 private slots:
     void play();
     void pause();
@@ -90,17 +92,25 @@
 
 private:
     void findPlayer();
+    void layoutPlanar();
+    void layoutHorizontal();
 
     QString m_watchingPlayer;
     Plasma::Service* m_controller;
     State m_state;
+    enum CurrentLayoutType
+    {
+        NoLayout,
+        PlanarLayout,
+        HorizontalLayout
+    };
+    CurrentLayoutType m_currentLayout;
 
     qreal m_volume;
     int m_length;
     QString m_track;
     QPixmap m_artwork;
 
-    QGraphicsGridLayout* m_layout;
     InfoPanel* m_textPanel;
     Controls* m_buttonPanel;
     Plasma::Slider* m_volumeSlider;


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

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