Project

General

Profile

Bug #6121 ยป main.cpp

Maximilian Kleinert, 11/12/2017 10:23 AM

 
#include <Wt/WApplication.h>
#include <Wt/WContainerWidget.h>
#include <Wt/WEnvironment.h>
#include <Wt/WStandardItem.h>
#include <Wt/WStandardItemModel.h>
#include <Wt/WTableView.h>
#include <Wt/WTextArea.h>

class TestApplication : public Wt::WApplication {
public:
TestApplication(const Wt::WEnvironment &env);

private:
Wt::WTextArea *ta_;
void my_cell_clicked(Wt::WModelIndex index, Wt::WMouseEvent);
};

TestApplication::TestApplication(const Wt::WEnvironment &env)
: WApplication(env) {
using namespace Wt;

auto table = std::make_unique<WTableView>();
table->setRowHeaderCount(1);
const Wt::WFlags<Wt::ItemFlag> zeroFlag;
auto model = std::make_shared<WStandardItemModel>();
int rows = 3;
int cols = 3;
for (auto i = 0; i < rows; ++i) {
for (auto j = 0; j < cols; ++j) {
auto item = cpp14::make_unique<WStandardItem>();
item->setData(0, ItemDataRole::User);
item->setData(WString("Item row {1}, col {2}").arg(i).arg(j),
ItemDataRole::Display);
item->setFlags(ItemFlag::Editable | ItemFlag::Selectable);
model->setItem(i, j, std::move(item));
}
}
for (auto i = 0; i < cols; ++i) {
model->setHeaderData(i, WString("Col {1}").arg(i));
}
table->setModel(model);
table->setEditTriggers(EditTrigger::None);
table->setSortingEnabled(false);


table->setSelectionBehavior(Wt::SelectionBehavior::Items);
table->setSelectionMode(Wt::SelectionMode::Single);

const int WIDTH = 120;
for (auto i = 0; i < table->model()->columnCount(); ++i) {
table->setColumnWidth(i, WIDTH);
table->setColumnAlignment(i, Wt::AlignmentFlag::Right);
}

table->clicked().connect(this, &TestApplication::my_cell_clicked);
table->setWidth((WIDTH + 7) * table->model()->columnCount() + 2);

root()->addWidget(std::move(table));
ta_ = root()->addNew<Wt::WTextArea>();
ta_->setColumns(80);
ta_->setRows(20);
}
void TestApplication::my_cell_clicked(Wt::WModelIndex index, Wt::WMouseEvent) {
auto col = index.column();
auto row = index.row();
ta_->setText(Wt::WString("{1}Entering clicked for row {2}, column {3}\n").arg(ta_->text()).arg(row).arg(col));
std::cout << "Entering clicked: row " << row << ", column " << col
<< std::endl;
}

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

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