Developer Documentation
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 //#define VERBOSE_DEBUGGING_OUTPUT
43 
44 #include "SpinBoxEventFilter.hh"
45 
46 #include <QAbstractSpinBox>
47 #include <QEvent>
48 #include <QMetaEnum>
49 #include <stack>
50 
51 #ifdef VERBOSE_DEBUGGING_OUTPUT
52 #include <iostream>
53 #endif
54 
55 SpinBoxEventFilter::SpinBoxEventFilter(QObject *parent) :
56  QObject(parent), scrolling(false) {
57 
58  scrollingTimer.setSingleShot(true);
59  scrollingTimer.setInterval(500);
60 
61  connect(&scrollingTimer, SIGNAL( timeout() ), this, SLOT( unsetScrolling() ));
62 }
63 
64 SpinBoxEventFilter::~SpinBoxEventFilter() {
65 }
66 
67 bool SpinBoxEventFilter::eventFilter(QObject *object, QEvent *event) {
68 
69  if (event->type() != QEvent::Wheel) return QObject::eventFilter(object, event);
70 
71  QWidget *widget = qobject_cast<QWidget*>(object);
72 
73  if (scrollAreas.find(widget) != scrollAreas.end()) {
74 #ifdef VERBOSE_DEBUGGING_OUTPUT
75  std::cout << "Reset isScrolling." << std::endl;
76 #endif
77  setScrolling();
78  return QObject::eventFilter(object, event);
79  }
80 
81  if (!isScrolling() && widget->isEnabled()) {
82 #ifdef VERBOSE_DEBUGGING_OUTPUT
83  std::cout << "Not scrolling. Letting wheel event pass." << std::endl;
84 #endif
85  return QObject::eventFilter(object, event);
86  }
87 
88  setScrolling();
89  event->ignore();
90 #ifdef VERBOSE_DEBUGGING_OUTPUT
91  std::cout << "Swallowing wheel event." << std::endl;
92 #endif
93  return true;
94 
95 //#ifdef VERBOSE_DEBUGGING_OUTPUT
96 // std::cout << "Not a spin box. Letting wheel event pass." << std::endl;
97 //#endif
98 // return QObject::eventFilter(object, event);
99 }
100 
101 inline bool SpinBoxEventFilter::isScrolling() {
102  return scrolling;
103 }
104 
105 void SpinBoxEventFilter::hookUpToWidgetTree(QWidget *root_widget) {
106  std::stack<QWidget*> dfs;
107  dfs.push(root_widget);
108 
109  while (!dfs.empty()) {
110  QWidget *cur = dfs.top(); dfs.pop();
111 
112  cur->installEventFilter(this);
113 
114  const QObjectList &children = cur->children();
115  for (QObjectList::const_iterator it = children.begin(), it_end = children.end();
116  it != it_end; ++it) {
117 
118  QWidget *widget = qobject_cast<QWidget*>(*it);
119  if (widget)
120  dfs.push(widget);
121  }
122  }
123 }
124 
125 void SpinBoxEventFilter::registerScrollArea(QWidget *scrollArea) {
126  scrollAreas.insert(scrollArea);
127  scrollArea->installEventFilter(this);
128 }
129 
130 void SpinBoxEventFilter::setScrolling() {
131  scrolling = true;
132  scrollingTimer.start();
133 }
134 
135 void SpinBoxEventFilter::unsetScrolling() {
136 #ifdef VERBOSE_DEBUGGING_OUTPUT
137  std::cout << "Scrolling timeout." << std::endl;
138 #endif
139  scrolling = false;
140 }