Warning, file /office/calligra/libs/widgets/KoTableView.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  * Copyright (C) 2015 Boudewijn Rempt <boud@valdyas.org>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Library General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Library General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this library; see the file COPYING.LIB.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 #include "KoTableView.h"
0020 
0021 #include <QEvent>
0022 #include <QHeaderView>
0023 
0024 KoTableView::KoTableView(QWidget *parent)
0025     : QTableView(parent)
0026 {
0027     setSelectionMode(QAbstractItemView::SingleSelection);
0028     verticalHeader()->hide();
0029     horizontalHeader()->hide();
0030     verticalHeader()->setDefaultSectionSize(20);
0031     setContextMenuPolicy(Qt::DefaultContextMenu);
0032     setViewMode(FIXED_COLUMNS);
0033 }
0034 
0035 void KoTableView::resizeEvent(QResizeEvent *event)
0036 {
0037     QTableView::resizeEvent(event);
0038     updateView();
0039 
0040     emit sigSizeChanged();
0041 }
0042 
0043 void KoTableView::setViewMode(KoTableView::ViewMode mode)
0044 {
0045     m_viewMode = mode;
0046 
0047     switch (m_viewMode) {
0048     case FIXED_COLUMNS:
0049         setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); // Horizontal scrollbar is never needed
0050         setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
0051         break;
0052     case FIXED_ROWS:
0053         setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
0054         setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); // Vertical scrollbar is never needed
0055         break;
0056     default:
0057         setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
0058         setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
0059         break;
0060     }
0061 
0062 }
0063 
0064 void KoTableView::updateView()
0065 {
0066     int columnCount = model()->columnCount(QModelIndex());
0067     int rowCount = model()->rowCount(QModelIndex());
0068     int rowHeight, columnWidth;
0069 
0070     if (m_viewMode == FIXED_COLUMNS) {
0071         columnWidth = viewport()->size().width() / columnCount;
0072 
0073         for (int i = 0; i < columnCount; ++i) {
0074             setColumnWidth(i, columnWidth);
0075         }
0076         if (columnCount > 1) {
0077             for (int i = 0; i < rowCount; ++i) {
0078                 setRowHeight(i, columnWidth);
0079             }
0080         }
0081     } else if (m_viewMode == FIXED_ROWS) {
0082         if (rowCount == 0) return;  // Don't divide by zero
0083         rowHeight = viewport()->size().height() / rowCount;
0084 
0085         for (int i = 0; i < rowCount; ++i) {
0086             setRowHeight(i, rowHeight);
0087         }
0088     }
0089 }