46 #include <Wt/WDialog.h> 47 #include <Wt/WPushButton.h> 48 #include <Wt/WContainerWidget.h> 49 #include <Wt/WAbstractItemModel.h> 50 #include <Wt/WLineEdit.h> 51 #include <Wt/WVBoxLayout.h> 52 #include <Wt/WTableView.h> 53 #include <Wt/WModelIndex.h> 55 #include <Wtx/Core/Core.h> 56 #include <Wtx/Util/Util.h> 58 #include "FieldDefLookupTable.h" 63 :
public Wt::WContainerWidget
69 void setModel(
const std::shared_ptr<Wt::WAbstractItemModel > & model );
71 std::shared_ptr<Wt::WAbstractItemModel > m_model;
75 TableView::TableView()
76 : Wt::WContainerWidget()
80 void TableView::setModel(
const std::shared_ptr<Wt::WAbstractItemModel > & model )
86 :
public Wt::WLineEdit
93 std::shared_ptr<Wt::WAbstractItemModel> (*getModel)(
int sid,
int tid,
const std::string & filter,
Wtx::Dbo::Session & session),
94 std::unique_ptr<Wt::WTableView> (*getTableView)(
int sid,
int tid,
const std::string & filter,
Wtx::Dbo::Session & session),
100 m_getTableView(getTableView)
102 changed() .connect(
this, &TableLineEdit::on_changed );
103 focussed() .connect(
this, &TableLineEdit::on_focussed );
112 int find_id(
const std::string & value )
119 auto v = Wtx::Core::toupper( value );
121 for(
int i=0; i < m_model-> rowCount(); i++ )
123 auto id = Wt::asNumber(m_model-> index( i, 0 ).data());
124 auto ky = Wtx::Core::toupper( Wt::asString(m_model-> index( i, 1 ).data()).toUTF8() );
126 if( ky.find(v) != std::string::npos )
149 int get_id(
const std::string & value )
153 for(
int i=0; i < m_model-> rowCount(); i++ )
155 auto id = Wt::asNumber(m_model-> index( i, 0 ).data());
156 auto ky = Wt::asString(m_model-> index( i, 1 ).data()).toUTF8();
174 std::string get_string(
int index )
176 if( m_model && index > -1 )
178 for(
int i=0; i < m_model-> rowCount(); i++ )
180 auto id = Wt::asNumber( m_model-> index( i, 0 ).data() );
181 auto ky = Wt::asString( m_model-> index( i, 1 ).data() ).toUTF8();
213 void loadModel(
int id,
const std::string & filter )
215 if( !m_loaded && m_getModel )
218 m_model = m_getModel( m_sourceID,
id, filter, m_session );
233 std::cout << __FILE__ <<
":" << __LINE__
236 <<
" " << m_valueText
247 setValueText( m_valueText );
267 auto value = Wt::WLineEdit::valueText().toUTF8();
276 m_targetID = find_id( value );
278 if( m_targetID == -1 )
280 auto dialog = addChild( std::make_unique<Wt::WDialog>(
"item picker") );
281 dialog-> rejectWhenEscapePressed(
true);
283 auto cw = dialog-> contents()-> addNew<Wt::WContainerWidget>();
284 auto lw = std::make_unique<Wt::WVBoxLayout>();
286 auto u_search = std::make_unique<Wt::WLineEdit>(value);
287 auto search = u_search.get();
288 search-> setDisabled(
true);
289 lw-> addWidget( std::move( u_search ) );
294 std::cout << __FILE__ <<
":" << __LINE__
297 <<
" " << search-> valueText().toUTF8()
302 auto u_table = m_getTableView( m_sourceID, m_targetID, search-> valueText().toUTF8(), m_session );
303 auto table = u_table.get();
311 table-> doubleClicked().connect( dialog, &Wt::WDialog::accept );
312 lw-> addWidget( std::move( u_table ), 1 );
314 auto u_select = std::make_unique<Wt::WPushButton>(
"select");
315 u_select-> clicked().connect( dialog, &Wt::WDialog::accept );
316 lw-> addWidget( std::move( u_select ) );
318 dialog-> finished().connect( [=]( Wt::DialogCode code )
324 if( dialog-> result() != Wt::DialogCode::Accepted )
331 if( table-> selectedIndexes().size() != 1 )
334 auto index = *table-> selectedIndexes().begin();
336 m_targetID = Wt::asNumber( index.data() );
338 std::cout << __FILE__ <<
":" << __LINE__
339 <<
" " << index.row()
340 <<
" " << index.column()
346 setValueText( get_string( m_targetID ) );
361 std::make_unique<Wt::WText>
363 Wt::WString(
"FieldDefLookupTable.cpp:{1} 'getTableView' not returning view '{2}'")
377 std::make_unique<Wt::WText>
379 Wt::WString(
"FieldDefLookupTable.cpp:{1} 'getTableView' not set '{2}'")
386 auto u_cancel = std::make_unique<Wt::WPushButton>(
"cancel");
387 u_cancel-> clicked().connect( dialog, &Wt::WDialog::reject );
388 lw-> addWidget( std::move( u_cancel ) );
390 cw-> setLayout( std::move(lw) );
396 setValueText( get_string( m_targetID ) );
400 std::cout << __FILE__ <<
":" << __LINE__
401 <<
"\n targetID: " << m_targetID
402 <<
"\n valueText: " << m_valueText
408 auto dialog = addChild( std::make_unique<Wt::WDialog>() );
409 dialog-> rejectWhenEscapePressed();
410 dialog-> contents()-> addNew<Wt::WText>(
"cannot put in false data");
419 Wt::WString valueText()
const 421 auto retVal = Wt::WString(
"[null]");
423 if( m_targetID != -1 )
424 retVal = Wt::WString(
"{1}: {2}]").arg( m_table ).arg( m_targetID );
427 std::cout << __FILE__ <<
":" << __LINE__
428 <<
"\n m_loaded: " << m_loaded
429 <<
"\n m_field: " << m_field
430 <<
"\n m_table: " << m_table
431 <<
"\n m_sourceID: " << m_sourceID
432 <<
"\n m_targetID: " << m_targetID
433 <<
"\n retVal: " << retVal
442 void setValueText(
const Wt::WString & value )
444 m_valueText = value.toUTF8();
451 if( m_valueText ==
"[null]" )
456 if( m_valueText.find(
":") != std::string::npos )
458 auto v = Wtx::Core::split( m_valueText,
':' );
460 m_targetID = std::stoi(v.at(1));
461 loadModel(m_targetID,
"");
462 m_valueText = get_string( m_targetID );
468 Wt::WLineEdit::setValueText( m_valueText );
471 std::cout << __FILE__ <<
":" << __LINE__
472 <<
"\n loaded: " << m_loaded
473 <<
"\n sourceID: " << m_sourceID
474 <<
"\n targetID: " << m_targetID
475 <<
"\n table: " << m_table
476 <<
"\n value: " << value
477 <<
"\n valueText:" << m_valueText
478 <<
"\n valueText:" << Wt::WLineEdit::valueText()
485 std::shared_ptr<Wt::WAbstractItemModel> (*m_getModel)(
int sid,
int tid,
const std::string & filter,
Wtx::Dbo::Session & session) =
nullptr;
486 std::unique_ptr<Wt::WTableView> (*m_getTableView)(
int sid,
int tid,
const std::string & filter,
Wtx::Dbo::Session & session) =
nullptr;
487 bool m_loaded =
false;
492 std::string m_valueText;
494 std::shared_ptr<Wt::WAbstractItemModel> m_model =
nullptr;
531 std::unique_ptr<Wt::WWidget> Wtx::Dbo::FieldDefLookupTable::createEditWidget(
int sid,
Wtx::Dbo::Session & session )
const 538 auto retVal = std::make_unique<STATIC::TableLineEdit>( sid, getLookupModel, getTableView, session );
539 retVal-> m_table =
"[" + targetTable;
540 retVal-> m_field = fieldName();
Flags
Field Definition Flags.
FieldDefLookupTable(TableDef &td, const char *fn, int w=-1, int h=-1, Flags f=Flags::None, std::string lb="", std::string ph="", std::string inf="", std::string tt="", int ht=-1)
ctor