ctkSignalMapper.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. #ifndef __ctkSignalMapper_h
  15. #define __ctkSignalMapper_h
  16. // Qt includes
  17. #include <QSignalMapper>
  18. class QAction;
  19. // CTKWidgets includes
  20. #include "ctkWidgetsExport.h"
  21. /// \ingroup Widgets
  22. /// Advanced QSignalMapper to simplify the use of mapping.
  23. class CTK_WIDGETS_EXPORT ctkSignalMapper: public QSignalMapper
  24. {
  25. Q_OBJECT
  26. public:
  27. ctkSignalMapper(QObject* newParent = 0);
  28. public Q_SLOTS:
  29. /// ctkSignalMapper exposes the map(QAction*) slot to be conveniently
  30. /// connected with signals that have a QAction* as their first argument.
  31. /// ctkSignalMapper reveals to be useful when connecting a
  32. /// QMenu::triggered(QAction*) or QActionGroup::triggered(QAction*).
  33. /// Example:
  34. /// <code>
  35. /// QMenu menu;
  36. /// QAction* action1 = menu.addAction("item1");
  37. /// QAction* action2 = menu.addAction("item2");
  38. /// QAction* action3 = menu.addAction("item3");
  39. /// ctkSignalMapper signalMapper;
  40. /// signalMapper.setMapping(action1, 1);
  41. /// signalMapper.setMapping(action2, 2);
  42. /// signalMapper.setMapping(action3, 3);
  43. /// QObject::connect(&menu, SIGNAL(triggered(QAction*)),
  44. /// &signalMapper, SLOT(map(QAction*)));
  45. /// //Connect the signal mapper mapped(int) signal with a listener
  46. /// QObject::connect(&signalMapper, SIGNAL(mapped(int)),
  47. /// &myObj, SLOT(actionTriggered(int)));
  48. /// // myObj::actionTriggered(2) will be called when the 2nd menu item will
  49. /// // be chosen in the menu.
  50. /// </code>
  51. void map(QAction* sender);
  52. protected:
  53. Q_DISABLE_COPY(ctkSignalMapper);
  54. };
  55. #endif