18
18
#include "stretchheaderview.h"
19
#include "core/logging.h"
20
21
#include <QDataStream>
23
23
#include <algorithm>
27
27
const int StretchHeaderView::kMinimumColumnWidth = 10;
28
const int StretchHeaderView::kMagicNumber = 0x502c950f;
29
30
StretchHeaderView::StretchHeaderView(Qt::Orientation orientation, QWidget* parent)
30
31
: QHeaderView(orientation, parent),
47
48
if (!stretch_enabled_)
50
float total_sum = std::accumulate(column_widths_.begin(), column_widths_.end(), 0.0);
51
float selected_sum = total_sum;
51
const ColumnWidthType total_sum =
52
std::accumulate(column_widths_.begin(), column_widths_.end(), 0.0);
53
ColumnWidthType selected_sum = total_sum;
53
55
if (!sections.isEmpty()) {
54
56
selected_sum = 0.0;
57
59
selected_sum += column_widths_[i];
60
if (total_sum != 0.0 && !qFuzzyCompare(total_sum, 1.0f)) {
61
const float mult = (selected_sum + (1.0 - total_sum)) / selected_sum;
62
if (total_sum != 0.0 && !qFuzzyCompare(total_sum, 1.0)) {
63
const ColumnWidthType mult = (selected_sum + (1.0 - total_sum)) / selected_sum;
62
64
for (int i=0 ; i<column_widths_.count() ; ++i) {
63
65
if (sections.isEmpty() || sections.contains(i))
64
66
column_widths_[i] *= mult;
70
72
if (!stretch_enabled_)
75
ColumnWidthType total_w = 0.0;
75
77
for (int i=0 ; i<column_widths_.count() ; ++i) {
76
const float w = column_widths_[i];
78
const ColumnWidthType w = column_widths_[i];
77
79
int pixels = w * width();
79
81
if (pixels != 0 && total_w - int(total_w) > 0.5)
169
171
if (in_mouse_move_event_) {
170
172
// Update this section's proportional width
171
column_widths_[logical] = float(new_size) / width();
173
column_widths_[logical] = ColumnWidthType(new_size) / width();
173
175
// Find the visible sections to the right of the section that's being resized
174
176
int visual = visualIndex(logical);
199
201
// Initialise the list of widths from the current state of the widget
200
202
column_widths_.resize(count());
201
203
for (int i=0 ; i<count() ; ++i) {
202
column_widths_[i] = float(sectionSize(i)) / width();
204
column_widths_[i] = ColumnWidthType(sectionSize(i)) / width();
205
207
// Stretch the columns to fill the widget
229
231
resizeSection(logical, kMinimumColumnWidth);
235
bool StretchHeaderView::RestoreState(const QByteArray& data) {
237
s.setVersion(QDataStream::Qt_4_6);
239
int magic_number = 0;
242
if (magic_number != kMagicNumber || s.atEnd()) {
246
QList<int> pixel_widths;
247
QList<int> visual_indices;
248
int sort_indicator_order = Qt::AscendingOrder;
249
int sort_indicator_section = 0;
251
s >> stretch_enabled_;
255
s >> sort_indicator_order;
256
s >> sort_indicator_section;
258
setSortIndicator(sort_indicator_section, Qt::SortOrder(sort_indicator_order));
260
const int persisted_column_count =
261
qMin(qMin(visual_indices.count(), pixel_widths.count()), column_widths_.count());
263
// Set column visible state, visual indices and, if we're not in stretch mode,
265
for (int i=0 ; i<count() && i<persisted_column_count ; ++i) {
266
setSectionHidden(i, pixel_widths[i] <= kMinimumColumnWidth);
267
moveSection(visualIndex(visual_indices[i]), i);
269
if (!stretch_enabled_) {
270
resizeSection(i, pixel_widths[i]);
274
if (stretch_enabled_) {
275
// In stretch mode, we've already set the proportional column widths so apply
280
emit StretchEnabledChanged(stretch_enabled_);
285
QByteArray StretchHeaderView::SaveState() const {
287
QDataStream s(&ret, QIODevice::WriteOnly);
289
QList<int> pixel_widths;
290
QList<int> visual_indices;
292
for (int i=0 ; i<count() ; ++i) {
293
pixel_widths << sectionSize(i);
294
visual_indices << logicalIndex(i);
297
s.setVersion(QDataStream::Qt_4_6);
300
s << stretch_enabled_;
304
s << int(sortIndicatorOrder());
305
s << sortIndicatorSection();