Salome HOME
Use not obligatory AXML flag in general validator
[modules/shaper.git] / src / ModuleBase / ModuleBase_ModelWidget.cpp
1 // File:        ModuleBase_ModelWidget.h
2 // Created:     25 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include "ModuleBase_ModelWidget.h"
6
7 #include <ModelAPI_Data.h>
8 #include <ModelAPI_Attribute.h>
9 #include <ModelAPI_Events.h>
10 #include <ModelAPI_Session.h>
11
12 #include <Config_Keywords.h>
13 #include <Config_WidgetAPI.h>
14
15 #include <Events_Loop.h>
16
17 #include <QEvent>
18 #include <QWidget>
19
20 ModuleBase_ModelWidget::ModuleBase_ModelWidget(QObject* theParent, const Config_WidgetAPI* theData,
21                                                const std::string& theParentId)
22     : QObject(theParent),
23       myParentId(theParentId)
24 {
25   myIsComputedDefault = false;
26   myIsObligatory = theData ? theData->getBooleanAttribute(FEATURE_OBLIGATORY, true) : true;
27   myAttributeID = theData ? theData->widgetId() : "";
28 }
29
30 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
31 {
32   return theObject->data()->attribute(attributeID())->isInitialized();
33 }
34
35 void ModuleBase_ModelWidget::enableFocusProcessing()
36 {
37   QList<QWidget*> aMyControls = getControls();
38   foreach(QWidget*  eachControl, aMyControls) {
39     if(!myFocusInWidgets.contains(eachControl)) {
40       enableFocusProcessing(eachControl);
41     }
42   }
43 }
44
45 bool ModuleBase_ModelWidget::focusTo()
46 {
47   QList<QWidget*> aControls = getControls();
48   QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
49   for (; anIt != aLast; anIt++) {
50     QWidget* aWidget = *anIt;
51     if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
52       aWidget->setFocus();
53       break;
54     }
55   }
56   return true;
57 }
58
59
60 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObj) const
61 {
62   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
63   static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
64   ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
65 }
66
67 void ModuleBase_ModelWidget::enableFocusProcessing(QWidget* theWidget)
68 {
69   theWidget->setFocusPolicy(Qt::StrongFocus);
70   theWidget->installEventFilter(this);
71   myFocusInWidgets.append(theWidget);
72 }
73
74 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
75 {
76   QWidget* aWidget = dynamic_cast<QWidget*>(theObject);
77   if (theEvent->type() == QEvent::MouseButtonRelease && 
78       myFocusInWidgets.contains(aWidget)) {
79     emit focusInWidget(this);
80   } 
81   // pass the event on to the parent class
82   return QObject::eventFilter(theObject, theEvent);
83 }