Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
SpinBoxEventFilter.cc
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43  * *
44  * $Revision$ *
45  * $Author$ *
46  * $Date$ *
47  * *
48 \*===========================================================================*/
49 
50 /*
51  * SpinBoxEventFilter.cc
52  *
53  * Created on: Aug 1, 2013
54  * Author: ebke
55  */
56 
57 //#define VERBOSE_DEBUGGING_OUTPUT
58 
59 #include "SpinBoxEventFilter.hh"
60 
61 #include <QAbstractSpinBox>
62 #include <QEvent>
63 #include <QMetaEnum>
64 #include <stack>
65 
66 #ifdef VERBOSE_DEBUGGING_OUTPUT
67 #include <iostream>
68 #endif
69 
70 SpinBoxEventFilter::SpinBoxEventFilter(QObject *parent) :
71  QObject(parent), scrolling(false) {
72 
73  scrollingTimer.setSingleShot(true);
74  scrollingTimer.setInterval(500);
75 
76  connect(&scrollingTimer, SIGNAL( timeout() ), this, SLOT( unsetScrolling() ));
77 }
78 
79 SpinBoxEventFilter::~SpinBoxEventFilter() {
80 }
81 
82 bool SpinBoxEventFilter::eventFilter(QObject *object, QEvent *event) {
83 
84  if (event->type() != QEvent::Wheel) return QObject::eventFilter(object, event);
85 
86  QWidget *widget = qobject_cast<QWidget*>(object);
87 
88  if (scrollAreas.find(widget) != scrollAreas.end()) {
89 #ifdef VERBOSE_DEBUGGING_OUTPUT
90  std::cout << "Reset isScrolling." << std::endl;
91 #endif
92  setScrolling();
93  return QObject::eventFilter(object, event);
94  }
95 
96  if (!isScrolling() && widget->isEnabled()) {
97 #ifdef VERBOSE_DEBUGGING_OUTPUT
98  std::cout << "Not scrolling. Letting wheel event pass." << std::endl;
99 #endif
100  return QObject::eventFilter(object, event);
101  }
102 
103  setScrolling();
104  event->ignore();
105 #ifdef VERBOSE_DEBUGGING_OUTPUT
106  std::cout << "Swallowing wheel event." << std::endl;
107 #endif
108  return true;
109 
110 //#ifdef VERBOSE_DEBUGGING_OUTPUT
111 // std::cout << "Not a spin box. Letting wheel event pass." << std::endl;
112 //#endif
113 // return QObject::eventFilter(object, event);
114 }
115 
116 inline bool SpinBoxEventFilter::isScrolling() {
117  return scrolling;
118 }
119 
120 void SpinBoxEventFilter::hookUpToWidgetTree(QWidget *root_widget) {
121  std::stack<QWidget*> dfs;
122  dfs.push(root_widget);
123 
124  while (!dfs.empty()) {
125  QWidget *cur = dfs.top(); dfs.pop();
126 
127  cur->installEventFilter(this);
128 
129  const QObjectList &children = cur->children();
130  for (QObjectList::const_iterator it = children.begin(), it_end = children.end();
131  it != it_end; ++it) {
132 
133  QWidget *widget = qobject_cast<QWidget*>(*it);
134  if (widget)
135  dfs.push(widget);
136  }
137  }
138 }
139 
140 void SpinBoxEventFilter::registerScrollArea(QWidget *scrollArea) {
141  scrollAreas.insert(scrollArea);
142  scrollArea->installEventFilter(this);
143 }
144 
145 void SpinBoxEventFilter::setScrolling() {
146  scrolling = true;
147  scrollingTimer.start();
148 }
149 
150 void SpinBoxEventFilter::unsetScrolling() {
151 #ifdef VERBOSE_DEBUGGING_OUTPUT
152  std::cout << "Scrolling timeout." << std::endl;
153 #endif
154  scrolling = false;
155 }