1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
// Created on: 2017-06-16
// Created by: Natalia ERMOLAEVA
// Copyright (c) 2017 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
#include <inspector/TreeModel_Tools.hxx>
#include <inspector/TreeModel_ModelBase.hxx>
#include <inspector/TreeModel_ColumnType.hxx>
#include <inspector/TreeModel_VisibilityState.hxx>
#include <Standard_WarningsDisable.hxx>
#include <QAction>
#include <QApplication>
#include <QFont>
#include <QFontMetrics>
#include <QHeaderView>
#include <QObject>
#include <QRegExp>
#include <QStringList>
#include <QStyle>
#include <QTreeView>
#include <Standard_WarningsRestore.hxx>
const int INFO_LENGHT = 60;
// =======================================================================
// function : ToString
// purpose :
// =======================================================================
QString TreeModel_Tools::ToString(const QByteArray& theValue)
{
char aBuffer[8];
QStringList aBytesList;
for (int aByteId = 0; aByteId < theValue.size(); aByteId++)
{
::sprintf(aBuffer, "#%02X", (unsigned char)theValue.at(aByteId));
aBytesList.append(QString(aBuffer));
}
return QString("@ByteArray[%1]").arg(aBytesList.join(" "));
}
// =======================================================================
// function : ToByteArray
// purpose :
// =======================================================================
QByteArray TreeModel_Tools::ToByteArray(const QString& theValue)
{
QByteArray aStateArray;
if (!theValue.startsWith("@ByteArray[") || !theValue.endsWith(']'))
return aStateArray;
QString aValue = theValue.mid(11, theValue.size() - 12);
#include <Standard_WarningsDisable.hxx>
QStringList lst = aValue.split(QRegExp("[\\s|,]"), QString::SkipEmptyParts);
#include <Standard_WarningsRestore.hxx>
for (QStringList::ConstIterator aByteId = lst.begin(); aByteId != lst.end(); ++aByteId)
{
int aBase = 10;
QString aString = *aByteId;
if (aString.startsWith("#"))
{
aBase = 16;
aString = aString.mid(1);
}
bool isOk = false;
int aNum = aString.toInt(&isOk, aBase);
if (!isOk || aNum < 0 || aNum > 255)
continue;
aStateArray.append((char)aNum);
}
return aStateArray;
}
// =======================================================================
// function : SaveState
// purpose :
// =======================================================================
void TreeModel_Tools::SaveState(QTreeView* theTreeView,
QMap<QString, QString>& theItems,
const QString& thePrefix)
{
QStringList aColumnWidths;
for (int aColumnId = 0; aColumnId < theTreeView->model()->columnCount(); aColumnId++)
{
if (theTreeView->isColumnHidden(aColumnId))
{
aColumnWidths.append(QString());
}
else
aColumnWidths.append(QString::number(theTreeView->columnWidth(aColumnId)));
}
theItems[thePrefix + "columns_width"] = aColumnWidths.join(",");
}
// =======================================================================
// function : RestoreState
// purpose :
// =======================================================================
bool TreeModel_Tools::RestoreState(QTreeView* theTreeView,
const QString& theKey,
const QString& theValue,
const QString& thePrefix)
{
if (theKey == thePrefix + "columns_width")
{
QStringList aValues = theValue.split(",");
for (int aColumnId = 0;
aColumnId < theTreeView->model()->columnCount() && aColumnId < aValues.size();
aColumnId++)
{
bool isOk;
int aWidth = aValues.at(aColumnId).toInt(&isOk);
if (isOk && !theTreeView->isColumnHidden(aColumnId)) // do not resize hidden columns
theTreeView->setColumnWidth(aColumnId, aWidth);
}
}
else
return false;
return true;
}
// =======================================================================
// function : SetDefaultHeaderSections
// purpose :
// =======================================================================
void TreeModel_Tools::SetDefaultHeaderSections(QTreeView* theTreeView)
{
TreeModel_ModelBase* aTreeModel = dynamic_cast<TreeModel_ModelBase*>(theTreeView->model());
for (int aColumnId = 0, aNbColumns = aTreeModel->columnCount(); aColumnId < aNbColumns;
aColumnId++)
{
TreeModel_HeaderSection* aSection = aTreeModel->ChangeHeaderItem(aColumnId);
theTreeView->setColumnWidth(aColumnId, aSection->GetWidth());
theTreeView->setColumnHidden(aColumnId, aSection->IsHidden());
}
}
// =======================================================================
// function : UseVisibilityColumn
// purpose :
// =======================================================================
void TreeModel_Tools::UseVisibilityColumn(QTreeView* theTreeView, const bool theActive)
{
QHeaderView* aHeader = theTreeView->header();
#if QT_VERSION < 0x050000
aHeader->setResizeMode(TreeModel_ColumnType_Visibility, QHeaderView::Fixed);
#else
aHeader->setSectionResizeMode(TreeModel_ColumnType_Visibility, QHeaderView::Fixed);
#endif
aHeader->moveSection(TreeModel_ColumnType_Name, TreeModel_ColumnType_Visibility);
TreeModel_ModelBase* aModel = dynamic_cast<TreeModel_ModelBase*>(theTreeView->model());
TreeModel_HeaderSection* anItem = aModel->ChangeHeaderItem((int)TreeModel_ColumnType_Visibility);
anItem->SetIsHidden(theActive);
anItem->SetWidth(TreeModel_ModelBase::ColumnVisibilityWidth());
aModel->SetUseVisibilityColumn(theActive);
}
// =======================================================================
// function : GetTextWidth
// purpose :
// =======================================================================
int TreeModel_Tools::GetTextWidth(const QString& theText, QObject*)
{
// TODO: find margins like QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin, 0,
// (QWidget*)theParent);
int aTextMargin = 10;
QFontMetrics aFontMetrics(QApplication::font());
QRect aBoundingRect = aFontMetrics.boundingRect(theText);
#include <Standard_WarningsDisable.hxx>
return qMax(aBoundingRect.width(), aFontMetrics.width(theText)) + aTextMargin * 2;
#include <Standard_WarningsRestore.hxx>
}
// =======================================================================
// function : CutString
// purpose :
// =======================================================================
QString TreeModel_Tools::CutString(const QString& theText,
const int theWidth,
const QString& theTail)
{
if (theText.isEmpty())
return theText;
int aLength = theWidth < 0 ? INFO_LENGHT : theWidth - 3;
int anIndex = theText.indexOf('\n');
if (anIndex > 0 && anIndex < aLength)
aLength = anIndex;
return aLength < theText.length() ? theText.mid(0, aLength) + theTail : theText;
}
// =======================================================================
// function : LightHighlightColor
// purpose :
// =======================================================================
QColor TreeModel_Tools::LightHighlightColor()
{
QWidget aWidget;
QPalette aPalette = aWidget.palette();
return aPalette.highlight().color().lighter();
}
// =======================================================================
// function : SetExpandedTo
// purpose :
// =======================================================================
void TreeModel_Tools::SetExpandedTo(QTreeView* theTreeView, const QModelIndex& theIndex)
{
QAbstractItemModel* aModel = theTreeView->model();
QModelIndex aParent = aModel->parent(theIndex);
while (aParent.isValid())
{
theTreeView->setExpanded(aParent, true);
aParent = aModel->parent(aParent);
}
}
// =======================================================================
// function : setExpanded
// purpose :
// =======================================================================
void TreeModel_Tools::SetExpanded(QTreeView* theTreeView,
const QModelIndex& theIndex,
const bool isExpanded,
int& theLevels)
{
bool isToExpand = theLevels == -1 || theLevels > 0;
if (!isToExpand)
return;
theTreeView->setExpanded(theIndex, isExpanded);
if (theLevels != -1)
theLevels--;
QAbstractItemModel* aModel = theTreeView->model();
for (int aRowId = 0, aRows = aModel->rowCount(theIndex); aRowId < aRows; aRowId++)
{
int aLevels = theLevels;
SetExpanded(theTreeView, aModel->index(aRowId, 0, theIndex), isExpanded, aLevels);
}
}
|