pqEventTranslator.cxx 6.21 KB
Newer Older
Clinton Stimpson's avatar
 
Clinton Stimpson committed
1 2 3 4 5
/*=========================================================================

   Program: ParaView
   Module:    pqEventTranslator.cxx

6
   Copyright (c) 2005-2008 Sandia Corporation, Kitware Inc.
Clinton Stimpson's avatar
 
Clinton Stimpson committed
7 8 9
   All rights reserved.

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

12
   See License_v1.2.txt for the full ParaView license.
Clinton Stimpson's avatar
 
Clinton Stimpson committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
   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.

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

Clinton Stimpson's avatar
 
Clinton Stimpson committed
33
#include "pqEventTranslator.h"
34

Clinton Stimpson's avatar
 
Clinton Stimpson committed
35 36 37
#include "pqAbstractButtonEventTranslator.h"
#include "pqAbstractItemViewEventTranslator.h"
#include "pqAbstractSliderEventTranslator.h"
38
#include "pqBasicWidgetEventTranslator.h"
Clinton Stimpson's avatar
 
Clinton Stimpson committed
39 40 41 42 43 44
#include "pqComboBoxEventTranslator.h"
#include "pqDoubleSpinBoxEventTranslator.h"
#include "pqLineEditEventTranslator.h"
#include "pqMenuEventTranslator.h"
#include "pqObjectNaming.h"
#include "pqSpinBoxEventTranslator.h"
Clinton Stimpson's avatar
 
Clinton Stimpson committed
45
#include "pqTabBarEventTranslator.h"
46
#include "pqTreeViewEventTranslator.h"
47 48
#include "pqNativeFileDialogEventTranslator.h"
#include "pq3DViewEventTranslator.h"
Clinton Stimpson's avatar
 
Clinton Stimpson committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

#include <QCoreApplication>
#include <QtDebug>
#include <QSet>
#include <QVector>

////////////////////////////////////////////////////////////////////////////////
// pqEventTranslator::pqImplementation

struct pqEventTranslator::pqImplementation
{
  ~pqImplementation()
  {
  }

  /// Stores the working set of widget translators  
  QVector<pqWidgetEventTranslator*> Translators;
  /// Stores the set of objects that should be ignored when translating events
  QSet<QObject*> IgnoredObjects;
68 69 70 71

  // list of widgets for which mouse propagation will happen
  // we'll only translate the first and ignore the rest
  QList<QWidget*> MouseParents;
Clinton Stimpson's avatar
 
Clinton Stimpson committed
72 73 74 75 76
};

////////////////////////////////////////////////////////////////////////////////
// pqEventTranslator

Clinton Stimpson's avatar
 
Clinton Stimpson committed
77 78
pqEventTranslator::pqEventTranslator(QObject* p)
 : QObject(p),
Clinton Stimpson's avatar
 
Clinton Stimpson committed
79 80 81 82 83 84
  Implementation(new pqImplementation())
{
}

pqEventTranslator::~pqEventTranslator()
{
Clinton Stimpson's avatar
 
Clinton Stimpson committed
85
  this->stop();
Clinton Stimpson's avatar
 
Clinton Stimpson committed
86 87 88
  delete Implementation;
}

Clinton Stimpson's avatar
 
Clinton Stimpson committed
89 90 91
void pqEventTranslator::start()
{
  QCoreApplication::instance()->installEventFilter(this);
92
  emit this->started();
Clinton Stimpson's avatar
 
Clinton Stimpson committed
93 94 95 96
}

void pqEventTranslator::stop()
{
97
  emit this->stopped();
Clinton Stimpson's avatar
 
Clinton Stimpson committed
98 99 100
  QCoreApplication::instance()->removeEventFilter(this);
}

101
void pqEventTranslator::addDefaultWidgetEventTranslators(pqTestUtility* util)
Clinton Stimpson's avatar
 
Clinton Stimpson committed
102
{
Clinton Stimpson's avatar
 
Clinton Stimpson committed
103
  addWidgetEventTranslator(new pqBasicWidgetEventTranslator());
Clinton Stimpson's avatar
 
Clinton Stimpson committed
104 105 106 107 108 109 110 111
  addWidgetEventTranslator(new pqAbstractButtonEventTranslator());
  addWidgetEventTranslator(new pqAbstractItemViewEventTranslator());
  addWidgetEventTranslator(new pqAbstractSliderEventTranslator());
  addWidgetEventTranslator(new pqComboBoxEventTranslator());
  addWidgetEventTranslator(new pqDoubleSpinBoxEventTranslator());
  addWidgetEventTranslator(new pqLineEditEventTranslator());
  addWidgetEventTranslator(new pqMenuEventTranslator());
  addWidgetEventTranslator(new pqSpinBoxEventTranslator());
Clinton Stimpson's avatar
 
Clinton Stimpson committed
112
  addWidgetEventTranslator(new pqTabBarEventTranslator());
113
  addWidgetEventTranslator(new pqTreeViewEventTranslator());
114 115
  addWidgetEventTranslator(new pq3DViewEventTranslator("QGLWidget"));
  addWidgetEventTranslator(new pqNativeFileDialogEventTranslator(util));
Clinton Stimpson's avatar
 
Clinton Stimpson committed
116 117 118 119 120 121
}

void pqEventTranslator::addWidgetEventTranslator(pqWidgetEventTranslator* Translator)
{
  if(Translator)
    {
Clinton Stimpson's avatar
 
Clinton Stimpson committed
122 123
    this->Implementation->Translators.push_front(Translator);
    Translator->setParent(this);
Clinton Stimpson's avatar
 
Clinton Stimpson committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    
    QObject::connect(
      Translator,
      SIGNAL(recordEvent(QObject*, const QString&, const QString&)),
      this,
      SLOT(onRecordEvent(QObject*, const QString&, const QString&)));
    }
}

void pqEventTranslator::ignoreObject(QObject* Object)
{
  this->Implementation->IgnoredObjects.insert(Object);
}

bool pqEventTranslator::eventFilter(QObject* Object, QEvent* Event)
{
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

  // mouse events are propagated to parents
  // our event translators/players don't quite like that,
  // so lets consume those extra ones
  if(Event->type() == QEvent::MouseButtonPress ||
     Event->type() == QEvent::MouseButtonDblClick ||
     Event->type() == QEvent::MouseButtonRelease)
    {
    if(!this->Implementation->MouseParents.empty() &&
      this->Implementation->MouseParents.first() == Object)
      {
      // right on track
      this->Implementation->MouseParents.removeFirst();
      return false;
      }

    QWidget* widget = qobject_cast<QWidget*>(Object);
    
    // find the chain of parent that will get this mouse event
    this->Implementation->MouseParents.clear();
    for(QWidget* w = widget->parentWidget(); w; w = w->parentWidget())
      {
      this->Implementation->MouseParents.append(w);
      if(w->isWindow() || w->testAttribute(Qt::WA_NoMousePropagation))
        {
        break;
        }
      }
    }

Clinton Stimpson's avatar
 
Clinton Stimpson committed
170 171 172 173 174 175
  for(int i = 0; i != this->Implementation->Translators.size(); ++i)
    {
    bool error = false;
    if(this->Implementation->Translators[i]->translateEvent(Object, Event, error))
      {
      if(error)
176
        {
Clinton Stimpson's avatar
 
Clinton Stimpson committed
177
        qWarning() << "Error translating an event for object " << Object;
178
        }
Clinton Stimpson's avatar
 
Clinton Stimpson committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
      return false;
      }
    }

  return false;
}

void pqEventTranslator::onRecordEvent(QObject* Object, const QString& Command, const QString& Arguments)
{
  if(this->Implementation->IgnoredObjects.contains(Object))
    return;

  const QString name = pqObjectNaming::GetName(*Object);
  if(name.isEmpty())
    return;
    
//  qDebug() << "Event: " << name << " " << Command << " " << Arguments;
  emit recordEvent(name, Command, Arguments);
}