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::User::ItemDelegate::ItemDelegate( const std::string & field, Wtx::Dbo::Session * s )
12 : m_field( field ),
13  m_session(s)
14 {
15 }
16 
17 std::unique_ptr<Wt::WWidget> Wtx::Sys::User::ItemDelegate::update( Wt::WWidget * widget, const Wt::WModelIndex & index, Wt::WFlags< Wt::ViewItemRenderFlag > flags )
18 {
19  auto w = Wt::WItemDelegate::update( widget, index, flags );
20 
21  auto _getValue = [=]()
22  {
23  int id = Wt::asNumber( index.model()-> data( index.row(), index.column() ) );
24 
25  if( id > 0 )
26  {
27  Wt::Dbo::Transaction t(*session());
28 
29  auto item =
30  session()-> load<Wtx::Sys::User::Item>(id );
31 
32  std::string fieldValue;
33 
34  if( m_field == "keyField" )
35  {
36  fieldValue = item-> keyField().value();
37  }
38 
39  if( m_field == "abbreviation" )
40  {
41  fieldValue = item-> abbreviation().value();
42  }
43 
44  if( fieldValue == "" )
45  fieldValue = Wt::WString("[sysUser: {1}]").arg( item.id() ).toUTF8();
46 
47  return fieldValue;
48 
49  }
50 
51  return std::string("~ ~ ~");
52 
53  };
54 
55  auto text = dynamic_cast<Wt::WText *>(w.get());
56  if( text )
57  {
58  text-> setText( _getValue() );
59 
60  } // endif( text )
61 
62  return std::move(w);
63 
64 } // endstd::unique_ptr<Wt::WWidget> Wtx::Sys::Lookup::Delegate::update( Wt::WWidget * widget, const Wt::WModelIndex & index, Wt::WFlags< Wt::ViewItemRenderFlag > flags )
65 
66 Wtx::Dbo::Session * Wtx::Sys::User::ItemDelegate::session()
67 {
68  return m_session;
69 }
70 
71 Wt::WItemDelegate * Wtx::Sys::User::ItemDelegate::create( Wtx::Dbo::Session * s )
72 {
73  return new Wtx::Sys::User::ItemDelegate("keyField",s);
74 }
75 
76 Wt::WItemDelegate * Wtx::Sys::User::ItemDelegate::createAbbreviation( Wtx::Dbo::Session * s )
77 {
78  return new Wtx::Sys::User::ItemDelegate("abbreviation",s);
79 }
80 
81