1 // Created on: 2017-06-16
2 // Created by: Natalia ERMOLAEVA
3 // Copyright (c) 2017 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15 
16 #include <inspector/DFBrowserPane_TableView.hxx>
17 #include <inspector/DFBrowserPane_Tools.hxx>
18 
19 #include <inspector/TreeModel_Tools.hxx>
20 
21 #include <Standard_WarningsDisable.hxx>
22 #include <QAbstractTableModel>
23 #include <QHeaderView>
24 #include <QHBoxLayout>
25 #include <QScrollBar>
26 #include <QTableView>
27 #include <QWidget>
28 #include <Standard_WarningsRestore.hxx>
29 
30 // =======================================================================
31 // function : Constructor
32 // purpose :
33 // =======================================================================
DFBrowserPane_TableView(QWidget * theParent,const QMap<int,int> & theDefaultColumnWidths)34 DFBrowserPane_TableView::DFBrowserPane_TableView (QWidget* theParent,
35                                                   const QMap<int, int>& theDefaultColumnWidths)
36 : QWidget (theParent)
37 {
38   QHBoxLayout* aLay = new QHBoxLayout (this);
39   aLay->setContentsMargins (0, 0, 0, 0);
40 
41   myTableView = new QTableView (theParent);
42   myTableView->setShowGrid (false);
43 
44   QHeaderView* aVHeader = myTableView->verticalHeader();
45   aVHeader->setVisible (false);
46   aVHeader->setDefaultSectionSize (aVHeader->minimumSectionSize());
47 
48   myTableView->horizontalHeader()->setStretchLastSection (true);
49   myTableView->horizontalHeader()->setVisible (false);
50   aLay->addWidget (myTableView);
51   myDefaultColumnWidths = theDefaultColumnWidths;
52 }
53 
54 // =======================================================================
55 // function : SetModel
56 // purpose :
57 // =======================================================================
SetModel(QAbstractTableModel * theModel)58 void DFBrowserPane_TableView::SetModel (QAbstractTableModel* theModel)
59 {
60   myTableView->setModel (theModel);
61 
62   for (int aColumnId = 0, aCount = theModel->columnCount(); aColumnId < aCount; aColumnId++)
63     myTableView->setColumnWidth (aColumnId, myDefaultColumnWidths.contains (aColumnId) ?
64             myDefaultColumnWidths[aColumnId] : DFBrowserPane_Tools::DefaultPanelColumnWidth (aColumnId));
65 }
66 
67 // =======================================================================
68 // function : SetFixedRowCount
69 // purpose :
70 // =======================================================================
SetFixedRowCount(const int theCount,QTableView * theView,const bool theScroll)71 void DFBrowserPane_TableView::SetFixedRowCount (const int theCount, QTableView* theView, const bool theScroll)
72 {
73   int aHeight = theView->verticalHeader()->defaultSectionSize()*theCount + TreeModel_Tools::HeaderSectionMargin();
74   if (theScroll)
75     aHeight += theView->horizontalScrollBar()->sizeHint().height();
76 
77   theView->setMaximumHeight (aHeight);
78 }
79 
80 // =======================================================================
81 // function : SetVisibleHorizontalHeader
82 // purpose :
83 // =======================================================================
SetVisibleHorizontalHeader(const bool & theVisible)84 void DFBrowserPane_TableView::SetVisibleHorizontalHeader (const bool& theVisible)
85 {
86   myTableView->horizontalHeader()->setVisible (theVisible);
87 }
88 
89 // =======================================================================
90 // function : GetSelectedColumnValues
91 // purpose :
92 // =======================================================================
GetSelectedColumnValues(QTableView * theTableView,const int theColumnId)93 QStringList DFBrowserPane_TableView::GetSelectedColumnValues (QTableView* theTableView, const int theColumnId)
94 {
95   QAbstractItemModel* aModel = theTableView->model();
96   QModelIndexList aSelectedIndices = theTableView->selectionModel()->selectedIndexes();
97 
98   QStringList aSelectedEntries;
99   for (QModelIndexList::const_iterator aSelectedIt = aSelectedIndices.begin();
100        aSelectedIt != aSelectedIndices.end(); aSelectedIt++)
101   {
102     QModelIndex anIndex = *aSelectedIt;
103     if (theColumnId == anIndex.column())
104       aSelectedEntries.append (aModel->data (aModel->index (anIndex.row(), theColumnId, anIndex.parent()),
105                                Qt::DisplayRole).toString());
106   }
107   return aSelectedEntries;
108 }
109