Refactor playlist view/header code

- Don't reload all settings when changing playlists
- Fix initial playlist header columns sizes
- Properly reset header state when resetting columns
This commit is contained in:
Jonas Kvinge
2020-08-29 19:55:00 +02:00
parent 4c3f86aa4d
commit 08f32d1de6
8 changed files with 181 additions and 157 deletions

View File

@@ -361,7 +361,7 @@ MainWindow::MainWindow(Application *app, SystemTrayIcon *tray_icon, OSDBase *osd
ui_->playlist->SetManager(app_->playlist_manager()); ui_->playlist->SetManager(app_->playlist_manager());
ui_->playlist->view()->SetApplication(app_); ui_->playlist->view()->Init(app_);
collection_view_->view()->setModel(collection_sort_model_); collection_view_->view()->setModel(collection_sort_model_);
collection_view_->view()->SetApplication(app_); collection_view_->view()->SetApplication(app_);
@@ -1066,7 +1066,6 @@ void MainWindow::SaveSettings() {
SaveGeometry(); SaveGeometry();
SavePlaybackStatus(); SavePlaybackStatus();
ui_->tabs->SaveSettings(kSettingsGroup); ui_->tabs->SaveSettings(kSettingsGroup);
ui_->playlist->view()->SaveGeometry();
ui_->playlist->view()->SaveSettings(); ui_->playlist->view()->SaveSettings();
app_->scrobbler()->WriteCache(); app_->scrobbler()->WriteCache();

View File

@@ -1054,7 +1054,6 @@ GstState GstEnginePipeline::state() const {
QFuture<GstStateChangeReturn> GstEnginePipeline::SetState(const GstState state) { QFuture<GstStateChangeReturn> GstEnginePipeline::SetState(const GstState state) {
return ConcurrentRun::Run<GstStateChangeReturn, GstElement*, GstState>(&set_state_threadpool_, &gst_element_set_state, pipeline_, state); return ConcurrentRun::Run<GstStateChangeReturn, GstElement*, GstState>(&set_state_threadpool_, &gst_element_set_state, pipeline_, state);
} }
bool GstEnginePipeline::Seek(const qint64 nanosec) { bool GstEnginePipeline::Seek(const qint64 nanosec) {

View File

@@ -56,7 +56,7 @@ class CollectionBackend;
class Player; class Player;
class QueuedItemDelegate : public QStyledItemDelegate { class QueuedItemDelegate : public QStyledItemDelegate {
public: public:
explicit QueuedItemDelegate(QObject *parent, int indicator_column = Playlist::Column_Title); explicit QueuedItemDelegate(QObject *parent, int indicator_column = Playlist::Column_Title);
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override; void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
@@ -145,14 +145,14 @@ class TagCompletionModel : public QStringListModel {
class TagCompleter : public QCompleter { class TagCompleter : public QCompleter {
Q_OBJECT Q_OBJECT
public: public:
explicit TagCompleter(CollectionBackend *backend, Playlist::Column column, QLineEdit *editor); explicit TagCompleter(CollectionBackend *backend, Playlist::Column column, QLineEdit *editor);
~TagCompleter() override; ~TagCompleter() override;
private slots: private slots:
void ModelReady(QFuture<TagCompletionModel*> future); void ModelReady(QFuture<TagCompletionModel*> future);
private: private:
QLineEdit *editor_; QLineEdit *editor_;
}; };

View File

@@ -143,5 +143,5 @@ void PlaylistHeader::enterEvent(QEvent*) {
} }
void PlaylistHeader::ResetColumns() { void PlaylistHeader::ResetColumns() {
view_->ResetColumns(); view_->ResetHeaderState();
} }

View File

@@ -128,7 +128,6 @@ void PlaylistProxyStyle::drawPrimitive(PrimitiveElement element, const QStyleOpt
} }
PlaylistView::PlaylistView(QWidget *parent) PlaylistView::PlaylistView(QWidget *parent)
: QTreeView(parent), : QTreeView(parent),
app_(nullptr), app_(nullptr),
@@ -143,11 +142,10 @@ PlaylistView::PlaylistView(QWidget *parent)
background_image_keep_aspect_ratio_(true), background_image_keep_aspect_ratio_(true),
blur_radius_(AppearanceSettingsPage::kDefaultBlurRadius), blur_radius_(AppearanceSettingsPage::kDefaultBlurRadius),
opacity_level_(AppearanceSettingsPage::kDefaultOpacityLevel), opacity_level_(AppearanceSettingsPage::kDefaultOpacityLevel),
initialized_(false),
background_initialized_(false), background_initialized_(false),
setting_initial_header_layout_(false), set_initial_header_layout_(false),
read_only_settings_(true), header_state_loaded_(false),
state_loaded_(false), header_state_restored_(false),
previous_background_image_opacity_(0.0), previous_background_image_opacity_(0.0),
fade_animation_(new QTimeLine(1000, this)), fade_animation_(new QTimeLine(1000, this)),
force_background_redraw_(false), force_background_redraw_(false),
@@ -169,20 +167,28 @@ PlaylistView::PlaylistView(QWidget *parent)
currenttrack_pause_(":/pictures/currenttrack_pause.png"), currenttrack_pause_(":/pictures/currenttrack_pause.png"),
cached_current_row_row_(-1), cached_current_row_row_(-1),
drop_indicator_row_(-1), drop_indicator_row_(-1),
drag_over_(false) { drag_over_(false),
column_alignment_(DefaultColumnAlignment()) {
setHeader(header_); setHeader(header_);
header_->setSectionsMovable(true); header_->setSectionsMovable(true);
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0) #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
header_->setFirstSectionMovable(true); header_->setFirstSectionMovable(true);
#endif #endif
header_->setSortIndicator(Playlist::Column_Title, Qt::AscendingOrder);
setStyle(style_); setStyle(style_);
setMouseTracking(true); setMouseTracking(true);
setAlternatingRowColors(true);
setAttribute(Qt::WA_MacShowFocusRect, false);
#ifdef Q_OS_MACOS
setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
#endif
connect(header_, SIGNAL(sectionResized(int, int, int)), SLOT(SaveGeometry())); connect(header_, SIGNAL(sectionResized(int, int, int)), SLOT(SetHeaderState()));
connect(header_, SIGNAL(sectionMoved(int, int, int)), SLOT(SaveGeometry())); connect(header_, SIGNAL(sectionMoved(int, int, int)), SLOT(SetHeaderState()));
connect(header_, SIGNAL(sortIndicatorChanged(int, Qt::SortOrder)), SLOT(SaveGeometry())); connect(header_, SIGNAL(sortIndicatorChanged(int, Qt::SortOrder)), SLOT(SetHeaderState()));
connect(header_, SIGNAL(SectionVisibilityChanged(int, bool)), SLOT(SaveGeometry())); connect(header_, SIGNAL(SectionVisibilityChanged(int, bool)), SLOT(SetHeaderState()));
connect(header_, SIGNAL(sectionResized(int, int, int)), SLOT(InvalidateCachedCurrentPixmap())); connect(header_, SIGNAL(sectionResized(int, int, int)), SLOT(InvalidateCachedCurrentPixmap()));
connect(header_, SIGNAL(sectionMoved(int, int, int)), SLOT(InvalidateCachedCurrentPixmap())); connect(header_, SIGNAL(sectionMoved(int, int, int)), SLOT(InvalidateCachedCurrentPixmap()));
@@ -196,26 +202,17 @@ PlaylistView::PlaylistView(QWidget *parent)
horizontalScrollBar()->installEventFilter(this); horizontalScrollBar()->installEventFilter(this);
verticalScrollBar()->installEventFilter(this); verticalScrollBar()->installEventFilter(this);
setAlternatingRowColors(true);
setAttribute(Qt::WA_MacShowFocusRect, false);
#ifdef Q_OS_MACOS
setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
#endif
// For fading // For fading
connect(fade_animation_, SIGNAL(valueChanged(qreal)), SLOT(FadePreviousBackgroundImage(qreal))); connect(fade_animation_, SIGNAL(valueChanged(qreal)), SLOT(FadePreviousBackgroundImage(qreal)));
fade_animation_->setDirection(QTimeLine::Backward); // 1.0 -> 0.0 fade_animation_->setDirection(QTimeLine::Backward); // 1.0 -> 0.0
initialized_ = true;
} }
PlaylistView::~PlaylistView() { PlaylistView::~PlaylistView() {
delete style_; style_->deleteLater();
} }
void PlaylistView::SetApplication(Application *app) { void PlaylistView::Init(Application *app) {
Q_ASSERT(app); Q_ASSERT(app);
app_ = app; app_ = app;
@@ -263,28 +260,6 @@ void PlaylistView::SetItemDelegates() {
} }
void PlaylistView::SetPlaylist(Playlist *playlist) {
if (playlist_) {
disconnect(playlist_, SIGNAL(MaybeAutoscroll(Playlist::AutoScroll)), this, SLOT(MaybeAutoscroll(Playlist::AutoScroll)));
disconnect(playlist_, SIGNAL(destroyed()), this, SLOT(PlaylistDestroyed()));
disconnect(playlist_, SIGNAL(QueueChanged()), this, SLOT(update()));
}
playlist_ = playlist;
LoadGeometry();
ReloadSettings();
setFocus();
read_only_settings_ = false;
JumpToLastPlayedTrack();
connect(playlist_, SIGNAL(RestoreFinished()), SLOT(JumpToLastPlayedTrack()));
connect(playlist_, SIGNAL(MaybeAutoscroll(Playlist::AutoScroll)), SLOT(MaybeAutoscroll(Playlist::AutoScroll)));
connect(playlist_, SIGNAL(destroyed()), SLOT(PlaylistDestroyed()));
connect(playlist_, SIGNAL(QueueChanged()), SLOT(update()));
}
void PlaylistView::setModel(QAbstractItemModel *m) { void PlaylistView::setModel(QAbstractItemModel *m) {
if (model()) { if (model()) {
@@ -301,44 +276,106 @@ void PlaylistView::setModel(QAbstractItemModel *m) {
} }
void PlaylistView::LoadGeometry() { void PlaylistView::SetPlaylist(Playlist *playlist) {
if (!state_loaded_) { if (playlist_) {
QSettings s; disconnect(playlist_, SIGNAL(MaybeAutoscroll(Playlist::AutoScroll)), this, SLOT(MaybeAutoscroll(Playlist::AutoScroll)));
s.beginGroup(Playlist::kSettingsGroup); disconnect(playlist_, SIGNAL(destroyed()), this, SLOT(PlaylistDestroyed()));
state_ = s.value("state").toByteArray(); disconnect(playlist_, SIGNAL(QueueChanged()), this, SLOT(update()));
state_loaded_ = true;
s.endGroup();
} }
if (!header_->RestoreState(state_)) { playlist_ = playlist;
// Maybe we're upgrading from a version that persisted the state with QHeaderView. RestoreHeaderState();
if (!header_->restoreState(state_)) { setFocus();
header_->HideSection(Playlist::Column_AlbumArtist); JumpToLastPlayedTrack();
header_->HideSection(Playlist::Column_Performer);
header_->HideSection(Playlist::Column_Composer); connect(playlist_, SIGNAL(RestoreFinished()), SLOT(JumpToLastPlayedTrack()));
header_->HideSection(Playlist::Column_Year); connect(playlist_, SIGNAL(MaybeAutoscroll(Playlist::AutoScroll)), SLOT(MaybeAutoscroll(Playlist::AutoScroll)));
header_->HideSection(Playlist::Column_OriginalYear); connect(playlist_, SIGNAL(destroyed()), SLOT(PlaylistDestroyed()));
header_->HideSection(Playlist::Column_Disc); connect(playlist_, SIGNAL(QueueChanged()), SLOT(update()));
header_->HideSection(Playlist::Column_Genre);
header_->HideSection(Playlist::Column_Filename); }
header_->HideSection(Playlist::Column_BaseFilename);
header_->HideSection(Playlist::Column_Filesize); void PlaylistView::LoadHeaderState() {
header_->HideSection(Playlist::Column_DateCreated);
header_->HideSection(Playlist::Column_DateModified); QSettings s;
header_->HideSection(Playlist::Column_PlayCount); s.beginGroup(Playlist::kSettingsGroup);
header_->HideSection(Playlist::Column_SkipCount); if (s.contains("state")) header_state_ = s.value("state").toByteArray();
header_->HideSection(Playlist::Column_LastPlayed); if (s.contains("column_alignments")) column_alignment_ = s.value("column_alignments").value<ColumnAlignmentMap>();
header_->HideSection(Playlist::Column_Comment); s.endGroup();
header_->HideSection(Playlist::Column_Grouping);
header_->HideSection(Playlist::Column_Mood); if (column_alignment_.isEmpty()) {
column_alignment_ = DefaultColumnAlignment();
}
header_state_loaded_ = true;
}
void PlaylistView::SetHeaderState() {
if (!header_state_loaded_) return;
header_state_ = header_->SaveState();
}
void PlaylistView::ResetHeaderState() {
set_initial_header_layout_ = true;
header_state_ = header_->ResetState();
RestoreHeaderState();
}
void PlaylistView::RestoreHeaderState() {
if (!header_state_loaded_) LoadHeaderState();
if (header_state_.isEmpty() || !header_->RestoreState(header_state_)) {
set_initial_header_layout_ = true;
}
if (set_initial_header_layout_) {
header_->HideSection(Playlist::Column_AlbumArtist);
header_->HideSection(Playlist::Column_Performer);
header_->HideSection(Playlist::Column_Composer);
header_->HideSection(Playlist::Column_Year);
header_->HideSection(Playlist::Column_OriginalYear);
header_->HideSection(Playlist::Column_Disc);
header_->HideSection(Playlist::Column_Genre);
header_->HideSection(Playlist::Column_Filename);
header_->HideSection(Playlist::Column_BaseFilename);
header_->HideSection(Playlist::Column_Filesize);
header_->HideSection(Playlist::Column_DateCreated);
header_->HideSection(Playlist::Column_DateModified);
header_->HideSection(Playlist::Column_PlayCount);
header_->HideSection(Playlist::Column_SkipCount);
header_->HideSection(Playlist::Column_LastPlayed);
header_->HideSection(Playlist::Column_Comment);
header_->HideSection(Playlist::Column_Grouping);
header_->HideSection(Playlist::Column_Mood);
header_->moveSection(header_->visualIndex(Playlist::Column_Track), 0);
header_->SetStretchEnabled(true);
header_->SetColumnWidth(Playlist::Column_Track, 0.04);
header_->SetColumnWidth(Playlist::Column_Title, 0.26);
header_->SetColumnWidth(Playlist::Column_Artist, 0.20);
header_->SetColumnWidth(Playlist::Column_Album, 0.14);
header_->SetColumnWidth(Playlist::Column_Length, 0.03);
header_->SetColumnWidth(Playlist::Column_Samplerate, 0.07);
header_->SetColumnWidth(Playlist::Column_Bitdepth, 0.07);
header_->SetColumnWidth(Playlist::Column_Bitrate, 0.07);
header_->SetColumnWidth(Playlist::Column_Filetype, 0.06);
header_->SetColumnWidth(Playlist::Column_Source, 0.06);
header_state_ = header_->SaveState();
header_->RestoreState(header_state_);
set_initial_header_layout_ = false;
header_->moveSection(header_->visualIndex(Playlist::Column_Track), 0);
setting_initial_header_layout_ = true;
}
else {
setting_initial_header_layout_ = true;
}
} }
// Make sure at least one column is visible // Make sure at least one column is visible
@@ -353,12 +390,9 @@ void PlaylistView::LoadGeometry() {
header_->ShowSection(Playlist::Column_Title); header_->ShowSection(Playlist::Column_Title);
} }
} header_state_restored_ = true;
void PlaylistView::SaveGeometry() { emit ColumnAlignmentChanged(column_alignment_);
if (!initialized_ || !state_loaded_) return;
state_ = header_->SaveState();
} }
@@ -805,11 +839,9 @@ void PlaylistView::JumpToLastPlayedTrack() {
last_current_item_ = last_played; last_current_item_ = last_played;
setCurrentIndex(last_current_item_); setCurrentIndex(last_current_item_);
currently_autoscrolling_ = true;
// Scroll to the item // Scroll to the item
currently_autoscrolling_ = true;
scrollTo(last_played, QAbstractItemView::PositionAtCenter); scrollTo(last_played, QAbstractItemView::PositionAtCenter);
currently_autoscrolling_ = false; currently_autoscrolling_ = false;
} }
@@ -1024,11 +1056,6 @@ void PlaylistView::ReloadSettings() {
select_track_ = s.value("select_track", false).toBool(); select_track_ = s.value("select_track", false).toBool();
s.endGroup(); s.endGroup();
s.beginGroup(Playlist::kSettingsGroup);
bool stretch = s.value("stretch", true).toBool();
column_alignment_ = s.value("column_alignments").value<ColumnAlignmentMap>();
s.endGroup();
s.beginGroup(AppearanceSettingsPage::kSettingsGroup); s.beginGroup(AppearanceSettingsPage::kSettingsGroup);
QVariant background_image_type_var = s.value(AppearanceSettingsPage::kBackgroundImageType); QVariant background_image_type_var = s.value(AppearanceSettingsPage::kBackgroundImageType);
QVariant background_image_position_var = s.value(AppearanceSettingsPage::kBackgroundImagePosition); QVariant background_image_position_var = s.value(AppearanceSettingsPage::kBackgroundImagePosition);
@@ -1042,34 +1069,9 @@ void PlaylistView::ReloadSettings() {
int opacity_level = s.value(AppearanceSettingsPage::kOpacityLevel, AppearanceSettingsPage::kDefaultOpacityLevel).toInt(); int opacity_level = s.value(AppearanceSettingsPage::kOpacityLevel, AppearanceSettingsPage::kDefaultOpacityLevel).toInt();
s.endGroup(); s.endGroup();
if (setting_initial_header_layout_) {
header_->SetStretchEnabled(stretch);
header_->SetColumnWidth(Playlist::Column_Track, 0.02);
header_->SetColumnWidth(Playlist::Column_Title, 0.16);
header_->SetColumnWidth(Playlist::Column_Artist, 0.12);
header_->SetColumnWidth(Playlist::Column_Album, 0.12);
header_->SetColumnWidth(Playlist::Column_Length, 0.03);
header_->SetColumnWidth(Playlist::Column_Samplerate, 0.07);
header_->SetColumnWidth(Playlist::Column_Bitdepth, 0.07);
header_->SetColumnWidth(Playlist::Column_Bitrate, 0.07);
header_->SetColumnWidth(Playlist::Column_Filetype, 0.06);
header_->SetColumnWidth(Playlist::Column_Source, 0.06);
setting_initial_header_layout_ = false;
}
if (currently_glowing_ && glow_enabled_ && isVisible()) StartGlowing(); if (currently_glowing_ && glow_enabled_ && isVisible()) StartGlowing();
if (!glow_enabled_) StopGlowing(); if (!glow_enabled_) StopGlowing();
if (column_alignment_.isEmpty()) {
column_alignment_ = DefaultColumnAlignment();
}
emit ColumnAlignmentChanged(column_alignment_);
// Background: // Background:
AppearanceSettingsPage::BackgroundImageType background_image_type(AppearanceSettingsPage::BackgroundImageType_Default); AppearanceSettingsPage::BackgroundImageType background_image_type(AppearanceSettingsPage::BackgroundImageType_Default);
if (background_image_type_var.isValid()) { if (background_image_type_var.isValid()) {
@@ -1144,7 +1146,7 @@ void PlaylistView::ReloadSettings() {
void PlaylistView::SaveSettings() { void PlaylistView::SaveSettings() {
if (!initialized_ || read_only_settings_) return; if (!header_state_loaded_) return;
QSettings s; QSettings s;
s.beginGroup(Playlist::kSettingsGroup); s.beginGroup(Playlist::kSettingsGroup);
@@ -1156,9 +1158,10 @@ void PlaylistView::SaveSettings() {
void PlaylistView::StretchChanged(const bool stretch) { void PlaylistView::StretchChanged(const bool stretch) {
if (!initialized_) return; if (!header_state_loaded_) return;
setHorizontalScrollBarPolicy(stretch ? Qt::ScrollBarAlwaysOff : Qt::ScrollBarAsNeeded); setHorizontalScrollBarPolicy(stretch ? Qt::ScrollBarAlwaysOff : Qt::ScrollBarAsNeeded);
SaveGeometry(); SetHeaderState();
} }
@@ -1354,19 +1357,3 @@ void PlaylistView::focusInEvent(QFocusEvent *event) {
} }
} }
void PlaylistView::ResetColumns() {
QSettings s;
s.beginGroup(Playlist::kSettingsGroup);
s.remove("state");
s.endGroup();
state_loaded_ = false;
read_only_settings_ = true;
setting_initial_header_layout_ = true;
ReloadSettings();
LoadGeometry();
read_only_settings_ = false;
SetPlaylist(playlist_);
}

View File

@@ -98,7 +98,7 @@ class PlaylistView : public QTreeView {
static ColumnAlignmentMap DefaultColumnAlignment(); static ColumnAlignmentMap DefaultColumnAlignment();
void SetApplication(Application *app); void Init(Application *app);
void SetItemDelegates(); void SetItemDelegates();
void SetPlaylist(Playlist *playlist); void SetPlaylist(Playlist *playlist);
void RemoveSelected(); void RemoveSelected();
@@ -109,14 +109,13 @@ class PlaylistView : public QTreeView {
AppearanceSettingsPage::BackgroundImageType background_image_type() const { return background_image_type_; } AppearanceSettingsPage::BackgroundImageType background_image_type() const { return background_image_type_; }
Qt::Alignment column_alignment(int section) const; Qt::Alignment column_alignment(int section) const;
void ResetColumns(); void ResetHeaderState();
// QTreeView // QTreeView
void setModel(QAbstractItemModel *model) override; void setModel(QAbstractItemModel *model) override;
public slots: public slots:
void ReloadSettings(); void ReloadSettings();
void SaveGeometry();
void SaveSettings(); void SaveSettings();
void SetColumnAlignment(const int section, const Qt::Alignment alignment); void SetColumnAlignment(const int section, const Qt::Alignment alignment);
void JumpToCurrentlyPlayingTrack(); void JumpToCurrentlyPlayingTrack();
@@ -163,6 +162,7 @@ class PlaylistView : public QTreeView {
void closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint) override; void closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint) override;
private slots: private slots:
void SetHeaderState();
void InhibitAutoscrollTimeout(); void InhibitAutoscrollTimeout();
void MaybeAutoscroll(const Playlist::AutoScroll autoscroll); void MaybeAutoscroll(const Playlist::AutoScroll autoscroll);
void InvalidateCachedCurrentPixmap(); void InvalidateCachedCurrentPixmap();
@@ -179,7 +179,9 @@ class PlaylistView : public QTreeView {
void AlbumCoverLoaded(const Song &song, AlbumCoverLoaderResult result = AlbumCoverLoaderResult()); void AlbumCoverLoaded(const Song &song, AlbumCoverLoaderResult result = AlbumCoverLoaderResult());
private: private:
void LoadGeometry(); void LoadHeaderState();
void RestoreHeaderState();
void ReloadBarPixmaps(); void ReloadBarPixmaps();
QList<QPixmap> LoadBarPixmap(const QString &filename); QList<QPixmap> LoadBarPixmap(const QString &filename);
void UpdateCachedCurrentRowPixmap(QStyleOptionViewItem option, const QModelIndex &idx); void UpdateCachedCurrentRowPixmap(QStyleOptionViewItem option, const QModelIndex &idx);
@@ -219,11 +221,12 @@ class PlaylistView : public QTreeView {
int blur_radius_; int blur_radius_;
int opacity_level_; int opacity_level_;
bool initialized_;
bool background_initialized_; bool background_initialized_;
bool setting_initial_header_layout_; bool set_initial_header_layout_;
bool read_only_settings_; bool read_only_settings_;
bool state_loaded_; bool header_state_loaded_;
bool header_state_restored_;
bool header_state_readonly_;
QImage background_image_; QImage background_image_;
QImage current_song_cover_art_; QImage current_song_cover_art_;
@@ -272,10 +275,9 @@ class PlaylistView : public QTreeView {
int drop_indicator_row_; int drop_indicator_row_;
bool drag_over_; bool drag_over_;
QByteArray header_state_;
ColumnAlignmentMap column_alignment_; ColumnAlignmentMap column_alignment_;
QByteArray state_;
Song song_playing_; Song song_playing_;
}; };

View File

@@ -67,21 +67,21 @@ void StretchHeaderView::NormaliseWidths(const QList<int>& sections) {
if (!sections.isEmpty()) { if (!sections.isEmpty()) {
selected_sum = 0.0; selected_sum = 0.0;
for (int i=0 ; i<count() ; ++i) for (int i = 0 ; i < count() ; ++i)
if (sections.contains(i)) if (sections.contains(i))
selected_sum += column_widths_[i]; selected_sum += column_widths_[i];
} }
if (total_sum != 0.0 && !qFuzzyCompare(total_sum, 1.0)) { if (total_sum != 0.0 && !qFuzzyCompare(total_sum, 1.0)) {
const ColumnWidthType mult = (selected_sum + (1.0 - total_sum)) / selected_sum; const ColumnWidthType mult = (selected_sum + (1.0 - total_sum)) / selected_sum;
for (int i=0 ; i<column_widths_.count() ; ++i) { for (int i = 0 ; i < column_widths_.count() ; ++i) {
if (sections.isEmpty() || sections.contains(i)) if (sections.isEmpty() || sections.contains(i))
column_widths_[i] *= mult; column_widths_[i] *= mult;
} }
} }
} }
void StretchHeaderView::UpdateWidths(const QList<int>& sections) { void StretchHeaderView::UpdateWidths(const QList<int> &sections) {
if (!stretch_enabled_) if (!stretch_enabled_)
return; return;
@@ -116,7 +116,7 @@ void StretchHeaderView::HideSection(const int logical) {
// Would this hide the last section? // Would this hide the last section?
bool all_hidden = true; bool all_hidden = true;
for (int i=0 ; i<count() ; ++i) { for (int i = 0 ; i < count() ; ++i) {
if (i != logical && !isSectionHidden(i) && sectionSize(i) > 0) { if (i != logical && !isSectionHidden(i) && sectionSize(i) > 0) {
all_hidden = false; all_hidden = false;
break; break;
@@ -134,6 +134,7 @@ void StretchHeaderView::HideSection(const int logical) {
column_widths_[logical] = 0.0; column_widths_[logical] = 0.0;
NormaliseWidths(); NormaliseWidths();
UpdateWidths(); UpdateWidths();
} }
void StretchHeaderView::ShowSection(int logical) { void StretchHeaderView::ShowSection(int logical) {
@@ -145,7 +146,7 @@ void StretchHeaderView::ShowSection(int logical) {
// How many sections are visible already? // How many sections are visible already?
int visible_count = 0; int visible_count = 0;
for (int i=0 ; i<count() ; ++i) { for (int i = 0 ; i < count() ; ++i) {
if (!isSectionHidden(i)) if (!isSectionHidden(i))
visible_count ++; visible_count ++;
} }
@@ -153,6 +154,7 @@ void StretchHeaderView::ShowSection(int logical) {
column_widths_[logical] = visible_count == 0 ? 1.0 : 1.0 / visible_count; column_widths_[logical] = visible_count == 0 ? 1.0 : 1.0 / visible_count;
NormaliseWidths(); NormaliseWidths();
UpdateWidths(); UpdateWidths();
} }
void StretchHeaderView::SetSectionHidden(const int logical, const bool hidden) { void StretchHeaderView::SetSectionHidden(const int logical, const bool hidden) {
@@ -241,7 +243,7 @@ void StretchHeaderView::SetColumnWidth(const int logical, const ColumnWidthType
column_widths_[logical] = width; column_widths_[logical] = width;
QList<int> other_columns; QList<int> other_columns;
for (int i=0 ; i<count() ; ++i) for (int i = 0 ; i < count() ; ++i)
if (!isSectionHidden(i) && i != logical) if (!isSectionHidden(i) && i != logical)
other_columns << i; other_columns << i;
@@ -330,3 +332,37 @@ QByteArray StretchHeaderView::SaveState() const {
return ret; return ret;
} }
QByteArray StretchHeaderView::ResetState() {
QByteArray ret;
QDataStream s(&ret, QIODevice::WriteOnly);
s.setVersion(QDataStream::Qt_5_6);
s << kMagicNumber;
stretch_enabled_ = true;
column_widths_.resize(count());
std::fill(column_widths_.begin(), column_widths_.end(), 1.0 / count());
QList<int> visual_indices;
QList<int> pixel_widths;
for (int i = 0 ; i < count() ; ++i) {
pixel_widths << 10;
visual_indices << count();
}
s << stretch_enabled_;
s << pixel_widths;
s << visual_indices;
s << column_widths_;
s << int(Qt::AscendingOrder);
s << 0;
RestoreState(ret);
return ret;
}

View File

@@ -52,6 +52,7 @@ class StretchHeaderView : public QHeaderView {
// Use these instead of QHeaderView::restoreState and QHeaderView::saveState to persist the proportional values directly and avoid floating point errors over time. // Use these instead of QHeaderView::restoreState and QHeaderView::saveState to persist the proportional values directly and avoid floating point errors over time.
bool RestoreState(const QByteArray &sdata); bool RestoreState(const QByteArray &sdata);
QByteArray SaveState() const; QByteArray SaveState() const;
QByteArray ResetState();
// Hides a section and resizes all other sections to fill the gap. Does nothing if you try to hide the last section. // Hides a section and resizes all other sections to fill the gap. Does nothing if you try to hide the last section.
void HideSection(const int logical); void HideSection(const int logical);
@@ -85,7 +86,7 @@ class StretchHeaderView : public QHeaderView {
private: private:
// Scales column_widths_ values so the total is 1.0. // Scales column_widths_ values so the total is 1.0.
void NormaliseWidths(const QList<int>& sections = QList<int>()); void NormaliseWidths(const QList<int> &sections = QList<int>());
// Resizes the actual columns to make them match the proportional values in column_widths_. // Resizes the actual columns to make them match the proportional values in column_widths_.
void UpdateWidths(const QList<int>& sections = QList<int>()); void UpdateWidths(const QList<int>& sections = QList<int>());