| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** All rights reserved. | 
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
|---|
| 6 | ** | 
|---|
| 7 | ** This file is part of the examples of the Qt Toolkit. | 
|---|
| 8 | ** | 
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 10 | ** Commercial Usage | 
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 14 | ** a written agreement between you and Nokia. | 
|---|
| 15 | ** | 
|---|
| 16 | ** GNU Lesser General Public License Usage | 
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 20 | ** packaging of this file.  Please review the following information to | 
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 23 | ** | 
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional | 
|---|
| 25 | ** rights.  These rights are described in the Nokia Qt LGPL Exception | 
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include <QtGui> | 
|---|
| 43 |  | 
|---|
| 44 | #include "freezetablewidget.h" | 
|---|
| 45 |  | 
|---|
| 46 | //! [constructor] | 
|---|
| 47 | FreezeTableWidget::FreezeTableWidget(QAbstractItemModel * model) | 
|---|
| 48 | { | 
|---|
| 49 | setModel(model); | 
|---|
| 50 | frozenTableView = new QTableView(this); | 
|---|
| 51 |  | 
|---|
| 52 | init(); | 
|---|
| 53 |  | 
|---|
| 54 | //connect the headers and scrollbars of both tableviews together | 
|---|
| 55 | connect(horizontalHeader(),SIGNAL(sectionResized(int,int,int)), this, | 
|---|
| 56 | SLOT(updateSectionWidth(int,int,int))); | 
|---|
| 57 | connect(verticalHeader(),SIGNAL(sectionResized(int,int,int)), this, | 
|---|
| 58 | SLOT(updateSectionHeight(int,int,int))); | 
|---|
| 59 |  | 
|---|
| 60 | connect(frozenTableView->verticalScrollBar(), SIGNAL(valueChanged(int)), | 
|---|
| 61 | verticalScrollBar(), SLOT(setValue(int))); | 
|---|
| 62 | connect(verticalScrollBar(), SIGNAL(valueChanged(int)), | 
|---|
| 63 | frozenTableView->verticalScrollBar(), SLOT(setValue(int))); | 
|---|
| 64 |  | 
|---|
| 65 |  | 
|---|
| 66 | } | 
|---|
| 67 | //! [constructor] | 
|---|
| 68 |  | 
|---|
| 69 | FreezeTableWidget::~FreezeTableWidget() | 
|---|
| 70 | { | 
|---|
| 71 | delete frozenTableView; | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | //! [init part1] | 
|---|
| 75 | void FreezeTableWidget::init() | 
|---|
| 76 | { | 
|---|
| 77 | frozenTableView->setModel(model()); | 
|---|
| 78 | frozenTableView->setFocusPolicy(Qt::NoFocus); | 
|---|
| 79 | frozenTableView->verticalHeader()->hide(); | 
|---|
| 80 | frozenTableView->horizontalHeader()->setResizeMode(QHeaderView::Fixed); | 
|---|
| 81 |  | 
|---|
| 82 | viewport()->stackUnder(frozenTableView); | 
|---|
| 83 | //! [init part1] | 
|---|
| 84 |  | 
|---|
| 85 | //! [init part2] | 
|---|
| 86 | frozenTableView->setStyleSheet("QTableView { border: none;" | 
|---|
| 87 | "background-color: #8EDE21;" | 
|---|
| 88 | "selection-background-color: #999}"); //for demo purposes | 
|---|
| 89 | frozenTableView->setSelectionModel(selectionModel()); | 
|---|
| 90 | for(int col=1; col<model()->columnCount(); col++) | 
|---|
| 91 | frozenTableView->setColumnHidden(col, true); | 
|---|
| 92 |  | 
|---|
| 93 | frozenTableView->setColumnWidth(0, columnWidth(0) ); | 
|---|
| 94 |  | 
|---|
| 95 | frozenTableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | 
|---|
| 96 | frozenTableView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | 
|---|
| 97 | frozenTableView->show(); | 
|---|
| 98 |  | 
|---|
| 99 | updateFrozenTableGeometry(); | 
|---|
| 100 |  | 
|---|
| 101 | setHorizontalScrollMode(ScrollPerPixel); | 
|---|
| 102 | setVerticalScrollMode(ScrollPerPixel); | 
|---|
| 103 | frozenTableView->setVerticalScrollMode(ScrollPerPixel); | 
|---|
| 104 | } | 
|---|
| 105 | //! [init part2] | 
|---|
| 106 |  | 
|---|
| 107 |  | 
|---|
| 108 | //! [sections] | 
|---|
| 109 | void FreezeTableWidget::updateSectionWidth(int logicalIndex, int, int newSize) | 
|---|
| 110 | { | 
|---|
| 111 | if(logicalIndex==0){ | 
|---|
| 112 | frozenTableView->setColumnWidth(0,newSize); | 
|---|
| 113 | updateFrozenTableGeometry(); | 
|---|
| 114 | } | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | void FreezeTableWidget::updateSectionHeight(int logicalIndex, int, int newSize) | 
|---|
| 118 | { | 
|---|
| 119 | frozenTableView->setRowHeight(logicalIndex, newSize); | 
|---|
| 120 | } | 
|---|
| 121 | //! [sections] | 
|---|
| 122 |  | 
|---|
| 123 |  | 
|---|
| 124 | //! [resize] | 
|---|
| 125 | void FreezeTableWidget::resizeEvent(QResizeEvent * event) | 
|---|
| 126 | { | 
|---|
| 127 | QTableView::resizeEvent(event); | 
|---|
| 128 | updateFrozenTableGeometry(); | 
|---|
| 129 | } | 
|---|
| 130 | //! [resize] | 
|---|
| 131 |  | 
|---|
| 132 |  | 
|---|
| 133 | //! [navigate] | 
|---|
| 134 | QModelIndex FreezeTableWidget::moveCursor(CursorAction cursorAction, | 
|---|
| 135 | Qt::KeyboardModifiers modifiers) | 
|---|
| 136 | { | 
|---|
| 137 | QModelIndex current = QTableView::moveCursor(cursorAction, modifiers); | 
|---|
| 138 |  | 
|---|
| 139 | if(cursorAction == MoveLeft && current.column()>0 | 
|---|
| 140 | && visualRect(current).topLeft().x() < frozenTableView->columnWidth(0) ){ | 
|---|
| 141 |  | 
|---|
| 142 | const int newValue = horizontalScrollBar()->value() + visualRect(current).topLeft().x() | 
|---|
| 143 | - frozenTableView->columnWidth(0); | 
|---|
| 144 | horizontalScrollBar()->setValue(newValue); | 
|---|
| 145 | } | 
|---|
| 146 | return current; | 
|---|
| 147 | } | 
|---|
| 148 | //! [navigate] | 
|---|
| 149 |  | 
|---|
| 150 | void FreezeTableWidget::scrollTo (const QModelIndex & index, ScrollHint hint){ | 
|---|
| 151 | if(index.column()>0) | 
|---|
| 152 | QTableView::scrollTo(index, hint); | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 |  | 
|---|
| 156 |  | 
|---|
| 157 | //! [geometry] | 
|---|
| 158 | void FreezeTableWidget::updateFrozenTableGeometry() | 
|---|
| 159 | { | 
|---|
| 160 | frozenTableView->setGeometry( verticalHeader()->width()+frameWidth(), | 
|---|
| 161 | frameWidth(), columnWidth(0), | 
|---|
| 162 | viewport()->height()+horizontalHeader()->height()); | 
|---|
| 163 | } | 
|---|
| 164 | //! [geometry] | 
|---|
| 165 |  | 
|---|
| 166 |  | 
|---|