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

List:       kde-panel-devel
Subject:    [PATCH] Picture navigation for the frame plasmoid
From:       "=?UTF-8?Q?=C3=89ric_ALBER?=" <eric.alber () gmail ! com>
Date:       2008-07-15 22:25:36
Message-ID: 676e80e0807151525v5e0b3e7dx45559f4bd055fcf () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Hello,

I worked a bit on the frame plasmoid to add picture navigation in slideshow
mode.
I implemented a Lightbox
<http://www.lokeshdhakar.com/projects/lightbox2/>style image
navigation

I attach the patch to this mail. I created it in the kdeplasma-addons
folder, so apply it in the same folder.

Tell me what you think about that :)

Regards,

Eric

[Attachment #5 (text/html)]

<div dir="ltr">Hello,<br><br>I worked a bit on the frame plasmoid to add picture \
navigation in slideshow mode.<br>I implemented a <a \
href="http://www.lokeshdhakar.com/projects/lightbox2/">Lightbox</a> style image \
navigation<br> <br>I attach the patch to this mail. I created it in the \
kdeplasma-addons folder, so apply it in the same folder.<br><br>Tell me what you \
think about that :)<br><br>Regards,<br><br>Eric<br><br><br></div>


["frame-browsing.patch" (text/x-patch)]

Index: applets/frame/frame.h
===================================================================
--- applets/frame/frame.h	(révision 832975)
+++ applets/frame/frame.h	(copie de travail)
@@ -50,29 +50,36 @@
 
     public slots:
         void createConfigurationInterface( KConfigDialog *parent );
-	void dataUpdated( const QString &name, const Plasma::DataEngine::Data &data );
+        void dataUpdated( const QString &name, const Plasma::DataEngine::Data &data \
);  
     protected Q_SLOTS:
         void dropEvent(QGraphicsSceneDragDropEvent *event);
         void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
         void configAccepted();
-        void updatePicture();
+        void nextPicture();
+        void previousPicture();
 
     private Q_SLOTS:
         void addDir();
         void removeDir();
+        void displayBrowseLabels();
 
     protected:
         void constraintsEvent(Plasma::Constraints constraints);
+        virtual void hoverEnterEvent(QGraphicsSceneHoverEvent* event);
+        virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent* event);
+        virtual void hoverMoveEvent(QGraphicsSceneHoverEvent* event);
+        virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
 
     private:
         void paintCache(const QStyleOptionGraphicsItem *option,
                         const QSize &contentsSize);
         void initSlideShow();
+        QPixmap createTextPixmap(const QString& text, int width, int height);
 
         /// The current color of the frame
         QColor m_frameColor;
-	/// Configuration dialog
+        /// Configuration dialog
         ConfigDialog *m_configDialog; 
         /// true if the user wants a frame. If false, there's only the black border \
around the picture  bool m_frame;
@@ -82,9 +89,9 @@
         bool m_smoothScaling;
         /// If true, the picture will have a drop shadow.
         bool m_shadow;
-	/// PoTD
-	QString m_potdProvider;
-	bool m_potd;
+        /// PoTD
+        QString m_potdProvider;
+        bool m_potd;
         /// Stores the current picture URL when slideShow is false. Wikipedia \
Picture of the Day is default.  KUrl m_currentUrl;
         /// The current slideshow folder
@@ -104,6 +111,16 @@
         bool m_random;
         bool m_recursiveSlideShow;
         SlideShow* m_mySlideShow;
+        /// Pixmap for the "Previous" label
+        QPixmap m_previousLabelPixmap;
+        /// Flag set to true if the "Previous" label must be displayed
+        bool m_displayPreviousLabel;
+        /// Pixmap for the "Next" label
+        QPixmap m_nextLabelPixmap;
+        /// Flag set to true if the "Next" label must be displayed
+        bool m_displayNextLabel;
+        /// Timer used to delay labels display after the mouse cursor enters the \
plasmoid. +        QTimer* m_browseLabelsTimer;
 };
 
 K_EXPORT_PLASMA_APPLET(frame, Frame)
Index: applets/frame/slideshow.cpp
===================================================================
--- applets/frame/slideshow.cpp	(révision 832975)
+++ applets/frame/slideshow.cpp	(copie de travail)
@@ -23,14 +23,13 @@
 
 #include "picture.h"
 #include "slideshow.h"
-#include <stdlib.h>
 
 SlideShow::SlideShow()
 {
     m_filters << "*.jpeg" << "*.jpg" << "*.png" << "*.svg" << "*.svgz"; // use mime \
types?  m_slideNumber = 0;
-    useRandom = false;
-    randomInt = 0;
+    m_useRandom = false;
+    m_randomInt = 0;
 }
 
 SlideShow::~SlideShow()
@@ -39,7 +38,7 @@
 
 void SlideShow::setRandom(bool tmp)
 {
-	useRandom = tmp;
+    m_useRandom = tmp;
 }
 
 void SlideShow::setDirs(const QStringList &slideShowPath, bool recursive)
@@ -63,7 +62,7 @@
 void SlideShow::addImage(const QString &imagePath)
 {
     if (!m_pictures.contains(imagePath)) {
-	    m_pictures.append(imagePath);
+        m_pictures.append(imagePath);
     }
 }
 
@@ -73,18 +72,19 @@
 
     dir.setNameFilters(m_filters);
     if (dir.entryList().isEmpty())  {
-	//TODO remove the path from the list
-	return;
+    //TODO remove the path from the list
+    return;
     }
     foreach (const QString &imageFile, dir.entryList(QDir::Files)) {
-	    addImage(path + "/" + imageFile);
+        addImage(path + "/" + imageFile);
     }
     KRandomSequence randomSequence;
-    indexList.clear();
+    m_indexList.clear();
     //get the number of sounds then shuffle it: each number will be taken once then \
                the sequence will come back
-    for (uint j = 0; j < m_pictures.count(); j++) 
-	indexList.append(j);
-    randomSequence.randomize(indexList);
+    for (int j = 0; j < m_pictures.count(); j++) {
+        m_indexList.append(j);
+    }
+    randomSequence.randomize(m_indexList);
 }
 
 void SlideShow::addRecursiveDir(const QString &path)
@@ -97,28 +97,34 @@
     }
 }
 
-QImage SlideShow::getImage() 
+QImage SlideShow::getNextImage() 
 {
-    KUrl url = getUrl();
+    KUrl url = getUrl(1);
     Picture myPicture;
     return myPicture.setPicture(url);
 }
 
-KUrl SlideShow::getUrl() 
+QImage SlideShow::getPreviousImage()
 {
+    KUrl url = getUrl(-1);
+    Picture myPicture;
+    return myPicture.setPicture(url);
+}
+
+KUrl SlideShow::getUrl(int delta)
+{
     if (!m_pictures.isEmpty()) {
-
-	    int index = -1;
-	    if(useRandom) {
-		    randomInt++;
-		    index = indexList[randomInt%m_pictures.count()];
-		    kDebug() << "Random was selected and the index was: " << index << " out of " \
                << m_pictures.count() << " images" << endl;
-	    } else {
-		    index = m_slideNumber++ % m_pictures.count();
-	    }
-
-	    return KUrl(m_pictures.at(index));
+        int index = -1;
+        if(m_useRandom) {
+            m_randomInt += delta;
+            index = m_indexList[m_randomInt % m_pictures.count()];
+            kDebug() << "Random was selected and the index was: " << index << " out \
of " << m_pictures.count() << " images" << endl; +        } else {
+            m_slideNumber += delta;
+            index = m_slideNumber % m_pictures.count();
+        }
+        return KUrl(m_pictures.at(index));
     } else {
-	    return KUrl("Default");
+        return KUrl("Default");
     }
 }
Index: applets/frame/slideshow.h
===================================================================
--- applets/frame/slideshow.h	(révision 832975)
+++ applets/frame/slideshow.h	(copie de travail)
@@ -23,35 +23,33 @@
 #include <QString>
 #include <QStringList>
 #include <QImage>
-#include <math.h>
-#include <iostream>
 
 class KUrl;
 
 class SlideShow
 {
-	public:
-		SlideShow();
-		~SlideShow();
-		
-		void setDirs(const QStringList &slideShowPaths, bool recursive = false);
-		void setImage(const QString &imagePath);
-		void setRandom(bool);
-		QImage getImage();
-		
-	private:
-		QStringList m_pictures;
-		QStringList m_filters;
-		int m_slideNumber;
-		bool useRandom;
-		
-		void addImage(const QString &imagePath);
-		void addDir(const QString &path);
-		void addRecursiveDir(const QString &path);
-		KUrl getUrl();
-		QList<int> indexList;
-		int randomInt;
+    public:
+        SlideShow();
+        ~SlideShow();
+    
+        void setDirs(const QStringList &slideShowPaths, bool recursive = false);
+        void setImage(const QString &imagePath);
+        void setRandom(bool);
+        QImage getNextImage();
+        QImage getPreviousImage();
+    
+    private:
+        void addImage(const QString &imagePath);
+        void addDir(const QString &path);
+        void addRecursiveDir(const QString &path);
+        KUrl getUrl(int delta);
 
+        QStringList m_pictures;
+        QStringList m_filters;
+        uint m_slideNumber;
+        bool m_useRandom;
+        QList<int> m_indexList;
+        uint m_randomInt;
 };
 
 #endif /*SLIDESHOW_H_*/
Index: applets/frame/frame.cpp
===================================================================
--- applets/frame/frame.cpp	(révision 832975)
+++ applets/frame/frame.cpp	(copie de travail)
@@ -47,7 +47,24 @@
 
 Frame::Frame(QObject *parent, const QVariantList &args)
     : Plasma::Applet(parent, args),
-      m_configDialog( 0 )
+      m_configDialog(NULL),
+      m_frame(false),
+      m_roundCorners(false),
+      m_smoothScaling(false),
+      m_shadow(false),
+      m_potd(false),
+      m_slideNumber(0),
+      m_slideShowTimer(NULL),
+      m_slideshowTime(0),
+      m_frameOutline(0),
+      m_swOutline(0),
+      m_slideShow(false),
+      m_random(false),
+      m_recursiveSlideShow(false),
+      m_mySlideShow(NULL),
+      m_displayPreviousLabel(false),
+      m_displayNextLabel(false),
+      m_browseLabelsTimer(NULL)
 {
     setHasConfigurationInterface(true);
     setAcceptDrops(true);
@@ -60,6 +77,20 @@
     } else {
         m_currentUrl = KUrl("Default");
     }
+
+    QRect prevBr(Plasma::Theme::defaultTheme()->fontMetrics().boundingRect(tr("Previous")));
 +    prevBr.adjust(0, 0, 10, 10);
+    QRect nextBr(Plasma::Theme::defaultTheme()->fontMetrics().boundingRect(tr("Next")));
 +    nextBr.adjust(0, 0, 10, 10);
+    int w = qMax(prevBr.width(), nextBr.width());
+    int h = qMax(prevBr.height(), nextBr.height());
+    m_previousLabelPixmap = createTextPixmap(tr("Previous"), w, h);
+    m_nextLabelPixmap = createTextPixmap(tr("Next"), w, h);
+
+    m_browseLabelsTimer = new QTimer(this);
+    m_browseLabelsTimer->setInterval(500);
+    m_browseLabelsTimer->setSingleShot(true);
+    connect(m_browseLabelsTimer, SIGNAL(timeout()), this, \
SLOT(displayBrowseLabels()));  }
 
 Frame::~Frame()
@@ -69,15 +100,17 @@
 
 void Frame::dataUpdated( const QString &name, const Plasma::DataEngine::Data &data )
 {
+    Q_UNUSED(name);
+
     QDate mCurrentDate = QDate::currentDate();
     const QString identifier = m_potdProvider + ':' + mCurrentDate.toString( \
Qt::ISODate );  
     QImage _picture = data[ identifier ].value<QImage>();
     
     if ( !_picture.isNull() ) {
-	m_picture = _picture;
-	resize(contentSizeHint());
-	m_pixmapCache = QPixmap();
+    m_picture = _picture;
+    resize(contentSizeHint());
+    m_pixmapCache = QPixmap();
         prepareGeometryChange();
         updateGeometry();
         update();
@@ -114,7 +147,7 @@
 
     // Initialize the slideshow timer
     m_slideShowTimer = new QTimer(this);
-    connect(m_slideShowTimer, SIGNAL(timeout()), this, SLOT(updatePicture()));
+    connect(m_slideShowTimer, SIGNAL(timeout()), this, SLOT(nextPicture()));
     m_slideShowTimer->setInterval(m_slideshowTime * 1000);
 
     initSlideShow();
@@ -131,6 +164,49 @@
     }
 }
 
+void Frame::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
+{
+    Q_UNUSED(event);
+    m_browseLabelsTimer->start();
+}
+
+void Frame::hoverLeaveEvent(QGraphicsSceneHoverEvent* event)
+{
+    Applet::hoverLeaveEvent(event);
+    m_displayPreviousLabel = false;
+    m_displayNextLabel = false;
+    if (m_slideShow) {
+        m_pixmapCache = QPixmap();
+        update();
+    }
+    m_browseLabelsTimer->stop();
+}
+
+void Frame::hoverMoveEvent(QGraphicsSceneHoverEvent* event)
+{
+    Applet::hoverMoveEvent(event);
+    bool old = m_displayPreviousLabel;
+    m_displayPreviousLabel = event->pos().x() < size().width() / 2;
+    m_displayNextLabel = !m_displayPreviousLabel;
+    if (m_slideShow && m_displayPreviousLabel != old) {
+        m_pixmapCache = QPixmap();
+        update();
+    }
+}
+
+void Frame::mousePressEvent(QGraphicsSceneMouseEvent *event)
+{
+    Applet::mousePressEvent(event);
+    if (m_slideShow && event->buttons() & Qt::LeftButton) {
+        if (m_displayPreviousLabel) {
+            previousPicture();
+        } else {
+            nextPicture();
+        }
+        m_slideShowTimer->start();
+    }
+}
+
 QSizeF Frame::contentSizeHint() const {
     if (!m_picture.isNull()) {
         QSizeF sizeHint;
@@ -146,14 +222,22 @@
     }
 }
 
-void Frame::updatePicture()
+void Frame::nextPicture()
 {
-    m_picture = m_mySlideShow->getImage();
+    m_picture = m_mySlideShow->getNextImage();
     resize(contentSizeHint());
     m_pixmapCache = QPixmap();
     update();
 }
 
+void Frame::previousPicture()
+{
+    m_picture = m_mySlideShow->getPreviousImage();
+    resize(contentSizeHint());
+    m_pixmapCache = QPixmap();
+    update();    
+}
+
 void Frame::addDir()
 {
     KDirSelectDialog dialog(KUrl(), true);
@@ -177,15 +261,23 @@
     }
 }
 
+void Frame::displayBrowseLabels()
+{
+    if (m_slideShow) {
+        m_pixmapCache = QPixmap();
+        update();
+    }
+}
+
 void Frame::createConfigurationInterface(KConfigDialog *parent)
 {
     m_configDialog = new ConfigDialog( parent );
     
     KService::List services = KServiceTypeTrader::self()->query( \
"PlasmaPoTD/Plugin");  foreach (const KService::Ptr &service, services) {
-	const QString *service_name = new QString ( service->name() );
-	const QVariant *service_identifier = new QVariant ( service->property( \
                "X-KDE-PlasmaPoTDProvider-Identifier", QVariant::String ).toString() \
                );
-	m_configDialog->ui.potdComboBox->insertItem( \
m_configDialog->ui.potdComboBox->count(), *service_name, *service_identifier ); +    \
const QString *service_name = new QString ( service->name() ); +    const QVariant \
*service_identifier = new QVariant ( service->property( \
"X-KDE-PlasmaPoTDProvider-Identifier", QVariant::String ).toString() ); +    \
m_configDialog->ui.potdComboBox->insertItem( \
m_configDialog->ui.potdComboBox->count(), *service_name, *service_identifier );  }
 
     parent->setButtons(  KDialog::Ok | KDialog::Cancel | KDialog::Apply);
@@ -205,11 +297,11 @@
     m_configDialog->setFrameColor(m_frameColor);
 
     if (m_slideShow)
-	m_configDialog->ui.pictureComboBox->setCurrentIndex(1);
+    m_configDialog->ui.pictureComboBox->setCurrentIndex(1);
     else if (m_potd)
-	m_configDialog->ui.pictureComboBox->setCurrentIndex(2);
+    m_configDialog->ui.pictureComboBox->setCurrentIndex(2);
     else
-	m_configDialog->ui.pictureComboBox->setCurrentIndex(0);
+    m_configDialog->ui.pictureComboBox->setCurrentIndex(0);
 
     m_configDialog->ui.randomCheckBox->setCheckState(m_random ? Qt::Checked : \
                Qt::Unchecked);
     m_configDialog->ui.recursiveCheckBox->setCheckState(m_recursiveSlideShow ? \
Qt::Checked : Qt::Unchecked); @@ -244,14 +336,14 @@
 
     if (m_configDialog->ui.pictureComboBox->currentIndex() == 1)
     {
-	m_slideShow = true;
-	m_potd = false;
+    m_slideShow = true;
+    m_potd = false;
     }  else if (m_configDialog->ui.pictureComboBox->currentIndex() == 2)   {
-	m_slideShow = false;
-	m_potd = true;
+    m_slideShow = false;
+    m_potd = true;
     }  else {
-	m_slideShow = false;
-	m_potd = false;
+    m_slideShow = false;
+    m_potd = false;
     }
 
     m_random = m_configDialog->random();
@@ -285,28 +377,28 @@
 void Frame::initSlideShow()
 {
     if (m_slideShow) {
-	    m_mySlideShow->setDirs(m_slideShowPaths, m_recursiveSlideShow);
+        m_mySlideShow->setDirs(m_slideShowPaths, m_recursiveSlideShow);
             m_mySlideShow->setRandom(m_random);
-	    m_slideShowTimer->start();
+        m_slideShowTimer->start();
     } else if (m_potd) {
-	Plasma::DataEngine *engine = dataEngine( "potd" );
-	if ( !engine )
-	    return;
+    Plasma::DataEngine *engine = dataEngine( "potd" );
+    if ( !engine )
+        return;
 
-	QDate mCurrentDate = QDate::currentDate();
-	const QString identifier = m_potdProvider + ':' + mCurrentDate.toString( \
Qt::ISODate ); +    QDate mCurrentDate = QDate::currentDate();
+    const QString identifier = m_potdProvider + ':' + mCurrentDate.toString( \
Qt::ISODate );  
-	engine->disconnectSource( identifier, this );
-	engine->connectSource( identifier, this );
+    engine->disconnectSource( identifier, this );
+    engine->connectSource( identifier, this );
     
-	const Plasma::DataEngine::Data data = engine->query( identifier );
+    const Plasma::DataEngine::Data data = engine->query( identifier );
     } else {
-	    m_mySlideShow->setImage(m_currentUrl.path());
-	    m_slideShowTimer->stop();
+        m_mySlideShow->setImage(m_currentUrl.path());
+        m_slideShowTimer->stop();
     }
     
     if (!m_potd)
-	updatePicture();
+        nextPicture();
 }
 
 void Frame::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
@@ -324,7 +416,7 @@
     KUrl droppedUrl = (KUrl::List::fromMimeData(event->mimeData())).at(0);
     // If the url is a local directory start slideshowmode
     if (droppedUrl.isLocalFile() && QFileInfo(droppedUrl.path()).isDir()) {
-    	m_slideShowPaths.append(droppedUrl.path());
+        m_slideShowPaths.append(droppedUrl.path());
         if (!m_slideShow) {
             m_slideShow = true;
         }
@@ -435,7 +527,32 @@
         p->drawPath(framePath);
     }
 
+    if (m_slideShow && !m_browseLabelsTimer->isActive()) {
+        if (m_displayPreviousLabel) {
+            p->drawPixmap(frameRect.x(), frameRect.y() + frameRect.height() / 4, \
m_previousLabelPixmap); +        }
+        if (m_displayNextLabel) {
+            p->drawPixmap(frameRect.x() + frameRect.width() - \
m_nextLabelPixmap.width(), frameRect.y() + frameRect.height() / 4, \
m_nextLabelPixmap); +        }
+    }
     delete p;
 }
 
+QPixmap Frame::createTextPixmap(const QString& text, int width, int height)
+{
+    Plasma::Theme* theme = Plasma::Theme::defaultTheme();
+    QPixmap label(width, height);
+    QPixmap alpha(width, height);
+    label.fill(theme->color(Plasma::Theme::BackgroundColor));
+    alpha.fill(QColor(128, 128, 128));
+    label.setAlphaChannel(alpha);
+
+    QPainter p(&label);
+    p.setFont(theme->font(Plasma::Theme::DefaultFont));
+    p.setPen(theme->color(Plasma::Theme::TextColor));
+    p.setBrush(theme->color(Plasma::Theme::TextColor));
+    p.drawText(0, height, width, -height, Qt::AlignCenter, text);
+    return label;
+}
+
 #include "frame.moc"



_______________________________________________
Panel-devel mailing list
Panel-devel@kde.org
https://mail.kde.org/mailman/listinfo/panel-devel


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

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