Skip to content

Commit

Permalink
Fix buttons order in the InsertDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
winseros committed Jan 2, 2024
1 parent 6815c2a commit 89b0061
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 51 deletions.
1 change: 1 addition & 0 deletions pbom/ui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ list(APPEND PROJECT_SOURCES_UI
"ui/insertdialog.cpp"
"ui/insertdialog.ui"
"ui/insertdialogbuttons.cpp"
"ui/insertdialogbuttons.ui"
"ui/renamedialog.cpp"
"ui/renamedialog.ui"
"ui/mainwindow.cpp"
Expand Down
35 changes: 17 additions & 18 deletions pbom/ui/insertdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,7 @@
</widget>
</item>
<item>
<widget class="pboman3::ui::InsertDialogButtons" name="buttons">
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
<widget class="pboman3::ui::InsertDialogButtons" name="buttons" native="true"/>
</item>
</layout>
</widget>
Expand All @@ -120,25 +116,28 @@
</customwidget>
<customwidget>
<class>pboman3::ui::InsertDialogButtons</class>
<extends>QDialogButtonBox</extends>
<extends>QWidget</extends>
<header>ui/insertdialogbuttons.h</header>
<container>1</container>
<slots>
<signal>next()</signal>
<signal>ok()</signal>
<signal>cancel()</signal>
<signal>back()</signal>
<signal>next()</signal>
</slots>
</customwidget>
</customwidgets>
<resources/>
<connections>
<connection>
<sender>buttons</sender>
<signal>accepted()</signal>
<signal>ok()</signal>
<receiver>InsertDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>393</x>
<y>565</y>
<y>572</y>
</hint>
<hint type="destinationlabel">
<x>393</x>
Expand All @@ -148,13 +147,13 @@
</connection>
<connection>
<sender>buttons</sender>
<signal>rejected()</signal>
<signal>back()</signal>
<receiver>InsertDialog</receiver>
<slot>reject()</slot>
<slot>back()</slot>
<hints>
<hint type="sourcelabel">
<x>393</x>
<y>565</y>
<y>572</y>
</hint>
<hint type="destinationlabel">
<x>393</x>
Expand All @@ -164,13 +163,13 @@
</connection>
<connection>
<sender>buttons</sender>
<signal>next()</signal>
<signal>cancel()</signal>
<receiver>InsertDialog</receiver>
<slot>next()</slot>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>393</x>
<y>565</y>
<y>572</y>
</hint>
<hint type="destinationlabel">
<x>393</x>
Expand All @@ -180,13 +179,13 @@
</connection>
<connection>
<sender>buttons</sender>
<signal>back()</signal>
<signal>next()</signal>
<receiver>InsertDialog</receiver>
<slot>back()</slot>
<slot>next()</slot>
<hints>
<hint type="sourcelabel">
<x>393</x>
<y>565</y>
<y>572</y>
</hint>
<hint type="destinationlabel">
<x>393</x>
Expand Down
89 changes: 63 additions & 26 deletions pbom/ui/insertdialogbuttons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,73 @@

namespace pboman3::ui {
InsertDialogButtons::InsertDialogButtons(QWidget* parent)
: QDialogButtonBox(StandardButtons(Ok | Cancel), parent),
btnNext_(nullptr),
btnBack_(nullptr) {
connect(this, &InsertDialogButtons::clicked, this, &InsertDialogButtons::onButtonClicked);
: QWidget(parent),
ui_(new Ui::InsertDialogButtons) {
ui_->setupUi(this);
ui_->btnNext->hide();
ui_->btnBack->hide();

connect(ui_->btnOk, &QAbstractButton::clicked, this, &InsertDialogButtons::onOkClick);
connect(ui_->btnCancel, &QAbstractButton::clicked, this, &InsertDialogButtons::onCancelClick);
connect(ui_->btnNext, &QAbstractButton::clicked, this, &InsertDialogButtons::onNextClick);
connect(ui_->btnBack, &QAbstractButton::clicked, this, &InsertDialogButtons::onBackClick);
}

InsertDialogButtons::~InsertDialogButtons() {
delete ui_;
}

void InsertDialogButtons::setIsTwoStep() {
void InsertDialogButtons::setIsTwoStep() const {
LOG(info, "Set the buttons panel for a 2-step dialog")
btnNext_ = addButton("Next", ActionRole);
btnNext_->setFocus();
btnBack_ = addButton("Back", ActionRole);
button(Ok)->hide();
btnBack_->hide();

ui_->btnOk->setAutoDefault(false);
ui_->btnOk->setDefault(false);
ui_->btnOk->hide();

ui_->btnNext->show();
ui_->btnNext->setFocus();
ui_->btnNext->setDefault(true);
ui_->btnNext->setAutoDefault(true);
}

void InsertDialogButtons::onNextClick() {
LOG(info, "User clicked the Next button")
ui_->btnNext->setAutoDefault(false);
ui_->btnNext->setDefault(false);
ui_->btnNext->hide();

ui_->btnOk->show();
ui_->btnOk->setFocus();
ui_->btnOk->setDefault(true);
ui_->btnOk->setAutoDefault(true);

ui_->btnBack->show();
emit next();
}

void InsertDialogButtons::onBackClick() {
LOG(info, "User clicked the Back button")

ui_->btnOk->setAutoDefault(false);
ui_->btnOk->setDefault(false);
ui_->btnOk->hide();

ui_->btnNext->show();
ui_->btnNext->setFocus();
ui_->btnNext->setDefault(true);
ui_->btnNext->setAutoDefault(true);

ui_->btnBack->hide();
emit back();
}

void InsertDialogButtons::onOkClick() {
LOG(info, "User clicked the Ok button")
emit ok();
}

void InsertDialogButtons::onButtonClicked(const QAbstractButton* btn) {
if (btn == btnNext_) {
LOG(info, "User clicked the Next button")
button(Ok)->show();
button(Ok)->setFocus();
btnNext_->hide();
btnBack_->show();
emit next();
} else if (btn == btnBack_) {
LOG(info, "User clicked the Back button")
button(Ok)->hide();
btnNext_->show();
btnNext_->setFocus();
btnBack_->hide();
emit back();
}
void InsertDialogButtons::onCancelClick() {
LOG(info, "User clicked the Cancel button")
emit cancel();
}
}
26 changes: 19 additions & 7 deletions pbom/ui/insertdialogbuttons.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
#pragma once

#include <QDialogButtonBox>
#include <QWidget>
#include "ui_insertdialogbuttons.h"

namespace pboman3::ui {
class InsertDialogButtons : public QDialogButtonBox {
Q_OBJECT
class InsertDialogButtons : public QWidget {
Q_OBJECT

public:
InsertDialogButtons(QWidget* parent = nullptr);

void setIsTwoStep();
~InsertDialogButtons() override;

void setIsTwoStep() const;

signals:
void next();

void back();

void ok();

void cancel();

private:
QPushButton* btnNext_;
QPushButton* btnBack_;
Ui::InsertDialogButtons* ui_;

void onNextClick();

void onBackClick();

void onOkClick();

void onButtonClicked(const QAbstractButton* btn);
void onCancelClick();
};
}
77 changes: 77 additions & 0 deletions pbom/ui/insertdialogbuttons.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>InsertDialogButtons</class>
<widget class="QWidget" name="InsertDialogButtons">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>538</width>
<height>42</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="btnBack">
<property name="text">
<string>Back</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnNext">
<property name="text">
<string>Next</string>
</property>
<property name="shortcut">
<string>Return</string>
</property>
<property name="default">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnOk">
<property name="text">
<string>Ok</string>
</property>
<property name="shortcut">
<string>Return</string>
</property>
<property name="autoDefault">
<bool>true</bool>
</property>
<property name="default">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnCancel">
<property name="text">
<string>Cancel</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

0 comments on commit 89b0061

Please sign in to comment.