1 #include "PtrCollectionFormDelegate.h"
2 
3 #include "model/TestDboObject.h"
4 #include "QuerySelectionBox.h"
5 
6 #include <Wt/WLogger.h>
7 
PtrCollectionFormDelegate(Wt::Dbo::Session & session)8 PtrCollectionFormDelegate::PtrCollectionFormDelegate(Wt::Dbo::Session& session)
9   : session_(session)
10 {
11 }
12 
createFormWidget()13 std::unique_ptr<Wt::WWidget> PtrCollectionFormDelegate::createFormWidget()
14 {
15   Wt::Dbo::Transaction t(session_);
16 
17   auto model = std::make_shared<Wt::Dbo::QueryModel<Wt::Dbo::ptr<TestDboPtr>>>();
18   model->setQuery(session_.find<TestDboPtr>());
19   model->addColumn("id", "ID");
20 
21   return std::make_unique<QuerySelectionBox<Wt::Dbo::ptr<TestDboPtr>>>(model);
22 }
23 
updateModelValue(Wt::WFormModel * model,Wt::WFormModel::Field field,Wt::WFormWidget * edit)24 void PtrCollectionFormDelegate::updateModelValue(Wt::WFormModel *model, Wt::WFormModel::Field field, Wt::WFormWidget *edit)
25 {
26   auto box = dynamic_cast<QuerySelectionBox<Wt::Dbo::ptr<TestDboPtr>> *>(edit);
27   if (box) {
28     model->setValue(field, box->selectedItems());
29   } else {
30     Wt::log("error") << "PtrCollectionFormDelegate" << ": " << "Could not cast edit to QuerySelectionBox!";
31   }
32 }
33 
updateViewValue(Wt::WFormModel * model,Wt::WFormModel::Field field,Wt::WFormWidget * edit)34 void PtrCollectionFormDelegate::updateViewValue(Wt::WFormModel *model, Wt::WFormModel::Field field, Wt::WFormWidget *edit)
35 {
36   auto box = dynamic_cast<QuerySelectionBox<Wt::Dbo::ptr<TestDboPtr>> *>(edit);
37   if (box) {
38     Wt::cpp17::any v = model->value(field);
39 
40     try {
41       std::vector<Wt::Dbo::ptr<TestDboPtr>> values = Wt::cpp17::any_cast<std::vector<Wt::Dbo::ptr<TestDboPtr>>>(v);
42       box->selectItems(values);
43     } catch (std::exception& e) {
44       Wt::log("error") << "PtrCollectionFormDelegate" << ": " << "Could not convert value to TestDboPtr vector: " << e.what();
45     }
46 
47   } else {
48     Wt::log("error") << "PtrCollectionFormDelegate" << ": " << "Could not cast edit to QuerySelectionBox!";
49   }
50 }
51