Selaa lähdekoodia

Add test to ctkCollapsibleGroupBox

Julien Finet 14 vuotta sitten
vanhempi
commit
d6bcacd767

+ 57 - 2
Libs/Widgets/Testing/Cpp/ctkCollapsibleGroupBoxTest1.cpp

@@ -20,6 +20,9 @@
 
 // Qt includes
 #include <QApplication>
+#include <QRadioButton>
+#include <QTimer>
+#include <QVBoxLayout>
 
 // CTK includes
 #include "ctkCollapsibleGroupBox.h"
@@ -33,9 +36,61 @@ int ctkCollapsibleGroupBoxTest1(int argc, char * argv [] )
 {
   QApplication app(argc, argv);
 
-  ctkCollapsibleGroupBox qctkObject;
+  QWidget topLevel;
+  ctkCollapsibleGroupBox* groupBox = new ctkCollapsibleGroupBox(QObject::tr("GroupBox"));
+  QRadioButton *radio1 = new QRadioButton(QObject::tr("&Radio button 1"));
+  QRadioButton *radio2 = new QRadioButton(QObject::tr("R&adio button 2"));
+  QRadioButton *radio3 = new QRadioButton(QObject::tr("Ra&dio button 3"));
 
+  radio1->setChecked(true);
 
-  return EXIT_SUCCESS;
+  QVBoxLayout *vbox = new QVBoxLayout;
+  vbox->addWidget(radio1);
+  vbox->addWidget(radio2);
+  vbox->addWidget(radio3);
+  vbox->addStretch(1);
+  groupBox->setLayout(vbox);
+  
+  QVBoxLayout* topLevelVBox = new QVBoxLayout;
+  topLevelVBox->addWidget(groupBox);
+  topLevel.setLayout(topLevelVBox);
+  
+  topLevel.show();
+  
+  if (groupBox->collapsed())
+    {
+    std::cerr<< "Wrong default collapse state." << std::endl;
+    return EXIT_FAILURE;
+    }
+  
+  groupBox->setCollapsed(true);
+  
+  if (groupBox->collapsed() != true)
+    {
+    std::cerr<< "ctkCollapsibleGroupBox::setCollapsed failed." << std::endl;
+    return EXIT_FAILURE;
+    }
+    
+  if (radio1->isVisible())
+    {
+    std::cerr << "ctkCollapsibleGroupBox::setChecked failed. "
+              << "Children are visible" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  groupBox->setChecked(true);
+  
+  if (groupBox->collapsed() != false)
+    {
+    std::cerr<< "ctkCollapsibleGroupBox::setChecked failed." << std::endl;
+    return EXIT_FAILURE;
+    }
+  
+  if (argc < 2 || QString(argv[1]) != "-I" )
+    {
+    QTimer::singleShot(200, &app, SLOT(quit()));
+    }
+  
+  return app.exec();
 }
 

+ 27 - 1
Libs/Widgets/ctkCollapsibleGroupBox.h

@@ -28,13 +28,26 @@
 #include "ctkWidgetsExport.h"
 
 /// A QGroupBox with an arrow indicator that shows/hides the groupbox contents
-/// when clicked.
+/// when clicked. It responds to the slot QGroupBox::setChecked(bool) or
+/// ctkCollapsibleGroupBox::setCollapsed(bool)
+/// When checked is true, the groupbox is expanded
+/// When checked is false, the groupbox is collapsed
 class CTK_WIDGETS_EXPORT ctkCollapsibleGroupBox : public QGroupBox
 {
   Q_OBJECT
 public:
   explicit ctkCollapsibleGroupBox(QWidget* parent = 0);
   virtual ~ctkCollapsibleGroupBox();
+  
+  /// Utility function to collapse the groupbox
+  /// Collapse(close) the group box if collapse is true, expand(open)
+  /// it otherwise.
+  /// \sa QGroupBox::setChecked(bool)
+  inline void setCollapsed(bool collapse);
+
+  /// Return the collapse state of the groupbox
+  /// true if the groupbox is collapsed (closed), false if it is expanded(open)
+  inline bool collapsed()const;
 
   /// Reimplemtented for internal reasons
   virtual int heightForWidth(int w) const;
@@ -45,6 +58,7 @@ public:
 
 protected slots:
   /// called when the arrow indicator is clicked
+  /// users can call it programatically by calling setChecked(bool)
   virtual void expand(bool expand);
 
 protected:
@@ -64,4 +78,16 @@ protected:
   int   MaxHeight;
 };
 
+//----------------------------------------------------------------------------
+bool ctkCollapsibleGroupBox::collapsed()const
+{
+  return !this->isChecked();
+}
+
+//----------------------------------------------------------------------------
+void ctkCollapsibleGroupBox::setCollapsed(bool collapse)
+{
+  this->setChecked(!collapse);
+}
+
 #endif