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