Wtx ~ Wt Extension Library
WtxLib
ItemDelegate.cpp
1 
2 #include <Wt/WContainerWidget.h>
3 #include <Wt/WAbstractItemModel.h>
4 #include <Wt/Dbo/Transaction.h>
5 
6 #include <Wtx/Dbo/Session.h>
7 
8 #include "Item.h"
9 #include "ItemDelegate.h"
10 
11 Wtx::Sys::Lookup::ItemDelegate::ItemDelegate( Wtx::Dbo::Session * s )
12 : m_session(s)
13 {
14 }
15 
16 std::unique_ptr<Wt::WWidget> Wtx::Sys::Lookup::ItemDelegate::update( Wt::WWidget * widget, const Wt::WModelIndex & index, Wt::WFlags< Wt::ViewItemRenderFlag > flags )
17 {
18  auto w = Wt::WItemDelegate::update( widget, index, flags );
19 
20  auto _getValue = [=]()
21  {
22  int id = Wt::asNumber( index.model()-> data( index.row(), index.column() ) );
23 
24  if( id > 0 )
25  {
26  Wt::Dbo::Transaction t(*session());
27 
28  std::string filter =
29  Wt::WString("SELECT COUNT(1) FROM \"{1}\" WHERE id = {2}")
30  .arg( /*std::string(*/ Wtx::Sys::Lookup::Item::TableDef().tableName() /*)*/ )
31  .arg( id )
32  .toUTF8()
33  ;
34 
35  int totalRows =
36  session()->
37  template query<int>( filter );
38 
39  if( totalRows > 0 )
40  {
41  Wt::Dbo::Transaction t(*session());
42  auto item = session()-> template load<Wtx::Sys::Lookup::Item>(id);
43  return item-> keyField().value();
44  }
45  else // if( totalRows not > 0 )
46  {
47  return Wt::WString("not found: {1}").arg(id).toUTF8();
48  }
49 
50  }
51  else // if( id not > 0 )
52  {
53  return std::string("~ ~ ~");
54  }
55 
56  };
57 
58  auto text = dynamic_cast<Wt::WText *>(w.get());
59  if( text )
60  {
61  text-> setText( _getValue() );
62 
63  } // endif( text )
64 
65  auto checkBox = dynamic_cast<Wt::WCheckBox *>(w.get());
66  if( checkBox )
67  {
68  checkBox-> setText( _getValue() );
69 
70  } // endif( checkBox )
71 
72  /*
73  ** if the widget has other things on it, such as a check-box or an icon, then
74  ** the widget is assembled as a container widget. Inside that container
75  ** are the text field, and the check-box and so on. So we have to
76  ** first grab the container, then try to find the child widget
77  **
78  */
79  auto containerWidget = dynamic_cast<Wt::WContainerWidget *>(w.get());
80  if( containerWidget )
81  {
82  auto t = dynamic_cast<Wt::WText*>( containerWidget-> find("t") );
83 
84  if( t )
85  t-> setText( _getValue() );
86 
87 
88  auto cb = dynamic_cast<Wt::WCheckBox*>( containerWidget-> find("c") );
89  if( cb )
90  {
91  cb-> setMargin( 0 );
92  cb-> setCheckState( checkState( index ) );
93  }
94 
95 
96  } // endif( containerWidget )
97 
98 
99  return std::move(w);
100 
101 } // endstd::unique_ptr<Wt::WWidget> Wtx::Sys::Lookup::Delegate::update( Wt::WWidget * widget, const Wt::WModelIndex & index, Wt::WFlags< Wt::ViewItemRenderFlag > flags )
102 
103 Wt::CheckState Wtx::Sys::Lookup::ItemDelegate::checkState( const Wt::WModelIndex & index )
104 {
105  return Wt::CheckState::Unchecked;
106 }
107 
108 Wtx::Dbo::Session * Wtx::Sys::Lookup::ItemDelegate::session()
109 {
110  return m_session;
111 }
112 
113 Wt::WItemDelegate * Wtx::Sys::Lookup::ItemDelegate::create( Wtx::Dbo::Session * s )
114 {
115  return new Wtx::Sys::Lookup::ItemDelegate(s);
116 }
117 
118 
Wtx::Dbo::Session * session()
Current Session.
virtual Wt::CheckState checkState(const Wt::WModelIndex &index)