Project

General

Profile

Created layout aware class derived from WTableView that a... » TableView.cpp

TableView Implementation - Matt Russell, 08/12/2015 05:44 PM

 
1
#include "TableView.h"
2

    
3
#include <Wt/WAbstractItemModel>
4

    
5
// Pixels
6
const int TableView::kTableHeaderRowHeight = 38;
7
const int TableView::kTableBodyRowHeight   = 48;
8
const int TableView::kTableCellPadding     = 7;
9
const int TableView::kScrollBarWidth       = 20;
10
const int TableView::kDefaultDateTimeWidth = 120;
11

    
12
TableView::TableView(Wt::WContainerWidget* parent)
13
    : Wt::WTableView(parent)
14
{
15
    setHeaderHeight(kTableHeaderRowHeight);
16
    setRowHeight(kTableBodyRowHeight);
17

    
18
    setLayoutSizeAware(true);
19
}
20

    
21
auto TableView::layoutSizeChanged(int width, int height) -> void {
22
    // Calculate our fixed width columns
23
    auto nfixed = 0;
24
    auto nrel = 0;
25

    
26
    // "fixed" number of pixels
27
    auto fixed = 0.0;
28

    
29
    for (auto col : widths_) if (col.second.unit() == Wt::WLength::Percentage) nrel++;
30

    
31
    auto percent_sum=0.0;
32
    for (auto col : widths_) {
33
        if (col.second.unit() != Wt::WLength::Percentage)
34
            fixed += col.second.toPixels();
35
        else
36
            percent_sum += col.second.value();
37
    }
38

    
39
    // Check to see if the relative columns are taking up ~100% of the
40
    // non-fixed width.  "Roughly" because sometimes they're intentionally off
41
    // by a fraction just to avoid a scroll bar
42
    const auto epsilon = 0.5;
43
    if (100.0 - percent_sum > epsilon) Wt::log("debug") << "Warning: Relative column widths do not take up 100% of the available width";
44

    
45
    // Columns who's width wasn't explicitly set are considered "fixed", and Wt
46
    // will default them to 150px or something.
47
    nfixed = model()->columnCount() - nrel;
48

    
49
    // Show scroll bar?
50
    bool show_scroll = visible_rows_ > 0 && model()->rowCount() > visible_rows_;
51

    
52
    auto remainder = width - (fixed + (kTableCellPadding * model()->columnCount()) + (show_scroll ? kScrollBarWidth : 0.0));
53

    
54
    for (auto col : widths_) {
55
        if (col.second.unit() == Wt::WLength::Percentage) {
56
            setColumnWidth(col.first,  Wt::WLength(col.second.value()/100.0*remainder, Wt::WLength::Pixel));
57
        }
58
    }
59

    
60
    // Pass the call up the chain
61
    Wt::WTableView::layoutSizeChanged(width, height);
62
}
63

    
64
auto TableView::setColumnWidth(int column, const Wt::WLength& width) -> void {
65
    // Just save the data and pass the the work up
66
    widths_.emplace(column, width);
67

    
68
    Wt::WTableView::setColumnWidth(column, width);
69
}
70

    
71
/* vim: set ts=4 sw=4 sts=4 expandtab ffs=unix : */
(2-2/2)