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

List:       kde-commits
Subject:    playground/base/nepomuk-kde/annotation/lib/gui
From:       Sebastian Trueg <sebastian () trueg ! de>
Date:       2010-05-07 19:30:32
Message-ID: 20100507193032.A06D6AC8B0 () svn ! kde ! org
[Download RAW message or body]

SVN commit 1124103 by trueg:

* Added search menu via the new Nepomuk::SearchMenu class.


 M  +73 -65    annotationmenu.cpp  
 M  +4 -2      annotationmenu.h  


--- trunk/playground/base/nepomuk-kde/annotation/lib/gui/annotationmenu.cpp \
#1124102:1124103 @@ -20,6 +20,7 @@
 */
 
 #include "annotationmenu.h"
+#include "searchmenu.h"
 #include "newresourcedialog.h"
 #include "pimo.h"
 #include "nuao.h"
@@ -37,8 +38,8 @@
 #include <KMenu>
 #include <KLocale>
 #include <KIcon>
-#include <KPixmapSequenceOverlayPainter>
 #include <KActionMenu>
+#include <KLineEdit>
 #include <KDebug>
 
 #include <Nepomuk/KRatingWidget>
@@ -109,6 +110,7 @@
 Q_DECLARE_METATYPE( Nepomuk::Types::Class )
 Q_DECLARE_METATYPE( Nepomuk::Tag )
 Q_DECLARE_METATYPE( Nepomuk::Annotation* )
+Q_DECLARE_METATYPE( KLineEdit* )
 
 static const int s_maxActionsPerSection = 5;
 
@@ -118,6 +120,7 @@
     Private()
         : m_configFlags( AnnotationMenu::ShowAll ),
           m_annotationTitleCreated( false ),
+          m_searchMenu( 0 ),
           m_annotationModel( 0 ),
           m_menuCreated( false ) {
     }
@@ -126,14 +129,14 @@
 
     QList<TypeElement> resourceTypes() const;
 
+    Query::Term createResourceTypeTerm() const;
+
     QList<QAction*> newResourceActions( KMenu* menu );
-    QAction* createBusyAction() const;
     QAction* createRelateToAction( const QUrl& resource ) const;
     QAction* createContextAction( const QUrl& resource ) const;
     QAction* createAnnotationAction( Annotation* annotation ) const;
     QAction* createTagAction( const Nepomuk::Tag& tag ) const;
     QAction* createShowAllTagsAction() const;
-    void setElidedActionText( QAction* action, const QString& text, const QString& \
typeLabel = QString() ) const;  
     void addNewResourceActions( KMenu* menu );
     void addRatingAction( KMenu* menu );
@@ -141,6 +144,7 @@
     void addContextActions( KMenu* menu );
     void addAnnotationActions( KMenu* menu );
     void addTagActions( KMenu* menu );
+    void addSearchAction();
 
     void _k_menuAboutToShow();
     void _k_ratingChanged(unsigned int);
@@ -148,6 +152,7 @@
     void _k_contextActionTriggered();
     void _k_newResourceActionTriggered();
     void _k_annotationActionTriggered();
+    void _k_searchResultTriggered( const Nepomuk::Query::Result& );
     void _k_tagActionToggled(bool);
     void _k_nextRelateToResourceReady( Soprano::Util::AsyncQuery* query );
     void _k_relateToResourceQueryFinished( Soprano::Util::AsyncQuery* );
@@ -173,6 +178,7 @@
 
     int m_tagActionCount;
 
+    SearchMenu* m_searchMenu;
     ResourceAnnotationModel* m_annotationModel;
 
     // true if actions have been created already.
@@ -215,6 +221,21 @@
 }
 
 
+Nepomuk::Query::Term Nepomuk::AnnotationMenu::Private::createResourceTypeTerm() \
const +{
+    Query::OrTerm typeOr;
+    Q_FOREACH( const TypeElement& elem, resourceTypes() ) {
+        const Types::Class& type = elem.first;
+        const Query::Term& term = elem.second;
+        if ( !term.isValid() )
+            typeOr.addSubTerm( Query::ResourceTypeTerm( type ) );
+        else
+            typeOr.addSubTerm( term );
+    }
+    return typeOr;
+}
+
+
 QList<QAction*> Nepomuk::AnnotationMenu::Private::newResourceActions( KMenu* menu )
 {
     kDebug();
@@ -232,27 +253,12 @@
 }
 
 
-QAction* Nepomuk::AnnotationMenu::Private::createBusyAction() const
-{
-    kDebug();
-    QWidget* w = new QWidget();
-    w->setMinimumSize( 22, 22 );
-    w->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Minimum );
-    KPixmapSequenceOverlayPainter* busyPainter = new KPixmapSequenceOverlayPainter( \
                w );
-    busyPainter->setWidget( w );
-    busyPainter->start();
-	QWidgetAction* busyAction = new QWidgetAction( q );
-    busyAction->setDefaultWidget( w );
-    return busyAction;
-}
-
-
 QAction* Nepomuk::AnnotationMenu::Private::createRelateToAction( const QUrl& \
resource ) const  {
     kDebug() << resource;
     Nepomuk::Resource res( resource );
     QAction* action = new QAction( q );
-    setElidedActionText( action, res.genericLabel(), Types::Class( \
res.resourceType() ).label() ); +    q->setElidedActionText( action, \
res.genericLabel(), Types::Class( res.resourceType() ).label() );  action->setData( \
                QVariant::fromValue( res ) );
     q->connect( action, SIGNAL( triggered() ), SLOT( _k_relateToActionTriggered() ) \
);  return action;
@@ -264,7 +270,7 @@
     kDebug() << resource;
     Nepomuk::Resource res( resource );
     QAction* action = new QAction( q );
-    setElidedActionText( action, res.genericLabel(), Types::Class( \
res.resourceType() ).label() ); +    q->setElidedActionText( action, \
res.genericLabel(), Types::Class( res.resourceType() ).label() );  action->setData( \
                QVariant::fromValue( res ) );
     q->connect( action, SIGNAL( triggered() ), SLOT( _k_contextActionTriggered() ) \
);  return action;
@@ -275,7 +281,7 @@
 {
     kDebug();
     QAction* action = new QAction( q );
-    setElidedActionText( action, annotation->comment() );
+    q->setElidedActionText( action, annotation->comment() );
     action->setData( QVariant::fromValue( annotation ) );
     q->connect( action, SIGNAL( triggered() ), SLOT(_k_annotationActionTriggered()) \
);  return action;
@@ -297,7 +303,7 @@
     kDebug() << tag;
     QAction* action = new QAction( q );
     action->setCheckable(true);
-    setElidedActionText( action, tag.genericLabel() );
+    q->setElidedActionText( action, tag.genericLabel() );
     action->setData( QVariant::fromValue( tag ) );
     if ( allResourcesTaggedWith( m_resources, tag ) ) {
         action->setChecked( true );
@@ -317,24 +323,6 @@
 }
 
 
-void Nepomuk::AnnotationMenu::Private::setElidedActionText( QAction* action, const \
                QString& text, const QString& typeLabel ) const
-{
-    int maxWidth = q->fontMetrics().width( "xxxxxxxxxxxxxxxxxxxxxxxxxxxx" );
-    QString typeSuffix;
-    if ( !typeLabel.isEmpty() ) {
-        typeSuffix = QString::fromLatin1( " (%1)" ).arg( typeLabel );
-        maxWidth -= q->fontMetrics().width( typeSuffix );
-    }
-
-    // FIXME: use a more appropriate width value
-    QString elidedText = q->fontMetrics().elidedText( text, Qt::ElideRight, maxWidth \
                );
-    if ( elidedText != text ) {
-        action->setToolTip( text + typeSuffix );
-    }
-    action->setText( elidedText + typeSuffix );
-}
-
-
 namespace {
     /// return the rating if it is the same for all resources, otherwise return 0
     int mergeRating( const QList<Nepomuk::Resource>& resources ) {
@@ -385,7 +373,7 @@
 {
     kDebug();
     // add widget action showing a busy thingi and query the resources async
-    m_relateToBusyAction = createBusyAction();
+    m_relateToBusyAction = q->createBusyAction(q);
     menu->addAction( m_relateToBusyAction );
 
     // query: select the resources that are not already related to m_resource
@@ -394,16 +382,7 @@
     Query::AndTerm mainTerm;
 
     // restrict to one of the supported resource types
-    Query::OrTerm typeOr;
-    Q_FOREACH( const TypeElement& elem, resourceTypes() ) {
-        const Types::Class& type = elem.first;
-        const Query::Term& term = elem.second;
-        if ( !term.isValid() )
-            typeOr.addSubTerm( Query::ResourceTypeTerm( type ) );
-        else
-            typeOr.addSubTerm( term );
-    }
-    mainTerm.addSubTerm( typeOr );
+    mainTerm.addSubTerm( createResourceTypeTerm() );
 
     // make sure we ignore resources that are already related to all our resources
     Query::AndTerm ignoreRelated;
@@ -445,23 +424,14 @@
     kDebug();
 
     // add widget action showing a busy thingi and query the resources async
-    m_contextQueryBusyAction = createBusyAction();
+    m_contextQueryBusyAction = q->createBusyAction(q);
     menu->addAction( m_contextQueryBusyAction );
 
     // query resources of the supported types that have a high usage count
     Query::AndTerm mainTerm;
 
     // restrict to one of the supported resource types
-    Query::OrTerm typeOr;
-    Q_FOREACH( const TypeElement& elem, resourceTypes() ) {
-        const Types::Class& type = elem.first;
-        const Query::Term& term = elem.second;
-        if ( !term.isValid() )
-            typeOr.addSubTerm( Query::ResourceTypeTerm( type ) );
-        else
-            typeOr.addSubTerm( term );
-    }
-    mainTerm.addSubTerm( typeOr );
+    mainTerm.addSubTerm( createResourceTypeTerm() );
 
     // sort the resources by most often used
     Query::ComparisonTerm ct( Nepomuk::Vocabulary::NUAO::usageCount(), Query::Term() \
); @@ -494,7 +464,7 @@
     kDebug();
     if ( m_resources.count() == 1 ) {
         // add widget action showing a busy thingi and query the resources async
-        m_annotationsBusyAction = createBusyAction();
+        m_annotationsBusyAction = q->createBusyAction(q);
         menu->addAction( m_annotationsBusyAction );
 
         m_annotationTitleCreated = false;
@@ -519,7 +489,7 @@
     m_tagActionCount = 0;
 
     // add widget action showing a busy thingi and query the resources async
-    m_tagBusyAction = createBusyAction();
+    m_tagBusyAction = q->createBusyAction(q);
     menu->addAction( m_tagBusyAction );
 
     // query most used tags
@@ -555,6 +525,20 @@
 }
 
 
+void Nepomuk::AnnotationMenu::Private::addSearchAction()
+{
+    kDebug();
+    if ( !m_searchMenu ) {
+        m_searchMenu = new SearchMenu( q );
+        m_searchMenu->setSearchWhileYouTypeEnabled( true );
+        q->connect( m_searchMenu, SIGNAL( resultTriggered( Nepomuk::Query::Result ) \
), +                    SLOT( _k_searchResultTriggered( Nepomuk::Query::Result ) ) );
+    }
+    QAction* action = q->addMenu( m_searchMenu );
+    action->setText( i18nc( "@action:inmenu", "Search..." ) );
+}
+
+
 void Nepomuk::AnnotationMenu::Private::_k_menuAboutToShow()
 {
     if ( !m_menuCreated ) {
@@ -592,6 +576,15 @@
             addNewResourceActions( q );
         }
 
+        if ( m_configFlags & AnnotationMenu::ShowSearchAction ) {
+            q->addSeparator();
+            addSearchAction();
+            if ( m_resources.isEmpty() )
+                m_searchMenu->setCoreQueryTerm( createResourceTypeTerm() );
+            else
+                m_searchMenu->setCoreQueryTerm( Query::Term() );
+        }
+
         if ( m_configFlags & AnnotationMenu::ShowAnnotationActions &&
              !m_resources.isEmpty() ) {
             // a header will only be added in case that we actually find some \
possible annotations @@ -655,6 +648,21 @@
 }
 
 
+void Nepomuk::AnnotationMenu::Private::_k_searchResultTriggered( const \
Nepomuk::Query::Result& result ) +{
+    kDebug();
+    Nepomuk::Resource res = result.resource();
+    if ( !m_resources.isEmpty() ) {
+        for ( int i = 0; i < m_resources.count(); ++i )
+            m_resources[i].addProperty( Nepomuk::Vocabulary::PIMO::isRelated(), \
res.pimoThing() ); +        emit q->resourcesAnnotated();
+    }
+    else {
+        ContextServiceInterface().setCurrentContext( res );
+    }
+}
+
+
 void Nepomuk::AnnotationMenu::Private::_k_tagActionToggled( bool enabled )
 {
     kDebug();
@@ -786,7 +794,7 @@
 
 
 Nepomuk::AnnotationMenu::AnnotationMenu( QWidget* parent )
-    : KMenu( parent ),
+    : Menu( parent ),
       d( new Private() )
 {
     kDebug();
--- trunk/playground/base/nepomuk-kde/annotation/lib/gui/annotationmenu.h \
#1124102:1124103 @@ -22,7 +22,7 @@
 #ifndef _NEPOMUK_ANNOTATION_MENU_H_
 #define _NEPOMUK_ANNOTATION_MENU_H_
 
-#include <KMenu>
+#include "nepomukmenu.h"
 
 #include <Nepomuk/Query/Term>
 
@@ -48,7 +48,7 @@
      *
      * \author Sebastian Trueg <trueg@kde.org>
      */
-    class NEPOMUKANNOTATION_EXPORT AnnotationMenu : public KMenu
+    class NEPOMUKANNOTATION_EXPORT AnnotationMenu : public Menu
     {
         Q_OBJECT
 
@@ -70,6 +70,7 @@
             ShowRelatedResourcesActions = 0x4,
             ShowAnnotationActions = 0x8,
             ShowNewResourceActions = 0x10,
+            ShowSearchAction = 0x20,
             ShowAll = 0xFFFF
         };
         Q_DECLARE_FLAGS( ConfigurationFlags, ConfigurationFlag )
@@ -118,6 +119,7 @@
         Q_PRIVATE_SLOT( d, void _k_contextActionTriggered() )
         Q_PRIVATE_SLOT( d, void _k_newResourceActionTriggered() )
         Q_PRIVATE_SLOT( d, void _k_annotationActionTriggered() )
+        Q_PRIVATE_SLOT( d, void _k_searchResultTriggered( const \
Nepomuk::Query::Result& ) )  Q_PRIVATE_SLOT( d, void _k_tagActionToggled(bool) )
         Q_PRIVATE_SLOT( d, void _k_nextRelateToResourceReady( \
                Soprano::Util::AsyncQuery* ) )
         Q_PRIVATE_SLOT( d, void _k_relateToResourceQueryFinished( \
Soprano::Util::AsyncQuery* ) )


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

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