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

List:       kde-panel-devel
Subject:    Re: Review Request: Comic applet proted to PopupApplet
From:       Giulio Camuffo <giuliocamuffo () gmail ! com>
Date:       2009-11-01 20:43:10
Message-ID: 200911012141.59323.giuliocamuffo () gmail ! com
[Download RAW message or body]

In data sabato 31 ottobre 2009 10:11:47, Marco Martin ha scritto:
: > On Saturday 31 October 2009, Aaron J. Seigo wrote:
> > On October 30, 2009, Giulio Camuffo wrote:
> > > In data venerd́ 30 ottobre 2009 18:03:07, Aaron J. Seigo ha scritto:
> > > : > On October 30, 2009, Giulio Camuffo wrote:
> > > : >
> > > > > So this patch fixes this.
> > > >
> > > > i think you forgot the patch
> >
> > it looks good except for setting a minimum size of 20x20. that's a bit
> > too big for people who will be wanting 16px tall panels :) set it to 8x8
> > if anything?
> 
> shouldn't be done in popupapplets with icon, if at all?
> 

removed the minimum size

["comic.patch" (text/x-patch)]

Index: comic.h
===================================================================
--- comic.h	(revision 1042640)
+++ comic.h	(copia locale)
@@ -26,7 +26,7 @@
 #include <QtGui/QImage>
 
 #include <KUrl>
-#include <plasma/applet.h>
+#include <plasma/popupapplet.h>
 #include <plasma/dataengine.h>
 
 class ArrowWidget;
@@ -45,7 +45,7 @@
 class TabBar;
 }
 
-class ComicApplet : public Plasma::Applet
+class ComicApplet : public Plasma::PopupApplet
 {
     Q_OBJECT
 
@@ -56,6 +56,8 @@
         void init();
         virtual QList<QAction*> contextualActions();
 
+        QGraphicsWidget *graphicsWidget();
+
     public Q_SLOTS:
         void dataUpdated( const QString &name, const Plasma::DataEngine::Data &data \
);  void createConfigurationInterface( KConfigDialog *parent );
@@ -83,13 +85,8 @@
         void fullView();
 
     protected:
-        void mousePressEvent( QGraphicsSceneMouseEvent* );
-        void mouseReleaseEvent( QGraphicsSceneMouseEvent* );
-        void wheelEvent( QGraphicsSceneWheelEvent* );
-        void hoverEnterEvent( QGraphicsSceneHoverEvent* );
-        void hoverLeaveEvent( QGraphicsSceneHoverEvent* );
-        void constraintsEvent( Plasma::Constraints constraints );
         QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) \
const; +        bool eventFilter( QObject *receiver, QEvent *event );
 
     private:
         void changeComic( bool differentComic );
@@ -131,6 +128,7 @@
         QTimer *mDateChangedTimer;
         QTimer *mReloadTimer;
         QList<QAction*> mActions;
+        QGraphicsWidget *mMainWidget;
         FullViewWidget *mFullViewWidget;
         QAction *mActionGoFirst;
         QAction *mActionGoLast;
Index: comic.cpp
===================================================================
--- comic.cpp	(revision 1042640)
+++ comic.cpp	(copia locale)
@@ -25,6 +25,8 @@
 #include <QtCore/QTimer>
 #include <QtGui/QAction>
 #include <QtGui/QGraphicsLinearLayout>
+#include <QtGui/QGraphicsScene>
+#include <QtGui/QGraphicsView>
 #include <QtGui/QGraphicsSceneMouseEvent>
 #include <QtGui/QGraphicsSceneWheelEvent>
 #include <QtGui/QLabel>
@@ -108,7 +110,7 @@
 static const int s_tabHeight = 24;//used because there is no initial preferredHeight \
set  
 ComicApplet::ComicApplet( QObject *parent, const QVariantList &args )
-    : Plasma::Applet( parent, args ),
+    : Plasma::PopupApplet( parent, args ),
       mShowPreviousButton( false ),
       mShowNextButton( false ),
       mShowComicUrl( false ),
@@ -117,6 +119,7 @@
       mShowComicIdentifier( false ),
       mArrowsOnHover( true ),
       mMiddleClick( true ),
+      mMainWidget( 0 ),
       mFullViewWidget( 0 ),
       mActionShop( 0 ),
       mEngine( 0 ),
@@ -127,63 +130,12 @@
       mZoomButton( 0 )
 {
     setHasConfigurationInterface( true );
-    setMinimumSize( QSizeF( 80, 40 ) );
     resize( 600, 250 );
+
     setAspectRatioMode( Plasma::IgnoreAspectRatio );
+    setPopupIcon( "face-smile-big" );
 
-    mTabBar = new Plasma::TabBar( this );
-    mTabBar->hide();
-    connect( mTabBar, SIGNAL( currentChanged( int ) ), this, SLOT( slotTabChanged( \
                int ) ) );
-
-    mLabelTop = new Plasma::Label( this );
-    mLabelTop->setMinimumWidth( 0 );
-    mLabelTop->nativeWidget()->setWordWrap( false );
-    mLabelTop->nativeWidget()->setSizePolicy( QSizePolicy( QSizePolicy::Preferred, \
                QSizePolicy::Fixed ) );
-    mLabelTop->setAlignment( Qt::AlignCenter );
-    mLabelTop->hide();
-
-    mImageWidget = new ImageWidget( this );
-    mImageWidget->setZValue( 0 );
-    mImageWidget->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, \
                QSizePolicy::Expanding ) );
-    mImageWidget->hide();
-
-    mLabelId = new Plasma::Label( this );
-    mLabelId->setMinimumWidth( 0 );
-    mLabelId->nativeWidget()->setWordWrap( false );
-    mLabelId->nativeWidget()->setCursor( Qt::PointingHandCursor );
-    mLabelId->nativeWidget()->setToolTip( i18n( "Jump to Strip ..." ) );
-    mLabelId->nativeWidget()->setSizePolicy( QSizePolicy( QSizePolicy::Preferred, \
                QSizePolicy::Fixed ) );
-    mLabelId->hide();
-
-    mLabelUrl = new Plasma::Label( this );
-    mLabelUrl->setMinimumWidth( 0 );
-    mLabelUrl->nativeWidget()->setWordWrap( false );
-    if ( hasAuthorization( "LaunchApp" ) ) {
-        mLabelUrl->nativeWidget()->setCursor( Qt::PointingHandCursor );
-        mLabelUrl->nativeWidget()->setToolTip( i18n( "Visit the comic website" ) );
-    }
-    mLabelUrl->nativeWidget()->setSizePolicy( QSizePolicy( QSizePolicy::Preferred, \
                QSizePolicy::Fixed ) );
-    mLabelUrl->hide();
-
-    mLeftArrow = new ArrowWidget( this );
-    mLeftArrow->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, \
                QSizePolicy::Expanding ) );
-    mLeftArrow->setCursor( Qt::PointingHandCursor );
-    mLeftArrow->hide();
-    mLeftArrow->setGeometry( QRectF( QPointF( 0, 0 ), mLeftArrow->preferredSize() ) \
                );//to intially have a size -- needed in updateSize
-    connect( mLeftArrow, SIGNAL( clicked() ), this, SLOT( slotPreviousDay() ) );
-
-    mRightArrow = new ArrowWidget( this );
-    mRightArrow->setDirection( Plasma::Right );
-    mRightArrow->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, \
                QSizePolicy::Expanding ) );
-    mRightArrow->setCursor( Qt::PointingHandCursor );
-    mRightArrow->hide();
-    mRightArrow->setGeometry( QRectF( QPointF( 0, 0 ), mRightArrow->preferredSize() \
                ) );//to intially have a size -- needed in updateSize
-    connect( mRightArrow, SIGNAL( clicked() ), this, SLOT( slotNextDay() ) );
-
-#ifdef HAVE_NEPOMUK
-    //for manually saving the comics
-    Nepomuk::ResourceManager::instance()->init();
-#endif
+    graphicsWidget();
 }
 
 void ComicApplet::createLayout()
@@ -241,7 +193,7 @@
         newLayout->addItem( mRightArrow );
     }
 
-    setLayout( newLayout );
+    mMainWidget->setLayout( newLayout );
 }
 
 void ComicApplet::init()
@@ -300,8 +252,6 @@
     mActions.append( mActionStorePosition );
     connect( mActionStorePosition, SIGNAL( triggered( bool ) ), this, SLOT( \
slotStorePosition() ) );  
-    connect( this, SIGNAL( geometryChanged() ), this, SLOT( slotSizeChanged() ) );
-
     updateTabBar();
     changeComic( true );
 }
@@ -311,6 +261,72 @@
     delete mFullViewWidget;
 }
 
+QGraphicsWidget *ComicApplet::graphicsWidget()
+{
+    if ( !mMainWidget ) {
+        mMainWidget = new QGraphicsWidget( this );
+        mMainWidget->setMinimumSize( 150, 60 );
+        mMainWidget->setAcceptHoverEvents( true );
+        mMainWidget->installEventFilter( this );
+
+        mTabBar = new Plasma::TabBar( mMainWidget );
+        mTabBar->hide();
+        connect( mTabBar, SIGNAL( currentChanged( int ) ), this, SLOT( \
slotTabChanged( int ) ) ); +
+        mLabelTop = new Plasma::Label( mMainWidget );
+        mLabelTop->setMinimumWidth( 0 );
+        mLabelTop->nativeWidget()->setWordWrap( false );
+        mLabelTop->nativeWidget()->setSizePolicy( QSizePolicy( \
QSizePolicy::Preferred, QSizePolicy::Fixed ) ); +        mLabelTop->setAlignment( \
Qt::AlignCenter ); +        mLabelTop->hide();
+
+        mImageWidget = new ImageWidget( mMainWidget );
+        mImageWidget->setZValue( 0 );
+        mImageWidget->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, \
QSizePolicy::Expanding ) ); +        mImageWidget->hide();
+
+        mLabelId = new Plasma::Label( mMainWidget );
+        mLabelId->setMinimumWidth( 0 );
+        mLabelId->nativeWidget()->setWordWrap( false );
+        mLabelId->nativeWidget()->setCursor( Qt::PointingHandCursor );
+        mLabelId->nativeWidget()->setToolTip( i18n( "Jump to Strip ..." ) );
+        mLabelId->nativeWidget()->setSizePolicy( QSizePolicy( \
QSizePolicy::Preferred, QSizePolicy::Fixed ) ); +        mLabelId->hide();
+
+        mLabelUrl = new Plasma::Label( mMainWidget );
+        mLabelUrl->setMinimumWidth( 0 );
+        mLabelUrl->nativeWidget()->setWordWrap( false );
+        if ( hasAuthorization( "LaunchApp" ) ) {
+            mLabelUrl->nativeWidget()->setCursor( Qt::PointingHandCursor );
+            mLabelUrl->nativeWidget()->setToolTip( i18n( "Visit the comic website" ) \
); +        }
+        mLabelUrl->nativeWidget()->setSizePolicy( QSizePolicy( \
QSizePolicy::Preferred, QSizePolicy::Fixed ) ); +        mLabelUrl->hide();
+
+        mLeftArrow = new ArrowWidget( mMainWidget );
+        mLeftArrow->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, \
QSizePolicy::Expanding ) ); +        mLeftArrow->setCursor( Qt::PointingHandCursor );
+        mLeftArrow->hide();
+        mLeftArrow->setGeometry( QRectF( QPointF( 0, 0 ), \
mLeftArrow->preferredSize() ) );//to intially have a size -- needed in updateSize +   \
connect( mLeftArrow, SIGNAL( clicked() ), this, SLOT( slotPreviousDay() ) ); +
+        mRightArrow = new ArrowWidget( mMainWidget );
+        mRightArrow->setDirection( Plasma::Right );
+        mRightArrow->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, \
QSizePolicy::Expanding ) ); +        mRightArrow->setCursor( Qt::PointingHandCursor \
); +        mRightArrow->hide();
+        mRightArrow->setGeometry( QRectF( QPointF( 0, 0 ), \
mRightArrow->preferredSize() ) );//to intially have a size -- needed in updateSize +  \
connect( mRightArrow, SIGNAL( clicked() ), this, SLOT( slotNextDay() ) ); +
+    #ifdef HAVE_NEPOMUK
+//         for manually saving the comics
+        Nepomuk::ResourceManager::instance()->init();
+    #endif
+    }
+
+    return mMainWidget;
+}
+
 void ComicApplet::dataUpdated( const QString&, const Plasma::DataEngine::Data &data \
)  {
     setBusy( false );
@@ -521,7 +537,7 @@
     mScaleComic = cg.readEntry( "scaleToContent_" + mComicIdentifier, false );
     mMaxStripNum[ mComicIdentifier ] = cg.readEntry( "maxStripNum_" + \
                mComicIdentifier, 0 );
     mStoredIdentifierSuffix = cg.readEntry( "storedPosition_" + mComicIdentifier, "" \
                );
-    mMaxSize = cg.readEntry( "maxSize", geometry().size() );
+    mMaxSize = cg.readEntry( "maxSize", mMainWidget->geometry().size() );
     mLastSize = mMaxSize;
     mSwitchTabTime = cg.readEntry( "switchTabTime", 10 );// 10 seconds as default
     mShowTabBar = cg.readEntry( "showTabBar", true );
@@ -638,8 +654,8 @@
 void ComicApplet::slotSizeChanged()
 {
     // if the applet was resized manually by the user
-    if ( geometry().size() != mLastSize ) {
-        mMaxSize = geometry().size();
+    if ( mMainWidget->geometry().size() != mLastSize ) {
+        mMaxSize = mMainWidget->geometry().size();
 
         KConfigGroup cg = config();
         cg.writeEntry( "maxSize", mMaxSize );
@@ -657,64 +673,6 @@
     KRun::runUrl( mShopUrl, "text/html", 0 );
 }
 
-void ComicApplet::mousePressEvent( QGraphicsSceneMouseEvent *event )
-{
-    slotStartTimer();
-    if ( event->button() == Qt::LeftButton ) {
-        if ( mLabelUrl->isUnderMouse() ) {
-            if ( hasAuthorization( "LaunchApp" ) ) {
-                // link clicked
-                KRun::runUrl( mWebsiteUrl, "text/html", 0 );
-            }
-        } else if ( mLabelId->isUnderMouse() ) {
-            // identifierSuffix clicked clicked
-            slotGoJump();
-        } else if ( mImageWidget->isUnderMouse() && ( geometry().size() != mLastSize \
                ) ) {
-            // only update the size by clicking on the image-rect if the user manual \
                resized the applet
-            updateSize();
-        }
-    } else if ( ( event->button() == Qt::MidButton ) && mMiddleClick ) { // handle \
                full view
-        fullView();
-    }
-
-    event->ignore();
-}
-
-void ComicApplet::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
-{
-    if ( mFullViewWidget ) {
-        mFullViewWidget->hide();
-    }
-
-    Applet::mouseReleaseEvent( event );
-}
-
-void ComicApplet::wheelEvent( QGraphicsSceneWheelEvent *event ) {
-    slotStartTimer();
-    if ( mImageWidget->isUnderMouse() && ( event->modifiers() == Qt::ControlModifier \
                ) ) {
-        const QPointF eventPos = event->pos();
-        const int numDegrees = event->delta() / 8;
-        const int numSteps = numDegrees / 15;
-
-        int index = mTabBar->currentIndex();
-        int count = mTabBar->count();
-        int newIndex = 0;
-
-        if ( numSteps % count != 0 ) {
-            if ( numSteps < 0 ) {
-                newIndex = ( index - numSteps ) % count;
-            } else if ( numSteps > 0 ) {
-                newIndex = index - ( numSteps % count );
-                newIndex = newIndex < 0 ? newIndex + count : newIndex;
-            }
-            mTabBar->setCurrentIndex( newIndex );
-        }
-        event->accept();
-    }
-    Applet::wheelEvent( event );
-}
-
-
 void ComicApplet::updateSize()
 {
     //HACK to work around shortcomings in the layout code FIXME later
@@ -730,7 +688,7 @@
     int bottomArea = ( mShowComicUrl && !mLabelUrl->text().isEmpty() ) ? \
                mLabelUrl->nativeWidget()->height() : 0;
     bottomArea = ( mShowComicIdentifier && !mLabelId->text().isEmpty() ) ? \
mLabelId->nativeWidget()->height() : bottomArea;  
-    QSizeF margins = geometry().size() - contentsRect().size();
+    QSizeF margins = mMainWidget->geometry().size() - \
mMainWidget->contentsRect().size();  QSizeF availableSize = mMaxSize - margins;
     availableSize.setHeight( availableSize.height() - topArea - bottomArea );
     availableSize.setWidth( availableSize.width() - leftArea - rightArea );
@@ -741,25 +699,30 @@
     mLastSize.setWidth( mLastSize.width() + leftArea + rightArea );
 
     createLayout();
-    resize( mLastSize );
+    mMainWidget->resize( mLastSize );
     emit sizeHintChanged(Qt::PreferredSize);
     emit appletTransformedItself();
 }
 
+QList<QAction*> ComicApplet::contextualActions()
+{
+    return mActions;
+}
+
 QSizeF ComicApplet::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
 {
     if (which != Qt::PreferredSize) {
         return Applet::sizeHint(which, constraint);
     } else {
-        return mImage.size();
+        QSize imageSize = mImage.size();
+        if (imageSize.width() * imageSize.width() > 0) {
+            return imageSize;
+        } else {
+            return Applet::sizeHint(which, constraint);
+        }
     }
 }
 
-QList<QAction*> ComicApplet::contextualActions()
-{
-    return mActions;
-}
-
 void ComicApplet::updateComic( const QString &identifierSuffix )
 {
     //always stop the timer when changing the comic, as not all clicks are caught by \
mousePressEvent here @@ -873,33 +836,91 @@
     slotStartTimer();
 }
 
-void ComicApplet::constraintsEvent( Plasma::Constraints constraints )
+bool ComicApplet::eventFilter( QObject *receiver, QEvent *event )
 {
-    if ( constraints && Plasma::SizeConstraint && mFrame ) {
-        QPointF buttons( ( size().width() - mFrame->size().width() ) / 2,
-                         contentsRect().bottom() - mFrame->size().height() - 5 );
-        mFrame->setPos( buttons );
+    if ( dynamic_cast<QGraphicsWidget *>(receiver) != mMainWidget ) {
+        return false;
     }
-}
 
-void ComicApplet::hoverEnterEvent( QGraphicsSceneHoverEvent *event )
-{
-    if ( !configurationRequired() && mArrowsOnHover && mFrameAnim ) {
-        mFrameAnim->setDirection( QAbstractAnimation::Forward );
-        mFrameAnim->start();
-    }
+    switch (event->type()) {
+        case QEvent::GraphicsSceneHoverLeave:
+            if ( mArrowsOnHover && mFrameAnim ) {
+                mFrameAnim->setDirection( QAbstractAnimation::Backward );
+                mFrameAnim->start();
+            }
 
-    Applet::hoverEnterEvent( event );
-}
+            break;
+        case QEvent::GraphicsSceneHoverEnter:
+            if ( !configurationRequired() && mArrowsOnHover && mFrameAnim ) {
+                mFrameAnim->setDirection( QAbstractAnimation::Forward );
+                mFrameAnim->start();
+            }
 
-void ComicApplet::hoverLeaveEvent( QGraphicsSceneHoverEvent *event )
-{
-    if ( mArrowsOnHover && mFrameAnim ) {
-        mFrameAnim->setDirection( QAbstractAnimation::Backward );
-        mFrameAnim->start();
+            break;
+        case QEvent::GraphicsSceneWheel:
+            {
+                slotStartTimer();
+                QGraphicsSceneWheelEvent *e = static_cast<QGraphicsSceneWheelEvent \
*>( event ); +                if ( mImageWidget->isUnderMouse() && ( e->modifiers() \
== Qt::ControlModifier ) ) { +                    const QPointF eventPos = e->pos();
+                    const int numDegrees = e->delta() / 8;
+                    const int numSteps = numDegrees / 15;
+
+                    int index = mTabBar->currentIndex();
+                    int count = mTabBar->count();
+                    int newIndex = 0;
+
+                    if ( numSteps % count != 0 ) {
+                        if ( numSteps < 0 ) {
+                            newIndex = ( index - numSteps ) % count;
+                        } else if ( numSteps > 0 ) {
+                            newIndex = index - ( numSteps % count );
+                            newIndex = newIndex < 0 ? newIndex + count : newIndex;
+                        }
+                        mTabBar->setCurrentIndex( newIndex );
+                    }
+                    e->accept();
+                }
+            }
+            break;
+        case QEvent::GraphicsSceneMousePress:
+            {
+                slotStartTimer();
+                QGraphicsSceneMouseEvent *e = static_cast<QGraphicsSceneMouseEvent \
*>( event ); +                if ( e->button() == Qt::LeftButton ) {
+                    if ( mLabelUrl->isUnderMouse() ) {
+                        if ( hasAuthorization( "LaunchApp" ) ) {
+                            // link clicked
+                            KRun::runUrl( mWebsiteUrl, "text/html", 0 );
+                        }
+                    } else if ( mLabelId->isUnderMouse() ) {
+                        // identifierSuffix clicked clicked
+                        slotGoJump();
+                    } else if ( mImageWidget->isUnderMouse() && ( \
mMainWidget->geometry().size() != mLastSize ) ) { +                        // only \
update the size by clicking on the image-rect if the user manual resized the applet + \
updateSize(); +                    }
+                } else if ( ( e->button() == Qt::MidButton ) && mMiddleClick ) { // \
handle full view +                    fullView();
+                }
+
+                e->ignore();
+            }
+            break;
+        case QEvent::GraphicsSceneResize:
+            if ( mFrame ) {
+                QPointF buttons( ( mMainWidget->size().width() - \
mFrame->size().width() ) / 2, +                                \
mMainWidget->contentsRect().bottom() - mFrame->size().height() - 5 ); +               \
mFrame->setPos( buttons ); +            }
+            slotSizeChanged();
+
+            break;
+        default:
+            break;
     }
 
-    Applet::hoverLeaveEvent( event );
+    return false;
 }
 
 void ComicApplet::slotScaleToContent()
@@ -917,7 +938,7 @@
 {
     if ( mArrowsOnHover ) {
         if ( !mFrame ) {
-            mFrame = new Plasma::Frame( this );
+            mFrame = new Plasma::Frame( mMainWidget );
             mFrame->setZValue( 10 );
             QGraphicsLinearLayout *l = new QGraphicsLinearLayout();
             mPrevButton = new Plasma::PushButton( mFrame );
@@ -972,7 +993,14 @@
 
     if ( !mFullViewWidget->isVisible() ) {
         mFullViewWidget->setImage( mImage );
-        mFullViewWidget->adaptPosition( mapToScene( 0, 0 ).toPoint() );
+        foreach (QGraphicsView *view, scene()->views()) {
+            if (view->sceneRect().contains(mMainWidget->pos())) {
+                QPoint viewPos = view->pos();
+                QPoint relPos = mapToScene( 0, 0 ).toPoint();
+                mFullViewWidget->adaptPosition( relPos + view->mapToGlobal(viewPos) \
); +                break;
+            }
+        }
         mFullViewWidget->show();
     }
 }



_______________________________________________
Plasma-devel mailing list
Plasma-devel@kde.org
https://mail.kde.org/mailman/listinfo/plasma-devel


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

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