Forráskód Böngészése

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 éve
szülő
commit
f3ca584343
2 módosított fájl, 26 hozzáadás és 9 törlés
  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:
 public:
   ctkActionsWidgetPrivate(ctkActionsWidget& object);
   ctkActionsWidgetPrivate(ctkActionsWidget& object);
   void setupUI();
   void setupUI();
+  void setupHeaders();
   void updateItems(QList<QStandardItem*>& items, QAction* action);
   void updateItems(QList<QStandardItem*>& items, QAction* action);
 
 
   QStandardItemModel*    ActionsModel;
   QStandardItemModel*    ActionsModel;
@@ -63,10 +64,7 @@ void ctkActionsWidgetPrivate::setupUI()
   Q_Q(ctkActionsWidget);
   Q_Q(ctkActionsWidget);
 
 
   this->ActionsModel = new QStandardItemModel(q);
   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 = new ctkSortFilterActionsProxyModel(q);
   this->SortFilterActionsProxyModel->setSourceModel(this->ActionsModel);
   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
 void ctkActionsWidgetPrivate
 ::updateItems(QList<QStandardItem*>& items, QAction* action)
 ::updateItems(QList<QStandardItem*>& items, QAction* action)
 {
 {
@@ -144,11 +151,14 @@ void ctkActionsWidget::addAction(QAction* action, const QString& group)
     }
     }
 
 
   d->updateItems(actionItems, action);
   d->updateItems(actionItems, action);
+  
   bool expandGroupItem = (actionGroupItem->rowCount() == 0);
   bool expandGroupItem = (actionGroupItem->rowCount() == 0);
   actionGroupItem->appendRow(actionItems);
   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)
   if (expandGroupItem)
     {
     {
-    qDebug() << d->ActionsModel->indexFromItem(actionGroupItem);
     d->ActionsTreeView->expand(
     d->ActionsTreeView->expand(
       d->SortFilterActionsProxyModel->mapFromSource(d->ActionsModel->indexFromItem(actionGroupItem)));
       d->SortFilterActionsProxyModel->mapFromSource(d->ActionsModel->indexFromItem(actionGroupItem)));
     }
     }
@@ -170,6 +180,7 @@ void ctkActionsWidget::clear()
 {
 {
   Q_D(ctkActionsWidget);
   Q_D(ctkActionsWidget);
   d->ActionsModel->clear();
   d->ActionsModel->clear();
+  d->setupHeaders();
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -328,9 +339,9 @@ bool ctkSortFilterActionsProxyModel::areMenuActionsVisible()const
 bool ctkSortFilterActionsProxyModel::filterAcceptsRow(int source_row, const QModelIndex & source_parent) const
 bool ctkSortFilterActionsProxyModel::filterAcceptsRow(int source_row, const QModelIndex & source_parent) const
 {
 {
   Q_D(const ctkSortFilterActionsProxyModel);
   Q_D(const ctkSortFilterActionsProxyModel);
+  QModelIndex shortcutIndex = this->sourceModel()->index(source_row, ctkActionsWidget::ShortcutColumn, source_parent);
   QStandardItem* shortcutItem = qobject_cast<QStandardItemModel*>(
   QStandardItem* shortcutItem = qobject_cast<QStandardItemModel*>(
-    this->sourceModel())->itemFromIndex(
-      source_parent.child(source_row, ctkActionsWidget::ShortcutColumn));
+    this->sourceModel())->itemFromIndex(shortcutIndex);
   QAction* action = shortcutItem ?
   QAction* action = shortcutItem ?
     qobject_cast<QAction*>(shortcutItem->data().value<QObject*>()) : 0;
     qobject_cast<QAction*>(shortcutItem->data().value<QObject*>()) : 0;
   if (!action)
   if (!action)

+ 8 - 2
Libs/Widgets/ctkActionsWidget.h

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