浏览代码

Fix various bugs in ctkActionsWidget

Top level actions were never hidden (because the child index was always
invalid).
clearing the model was also clearing the contents of the column header
labels
Julien Finet 14 年之前
父节点
当前提交
f3ca584343
共有 2 个文件被更改,包括 26 次插入9 次删除
  1. 18 7
      Libs/Widgets/ctkActionsWidget.cpp
  2. 8 2
      Libs/Widgets/ctkActionsWidget.h

+ 18 - 7
Libs/Widgets/ctkActionsWidget.cpp

@@ -41,6 +41,7 @@ protected:
 public:
   ctkActionsWidgetPrivate(ctkActionsWidget& object);
   void setupUI();
+  void setupHeaders();
   void updateItems(QList<QStandardItem*>& items, QAction* action);
 
   QStandardItemModel*    ActionsModel;
@@ -63,10 +64,7 @@ void ctkActionsWidgetPrivate::setupUI()
   Q_Q(ctkActionsWidget);
 
   this->ActionsModel = new QStandardItemModel(q);
-  this->ActionsModel->setColumnCount(4);
-  QStringList headers;
-  headers << "Action" << "Shortcut" << "Context" << "Details";
-  this->ActionsModel->setHorizontalHeaderLabels(headers);
+  this->setupHeaders();
 
   this->SortFilterActionsProxyModel = new ctkSortFilterActionsProxyModel(q);
   this->SortFilterActionsProxyModel->setSourceModel(this->ActionsModel);
@@ -81,6 +79,15 @@ void ctkActionsWidgetPrivate::setupUI()
 }
 
 //-----------------------------------------------------------------------------
+void ctkActionsWidgetPrivate::setupHeaders()
+{
+  this->ActionsModel->setColumnCount(4);
+  QStringList headers;
+  headers << "Action" << "Shortcut" << "Context" << "Details";
+  this->ActionsModel->setHorizontalHeaderLabels(headers);
+}
+
+//-----------------------------------------------------------------------------
 void ctkActionsWidgetPrivate
 ::updateItems(QList<QStandardItem*>& items, QAction* action)
 {
@@ -144,11 +151,14 @@ void ctkActionsWidget::addAction(QAction* action, const QString& group)
     }
 
   d->updateItems(actionItems, action);
+  
   bool expandGroupItem = (actionGroupItem->rowCount() == 0);
   actionGroupItem->appendRow(actionItems);
+  // if the group didn't exist yet or was empty, then open/expand it
+  // automatcally to show its contents. If the group was not empty, then let
+  // it as is (maybe the user closed/collapsed it for a good reason...
   if (expandGroupItem)
     {
-    qDebug() << d->ActionsModel->indexFromItem(actionGroupItem);
     d->ActionsTreeView->expand(
       d->SortFilterActionsProxyModel->mapFromSource(d->ActionsModel->indexFromItem(actionGroupItem)));
     }
@@ -170,6 +180,7 @@ void ctkActionsWidget::clear()
 {
   Q_D(ctkActionsWidget);
   d->ActionsModel->clear();
+  d->setupHeaders();
 }
 
 //-----------------------------------------------------------------------------
@@ -328,9 +339,9 @@ bool ctkSortFilterActionsProxyModel::areMenuActionsVisible()const
 bool ctkSortFilterActionsProxyModel::filterAcceptsRow(int source_row, const QModelIndex & source_parent) const
 {
   Q_D(const ctkSortFilterActionsProxyModel);
+  QModelIndex shortcutIndex = this->sourceModel()->index(source_row, ctkActionsWidget::ShortcutColumn, source_parent);
   QStandardItem* shortcutItem = qobject_cast<QStandardItemModel*>(
-    this->sourceModel())->itemFromIndex(
-      source_parent.child(source_row, ctkActionsWidget::ShortcutColumn));
+    this->sourceModel())->itemFromIndex(shortcutIndex);
   QAction* action = shortcutItem ?
     qobject_cast<QAction*>(shortcutItem->data().value<QObject*>()) : 0;
   if (!action)

+ 8 - 2
Libs/Widgets/ctkActionsWidget.h

@@ -45,6 +45,7 @@ class QTreeView;
 /// ctkActionsWidget internally uses a QStandardItemModel where each item data
 /// (QStandardItem::data) contain a pointer to the QAction.
 /// QActions can optionally be ordered by group
+/// TODO: Add "hide empty group" property to hide empty groups
 class CTK_WIDGETS_EXPORT ctkActionsWidget : public QWidget
 {
   Q_OBJECT
@@ -69,15 +70,20 @@ public:
   /// Return a pointer on a group item (you probably have no use for it)
   QStandardItem* groupItem(const QString& category);
 
-  /// Show/hide QActions that have an empty shortcut
+  /// If true, shows QActions that have an empty shortcut, otherwise hide them.
+  /// True by default
   void setActionsWithNoShortcutVisible(bool show);
   bool areActionsWithNoShortcutVisible()const;
 
-  /// Show/hide QActions that have an empty shortcut
+  /// If true, shows QMenus, otherwise hide them.
+  /// True by default
   void setMenuActionsVisible(bool show);
   bool areMenuActionsVisible()const;
 
+  /// Return the unsorted/unfiltered model of all the actions
   QStandardItemModel* model()const;
+
+  /// return the view used to display the action model
   QTreeView* view()const;
 
 protected slots: