pqTextLinker.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Kitware Inc.
2 // SPDX-FileCopyrightText: Copyright (c) Sandia Corporation
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef pqTextLinker_h
6 #define pqTextLinker_h
7 
8 #include "pqCoreModule.h"
9 
11 
12 #include <QString>
13 
14 #include <array>
15 #include <memory>
16 
25 {
29  pqTextLinker() noexcept = default;
30 
35  template <typename T, typename U>
36  explicit pqTextLinker(T* /*t*/, U* /*u*/)
37  {
38  static_assert(sizeof(T) == 0 && sizeof(U) == 0,
39  "Only specializations of pqTextLinker(T* t, U* u) can be used");
40  }
41 
47  {
48  for (decltype(other.Objs)::size_type i = 0; i < other.Objs.size(); ++i)
49  {
50  if (other.Objs[i])
51  {
52  this->Objs[i] = other.Objs[i]->clone();
53  }
54  else
55  {
56  this->Objs[i] = nullptr;
57  }
58  }
59 
60  return *this;
61  }
62 
66  void link()
67  {
68  if (this->Objs[0] && this->Objs[1])
69  {
70  this->Objs[0]->link(this->Objs[1].get());
71  this->Objs[1]->link(this->Objs[0].get());
72  }
73  }
74 
78  void unlink()
79  {
80  for (auto const& obj : this->Objs)
81  {
82  if (obj)
83  {
84  obj->unlink();
85  }
86  }
87  }
88 
92  bool isLinked() const noexcept
93  {
94  bool linked = false;
95  for (auto const& obj : this->Objs)
96  {
97  if (!obj)
98  {
99  return false;
100  }
101 
102  linked |= obj->isLinked();
103  }
104 
105  return linked;
106  }
107 
111  bool isLinkedTo(const QObject* Obj) const
112  {
113  bool linked = false;
114  for (auto const& obj : this->Objs)
115  {
116  if (!obj)
117  {
118  return false;
119  }
120 
121  linked |= (Obj == obj->getLinked());
122  }
123 
124  return linked;
125  }
126 
130  QString getFirstObjectName() const
131  {
132  if (this->Objs[0])
133  {
134  return this->Objs[0]->getName();
135  }
136 
137  return QString();
138  }
139 
143  QString getSecondObjectName() const
144  {
145  if (this->Objs[1])
146  {
147  return this->Objs[1]->getName();
148  }
149 
150  return QString();
151  }
152 
153 private:
154  std::array<std::unique_ptr<pqLinkedObjectInterface>, 2> Objs = { { nullptr, nullptr } };
155 };
156 
157 #include "pqTextLinker.txx"
158 
159 #endif // pqTextLinker_h
bool isLinkedTo(const QObject *Obj) const
Returns true if the linked object is the given input parameter.
Definition: pqTextLinker.h:111
pqTextLinker & operator=(const pqTextLinker &other)
Copy assignement operators performs a deep copy of the underlying data (only if they exist) ...
Definition: pqTextLinker.h:46
pqTextLinker(T *, U *)
Templated constructors that takes abritrary QTextEdit like objects.
Definition: pqTextLinker.h:36
#define PQCORE_EXPORT
Definition: pqCoreModule.h:15
bool isLinked() const noexcept
Returns true if one object is linked to the other.
Definition: pqTextLinker.h:92
Ease of use object that connects two pqLinkedObjectInterface together.
Definition: pqTextLinker.h:24
void link()
Links both objects to each other.
Definition: pqTextLinker.h:66
QString getFirstObjectName() const
Returns the first object name.
Definition: pqTextLinker.h:130
#define const
Definition: zconf.h:238
QString getSecondObjectName() const
Returns the second object name.
Definition: pqTextLinker.h:143
void unlink()
Unlinks both objects.
Definition: pqTextLinker.h:78