pqAbstractButtonEventTranslator.cxx 4.94 KB
Newer Older
1 2 3 4 5
/*=========================================================================

   Program: ParaView
   Module:    pqAbstractButtonEventTranslator.cxx

6
   Copyright (c) 2005-2008 Sandia Corporation, Kitware Inc.
7 8 9
   All rights reserved.

   ParaView is a free software; you can redistribute it and/or modify it
Mathieu Westphal's avatar
Mathieu Westphal committed
10
   under the terms of the ParaView license version 1.2.
11

12
   See License_v1.2.txt for the full ParaView license.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
   A copy of this license can be obtained by contacting
   Kitware Inc.
   28 Corporate Drive
   Clifton Park, NY 12065
   USA

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

=========================================================================*/

#include "pqAbstractButtonEventTranslator.h"

#include <QAbstractButton>
#include <QAction>
37
#include <QDebug>
38 39
#include <QKeyEvent>
#include <QMouseEvent>
40
#include <QPushButton>
41
#include <QToolButton>
42

43 44
pqAbstractButtonEventTranslator::pqAbstractButtonEventTranslator(QObject* p)
  : pqWidgetEventTranslator(p)
45
{
46
  this->LastMouseEventType = QEvent::None;
47 48
}

Mathieu Westphal's avatar
Mathieu Westphal committed
49
bool pqAbstractButtonEventTranslator::translateEvent(QObject* Object, QEvent* Event, bool& Error)
50 51
{
  QAbstractButton* const object = qobject_cast<QAbstractButton*>(Object);
52 53
  if (!object)
  {
54
    return false;
55
  }
56 57 58
  QPushButton* pushButton = qobject_cast<QPushButton*>(object);
  QToolButton* toolButton = qobject_cast<QToolButton*>(object);
  bool withMenu = (pushButton && pushButton->menu()) ||
59 60 61 62
    (toolButton && (toolButton->menu() ||
                     toolButton->defaultAction() && toolButton->defaultAction()->menu()));
  switch (Event->type())
  {
63
    case QEvent::KeyPress:
64
    {
65
      QKeyEvent* const e = dynamic_cast<QKeyEvent*>(Event);
66 67
      if (e->key() == Qt::Key_Space)
      {
68
        onActivate(object);
69
      }
Mathieu Westphal's avatar
Mathieu Westphal committed
70
      return true;
71
      break;
72
    }
73
    case QEvent::MouseButtonPress:
74
    {
75 76 77
      QMouseEvent* const e = dynamic_cast<QMouseEvent*>(Event);
      this->LastMouseEventType = Event->type();
      // Menus are opened on mouse press
78 79 80
      if (e->button() == Qt::LeftButton && object->rect().contains(e->pos()) &&
        this->hasMenu(object))
      {
81
        onActivate(object);
82
      }
Mathieu Westphal's avatar
Mathieu Westphal committed
83
      return true;
84
      break;
85
    }
86
    case QEvent::Timer:
87
    {
88
      if (this->LastMouseEventType == QEvent::MouseButtonPress)
89
      {
90
        QToolButton* tButton = qobject_cast<QToolButton*>(object);
91 92
        if (tButton && tButton->popupMode() == QToolButton::DelayedPopup)
        {
93
          emit recordEvent(object, "longActivate", "");
94 95
          // Tell comming mouse button release to not record activate.
          this->LastMouseEventType = QEvent::None;
96
        }
97
      }
Mathieu Westphal's avatar
Mathieu Westphal committed
98
      return true;
99
      break;
100
    }
101
    case QEvent::MouseButtonRelease:
102 103
    {
      bool lastEventWasMouseButtonPress = this->LastMouseEventType == QEvent::MouseButtonPress;
104 105 106
      QMouseEvent* const e = dynamic_cast<QMouseEvent*>(Event);
      this->LastMouseEventType = Event->type();
      // Buttons are activated on mouse release. Menus should already be recorded.
107 108 109
      if (e->button() == Qt::LeftButton && object->rect().contains(e->pos()) &&
        !this->hasMenu(object) && lastEventWasMouseButtonPress)
      {
110
        onActivate(object);
111
      }
Mathieu Westphal's avatar
Mathieu Westphal committed
112
      return true;
113 114
      break;
    }
115 116 117
    default:
      break;
  }
Mathieu Westphal's avatar
Mathieu Westphal committed
118
  return this->Superclass::translateEvent(Object, Event, Error);
119 120
}

121
void pqAbstractButtonEventTranslator::onActivate(QAbstractButton* actualObject)
122
{
123 124 125
  QObject* object = actualObject;
  QToolButton* tb = qobject_cast<QToolButton*>(object);
  if (tb && tb->defaultAction())
126
  {
127
    object = tb->defaultAction();
128 129 130
  }
  if (actualObject->isCheckable())
  {
131
    const bool new_value = !actualObject->isChecked();
132
    emit recordEvent(object, "set_boolean", new_value ? "true" : "false");
133
  }
134
  else
135
  {
136
    emit recordEvent(object, "activate", "");
137
  }
138
}
139 140 141 142 143 144 145 146 147 148 149 150

bool pqAbstractButtonEventTranslator::hasMenu(QAbstractButton* button) const
{
  bool hasMenu = false;
  QPushButton* pushButton = qobject_cast<QPushButton*>(button);
  if (pushButton)
  {
    hasMenu = pushButton->menu() != 0;
  }
  QToolButton* toolButton = qobject_cast<QToolButton*>(button);
  if (toolButton)
  {
151 152
    hasMenu = toolButton->menu() != 0 ||
      (toolButton->defaultAction() && toolButton->defaultAction()->menu() != 0);
Mathieu Westphal's avatar
Mathieu Westphal committed
153
    hasMenu = hasMenu && toolButton->popupMode() != QToolButton::DelayedPopup;
154 155 156
  }
  return hasMenu;
}