File: list-view.cpp

package info (click to toggle)
higan 098-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 11,904 kB
  • ctags: 13,286
  • sloc: cpp: 108,285; ansic: 778; makefile: 32; sh: 18
file content (256 lines) | stat: -rw-r--r-- 7,357 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#if defined(Hiro_ListView)

namespace hiro {

auto pListView::construct() -> void {
  qtWidget = qtListView = new QtListView(*this);
  qtListView->setAllColumnsShowFocus(true);
  qtListView->setContextMenuPolicy(Qt::CustomContextMenu);
  qtListView->setRootIsDecorated(false);
  qtListView->setHeaderHidden(true);
  qtListView->header()->setMovable(false);

  qtListViewDelegate = new QtListViewDelegate(*this);
  qtListView->setItemDelegate(qtListViewDelegate);

  qtListView->connect(qtListView, SIGNAL(itemActivated(QTreeWidgetItem*, int)), SLOT(onActivate()));
  qtListView->connect(qtListView, SIGNAL(itemSelectionChanged()), SLOT(onChange()));
  qtListView->connect(qtListView, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(onContext()));
  qtListView->connect(qtListView->header(), SIGNAL(sectionClicked(int)), SLOT(onSort(int)));
  qtListView->connect(qtListView, SIGNAL(itemChanged(QTreeWidgetItem*, int)), SLOT(onToggle(QTreeWidgetItem*, int)));

  setBackgroundColor(state().backgroundColor);
  setBatchable(state().batchable);
  setBordered(state().bordered);
  setForegroundColor(state().foregroundColor);

  pWidget::construct();
}

auto pListView::destruct() -> void {
  delete qtListViewDelegate;
  delete qtListView;
  qtWidget = qtListView = nullptr;
  qtListViewDelegate = nullptr;
}

auto pListView::append(sListViewHeader header) -> void {
  lock();
  if(auto self = header->self()) {
    self->_setState();
  }
  unlock();
}

auto pListView::append(sListViewItem item) -> void {
  lock();
  if(auto self = item->self()) {
    self->qtItem = new QTreeWidgetItem(qtListView);
    self->_setState();
  }
  unlock();
}

auto pListView::remove(sListViewHeader header) -> void {
}

auto pListView::remove(sListViewItem item) -> void {
}

auto pListView::resizeColumns() -> void {
  lock();

  if(auto& header = state().header) {
    vector<signed> widths;
    signed minimumWidth = 0;
    signed expandable = 0;
    for(auto column : range(header->columnCount())) {
      signed width = _width(column);
      widths.append(width);
      minimumWidth += width;
      if(header->column(column).expandable()) expandable++;
    }

    signed maximumWidth = self().geometry().width() - 6;
    if(auto scrollBar = qtListView->verticalScrollBar()) {
      if(scrollBar->isVisible()) maximumWidth -= scrollBar->geometry().width();
    }

    signed expandWidth = 0;
    if(expandable && maximumWidth > minimumWidth) {
      expandWidth = (maximumWidth - minimumWidth) / expandable;
    }

    for(auto column : range(header->columnCount())) {
      signed width = widths[column];
      if(header->column(column).expandable()) width += expandWidth;
      qtListView->setColumnWidth(column, width);
    }
  }

  unlock();
}

auto pListView::setAlignment(Alignment alignment) -> void {
}

auto pListView::setBackgroundColor(Color color) -> void {
  if(color) {
    QPalette palette = qtListView->palette();
    palette.setColor(QPalette::Base, QColor(color.red(), color.green(), color.blue()));
    palette.setColor(QPalette::AlternateBase, QColor(max(0, (signed)color.red() - 17), max(0, (signed)color.green() - 17), max(0, (signed)color.blue() - 17)));
    qtListView->setPalette(palette);
    qtListView->setAutoFillBackground(true);
  } else {
    //todo: set default color
  }
}

auto pListView::setBatchable(bool batchable) -> void {
  lock();
  qtListView->setSelectionMode(batchable ? QAbstractItemView::ExtendedSelection : QAbstractItemView::SingleSelection);
  unlock();
}

auto pListView::setBordered(bool bordered) -> void {
  qtListView->repaint();
}

auto pListView::setForegroundColor(Color color) -> void {
  if(color) {
    QPalette palette = qtListView->palette();
    palette.setColor(QPalette::Text, QColor(color.red(), color.green(), color.blue()));
    qtListView->setPalette(palette);
  } else {
    //todo: set default color
  }
}

//called on resize/show events
auto pListView::_onSize() -> void {
  //resize columns only if at least one column is expandable
  if(auto& header = state().header) {
    for(auto& column : header->state.columns) {
      if(column->expandable()) return resizeColumns();
    }
  }
}

auto pListView::_width(unsigned column) -> unsigned {
  if(auto& header = state().header) {
    if(auto width = header->column(column).width()) return width;
    unsigned width = 1;
    if(!header->column(column).visible()) return width;
    if(header->visible()) width = max(width, _widthOfColumn(column));
    for(auto row : range(state().items)) {
      width = max(width, _widthOfCell(row, column));
    }
    return width;
  }
  return 1;
}

auto pListView::_widthOfColumn(unsigned _column) -> unsigned {
  unsigned width = 8;
  if(auto& header = state().header) {
    if(auto column = header->column(_column)) {
      if(auto& icon = column->state.icon) {
        width += icon.width() + 2;
      }
      if(auto& text = column->state.text) {
        width += pFont::size(column->font(true), text).width();
      }
    }
  }
  return width;
}

auto pListView::_widthOfCell(unsigned _row, unsigned _column) -> unsigned {
  unsigned width = 8;
  if(auto item = self().item(_row)) {
    if(auto cell = item->cell(_column)) {
      if(cell->state.checkable) {
        width += 16 + 2;
      }
      if(auto& icon = cell->state.icon) {
        width += icon.width() + 2;
      }
      if(auto& text = cell->state.text) {
        width += pFont::size(cell->font(true), text).width();
      }
    }
  }
  return width;
}

auto QtListView::onActivate() -> void {
  if(!p.locked()) p.self().doActivate();
}

auto QtListView::onChange() -> void {
  for(auto& item : p.state().items) {
    item->state.selected = false;
    if(auto self = item->self()) {
      if(self->qtItem->isSelected()) item->state.selected = true;
    }
  }
  if(!p.locked()) p.self().doChange();
}

auto QtListView::onContext() -> void {
  if(!p.locked()) p.self().doContext();
}

auto QtListView::onSort(int columnNumber) -> void {
  if(auto& header = p.state().header) {
    if(auto column = header->column(columnNumber)) {
      if(!p.locked() && column.sortable()) p.self().doSort(column);
    }
  }
}

auto QtListView::onToggle(QTreeWidgetItem* qtItem, int column) -> void {
  for(auto& item : p.state().items) {
    if(auto self = item->self()) {
      if(qtItem == self->qtItem) {
        if(auto cell = item->cell(column)) {
          cell->state.checked = (qtItem->checkState(column) == Qt::Checked);
          if(!p.locked()) p.self().doToggle(cell);
        }
      }
    }
  }
}

auto QtListView::mousePressEvent(QMouseEvent* event) -> void {
  QTreeWidget::mousePressEvent(event);
  if(event->button() == Qt::RightButton) onContext();
}

auto QtListView::resizeEvent(QResizeEvent* event) -> void {
  QTreeWidget::resizeEvent(event);
  p._onSize();
}

auto QtListView::showEvent(QShowEvent* event) -> void {
  QTreeWidget::showEvent(event);
  p._onSize();
}

QtListViewDelegate::QtListViewDelegate(pListView& p) : QStyledItemDelegate(p.qtListView), p(p) {
}

auto QtListViewDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const -> void {
  QStyledItemDelegate::paint(painter, option, index);
  if(p.state().bordered) {
    QPen pen;
    pen.setColor(QColor(160, 160, 160));
    pen.setWidth(1);
    painter->setPen(pen);
    painter->drawRect(option.rect);
  }
}

}

#endif