2010-08-27 14:42:06 +02:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-08-27 14:42:06 +02:00
|
|
|
|
|
|
|
Clementine is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stretchheaderview.h"
|
2011-07-24 13:46:31 +02:00
|
|
|
#include "core/logging.h"
|
2010-08-27 14:42:06 +02:00
|
|
|
|
|
|
|
#include <QDataStream>
|
|
|
|
|
|
|
|
#include <algorithm>
|
2010-08-27 15:15:32 +02:00
|
|
|
#include <cmath>
|
2010-08-27 15:22:44 +02:00
|
|
|
#include <numeric>
|
2010-08-27 14:42:06 +02:00
|
|
|
|
2014-01-20 12:08:43 +01:00
|
|
|
const int StretchHeaderView::kMinimumColumnWidth = 20;
|
2011-07-24 13:46:31 +02:00
|
|
|
const int StretchHeaderView::kMagicNumber = 0x502c950f;
|
2010-08-27 14:42:06 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
StretchHeaderView::StretchHeaderView(Qt::Orientation orientation,
|
|
|
|
QWidget* parent)
|
|
|
|
: QHeaderView(orientation, parent),
|
|
|
|
stretch_enabled_(false),
|
|
|
|
in_mouse_move_event_(false) {
|
|
|
|
connect(this, SIGNAL(sectionResized(int, int, int)),
|
|
|
|
SLOT(SectionResized(int, int, int)));
|
2014-01-20 12:08:43 +01:00
|
|
|
setMinimumSectionSize(kMinimumColumnWidth);
|
2010-08-27 14:42:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void StretchHeaderView::setModel(QAbstractItemModel* model) {
|
|
|
|
QHeaderView::setModel(model);
|
|
|
|
|
|
|
|
if (stretch_enabled_) {
|
2010-08-27 15:22:44 +02:00
|
|
|
column_widths_.resize(count());
|
|
|
|
std::fill(column_widths_.begin(), column_widths_.end(), 1.0 / count());
|
2010-08-27 14:42:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-27 15:15:32 +02:00
|
|
|
void StretchHeaderView::NormaliseWidths(const QList<int>& sections) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!stretch_enabled_) return;
|
2010-08-27 14:42:06 +02:00
|
|
|
|
2011-07-24 13:46:31 +02:00
|
|
|
const ColumnWidthType total_sum =
|
|
|
|
std::accumulate(column_widths_.begin(), column_widths_.end(), 0.0);
|
|
|
|
ColumnWidthType selected_sum = total_sum;
|
2010-08-27 15:22:44 +02:00
|
|
|
|
|
|
|
if (!sections.isEmpty()) {
|
|
|
|
selected_sum = 0.0;
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < count(); ++i)
|
|
|
|
if (sections.contains(i)) selected_sum += column_widths_[i];
|
2010-08-27 15:15:32 +02:00
|
|
|
}
|
2010-08-27 14:42:06 +02:00
|
|
|
|
2011-07-24 13:46:31 +02:00
|
|
|
if (total_sum != 0.0 && !qFuzzyCompare(total_sum, 1.0)) {
|
2014-02-07 16:34:20 +01:00
|
|
|
const ColumnWidthType mult =
|
|
|
|
(selected_sum + (1.0 - total_sum)) / selected_sum;
|
|
|
|
for (int i = 0; i < column_widths_.count(); ++i) {
|
|
|
|
if (sections.isEmpty() || sections.contains(i)) column_widths_[i] *= mult;
|
2010-08-27 14:42:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StretchHeaderView::UpdateWidths(const QList<int>& sections) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!stretch_enabled_) return;
|
2010-08-27 14:42:06 +02:00
|
|
|
|
2011-07-24 13:46:31 +02:00
|
|
|
ColumnWidthType total_w = 0.0;
|
2010-08-27 15:15:32 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < column_widths_.count(); ++i) {
|
2011-07-24 13:46:31 +02:00
|
|
|
const ColumnWidthType w = column_widths_[i];
|
2010-08-27 15:15:32 +02:00
|
|
|
int pixels = w * width();
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (pixels != 0 && total_w - int(total_w) > 0.5) pixels++;
|
2010-08-27 15:15:32 +02:00
|
|
|
|
|
|
|
total_w += w;
|
2010-08-27 14:42:06 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!sections.isEmpty() && !sections.contains(i)) continue;
|
2010-08-27 14:42:06 +02:00
|
|
|
|
|
|
|
if (pixels == 0 && !isSectionHidden(i))
|
|
|
|
hideSection(i);
|
2010-12-15 18:46:31 +01:00
|
|
|
else if (pixels != 0 && isSectionHidden(i)) {
|
2010-08-27 14:42:06 +02:00
|
|
|
showSection(i);
|
2010-12-15 18:46:31 +01:00
|
|
|
}
|
2010-08-27 14:42:06 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (pixels != 0) resizeSection(i, pixels);
|
2010-08-27 14:42:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StretchHeaderView::HideSection(int logical) {
|
2010-12-11 15:26:49 +01:00
|
|
|
// Would this hide the last section?
|
|
|
|
bool all_hidden = true;
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < count(); ++i) {
|
2010-12-11 15:26:49 +01:00
|
|
|
if (i != logical && !isSectionHidden(i) && sectionSize(i) > 0) {
|
|
|
|
all_hidden = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (all_hidden) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-27 14:42:06 +02:00
|
|
|
if (!stretch_enabled_) {
|
|
|
|
hideSection(logical);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
column_widths_[logical] = 0.0;
|
|
|
|
NormaliseWidths();
|
|
|
|
UpdateWidths();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StretchHeaderView::ShowSection(int logical) {
|
|
|
|
if (!stretch_enabled_) {
|
|
|
|
showSection(logical);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// How many sections are visible already?
|
|
|
|
int visible_count = 0;
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < count(); ++i) {
|
|
|
|
if (!isSectionHidden(i)) visible_count++;
|
2010-08-27 14:42:06 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
column_widths_[logical] = visible_count == 0 ? 1.0 : 1.0 / visible_count;
|
2010-08-27 14:42:06 +02:00
|
|
|
NormaliseWidths();
|
|
|
|
UpdateWidths();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StretchHeaderView::SetSectionHidden(int logical, bool hidden) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (hidden) {
|
2010-12-15 18:46:31 +01:00
|
|
|
HideSection(logical);
|
|
|
|
} else {
|
|
|
|
ShowSection(logical);
|
|
|
|
}
|
2010-08-27 14:42:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void StretchHeaderView::resizeEvent(QResizeEvent* event) {
|
|
|
|
QHeaderView::resizeEvent(event);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!stretch_enabled_) return;
|
2010-08-27 14:42:06 +02:00
|
|
|
|
|
|
|
UpdateWidths();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StretchHeaderView::mouseMoveEvent(QMouseEvent* e) {
|
|
|
|
in_mouse_move_event_ = true;
|
|
|
|
QHeaderView::mouseMoveEvent(e);
|
|
|
|
in_mouse_move_event_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StretchHeaderView::SectionResized(int logical, int, int new_size) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!stretch_enabled_) return;
|
2010-08-27 14:42:06 +02:00
|
|
|
|
|
|
|
if (in_mouse_move_event_) {
|
|
|
|
// Update this section's proportional width
|
2011-07-24 13:46:31 +02:00
|
|
|
column_widths_[logical] = ColumnWidthType(new_size) / width();
|
2010-08-27 14:42:06 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
// Find the visible sections to the right of the section that's being
|
|
|
|
// resized
|
2010-08-27 14:42:06 +02:00
|
|
|
int visual = visualIndex(logical);
|
|
|
|
QList<int> logical_sections_to_resize;
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < count(); ++i) {
|
2010-08-27 14:42:06 +02:00
|
|
|
if (!isSectionHidden(i) && visualIndex(i) > visual)
|
|
|
|
logical_sections_to_resize << i;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resize just those columns
|
|
|
|
if (!logical_sections_to_resize.isEmpty()) {
|
|
|
|
in_mouse_move_event_ = false;
|
|
|
|
UpdateWidths(logical_sections_to_resize);
|
2010-08-27 15:15:32 +02:00
|
|
|
NormaliseWidths(logical_sections_to_resize);
|
2010-08-27 14:42:06 +02:00
|
|
|
in_mouse_move_event_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StretchHeaderView::ToggleStretchEnabled() {
|
|
|
|
SetStretchEnabled(!is_stretch_enabled());
|
|
|
|
}
|
|
|
|
|
|
|
|
void StretchHeaderView::SetStretchEnabled(bool enabled) {
|
|
|
|
stretch_enabled_ = enabled;
|
|
|
|
|
|
|
|
if (enabled) {
|
|
|
|
// Initialise the list of widths from the current state of the widget
|
2010-08-27 15:22:44 +02:00
|
|
|
column_widths_.resize(count());
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < count(); ++i) {
|
2011-07-24 13:46:31 +02:00
|
|
|
column_widths_[i] = ColumnWidthType(sectionSize(i)) / width();
|
2010-08-27 14:42:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stretch the columns to fill the widget
|
|
|
|
NormaliseWidths();
|
|
|
|
UpdateWidths();
|
|
|
|
}
|
|
|
|
|
|
|
|
emit StretchEnabledChanged(enabled);
|
|
|
|
}
|
2010-08-27 15:15:32 +02:00
|
|
|
|
2011-07-24 13:46:31 +02:00
|
|
|
void StretchHeaderView::SetColumnWidth(int logical, ColumnWidthType width) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!stretch_enabled_) return;
|
2010-08-27 15:15:32 +02:00
|
|
|
|
|
|
|
column_widths_[logical] = width;
|
|
|
|
|
|
|
|
QList<int> other_columns;
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < count(); ++i)
|
|
|
|
if (!isSectionHidden(i) && i != logical) other_columns << i;
|
2010-08-27 15:15:32 +02:00
|
|
|
NormaliseWidths(other_columns);
|
|
|
|
}
|
2010-12-15 18:46:31 +01:00
|
|
|
|
2011-07-24 13:46:31 +02:00
|
|
|
bool StretchHeaderView::RestoreState(const QByteArray& data) {
|
|
|
|
QDataStream s(data);
|
|
|
|
s.setVersion(QDataStream::Qt_4_6);
|
|
|
|
|
|
|
|
int magic_number = 0;
|
|
|
|
s >> magic_number;
|
|
|
|
|
|
|
|
if (magic_number != kMagicNumber || s.atEnd()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<int> pixel_widths;
|
|
|
|
QList<int> visual_indices;
|
2011-07-25 17:53:58 +02:00
|
|
|
int sort_indicator_order = Qt::AscendingOrder;
|
|
|
|
int sort_indicator_section = 0;
|
2011-07-24 13:46:31 +02:00
|
|
|
|
|
|
|
s >> stretch_enabled_;
|
|
|
|
s >> pixel_widths;
|
|
|
|
s >> visual_indices;
|
|
|
|
s >> column_widths_;
|
2011-07-25 17:53:58 +02:00
|
|
|
s >> sort_indicator_order;
|
|
|
|
s >> sort_indicator_section;
|
|
|
|
|
|
|
|
setSortIndicator(sort_indicator_section, Qt::SortOrder(sort_indicator_order));
|
2011-07-24 13:46:31 +02:00
|
|
|
|
|
|
|
const int persisted_column_count =
|
2014-02-07 16:34:20 +01:00
|
|
|
qMin(qMin(visual_indices.count(), pixel_widths.count()),
|
|
|
|
column_widths_.count());
|
2011-07-24 13:46:31 +02:00
|
|
|
|
|
|
|
// Set column visible state, visual indices and, if we're not in stretch mode,
|
|
|
|
// pixel widths.
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < count() && i < persisted_column_count; ++i) {
|
2011-07-24 13:46:31 +02:00
|
|
|
setSectionHidden(i, pixel_widths[i] <= kMinimumColumnWidth);
|
|
|
|
moveSection(visualIndex(visual_indices[i]), i);
|
|
|
|
|
|
|
|
if (!stretch_enabled_) {
|
|
|
|
resizeSection(i, pixel_widths[i]);
|
|
|
|
}
|
|
|
|
}
|
2014-02-07 16:34:20 +01:00
|
|
|
|
2012-01-04 19:45:08 +01:00
|
|
|
// Have we added more columns since the last time?
|
|
|
|
while (column_widths_.count() < count()) {
|
|
|
|
column_widths_ << 0;
|
|
|
|
}
|
2011-07-24 13:46:31 +02:00
|
|
|
|
|
|
|
if (stretch_enabled_) {
|
2014-02-07 16:34:20 +01:00
|
|
|
// In stretch mode, we've already set the proportional column widths so
|
|
|
|
// apply
|
2011-07-24 13:46:31 +02:00
|
|
|
// them now.
|
|
|
|
UpdateWidths();
|
|
|
|
}
|
|
|
|
|
2011-07-25 14:39:47 +02:00
|
|
|
emit StretchEnabledChanged(stretch_enabled_);
|
|
|
|
|
2011-07-24 13:46:31 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray StretchHeaderView::SaveState() const {
|
|
|
|
QByteArray ret;
|
|
|
|
QDataStream s(&ret, QIODevice::WriteOnly);
|
|
|
|
|
|
|
|
QList<int> pixel_widths;
|
|
|
|
QList<int> visual_indices;
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < count(); ++i) {
|
2011-07-24 13:46:31 +02:00
|
|
|
pixel_widths << sectionSize(i);
|
|
|
|
visual_indices << logicalIndex(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
s.setVersion(QDataStream::Qt_4_6);
|
|
|
|
s << kMagicNumber;
|
|
|
|
|
|
|
|
s << stretch_enabled_;
|
|
|
|
s << pixel_widths;
|
|
|
|
s << visual_indices;
|
|
|
|
s << column_widths_;
|
2011-07-25 17:53:58 +02:00
|
|
|
s << int(sortIndicatorOrder());
|
|
|
|
s << sortIndicatorSection();
|
2011-07-24 13:46:31 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|