Back to index

fet  5.18.0
sparsetableview.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                                 FET
00003                           -------------------
00004    copyright            : (C) by Lalescu Liviu
00005     email                : Please see http://lalescu.ro/liviu/ for details about contacting Liviu Lalescu (in particular, you can find here the e-mail address)
00006  ***************************************************************************
00007                       sparsetableview.cpp  -  description
00008                              -------------------
00009     begin                : 2010
00010     copyright            : (C) 2010 by Liviu Lalescu
00011                          : http://lalescu.ro/liviu/
00012  ***************************************************************************
00013  *                                                                         *
00014  *   This program is free software; you can redistribute it and/or modify  *
00015  *   it under the terms of the GNU General Public License as published by  *
00016  *   the Free Software Foundation; either version 2 of the License, or     *
00017  *   (at your option) any later version.                                   *
00018  *                                                                         *
00019  ***************************************************************************/
00020 
00021 #include "sparsetableview.h"
00022 
00023 #include "timetable_defs.h"
00024 
00025 #include <QHeaderView>
00026 
00027 #include <QHash>
00028 #include <QPair>
00029 
00030 #include <QSize>
00031 
00032 SparseTableView::SparseTableView() : QTableView()
00033 {
00034        this->setModel(&model);
00035 }
00036 
00037 int SparseTableView::maxHorizontalHeaderSectionSize()
00038 {
00039        return this->width()/3;
00040 }
00041 
00042 void SparseTableView::resizeToFit()
00043 {
00044        QHash<int, int> columnSizes;
00045        QHash<int, int> rowSizes;
00046        
00047        QHashIterator<QPair<int, int>, QString> i(model.items);
00048        while(i.hasNext()){
00049               i.next();
00050               
00051               QPair<int, int> pair=i.key();
00052               
00053               if(this->isRowHidden(pair.first))
00054                      continue;
00055               if(this->isColumnHidden(pair.second))
00056                      continue;
00057               
00058               QString str=i.value();
00059               
00060               QAbstractItemDelegate* delegate=this->itemDelegate(model.index(pair.first, pair.second));
00061               
00062               QSize size=delegate->sizeHint(this->viewOptions(), model.index(pair.first, pair.second));
00063               
00064               if(size.width() > columnSizes.value(pair.second, 0))
00065                      columnSizes.insert(pair.second, size.width());
00066               if(size.height() > rowSizes.value(pair.first, 0))
00067                      rowSizes.insert(pair.first, size.height());
00068        }
00069        
00070        horizontalSizesUntruncated.clear();
00071        
00072        for(int i=0; i<model.columnCount(); i++){
00073               int k=columnSizes.value(i, 0);
00074               if(this->showGrid())
00075                      k++;
00076               if(k<this->horizontalHeader()->sectionSizeHint(i))
00077                      k=this->horizontalHeader()->sectionSizeHint(i);
00078                      
00079               //not too wide
00080               horizontalSizesUntruncated.append(k);
00081               
00082               if(k>maxHorizontalHeaderSectionSize()){
00083                      k=maxHorizontalHeaderSectionSize();
00084               }
00085                      
00086               this->horizontalHeader()->resizeSection(i, k);
00087        }
00088 
00089        for(int i=0; i<model.rowCount(); i++){
00090               int k=rowSizes.value(i, 0);
00091               if(this->showGrid())
00092                      k++;
00093               if(k<this->verticalHeader()->sectionSizeHint(i))
00094                      k=this->verticalHeader()->sectionSizeHint(i);
00095               this->verticalHeader()->resizeSection(i, k);
00096        }
00097 }
00098 
00099 /*void SparseTableView::itWasResized()
00100 {
00101        assert(model.columnCount()==horizontalSizesUntruncated.count());
00102        for(int i=0; i<model.columnCount(); i++){
00103               int size=horizontalSizesUntruncated.at(i);
00104               
00105               if(size>maxHorizontalHeaderSectionSize())
00106                      size=maxHorizontalHeaderSectionSize();
00107               
00108               if(this->horizontalHeader()->sectionSize(i)!=size)
00109                      this->horizontalHeader()->resizeSection(i, size);
00110        }
00111 }*/
00112 
00113 void SparseTableView::resizeEvent(QResizeEvent* event)
00114 {
00115        QTableView::resizeEvent(event);
00116        
00117        //So that if user resizes horizontal header and scroll bar appears, no automatic resize is done.
00118        if(event){
00119               if(event->size().width()==event->oldSize().width())
00120                      return;
00121        }
00122        
00123        //this->itWasResized();
00124        if(model.columnCount()!=horizontalSizesUntruncated.count()){ //do NOT remove this test, or you will get assertion failed a few lines below, when user clicks "Swap axes".
00125        //This situation can appear if user clicks "Swap axes", and it is taken care of then, so we can return in this case, safely assuming that the table will be redrawn correctly.
00126               //cout<<"different sizes"<<endl;
00127               return;
00128        }
00129        assert(model.columnCount()==horizontalSizesUntruncated.count());
00130        for(int i=0; i<model.columnCount(); i++){
00131               int size=horizontalSizesUntruncated.at(i);
00132               
00133               if(size>maxHorizontalHeaderSectionSize())
00134                      size=maxHorizontalHeaderSectionSize();
00135               
00136               if(this->horizontalHeader()->sectionSize(i)!=size)
00137                      this->horizontalHeader()->resizeSection(i, size);
00138        }
00139 }
00140 
00141 void SparseTableView::allTableChanged()
00142 {
00143        model.allItemsChanged();
00144        model.allHeadersChanged();
00145 }