Selaa lähdekoodia

Add enabled property + test to ctkToolTipTrapper

Julien Finet 14 vuotta sitten
vanhempi
commit
571775a567

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

@@ -36,6 +36,7 @@ CREATE_TEST_SOURCELIST(Tests ${KIT}CppTests.cxx
   ctkSettingsTest1.cpp
   ctkSettingsDialogTest1.cpp
   ctkSliderWidgetTest1.cpp
+  ctkToolTipTrapperTest1.cpp
   ctkTreeComboBoxTest1.cpp
   ctkWorkflowWidgetTest1.cpp
   ctkWorkflowWidgetTest2.cpp
@@ -112,6 +113,7 @@ SIMPLE_TEST( ctkSettingsPanelTest1 )
 SIMPLE_TEST( ctkSettingsTest1 )
 SIMPLE_TEST( ctkSettingsDialogTest1 )
 SIMPLE_TEST( ctkSliderWidgetTest1 )
+SIMPLE_TEST( ctkToolTipTrapperTest1 )
 SIMPLE_TEST( ctkTreeComboBoxTest1 )
 SIMPLE_TEST( ctkWorkflowWidgetTest1 )
 SIMPLE_TEST( ctkWorkflowWidgetTest2 )

+ 68 - 0
Libs/Widgets/Testing/Cpp/ctkToolTipTrapperTest1.cpp

@@ -0,0 +1,68 @@
+/*=========================================================================
+
+  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.commontk.org/LICENSE
+
+  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 <QPushButton>
+#include <QTimer>
+
+// CTK includes
+#include "ctkToolTipTrapper.h"
+
+// STD includes
+#include <cstdlib>
+#include <iostream>
+
+//-----------------------------------------------------------------------------
+int ctkToolTipTrapperTest1(int argc, char * argv [] )
+{
+  QApplication app(argc, argv);
+
+  ctkToolTipTrapper trapper;
+  
+  if (trapper.isEnabled() != true)
+    {
+    std::cerr << "ctkToolTipTrapper::isEnabled default value" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  trapper.setEnabled(false);
+
+  if (trapper.isEnabled() != false)
+    {
+    std::cerr << "ctkToolTipTrapper::setEnabled failed" << std::endl;
+    return EXIT_FAILURE;
+    }
+  
+  QPushButton button("button");
+  button.setToolTip("Button tooltip text");
+  button.setCheckable(true);
+  QObject::connect(&button, SIGNAL(toggled(bool)),
+                   &trapper, SLOT(setEnabled(bool)));
+  button.show();
+
+  if (argc < 2 || QString(argv[1]) != "-I" )
+    {
+    QTimer::singleShot(200, &app, SLOT(quit()));
+    }
+
+  return app.exec();
+}
+

+ 51 - 3
Libs/Widgets/ctkToolTipTrapper.cpp

@@ -55,10 +55,31 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 // CTK includes
 #include "ctkToolTipTrapper.h"
 
+class ctkToolTipTrapperPrivate
+{
+  Q_DECLARE_PUBLIC(ctkToolTipTrapper);
+protected:
+  ctkToolTipTrapper* const q_ptr;
+public:
+  ctkToolTipTrapperPrivate(ctkToolTipTrapper& object);
+
+  bool Enabled;
+};
+
+// --------------------------------------------------------------------------
+ctkToolTipTrapperPrivate::ctkToolTipTrapperPrivate(ctkToolTipTrapper& object)
+  : q_ptr(&object)
+{
+  this->Enabled = false;
+}
+
 //------------------------------------------------------------------------------
-ctkToolTipTrapper::ctkToolTipTrapper(QObject * newParent):Superclass(newParent)
+ctkToolTipTrapper::ctkToolTipTrapper(QObject * newParent)
+  : Superclass(newParent)
+  , d_ptr(new ctkToolTipTrapperPrivate(*this))
 {
-  QCoreApplication::instance()->installEventFilter(this);
+  Q_D(ctkToolTipTrapper);
+  this->setEnabled(true);
 }
 
 //------------------------------------------------------------------------------
@@ -68,8 +89,35 @@ ctkToolTipTrapper::~ctkToolTipTrapper()
 }
 
 //------------------------------------------------------------------------------
-bool ctkToolTipTrapper::eventFilter(QObject* /*watched*/, QEvent* input_event)
+bool ctkToolTipTrapper::isEnabled()const
+{
+  Q_D(const ctkToolTipTrapper);
+  return d->Enabled;
+}
+
+//------------------------------------------------------------------------------
+void ctkToolTipTrapper::setEnabled(bool enable)
+{
+  Q_D(ctkToolTipTrapper);
+  if (enable == d->Enabled)
+    {
+    return;
+    }
+  d->Enabled = enable;
+  if (enable)
+    {
+    QCoreApplication::instance()->installEventFilter(this);
+    }
+  else
+    {
+    QCoreApplication::instance()->removeEventFilter(this);
+    }
+}
+
+//------------------------------------------------------------------------------
+bool ctkToolTipTrapper::eventFilter(QObject* watched, QEvent* input_event)
 {
+  Q_UNUSED(watched);
   if(input_event->type() == QEvent::ToolTip)
     {
     return true;

+ 15 - 0
Libs/Widgets/ctkToolTipTrapper.h

@@ -55,18 +55,33 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 // Qt includes
 #include <QObject>
 
+// CTK includes
 #include "ctkWidgetsExport.h"
+class ctkToolTipTrapperPrivate;
 
 /// To prevent tooltips from appearing, create an instance of this object.
 class CTK_WIDGETS_EXPORT ctkToolTipTrapper : public QObject
 {
   Q_OBJECT
+  Q_PROPERTY( bool enabled READ isEnabled WRITE setEnabled)
 public:
   typedef QObject Superclass;
   explicit ctkToolTipTrapper(QObject * newParent = 0);
   virtual ~ctkToolTipTrapper();
 
+  bool isEnabled()const;
+
   bool eventFilter(QObject* watched, QEvent* event);
+
+public slots:
+  void setEnabled(bool enable);
+
+protected:
+  QScopedPointer<ctkToolTipTrapperPrivate> d_ptr;
+
+private:
+  Q_DECLARE_PRIVATE(ctkToolTipTrapper);
+  Q_DISABLE_COPY(ctkToolTipTrapper);
 };
 
 #endif