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

List:       kde-commits
Subject:    playground/base/plasma/widgets/listview
From:       Christopher Blauvelt <cblauvelt () gmail ! com>
Date:       2007-10-06 5:53:28
Message-ID: 1191650008.939052.8831.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 721792 by cblauvelt:

Add the ability to capture keypresses and update the selection accordingly.  
Using the shift key to make expand a selection remains to be done.



 M  +62 -3     listview.cpp  
 M  +3 -1      listview.h  


--- trunk/playground/base/plasma/widgets/listview/listview.cpp #721791:721792
@@ -39,6 +39,7 @@
     {
 
         setItemDelegate(new QItemDelegate(0));
+        setFlag(QGraphicsItem::ItemIsFocusable);
     }
 
     ListView::~ListView() { }
@@ -72,6 +73,10 @@
         }
 
         setSelectionModel(new QItemSelectionModel(d->model));
+        if(d->selectionModel){
+            connect(d->selectionModel, SIGNAL(selectionChanged(const \
QItemSelection&, const QItemSelection&)), +                    this, \
SLOT(selectionChanged(const QItemSelection&, const QItemSelection&))); +        }
 
         update();
     }
@@ -96,10 +101,19 @@
         update();
     }
 
-    void ListView:: dataChanged(const QModelIndex &topLeft, const QModelIndex \
&bottomRight) +    void ListView::dataChanged(const QModelIndex &topLeft, const \
QModelIndex &bottomRight)  {
+        Q_UNUSED(topLeft)
+        Q_UNUSED(bottomRight)
         update();
     }
+    
+    void ListView::selectionChanged(const QItemSelection &topLeft, const \
QItemSelection &bottomRight) +    {
+        Q_UNUSED(topLeft)
+        Q_UNUSED(bottomRight)
+        update();
+    }
 
     QSizeF ListView::sizeHint() const
     {
@@ -150,12 +164,57 @@
         }
         //update current index
         d->selectionModel->setCurrentIndex(clickedIndex, \
                QItemSelectionModel::NoUpdate);
-
-        update();
     }
+    
+    void ListView::keyPressEvent(QKeyEvent *event)
+    {
+        event->accept();
+        kDebug() << "keypress event: " << event->key() << endl;
+        kDebug() << "keypress event: " << event->text() << endl;
+        
+        //get current selection, if no current selection select item (0,0)
+        QModelIndex currentIndex = d->selectionModel->currentIndex();
+        if(!currentIndex.isValid()){
+            currentIndex = d->model->index(0,0);
+            if(!currentIndex.isValid()){
+                kDebug() << "Error!  A default selection could not be created." << \
endl; +                return;
+            }
+        }
+            
+        //handle arrow keys
+        if(event->key() & (Qt::Key_Left | Qt::Key_Up | Qt::Key_Right | Qt::Key_Down) \
!= 0){ +            //find QModelIndex in direction of key
+            QModelIndex newIndex;
+            switch (event->key()) {
+                case Qt::Key_Left:
+                    newIndex = \
d->model->index(currentIndex.row(),currentIndex.column()-1); +                    \
break; +                case Qt::Key_Up:
+                    newIndex = \
d->model->index(currentIndex.row()-1,currentIndex.column()); +                    \
break; +                case Qt::Key_Right:
+                    newIndex = \
d->model->index(currentIndex.row(),currentIndex.column()+1); +                    \
break; +                case Qt::Key_Down:
+                    newIndex = \
d->model->index(currentIndex.row()+1,currentIndex.column()); +                    \
break; +            }
+            if(!newIndex.isValid()){
+                return;
+            }
+            d->selectionModel->setCurrentIndex(newIndex,QItemSelectionModel::ClearAndSelect);
 +        }
+        else if(event->text() != QString()) {
+            //find the first QModelIndex whose text starts with the pressed letter.
+            return;
+        }
+    }
 
     void ListView::paintWidget(QPainter *painter,  const QStyleOptionGraphicsItem \
*option,  QWidget *widget)  {
+         Q_UNUSED(option)
+         Q_UNUSED(widget)
          QPalette::ColorGroup cg;
          cg = QPalette::Normal;
 
--- trunk/playground/base/plasma/widgets/listview/listview.h #721791:721792
@@ -26,6 +26,7 @@
 #include <QAbstractItemModel>
 #include <QItemSelectionModel>
 #include <QGraphicsSceneMouseEvent>
+#include <QKeyEvent>
 
 #include <plasma/widgets/widget.h>
 #include <plasma/widgets/lineedit.h>
@@ -51,7 +52,6 @@
 
     void setModel(QAbstractItemModel *model);
     QAbstractItemModel * model();
-
     void setItemDelegate(QAbstractItemDelegate *delegate);
     QAbstractItemDelegate * delegate();
 
@@ -62,11 +62,13 @@
 
 public slots:
     void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
+    void selectionChanged(const QItemSelection &topLeft, const QItemSelection \
&bottomRight);  void toUpdate();
 
 protected:
     void mousePressEvent(QGraphicsSceneMouseEvent *event);
     void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
+    void keyPressEvent(QKeyEvent *event);
 
 private:
     class Private;


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

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