Project

General

Profile

WTableView QueryModel and update ยป main.cpp

Maximilian Kleinert, 04/06/2019 03:31 PM

 
#include <Wt/WApplication.h>
#include <Wt/WContainerWidget.h>
#include <Wt/WPushButton.h>
#include <Wt/WHBoxLayout.h>
#include <Wt/WVBoxLayout.h>
#include <Wt/WLineEdit.h>
#include <Wt/Dbo/Dbo.h>
#include <Wt/Dbo/Session.h>
#include <Wt/Dbo/backend/Sqlite3.h>
#include <Wt/Dbo/QueryModel.h>
#include <Wt/WTableView.h>
#include <Wt/WTabWidget.h>
#include <Wt/WBootstrapTheme.h>

#define USE_BOXINGLAYOUT 1

class Test {
public:
std::string name_;

template<class Action>
void persist(Action &a)
{
Wt::Dbo::field(a, name_, "name");
}
};

class Session : public Wt::Dbo::Session {
public:
Session()
{
auto sqlite3 = std::make_unique<Wt::Dbo::backend::Sqlite3>(":memory:");
sqlite3->setProperty("show-queries", "true");
setConnection(std::move(sqlite3));

mapClass<Test>("test");
createTables();
}
};

class MyApp : public Wt::WApplication {
public:
explicit MyApp(const Wt::WEnvironment &env) : Wt::WApplication(env)
{
root()->setMaximumSize(Wt::WLength(80, Wt::WLength::Unit::Percentage), Wt::WLength(80, Wt::WLength::Unit::Percentage));
root()->setMargin(20);
auto theme = std::make_shared<Wt::WBootstrapTheme>();
theme->setVersion(Wt::BootstrapVersion::v3);
setTheme(theme);
auto tab = root()->addNew<Wt::WTabWidget>();

auto c = std::make_unique<Wt::WContainerWidget>();
c->setMargin(20);
auto hbox = c->setLayout(std::make_unique<Wt::WHBoxLayout>());

lineEdit_ = hbox->addWidget(std::make_unique<Wt::WLineEdit>(), 1);
auto add = hbox->addWidget(std::make_unique<Wt::WPushButton>("Add"));

add->clicked().connect([this]() {
{
Wt::Dbo::Transaction t{session_};
auto test = std::make_unique<Test>();
test->name_ = lineEdit_->text().toUTF8();
session_.add(std::move(test));
}
model_->reload();
});

tab->addTab(std::move(c), "Adder", Wt::ContentLoading::Lazy);
c = std::make_unique<Wt::WContainerWidget>();
c->setMargin(20);
auto tableview = createTableView();

#ifdef USE_BOXINGLAYOUT
boxTableView(c.get(), std::move(tableview));
#else
c->addWidget(std::move(tableview));
#endif
tab->addTab(std::move(c), "View", Wt::ContentLoading::Lazy);
}

private:
Session session_;
Wt::Dbo::QueryModel<Wt::Dbo::ptr<Test>>* model_;
Wt::WLineEdit *lineEdit_;

void boxTableView(Wt::WContainerWidget* c, std::unique_ptr<Wt::WTableView>&& tableview)
{
auto vbox = c->setLayout(std::make_unique<Wt::WVBoxLayout>());
auto innerbox = vbox->addWidget(std::make_unique<Wt::WContainerWidget>());
vbox->addWidget(std::make_unique<Wt::WContainerWidget>(), 1);

auto hbox = innerbox->setLayout(std::make_unique<Wt::WHBoxLayout>());
hbox->addWidget(std::move(tableview));
hbox->addWidget(std::make_unique<Wt::WContainerWidget>(), 1);
}
std::unique_ptr<Wt::WTableView> createTableView()
{
auto model = std::make_shared<Wt::Dbo::QueryModel<Wt::Dbo::ptr<Test>>>();
model->setQuery(session_.find<Test>(), false);
model->addColumn("id", "Id");
model->addColumn("name", "Name");
model_ = model.get();

auto table = std::make_unique<Wt::WTableView>();
table->setModel(model);
table->setColumnResizeEnabled(false);
table->setColumnAlignment(0, Wt::AlignmentFlag::Center);
table->setHeaderAlignment(0, Wt::AlignmentFlag::Center);
table->setAlternatingRowColors(true);
table->setRowHeight(28);
table->setHeaderHeight(28);
table->setSelectionMode(Wt::SelectionMode::Single);
table->setEditTriggers(Wt::EditTrigger::None);
return table;
}
};

std::unique_ptr<Wt::WApplication> createApplication(const Wt::WEnvironment &env)
{
std::unique_ptr<Wt::WApplication> app = std::make_unique<MyApp>(env);
return app;
}

int main(int argc, char **argv)
{
return Wt::WRun(argc, argv, &createApplication);
}
    (1-1/1)