1 /*
2  * Strawberry Music Player
3  * This file was part of Clementine.
4  * Copyright 2010, David Sansome <me@davidsansome.com>
5  * Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
6  *
7  * Strawberry is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * Strawberry is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with Strawberry.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #include "config.h"
23 
24 #include <QAbstractItemModel>
25 #include <QWidget>
26 #include <QFlags>
27 #include <QVariant>
28 #include <QString>
29 #include <QtAlgorithms>
30 #include <QMenu>
31 #include <QAction>
32 #include <QActionGroup>
33 #include <QSettings>
34 #include <QEvent>
35 #include <QContextMenuEvent>
36 #include <QEnterEvent>
37 
38 #include "playlistheader.h"
39 #include "playlistview.h"
40 
41 #include "settings/playlistsettingspage.h"
42 
PlaylistHeader(Qt::Orientation orientation,PlaylistView * view,QWidget * parent)43 PlaylistHeader::PlaylistHeader(Qt::Orientation orientation, PlaylistView *view, QWidget *parent)
44     : StretchHeaderView(orientation, parent),
45       view_(view),
46       menu_section_(0),
47       menu_(new QMenu(this)),
48       action_hide_(nullptr),
49       action_reset_(nullptr),
50       action_stretch_(nullptr),
51       action_rating_lock_(nullptr),
52       action_align_left_(nullptr),
53       action_align_center_(nullptr),
54       action_align_right_(nullptr) {
55 
56   action_hide_ = menu_->addAction(tr("&Hide..."), this, &PlaylistHeader::HideCurrent);
57   action_stretch_ = menu_->addAction(tr("&Stretch columns to fit window"), this, &PlaylistHeader::ToggleStretchEnabled);
58   action_reset_ = menu_->addAction(tr("&Reset columns to default"), this, &PlaylistHeader::ResetColumns);
59   action_rating_lock_ = menu_->addAction(tr("&Lock rating"), this, &PlaylistHeader::ToggleRatingEditStatus);
60   action_rating_lock_->setCheckable(true);
61   menu_->addSeparator();
62 
63   QMenu *align_menu = new QMenu(tr("&Align text"), this);
64   QActionGroup *align_group = new QActionGroup(this);
65   action_align_left_ = new QAction(tr("&Left"), align_group);
66   action_align_center_ = new QAction(tr("&Center"), align_group);
67   action_align_right_ = new QAction(tr("&Right"), align_group);
68 
69   action_align_left_->setCheckable(true);
70   action_align_center_->setCheckable(true);
71   action_align_right_->setCheckable(true);
72   align_menu->addActions(align_group->actions());
73 
74   QObject::connect(align_group, &QActionGroup::triggered, this, &PlaylistHeader::SetColumnAlignment);
75 
76   menu_->addMenu(align_menu);
77   menu_->addSeparator();
78 
79   action_stretch_->setCheckable(true);
80   action_stretch_->setChecked(is_stretch_enabled());
81 
82   QObject::connect(this, &PlaylistHeader::StretchEnabledChanged, action_stretch_, &QAction::setChecked);
83 
84   QSettings s;
85   s.beginGroup(PlaylistSettingsPage::kSettingsGroup);
86   action_rating_lock_->setChecked(s.value("rating_locked", false).toBool());
87   s.endGroup();
88 
89 }
90 
contextMenuEvent(QContextMenuEvent * e)91 void PlaylistHeader::contextMenuEvent(QContextMenuEvent *e) {
92 
93   menu_section_ = logicalIndexAt(e->pos());
94 
95   if (menu_section_ == -1 || (menu_section_ == logicalIndex(0) && logicalIndex(1) == -1)) {
96     action_hide_->setVisible(false);
97   }
98   else {
99     action_hide_->setVisible(true);
100 
101     QString title(model()->headerData(menu_section_, Qt::Horizontal).toString());
102     action_hide_->setText(tr("&Hide %1").arg(title));
103 
104     Qt::Alignment alignment = view_->column_alignment(menu_section_);
105     if      (alignment & Qt::AlignLeft)    action_align_left_->setChecked(true);
106     else if (alignment & Qt::AlignHCenter) action_align_center_->setChecked(true);
107     else if (alignment & Qt::AlignRight)   action_align_right_->setChecked(true);
108 
109     // Show rating lock action only for ratings section
110     action_rating_lock_->setVisible(menu_section_ == Playlist::Column_Rating);
111 
112   }
113 
114   qDeleteAll(show_actions_);
115   show_actions_.clear();
116   for (int i = 0; i < count(); ++i) {
117     AddColumnAction(i);
118   }
119 
120   menu_->popup(e->globalPos());
121 
122 }
123 
AddColumnAction(int index)124 void PlaylistHeader::AddColumnAction(int index) {
125 
126 #ifndef HAVE_MOODBAR
127   if (index == Playlist::Column_Mood) {
128     return;
129   }
130 #endif
131 
132   QString title(model()->headerData(index, Qt::Horizontal).toString());
133 
134   QAction *action = menu_->addAction(title);
135   action->setCheckable(true);
136   action->setChecked(!isSectionHidden(index));
137   show_actions_ << action;
138 
139   QObject::connect(action, &QAction::triggered, this, [this, index]() { ToggleVisible(index); });
140 
141 }
142 
HideCurrent()143 void PlaylistHeader::HideCurrent() {
144   if (menu_section_ == -1) return;
145 
146   SetSectionHidden(menu_section_, true);
147 }
148 
SetColumnAlignment(QAction * action)149 void PlaylistHeader::SetColumnAlignment(QAction *action) {
150 
151   Qt::Alignment alignment = Qt::AlignVCenter;
152 
153   if (action == action_align_left_) alignment |= Qt::AlignLeft;
154   if (action == action_align_center_) alignment |= Qt::AlignHCenter;
155   if (action == action_align_right_) alignment |= Qt::AlignRight;
156 
157   view_->SetColumnAlignment(menu_section_, alignment);
158 
159 }
160 
ToggleVisible(const int section)161 void PlaylistHeader::ToggleVisible(const int section) {
162   SetSectionHidden(section, !isSectionHidden(section));
163   emit SectionVisibilityChanged(section, !isSectionHidden(section));
164 }
165 
166 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
enterEvent(QEnterEvent *)167 void PlaylistHeader::enterEvent(QEnterEvent*) {
168 #else
169 void PlaylistHeader::enterEvent(QEvent*) {
170 #endif
171   emit MouseEntered();
172 }
173 
174 void PlaylistHeader::ResetColumns() {
175   view_->ResetHeaderState();
176 }
177 
178 void PlaylistHeader::ToggleRatingEditStatus() {
179   emit SectionRatingLockStatusChanged(action_rating_lock_->isChecked());
180 }
181