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

List:       kde-commits
Subject:    playground/base/nepomuk-kde/annotation
From:       Sebastian Trueg <sebastian () trueg ! de>
Date:       2010-05-06 16:37:19
Message-ID: 20100506163719.96C92AC8AC () svn ! kde ! org
[Download RAW message or body]

SVN commit 1123694 by trueg:

Made the AnnotationMenu a subclass of KMenu which makes its usage more generic.

 M  +125 -84   lib/gui/annotationmenu.cpp  
 M  +13 -10    lib/gui/annotationmenu.h  
 M  +1 -2      test/annotationmenutester.cpp  


--- trunk/playground/base/nepomuk-kde/annotation/lib/gui/annotationmenu.cpp \
#1123693:1123694 @@ -60,6 +60,44 @@
 #include <Soprano/QueryResultIterator>
 #include <Soprano/Util/AsyncQuery>
 
+// layout with resoruce:
+//
+// ------------------
+// - resource label -
+// ------------------
+// Rating: *****
+// ------------------
+// Relate to:
+// res1
+// res2
+// res3
+// res4
+// res5
+// more...
+// ------------------
+// New Task...
+// New Event...
+// New Person...
+// ------------------
+//
+//
+// layout without resource:
+//
+// ------------------
+// New Task...
+// New Event...
+// New Person...
+// ------------------
+// Set current context:
+// res1
+// res2
+// res3
+// res4
+// res5
+// more...
+// ------------------
+//
+
 // TODO: most of the "related resources" and "set context" action methods are \
similar and could be merged  
 Q_DECLARE_METATYPE( Nepomuk::Types::Class )
@@ -72,7 +110,8 @@
     Private()
         : m_enableNewResourceActions( true ),
           m_annotationTitleCreated( false ),
-          m_annotationModel( 0 ) {
+          m_annotationModel( 0 ),
+          m_menuCreated( false ) {
     }
 
     typedef QPair<Nepomuk::Types::Class, Nepomuk::Query::Term> TypeElement;
@@ -91,6 +130,7 @@
     void addContextActions( KMenu* menu );
     void addAnnotationActions( KMenu* menu );
 
+    void _k_menuAboutToShow();
     void _k_ratingChanged(unsigned int);
     void _k_relateToActionTriggered();
     void _k_contextActionTriggered();
@@ -107,7 +147,7 @@
 
     QList<TypeElement> m_resourceTypes;
 
-    Resource m_resource;
+    QList<Resource> m_resources;
 
     QPointer<KMenu> m_menu;
     QPointer<QAction> m_relateToBusyAction;
@@ -117,6 +157,9 @@
 
     ResourceAnnotationModel* m_annotationModel;
 
+    // true if actions have been created already.
+    bool m_menuCreated;
+
     AnnotationMenu* q;
 };
 
@@ -221,12 +264,28 @@
 }
 
 
+namespace {
+    /// return the rating if it is the same for all resources, otherwise return 0
+    int mergeRating( const QList<Nepomuk::Resource>& resources ) {
+        int rating = 0;
+        Q_FOREACH( const Nepomuk::Resource& res, resources ) {
+            int r = res.rating();
+            if ( rating == 0 ||
+                 r == rating )
+                rating = r;
+            else
+                return 0;
+        }
+        return rating;
+    }
+}
+
 void Nepomuk::AnnotationMenu::Private::addRatingAction( KMenu* menu )
 {
     kDebug();
 	QWidgetAction* rateAction = new QWidgetAction( menu );
 	KRatingWidget* ratingWidget = new KRatingWidget();
-    ratingWidget->setRating( m_resource.rating() );
+    ratingWidget->setRating( mergeRating( m_resources ) );
 	rateAction->setDefaultWidget( ratingWidget );
 	connect( ratingWidget, SIGNAL(ratingChanged(unsigned int)),
              q, SLOT(_k_ratingChanged(unsigned int)) );
@@ -275,8 +334,13 @@
     }
     mainTerm.addSubTerm( typeOr );
 
-    // make sure we ignore resources that are already related
-    mainTerm.addSubTerm( Query::NegationTerm::negateTerm( Query::ComparisonTerm( \
Types::Property(), Query::ResourceTerm( m_resource ) ).inverted() ) ); +    // make \
sure we ignore resources that are already related to all our resources +    \
Query::AndTerm ignoreRelated; +    Q_FOREACH( const Resource& res, m_resources )
+        ignoreRelated.addSubTerm(
+            Query::ComparisonTerm( Types::Property(), Query::ResourceTerm( res ) \
).inverted() +            );
+    mainTerm.addSubTerm( Query::NegationTerm::negateTerm( ignoreRelated ) );
 
     // sort the resources by most often used
     Query::ComparisonTerm ct;
@@ -361,7 +425,7 @@
 void Nepomuk::AnnotationMenu::Private::addAnnotationActions( KMenu* menu )
 {
     kDebug();
-
+    if ( m_resources.count() == 1 ) {
     // add widget action showing a busy thingi and query the resources async
     m_annotationsBusyAction = createBusyAction();
     menu->addAction( m_annotationsBusyAction );
@@ -376,14 +440,51 @@
                     SLOT( _k_annotationModelFinished() ) );
     }
 
-    m_annotationModel->setResource( m_resource );
+        m_annotationModel->setResource( m_resources.first() );
 }
+}
 
 
+void Nepomuk::AnnotationMenu::Private::_k_menuAboutToShow()
+{
+    if ( !m_menuCreated ) {
+        m_menuCreated = true;
+
+        if ( !m_resources.isEmpty() ) {
+            if ( m_resources.count() == 1 )
+                q->addTitle( KIcon( "nepomuk" ), i18nc( "@title menu title", \
"Annotate %1", m_resources.first().genericLabel() ) ); +            else
+                q->addTitle( KIcon( "nepomuk" ), i18nc( "@title menu title", \
"Annotate resource" ) ); +            addRatingAction( q );
+
+            q->addTitle( i18nc( "@title:menu title above a set of resources that can \
be marked as related to the current resource.", +                                \
"Relate to:" ) ); +            addRelateToActions( q );
+        }
+        else {
+            q->addTitle( i18nc( "@title:menu title above a set of resources that can \
be set as the current working context.", +                                "Work in \
Context of:" ) ); +            addContextActions( q );
+        }
+
+        if ( m_enableNewResourceActions ) {
+            q->addSeparator();
+            addNewResourceActions( q );
+        }
+
+        if ( !m_resources.isEmpty() ) {
+            // a header will only be added in case that we actually find some \
possible annotations +            addAnnotationActions( q );
+        }
+    }
+}
+
+
 void Nepomuk::AnnotationMenu::Private::_k_ratingChanged( unsigned int rating )
 {
     kDebug();
-    m_resource.setRating( rating );
+    for ( int i = 0; i < m_resources.count(); ++i )
+        m_resources[i].setRating( rating );
 }
 
 
@@ -391,7 +492,8 @@
 {
     kDebug();
     Nepomuk::Resource res = qobject_cast<QAction*>( q->sender() \
                )->data().value<Nepomuk::Resource>();
-    m_resource.addProperty( Nepomuk::Vocabulary::PIMO::isRelated(), res.pimoThing() \
); +    for ( int i = 0; i < m_resources.count(); ++i )
+        m_resources[i].addProperty( Nepomuk::Vocabulary::PIMO::isRelated(), \
res.pimoThing() );  }
 
 
@@ -409,8 +511,9 @@
     Nepomuk::Types::Class type = qobject_cast<QAction*>( q->sender() \
                )->data().value<Nepomuk::Types::Class>();
     Nepomuk::Resource newRes = NewResourceDialog::createResource( type, m_menu );
     if ( newRes.isValid() ) {
-        if ( m_resource.isValid() ) {
-            m_resource.addProperty( Nepomuk::Vocabulary::PIMO::isRelated(), \
newRes.pimoThing() ); +        if ( !m_resources.isEmpty() ) {
+            for ( int i = 0; i < m_resources.count(); ++i )
+                m_resources[i].addProperty( Nepomuk::Vocabulary::PIMO::isRelated(), \
newRes.pimoThing() );  }
         else {
             ContextServiceInterface().setCurrentContext( newRes );
@@ -423,7 +526,7 @@
 {
     kDebug();
     Nepomuk::Annotation* annotation = qobject_cast<QAction*>( q->sender() \
                )->data().value<Nepomuk::Annotation*>();
-    annotation->create( m_resource );
+    annotation->create( m_resources.first() );
 }
 
 
@@ -485,6 +588,7 @@
                           m_annotationsBusyAction );
     }
 
+    if ( !annotation->exists( m_resources.first() ) )
     m_menu->insertAction( m_annotationsBusyAction, createAnnotationAction( \
annotation ) );  }
 
@@ -497,11 +601,12 @@
 
 
 
-Nepomuk::AnnotationMenu::AnnotationMenu( QObject* parent )
-    : QObject( parent ),
+Nepomuk::AnnotationMenu::AnnotationMenu( QWidget* parent )
+    : KMenu( parent ),
       d( new Private() )
 {
     d->q = this;
+    connect( this, SIGNAL( aboutToShow() ), this, SLOT( _k_menuAboutToShow() ) );
 }
 
 
@@ -523,81 +628,17 @@
 }
 
 
-// layout with resoruce:
-//
-// ------------------
-// - resource label -
-// ------------------
-// Rating: *****
-// ------------------
-// Relate to:
-// res1
-// res2
-// res3
-// res4
-// res5
-// more...
-// ------------------
-// New Task...
-// New Event...
-// New Person...
-// ------------------
-//
-//
-// layout without resource:
-//
-// ------------------
-// New Task...
-// New Event...
-// New Person...
-// ------------------
-// Set current context:
-// res1
-// res2
-// res3
-// res4
-// res5
-// more...
-// ------------------
-//
-
-void Nepomuk::AnnotationMenu::exec( const Resource& resource, const QPoint& pos )
+void Nepomuk::AnnotationMenu::setResource( const Resource& res )
 {
-    d->m_resource = resource;
-
-    KMenu menu;
-
-    if ( d->m_resource.isValid() ) {
-        menu.addTitle( KIcon( "nepomuk" ), d->m_resource.genericLabel() );
-        d->addRatingAction( &menu );
-
-        menu.addTitle( i18nc( "@title:menu title above a set of resources that can \
                be marked as related to the current resource.",
-                              "Relate to:" ) );
-        d->addRelateToActions( &menu );
+    setResources( QList<Resource>() << res );
     }
-    else {
-        menu.addTitle( i18nc( "@title:menu title above a set of resources that can \
                be set as the current working context.",
-                              "Work in Context of:" ) );
-        d->addContextActions( &menu );
-    }
 
-    if ( d->m_enableNewResourceActions ) {
-        menu.addSeparator();
-        d->addNewResourceActions( &menu );
-    }
 
-    if ( d->m_resource.isValid() ) {
-        // a header will only be added in case that we actually find some possible \
                annotations
-        d->addAnnotationActions( &menu );
-    }
-
-    menu.exec( pos );
-}
-
-
-void Nepomuk::AnnotationMenu::exec( const QPoint& pos )
+void Nepomuk::AnnotationMenu::setResources( const QList<Resource>& res )
 {
-    exec( Resource(), pos );
+    d->m_resources = res;
+    clear();
+    d->m_menuCreated = false;
 }
 
 #include "annotationmenu.moc"
--- trunk/playground/base/nepomuk-kde/annotation/lib/gui/annotationmenu.h \
#1123693:1123694 @@ -22,7 +22,7 @@
 #ifndef _NEPOMUK_ANNOTATION_MENU_H_
 #define _NEPOMUK_ANNOTATION_MENU_H_
 
-#include <QtCore/QObject>
+#include <KMenu>
 
 #include <Nepomuk/Query/Term>
 
@@ -48,7 +48,7 @@
      *
      * \author Sebastian Trueg <trueg@kde.org>
      */
-    class NEPOMUKANNOTATION_EXPORT AnnotationMenu : public QObject
+    class NEPOMUKANNOTATION_EXPORT AnnotationMenu : public KMenu
     {
         Q_OBJECT
 
@@ -56,12 +56,12 @@
         /**
          * Create a new menu.
          */
-        AnnotationMenu( QObject* parent = 0 );
+        AnnotationMenu( QWidget* parent = 0 );
 
         /**
          * Destructor
          */
-        ~AnnotationMenu();
+        virtual ~AnnotationMenu();
 
         /**
          * The supported resource types can be changed with this method. By default
@@ -75,24 +75,27 @@
          */
         void addSupportedType( const Types::Class& type, const Query::Term& \
queryTerm = Query::Term() );  
+        /**
+         * By default actions to create new resource are enabled.
+         */
         void enableNewResourceActions( bool enabled );
 
         /**
-         * Open the menu at global position \p pos for annotations
-         * of \p resource.
+         * The same as calling setResources() with a list containg \p res.
          */
-        void exec( const Resource& resource, const QPoint& pos );
+        void setResource( const Resource& res );
 
         /**
-         * Open the menu at global position \p pos without
-         * possibility to annotation any resource.
+         * Set the resources that should be annotated by the menu. If the list is \
empty +         * actions to change the context or create new resources are created.
          */
-        void exec( const QPoint& pos );
+        void setResources( const QList<Resource>& res );
 
     private:
         class Private;
         Private* const d;
 
+        Q_PRIVATE_SLOT( d, void _k_menuAboutToShow() )
         Q_PRIVATE_SLOT( d, void _k_ratingChanged(unsigned int) )
         Q_PRIVATE_SLOT( d, void _k_relateToActionTriggered() )
         Q_PRIVATE_SLOT( d, void _k_contextActionTriggered() )
--- trunk/playground/base/nepomuk-kde/annotation/test/annotationmenutester.cpp \
#1123693:1123694 @@ -55,8 +55,7 @@
 
     Nepomuk::AnnotationMenu m;
     if ( args->count() > 0 )
-        m.exec( Nepomuk::Resource( args->url( 0 ) ), QCursor::pos() );
-    else
+        m.setResource( Nepomuk::Resource( args->url( 0 ) ) );
         m.exec( QCursor::pos() );
 
     return 0;


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

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