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{
23 bool strictSelectionMode {false};
25
26 QScroller* scroller {0};
28};
29
31 : QListWidget(parent)
32 , m_d(new Private)
33{
34 setSelectionMode(QAbstractItemView::ExtendedSelection);
35 setContextMenuPolicy(Qt::DefaultContextMenu);
36 setViewMode(QListView::IconMode);
37 setMovement(QListView::Static);
38 setGridSize(QSize(56, 56));
39 setIconSize(QSize(56, 56));
40 setResizeMode(QListWidget::Adjust);
41 setUniformItemSizes(true);
42
44 if (m_d->scroller) {
45 connect(m_d->scroller, SIGNAL(stateChanged(QScroller::State)), this, SLOT(slotScrollerStateChange(QScroller::State)));
46 }
47
48 connect(this, SIGNAL(clicked(QModelIndex)), SIGNAL(currentResourceClicked(const QModelIndex &)));
49
50 m_d->prev_scrollbar_style = horizontalScrollBar()->styleSheet();
51}
52
56
58{
59 m_d->viewMode = viewMode;
60
61 switch (viewMode) {
63 setViewMode(ViewMode::IconMode);
64 setFlow(Flow::LeftToRight);
65 setWrapping(true);
66 horizontalScrollBar()->setStyleSheet(m_d->prev_scrollbar_style);
67 setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAsNeeded);
68 break;
69 }
71 setViewMode(ViewMode::IconMode);
72 setFlow(Flow::LeftToRight);
73 setWrapping(false);
74
75 // this is the only way to hide it and not have it occupy space
76 horizontalScrollBar()->setStyleSheet("QScrollBar::horizontal {height: 0px;}");
77 setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff);
78 break;
79 }
81 setViewMode(ViewMode::ListMode);
82 setFlow(Flow::TopToBottom);
83 setWrapping(false);
84 // horizontalScrollBar()->setStyleSheet(m_d->prev_scrollbar_style);
85 setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAsNeeded);
86 setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAsNeeded);
87 break;
88 }
89 }
90
92}
93
95{
96 switch (m_d->viewMode) {
98 setGridSize(size);
99 setIconSize(size);
100 break;
101 }
103 // you can not set the item size in strip mode
104 // it is configured automatically based on size
105 break;
106 }
108 const int w = width();
109 setGridSize(QSize(w, size.height()));
110 setIconSize(QSize(size));
111 break;
112 }
113 }
114}
115
117{
118 m_d->strictSelectionMode = enable;
119}
120
122{
123 m_d->tip.setFixedToolTipThumbnailSize(size);
124}
125
127{
128 m_d->tip.setToolTipShouldRenderCheckers(value);
129}
130
131void KisResourceItemListWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
132{
133 // QAbstractItemView moves the current index if the row it points to is removed,
134 // which we don't want for strict selections
135 QModelIndex current = currentIndex();
136 if (selectionMode() == SingleSelection
137 && m_d->strictSelectionMode
138 && current.isValid()
139 && current.row() >= start
140 && current.row() <= end) {
141
142 selectionModel()->clear();
143 }
144 QListWidget::rowsAboutToBeRemoved(parent, start, end);
145}
146
147void KisResourceItemListWidget::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
148{
149 // base class takes care of viewport updates
150 QListWidget::selectionChanged(selected, deselected);
151
152 if (selected.isEmpty()) {
153 Q_EMIT currentResourceChanged(QModelIndex());
154 }
155 else {
156 Q_EMIT currentResourceChanged(selected.indexes().first());
157 }
158}
159
160QItemSelectionModel::SelectionFlags KisResourceItemListWidget::selectionCommand(const QModelIndex &index, const QEvent *event) const
161{
162 QItemSelectionModel::SelectionFlags cmd = QListWidget::selectionCommand(index, event);
163
164 // avoid deselecting the current item by Ctrl-clicking in single selection mode
165 if (selectionMode() == SingleSelection
166 && m_d->strictSelectionMode
167 && cmd.testFlag(QItemSelectionModel::Deselect)) {
168
169 cmd = QItemSelectionModel::NoUpdate;
170 }
171 return cmd;
172}
173
175{
176 QListWidget::contextMenuEvent(event);
177 Q_EMIT contextMenuRequested(event->globalPos());
178}
179
181{
182 return QListWidget::viewportEvent(event);
183}
184
186{
187 if (m_d->viewMode == ListViewMode::IconStripHorizontal) {
188 const int height = event->size().height();
189 setGridSize(QSize(height, height));
190 setIconSize(QSize(height, height));
191 }
192 // trigger relaying the items, internally here not externally
193 // by calling setItemSize
194 else {
195 setGridSize(gridSize());
196 }
197}
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 &)
void currentResourceChanged(const QModelIndex &)
void resizeEvent(QResizeEvent *event) override