Skip to content

Commit 65772ce

Browse files
committed
Current preset is checked in the popup
1 parent ab0c59b commit 65772ce

3 files changed

Lines changed: 160 additions & 18 deletions

File tree

src/ui/layout/AnchorPopupMenu.cpp

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,26 @@ AnchorPopupMenu::AnchorPopupMenu(LayoutScene& scene, QWidget* parent)
1414

1515
ui->setupUi(this);
1616

17+
_horzGroup = new QActionGroup(this);
18+
_horzGroup->addAction(ui->actionParentLeft);
19+
_horzGroup->addAction(ui->actionSelfLeft);
20+
_horzGroup->addAction(ui->actionParentHCenter);
21+
_horzGroup->addAction(ui->actionSelfHCenter);
22+
_horzGroup->addAction(ui->actionParentRight);
23+
_horzGroup->addAction(ui->actionSelfRight);
24+
_horzGroup->addAction(ui->actionParentHStretch);
25+
_horzGroup->addAction(ui->actionSelfHStretch);
26+
27+
_vertGroup = new QActionGroup(this);
28+
_vertGroup->addAction(ui->actionParentTop);
29+
_vertGroup->addAction(ui->actionSelfTop);
30+
_vertGroup->addAction(ui->actionParentVCenter);
31+
_vertGroup->addAction(ui->actionSelfVCenter);
32+
_vertGroup->addAction(ui->actionParentBottom);
33+
_vertGroup->addAction(ui->actionSelfBottom);
34+
_vertGroup->addAction(ui->actionParentVStretch);
35+
_vertGroup->addAction(ui->actionSelfVStretch);
36+
1737
ui->btnParentLeft->setDefaultAction(ui->actionParentLeft);
1838
ui->btnSelfLeft->setDefaultAction(ui->actionSelfLeft);
1939
ui->btnParentTop->setDefaultAction(ui->actionParentTop);
@@ -298,3 +318,90 @@ void AnchorPopupMenu::on_btnSelfStretch_clicked()
298318
}
299319
close();
300320
}
321+
322+
static inline bool compareFloat(float a, float b, float tolerance)
323+
{
324+
return std::abs(a - b) <= tolerance;
325+
}
326+
327+
// Detect current presets. Tolerance is relatively big because of offset pixel rounding,
328+
// which may lead relative part to be not exactly the value requested.
329+
void AnchorPopupMenu::showEvent(QShowEvent* event)
330+
{
331+
for (auto action : _horzGroup->actions())
332+
action->setChecked(false);
333+
334+
for (auto action : _vertGroup->actions())
335+
action->setChecked(false);
336+
337+
float minX, maxX, minY, maxY;
338+
if (!_scene.getAnchorValues(minX, maxX, minY, maxY)) return;
339+
340+
constexpr float tolerance = 1.f / 1920.f;
341+
342+
if (compareFloat(minX, 0.f, tolerance) && compareFloat(maxX, 0.f, tolerance))
343+
ui->actionParentLeft->setChecked(true);
344+
else if (compareFloat(minX, 0.f, tolerance) && compareFloat(maxX, 1.f, tolerance))
345+
ui->actionParentHStretch->setChecked(true);
346+
else if (compareFloat(minX, 0.5f, tolerance) && compareFloat(maxX, 0.5f, tolerance))
347+
ui->actionParentHCenter->setChecked(true);
348+
else if (compareFloat(minX, 1.f, tolerance) && compareFloat(maxX, 1.f, tolerance))
349+
ui->actionParentRight->setChecked(true);
350+
else
351+
{
352+
LayoutManipulator* target = _scene.getAnchorTarget();
353+
const auto baseSize = target->getBaseSize();
354+
const auto minPt = target->getWidget()->getPosition().d_x;
355+
const auto midPt = minPt + target->getWidget()->getSize().d_width * 0.5f;
356+
const auto maxPt = minPt + target->getWidget()->getSize().d_width;
357+
const auto minAnchorValue = CEGUI::CoordConverter::asRelative(minPt, baseSize.d_width);
358+
const auto midAnchorValue = CEGUI::CoordConverter::asRelative(midPt, baseSize.d_width);
359+
const auto maxAnchorValue = CEGUI::CoordConverter::asRelative(maxPt, baseSize.d_width);
360+
361+
if (compareFloat(minX, minAnchorValue, tolerance))
362+
{
363+
if (compareFloat(maxX, minAnchorValue, tolerance))
364+
ui->actionSelfLeft->setChecked(true);
365+
else if (compareFloat(maxX, maxAnchorValue, tolerance))
366+
ui->actionSelfHStretch->setChecked(true);
367+
}
368+
else if (compareFloat(minX, midAnchorValue, tolerance) && compareFloat(maxX, midAnchorValue, tolerance))
369+
ui->actionSelfHCenter->setChecked(true);
370+
else if (compareFloat(minX, maxAnchorValue, tolerance) && compareFloat(maxX, maxAnchorValue, tolerance))
371+
ui->actionSelfRight->setChecked(true);
372+
}
373+
374+
if (compareFloat(minY, 0.f, tolerance) && compareFloat(maxY, 0.f, tolerance))
375+
ui->actionParentTop->setChecked(true);
376+
else if (compareFloat(minY, 0.f, tolerance) && compareFloat(maxY, 1.f, tolerance))
377+
ui->actionParentVStretch->setChecked(true);
378+
else if (compareFloat(minY, 0.5f, tolerance) && compareFloat(maxY, 0.5f, tolerance))
379+
ui->actionParentVCenter->setChecked(true);
380+
else if (compareFloat(minY, 1.f, tolerance) && compareFloat(maxY, 1.f, tolerance))
381+
ui->actionParentBottom->setChecked(true);
382+
else
383+
{
384+
LayoutManipulator* target = _scene.getAnchorTarget();
385+
const auto baseSize = target->getBaseSize();
386+
const auto minPt = target->getWidget()->getPosition().d_y;
387+
const auto midPt = minPt + target->getWidget()->getSize().d_height * 0.5f;
388+
const auto maxPt = minPt + target->getWidget()->getSize().d_height;
389+
const auto minAnchorValue = CEGUI::CoordConverter::asRelative(minPt, baseSize.d_height);
390+
const auto midAnchorValue = CEGUI::CoordConverter::asRelative(midPt, baseSize.d_height);
391+
const auto maxAnchorValue = CEGUI::CoordConverter::asRelative(maxPt, baseSize.d_height);
392+
393+
if (compareFloat(minY, minAnchorValue, tolerance))
394+
{
395+
if (compareFloat(maxY, minAnchorValue, tolerance))
396+
ui->actionSelfTop->setChecked(true);
397+
else if (compareFloat(maxY, maxAnchorValue, tolerance))
398+
ui->actionSelfVStretch->setChecked(true);
399+
}
400+
else if (compareFloat(minY, midAnchorValue, tolerance) && compareFloat(maxY, midAnchorValue, tolerance))
401+
ui->actionSelfVCenter->setChecked(true);
402+
else if (compareFloat(minY, maxAnchorValue, tolerance) && compareFloat(maxY, maxAnchorValue, tolerance))
403+
ui->actionSelfBottom->setChecked(true);
404+
}
405+
406+
QWidget::showEvent(event);
407+
}

src/ui/layout/AnchorPopupMenu.h

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class AnchorPopupMenu;
1212
}
1313

1414
class LayoutScene;
15+
class QActionGroup;
1516

1617
class AnchorPopupMenu : public QWidget
1718
{
@@ -30,47 +31,33 @@ private slots:
3031
void on_actionSelfLeft_triggered();
3132
void on_actionSelfTop_triggered();
3233
void on_btnSelfLeftTop_clicked();
33-
3434
void on_actionParentHCenter_triggered();
35-
3635
void on_actionParentVCenter_triggered();
37-
3836
void on_actionSelfHCenter_triggered();
39-
4037
void on_actionSelfVCenter_triggered();
41-
4238
void on_btnParentCenter_clicked();
43-
4439
void on_btnSelfCenter_clicked();
45-
4640
void on_actionParentRight_triggered();
47-
4841
void on_actionParentBottom_triggered();
49-
5042
void on_btnParentRightBottom_clicked();
51-
5243
void on_actionSelfRight_triggered();
53-
5444
void on_actionSelfBottom_triggered();
55-
5645
void on_btnSelfRightBottom_clicked();
57-
5846
void on_actionParentHStretch_triggered();
59-
6047
void on_actionParentVStretch_triggered();
61-
6248
void on_btnParentStretch_clicked();
63-
6449
void on_actionSelfHStretch_triggered();
65-
6650
void on_actionSelfVStretch_triggered();
67-
6851
void on_btnSelfStretch_clicked();
6952

7053
private:
7154

55+
virtual void showEvent(QShowEvent* event) override;
56+
7257
Ui::AnchorPopupMenu *ui;
7358
LayoutScene& _scene;
59+
QActionGroup* _horzGroup = nullptr;
60+
QActionGroup* _vertGroup = nullptr;
7461
};
7562

7663
#endif // ANCHORPOPUPMENU_H

ui/layout/AnchorPopupMenu.ui

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,9 @@
528528
</layout>
529529
</widget>
530530
<action name="actionParentLeft">
531+
<property name="checkable">
532+
<bool>true</bool>
533+
</property>
531534
<property name="icon">
532535
<iconset resource="../../data/Resources.qrc">
533536
<normaloff>:/icons/anchors/ParentLeft.png</normaloff>:/icons/anchors/ParentLeft.png</iconset>
@@ -540,6 +543,9 @@
540543
</property>
541544
</action>
542545
<action name="actionSelfLeft">
546+
<property name="checkable">
547+
<bool>true</bool>
548+
</property>
543549
<property name="icon">
544550
<iconset resource="../../data/Resources.qrc">
545551
<normaloff>:/icons/anchors/SelfLeft.png</normaloff>:/icons/anchors/SelfLeft.png</iconset>
@@ -552,6 +558,9 @@
552558
</property>
553559
</action>
554560
<action name="actionParentTop">
561+
<property name="checkable">
562+
<bool>true</bool>
563+
</property>
555564
<property name="icon">
556565
<iconset resource="../../data/Resources.qrc">
557566
<normaloff>:/icons/anchors/ParentTop.png</normaloff>:/icons/anchors/ParentTop.png</iconset>
@@ -564,6 +573,9 @@
564573
</property>
565574
</action>
566575
<action name="actionSelfTop">
576+
<property name="checkable">
577+
<bool>true</bool>
578+
</property>
567579
<property name="icon">
568580
<iconset resource="../../data/Resources.qrc">
569581
<normaloff>:/icons/anchors/SelfTop.png</normaloff>:/icons/anchors/SelfTop.png</iconset>
@@ -576,6 +588,9 @@
576588
</property>
577589
</action>
578590
<action name="actionParentHCenter">
591+
<property name="checkable">
592+
<bool>true</bool>
593+
</property>
579594
<property name="icon">
580595
<iconset resource="../../data/Resources.qrc">
581596
<normaloff>:/icons/anchors/ParentHCenter.png</normaloff>:/icons/anchors/ParentHCenter.png</iconset>
@@ -588,6 +603,9 @@
588603
</property>
589604
</action>
590605
<action name="actionParentVCenter">
606+
<property name="checkable">
607+
<bool>true</bool>
608+
</property>
591609
<property name="icon">
592610
<iconset resource="../../data/Resources.qrc">
593611
<normaloff>:/icons/anchors/ParentVCenter.png</normaloff>:/icons/anchors/ParentVCenter.png</iconset>
@@ -600,6 +618,9 @@
600618
</property>
601619
</action>
602620
<action name="actionSelfHCenter">
621+
<property name="checkable">
622+
<bool>true</bool>
623+
</property>
603624
<property name="icon">
604625
<iconset resource="../../data/Resources.qrc">
605626
<normaloff>:/icons/anchors/SelfHCenter.png</normaloff>:/icons/anchors/SelfHCenter.png</iconset>
@@ -612,6 +633,9 @@
612633
</property>
613634
</action>
614635
<action name="actionSelfVCenter">
636+
<property name="checkable">
637+
<bool>true</bool>
638+
</property>
615639
<property name="icon">
616640
<iconset resource="../../data/Resources.qrc">
617641
<normaloff>:/icons/anchors/SelfVCenter.png</normaloff>:/icons/anchors/SelfVCenter.png</iconset>
@@ -624,6 +648,9 @@
624648
</property>
625649
</action>
626650
<action name="actionParentRight">
651+
<property name="checkable">
652+
<bool>true</bool>
653+
</property>
627654
<property name="icon">
628655
<iconset resource="../../data/Resources.qrc">
629656
<normaloff>:/icons/anchors/ParentRight.png</normaloff>:/icons/anchors/ParentRight.png</iconset>
@@ -636,6 +663,9 @@
636663
</property>
637664
</action>
638665
<action name="actionSelfRight">
666+
<property name="checkable">
667+
<bool>true</bool>
668+
</property>
639669
<property name="icon">
640670
<iconset resource="../../data/Resources.qrc">
641671
<normaloff>:/icons/anchors/SelfRight.png</normaloff>:/icons/anchors/SelfRight.png</iconset>
@@ -648,6 +678,9 @@
648678
</property>
649679
</action>
650680
<action name="actionParentBottom">
681+
<property name="checkable">
682+
<bool>true</bool>
683+
</property>
651684
<property name="icon">
652685
<iconset resource="../../data/Resources.qrc">
653686
<normaloff>:/icons/anchors/ParentBottom.png</normaloff>:/icons/anchors/ParentBottom.png</iconset>
@@ -660,6 +693,9 @@
660693
</property>
661694
</action>
662695
<action name="actionSelfBottom">
696+
<property name="checkable">
697+
<bool>true</bool>
698+
</property>
663699
<property name="icon">
664700
<iconset resource="../../data/Resources.qrc">
665701
<normaloff>:/icons/anchors/SelfBottom.png</normaloff>:/icons/anchors/SelfBottom.png</iconset>
@@ -672,6 +708,9 @@
672708
</property>
673709
</action>
674710
<action name="actionParentHStretch">
711+
<property name="checkable">
712+
<bool>true</bool>
713+
</property>
675714
<property name="icon">
676715
<iconset resource="../../data/Resources.qrc">
677716
<normaloff>:/icons/anchors/ParentHStretch.png</normaloff>:/icons/anchors/ParentHStretch.png</iconset>
@@ -684,6 +723,9 @@
684723
</property>
685724
</action>
686725
<action name="actionParentVStretch">
726+
<property name="checkable">
727+
<bool>true</bool>
728+
</property>
687729
<property name="icon">
688730
<iconset resource="../../data/Resources.qrc">
689731
<normaloff>:/icons/anchors/ParentVStretch.png</normaloff>:/icons/anchors/ParentVStretch.png</iconset>
@@ -696,6 +738,9 @@
696738
</property>
697739
</action>
698740
<action name="actionSelfHStretch">
741+
<property name="checkable">
742+
<bool>true</bool>
743+
</property>
699744
<property name="icon">
700745
<iconset resource="../../data/Resources.qrc">
701746
<normaloff>:/icons/anchors/SelfHStretch.png</normaloff>:/icons/anchors/SelfHStretch.png</iconset>
@@ -708,6 +753,9 @@
708753
</property>
709754
</action>
710755
<action name="actionSelfVStretch">
756+
<property name="checkable">
757+
<bool>true</bool>
758+
</property>
711759
<property name="icon">
712760
<iconset resource="../../data/Resources.qrc">
713761
<normaloff>:/icons/anchors/SelfVStretch.png</normaloff>:/icons/anchors/SelfVStretch.png</iconset>

0 commit comments

Comments
 (0)