Krita Source Code Documentation
Loading...
Searching...
No Matches
KisResourceItemListWidget.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2023 Srirupa Datta <srirupa.sps@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
8
9#include <QDebug>
10#include <QEvent>
11#include <QHeaderView>
12#include <QHelpEvent>
13#include <QScroller>
14#include <QScrollBar>
15#include <QScrollArea>
16
17#include "KisIconToolTip.h"
18
19
21{
24 bool strictSelectionMode {false};
26
27 QScroller* scroller {0};
29};
30
32 : QListWidget(parent)
33 , m_d(new Private(this))
34{
35 setSelectionMode(QAbstractItemView::ExtendedSelection);
36 setContextMenuPolicy(Qt::DefaultContextMenu);
37 setViewMode(QListView::IconMode);
38 setMovement(QListView::Static);
39 setGridSize(QSize(56, 56));
40 setIconSize(QSize(56, 56));
41 setResizeMode(QListWidget::Adjust);
42 setUniformItemSizes(true);
43
45 if (m_d->scroller) {
46 connect(m_d->scroller, SIGNAL(stateChanged(QScroller::State)), this, SLOT(slotScrollerStateChange(QScroller::State)));
47 }
48
49 connect(this, SIGNAL(clicked(QModelIndex)), SIGNAL(currentResourceClicked(const QModelIndex &)));
50
51 m_d->prev_scrollbar_style = horizontalScrollBar()->styleSheet();
52}
53
57
59{
60 m_d->viewMode = viewMode;
61
62 switch (viewMode) {
64 setViewMode(ViewMode::IconMode);
65 setFlow(Flow::LeftToRight);
66 setWrapping(true);
67 horizontalScrollBar()->setStyleSheet(m_d->prev_scrollbar_style);
68 setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAsNeeded);
69 break;
70 }
72 setViewMode(ViewMode::IconMode);
73 setFlow(Flow::LeftToRight);
74 setWrapping(false);
75
76 // this is the only way to hide it and not have it occupy space
77 horizontalScrollBar()->setStyleSheet("QScrollBar::horizontal {height: 0px;}");
78 setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff);
79 break;
80 }
82 setViewMode(ViewMode::ListMode);
83 setFlow(Flow::TopToBottom);
84 setWrapping(false);
85 // horizontalScrollBar()->setStyleSheet(m_d->prev_scrollbar_style);
86 setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAsNeeded);
87 setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAsNeeded);
88 break;
89 }
90 }
91
93}
94
96{
97 switch (m_d->viewMode) {
99 setGridSize(size);
100 setIconSize(size);
101 break;
102 }
104 // you can not set the item size in strip mode
105 // it is configured automatically based on size
106 break;
107 }
109 const int w = width();
110 setGridSize(QSize(w, size.height()));
111 setIconSize(QSize(size));
112 break;
113 }
114 }
115}
116
118{
119 m_d->strictSelectionMode = enable;
120}
121
123{
124 m_d->tip.setFixedToolTipThumbnailSize(size);
125}
126
128{
129 m_d->tip.setToolTipShouldRenderCheckers(value);
130}
131
132void KisResourceItemListWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
133{
134 // QAbstractItemView moves the current index if the row it points to is removed,
135 // which we don't want for strict selections
136 QModelIndex current = currentIndex();
137 if (selectionMode() == SingleSelection
138 && m_d->strictSelectionMode
139 && current.isValid()
140 && current.row() >= start
141 && current.row() <= end) {
142
143 selectionModel()->clear();
144 }
145 QListWidget::rowsAboutToBeRemoved(parent, start, end);
146}
147
148void KisResourceItemListWidget::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
149{
150 // base class takes care of viewport updates
151 QListWidget::selectionChanged(selected, deselected);
152
153 if (selected.isEmpty()) {
154 Q_EMIT currentResourceChanged(QModelIndex());
155 }
156 else {
157 Q_EMIT currentResourceChanged(selected.indexes().first());
158 }
159}
160
161QItemSelectionModel::SelectionFlags KisResourceItemListWidget::selectionCommand(const QModelIndex &index, const QEvent *event) const
162{
163 QItemSelectionModel::SelectionFlags cmd = QListWidget::selectionCommand(index, event);
164
165 // avoid deselecting the current item by Ctrl-clicking in single selection mode
166 if (selectionMode() == SingleSelection
167 && m_d->strictSelectionMode
168 && cmd.testFlag(QItemSelectionModel::Deselect)) {
169
170 cmd = QItemSelectionModel::NoUpdate;
171 }
172 return cmd;
173}
174
176{
177 QListWidget::contextMenuEvent(event);
178 Q_EMIT contextMenuRequested(event->globalPos());
179}
180
182{
183 return QListWidget::viewportEvent(event);
184}
185
187{
188 if (m_d->viewMode == ListViewMode::IconStripHorizontal) {
189 const int height = event->size().height();
190 setGridSize(QSize(height, height));
191 setIconSize(QSize(height, height));
192 }
193 // trigger relaying the items, internally here not externally
194 // by calling setItemSize
195 else {
196 setGridSize(gridSize());
197 }
198}
float value(const T *src, size_t ch)
int iconSize(qreal width, qreal height)
KRITAWIDGETUTILS_EXPORT QScroller * createPreconfiguredScroller(QAbstractScrollArea *target)
QItemSelectionModel::SelectionFlags selectionCommand(const QModelIndex &index, const QEvent *event=nullptr) const override
KisResourceItemListWidget(QWidget *parent=nullptr)
bool viewportEvent(QEvent *event) override
const QScopedPointer< Private > m_d
void contextMenuEvent(QContextMenuEvent *event) override
void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) override
void setListViewMode(ListViewMode layout)
void slotScrollerStateChange(QScroller::State state)
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) override
void setStrictSelectionMode(bool enable)
setStrictSelectionMode sets additional restrictions on the selection.
void setItemSize(QSize size)
setItemSize convenience function which sets both the icon and the grid size to the same value.
void contextMenuRequested(const QPoint &)
void setFixedToolTipThumbnailSize(const QSize &size)
void currentResourceClicked(const QModelIndex &)
Private(KisResourceItemListWidget *_q)
void currentResourceChanged(const QModelIndex &)
void resizeEvent(QResizeEvent *event) override