TSupplierGuiCompBase.h
Go to the documentation of this file.
1 #ifndef iqtinsp_TSupplierGuiCompBase_included
2 #define iqtinsp_TSupplierGuiCompBase_included
3 
4 
5 // Qt includes
6 #include<QtCore/QtGlobal>
7 #include<QtCore/QMap>
8 #if QT_VERSION >= 0x050000
9 #include <QtWidgets/QMessageBox>
10 #else
11 #include <QtGui/QMessageBox>
12 #endif
13 
14 // ACF includes
15 #include <istd/CChangeNotifier.h>
16 #include <istd/CChangeGroup.h>
17 #include <istd/TDelPtr.h>
18 #include <ifile/IFilePersistence.h>
19 #include <imod/IObserver.h>
20 #include <iprm/IParamsSet.h>
22 #include <iinsp/ISupplier.h>
23 #include <iqtgui/IGuiObject.h>
26 
27 // ACF-Solutions includes
28 #include <iqtinsp/iqtinsp.h>
29 
30 
31 namespace iqtinsp
32 {
33 
34 
35 template <class UI>
38  iqtgui::TDesignerGuiObserverCompBase<UI, iinsp::ISupplier> >
39 {
40 public:
43 
44  I_BEGIN_BASE_COMPONENT(TSupplierGuiCompBase);
45  I_ASSIGN(m_paramsLoaderCompPtr, "ParamsLoader", "Loads and saves parameters from and to file", false, "ParamsLoader");
46  I_ASSIGN(m_paramsSetGuiCompPtr, "ParamsSetGui", "Shows parameter set", false, "ParamsSetGui");
47  I_ASSIGN_TO(m_paramsSetObserverCompPtr, m_paramsSetGuiCompPtr, false);
48  I_ASSIGN_TO(m_paramsSetExtenderCompPtr, m_paramsSetGuiCompPtr, false);
49  I_ASSIGN(m_connectParametersToEditorAttrPtr, "ConnectParametersToEditor", "If enabled, the parameter set of the supplier will be connected to the parameter editor", true, true);
50  I_ASSIGN(m_viewCalibrationModeAttrPtr, "ViewCalibrationMode", "Control when calibration from supplier will be set to view\n\t0 - never use calibration\n\t1 - only indirect (used as slave)\n\t2 - allways set calibration if available", true, 0);
51  I_END_COMPONENT;
52 
54 
55  // reimplemented (iqt2d::IViewExtender)
56  virtual void AddItemsToScene(iqt2d::IViewProvider* providerPtr, int flags);
57  virtual void RemoveItemsFromScene(iqt2d::IViewProvider* providerPtr);
58 
59 protected:
60  typedef typename BaseClass::Shapes Shapes;
61 
62  using BaseClass::GetObservedObject;
63 
67  bool AreParamsEditable() const;
68 
72  bool IsLoadParamsSupported() const;
73 
77  bool IsSaveParamsSupported() const;
78 
82  bool LoadParams();
83 
87  bool SaveParams();
88 
92  bool DoTest();
93 
97  virtual void UpdateVisualStatus();
98 
103 
108  virtual void OnSupplierParamsChanged();
109 
110  // reimplemented (iqtgui::TGuiObserverWrap)
111  virtual void OnGuiModelAttached();
112  virtual void OnGuiModelDetached();
113 
114  // reimplemented (iqt2d::TViewExtenderCompBase)
115  virtual void CreateShapes(int sceneId, Shapes& result);
116 
117  // reimplemented (imod::IObserver)
118  virtual void AfterUpdate(imod::IModel* modelPtr, const istd::IChangeable::ChangeSet& changeSet);
119 
120  // abstract methods
126  virtual QWidget* GetParamsWidget() const = 0;
127 
128 protected:
130  {
131  public:
133 
134  using imod::CSingleModelObserverBase::EnsureModelDetached;
135 
136  protected:
137  // reimplemented (imod::CSingleModelObserverBase)
138  virtual void OnUpdate(const istd::IChangeable::ChangeSet& changeSet);
139 
140  private:
141  TSupplierGuiCompBase& m_parent;
142  };
143 
145  {
146  public:
148 
149  using imod::CSingleModelObserverBase::EnsureModelDetached;
150 
151  protected:
152  // reimplemented (imod::CSingleModelObserverBase)
153  virtual void OnUpdate(const istd::IChangeable::ChangeSet& changeSet);
154 
155  private:
156  TSupplierGuiCompBase& m_parent;
157  };
158 
161 
162 protected:
163  using BaseClass::m_visualStatus;
164 
165 private:
169  enum ViewCalibrationMode
170  {
174  VCM_NONE,
178  VCM_INDIRECT,
182  VCM_ALWAYS
183  };
184 
185  I_REF(ifile::IFilePersistence, m_bitmapLoaderCompPtr);
186  I_REF(ifile::IFilePersistence, m_paramsLoaderCompPtr);
187 
188  I_REF(iqtgui::IGuiObject, m_paramsSetGuiCompPtr);
189  I_REF(imod::IObserver, m_paramsSetObserverCompPtr);
190  I_REF(iqt2d::IViewExtender, m_paramsSetExtenderCompPtr);
191 
192  I_ATTR(bool, m_connectParametersToEditorAttrPtr);
193  I_ATTR(int, m_viewCalibrationModeAttrPtr);
194 
195  bool m_areParamsEditable;
196  typedef QMap<const iqt2d::IViewProvider*, istd::TDelPtr<i2d::ICalibration2d> > ProviderToCalibrationMap;
197  ProviderToCalibrationMap m_providerToCalibrationMap;
198 };
199 
200 
201 // public methods
202 
203 template <class UI>
205  :m_paramsObserver(this),
206  m_statusObserver(this),
207  m_areParamsEditable(false)
208 {
209  BaseClass::SetStatusIcon(QIcon(":/Icons/StateUnknown"));
210  BaseClass::SetStatusText("Not yet processed");
211 }
212 
213 
214 // reimplemented (iqt2d::IViewExtender)
215 
216 template <class UI>
218 {
219  if (*m_viewCalibrationModeAttrPtr > VCM_NONE){
220  if ((*m_viewCalibrationModeAttrPtr >= VCM_ALWAYS) || ((flags & iqt2d::IViewExtender::SF_DIRECT) == 0)){
221  const i2d::ICalibrationProvider* calibrationProviderPtr = ExtractCalibrationProvider();
222 
223  iview::CCalibratedViewBase* viewPtr = dynamic_cast<iview::CCalibratedViewBase*>(providerPtr->GetView());
224 
225  if ((calibrationProviderPtr != NULL) && (viewPtr != NULL)){
226  istd::TDelPtr<i2d::ICalibration2d> calibrationCopyPtr;
227 
228  const i2d::ICalibration2d* calibrationPtr = calibrationProviderPtr->GetCalibration();
229  if (calibrationPtr != NULL){
230  calibrationCopyPtr.SetCastedOrRemove(calibrationPtr->CloneMe());
231  }
232 
233  viewPtr->SetDisplayCalibration(calibrationCopyPtr.GetPtr());
234 
235  m_providerToCalibrationMap[providerPtr].TakeOver(calibrationCopyPtr);
236  }
237  }
238  }
239 
240  BaseClass::AddItemsToScene(providerPtr, flags);
241 
242  if ((flags & iqt2d::IViewExtender::SF_DIRECT) != 0){
243  if (m_paramsSetExtenderCompPtr.IsValid()){
244  m_paramsSetExtenderCompPtr->AddItemsToScene(providerPtr, flags);
245  }
246  }
247 }
248 
249 
250 template <class UI>
252 {
253  if ( m_providerToCalibrationMap.contains(providerPtr) &&
254  (*m_viewCalibrationModeAttrPtr >= VCM_ALWAYS)){
255  iview::CCalibratedViewBase* viewPtr = dynamic_cast<iview::CCalibratedViewBase*>(providerPtr->GetView());
256  const i2d::ICalibration2d* calibrationPtr = m_providerToCalibrationMap[providerPtr].GetPtr();
257 
258  if ((viewPtr != NULL) && (calibrationPtr != NULL) && (viewPtr->GetCalibration() == calibrationPtr)){
259  viewPtr->SetDisplayCalibration(NULL);
260  }
261  }
262 
263  m_providerToCalibrationMap.remove(providerPtr);
264 
265  if (m_paramsSetExtenderCompPtr.IsValid()){
266  m_paramsSetExtenderCompPtr->RemoveItemsFromScene(providerPtr);
267  }
268 
269  BaseClass::RemoveItemsFromScene(providerPtr);
270 }
271 
272 
273 // protected methods
274 
275 template <class UI>
277 {
278  return m_areParamsEditable;
279 }
280 
281 
282 template <class UI>
284 {
285  const iinsp::ISupplier* supplierPtr = BaseClass::GetObservedObject();
286  if ((supplierPtr != NULL) && m_paramsLoaderCompPtr.IsValid()){
287  const iprm::IParamsSet* paramsPtr = supplierPtr->GetModelParametersSet();
288  if (paramsPtr != NULL){
289  if (m_paramsLoaderCompPtr->IsOperationSupported(
290  paramsPtr,
291  NULL,
292  ifile::IFilePersistence::QF_LOAD | ifile::IFilePersistence::QF_ANONYMOUS)){
293  return true;
294  }
295  }
296  }
297 
298  return false;
299 }
300 
301 
302 template <class UI>
304 {
305  const iinsp::ISupplier* supplierPtr = BaseClass::GetObservedObject();
306  if ((supplierPtr != NULL) && m_paramsLoaderCompPtr.IsValid()){
307  const iprm::IParamsSet* paramsPtr = supplierPtr->GetModelParametersSet();
308  if (paramsPtr != NULL){
309  if (m_paramsLoaderCompPtr->IsOperationSupported(
310  paramsPtr,
311  NULL,
312  ifile::IFilePersistence::QF_SAVE | ifile::IFilePersistence::QF_ANONYMOUS)){
313  return true;
314  }
315  }
316  }
317 
318  return false;
319 }
320 
321 
322 template <class UI>
324 {
325  iinsp::ISupplier* supplierPtr = BaseClass::GetObservedObject();
326  if ((supplierPtr != NULL) && m_paramsLoaderCompPtr.IsValid()){
327  iprm::IParamsSet* paramsPtr = supplierPtr->GetModelParametersSet();
328  if (paramsPtr != NULL){
329  if (m_paramsLoaderCompPtr->LoadFromFile(*paramsPtr, "") != ifile::IFilePersistence::OS_FAILED){
330  return true;
331  }
332  else{
333  QMessageBox::warning(
334  BaseClass::GetQtWidget(),
335  QObject::tr("Error"),
336  QObject::tr("Cannot load parameters"));
337  }
338  }
339  }
340 
341  return false;
342 }
343 
344 
345 template <class UI>
347 {
348  const iinsp::ISupplier* supplierPtr = BaseClass::GetObservedObject();
349  if ((supplierPtr != NULL) && m_paramsLoaderCompPtr.IsValid()){
350  const iprm::IParamsSet* paramsPtr = supplierPtr->GetModelParametersSet();
351  if (paramsPtr != NULL){
352  if (m_paramsLoaderCompPtr->SaveToFile(*paramsPtr, "") != ifile::IFilePersistence::OS_FAILED){
353  return true;
354  }
355  else{
356  QMessageBox::warning(
357  BaseClass::GetQtWidget(),
358  QObject::tr("Error"),
359  QObject::tr("Cannot save parameters"));
360  }
361  }
362  }
363 
364  return false;
365 }
366 
367 
368 template <class UI>
370 {
371  iinsp::ISupplier* supplierPtr = BaseClass::GetObservedObject();
372  if (supplierPtr != NULL){
373  istd::CChangeGroup changeGroup(supplierPtr);
374 
375  supplierPtr->InvalidateSupplier();
376  supplierPtr->EnsureWorkInitialized();
377  supplierPtr->EnsureWorkFinished();
378 
379  return supplierPtr->GetWorkStatus() != iinsp::ISupplier::WS_FAILED;
380  }
381 
382  return false;
383 }
384 
385 
386 template <class UI>
388 {
389  QString statusText = "";
390  QIcon statusIcon = QIcon(":/Icons/StateUnknown");
391 
392  QString description;
393 
394  const iinsp::ISupplier* supplierPtr = BaseClass::GetObservedObject();
395  if (supplierPtr != NULL){
396  const istd::IInformationProvider* infoProviderPtr = dynamic_cast<const istd::IInformationProvider*>(supplierPtr);
397  if (infoProviderPtr == NULL){
398  infoProviderPtr = CompCastPtr<const istd::IInformationProvider>(supplierPtr);
399  }
400 
401  int category = (infoProviderPtr == NULL) ? istd::IInformationProvider::IC_INFO : infoProviderPtr->GetInformationCategory();
402 
403  int workStatus = supplierPtr->GetWorkStatus();
404 
405  switch (workStatus){
407  statusText = QObject::tr("Locked");
408  break;
409 
411  switch (category){
412  case istd::IInformationProvider::IC_WARNING:
413  statusText = QObject::tr("Processing completed with warnings");
414  statusIcon = QIcon(":/Icons/StateWarning");
415  break;
416 
417  case istd::IInformationProvider::IC_ERROR:
418  statusText = QObject::tr("Processing completed with errors");
419  statusIcon = QIcon(":/Icons/StateInvalid");
420  break;
421 
422  default:
423  statusText = QObject::tr("Processing completed without errors");
424  statusIcon = QIcon(":/Icons/StateOk");
425  break;
426  }
427  break;
428 
430  statusText = QObject::tr("Processing canceled by user");
431  break;
432 
434  statusText = QObject::tr("Processing not possible");
435  statusIcon = QIcon(":/Icons/Error");
436  break;
437 
438  default:
439  break;
440  }
441  }
442 
443  istd::CChangeNotifier visualStatusNotifier(&m_visualStatus);
444  BaseClass::SetStatusIcon(statusIcon);
445  BaseClass::SetStatusText(statusText);
446 }
447 
448 template <class UI>
450 {
451  const i2d::ICalibrationProvider* calibrationProviderPtr = dynamic_cast<const i2d::ICalibrationProvider*>(GetObservedObject());
452  if (calibrationProviderPtr == NULL){
453  calibrationProviderPtr = CompCastPtr<const i2d::ICalibrationProvider>(GetObservedObject());
454  }
455 
456  return calibrationProviderPtr;
457 }
458 
459 
460 template <class UI>
462 {
463 }
464 
465 
466 // reimplemented (iqtgui::TGuiObserverWrap)
467 
468 template <class UI>
470 {
471  BaseClass::OnGuiModelAttached();
472 
473  iinsp::ISupplier* supplierPtr = BaseClass::GetObservedObject();
474  Q_ASSERT(supplierPtr != NULL); // model must be attached
475 
476  iprm::IParamsSet* paramsPtr = const_cast<iprm::IParamsSet*>(supplierPtr->GetModelParametersSet());
477  imod::IModel* paramsModelPtr = dynamic_cast<imod::IModel*>(paramsPtr);
478 
479  m_areParamsEditable = !*m_connectParametersToEditorAttrPtr;
480  bool areParamsAttachedToEditor = !*m_connectParametersToEditorAttrPtr;
481  QWidget* paramsWidget = GetParamsWidget();
482  if (paramsWidget != NULL){
483  if (m_paramsSetGuiCompPtr.IsValid()){
484  m_paramsSetGuiCompPtr->CreateGui(paramsWidget);
485  }
486 
487  if (*m_connectParametersToEditorAttrPtr && (paramsModelPtr != NULL) && m_paramsSetObserverCompPtr.IsValid()){
488  areParamsAttachedToEditor = paramsModelPtr->AttachObserver(m_paramsSetObserverCompPtr.GetPtr());
489  if (!areParamsAttachedToEditor){
490  qWarning("Supplier parameters could not be connected to the editor");
491  }
492 
493  m_areParamsEditable = true;
494 
495 
496  // Attach internal parameter observer:
497  paramsModelPtr->AttachObserver(&m_paramsObserver);
498  }
499 
500  paramsWidget->setVisible(m_areParamsEditable);
501  paramsWidget->setEnabled(areParamsAttachedToEditor);
502  }
503 
504  imod::IModel* statusModelPtr = supplierPtr->GetWorkStatusModel();
505  if (statusModelPtr != NULL){
506  statusModelPtr->AttachObserver(&m_statusObserver);
507  }
508 
509  BaseClass::SetStatusIcon(QIcon(":/Icons/StateUnknown"));
510 }
511 
512 
513 template <class UI>
515 {
516  iinsp::ISupplier* supplierPtr = BaseClass::GetObservedObject();
517  Q_ASSERT(supplierPtr != NULL); // model must be attached
518 
519  iprm::IParamsSet* paramsPtr = const_cast<iprm::IParamsSet*>(supplierPtr->GetModelParametersSet());
520  imod::IModel* paramsModelPtr = dynamic_cast<imod::IModel*>(paramsPtr);
521 
522  if ( m_paramsSetObserverCompPtr.IsValid() &&
523  (paramsModelPtr != NULL) &&
524  paramsModelPtr->IsAttached(m_paramsSetObserverCompPtr.GetPtr())){
525  paramsModelPtr->DetachObserver(m_paramsSetObserverCompPtr.GetPtr());
526  }
527 
528  if (m_paramsSetGuiCompPtr.IsValid() && m_paramsSetGuiCompPtr->IsGuiCreated()){
529  m_paramsSetGuiCompPtr->DestroyGui();
530  }
531 
532  m_areParamsEditable = false;
533 
534  m_paramsObserver.EnsureModelDetached();
535 
536  m_statusObserver.EnsureModelDetached();
537 
538  BaseClass::OnGuiModelDetached();
539 }
540 
541 
542 // reimplemented (iqt2d::TViewExtenderCompBase)
543 
544 template <class UI>
545 void TSupplierGuiCompBase<UI>::CreateShapes(int /*sceneId*/, Shapes& /*result*/)
546 {
547 }
548 
549 
550 // reimplemented (imod::IObserver)
551 
552 template <class UI>
554 {
555  if (!BaseClass::IsGuiCreated() || changeSet.ContainsExplicit(istd::IChangeable::CF_DESTROYING)){
556  BaseClass::AfterUpdate(modelPtr, changeSet);
557 
558  return;
559  }
560 
561  UpdateVisualStatus();
562 
563  if (*m_viewCalibrationModeAttrPtr >= VCM_ALWAYS){
564  const i2d::ICalibrationProvider* calibrationProviderPtr = ExtractCalibrationProvider();
565 
566  for ( ProviderToCalibrationMap::Iterator viewIter = m_providerToCalibrationMap.begin();
567  viewIter != m_providerToCalibrationMap.end();
568  ++viewIter){
569  const iqt2d::IViewProvider* providerPtr = viewIter.key();
570  iview::CCalibratedViewBase* viewPtr = dynamic_cast<iview::CCalibratedViewBase*>(providerPtr->GetView());
571 
572  if ((calibrationProviderPtr != NULL) && (viewPtr != NULL)){
573  istd::TDelPtr<i2d::ICalibration2d> calibrationCopyPtr;
574 
575  const i2d::ICalibration2d* calibrationPtr = calibrationProviderPtr->GetCalibration();
576  if (calibrationPtr != NULL){
577  const i2d::ICalibration2d* viewCalibrationPtr = viewIter.value().GetPtr();
578 
579  if ((viewCalibrationPtr != NULL) && viewCalibrationPtr->IsEqual(*calibrationPtr)){
580  continue;
581  }
582 
583  calibrationCopyPtr.SetCastedOrRemove(calibrationPtr->CloneMe());
584  }
585 
586  viewPtr->SetDisplayCalibration(calibrationCopyPtr.GetPtr());
587 
588  viewIter.value().TakeOver(calibrationCopyPtr);
589  }
590  }
591  }
592 
593  BaseClass::AfterUpdate(modelPtr, changeSet);
594 }
595 
596 
597 // public methods of embedded class ParamsObserver
598 
599 template <class UI>
601  :m_parent(*parentPtr)
602 {
603  Q_ASSERT(parentPtr != NULL);
604 }
605 
606 
607 // reimplemented (imod::CSingleModelObserverBase)
608 
609 template <class UI>
611 {
612  m_parent.OnSupplierParamsChanged();
613 }
614 
615 
616 // public methods of embedded class StatusObserver
617 
618 template <class UI>
620  :m_parent(*parentPtr)
621 {
622  Q_ASSERT(parentPtr != NULL);
623 }
624 
625 
626 // reimplemented (imod::CSingleModelObserverBase)
627 
628 template <class UI>
630 {
631  m_parent.UpdateVisualStatus();
632 }
633 
634 
635 } // namespace iqtinsp
636 
637 
638 #endif // !iqtinsp_TSupplierGuiCompBase_included
639 
640 
virtual const i2d::ICalibrationProvider * ExtractCalibrationProvider() const
Extract calibration provider from the observed object.
virtual void AddItemsToScene(iqt2d::IViewProvider *providerPtr, int flags)
Supplier is locked becouse it is doing processing step.
Definition: ISupplier.h:38
Work was done correctly and no error occure.
Definition: ISupplier.h:42
ParamsObserver(TSupplierGuiCompBase *parentPtr)
virtual bool IsAttached(const IObserver *observerPtr) const =0
virtual void UpdateVisualStatus()
Update visual status of supplier state.
iqt2d::TViewExtenderCompBase< iqtgui::TDesignerGuiObserverCompBase< UI, iinsp::ISupplier > > BaseClass
bool LoadParams()
Load parameters from external source (file).
virtual void CreateShapes(int sceneId, Shapes &result)
virtual void EnsureWorkFinished()=0
Ensure that all objects are produced.
virtual InformationCategory GetInformationCategory() const =0
void SetDisplayCalibration(const i2d::ICalibration2d *calibrationPtr)
bool AreParamsEditable() const
Check, if parameters are correct connected to GUI and can be editable.
bool IsSaveParamsSupported() const
Check if parameter storing is supported.
virtual void AfterUpdate(imod::IModel *modelPtr, const istd::IChangeable::ChangeSet &changeSet)
Type * GetPtr() const
virtual int GetWorkStatus() const =0
Get status of last work.
virtual void RemoveItemsFromScene(iqt2d::IViewProvider *providerPtr)
virtual const i2d::ICalibration2d * GetCalibration() const
Work couldn't be done.
Definition: ISupplier.h:50
Work was canceled.
Definition: ISupplier.h:46
virtual iview::IShapeView * GetView() const =0
virtual iprm::IParamsSet * GetModelParametersSet() const =0
Get parameter set using by this supplier.
#define NULL
virtual void InvalidateSupplier()=0
Called to signalize that this supplier is invalid.
virtual void OnSupplierParamsChanged()
Method will be called every time if the parameter set of the supplier has been changed.
virtual bool AttachObserver(IObserver *observerPtr)=0
virtual QWidget * GetParamsWidget() const =0
Get parameters widget object.
bool ContainsExplicit(int changeId, bool singleOnly=false) const
bool DoTest()
Do inspection test.
virtual void OnUpdate(const istd::IChangeable::ChangeSet &changeSet)
Base interface for suppliers providing calculating of objects on demand ("pull data" model)...
Definition: ISupplier.h:22
virtual void EnsureWorkInitialized()=0
Force the supplier to initialize its work.
StatusObserver(TSupplierGuiCompBase *parentPtr)
bool SaveParams()
Save parameters for example to file.
virtual void OnUpdate(const istd::IChangeable::ChangeSet &changeSet)
virtual const ICalibration2d * GetCalibration() const =0
bool IsLoadParamsSupported() const
Check if parameter loading is supported.
virtual void DetachObserver(IObserver *observerPtr)=0
virtual imod::IModel * GetWorkStatusModel() const =0
Get access to the status model of the supplier.
bool SetCastedOrRemove(SourceType *ptr)

© 2007-2017 Witold Gantzke and Kirill Lepskiy