Wtx ~ Wt Extension Library
WtxLib
TemplateFormView.h
1 /**************************************************************************
2 ###########################################################################
3 ##
4 ## $SHOWOFFDB_BEGIN_LICENSE$
5 ## Copyright (C) 2011 Lorimark Solutions, LLC and/or its subsidiary(-ies).
6 ## All rights reserved.
7 ## Contact: Lorimark Solutions, LLC (info@showoff-db.org)
8 ##
9 ## This file is part of the Showoff Database Application Framework.
10 ##
11 ## Commercial Usage
12 ## Licensees holding valid ShowoffDB Commercial licenses may use this file in
13 ## accordance with the ShowoffDB Commercial License Agreement provided with the
14 ## Software or, alternatively, in accordance with the terms contained in
15 ## a written agreement between you and Lorimark Solutions, LLC.
16 ##
17 ## GNU Lesser General Public License Usage
18 ## Alternatively, this file may be used under the terms of the GNU Lesser
19 ## General Public License version 2.1 as published by the Free Software
20 ## Foundation and appearing in the file LICENSE.LGPL included in the
21 ## packaging of this file. Please review the following information to
22 ## ensure the GNU Lesser General Public License version 2.1 requirements
23 ## will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ##
25 ## In addition, as a special exception, Lorimark Solutions, LLC gives
26 ## you certain additional rights. These rights are described in the
27 ## Lorimark Solutions, LLC ShowoffDB LGPL Exception version 1.0, included in
28 ## the file LGPL_EXCEPTION.txt in this package.
29 ##
30 ## GNU General Public License Usage
31 ## Alternatively, this file may be used under the terms of the GNU
32 ## General Public License version 3.0 as published by the Free Software
33 ## Foundation and appearing in the file LICENSE.GPL included in the
34 ## packaging of this file. Please review the following information to
35 ## ensure the GNU General Public License version 3.0 requirements will be
36 ## met: http://www.gnu.org/copyleft/gpl.html.
37 ##
38 ## If you have questions regarding the use of this file, please contact
39 ## Lorimark Solutions, LLC at info@showoff-db.org.
40 ## $SHOWOFFDB_END_LICENSE$
41 ##
42 #############################################################################
43 ****************************************************************************/
44 
45 
46 #ifndef __WTX_DBO_TEMPLATEFORMVIEW_H_234e0174_6b7d_11e8_b503_fb1ef3651ec0__
47 #define __WTX_DBO_TEMPLATEFORMVIEW_H_234e0174_6b7d_11e8_b503_fb1ef3651ec0__
48 
49 #include <Wt/WFormWidget.h>
50 
51 #include <Wtx/Dbo/FieldDefBase.h>
52 #include <Wtx/Dbo/FormModel.h>
53 #include <Wtx/TemplateFormView.h>
54 
55 namespace Wtx {
56  namespace Dbo {
57 
58 template <class C> class TemplateFormView
59 : public Wtx::TemplateFormView
60 {
61 
62  public:
63 
65  : public Wt::WObject
66  {
67  public:
68 
69  FormWidgetWrapper( Wt::WFormModel * model, Wt::WFormModel::Field field, Wt::WFormWidget * widget )
70  : Wt::WObject(),
71  m_model(model),
72  m_field(field),
73  m_widget(widget)
74  {
75  widget-> focussed().connect( this, &FormWidgetWrapper::on_focussed );
76  widget-> blurred() .connect( this, &FormWidgetWrapper::on_blurred );
77  widget-> clicked() .connect( this, &FormWidgetWrapper::on_clicked );
78  m_prevValue = widget-> valueText().toUTF8();
79  }
80 
81  Wt::Signal<FormWidgetWrapper*> & focussed()
82  {
83  return m_focussed;
84  }
85 
86  Wt::Signal<FormWidgetWrapper*> & blurred()
87  {
88  return m_blurred;
89  }
90 
91  Wt::Signal<FormWidgetWrapper*> & clicked()
92  {
93  return m_clicked;
94  }
95 
96  Wt::WFormModel::Field field() const
97  {
98  return m_field;
99  }
100 
101  std::string fieldName() const
102  {
103  return std::string( m_field );
104  }
105 
106  Wt::WFormWidget * widget() const
107  {
108  return m_widget;
109  }
110 
111  const std::string & prevValue() const
112  {
113  return m_prevValue;
114  }
115 
116  bool isChanged() const
117  {
118  return m_prevValue != widget()-> valueText();
119  }
120 
121  bool is( Wtx::Dbo::FieldDefBase * fieldDef )
122  {
123  return fieldDef-> fieldName() == fieldName();
124  }
125 
126  bool is( Wtx::Dbo::FieldDefBase & fieldDef )
127  {
128  return fieldDef.fieldName() == fieldName();
129  }
130 
131  void resetChangedFlags()
132  {
133  m_prevValue = widget()-> valueText().toUTF8();
134  }
135 
136  Wt::WFormModel * model() const
137  {
138  return m_model;
139  }
140 
141  private:
142 
143  void on_focussed()
144  {
145  m_focussed.emit(this);
146  }
147 
148  void on_blurred()
149  {
150  m_blurred.emit(this);
151  }
152 
153  void on_clicked()
154  {
155  m_clicked.emit(this);
156  }
157 
158  Wt::WFormModel * m_model = nullptr;
159  Wt::WFormModel::Field m_field = nullptr;
160  Wt::WFormWidget * m_widget = nullptr;
161 
162  Wt::Signal<FormWidgetWrapper*> m_focussed;
163  Wt::Signal<FormWidgetWrapper*> m_blurred;
164  Wt::Signal<FormWidgetWrapper*> m_clicked;
165 
166  std::string m_prevValue;
167 
168  }; // endclass FormWidgetWrapper
169 
170  TemplateFormView( const Wt::WString & text )
171  : Wtx::TemplateFormView(text)
172  {
173  }
174 
179  TemplateFormView( const Wt::WString & text, std::unique_ptr< Wtx::Dbo::FormModel<C> > model )
180  : Wtx::TemplateFormView(text),
181  m_formModel( std::move(model) )
182  {
183 // COUT2_LINE;
184  }
185 
195  {
196  return m_formModel.get();
197  }
198 
203  void setFormModel( std::unique_ptr< Wtx::Dbo::FormModel<C> > model )
204  {
205  m_formModel = std::move(model);
206  }
207 
212  Wtx::Dbo::FormModel<C> * formModel( Wt::WFormModel * model )
213  {
214  return dynamic_cast<Wtx::Dbo::FormModel<C>*>( model );
215  }
216 
221  Wtx::Dbo::FieldDefBase * fieldDef( Wt::WFormModel * model, Wt::WFormModel::Field field )
222  {
223  if( auto wtxModel = formModel(model) )
224  return wtxModel-> tableDef().fieldDef( field );
225 
226  return nullptr;
227  }
228 
236  virtual void on_focussed( FormWidgetWrapper * fww )
237  {
238 // std::cout << __FILE__ << ":" << __LINE__ << " on_focussed:" << fww-> fieldName() << std::endl;
239  }
240 
248  virtual void on_blurred( FormWidgetWrapper * fww )
249  {
250 // std::cout << __FILE__ << ":" << __LINE__ << " on_blurred:" << fww-> fieldName() << std::endl;
251  }
252 
257  virtual void on_clicked( FormWidgetWrapper * fww )
258  {
259 // std::cout << __FILE__ << ":" << __LINE__ << " " << std::endl;
260 
261  }
262 
267  std::unique_ptr<Wt::WWidget> createFormWidget( Wt::WFormModel::Field field )
268  {
269 // std::cout << __FILE__ << ":" << __LINE__ << " " << field << std::endl;
270 
271  auto retVal = createEditWidget( field );
272 
273  auto fw = dynamic_cast<Wt::WFormWidget*>(retVal.get());
274 
275  if( retVal && formModel() && fw )
276  {
277  auto fww = std::make_unique<FormWidgetWrapper>( formModel(), field, fw );
278  fww-> focussed().connect( this, &TemplateFormView<C>::on_focussed );
279  fww-> blurred() .connect( this, &TemplateFormView<C>::on_blurred );
280  fww-> clicked() .connect( this, &TemplateFormView<C>::on_clicked );
281  m_formWidgets.push_back(std::move(fww));
282  }
283 
284  return retVal;
285  }
286 
287  void resetEditWidgetChangedFlags()
288  {
289  for( auto & fww : m_formWidgets )
290  {
291  fww-> resetChangedFlags();
292  }
293  }
294 
299  std::unique_ptr<Wt::WWidget> createEditWidget( Wt::WFormModel::Field field )
300  {
301 // COUT2_( field );
302  if( formModel() )
303  {
304  return formModel()-> createEditWidget( field );
305  }
306 
308  }
309 
310 
311  /*
312  ** base class virtual overridea
313  **
314  */
315 
316  virtual void updateView()
317  {
318  Wtx::TemplateFormView::updateView( formModel() );
319 
320  resetEditWidgetChangedFlags();
321  }
322 
331  virtual void updateView( Wt::WFormModel *model )
332  {
333 // COUT2_LINE;
334  Wtx::TemplateFormView::updateView( model );
335  resetEditWidgetChangedFlags();
336  }
337 
342  virtual void updateViewField( Wt::WFormModel *model, Wt::WFormModel::Field field )
343  {
344 // COUT2_( field );
345 
346  /*
347  ** using the enhanced features of the Wtx::Model query the field definition
348  ** object to see if there is even supposed to be a UI component to this
349  ** model field.
350  **
351  */
352 // if( fieldDef(model,field)-> flags() & Wtx::Dbo::FieldDefBase::NoUi )
353 // {
354 // COUT_( "no UI" );
355 // return;
356 // }
357 
358  Wtx::TemplateFormView::updateViewField( model, field );
359  resetEditWidgetChangedFlags();
360  }
361 
362 
367  virtual void updateViewValue( Wt::WFormModel *model, Wt::WFormModel::Field field, Wt::WFormWidget *edit )
368  {
369 // COUT2_( field );
370  Wtx::TemplateFormView::updateViewValue( model, field, edit );
371  resetEditWidgetChangedFlags();
372  }
373 
374 
379  virtual bool updateViewValue( Wt::WFormModel *model, Wt::WFormModel::Field field, Wt::WWidget *edit )
380  {
381 // COUT2_( field );
382  return Wtx::TemplateFormView::updateViewValue( model, field, edit );
383  resetEditWidgetChangedFlags();
384  }
385 
386 
387  virtual void updateModel()
388  {
389  updateModel( formModel() );
390  }
391 
396  virtual void updateModel( Wt::WFormModel *model )
397  {
398 // COUT2_LINE;
399  Wtx::TemplateFormView::updateModel( model );
400  }
401 
402 
407  virtual void updateModelField( Wt::WFormModel *model, Wt::WFormModel::Field field )
408  {
409 // COUT2_( field );
410  Wtx::TemplateFormView::updateModelField( model, field );
411  }
412 
413 
418  virtual void updateModelValue( Wt::WFormModel *model, Wt::WFormModel::Field field, Wt::WFormWidget *edit )
419  {
420 // COUT2_( field );
421  Wtx::TemplateFormView::updateModelValue( model, field, edit );
422  }
423 
424 
429  virtual bool updateModelValue( Wt::WFormModel *model, Wt::WFormModel::Field field, Wt::WWidget *edit )
430  {
431 // COUT2_( field );
432  return Wtx::TemplateFormView::updateModelValue( model, field, edit );
433  }
434 
435  /*
436  ** Template class overrides
437  **
438  */
439  virtual void bindString( const std::string & varName, const Wt::WString & value, Wt::TextFormat textFormat=Wt::TextFormat::XHTML )
440  {
441 // COUT2_( varName << "=" << value );
442  Wtx::TemplateFormView::bindString( varName, value, textFormat );
443  }
444 
449  virtual void bindWidget( const std::string & varName, std::unique_ptr< Wt::WWidget > widget )
450  {
451 // COUT2_( varName );
452  Wtx::TemplateFormView::bindWidget( varName, std::move(widget) );
453  }
454 
459  virtual std::unique_ptr< Wt::WWidget > removeWidget( Wt::WWidget *widget ) override
460  {
461 // COUT2_LINE;
462  return Wtx::TemplateFormView::removeWidget( widget );
463  }
464 
469  virtual bool conditionValue( const std::string & name ) const
470  {
471 // COUT2_( name );
472  return Wtx::TemplateFormView::conditionValue( name );
473  }
474 
479  virtual void resolveString( const std::string & varName, const std::vector< Wt::WString > & args, std::ostream & result )
480  {
481  show( __FUNCTION__, varName, args );
482  Wtx::TemplateFormView::resolveString( varName, args, result );
483  }
484 
489  virtual void handleUnresolvedVariable( const std::string & varName, const std::vector< Wt::WString > & args, std::ostream & result )
490  {
491  show( __FUNCTION__, varName, args );
492  Wtx::TemplateFormView::handleUnresolvedVariable( varName, args, result );
493  }
494 
499  virtual WWidget * resolveWidget( const std::string & varName )
500  {
501  show( __FUNCTION__, varName );
502  return Wtx::TemplateFormView::resolveWidget( varName );
503  }
504 
509  virtual bool resolveFunction( const std::string & name, const std::vector< Wt::WString > & args, std::ostream & result )
510  {
511  show( __FUNCTION__, name, args );
512 
513  return Wtx::TemplateFormView::resolveFunction( name, args, result );
514  }
515 
520  virtual void refresh() override
521  {
522 // COUT2_LINE;
523  Wtx::TemplateFormView::refresh();
524  }
525 
530  virtual void renderTemplate( std::ostream & result )
531  {
532 // COUT2_LINE;
533  Wtx::TemplateFormView::renderTemplate( result );
534  }
535 
536 
542  std::vector< std::unique_ptr< FormWidgetWrapper > > & formWidgetWrappers()
543  {
544  return m_formWidgets;
545  }
546 
555  {
556  for( auto & fww : m_formWidgets )
557  {
558  if( fww-> fieldName() == fieldDef.fieldName() )
559  return fww.get();
560  }
561 
562  return nullptr;
563  }
564 
565  FormWidgetWrapper * formWidgetWrapper( const std::string & fieldName )
566  {
567  for( auto & fww : m_formWidgets )
568  {
569  if( fww-> fieldName() == fieldName )
570  {
571  return fww.get();
572  }
573  }
574  }
575 
583  Wt::WFormWidget * formWidget( Wtx::Dbo::FieldDefBase & fieldDef )
584  {
585  for( auto & fww : m_formWidgets )
586  {
587  if( fww-> fieldName() == fieldDef.fieldName() )
588  return fww-> widget();
589  }
590 
591  return nullptr;
592  }
593 
594  private:
595 
600  std::unique_ptr<Wtx::Dbo::FormModel<C>> m_formModel;
601 
602  std::vector<std::unique_ptr<FormWidgetWrapper>> m_formWidgets;
603 
604 }; // endclass Wtx::Dbo::TemplateFormView
605 
606 
607  } // endnamespace Dbo {
608 } // endnamespace Wtx {
609 
610 #endif // __WTX_DBO_TEMPLATEFORMVIEW_H___
611 
612 
std::vector< std::unique_ptr< FormWidgetWrapper > > & formWidgetWrappers()
Widget Wrapper Collection.
virtual void on_focussed(FormWidgetWrapper *fww)
Focussed Stub.
virtual void updateView(Wt::WFormModel *model)
Update a View from a Model.
Wtx::Dbo::FormModel< C > * formModel() const
Internal Form Model.
std::unique_ptr< Wt::WWidget > createEditWidget(Wt::WFormModel::Field f)
Create Edit Widget.
Template Form View.
Wt::WFormWidget * formWidget(Wtx::Dbo::FieldDefBase &fieldDef)
Get Form Widget Wrapper.
FormWidgetWrapper * formWidgetWrapper(Wtx::Dbo::FieldDefBase &fieldDef)
Get Form Widget Wrapper.
virtual void on_blurred(FormWidgetWrapper *fww)
Blurred Stub.
virtual void on_clicked(FormWidgetWrapper *fww)
Clicked Stub.
TemplateFormView(const Wt::WString &text, std::unique_ptr< Wtx::Dbo::FormModel< C > > model)
Constructor with Model.
witty extension library
Definition: Activity.h:51