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