Skip to content

Commit

Permalink
Refactor align widget
Browse files Browse the repository at this point in the history
Ref: #366
  • Loading branch information
rodlie committed Dec 27, 2024
1 parent 412b272 commit a3388f4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 59 deletions.
3 changes: 2 additions & 1 deletion src/app/friction.qss
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,8 @@ QMenu::separator {
background-color: %4;
}

QMenu::item:disabled {
QMenu::item:disabled,
AlignWidget QPushButton::disabled {
background: transparent;
}

Expand Down
115 changes: 58 additions & 57 deletions src/ui/widgets/alignwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,21 @@
#include <QLabel>
#include <QHBoxLayout>
#include <QPushButton>
#include <QStandardItemModel>

#include "GUI/global.h"
#include "Private/esettings.h"
#include "themesupport.h"

#define INDEX_ALIGN_GEOMETRY 0
#define INDEX_ALIGN_GEOMETRY_PIVOT 1
#define INDEX_ALIGN_PIVOT 2

#define INDEX_REL_SCENE 0
#define INDEX_REL_LAST_SELECTED 1
#define INDEX_REL_LAST_SELECTED_PIVOT 2
#define INDEX_REL_BOUNDINGBOX 3

AlignWidget::AlignWidget(QWidget* const parent)
: QWidget(parent)
, mAlignPivot(nullptr)
Expand All @@ -42,58 +52,32 @@ AlignWidget::AlignWidget(QWidget* const parent)
mainLayout->setContentsMargins(5, 5, 5, 5);
setLayout(mainLayout);

const auto labelsLay = new QHBoxLayout;
mainLayout->insertLayout(0, labelsLay);

const auto labelAlignPivot = new QLabel(tr("Align:"), this);
labelAlignPivot->setAlignment(Qt::AlignLeft);
labelsLay->addWidget(labelAlignPivot, 1);

const auto labelRelativeTo = new QLabel(tr("Relative to:"), this);
labelRelativeTo->setAlignment(Qt::AlignLeft);
labelsLay->addWidget(labelRelativeTo, 1);

labelsLay->addStretch();

const auto combosLay = new QHBoxLayout;
mainLayout->addLayout(combosLay);

combosLay->addWidget(new QLabel(tr("Align")));
mAlignPivot = new QComboBox(this);
mAlignPivot->setMinimumWidth(20);
mAlignPivot->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
mAlignPivot->setFocusPolicy(Qt::NoFocus);
mAlignPivot->addItem(tr("Geometry"));
mAlignPivot->addItem(tr("Geometry by Pivot"));
mAlignPivot->addItem(tr("Pivot"));
mAlignPivot->addItem(tr("Geometry")); // INDEX_ALIGN_GEOMETRY
mAlignPivot->addItem(tr("Geometry by Pivot")); // INDEX_ALIGN_GEOMETRY_PIVOT
mAlignPivot->addItem(tr("Pivot")); // INDEX_ALIGN_PIVOT
combosLay->addWidget(mAlignPivot);

combosLay->addWidget(new QLabel(tr("To")));
mRelativeTo = new QComboBox(this);
mRelativeTo->setMinimumWidth(20);
mRelativeTo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
mRelativeTo->setFocusPolicy(Qt::NoFocus);
mRelativeTo->addItem(tr("Scene"));
mRelativeTo->addItem(tr("Last Selected"));
mRelativeTo->addItem(tr("Scene")); // INDEX_REL_SCENE
mRelativeTo->addItem(tr("Last Selected")); // INDEX_REL_LAST_SELECTED
mRelativeTo->addItem(tr("Last Selected Pivot")); // INDEX_REL_LAST_SELECTED_PIVOT
mRelativeTo->addItem(tr("Bounding Box")); // INDEX_REL_BOUNDINGBOX
combosLay->addWidget(mRelativeTo);

connect(mAlignPivot, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [this](int index) {
static bool isItem2Removed = true;

if (index == 2) {
if (isItem2Removed) {
mRelativeTo->insertItem(2, tr("Last Selected Pivot"));
mRelativeTo->insertItem(3, tr("Bounding Box"));
isItem2Removed = false;
}
mRelativeTo->setCurrentIndex(3);
} else {
if (!isItem2Removed) {
mRelativeTo->removeItem(2);
mRelativeTo->removeItem(2);
isItem2Removed = true;
mRelativeTo->setCurrentIndex(0);
}
}
});
setComboBoxItemState(mRelativeTo, INDEX_REL_LAST_SELECTED_PIVOT, false);
setComboBoxItemState(mRelativeTo, INDEX_REL_BOUNDINGBOX, false);

const auto buttonsLay = new QHBoxLayout;
mainLayout->addLayout(buttonsLay);
Expand Down Expand Up @@ -153,28 +137,30 @@ AlignWidget::AlignWidget(QWidget* const parent)
});
buttonsLay->addWidget(bottomButton);

connect(mRelativeTo, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [this, leftButton, rightButton, topButton, bottomButton]() {
if (mRelativeTo->currentIndex() == 2) {
leftButton->setEnabled(false);
leftButton->setIcon(QIcon());
rightButton->setEnabled(false);
rightButton->setIcon(QIcon());
topButton->setEnabled(false);
topButton->setIcon(QIcon());
bottomButton->setEnabled(false);
bottomButton->setIcon(QIcon());
} else {
leftButton->setEnabled(true);
leftButton->setIcon(QIcon::fromTheme("pivot-align-left"));
rightButton->setEnabled(true);
rightButton->setIcon(QIcon::fromTheme("pivot-align-right"));
topButton->setEnabled(true);
topButton->setIcon(QIcon::fromTheme("pivot-align-top"));
bottomButton->setEnabled(true);
bottomButton->setIcon(QIcon::fromTheme("pivot-align-bottom"));
}
connect(mAlignPivot,
QOverload<int>::of(&QComboBox::currentIndexChanged),
this, [this](int index) {
setComboBoxItemState(mRelativeTo,
INDEX_REL_LAST_SELECTED_PIVOT,
index == INDEX_ALIGN_PIVOT);
setComboBoxItemState(mRelativeTo,
INDEX_REL_BOUNDINGBOX,
index == INDEX_ALIGN_PIVOT);
if (index == INDEX_ALIGN_PIVOT) { mRelativeTo->setCurrentIndex(INDEX_REL_BOUNDINGBOX); }
else { mRelativeTo->setCurrentIndex(INDEX_REL_SCENE); }
});

connect(mRelativeTo,
QOverload<int>::of(&QComboBox::currentIndexChanged),
this, [leftButton,
rightButton,
topButton,
bottomButton](int index) {
leftButton->setEnabled(index != INDEX_REL_LAST_SELECTED_PIVOT);
rightButton->setEnabled(index != INDEX_REL_LAST_SELECTED_PIVOT);
topButton->setEnabled(index != INDEX_REL_LAST_SELECTED_PIVOT);
bottomButton->setEnabled(index != INDEX_REL_LAST_SELECTED_PIVOT);
});

eSizesUI::widget.add(leftButton, [leftButton,
hCenterButton,
Expand All @@ -198,3 +184,18 @@ void AlignWidget::triggerAlign(const Qt::Alignment align)
const auto relativeTo = static_cast<AlignRelativeTo>(mRelativeTo->currentIndex());
emit alignTriggered(align, alignPivot, relativeTo);
}

void AlignWidget::setComboBoxItemState(QComboBox *box,
int index,
bool enabled)
{
auto model = qobject_cast<QStandardItemModel*>(box->model());
if (!model) { return; }

if (index >= box->count() || index < 0) { return; }

auto item = model->item(index);
if (!item) { return; }

item->setEnabled(enabled);
}
4 changes: 3 additions & 1 deletion src/ui/widgets/alignwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ class UI_EXPORT AlignWidget : public QWidget

private:
void triggerAlign(const Qt::Alignment align);
void setComboBoxItemState(QComboBox *box,
int index,
bool enabled);

QComboBox *mAlignPivot;
QComboBox *mRelativeTo;
QComboBox *mRelativeToPivot;
};

#endif // ALIGNWIDGET_H

0 comments on commit a3388f4

Please sign in to comment.