@@ -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+ }
0 commit comments