ArnLib  4.0.x
Active Registry Network
ArnItemValve.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2022 Michael Wiklund.
2 // All rights reserved.
3 // Contact: arnlib@wiklunden.se
4 //
5 // This file is part of the ArnLib - Active Registry Network.
6 // Parts of ArnLib depend on Qt and/or other libraries that have their own
7 // licenses. Usage of these other libraries is subject to their respective
8 // license agreements.
9 //
10 // GNU Lesser General Public License Usage
11 // This file may be used under the terms of the GNU Lesser General Public
12 // License version 2.1 as published by the Free Software Foundation and
13 // appearing in the file LICENSE_LGPL.txt included in the packaging of this
14 // file. In addition, as a special exception, you may use the rights described
15 // in the Nokia Qt LGPL Exception version 1.1, included in the file
16 // LGPL_EXCEPTION.txt in this package.
17 //
18 // GNU General Public License Usage
19 // Alternatively, this file may be used under the terms of the GNU General Public
20 // License version 3.0 as published by the Free Software Foundation and appearing
21 // in the file LICENSE_GPL.txt included in the packaging of this file.
22 //
23 // Other Usage
24 // Alternatively, this file may be used in accordance with the terms and conditions
25 // contained in a signed written agreement between you and Michael Wiklund.
26 //
27 // This program is distributed in the hope that it will be useful, but WITHOUT ANY
28 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
29 // PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
30 //
31 
32 #include "ArnInc/ArnItemValve.hpp"
33 #include "private/ArnItemValve_p.hpp"
34 
35 
36 ArnItemValvePrivate::ArnItemValvePrivate()
37 {
38  _switchValue = true;
39  _targetItem = arnNullptr;
40 }
41 
42 
43 ArnItemValvePrivate::~ArnItemValvePrivate()
44 {
45 }
46 
47 
48 ArnItemValve::ArnItemValve( QObject *parent)
49  : ArnItemB( *new ArnItemValvePrivate, parent)
50 {
51 }
52 
53 
54 ArnItemValve::ArnItemValve( ArnItemValvePrivate& dd, QObject* parent)
55  : ArnItemB( dd, parent)
56 {
57 }
58 
59 
61 {
62  Q_D(ArnItemValve);
63 
64  d->_targetItem = targetItem;
65  d->_switchMode = mode;
66  doControl();
67 
68  return true;
69 }
70 
71 
73 {
74  Q_D(const ArnItemValve);
75 
76  return d->_switchMode;
77 }
78 
79 
81 {
82  Q_D(const ArnItemValve);
83 
84  if (isOpen())
85  return ArnItemB::toBool();
86  else
87  return d->_switchValue;
88 }
89 
90 
92 {
93  setValue( value);
94  return *this;
95 }
96 
97 
98 void ArnItemValve::setValue( bool value)
99 {
100  Q_D(ArnItemValve);
101 
102  if (isOpen())
104  else if (value != d->_switchValue) {
105  d->_switchValue = value;
106  doControl();
107  emit changed( d->_switchValue);
108  }
109 }
110 
111 
112 void ArnItemValve::itemUpdated( const ArnLinkHandle& handleData, const QByteArray* value)
113 {
114  Q_D(ArnItemValve);
115 
116  ArnItemB::itemUpdated( handleData, value);
117 
118  if (value)
119  d->_switchValue = (value->toInt() != 0);
120  else
121  d->_switchValue = ArnItemB::toBool();
122 
123  doControl();
124  emit changed( d->_switchValue);
125 }
126 
127 
128 void ArnItemValve::doControl()
129 {
130  Q_D(ArnItemValve);
131 
132  if (!d->_targetItem) return; // No target to control
133 
134  if (d->_switchMode.is( SwitchMode::InStream))
135  (d->_targetItem->*&ArnItemValve::setEnableUpdNotify)( d->_switchValue); // Control target changed() signal
136  if (d->_switchMode.is( SwitchMode::OutStream))
137  (d->_targetItem->*&ArnItemValve::setEnableSetValue)( d->_switchValue); // Control target assign value
138 }
void setValue(bool value)
Assign a bool to an Arn Data Object
ArnItemValve & operator=(bool value)
void setValue(const ArnBasicItem &other, int ignoreSame=Arn::SameValue::DefaultAction)
void changed(int value)
bool setTarget(ArnItemB *targetItem, SwitchMode mode=SwitchMode::InOutStream)
Valve for controlling stream to/from an ArnItemB.
bool toBool(bool *isOk=arnNullptr) const
SwitchMode switchMode() const
bool isOpen() const
State of the handle.
bool toBool() const
Control target item accepting assign of value (setValue)
Base class handle for an Arn Data Object.
Definition: ArnItemB.hpp:59
Control target item notifying (signal) updated value.
ArnItemValve(QObject *parent=arnNullptr)
Assigning same value is ignored.
Definition: Arn.hpp:66