leocad/qt/lc_qutils.cpp

110 lines
2.9 KiB
C++
Raw Normal View History

2013-08-09 06:57:18 +02:00
#include "lc_global.h"
#include "lc_qutils.h"
#include "lc_application.h"
#include "lc_library.h"
#include "pieceinf.h"
QString lcFormatValue(float Value)
{
QString String = QString::number(Value, 'f', 6);
int Dot = String.indexOf('.');
if (Dot != -1)
{
while (String.endsWith('0'))
String.chop(1);
if (String.endsWith('.'))
String.chop(1);
}
return String;
}
QString lcFormatValueLocalized(float Value)
{
return QLocale::system().toString(Value, 'g', 100);
}
2013-08-09 06:57:18 +02:00
// Resize all columns to content except for one stretching column. (taken from QT creator)
lcQTreeWidgetColumnStretcher::lcQTreeWidgetColumnStretcher(QTreeWidget *treeWidget, int columnToStretch)
: QObject(treeWidget->header()), m_columnToStretch(columnToStretch)
{
parent()->installEventFilter(this);
QHideEvent fake;
lcQTreeWidgetColumnStretcher::eventFilter(parent(), &fake);
}
bool lcQTreeWidgetColumnStretcher::eventFilter(QObject *obj, QEvent *ev)
{
if (obj == parent())
{
if (ev->type() == QEvent::Show)
{
QHeaderView *hv = qobject_cast<QHeaderView*>(obj);
for (int i = 0; i < hv->count(); ++i)
2014-01-26 07:56:36 +01:00
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
hv->setSectionResizeMode(i, QHeaderView::Interactive);
#else
2013-08-09 06:57:18 +02:00
hv->setResizeMode(i, QHeaderView::Interactive);
2014-01-26 07:56:36 +01:00
#endif
2013-08-09 06:57:18 +02:00
}
else if (ev->type() == QEvent::Hide)
{
QHeaderView *hv = qobject_cast<QHeaderView*>(obj);
for (int i = 0; i < hv->count(); ++i)
2014-01-26 07:56:36 +01:00
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
hv->setSectionResizeMode(i, i == m_columnToStretch ? QHeaderView::Stretch : QHeaderView::ResizeToContents);
#else
2013-08-09 06:57:18 +02:00
hv->setResizeMode(i, i == m_columnToStretch ? QHeaderView::Stretch : QHeaderView::ResizeToContents);
2014-01-26 07:56:36 +01:00
#endif
2013-08-09 06:57:18 +02:00
}
else if (ev->type() == QEvent::Resize)
{
QHeaderView *hv = qobject_cast<QHeaderView*>(obj);
2014-01-26 07:56:36 +01:00
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
if (hv->sectionResizeMode(m_columnToStretch) == QHeaderView::Interactive)
#else
2013-08-09 06:57:18 +02:00
if (hv->resizeMode(m_columnToStretch) == QHeaderView::Interactive)
2014-01-26 07:56:36 +01:00
#endif
2013-08-09 06:57:18 +02:00
{
QResizeEvent *re = static_cast<QResizeEvent*>(ev);
int diff = re->size().width() - re->oldSize().width() ;
hv->resizeSection(m_columnToStretch, qMax(32, hv->sectionSize(1) + diff));
}
}
}
return false;
}
lcQPartsListModel::lcQPartsListModel(QObject *parent)
: QAbstractListModel(parent)
{
}
int lcQPartsListModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return lcGetPiecesLibrary()->mPieces.GetSize() * 2;
}
QVariant lcQPartsListModel::data(const QModelIndex &index, int role) const
{
int partIndex = index.row() / 2;
if (partIndex < 0 || partIndex >= lcGetPiecesLibrary()->mPieces.GetSize())
return QVariant();
if (role == Qt::DisplayRole || role == Qt::EditRole)
{
if (index.row() & 1)
return QString(lcGetPiecesLibrary()->mPieces[partIndex]->m_strName);
else
return QString(lcGetPiecesLibrary()->mPieces[partIndex]->m_strDescription);
}
return QVariant();
}