| 1 |
#if defined(Hiro_TableView) |
| 2 |
|
| 3 |
namespace hiro { |
| 4 |
|
| 5 |
auto pTableView::construct() -> void { |
| 6 |
qtWidget = qtTableView = new QtTableView(*this); |
| 7 |
qtTableView->setAllColumnsShowFocus(true); |
| 8 |
qtTableView->setContextMenuPolicy(Qt::CustomContextMenu); |
| 9 |
qtTableView->setRootIsDecorated(false); |
| 10 |
qtTableView->setHeaderHidden(true); |
| 11 |
qtTableView->header()->setSectionsMovable(false); |
| 12 |
|
| 13 |
qtTableViewDelegate = new QtTableViewDelegate(*this); |
| 14 |
qtTableView->setItemDelegate(qtTableViewDelegate); |
| 15 |
|
| 16 |
qtTableView->connect(qtTableView, SIGNAL(itemActivated(QTreeWidgetItem*, int)), SLOT(onActivate(QTreeWidgetItem*, int))); |
| 17 |
qtTableView->connect(qtTableView, SIGNAL(itemSelectionChanged()), SLOT(onChange())); |
| 18 |
qtTableView->connect(qtTableView, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(onContext(const QPoint&))); |
| 19 |
qtTableView->connect(qtTableView->header(), SIGNAL(sectionClicked(int)), SLOT(onSort(int))); |
| 20 |
qtTableView->connect(qtTableView, SIGNAL(itemChanged(QTreeWidgetItem*, int)), SLOT(onToggle(QTreeWidgetItem*, int))); |
| 21 |
|
| 22 |
setBackgroundColor(state().backgroundColor); |
| 23 |
setBatchable(state().batchable); |
| 24 |
setBordered(state().bordered); |
| 25 |
setForegroundColor(state().foregroundColor); |
| 26 |
setHeadered(state().headered); |
| 27 |
setSortable(state().sortable); |
| 28 |
|
| 29 |
pWidget::construct(); |
| 30 |
} |
| 31 |
|
| 32 |
auto pTableView::destruct() -> void { |
| 33 |
if(Application::state().quit) return; //TODO: hack |
| 34 |
delete qtTableViewDelegate; |
| 35 |
delete qtTableView; |
| 36 |
qtWidget = qtTableView = nullptr; |
| 37 |
qtTableViewDelegate = nullptr; |
| 38 |
} |
| 39 |
|
| 40 |
auto pTableView::append(sTableViewColumn column) -> void { |
| 41 |
} |
| 42 |
|
| 43 |
auto pTableView::append(sTableViewItem item) -> void { |
| 44 |
lock(); |
| 45 |
if(auto self = item->self()) { |
| 46 |
self->qtItem = new QTreeWidgetItem(qtTableView); |
| 47 |
self->_setState(); |
| 48 |
} |
| 49 |
unlock(); |
| 50 |
} |
| 51 |
|
| 52 |
auto pTableView::remove(sTableViewColumn column) -> void { |
| 53 |
} |
| 54 |
|
| 55 |
auto pTableView::remove(sTableViewItem item) -> void { |
| 56 |
} |
| 57 |
|
| 58 |
auto pTableView::resizeColumns() -> void { |
| 59 |
auto lock = acquire(); |
| 60 |
|
| 61 |
std::vector<s32> widths; |
| 62 |
s32 minimumWidth = 0; |
| 63 |
s32 expandable = 0; |
| 64 |
for(auto column : range(self().columnCount())) { |
| 65 |
s32 width = _width(column); |
| 66 |
widths.push_back(width); |
| 67 |
minimumWidth += width; |
| 68 |
if(self().column(column).expandable()) expandable++; |
| 69 |
} |
| 70 |
|
| 71 |
s32 maximumWidth = self().geometry().width() - 6; |
| 72 |
if(auto scrollBar = qtTableView->verticalScrollBar()) { |
| 73 |
if(scrollBar->isVisible()) maximumWidth -= scrollBar->geometry().width(); |
| 74 |
} |
| 75 |
|
| 76 |
s32 expandWidth = 0; |
| 77 |
if(expandable && maximumWidth > minimumWidth) { |
| 78 |
expandWidth = (maximumWidth - minimumWidth) / expandable; |
| 79 |
} |
| 80 |
|
| 81 |
for(auto column : range(self().columnCount())) { |
| 82 |
s32 width = widths[column]; |
| 83 |
if(self().column(column).expandable()) width += expandWidth; |
| 84 |
qtTableView->setColumnWidth(column, width); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
auto pTableView::setAlignment(Alignment alignment) -> void { |
| 89 |
} |
| 90 |
|
| 91 |
auto pTableView::setBackgroundColor(Color color) -> void { |
| 92 |
//note: QPalette::AlternateBase can be used for alternating row colors |
| 93 |
static auto defaultColor = qtTableView->palette().color(QPalette::Base); |
| 94 |
|
| 95 |
auto palette = qtTableView->palette(); |
| 96 |
palette.setColor(QPalette::Base, CreateColor(color, defaultColor)); |
| 97 |
qtTableView->setPalette(palette); |
| 98 |
qtTableView->setAutoFillBackground((bool)color); |
| 99 |
} |
| 100 |
|
| 101 |
auto pTableView::setBatchable(bool batchable) -> void { |
| 102 |
lock(); |
| 103 |
qtTableView->setSelectionMode(batchable ? QAbstractItemView::ExtendedSelection : QAbstractItemView::SingleSelection); |
| 104 |
unlock(); |
| 105 |
} |
| 106 |
|
| 107 |
auto pTableView::setBordered(bool bordered) -> void { |
| 108 |
qtTableView->repaint(); |
| 109 |
} |
| 110 |
|
| 111 |
auto pTableView::setForegroundColor(Color color) -> void { |
| 112 |
static auto defaultColor = qtTableView->palette().color(QPalette::Text); |
| 113 |
|
| 114 |
auto palette = qtTableView->palette(); |
| 115 |
palette.setColor(QPalette::Text, CreateColor(color, defaultColor)); |
| 116 |
qtTableView->setPalette(palette); |
| 117 |
qtTableView->setAutoFillBackground((bool)color); |
| 118 |
} |
| 119 |
|
| 120 |
auto pTableView::setHeadered(bool headered) -> void { |
| 121 |
qtTableView->setHeaderHidden(!headered); |
| 122 |
} |
| 123 |
|
| 124 |
auto pTableView::setSortable(bool sortable) -> void { |
| 125 |
qtTableView->header()->setSectionsClickable(sortable); |
| 126 |
} |
| 127 |
|
| 128 |
//called on resize/show events |
| 129 |
auto pTableView::_onSize() -> void { |
| 130 |
//resize columns only if at least one column is expandable |
| 131 |
for(auto& column : state().columns) { |
| 132 |
if(column->expandable()) return resizeColumns(); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
auto pTableView::_width(u32 column) -> u32 { |
| 137 |
if(auto width = self().column(column).width()) return width; |
| 138 |
u32 width = 1; |
| 139 |
if(!self().column(column).visible()) return width; |
| 140 |
if(state().headered) width = max(width, _widthOfColumn(column)); |
| 141 |
for(auto row : range(state().items.size())) { |
| 142 |
width = max(width, _widthOfCell(row, column)); |
| 143 |
} |
| 144 |
return width; |
| 145 |
} |
| 146 |
|
| 147 |
auto pTableView::_widthOfColumn(u32 _column) -> u32 { |
| 148 |
u32 width = 8; |
| 149 |
if(auto column = self().column(_column)) { |
| 150 |
if(auto& icon = column->state.icon) { |
| 151 |
width += icon.width() + 4; |
| 152 |
} |
| 153 |
if(auto& text = column->state.text) { |
| 154 |
width += pFont::size(column->font(true), text).width(); |
| 155 |
} |
| 156 |
if(column->state.sorting != Sort::None) { |
| 157 |
width += 12; |
| 158 |
} |
| 159 |
} |
| 160 |
return width; |
| 161 |
} |
| 162 |
|
| 163 |
auto pTableView::_widthOfCell(u32 _row, u32 _column) -> u32 { |
| 164 |
u32 width = 8; |
| 165 |
if(auto item = self().item(_row)) { |
| 166 |
if(auto cell = item->cell(_column)) { |
| 167 |
if(cell->state.checkable) { |
| 168 |
width += 16 + 4; |
| 169 |
} |
| 170 |
if(auto& icon = cell->state.icon) { |
| 171 |
width += icon.width() + 4; |
| 172 |
} |
| 173 |
if(auto& text = cell->state.text) { |
| 174 |
width += pFont::size(cell->font(true), text).width(); |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
return width; |
| 179 |
} |
| 180 |
|
| 181 |
auto QtTableView::onActivate(QTreeWidgetItem* qtItem, int column) -> void { |
| 182 |
if(p.locked()) return; |
| 183 |
|
| 184 |
for(auto& item : p.state().items) { |
| 185 |
if(auto self = item->self()) { |
| 186 |
if(qtItem == self->qtItem) { |
| 187 |
if(auto cell = item->cell(column)) { |
| 188 |
return p.self().doActivate(cell); |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
p.self().doActivate({}); |
| 195 |
} |
| 196 |
|
| 197 |
auto QtTableView::onChange() -> void { |
| 198 |
for(auto& item : p.state().items) { |
| 199 |
item->state.selected = false; |
| 200 |
if(auto self = item->self()) { |
| 201 |
if(self->qtItem->isSelected()) item->state.selected = true; |
| 202 |
} |
| 203 |
} |
| 204 |
if(!p.locked()) p.self().doChange(); |
| 205 |
} |
| 206 |
|
| 207 |
auto QtTableView::onContext(const QPoint&) -> void { |
| 208 |
if(p.locked()) return; |
| 209 |
|
| 210 |
//todo: determine actual cell clicked instead of returning the first cell |
| 211 |
auto item = p.self().selected(); |
| 212 |
auto cell = item.cell(0); |
| 213 |
p.self().doContext(cell); |
| 214 |
} |
| 215 |
|
| 216 |
auto QtTableView::onSort(int columnNumber) -> void { |
| 217 |
if(auto column = p.self().column(columnNumber)) { |
| 218 |
if(!p.locked() && p.state().sortable) p.self().doSort(column); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
auto QtTableView::onToggle(QTreeWidgetItem* qtItem, int column) -> void { |
| 223 |
for(auto& item : p.state().items) { |
| 224 |
if(auto self = item->self()) { |
| 225 |
if(qtItem == self->qtItem) { |
| 226 |
if(auto cell = item->cell(column)) { |
| 227 |
cell->state.checked = (qtItem->checkState(column) == Qt::Checked); |
| 228 |
if(!p.locked()) p.self().doToggle(cell); |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
auto QtTableView::mousePressEvent(QMouseEvent* event) -> void { |
| 236 |
QTreeWidget::mousePressEvent(event); |
| 237 |
if(event->button() == Qt::RightButton) { |
| 238 |
//todo: determine actual cell clicked instead of returning the first cell |
| 239 |
auto item = p.self().selected(); |
| 240 |
auto cell = item.cell(0); |
| 241 |
p.self().doContext(cell); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
auto QtTableView::resizeEvent(QResizeEvent* event) -> void { |
| 246 |
QTreeWidget::resizeEvent(event); |
| 247 |
p._onSize(); |
| 248 |
} |
| 249 |
|
| 250 |
auto QtTableView::showEvent(QShowEvent* event) -> void { |
| 251 |
QTreeWidget::showEvent(event); |
| 252 |
p._onSize(); |
| 253 |
} |
| 254 |
|
| 255 |
QtTableViewDelegate::QtTableViewDelegate(pTableView& p) : QStyledItemDelegate(p.qtTableView), p(p) { |
| 256 |
} |
| 257 |
|
| 258 |
auto QtTableViewDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const -> void { |
| 259 |
QStyledItemDelegate::paint(painter, option, index); |
| 260 |
if(p.state().bordered) { |
| 261 |
QPen pen; |
| 262 |
pen.setColor(QColor(160, 160, 160)); |
| 263 |
pen.setWidth(1); |
| 264 |
painter->setPen(pen); |
| 265 |
painter->drawRect(option.rect); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
} |
| 270 |
|
| 271 |
#endif |