Просмотр исходного кода

Merge branch 'ctkactionsignalmapper'

* ctkactionsignalmapper:
  Add ctkSignalMapper
Julien Finet лет назад: 14
Родитель
Сommit
219097cc2e

+ 3 - 0
Libs/Widgets/CMakeLists.txt

@@ -99,6 +99,8 @@ SET(KIT_SRCS
   ctkSettingsDialog.h
   ctkSettingsPanel.cpp
   ctkSettingsPanel.h
+  ctkSignalMapper.cpp
+  ctkSignalMapper.h
   ctkSimpleLayoutManager.cpp
   ctkSimpleLayoutManager.h
   ctkQImageView.cpp
@@ -209,6 +211,7 @@ SET(KIT_MOC_SRCS
   ctkSettings.h
   ctkSettingsDialog.h
   ctkSettingsPanel.h
+  ctkSignalMapper.h
   ctkSimpleLayoutManager.h
   ctkQImageView.h
   ctkSliderWidget.h

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

@@ -55,6 +55,7 @@ SET(TEST_SOURCES
   ctkSettingsPanelTest2.cpp
   ctkSettingsTest1.cpp
   ctkSettingsDialogTest1.cpp
+  ctkSignalMapperTest1.cpp
   ctkSliderWidgetTest1.cpp
   ctkSliderWidgetTest2.cpp
   ctkThumbnailListWidgetTest1.cpp
@@ -176,6 +177,7 @@ SIMPLE_TEST( ctkSettingsDialogTest1 )
 SIMPLE_TEST( ctkSettingsPanelTest1 )
 SIMPLE_TEST( ctkSettingsPanelTest2 )
 SIMPLE_TEST( ctkSettingsTest1 )
+SIMPLE_TEST( ctkSignalMapperTest1 )
 SIMPLE_TEST( ctkSliderWidgetTest1 )
 SIMPLE_TEST( ctkSliderWidgetTest2 )
 SIMPLE_TEST( ctkThumbnailListWidgetTest1 )

+ 68 - 0
Libs/Widgets/Testing/Cpp/ctkSignalMapperTest1.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.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 <QAction>
+#include <QApplication>
+#include <QDebug>
+#include <QActionGroup>
+#include <QSignalSpy>
+
+// CTK includes
+#include "ctkSignalMapper.h"
+
+// STD includes
+#include <cstdlib>
+#include <iostream>
+
+//-----------------------------------------------------------------------------
+int ctkSignalMapperTest1(int argc, char* argv[])
+{
+  QApplication app(argc, argv);
+
+  QAction action1(0);
+  QAction action2(0);
+  QAction action3(0);
+
+  QActionGroup actionGroup(0);
+  actionGroup.setExclusive(true);
+  actionGroup.addAction(&action1);
+  actionGroup.addAction(&action2);
+  actionGroup.addAction(&action3);
+
+  ctkSignalMapper signalMapper;
+  signalMapper.setMapping(&action1, 1);
+  signalMapper.setMapping(&action2, 2);
+  signalMapper.setMapping(&action3, 3);
+
+  QObject::connect(&actionGroup, SIGNAL(triggered(QAction*)),
+                   &signalMapper, SLOT(map(QAction*)));
+  QSignalSpy signalSpy(&signalMapper, SIGNAL(mapped(int)));
+
+  action2.trigger();
+
+  if (signalSpy.count() != 1 ||
+      signalSpy.at(0).at(0).toInt() != 2)
+    {
+    std::cerr << "ctkSignalMapper::map(QAction*) failed." << std::endl;
+    return EXIT_FAILURE;
+    }
+  return EXIT_SUCCESS;
+}

+ 37 - 0
Libs/Widgets/ctkSignalMapper.cpp

@@ -0,0 +1,37 @@
+/*=========================================================================
+
+  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 <QAction>
+
+// qMRMLWidgets includes
+#include "ctkSignalMapper.h"
+
+//-----------------------------------------------------------------------------
+ctkSignalMapper::ctkSignalMapper(QObject* parentObject)
+  :QSignalMapper(parentObject)
+{
+}
+
+//-----------------------------------------------------------------------------
+void ctkSignalMapper::map(QAction* sender)
+{
+  this->QSignalMapper::map(qobject_cast<QObject*>(sender));
+}

+ 50 - 0
Libs/Widgets/ctkSignalMapper.h

@@ -0,0 +1,50 @@
+/*=========================================================================
+
+  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 __ctkSignalMapper_h
+#define __ctkSignalMapper_h
+
+// Qt includes
+#include <QSignalMapper>
+class QAction;
+
+// CTKWidgets includes
+#include "ctkWidgetsExport.h"
+
+/// Advanced QSignalMapper to simplify the use of mapping.
+class CTK_WIDGETS_EXPORT ctkSignalMapper: public QSignalMapper
+{
+  Q_OBJECT
+  
+public:
+  ctkSignalMapper(QObject* newParent = 0);
+  
+public slots:
+  /// ctkSignalMapper exposes the map(QAction*) slot to be conveniently
+  /// connected with signals that have a QAction* as their first argument.
+  /// ctkActionSignalMapper reveals to be useful when connecting a
+  /// QActionGroup::triggered(QAction*).
+  void map(QAction* sender);
+
+protected:
+  Q_DISABLE_COPY(ctkSignalMapper);
+};
+
+#endif