ctkSignalMapper.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. /// Advanced QSignalMapper to simplify the use of mapping.
  22. class CTK_WIDGETS_EXPORT ctkSignalMapper: public QSignalMapper
  23. {
  24. Q_OBJECT
  25. public:
  26. ctkSignalMapper(QObject* newParent = 0);
  27. public slots:
  28. /// ctkSignalMapper exposes the map(QAction*) slot to be conveniently
  29. /// connected with signals that have a QAction* as their first argument.
  30. /// ctkSignalMapper reveals to be useful when connecting a
  31. /// QMenu::triggered(QAction*) or QActionGroup::triggered(QAction*).
  32. /// Example:
  33. /// <code>
  34. /// QMenu menu;
  35. /// QAction* action1 = menu.addAction("item1");
  36. /// QAction* action2 = menu.addAction("item2");
  37. /// QAction* action3 = menu.addAction("item3");
  38. /// ctkSignalMapper signalMapper;
  39. /// signalMapper.setMapping(action1, 1);
  40. /// signalMapper.setMapping(action2, 2);
  41. /// signalMapper.setMapping(action3, 3);
  42. /// QObject::connect(&menu, SIGNAL(triggered(QAction*)),
  43. /// &signalMapper, SLOT(map(QAction*)));
  44. /// //Connect the signal mapper mapped(int) signal with a listener
  45. /// QObject::connect(&signalMapper, SIGNAL(mapped(int)),
  46. /// &myObj, SLOT(actionTriggered(int)));
  47. /// // myObj::actionTriggered(2) will be called when the 2nd menu item will
  48. /// // be chosen in the menu.
  49. /// </code>
  50. void map(QAction* sender);
  51. protected:
  52. Q_DISABLE_COPY(ctkSignalMapper);
  53. };
  54. #endif