Krita Source Code Documentation
Loading...
Searching...
No Matches
KisResourceItemView.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 * SPDX-FileCopyrightText: 2008 Jan Hambrecht <jaham@gmx.net>
3 * SPDX-FileCopyrightText: 2011 José Luis Vergara <pentalis@gmail.com
4 *
5 * SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
9
10#include <QEvent>
11#include <QHeaderView>
12#include <QScroller>
13#include <QHelpEvent>
14#include <QDebug>
15
16#include <KisKineticScroller.h>
17
18#include <QtMath>
19
21 : QTableView(parent)
22{
23 setSelectionMode(QAbstractItemView::SingleSelection);
24 verticalHeader()->hide();
25 horizontalHeader()->hide();
26 verticalHeader()->setDefaultSectionSize(20);
27 setContextMenuPolicy(Qt::DefaultContextMenu);
29
30 QScroller *scroller = KisKineticScroller::createPreconfiguredScroller(this);
31 if (scroller) {
32 connect(scroller, SIGNAL(stateChanged(QScroller::State)), this, SLOT(slotScrollerStateChange(QScroller::State)));
33 }
34
35 connect(this, SIGNAL(clicked(QModelIndex)), SLOT(slotItemClicked(QModelIndex)));
36}
37
39{
40 if (!model()) return true;
41
42 if (event->type() == QEvent::ToolTip) {
43 QHelpEvent *he = static_cast<QHelpEvent *>(event);
44#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
45 QStyleOptionViewItem option = viewOptions();
46#else
47 QStyleOptionViewItem option;
48 initViewItemOption(&option);
49#endif
50 QModelIndex index = model()->buddy(indexAt(he->pos()));
51 if (index.isValid()) {
52 option.rect = visualRect(index);
53 m_tip.showTip(this, he->pos(), option, index);
54 return true;
55 }
56 }
57
58 return QTableView::viewportEvent(event);
59}
60
61void KisResourceItemView::selectionChanged(const QItemSelection &selected, const QItemSelection &/*deselected*/)
62{
63 if (selected.isEmpty()) {
64 Q_EMIT currentResourceChanged(QModelIndex());
65 }
66 else {
67 Q_EMIT currentResourceChanged(selected.indexes().first());
68 }
69}
70
72{
73 m_beforeClickIndex = currentIndex();
74 QTableView::mousePressEvent(event);
75}
76
77void KisResourceItemView::slotItemClicked(const QModelIndex &index)
78{
79 if (m_beforeClickIndex == index) {
80 Q_EMIT currentResourceClicked(index);
81 }
82 m_beforeClickIndex = QModelIndex();
83}
84
85void KisResourceItemView::contextMenuEvent(QContextMenuEvent *event)
86{
87 QTableView::contextMenuEvent(event);
88 Q_EMIT contextMenuRequested(event->globalPos());
89}
90
91void KisResourceItemView::resizeEvent(QResizeEvent *event)
92{
93 QTableView::resizeEvent(event);
94 updateView();
95 Q_EMIT sigSizeChanged();
96}
97
99{
100 m_viewMode = mode;
101
102 switch (m_viewMode) {
103 case FIXED_COLUMNS:
104 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); // Horizontal scrollbar is never needed
105 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
106 break;
107 case FIXED_ROWS:
108 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
109 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); // Vertical scrollbar is never needed
110 break;
111 default:
112 setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
113 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
114 }
115
116}
117
119{
120 if (!model()) return;
121
122 int columnCount = model()->columnCount(QModelIndex());
123 int rowCount = model()->rowCount(QModelIndex());
124 int rowHeight, columnWidth;
125
126 if (m_viewMode == FIXED_COLUMNS) {
127 columnWidth = qFloor(viewport()->size().width() / static_cast<double>(columnCount));
128
129 for (int i = 0; i < columnCount; ++i) {
130 setColumnWidth(i, columnWidth);
131 }
132 // keep aspect ratio always square.
133 if (columnCount > 1) {
134 for (int i = 0; i < rowCount; ++i) {
135 setRowHeight(i, columnWidth);
136 }
137 }
138 } else if (m_viewMode == FIXED_ROWS) {
139 if (rowCount == 0) return; // Don't divide by zero
140 rowHeight = qFloor(viewport()->size().height() / static_cast<double>(rowCount));
141
142 for (int i = 0; i < rowCount; ++i) {
143 setRowHeight(i, rowHeight);
144 }
145 }
146}
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
void slotItemClicked(const QModelIndex &index)
void setViewMode(ViewMode mode)
void currentResourceClicked(const QModelIndex &)
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) override
void currentResourceChanged(const QModelIndex &)
void mousePressEvent(QMouseEvent *event) override
void contextMenuRequested(const QPoint &)
@ FIXED_ROWS
The number of columns is fixed.
bool viewportEvent(QEvent *event) override
void slotScrollerStateChange(QScroller::State state)
KisResourceItemView(QWidget *parent=0)
void contextMenuEvent(QContextMenuEvent *event) override
void resizeEvent(QResizeEvent *event) override
void showTip(QWidget *widget, const QPoint &pos, const QStyleOptionViewItem &option, const QModelIndex &index)
KRITAWIDGETUTILS_EXPORT QScroller * createPreconfiguredScroller(QAbstractScrollArea *target)