Browse Source

ENH: Allow clicking anywhere in field to toggle check boxes.

Clinton Stimpson 18 years ago
parent
commit
073b109508
2 changed files with 50 additions and 2 deletions
  1. 48 2
      Source/QtDialog/QCMakeCacheView.cxx
  2. 2 0
      Source/QtDialog/QCMakeCacheView.h

+ 48 - 2
Source/QtDialog/QCMakeCacheView.cxx

@@ -314,11 +314,14 @@ bool QCMakeCacheModel::setData (const QModelIndex& idx, const QVariant& value, i
   return false;
 }
 
-QModelIndex QCMakeCacheModel::buddy ( const QModelIndex& idx ) const
+QModelIndex QCMakeCacheModel::buddy(const QModelIndex& idx) const
 {
   if(idx.column() == 0)
     {
-    return this->index(idx.row(), 1);
+    if(this->Properties[idx.row()].Type != QCMakeCacheProperty::BOOL)
+      {
+      return this->index(idx.row(), 1);
+      }
     }
   return idx;
 }
@@ -387,6 +390,49 @@ QWidget* QCMakeCacheModelDelegate::createEditor(QWidget* p,
   return new QLineEdit(p);
 }
   
+bool QCMakeCacheModelDelegate::editorEvent(QEvent* event, QAbstractItemModel* model, 
+       const QStyleOptionViewItem& option, const QModelIndex& index)
+{
+  Qt::ItemFlags flags = model->flags(index);
+  if (!(flags & Qt::ItemIsUserCheckable) || !(option.state & QStyle::State_Enabled)
+      || !(flags & Qt::ItemIsEnabled))
+    {
+    return false;
+    }
+
+  QVariant value = index.data(Qt::CheckStateRole);
+  if (!value.isValid())
+    {
+    return false;
+    }
+
+  if ((event->type() == QEvent::MouseButtonRelease)
+      || (event->type() == QEvent::MouseButtonDblClick))
+    {
+    // eat the double click events inside the check rect
+    if (event->type() == QEvent::MouseButtonDblClick)
+      {
+      return true;
+      }
+    } 
+  else if (event->type() == QEvent::KeyPress)
+    {
+    if(static_cast<QKeyEvent*>(event)->key() != Qt::Key_Space &&
+       static_cast<QKeyEvent*>(event)->key() != Qt::Key_Select)
+      {
+      return false;
+      }
+    } 
+  else 
+    {
+    return false;
+    }
+
+  Qt::CheckState state = (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked
+                          ? Qt::Unchecked : Qt::Checked);
+  return model->setData(index, state, Qt::CheckStateRole);
+}
+  
 QCMakeCacheFileEditor::QCMakeCacheFileEditor(QWidget* p)
   : QLineEdit(p)
 {

+ 2 - 0
Source/QtDialog/QCMakeCacheView.h

@@ -105,6 +105,8 @@ public:
   /// create our own editors for cache properties
   QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, 
       const QModelIndex& index ) const;
+  bool editorEvent (QEvent* event, QAbstractItemModel* model, 
+       const QStyleOptionViewItem& option, const QModelIndex& index);
 };
 
 /// Editor widget for editing paths or file paths