瀏覽代碼

Merge branch 'ctkFlowLayout-replaceLayout'

* ctkFlowLayout-replaceLayout:
  Add ctkFlowLayout::replaceLayout
Julien Finet 13 年之前
父節點
當前提交
4b042a05bf
共有 2 個文件被更改,包括 37 次插入0 次删除
  1. 27 0
      Libs/Widgets/ctkFlowLayout.cpp
  2. 10 0
      Libs/Widgets/ctkFlowLayout.h

+ 27 - 0
Libs/Widgets/ctkFlowLayout.cpp

@@ -463,3 +463,30 @@ QLayoutItem *ctkFlowLayout::takeAt(int index)
   this->invalidate();
   return item;
 }
+
+// --------------------------------------------------------------------------
+ctkFlowLayout* ctkFlowLayout::replaceLayout(QWidget* widget)
+{
+  QLayout* oldLayout = widget->layout();
+
+  ctkFlowLayout* flowLayout = new ctkFlowLayout;
+  bool isVerticalLayout = qobject_cast<QVBoxLayout*>(oldLayout) != 0;
+  flowLayout->setPreferredExpandingDirections(
+    isVerticalLayout ? Qt::Vertical : Qt::Horizontal);
+  flowLayout->setAlignItems(false);
+  int margins[4];
+  oldLayout->getContentsMargins(&margins[0],&margins[1],&margins[2],&margins[3]);
+  QLayoutItem* item = 0;
+  while((item = oldLayout->takeAt(0)))
+    {
+    if (item->widget())
+      {
+      flowLayout->addWidget(item->widget());
+      }
+    }
+  // setLayout() will take care or reparenting layouts and widgets
+  delete oldLayout;
+  flowLayout->setContentsMargins(0,0,0,0);
+  widget->setLayout(flowLayout);
+  return flowLayout;
+}

+ 10 - 0
Libs/Widgets/ctkFlowLayout.h

@@ -78,6 +78,16 @@ public:
   bool alignItems()const;
   void setAlignItems(bool);
 
+  /// Replace the existing BoxLayout of the widget with a
+  /// flow layout.
+  /// When using the Designer, it is not possible to use a flow layout.
+  /// Instead, you can use a H/VBoxLayout and replace it programatically
+  /// in the setupUi() function with a flow layout. replaceLayout() makes
+  /// the operation easier.
+  /// \todo replaceLayout should take an existing layout instead of a widget,
+  /// indeed, a layout can have another layout as a parent, not only a widget.
+  static ctkFlowLayout* replaceLayout(QWidget* widget);
+
   /// Reimplemented for internal reasons
   virtual void addItem(QLayoutItem *item);
   virtual Qt::Orientations expandingDirections() const;