format VisualGroup.cpp file
Signed-off-by: Tayou <tayou@gmx.net>
This commit is contained in:
parent
54d88e4dbf
commit
534d156b12
@ -35,22 +35,17 @@
|
|||||||
|
|
||||||
#include "VisualGroup.h"
|
#include "VisualGroup.h"
|
||||||
|
|
||||||
|
#include <QApplication>
|
||||||
|
#include <QDebug>
|
||||||
#include <QModelIndex>
|
#include <QModelIndex>
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
#include <QtMath>
|
#include <QtMath>
|
||||||
#include <QApplication>
|
|
||||||
#include <QDebug>
|
|
||||||
|
|
||||||
#include "InstanceView.h"
|
#include "InstanceView.h"
|
||||||
|
|
||||||
VisualGroup::VisualGroup(const QString &text, InstanceView *view) : view(view), text(text), collapsed(false)
|
VisualGroup::VisualGroup(const QString& text, InstanceView* view) : view(view), text(text), collapsed(false) {}
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
VisualGroup::VisualGroup(const VisualGroup *other)
|
VisualGroup::VisualGroup(const VisualGroup* other) : view(other->view), text(other->text), collapsed(other->collapsed) {}
|
||||||
: view(other->view), text(other->text), collapsed(other->collapsed)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void VisualGroup::update()
|
void VisualGroup::update()
|
||||||
{
|
{
|
||||||
@ -64,13 +59,11 @@ void VisualGroup::update()
|
|||||||
int positionInRow = 0;
|
int positionInRow = 0;
|
||||||
int currentRow = 0;
|
int currentRow = 0;
|
||||||
int offsetFromTop = 0;
|
int offsetFromTop = 0;
|
||||||
for (auto item: temp_items)
|
for (auto item : temp_items) {
|
||||||
{
|
if (positionInRow == itemsPerRow) {
|
||||||
if(positionInRow == itemsPerRow)
|
|
||||||
{
|
|
||||||
rows[currentRow].height = maxRowHeight;
|
rows[currentRow].height = maxRowHeight;
|
||||||
rows[currentRow].top = offsetFromTop;
|
rows[currentRow].top = offsetFromTop;
|
||||||
currentRow ++;
|
currentRow++;
|
||||||
offsetFromTop += maxRowHeight + 5;
|
offsetFromTop += maxRowHeight + 5;
|
||||||
positionInRow = 0;
|
positionInRow = 0;
|
||||||
maxRowHeight = 0;
|
maxRowHeight = 0;
|
||||||
@ -83,8 +76,7 @@ void VisualGroup::update()
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
auto itemHeight = view->itemDelegate()->sizeHint(viewItemOption, item).height();
|
auto itemHeight = view->itemDelegate()->sizeHint(viewItemOption, item).height();
|
||||||
if(itemHeight > maxRowHeight)
|
if (itemHeight > maxRowHeight) {
|
||||||
{
|
|
||||||
maxRowHeight = itemHeight;
|
maxRowHeight = itemHeight;
|
||||||
}
|
}
|
||||||
rows[currentRow].items.append(item);
|
rows[currentRow].items.append(item);
|
||||||
@ -94,16 +86,13 @@ void VisualGroup::update()
|
|||||||
rows[currentRow].top = offsetFromTop;
|
rows[currentRow].top = offsetFromTop;
|
||||||
}
|
}
|
||||||
|
|
||||||
QPair<int, int> VisualGroup::positionOf(const QModelIndex &index) const
|
QPair<int, int> VisualGroup::positionOf(const QModelIndex& index) const
|
||||||
{
|
{
|
||||||
int y = 0;
|
int y = 0;
|
||||||
for (auto & row: rows)
|
for (auto& row : rows) {
|
||||||
{
|
for (auto x = 0; x < row.items.size(); x++) {
|
||||||
for(auto x = 0; x < row.items.size(); x++)
|
if (row.items[x] == index) {
|
||||||
{
|
return qMakePair(x, y);
|
||||||
if(row.items[x] == index)
|
|
||||||
{
|
|
||||||
return qMakePair(x,y);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
y++;
|
y++;
|
||||||
@ -112,50 +101,44 @@ QPair<int, int> VisualGroup::positionOf(const QModelIndex &index) const
|
|||||||
return qMakePair(0, 0);
|
return qMakePair(0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int VisualGroup::rowTopOf(const QModelIndex &index) const
|
int VisualGroup::rowTopOf(const QModelIndex& index) const
|
||||||
{
|
{
|
||||||
auto position = positionOf(index);
|
auto position = positionOf(index);
|
||||||
return rows[position.second].top;
|
return rows[position.second].top;
|
||||||
}
|
}
|
||||||
|
|
||||||
int VisualGroup::rowHeightOf(const QModelIndex &index) const
|
int VisualGroup::rowHeightOf(const QModelIndex& index) const
|
||||||
{
|
{
|
||||||
auto position = positionOf(index);
|
auto position = positionOf(index);
|
||||||
return rows[position.second].height;
|
return rows[position.second].height;
|
||||||
}
|
}
|
||||||
|
|
||||||
VisualGroup::HitResults VisualGroup::hitScan(const QPoint &pos) const
|
VisualGroup::HitResults VisualGroup::hitScan(const QPoint& pos) const
|
||||||
{
|
{
|
||||||
VisualGroup::HitResults results = VisualGroup::NoHit;
|
VisualGroup::HitResults results = VisualGroup::NoHit;
|
||||||
int y_start = verticalPosition();
|
int y_start = verticalPosition();
|
||||||
int body_start = y_start + headerHeight();
|
int body_start = y_start + headerHeight();
|
||||||
int body_end = body_start + contentHeight() + 5; // FIXME: wtf is this 5?
|
int body_end = body_start + contentHeight() + 5; // FIXME: wtf is this 5?
|
||||||
int y = pos.y();
|
int y = pos.y();
|
||||||
// int x = pos.x();
|
// int x = pos.x();
|
||||||
if (y < y_start)
|
if (y < y_start) {
|
||||||
{
|
|
||||||
results = VisualGroup::NoHit;
|
results = VisualGroup::NoHit;
|
||||||
}
|
} else if (y < body_start) {
|
||||||
else if (y < body_start)
|
|
||||||
{
|
|
||||||
results = VisualGroup::HeaderHit;
|
results = VisualGroup::HeaderHit;
|
||||||
int collapseSize = headerHeight() - 4;
|
int collapseSize = headerHeight() - 4;
|
||||||
|
|
||||||
// the icon
|
// the icon
|
||||||
QRect iconRect = QRect(view->m_leftMargin + 2, 2 + y_start, collapseSize, collapseSize);
|
QRect iconRect = QRect(view->m_leftMargin + 2, 2 + y_start, collapseSize, collapseSize);
|
||||||
if (iconRect.contains(pos))
|
if (iconRect.contains(pos)) {
|
||||||
{
|
|
||||||
results |= VisualGroup::CheckboxHit;
|
results |= VisualGroup::CheckboxHit;
|
||||||
}
|
}
|
||||||
}
|
} else if (y < body_end) {
|
||||||
else if (y < body_end)
|
|
||||||
{
|
|
||||||
results |= VisualGroup::BodyHit;
|
results |= VisualGroup::BodyHit;
|
||||||
}
|
}
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
void VisualGroup::drawHeader(QPainter *painter, const QStyleOptionViewItem &option)
|
void VisualGroup::drawHeader(QPainter* painter, const QStyleOptionViewItem& option)
|
||||||
{
|
{
|
||||||
QRect optRect = option.rect;
|
QRect optRect = option.rect;
|
||||||
optRect.setTop(optRect.top() + 7);
|
optRect.setTop(optRect.top() + 7);
|
||||||
@ -163,7 +146,7 @@ void VisualGroup::drawHeader(QPainter *painter, const QStyleOptionViewItem &opti
|
|||||||
font.setBold(true);
|
font.setBold(true);
|
||||||
const QFontMetrics fontMetrics = QFontMetrics(font);
|
const QFontMetrics fontMetrics = QFontMetrics(font);
|
||||||
|
|
||||||
int centerHeight = optRect.top() + fontMetrics.height()/2;
|
int centerHeight = optRect.top() + fontMetrics.height() / 2;
|
||||||
|
|
||||||
QPen pen;
|
QPen pen;
|
||||||
pen.setWidth(2);
|
pen.setWidth(2);
|
||||||
@ -172,28 +155,32 @@ void VisualGroup::drawHeader(QPainter *painter, const QStyleOptionViewItem &opti
|
|||||||
pen.setColor(penColor);
|
pen.setColor(penColor);
|
||||||
painter->setPen(pen);
|
painter->setPen(pen);
|
||||||
|
|
||||||
int arrowOffsetLeft = fontMetrics.height()/2 + 7;
|
int arrowOffsetLeft = fontMetrics.height() / 2 + 7;
|
||||||
int textOffsetLeft = arrowOffsetLeft *2;
|
int textOffsetLeft = arrowOffsetLeft * 2;
|
||||||
int arrowSize = 6;
|
int arrowSize = 6;
|
||||||
|
|
||||||
//BEGIN: arrow
|
// BEGIN: arrow
|
||||||
{
|
{
|
||||||
painter->setRenderHint(QPainter::Antialiasing, false);
|
painter->setRenderHint(QPainter::Antialiasing, false);
|
||||||
painter->save();
|
painter->save();
|
||||||
|
|
||||||
QPolygon polygon;
|
QPolygon polygon;
|
||||||
if (collapsed) {
|
if (collapsed) {
|
||||||
polygon << QPoint(arrowOffsetLeft - arrowSize/2, centerHeight - arrowSize) << QPoint(arrowOffsetLeft + arrowSize/2, centerHeight) << QPoint(arrowOffsetLeft - arrowSize/2, centerHeight + arrowSize);
|
polygon << QPoint(arrowOffsetLeft - arrowSize / 2, centerHeight - arrowSize)
|
||||||
|
<< QPoint(arrowOffsetLeft + arrowSize / 2, centerHeight)
|
||||||
|
<< QPoint(arrowOffsetLeft - arrowSize / 2, centerHeight + arrowSize);
|
||||||
painter->drawPolyline(polygon);
|
painter->drawPolyline(polygon);
|
||||||
} else {
|
} else {
|
||||||
polygon << QPoint(arrowOffsetLeft - arrowSize, centerHeight - arrowSize/2) << QPoint(arrowOffsetLeft, centerHeight + arrowSize/2) << QPoint(arrowOffsetLeft + arrowSize, centerHeight - arrowSize/2);
|
polygon << QPoint(arrowOffsetLeft - arrowSize, centerHeight - arrowSize / 2)
|
||||||
|
<< QPoint(arrowOffsetLeft, centerHeight + arrowSize / 2)
|
||||||
|
<< QPoint(arrowOffsetLeft + arrowSize, centerHeight - arrowSize / 2);
|
||||||
painter->drawPolyline(polygon);
|
painter->drawPolyline(polygon);
|
||||||
}
|
}
|
||||||
painter->restore();
|
painter->restore();
|
||||||
}
|
}
|
||||||
//END: arrow
|
// END: arrow
|
||||||
|
|
||||||
//BEGIN: text
|
// BEGIN: text
|
||||||
{
|
{
|
||||||
painter->setRenderHint(QPainter::Antialiasing);
|
painter->setRenderHint(QPainter::Antialiasing);
|
||||||
QRect textRect(optRect);
|
QRect textRect(optRect);
|
||||||
@ -207,7 +194,7 @@ void VisualGroup::drawHeader(QPainter *painter, const QStyleOptionViewItem &opti
|
|||||||
painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, !text.isEmpty() ? text : QObject::tr("Ungrouped"));
|
painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, !text.isEmpty() ? text : QObject::tr("Ungrouped"));
|
||||||
painter->restore();
|
painter->restore();
|
||||||
}
|
}
|
||||||
//END: text
|
// END: text
|
||||||
}
|
}
|
||||||
|
|
||||||
int VisualGroup::totalHeight() const
|
int VisualGroup::totalHeight() const
|
||||||
@ -222,7 +209,7 @@ int VisualGroup::headerHeight() const
|
|||||||
QFontMetrics fontMetrics(font);
|
QFontMetrics fontMetrics(font);
|
||||||
|
|
||||||
const int height = fontMetrics.height() + 1 /* 1 pixel-width gradient */
|
const int height = fontMetrics.height() + 1 /* 1 pixel-width gradient */
|
||||||
+ 11 /* top and bottom separation */;
|
+ 11 /* top and bottom separation */;
|
||||||
return height;
|
return height;
|
||||||
/*
|
/*
|
||||||
int raw = view->viewport()->fontMetrics().height() + 4;
|
int raw = view->viewport()->fontMetrics().height() + 4;
|
||||||
@ -235,8 +222,7 @@ int VisualGroup::headerHeight() const
|
|||||||
|
|
||||||
int VisualGroup::contentHeight() const
|
int VisualGroup::contentHeight() const
|
||||||
{
|
{
|
||||||
if (collapsed)
|
if (collapsed) {
|
||||||
{
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
auto last = rows[numRows() - 1];
|
auto last = rows[numRows() - 1];
|
||||||
@ -256,11 +242,9 @@ int VisualGroup::verticalPosition() const
|
|||||||
QList<QModelIndex> VisualGroup::items() const
|
QList<QModelIndex> VisualGroup::items() const
|
||||||
{
|
{
|
||||||
QList<QModelIndex> indices;
|
QList<QModelIndex> indices;
|
||||||
for (int i = 0; i < view->model()->rowCount(); ++i)
|
for (int i = 0; i < view->model()->rowCount(); ++i) {
|
||||||
{
|
|
||||||
const QModelIndex index = view->model()->index(i, 0);
|
const QModelIndex index = view->model()->index(i, 0);
|
||||||
if (index.data(InstanceViewRoles::GroupRole).toString() == text)
|
if (index.data(InstanceViewRoles::GroupRole).toString() == text) {
|
||||||
{
|
|
||||||
indices.append(index);
|
indices.append(index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user