Debian Codegraph @

tests/manual/widgets/itemviews/qheaderview/qheaderviewtest1.cpp

raw
1 // Copyright (C) 2012 Thorbjørn Lund Martsum - tmartsum[at]gmail.com
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include <QtWidgets/QtWidgets>
5
6 struct ManualTask {
7 const char *title;
8 const char *instructions;
9 unsigned sectionsMovable : 1;
10 unsigned selectionMode : 3;
11 };
12
13 ManualTask tasks[] = {
14 { QT_TR_NOOP("0. Default"),
15 "Please provide instructions",
16 true, QAbstractItemView::SingleSelection
17 },
18 { QT_TR_NOOP("1. Autoscroll"),
19 "<ol>"
20 "<li>Press and hold on section 9 of vertical header.<br/>"
21 "<em>(all cells in the row will be selected)</em>"
22 "</li>"
23 "<li>Extend the selection by moving the mouse down.<br/>"
24 "<em>(selection will extend to the next rows)</em>"
25 "</li>"
26 "<li>Continue to move the mouse down and outside the window geometry.<br/>"
27 "<em>(The view should scroll automatically and the selection should still extend)</em>"
28 "</li>"
29 "<li>While still holding the button, do the same in the opposite direction, i.e. move mouse up and outside the window geometry.<br/>"
30 "<em>(Verify that the view scrolls automatically and the selection changes)</em>"
31 "</li>"
32 "<li>Verify that it works in the other dimension, i.e Press and hold section 9 of the horizontal header.<br/>"
33 "<em>All cells in the column will be selected</em>"
34 "</li>"
35 "<li>Extend the selection by moving the mouse to the far right and outside the window geometry.<br/>"
36 "<em>(selection will extend to the next columns)</em>"
37 "</li>"
38 "<li>Verify that it works in the opposite direction (i.e. move mouse to the left of the window geometry).<br/>"
39 "<em>(Verify that the view scrolls automatically and the selection changes)</em>"
40 "</li>"
41 "</ol>",
42 false, QAbstractItemView::ExtendedSelection
43 }
44
45 };
46
47
48 class Window : public QWidget
49 {
50 Q_OBJECT
51 public:
52 Window(QWidget *parent = nullptr): QWidget(parent), ckMovable(0), tableView(0), cbSelectionMode(0), m_taskInstructions(0)
53 {
54 m_taskInstructions = new QLabel();
55 if (sizeof(tasks) > 0)
56 m_taskInstructions->setText(tr(tasks[0].instructions));
57
58 QVBoxLayout *vbox = new QVBoxLayout(this);
59 vbox->addLayout(setupComboBox());
60 vbox->addWidget(setupGroupBox());
61 vbox->addWidget(setupTableView());
62 vbox->addWidget(m_taskInstructions);
63 }
64
65 void updateControls()
66 {
67 ckMovable->setChecked(tableView->verticalHeader()->sectionsMovable());
68 QAbstractItemView::SelectionMode sMode = tableView->selectionMode();
69 cbSelectionMode->setCurrentIndex((int)sMode);
70 }
71
72 private:
73 QFormLayout *setupComboBox()
74 {
75 QComboBox *combo = new QComboBox;
76 for (size_t i = 0; i < sizeof(tasks) / sizeof(tasks[0]); ++i) {
77 combo->addItem(tr(tasks[i].title));
78 }
79
80 connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(on_taskCombo_currentIndexChanged(int)));
81 QFormLayout *form = new QFormLayout;
82 form->addRow(tr("Choose task:"), combo);
83 return form;
84 }
85
86 QGroupBox *setupGroupBox()
87 {
88 QGroupBox *grp = new QGroupBox(tr("Properties"));
89 QFormLayout *form = new QFormLayout;
90 grp->setLayout(form);
91 ckMovable = new QCheckBox;
92 ckMovable->setObjectName(QLatin1String("ckMovable"));
93 connect(ckMovable, SIGNAL(toggled(bool)), this, SLOT(on_ckMovable_toggled(bool)));
94 form->addRow(tr("SectionsMovable"), ckMovable);
95
96 cbSelectionMode = new QComboBox;
97 cbSelectionMode->setObjectName(QLatin1String("cbSelectionMode"));
98 cbSelectionMode->addItems(QStringList() << QLatin1String("NoSelection")
99 << QLatin1String("SingleSelection")
100 << QLatin1String("MultiSelection")
101 << QLatin1String("ExtendedSelection")
102 << QLatin1String("ContiguousSelection")
103 );
104
105 connect(cbSelectionMode, SIGNAL(currentIndexChanged(int)), this, SLOT(on_cbSelectionMode_currentIndexChanged(int)));
106 form->addRow(tr("SelectionMode"), cbSelectionMode);
107 return grp;
108 }
109
110 QTableView *setupTableView()
111 {
112 tableView = new QTableView;
113 const int rowCount = 200;
114 m.setRowCount(rowCount);
115 m.setColumnCount(250);
116 tableView->setSelectionMode(QAbstractItemView::SingleSelection);
117 tableView->setModel(&m);
118 tableView->verticalHeader()->swapSections(rowCount - 1, 5);
119 return tableView;
120 }
121
122 private Q_SLOTS:
123 void on_ckMovable_toggled(bool arg)
124 {
125 tableView->verticalHeader()->setSectionsMovable(arg);
126 tableView->horizontalHeader()->setSectionsMovable(arg);
127 }
128
129 void on_cbSelectionMode_currentIndexChanged(int idx)
130 {
131 tableView->setSelectionMode((QAbstractItemView::SelectionMode)idx);
132 }
133
134 void on_taskCombo_currentIndexChanged(int idx)
135 {
136 ManualTask &task = tasks[idx];
137 m_taskInstructions->setText(tr(task.instructions));
138 ckMovable->setChecked(task.sectionsMovable);
139 cbSelectionMode->setCurrentIndex((QAbstractItemView::SelectionMode)task.selectionMode);
140 }
141
142 public:
143 QCheckBox *ckMovable;
144 QTableView *tableView;
145 QStandardItemModel m;
146 QComboBox *cbSelectionMode;
147 QLabel *m_taskInstructions;
148 };
149
150 class SomeHandler : public QObject
151 {
152 Q_OBJECT
153 QHeaderView *m_hv;
154 QTableView *m_tv;
155 public:
156 SomeHandler(QHeaderView *hv, QTableView *tv);
157 public slots:
158 void slotSectionResized(int, int, int);
159 };
160
161 SomeHandler::SomeHandler(QHeaderView *hv, QTableView *tv)
162 {
163 m_hv = hv;
164 m_tv = tv;
165 m_tv->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
166 connect(hv, SIGNAL(sectionResized(int,int,int)), this, SLOT(slotSectionResized(int,int,int)));
167 }
168 void SomeHandler::slotSectionResized(int logsection, int oldsize, int newsize)
169 {
170 int offset = m_hv->offset();
171 m_tv->setUpdatesEnabled(false);
172 // Do some manual resizing - lets make every section having the new size.
173 m_hv->blockSignals(true);
174 m_hv->setDefaultSectionSize(newsize);
175 m_hv->blockSignals(false);
176
177 // Adjust offset and scrollbar. Maybe it isn't 100% perfect
178 // but proof of concept
179 // The test has sense without the define, too.
180 #define DO_CORRECT_OFFSET_AND_SB
181 #ifdef DO_CORRECT_OFFSET_AND_SB
182 int leftRemoved = (m_hv->visualIndex(logsection)) * (oldsize - newsize);
183 int newoffset = offset - leftRemoved;
184 if (newoffset < 0)
185 newoffset = 0;
186
187 if (newoffset > 0 && newoffset >= m_hv->count() * newsize - m_tv->viewport()->width())
188 m_hv->setOffsetToLastSection();
189 else
190 m_hv->setOffset(newoffset);
191
192 m_tv->horizontalScrollBar()->blockSignals(true);
193 m_tv->horizontalScrollBar()->setRange(0, m_hv->count() * newsize - m_tv->viewport()->width() );
194 m_tv->horizontalScrollBar()->setValue(newoffset);
195 m_tv->horizontalScrollBar()->blockSignals(false);
196 #endif
197 m_tv->setUpdatesEnabled(true);
198 }
199
200 int main(int argc, char *argv[])
201 {
202 QApplication app(argc, argv);
203 Window window;
204 // Comment in the line below to test selection with keyboard (space)
205 // tv.setEditTriggers(QAbstractItemView::NoEditTriggers);
206 QHeaderView *hHeader = window.tableView->horizontalHeader();
207 QHeaderView *vHeader = window.tableView->verticalHeader();
208 SomeHandler handler(hHeader, window.tableView);
209 hHeader->setDefaultSectionSize(30);
210 window.resize(600, 600);
211 window.show();
212 hHeader->setSectionsMovable(true);
213 vHeader->setSectionsMovable(true);
214 window.updateControls();
215 app.exec();
216 }
217 #include "qheaderviewtest1.moc"