1 /*=========================================================================
2 
3   Library:   CTK
4 
5   Copyright (c) Kitware Inc.
6 
7   Licensed under the Apache License, Version 2.0 (the "License");
8   you may not use this file except in compliance with the License.
9   You may obtain a copy of the License at
10 
11       http://www.apache.org/licenses/LICENSE-2.0.txt
12 
13   Unless required by applicable law or agreed to in writing, software
14   distributed under the License is distributed on an "AS IS" BASIS,
15   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   See the License for the specific language governing permissions and
17   limitations under the License.
18 
19 =========================================================================*/
20 // Qt includes
21 #include <QDebug>
22 #include <QEvent>
23 
24 // CTK includes
25 #include "ctkConsole.h"
26 #include "ctkConsoleEventTranslator.h"
27 
28 // ----------------------------------------------------------------------------
ctkConsoleEventTranslator(QObject * parent)29 ctkConsoleEventTranslator::ctkConsoleEventTranslator(QObject* parent)
30   : pqWidgetEventTranslator(parent)
31 {
32   this->CurrentObject = 0;
33   this->CurrentCompleter = 0;
34   this->CurrentPopup = 0;
35 }
36 
37 // ----------------------------------------------------------------------------
translateEvent(QObject * Object,QEvent * Event,bool & Error)38 bool ctkConsoleEventTranslator::translateEvent(QObject *Object,
39                                                QEvent *Event,
40                                                bool &Error)
41 {
42   Q_UNUSED(Error);
43   // We don't want to save the actions in the completer.
44   ctkConsole* console = NULL;
45   for (QObject* test = Object ; console == NULL && test != NULL ; test = test->parent())
46     {
47     console = qobject_cast<ctkConsole*>(test);
48     }
49   // We don't want to save the actions in the completer.
50   if (this->CurrentCompleter && Object == this->CurrentCompleter)
51     {
52     return true;
53     }
54   if (this->CurrentPopup &&
55       this->CurrentPopup == qobject_cast<QAbstractItemView*>(Object))
56     {
57     return true;
58     }
59   if (!console)
60     {
61     return false;
62     }
63 
64 
65   if (Event->type() == QEvent::Enter && Object == console)
66     {
67     if (this->CurrentObject != Object)
68       {
69       if (this->CurrentObject)
70         {
71         qDebug() << "Disconnect ...";
72         QObject::disconnect(this->CurrentObject, 0 , this, 0);
73         }
74       this->CurrentObject = Object;
75       this->CurrentCompleter = qobject_cast<QObject*>(console->completer());
76       this->CurrentPopup = console->completer()->popup();
77       QObject::connect(console, SIGNAL(destroyed(QObject*)),
78                        this, SLOT(onDestroyed(QObject*)));
79       QObject::connect(console, SIGNAL(aboutToExecute(QString)),
80                        this, SLOT(onAboutToExecute(QString)));
81       }
82     }
83 
84   return true;
85 }
86 
87 // ----------------------------------------------------------------------------
onDestroyed(QObject *)88 void ctkConsoleEventTranslator::onDestroyed(QObject* /*object*/)
89 {
90   this->CurrentObject = 0;
91   this->CurrentCompleter = 0;
92   this->CurrentPopup = 0;
93 }
94 
95 // ----------------------------------------------------------------------------
onAboutToExecute(const QString & command)96 void ctkConsoleEventTranslator::onAboutToExecute(const QString& command)
97 {
98   this->recordEvent(this->CurrentObject, "command", command);
99 }
100