0030268: Inspectors - improvements in VInspector plugin
[occt.git] / tools / ViewControl / ViewControl_Tools.cxx
CommitLineData
14bbbdcb 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
6822a3be 16#include <inspector/ViewControl_Tools.hxx>
14bbbdcb 17
7e1c1e48 18#include <inspector/ViewControl_TableModel.hxx>
19#include <inspector/TreeModel_ModelBase.hxx>
20
130eb114 21#include <Standard_WarningsDisable.hxx>
6822a3be 22#include <QAction>
7e1c1e48 23#include <QHeaderView>
6822a3be 24#include <QObject>
25#include <QPalette>
7e1c1e48 26#include <QTableView>
6822a3be 27#include <QWidget>
130eb114 28#include <Standard_WarningsRestore.hxx>
14bbbdcb 29
30// =======================================================================
6822a3be 31// function : CreateAction
14bbbdcb 32// purpose :
33// =======================================================================
6822a3be 34QAction* ViewControl_Tools::CreateAction (const QString& theText, const char* theSlot, QObject* theParent, QObject* theContext)
14bbbdcb 35{
6822a3be 36 QAction* anAction = new QAction (theText, theParent);
37 QObject::connect (anAction, SIGNAL (triggered (bool)), theContext, theSlot);
38 return anAction;
14bbbdcb 39}
40
41// =======================================================================
6822a3be 42// function : SetWhiteBackground
14bbbdcb 43// purpose :
44// =======================================================================
6822a3be 45void ViewControl_Tools::SetWhiteBackground (QWidget* theControl)
14bbbdcb 46{
6822a3be 47 QPalette aPalette = theControl->palette();
48 aPalette.setColor (QPalette::All, QPalette::Foreground, Qt::white);
49 theControl->setPalette (aPalette);
14bbbdcb 50}
7e1c1e48 51
52// =======================================================================
53// function : SetDefaultHeaderSections
54// purpose :
55// =======================================================================
56void ViewControl_Tools::SetDefaultHeaderSections(QTableView* theTableView, const Qt::Orientation theOrientation)
57{
58 ViewControl_TableModel * aTableModel = dynamic_cast<ViewControl_TableModel*> (theTableView->model());
59 ViewControl_TableModelValues* aModelValues = aTableModel->ModelValues();
60 if (!aModelValues)
61 return;
62
63 int aSectionSize;
64 if (aModelValues->DefaultSectionSize (Qt::Horizontal, aSectionSize) )
65 theTableView->horizontalHeader()->setDefaultSectionSize (aSectionSize);
66 else {
67 bool isStretchLastSection = true;
68 for (int aColumnId = 0, aNbColumns = aTableModel->columnCount(); aColumnId < aNbColumns; aColumnId++)
69 {
70 TreeModel_HeaderSection aSection = aModelValues->HeaderItem (theOrientation, aColumnId);
71 if (aSection.IsEmpty())
72 continue;
73
74 int aColumnWidth = aSection.GetWidth();
75 if (aColumnWidth > 0)
76 {
77 theTableView->setColumnWidth (aColumnId, aColumnWidth);
78 if (aColumnId == aNbColumns - 1)
79 isStretchLastSection = false;
80 }
81 theTableView->setColumnHidden (aColumnId, aSection.IsHidden());
82 }
83 if (isStretchLastSection != theTableView->horizontalHeader()->stretchLastSection())
84 theTableView->horizontalHeader()->setStretchLastSection (isStretchLastSection);
85 }
86}
87
88// =======================================================================
89// function : CreateTableModelValues
90// purpose :
91// =======================================================================
92ViewControl_TableModelValues* ViewControl_Tools::CreateTableModelValues (QItemSelectionModel* theSelectionModel)
93{
94 ViewControl_TableModelValues* aTableValues = 0;
95
96 QModelIndex anIndex = TreeModel_ModelBase::SingleSelected (theSelectionModel->selectedIndexes(), 0);
97 TreeModel_ItemBasePtr anItemBase = TreeModel_ModelBase::GetItemByIndex (anIndex);
98 if (!anItemBase)
99 return aTableValues;
100
101 const Handle(TreeModel_ItemProperties)& anItemProperties = anItemBase->Properties();
102 if (anItemProperties.IsNull())
103 return aTableValues;
104
105 aTableValues = new ViewControl_TableModelValues();
106 aTableValues->SetProperties (anItemProperties);
107 return aTableValues;
108}