Bladeren bron

New widget ctkExpandButton, to hide/show some widgets.

Benjamin Long 13 jaren geleden
bovenliggende
commit
48527729e5

+ 13 - 1
Libs/Visualization/VTK/Widgets/Resources/UI/ctkVTKScalarsToColorsWidget.ui

@@ -6,7 +6,7 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>442</width>
+    <width>508</width>
     <height>144</height>
    </rect>
   </property>
@@ -175,6 +175,13 @@
        </property>
       </widget>
      </item>
+     <item>
+      <widget class="ctkExpandButton" name="ExpandButton">
+       <property name="contextMenuPolicy">
+        <enum>Qt::DefaultContextMenu</enum>
+       </property>
+      </widget>
+     </item>
     </layout>
    </item>
    <item row="1" column="0">
@@ -227,6 +234,11 @@
    <extends>QWidget</extends>
    <header>ctkDoubleRangeSlider.h</header>
   </customwidget>
+  <customwidget>
+   <class>ctkExpandButton</class>
+   <extends>QToolButton</extends>
+   <header location="global">ctkExpandButton.h</header>
+  </customwidget>
  </customwidgets>
  <resources/>
  <connections/>

+ 17 - 0
Libs/Visualization/VTK/Widgets/ctkVTKScalarsToColorsWidget.cpp

@@ -106,6 +106,10 @@ void ctkVTKScalarsToColorsWidgetPrivate::setupUi(QWidget* widget)
   this->XSpinBox->setVisible(false);
   this->OpacityLabel->setVisible(false);
   this->OpacitySpinBox->setVisible(false);
+  this->SharpnessLabel->setVisible(false);
+  this->SharpnessSpinBox->setVisible(false);
+  this->MidPointLabel->setVisible(false);
+  this->MidPointSpinBox->setVisible(false);
   QObject::connect(this->XRangeSlider, SIGNAL(valuesChanged(double,double)),
                    q, SLOT(setXRange(double,double)));
   QObject::connect(this->YRangeSlider, SIGNAL(valuesChanged(double,double)),
@@ -120,6 +124,9 @@ void ctkVTKScalarsToColorsWidgetPrivate::setupUi(QWidget* widget)
   q->qvtkConnect(this->View->chart()->GetAxis(1),vtkCommand::ModifiedEvent,
                     q, SLOT(onAxesModified()));
 
+  this->ExpandButton->setMirrorOnExpand(true);
+  QObject::connect(this->ExpandButton, SIGNAL(clicked(bool)),
+                   q, SLOT(onExpandButton(bool)));
 }
 
 // ----------------------------------------------------------------------------
@@ -548,3 +555,13 @@ void ctkVTKScalarsToColorsWidget::restorePalette()
   Q_D(ctkVTKScalarsToColorsWidget);
   d->XSpinBox->setPalette(this->palette());
 }
+
+// ----------------------------------------------------------------------------
+void ctkVTKScalarsToColorsWidget::onExpandButton(bool state)
+{
+  Q_D(ctkVTKScalarsToColorsWidget);
+  d->MidPointLabel->setVisible(state);;
+  d->MidPointSpinBox->setVisible(state);
+  d->SharpnessLabel->setVisible(state);
+  d->SharpnessSpinBox->setVisible(state);
+}

+ 1 - 0
Libs/Visualization/VTK/Widgets/ctkVTKScalarsToColorsWidget.h

@@ -84,6 +84,7 @@ protected slots:
   void onSharpnessChanged(double sharpness);
   void onAxesModified();
   void restorePalette();
+  void onExpandButton(bool state);
 protected:
   QScopedPointer<ctkVTKScalarsToColorsWidgetPrivate> d_ptr;
 

+ 3 - 0
Libs/Widgets/CMakeLists.txt

@@ -63,6 +63,8 @@ SET(KIT_SRCS
   ctkErrorLogStatusMessageHandler.h
   ctkErrorLogWidget.cpp
   ctkErrorLogWidget.h
+  ctkExpandButton.cpp
+  ctkExpandButton.h
   ctkFileDialog.cpp
   ctkFileDialog.h
   ctkFittedTextBrowser.cpp
@@ -201,6 +203,7 @@ SET(KIT_MOC_SRCS
   ctkDynamicSpacer.h
   ctkErrorLogWidget.h
   ctkErrorLogStatusMessageHandler.h
+  ctkExpandButton.h
   ctkFileDialog.h
   ctkFittedTextBrowser.h
   ctkFlowLayout.h

+ 3 - 0
Libs/Widgets/Plugins/CMakeLists.txt

@@ -38,6 +38,8 @@ SET(PLUGIN_SRCS
   ctkDoubleSliderPlugin.h
   ctkDynamicSpacerPlugin.cpp
   ctkDynamicSpacerPlugin.h
+  ctkExpandButtonPlugin.cpp
+  ctkExpandButtonPlugin.h
   #ctkExtensionFactory.cpp
   #ctkExtensionFactory.h
   ctkFittedTextBrowserPlugin.cpp
@@ -99,6 +101,7 @@ SET(PLUGIN_MOC_SRCS
   ctkDoubleRangeSliderPlugin.h
   ctkDoubleSliderPlugin.h
   ctkDynamicSpacerPlugin.h
+  ctkExpandButtonPlugin.h
   #ctkExtensionFactory.h
   ctkFittedTextBrowserPlugin.h
   ctkFontButtonPlugin.h

+ 73 - 0
Libs/Widgets/Plugins/ctkExpandButtonPlugin.cpp

@@ -0,0 +1,73 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0.txt
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+// QT includes
+#include <QApplication>
+#include <QStyle>
+
+// CTK includes
+#include "ctkExpandButtonPlugin.h"
+#include "ctkExpandButton.h"
+
+//-----------------------------------------------------------------------------
+ctkExpandButtonPlugin::ctkExpandButtonPlugin(QObject *_parent)
+        : QObject(_parent)
+{
+
+}
+
+//-----------------------------------------------------------------------------
+QWidget *ctkExpandButtonPlugin::createWidget(QWidget *_parent)
+{
+  ctkExpandButton* _widget = new ctkExpandButton(_parent);
+  return _widget;
+}
+
+//-----------------------------------------------------------------------------
+QString ctkExpandButtonPlugin::domXml() const
+{
+  return "<widget class=\"ctkExpandButton\" \
+          name=\"Button\">\n"
+          "</widget>\n";
+}
+
+// --------------------------------------------------------------------------
+QIcon ctkExpandButtonPlugin::icon() const
+{
+  return QIcon(QApplication::style()->standardIcon(
+                 QStyle::SP_ToolBarHorizontalExtensionButton));
+}
+
+//-----------------------------------------------------------------------------
+QString ctkExpandButtonPlugin::includeFile() const
+{
+  return "ctkExpandButton.h";
+}
+
+//-----------------------------------------------------------------------------
+bool ctkExpandButtonPlugin::isContainer() const
+{
+  return false;
+}
+
+//-----------------------------------------------------------------------------
+QString ctkExpandButtonPlugin::name() const
+{
+  return "ctkExpandButton";
+}

+ 44 - 0
Libs/Widgets/Plugins/ctkExpandButtonPlugin.h

@@ -0,0 +1,44 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0.txt
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+#ifndef __ctkExpandButtonPlugin_h
+#define __ctkExpandButtonPlugin_h
+
+// CTK includes
+#include "ctkWidgetsAbstractPlugin.h"
+
+class CTK_WIDGETS_PLUGINS_EXPORT ctkExpandButtonPlugin :
+    public QObject,
+    public ctkWidgetsAbstractPlugin
+{
+  Q_OBJECT
+
+public:
+  ctkExpandButtonPlugin(QObject *_parent = 0);
+
+  QWidget *createWidget(QWidget *parent);
+  QString  domXml() const;
+  QIcon    icon() const;
+  QString  includeFile() const;
+  bool     isContainer() const;
+  QString  name() const;
+};
+
+#endif

+ 2 - 0
Libs/Widgets/Testing/Cpp/CMakeLists.txt

@@ -31,6 +31,7 @@ SET(TEST_SOURCES
   ctkDynamicSpacerTest1.cpp
   ctkDynamicSpacerTest2.cpp
   ctkErrorLogWidgetTest1.cpp
+  ctkExpandButtonTest1.cpp
   ctkFileDialogTest1.cpp
   ctkFittedTextBrowserTest1.cpp
   ctkFlowLayoutTest1.cpp
@@ -160,6 +161,7 @@ SIMPLE_TEST( ctkDynamicSpacerTest2 )
 SIMPLE_TEST( ctkErrorLogWidgetTest1 )
 SIMPLE_TEST( ctkExampleUseOfWorkflowWidgetUsingDerivedSteps )
 SIMPLE_TEST( ctkExampleUseOfWorkflowWidgetUsingSignalsAndSlots )
+SIMPLE_TEST( ctkExpandButtonTest1 )
 SIMPLE_TEST( ctkFileDialogTest1 )
 SIMPLE_TEST( ctkFittedTextBrowserTest1 )
 SIMPLE_TEST( ctkFlowLayoutTest1 )

+ 86 - 0
Libs/Widgets/Testing/Cpp/ctkExpandButtonTest1.cpp

@@ -0,0 +1,86 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0.txt
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+// Qt includes
+#include <QApplication>
+#include <QHBoxLayout>
+#include <QPushButton>
+#include <QTimer>
+#include <QVBoxLayout>
+#include <QWidget>
+
+// CTK includes
+#include "ctkExpandButton.h"
+
+//-----------------------------------------------------------------------------
+int ctkExpandButtonTest1(int argc, char* argv[])
+{
+  QApplication app(argc, argv);
+
+  QWidget widget(0);
+  widget.show();
+  QBoxLayout* layout = new QBoxLayout(QBoxLayout::LeftToRight, &widget);
+
+  // Expand button horizontal
+  QHBoxLayout* hLayout = new QHBoxLayout();
+  layout->addLayout(hLayout);
+  ctkExpandButton* hbutton = new ctkExpandButton(&widget);
+
+  QPushButton* pushButton = new QPushButton("test");
+  hLayout->addWidget(pushButton);
+  pushButton->setVisible(false);
+  QPushButton* pushButton2 = new QPushButton("foo");
+  hLayout->addWidget(pushButton2);
+  pushButton2->setVisible(false);
+  hLayout->addWidget(hbutton);
+  hbutton->setMirrorOnExpand(true);
+
+  // Expand button vertical
+  QVBoxLayout* vLayout = new QVBoxLayout();
+  layout->addLayout(vLayout);
+  ctkExpandButton* vbutton = new ctkExpandButton(&widget);
+
+  QPushButton* pushButton3 = new QPushButton("test vertival");
+  vLayout->addWidget(pushButton3);
+  pushButton3->setVisible(false);
+  QPushButton* pushButton4 = new QPushButton("foo vertical");
+  vLayout->addWidget(pushButton4);
+  pushButton4->setVisible(false);
+  vLayout->addWidget(vbutton);
+  vbutton->setMirrorOnExpand(true);
+  vbutton->setOrientation(Qt::Vertical);
+
+
+  QObject::connect(hbutton, SIGNAL(clicked(bool)),
+                   pushButton, SLOT(setVisible(bool)));
+  QObject::connect(hbutton, SIGNAL(clicked(bool)),
+                   pushButton2, SLOT(setVisible(bool)));
+  QObject::connect(vbutton, SIGNAL(clicked(bool)),
+                   pushButton3, SLOT(setVisible(bool)));
+  QObject::connect(vbutton, SIGNAL(clicked(bool)),
+                   pushButton4, SLOT(setVisible(bool)));
+
+  if (argc < 2 || QString(argv[1]) != "-I" )
+    {
+    QTimer::singleShot(500, &app, SLOT(quit()));
+    }
+
+  return app.exec();
+}

+ 161 - 0
Libs/Widgets/ctkExpandButton.cpp

@@ -0,0 +1,161 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0.txt
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+// QT includes
+#include <QIcon>
+#include <QStyleOption>
+
+// CTK includes
+#include "ctkExpandButton.h"
+
+class ctkExpandButtonPrivate
+{
+  Q_DECLARE_PUBLIC(ctkExpandButton);
+protected:
+  ctkExpandButton* const q_ptr;
+public:
+  ctkExpandButtonPrivate(ctkExpandButton& object);
+  void                 init();
+  bool                 mirrorOnExpand;
+  QPixmap              defaultPixmap;
+  Qt::Orientation      orientation;
+  Qt::LayoutDirection  direction;
+};
+
+//-----------------------------------------------------------------------------
+ctkExpandButtonPrivate::ctkExpandButtonPrivate(ctkExpandButton &object)
+  : q_ptr(&object)
+{
+  this->mirrorOnExpand  = false;
+  this->orientation   = Qt::Horizontal;
+  this->direction     = Qt::LeftToRight;
+}
+
+//-----------------------------------------------------------------------------
+void ctkExpandButtonPrivate::init()
+{
+  Q_Q(ctkExpandButton);
+  q->setAutoRaise(true);
+  q->setOrientation(Qt::Horizontal);
+  q->setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Minimum);
+  q->setCheckable(true);
+}
+
+//-----------------------------------------------------------------------------
+// ctkExpandButton methods
+
+//-----------------------------------------------------------------------------
+ctkExpandButton::ctkExpandButton(QWidget *_parent)
+  :Superclass(_parent)
+  , d_ptr(new ctkExpandButtonPrivate(*this))
+{
+  Q_D(ctkExpandButton);
+  d->init();
+}
+
+//-----------------------------------------------------------------------------
+ctkExpandButton::~ctkExpandButton()
+{
+}
+
+//-----------------------------------------------------------------------------
+void ctkExpandButton::setMirrorOnExpand(bool newBehavior)
+{
+  Q_D(ctkExpandButton);
+  d->mirrorOnExpand = newBehavior;
+  this->updateIcon(d->direction);
+}
+
+//-----------------------------------------------------------------------------
+bool ctkExpandButton::mirrorOnExpand() const
+{
+  Q_D(const ctkExpandButton);
+  return d->mirrorOnExpand;
+}
+
+//-----------------------------------------------------------------------------
+QSize ctkExpandButton::sizeHint() const
+{
+  int ext = this->style()->pixelMetric(QStyle::PM_ToolBarExtensionExtent);
+  return QSize(ext, ext);
+}
+
+//-----------------------------------------------------------------------------
+void ctkExpandButton::setOrientation(Qt::Orientation newOrientation)
+{
+  Q_D(ctkExpandButton);
+  QStyleOption opt;
+  opt.init(this);
+  if(newOrientation == Qt::Horizontal)
+    {
+    d->defaultPixmap = this->style()->standardPixmap(
+        QStyle::SP_ToolBarHorizontalExtensionButton, &opt);
+    d->orientation = Qt::Horizontal;
+    }
+  else
+    {
+    d->defaultPixmap = this->style()->standardPixmap(
+        QStyle::SP_ToolBarVerticalExtensionButton, &opt);
+    d->orientation = Qt::Vertical;
+    }
+  this->updateIcon(d->direction);
+}
+
+//-----------------------------------------------------------------------------
+Qt::Orientation ctkExpandButton::orientation() const
+{
+  Q_D(const ctkExpandButton);
+  return d->orientation;
+}
+
+//-----------------------------------------------------------------------------
+void ctkExpandButton::updateIcon(Qt::LayoutDirection newDirection)
+{
+  Q_D(ctkExpandButton);
+  // If the orientation is vertical, UpToBottom is LeftToRight and
+  // BottomToUp is RightToLeft. Rotate 90' clockwise.
+  if(newDirection == Qt::LeftToRight)
+    {
+    this->setIcon(QIcon(d->defaultPixmap));
+    d->direction = Qt::LeftToRight;
+    }
+  else
+    {
+    QImage mirrorImage =
+        d->defaultPixmap.toImage().mirrored(d->orientation == Qt::Horizontal,
+                                            d->orientation == Qt::Vertical);
+    this->setIcon(QIcon(QPixmap::fromImage(mirrorImage)));
+    d->direction = Qt::RightToLeft;
+    }
+}
+
+//-----------------------------------------------------------------------------
+void ctkExpandButton::nextCheckState()
+{
+  Q_D(ctkExpandButton);
+  if (d->mirrorOnExpand)
+    {
+    Qt::LayoutDirection newDirection =
+        this->isChecked() ? Qt::LeftToRight : Qt::RightToLeft;
+    this->updateIcon(newDirection);
+    }
+
+  return this->Superclass::nextCheckState();
+}

+ 75 - 0
Libs/Widgets/ctkExpandButton.h

@@ -0,0 +1,75 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0.txt
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+#ifndef __ctkExpandButton_h
+#define __ctkExpandButton_h
+
+// QT includes
+#include <QToolButton>
+
+// CTK includes
+#include <ctkPimpl.h>
+
+#include "ctkWidgetsExport.h"
+
+class ctkExpandButtonPrivate;
+
+/// QToolButton with a standard icon
+/// This button has the same behavior as the extension
+/// button which appear as the last item in the toolbar
+/// ctkButton's purpose is to show, if the button is checked
+/// or hide, some others widgets.
+/// we can set the orientation of the icon with setOrientation()
+/// the widget can also mirror the icon on click if mirrorOnExpand is true.
+
+class CTK_WIDGETS_EXPORT  ctkExpandButton : public QToolButton
+{
+  Q_OBJECT
+  Q_PROPERTY(bool mirrorOnExpand READ mirrorOnExpand WRITE setMirrorOnExpand)
+public:
+  /// Superclass typedef
+  typedef QToolButton Superclass;
+
+  explicit ctkExpandButton(QWidget *_parent = 0);
+  virtual ~ctkExpandButton();
+
+  void setMirrorOnExpand(bool newBehavior);
+  bool mirrorOnExpand() const;
+
+  void setOrientation(Qt::Orientation newOrientation);
+  Qt::Orientation orientation() const;
+
+  virtual QSize sizeHint() const;
+
+private slots:
+  void updateIcon(Qt::LayoutDirection newDirection);
+
+protected:
+  virtual void nextCheckState();
+
+protected:
+  QScopedPointer<ctkExpandButtonPrivate> d_ptr;
+
+private:
+  Q_DECLARE_PRIVATE(ctkExpandButton);
+  Q_DISABLE_COPY(ctkExpandButton);
+};
+
+#endif