Project

General

Profile

Bug #6150 » tableedit.cpp

Maximilian Kleinert, 11/20/2017 08:16 PM

 
1
#include <Wt/WApplication.h>
2
#include <Wt/WContainerWidget.h>
3
#include <Wt/WEnvironment.h>
4
#include <Wt/WStandardItem.h>
5
#include <Wt/WStandardItemModel.h>
6
#include <Wt/WTableView.h>
7
#include <Wt/WTextArea.h>
8

    
9
class TestApplication : public Wt::WApplication {
10
public:
11
  TestApplication(const Wt::WEnvironment &env);
12
};
13

    
14
TestApplication::TestApplication(const Wt::WEnvironment &env)
15
    : WApplication(env) {
16
  using namespace Wt;
17

    
18
  auto table = std::make_unique<WTableView>();
19
  const Wt::WFlags<Wt::ItemFlag> zeroFlag;
20
  auto model = std::make_shared<WStandardItemModel>();
21
  int rows = 3;
22
  int cols = 3;
23
  for (auto i = 0; i < rows; ++i) {
24
    for (auto j = 0; j < cols; ++j) {
25
      auto item = cpp14::make_unique<WStandardItem>();
26
      item->setData(0, ItemDataRole::User);
27
      item->setData(i * j, ItemDataRole::Display);
28
      item->setFlags(ItemFlag::Editable | ItemFlag::Selectable);
29
      model->setItem(i, j, std::move(item));
30
    }
31
  }
32
  for (auto i = 0; i < cols; ++i) {
33
    model->setHeaderData(i, WString("Col {1}").arg(i));
34
  }
35
  table->setModel(model);
36

    
37
  table->setRowHeight(28);
38
  table->setHeaderHeight(28);
39
  table->setEditTriggers(EditTrigger::SingleClicked);
40
  table->setSortingEnabled(false);
41

    
42
  table->setSelectionBehavior(Wt::SelectionBehavior::Items);
43

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

    
50
  table->setWidth((WIDTH + 7) * table->model()->columnCount() + 2);
51

    
52
  root()->addWidget(std::move(table));
53
}
54

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

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