1 /*
2  * Cantata
3  *
4  * Copyright (c) 2018-2020 Craig Drummond <craig.p.drummond@gmail.com>
5  *
6  * ----
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; see the file COPYING.  If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 
24 #include "categorizedview.h"
25 #include "kcategorizedview/kcategorydrawer.h"
26 #include "kcategorizedview/kcategorizedsortfilterproxymodel.h"
27 #include "config.h"
28 #include "icons.h"
29 #include "support/utils.h"
30 #include <QMimeData>
31 #include <QDrag>
32 #include <QMouseEvent>
33 #include <QMenu>
34 #include <QPainter>
35 #include <QPaintEvent>
36 #include <QModelIndex>
37 #include <QApplication>
38 #include <QLinearGradient>
39 #include <algorithm>
40 
41 class CategoryDrawer : public KCategoryDrawer
42 {
43 public:
CategoryDrawer(KCategorizedView * view)44     CategoryDrawer(KCategorizedView *view)
45         : KCategoryDrawer(view) {
46     }
47 
~CategoryDrawer()48     ~CategoryDrawer() override {
49     }
50 
drawCategory(const QModelIndex & index,int,const QStyleOption & option,QPainter * painter) const51     void drawCategory(const QModelIndex &index, int /*sortRole*/, const QStyleOption &option, QPainter *painter) const override
52     {
53         const QString category = index.model()->data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
54         QFont font(QApplication::font());
55         font.setBold(true);
56         const QFontMetrics fontMetrics = QFontMetrics(font);
57 
58         QRect r(option.rect);
59         r.setTop(r.top() + 2);
60         r.setLeft(r.left() + 2);
61         r.setHeight(fontMetrics.height());
62         r.setRight(r.right() - 2);
63 
64         painter->save();
65         painter->setFont(font);
66         QColor col(option.palette.text().color());
67         painter->setPen(col);
68         painter->drawText(r, Qt::AlignLeft | Qt::AlignVCenter, category);
69 
70         r.adjust(0, 4, 0, 4);
71 
72         double alpha=0.5;
73         double fadeSize=64.0;
74         double fadePos=fadeSize/r.width();
75         QLinearGradient grad(r.bottomLeft(), r.bottomRight());
76 
77         col.setAlphaF(Qt::RightToLeft==option.direction ? 0.0 : alpha);
78         grad.setColorAt(0, col);
79         col.setAlphaF(alpha);
80         grad.setColorAt(fadePos, col);
81         grad.setColorAt(1.0-fadePos, col);
82         col.setAlphaF(Qt::LeftToRight==option.direction ? 0.0 : alpha);
83         grad.setColorAt(1, col);
84         painter->setPen(QPen(grad, 1));
85 
86         painter->drawLine(r.bottomLeft(), r.bottomRight());
87         painter->restore();
88     }
89 };
90 
CategorizedView(QWidget * parent)91 CategorizedView::CategorizedView(QWidget *parent)
92     : KCategorizedView(parent)
93     , eventFilter(nullptr)
94     , menu(nullptr)
95     , zoomLevel(1.0)
96 {
97     proxy=new KCategorizedSortFilterProxyModel(this);
98     proxy->setCategorizedModel(true);
99     setCategoryDrawer(new CategoryDrawer(this));
100     setDragEnabled(true);
101     setContextMenuPolicy(Qt::NoContextMenu);
102     setDragDropMode(QAbstractItemView::DragOnly);
103     setSelectionMode(QAbstractItemView::ExtendedSelection);
104     setAlternatingRowColors(false);
105     setUniformItemSizes(true);
106     setAttribute(Qt::WA_MouseTracking);
107     setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
108     connect(this, SIGNAL(customContextMenuRequested(const QPoint &)), SLOT(showCustomContextMenu(const QPoint &)));
109     connect(this, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(checkDoubleClick(const QModelIndex &)));
110     connect(this, SIGNAL(clicked(const QModelIndex &)), this, SLOT(checkClicked(const QModelIndex &)));
111     connect(this, SIGNAL(activated(const QModelIndex &)), this, SLOT(checkActivated(const QModelIndex &)));
112 
113     setViewMode(QListView::IconMode);
114     setResizeMode(QListView::Adjust);
115     setWordWrap(true);
116 }
117 
~CategorizedView()118 CategorizedView::~CategorizedView()
119 {
120 }
121 
selectionChanged(const QItemSelection & selected,const QItemSelection & deselected)122 void CategorizedView::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
123 {
124     //KCategorizedView::selectionChanged(selected, deselected);
125     bool haveSelection=haveSelectedItems();
126 
127     setContextMenuPolicy(haveSelection ? Qt::ActionsContextMenu : (menu ? Qt::CustomContextMenu : Qt::NoContextMenu));
128     emit itemsSelected(haveSelection);
129 }
130 
haveSelectedItems() const131 bool CategorizedView::haveSelectedItems() const
132 {
133     // Dont need the sorted type of 'selectedIndexes' here...
134     return selectionModel() && selectionModel()->selectedIndexes().count()>0;
135 }
136 
haveUnSelectedItems() const137 bool CategorizedView::haveUnSelectedItems() const
138 {
139     // Dont need the sorted type of 'selectedIndexes' here...
140     return selectionModel() && model() && selectionModel()->selectedIndexes().count()!=model()->rowCount();
141 }
142 
mouseReleaseEvent(QMouseEvent * event)143 void CategorizedView::mouseReleaseEvent(QMouseEvent *event)
144 {
145     if (Qt::NoModifier==event->modifiers() && Qt::LeftButton==event->button()) {
146         KCategorizedView::mouseReleaseEvent(event);
147     }
148 }
149 
selectedIndexes(bool sorted) const150 QModelIndexList CategorizedView::selectedIndexes(bool sorted) const
151 {
152     QModelIndexList indexes=selectionModel() ? selectionModel()->selectedIndexes() : QModelIndexList();
153     QModelIndexList actual;
154 
155     for (const auto &idx: indexes) {
156         actual.append(proxy->mapToSource(idx));
157     }
158 
159     if (sorted) {
160         std::sort(actual.begin(), actual.end());
161     }
162     return actual;
163 }
164 
setModel(QAbstractItemModel * m)165 void CategorizedView::setModel(QAbstractItemModel *m)
166 {
167     QAbstractItemModel *old=proxy->sourceModel();
168     proxy->setSourceModel(m);
169 
170     if (old) {
171         disconnect(old, SIGNAL(layoutChanged()), this, SLOT(correctSelection()));
172     }
173 
174     if (m && old!=m) {
175         connect(m, SIGNAL(layoutChanged()), this, SLOT(correctSelection()));
176     }
177     if (m) {
178         KCategorizedView::setModel(proxy);
179     } else {
180         KCategorizedView::setModel(nullptr);
181     }
182 }
183 
addDefaultAction(QAction * act)184 void CategorizedView::addDefaultAction(QAction *act)
185 {
186     if (!menu) {
187         menu=new QMenu(this);
188     }
189     menu->addAction(act);
190 }
191 
setBackgroundImage(const QIcon & icon)192 void CategorizedView::setBackgroundImage(const QIcon &icon)
193 {
194     QPalette pal=parentWidget()->palette();
195 //    if (!icon.isNull()) {
196 //        pal.setColor(QPalette::Base, Qt::transparent);
197 //    }
198     #ifndef Q_OS_MAC
199     setPalette(pal);
200     #endif
201     viewport()->setPalette(pal);
202     bgnd=TreeView::createBgndPixmap(icon);
203 }
204 
paintEvent(QPaintEvent * e)205 void CategorizedView::paintEvent(QPaintEvent *e)
206 {
207     if (!bgnd.isNull()) {
208         QPainter p(viewport());
209         QSize sz=size();
210         p.fillRect(0, 0, sz.width(), sz.height(), QApplication::palette().color(QPalette::Base));
211         p.drawPixmap((sz.width()-bgnd.width())/2, (sz.height()-bgnd.height())/2, bgnd);
212     }
213     if (!info.isEmpty() && model() && 0==model()->rowCount()) {
214         QPainter p(viewport());
215         QColor col(palette().text().color());
216         col.setAlphaF(0.5);
217         QFont f(font());
218         f.setItalic(true);
219         p.setPen(col);
220         p.setFont(f);
221         p.drawText(rect().adjusted(8, 8, -16, -16), Qt::AlignCenter|Qt::TextWordWrap, info);
222     }
223     KCategorizedView::paintEvent(e);
224 }
225 
setRootIndex(const QModelIndex & idx)226 void CategorizedView::setRootIndex(const QModelIndex &idx)
227 {
228     KCategorizedView::setRootIndex(idx.model()==proxy->sourceModel() ? proxy->mapFromSource(idx) : idx);
229 }
230 
rootIndex() const231 QModelIndex CategorizedView::rootIndex() const
232 {
233     QModelIndex idx=KCategorizedView::rootIndex();
234     return idx.model()==proxy ? proxy->mapToSource(idx) : idx;
235 }
236 
indexAt(const QPoint & point,bool ensureFromSource) const237 QModelIndex CategorizedView::indexAt(const QPoint &point, bool ensureFromSource) const
238 {
239     QModelIndex idx=KCategorizedView::indexAt(point);
240     return ensureFromSource && idx.model()==proxy ? proxy->mapToSource(idx) : idx;
241 }
242 
mapFromSource(const QModelIndex & idx) const243 QModelIndex CategorizedView::mapFromSource(const QModelIndex &idx) const
244 {
245     return idx.model()==proxy->sourceModel() ? proxy->mapFromSource(idx) : idx;
246 }
247 
setPlain(bool plain)248 void CategorizedView::setPlain(bool plain)
249 {
250     proxy->setCategorizedModel(!plain);
251     setViewMode(plain ? QListView::ListMode : QListView::IconMode);
252 }
253 
254 // Workaround for https://bugreports.qt-project.org/browse/QTBUG-18009
correctSelection()255 void CategorizedView::correctSelection()
256 {
257     if (!selectionModel()) {
258         return;
259     }
260 
261     QItemSelection s = selectionModel()->selection();
262     setCurrentIndex(currentIndex());
263     selectionModel()->select(s, QItemSelectionModel::SelectCurrent);
264 }
265 
showCustomContextMenu(const QPoint & pos)266 void CategorizedView::showCustomContextMenu(const QPoint &pos)
267 {
268     if (menu) {
269         menu->popup(mapToGlobal(pos));
270     }
271 }
272 
checkDoubleClick(const QModelIndex & idx)273 void CategorizedView::checkDoubleClick(const QModelIndex &idx)
274 {
275     if (!TreeView::getForceSingleClick() && idx.model() && idx.model()->rowCount(idx)) {
276         return;
277     }
278     emit itemDoubleClicked(idx.model()==proxy ? proxy->mapToSource(idx) : idx);
279 }
280 
checkClicked(const QModelIndex & idx)281 void CategorizedView::checkClicked(const QModelIndex &idx)
282 {
283     emit itemClicked(idx.model()==proxy ? proxy->mapToSource(idx) : idx);
284 }
285 
checkActivated(const QModelIndex & idx)286 void CategorizedView::checkActivated(const QModelIndex &idx)
287 {
288     emit itemActivated(idx.model()==proxy ? proxy->mapToSource(idx) : idx);
289 }
290 
rowsInserted(const QModelIndex & parent,int start,int end)291 void CategorizedView::rowsInserted(const QModelIndex &parent, int start, int end)
292 {
293     if (parent==KCategorizedView::rootIndex()) {
294         KCategorizedView::rowsInserted(parent, start, end);
295     }
296 }
297