ICF 3.0.5.47
Technical documentation of ICF Libraries
TCommonSupplierGuiCompBase.h
Go to the documentation of this file.
1/********************************************************************************
2** This file is part of the ICF Framework. Copyright (C) Witold Gantzke & Kirill Lepskiy
3** ICF Framework may be used under the terms of the LGPL License v. 2.1 by the Free Software Foundation.
4********************************************************************************/
5
6#ifndef iqtinsp_TCommonSupplierGuiCompBase_included
7#define iqtinsp_TCommonSupplierGuiCompBase_included
8
9
10// Qt includes
11#include<QtCore/QtGlobal>
12#include<QtCore/QMap>
13#include <QtWidgets/QMessageBox>
14
15// ICF includes
17#include <istd/CChangeGroup.h>
19#include <imod/IObserver.h>
20#include <iprm/IParamsSet.h>
21#include <iinsp/ISupplier.h>
22#include <istdgui/IGuiObject.h>
24#include <iinspgui/iinspgui.h>
25
26
27namespace iinspgui
28{
29
30
31template <class UI>
33{
34public:
36
37 I_BEGIN_BASE_COMPONENT(TCommonSupplierGuiCompBase);
38 I_ASSIGN(m_paramsLoaderCompPtr, "ParamsLoader", "Loads and saves parameters from and to file", false, "ParamsLoader");
39 I_ASSIGN(m_paramsSetGuiCompPtr, "ParamsSetGui", "Shows parameter set", false, "ParamsSetGui");
40 I_ASSIGN_TO(m_paramsSetObserverCompPtr, m_paramsSetGuiCompPtr, false);
41 I_ASSIGN(m_connectParametersToEditorAttrPtr, "ConnectParametersToEditor", "If enabled, the parameter set of the supplier will be connected to the parameter editor", true, true);
42 I_END_COMPONENT;
43
45
46protected:
48
52 bool AreParamsEditable() const;
53
57 bool IsLoadParamsSupported() const;
58
62 bool IsSaveParamsSupported() const;
63
67 bool LoadParams();
68
72 bool SaveParams();
73
77 bool DoTest();
78
82 virtual void UpdateVisualStatus();
83
88 virtual void OnSupplierParamsChanged();
89
93 virtual void BeforeSupplierGuiUpdated();
94
95 // reimplemented (istdgui::TGuiObserverWrap)
96 void OnGuiModelAttached() override;
97 void OnGuiModelDetached() override;
98
99 // reimplemented (imod::IObserver)
100 void AfterModelChange(imod::IModel* modelPtr, const istd::IChangeable::ChangeSet& changeSet) override;
101
102 // abstract methods
108 virtual QWidget* GetParamsWidget() const = 0;
109
110protected:
112 {
113 public:
115
117
118 protected:
119 // reimplemented (imod::CSingleModelObserverBase)
120 void OnUpdate(const istd::IChangeable::ChangeSet& changeSet) override;
121
122 private:
124 };
125
127 {
128 public:
130
132
133 protected:
134 // reimplemented (imod::CSingleModelObserverBase)
135 void OnUpdate(const istd::IChangeable::ChangeSet& changeSet) override;
136
137 private:
139 };
140
143
144protected:
146
147private:
148 I_REF(ifile::IFilePersistence, m_bitmapLoaderCompPtr);
149 I_REF(ifile::IFilePersistence, m_paramsLoaderCompPtr);
150
151 I_REF(istdgui::IGuiObject, m_paramsSetGuiCompPtr);
152 I_REF(imod::IObserver, m_paramsSetObserverCompPtr);
153
154 I_ATTR(bool, m_connectParametersToEditorAttrPtr);
155
156 bool m_areParamsEditable;
157};
158
159
160// public methods
161
162template <class UI>
164 :m_paramsObserver(this),
165 m_statusObserver(this),
166 m_areParamsEditable(false)
167{
168 BaseClass::SetStatusIcon(QIcon(":/IcfIcons/StateUnknown"));
169 BaseClass::SetStatusText("Not yet processed");
170}
171
172
173// protected methods
174
175template <class UI>
177{
178 return m_areParamsEditable;
179}
180
181
182template <class UI>
184{
185 const iinsp::ISupplier* supplierPtr = BaseClass::GetObservedObject();
186 if ((supplierPtr != nullptr) && m_paramsLoaderCompPtr != nullptr) {
187 const iprm::IParamsSet* paramsPtr = supplierPtr->GetModelParametersSet();
188 if (paramsPtr != nullptr) {
189 if (m_paramsLoaderCompPtr->IsOperationSupported(
190 paramsPtr,
191 nullptr,
193 return true;
194 }
195 }
196 }
197
198 return false;
199}
200
201
202template <class UI>
204{
205 const iinsp::ISupplier* supplierPtr = BaseClass::GetObservedObject();
206 if ((supplierPtr != nullptr) && m_paramsLoaderCompPtr != nullptr) {
207 const iprm::IParamsSet* paramsPtr = supplierPtr->GetModelParametersSet();
208 if (paramsPtr != nullptr) {
209 if (m_paramsLoaderCompPtr->IsOperationSupported(
210 paramsPtr,
211 nullptr,
213 return true;
214 }
215 }
216 }
217
218 return false;
219}
220
221
222template <class UI>
224{
225 iinsp::ISupplier* supplierPtr = BaseClass::GetObservedObject();
226 if ((supplierPtr != nullptr) && m_paramsLoaderCompPtr != nullptr) {
227 iprm::IParamsSet* paramsPtr = supplierPtr->GetModelParametersSet();
228 if (paramsPtr != nullptr) {
229 auto loadState = m_paramsLoaderCompPtr->LoadFromFile(*paramsPtr, "");
230 if (loadState.WaitForFinished() == iproc::CTaskState::Status::Ready) {
231 return true;
232 }
233 else {
234 QMessageBox::warning(
235 BaseClass::GetQtWidget(),
236 QObject::tr("Error"),
237 QObject::tr("Cannot load parameters"));
238 }
239 }
240 }
241
242 return false;
243}
244
245
246template <class UI>
248{
249 const iinsp::ISupplier* supplierPtr = BaseClass::GetObservedObject();
250 if ((supplierPtr != nullptr) && m_paramsLoaderCompPtr != nullptr) {
251 const iprm::IParamsSet* paramsPtr = supplierPtr->GetModelParametersSet();
252 if (paramsPtr != nullptr) {
253 auto saveState = m_paramsLoaderCompPtr->SaveToFile(*paramsPtr, "");
254 if (saveState.WaitForFinished() != iproc::CTaskState::Status::Failed) {
255 return true;
256 }
257 else {
258 QMessageBox::warning(
259 BaseClass::GetQtWidget(),
260 QObject::tr("Error"),
261 QObject::tr("Cannot save parameters"));
262 }
263 }
264 }
265
266 return false;
267}
268
269
270template <class UI>
272{
273 iinsp::ISupplier* supplierPtr = BaseClass::GetObservedObject();
274 if (supplierPtr != nullptr) {
275 istd::CChangeGroup changeGroup(supplierPtr);
276
277 supplierPtr->InvalidateSupplier();
278 supplierPtr->EnsureWorkInitialized();
279 supplierPtr->EnsureWorkFinished();
280
281 return supplierPtr->GetWorkStatus() != iinsp::ISupplier::WS_FAILED;
282 }
283
284 return false;
285}
286
287
288template <class UI>
290{
291 QString statusText = "";
292 static QIcon unknownIcon(":/IcfIcons/StateUnknown");
293 static QIcon warningIcon(":/IcfIcons/StateWarning");
294 static QIcon invalidIcon(":/IcfIcons/StateInvalid");
295 static QIcon okIcon(":/IcfIcons/StateOk");
296
297 QIcon statusIcon = unknownIcon;
298
299 QString description;
300
301 const iinsp::ISupplier* supplierPtr = BaseClass::GetObservedObject();
302 if (supplierPtr != nullptr) {
303 const istd::IInformationProvider* infoProviderPtr = dynamic_cast<const istd::IInformationProvider*>(supplierPtr);
304 if (infoProviderPtr == nullptr) {
305 infoProviderPtr = CompCastPtr<const istd::IInformationProvider>(supplierPtr);
306 }
307
308 int category = (infoProviderPtr == nullptr) ? istd::IInformationProvider::IC_INFO : infoProviderPtr->GetInformationCategory();
309
310 int workStatus = supplierPtr->GetWorkStatus();
311
312 switch (workStatus) {
314 statusText = QObject::tr("Locked");
315 break;
316
318 switch (category) {
320 statusText = QObject::tr("Processing completed with warnings");
321 statusIcon = warningIcon;
322 break;
323
325 statusText = QObject::tr("Processing completed with errors");
326 statusIcon = invalidIcon;
327 break;
328
329 default:
330 statusText = QObject::tr("Processing completed without errors");
331 statusIcon = okIcon;
332 break;
333 }
334 break;
335
337 statusText = QObject::tr("Processing canceled by user");
338 break;
339
341 statusText = QObject::tr("Processing not possible");
342 statusIcon = invalidIcon;
343 break;
344
345 default:
346 break;
347 }
348 }
349
350 istd::CChangeNotifier visualStatusNotifier(&m_visualStatus);
351 BaseClass::SetStatusIcon(statusIcon);
352 BaseClass::SetStatusText(statusText);
353}
354
355
356template <class UI>
360
361
362template <class UI>
366
367
368// reimplemented (istdgui::TGuiObserverWrap)
369
370template <class UI>
372{
373 BaseClass::OnGuiModelAttached();
374
375 iinsp::ISupplier* supplierPtr = BaseClass::GetObservedObject();
376 Q_ASSERT(supplierPtr != nullptr); // model must be attached
377
378 iprm::IParamsSet* paramsPtr = const_cast<iprm::IParamsSet*>(supplierPtr->GetModelParametersSet());
379 imod::IModel* paramsModelPtr = dynamic_cast<imod::IModel*>(paramsPtr);
380
381 m_areParamsEditable = !*m_connectParametersToEditorAttrPtr;
382 bool areParamsAttachedToEditor = !*m_connectParametersToEditorAttrPtr;
383 QWidget* paramsWidget = GetParamsWidget();
384 if (paramsWidget != nullptr) {
385 if (m_paramsSetGuiCompPtr != nullptr) {
386 m_paramsSetGuiCompPtr->CreateGui(paramsWidget);
387 }
388
389 if (*m_connectParametersToEditorAttrPtr && (paramsModelPtr != nullptr) && m_paramsSetObserverCompPtr != nullptr) {
390 areParamsAttachedToEditor = paramsModelPtr->AttachObserver(m_paramsSetObserverCompPtr.get());
391 if (!areParamsAttachedToEditor) {
392 qWarning("Supplier parameters could not be connected to the editor");
393 }
394
395 m_areParamsEditable = true;
396
397
398 // Attach internal parameter observer:
399 paramsModelPtr->AttachObserver(&m_paramsObserver);
400 }
401
402 paramsWidget->setVisible(m_areParamsEditable);
403 paramsWidget->setEnabled(areParamsAttachedToEditor);
404 }
405
406 imod::IModel* statusModelPtr = supplierPtr->GetWorkStatusModel();
407 if (statusModelPtr != nullptr) {
408 statusModelPtr->AttachObserver(&m_statusObserver);
409 }
410
411 BaseClass::SetStatusIcon(QIcon(":/IcfIcons/StateUnknown"));
412}
413
414
415template <class UI>
417{
418 iinsp::ISupplier* supplierPtr = BaseClass::GetObservedObject();
419 Q_ASSERT(supplierPtr != nullptr); // model must be attached
420
421 iprm::IParamsSet* paramsPtr = const_cast<iprm::IParamsSet*>(supplierPtr->GetModelParametersSet());
422 imod::IModel* paramsModelPtr = dynamic_cast<imod::IModel*>(paramsPtr);
423
424 if (m_paramsSetObserverCompPtr != nullptr &&
425 (paramsModelPtr != nullptr) &&
426 paramsModelPtr->IsAttached(m_paramsSetObserverCompPtr.get())) {
427 paramsModelPtr->DetachObserver(m_paramsSetObserverCompPtr.get());
428 }
429
430 if (m_paramsSetGuiCompPtr != nullptr && m_paramsSetGuiCompPtr->IsGuiCreated()) {
431 m_paramsSetGuiCompPtr->DestroyGui();
432 }
433
434 m_areParamsEditable = false;
435
436 m_paramsObserver.EnsureModelDetached();
437
438 m_statusObserver.EnsureModelDetached();
439
440 BaseClass::OnGuiModelDetached();
441}
442
443
444// reimplemented (imod::IObserver)
445
446template <class UI>
448{
449 if (!BaseClass::IsGuiCreated() || changeSet.ContainsExplicit(istd::IChangeable::CF_DESTROYING)) {
450 BaseClass::AfterModelChange(modelPtr, changeSet);
451
452 return;
453 }
454
455 UpdateVisualStatus();
456
457 BeforeSupplierGuiUpdated();
458
459 BaseClass::AfterModelChange(modelPtr, changeSet);
460}
461
462
463// public methods of embedded class ParamsObserver
464
465template <class UI>
467 :m_parent(*parentPtr)
468{
469 Q_ASSERT(parentPtr != nullptr);
470}
471
472
473// reimplemented (imod::CSingleModelObserverBase)
474
475template <class UI>
477{
478 m_parent.OnSupplierParamsChanged();
479}
480
481
482// public methods of embedded class StatusObserver
483
484template <class UI>
486 :m_parent(*parentPtr)
487{
488 Q_ASSERT(parentPtr != nullptr);
489}
490
491
492// reimplemented (imod::CSingleModelObserverBase)
493
494template <class UI>
496{
497 m_parent.UpdateVisualStatus();
498}
499
500
501} // namespace iinspgui
502
503
504#endif // !iqtinsp_TCommonSupplierGuiCompBase_included
505
506
Dest * CompCastPtr(istd::IPolymorphic *objectPtr)
Cast to specified interface trying to use component interface query.
Interface providing loading and saving of data objects.
@ QF_ANONYMOUS
Operations without specified path (anonymous) should be considered.
@ QF_LOAD
Check for load operation.
@ QF_SAVE
Check for save operation.
Base interface for suppliers providing calculating of objects on demand ("pull data" model).
Definition ISupplier.h:28
virtual void InvalidateSupplier()=0
Called to signalize that this supplier is invalid.
virtual int GetWorkStatus() const =0
Get status of last work.
virtual imod::IModel * GetWorkStatusModel() const =0
Get access to the status model of the supplier.
@ WS_OK
Work was done correctly and no error occure.
Definition ISupplier.h:47
@ WS_CANCELED
Work was canceled.
Definition ISupplier.h:51
@ WS_LOCKED
Supplier is locked becouse it is doing processing step.
Definition ISupplier.h:43
@ WS_FAILED
Work couldn't be done.
Definition ISupplier.h:55
virtual iprm::IParamsSet * GetModelParametersSet() const =0
Get parameter set using by this supplier.
virtual void EnsureWorkInitialized()=0
Force the supplier to initialize its work.
virtual void EnsureWorkFinished()=0
Ensure that all objects are produced.
void OnUpdate(const istd::IChangeable::ChangeSet &changeSet) override
Called on update of observed model.
void OnUpdate(const istd::IChangeable::ChangeSet &changeSet) override
Called on update of observed model.
void AfterModelChange(imod::IModel *modelPtr, const istd::IChangeable::ChangeSet &changeSet) override
This function will be called after update of the observer contents occures.
virtual void OnSupplierParamsChanged()
Method will be called every time if the parameter set of the supplier has been changed.
bool AreParamsEditable() const
Check, if parameters are correct connected to GUI and can be editable.
virtual QWidget * GetParamsWidget() const =0
Get parameters widget object.
virtual void UpdateVisualStatus()
Update visual status of supplier state.
bool IsLoadParamsSupported() const
Check if parameter loading is supported.
bool LoadParams()
Load parameters from external source (file).
istdgui::TDesignerGuiObserverCompBase< UI, iinsp::ISupplier > BaseClass
virtual void BeforeSupplierGuiUpdated()
This method will be called after the supplier's output has been changed and new results are available...
bool IsSaveParamsSupported() const
Check if parameter storing is supported.
void OnGuiModelDetached() override
Called when model is detached or GUI is destroyed.
void OnGuiModelAttached() override
Called when model is attached and GUI is created.
bool SaveParams()
Save parameters for example to file.
Basic implementation for a single model observer.
void EnsureModelDetached()
Make sure this observer is detached.
Common interface for model objects, that supports Model/Observer design pattern.
Definition IModel.h:29
virtual void DetachObserver(IObserver *observerPtr)=0
Detaches model object from observer observerPtr.
virtual bool AttachObserver(IObserver *observerPtr)=0
Attaches model object to observer observerPtr.
virtual bool IsAttached(const IObserver *observerPtr) const =0
Returns true if observer observer is attached to this model object.
Common interface for all classes implementing the Observer functionality in the Model/Observer design...
Definition IObserver.h:30
Set of general parameters.
Definition IParamsSet.h:29
Help class which provides the group of changes for update mechanism of the model.
Help class which provides the automatic update mechanism of the model.
Set of change flags (its IDs).
Definition IChangeable.h:38
bool ContainsExplicit(int changeId, bool singleOnly=false) const
Check if there is specific change flag in the set explicit set by user.
@ CF_DESTROYING
Change flag indicate that model is during destruction.
Common interface for class providing some basic information object.
@ IC_ERROR
Information about error, processing could not be done correctly.
@ IC_INFO
Normal information level, can be also interpreted as OK.
@ IC_WARNING
Information about warning, processing could be done.
virtual InformationCategory GetInformationCategory() const =0
Get category of the information.
void SetStatusText(const QString &text)
Set status text;.
imod::TModelWrap< VisualStatus > m_visualStatus
void SetStatusIcon(const QIcon &icon)
Set status icon;.
Common interface for GUI objects using in component context.
Definition IGuiObject.h:27
Base class for all Qt GUI components.
Contains Qt based implementations of inspection classes and components.

© Witold Gantzke and Kirill Lepskiy