Update collapsible icon styling to use native buttons for the header/title

This commit is contained in:
Dan Paulat 2023-06-28 21:44:47 -05:00
parent 6d9d923081
commit 6a92e7601e
2 changed files with 30 additions and 47 deletions

View file

@ -17,7 +17,17 @@ public:
void Initialize(); void Initialize();
void SetExpanded(bool expanded); void SetExpanded(bool expanded);
const QIcon kCollapsedIcon_ {
":/res/icons/font-awesome-6/square-caret-right-regular.svg"};
const QIcon kExpandedIcon_ {
":/res/icons/font-awesome-6/square-caret-down-regular.svg"};
const std::map<bool, const QIcon&> kIcon_ {{false, kCollapsedIcon_},
{true, kExpandedIcon_}};
CollapsibleGroup* self_; CollapsibleGroup* self_;
bool expanded_ {true};
}; };
CollapsibleGroup::CollapsibleGroup(QWidget* parent) : CollapsibleGroup::CollapsibleGroup(QWidget* parent) :
@ -48,9 +58,9 @@ void CollapsibleGroupImpl::Initialize()
{ {
QObject::connect( QObject::connect(
self_->ui->titleButton, self_->ui->titleButton,
&QAbstractButton::toggled, &QAbstractButton::clicked,
self_, self_,
[this](bool checked) { SetExpanded(checked); }, [this]() { SetExpanded(!expanded_); },
Qt::DirectConnection); Qt::DirectConnection);
self_->Expand(); self_->Expand();
@ -74,25 +84,25 @@ void CollapsibleGroup::SetTitle(const QString& title)
void CollapsibleGroup::Collapse() void CollapsibleGroup::Collapse()
{ {
// Update the title frame // Update the title frame
if (ui->titleButton->isChecked()) p->SetExpanded(false);
{
ui->titleButton->setChecked(false);
}
} }
void CollapsibleGroup::Expand() void CollapsibleGroup::Expand()
{ {
// Update the title frame // Update the title frame
if (!ui->titleButton->isChecked()) p->SetExpanded(true);
{
ui->titleButton->setChecked(true);
}
} }
void CollapsibleGroupImpl::SetExpanded(bool expanded) void CollapsibleGroupImpl::SetExpanded(bool expanded)
{ {
// Update icon
self_->ui->titleButton->setIcon(kIcon_.at(expanded));
// Update contents visibility // Update contents visibility
self_->ui->contentsFrame->setVisible(expanded); self_->ui->contentsFrame->setVisible(expanded);
// Update internal state
expanded_ = expanded;
} }
} // namespace ui } // namespace ui

View file

@ -30,45 +30,18 @@
<number>0</number> <number>0</number>
</property> </property>
<item> <item>
<widget class="QFrame" name="titleFrame"> <widget class="QPushButton" name="titleButton">
<property name="frameShape"> <property name="styleSheet">
<enum>QFrame::Box</enum> <string notr="true">text-align: left;</string>
</property> </property>
<property name="frameShadow"> <property name="text">
<enum>QFrame::Sunken</enum> <string>Title</string>
</property>
<property name="icon">
<iconset resource="../../../../scwx-qt.qrc">
<normaloff>:/res/icons/font-awesome-6/square-caret-right-regular.svg</normaloff>
<normalon>:/res/icons/font-awesome-6/square-caret-down-regular.svg</normalon>:/res/icons/font-awesome-6/square-caret-right-regular.svg</iconset>
</property> </property>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="leftMargin">
<number>1</number>
</property>
<property name="topMargin">
<number>1</number>
</property>
<property name="rightMargin">
<number>1</number>
</property>
<property name="bottomMargin">
<number>1</number>
</property>
<item>
<widget class="QPushButton" name="titleButton">
<property name="styleSheet">
<string notr="true">background: transparent; border: none; color: palette(window-text); text-align: left;</string>
</property>
<property name="text">
<string>Title</string>
</property>
<property name="icon">
<iconset resource="../../../../scwx-qt.qrc">
<normaloff>:/res/icons/font-awesome-6/square-caret-right-regular.svg</normaloff>
<normalon>:/res/icons/font-awesome-6/square-caret-down-regular.svg</normalon>:/res/icons/font-awesome-6/square-caret-right-regular.svg</iconset>
</property>
<property name="checkable">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget> </widget>
</item> </item>
<item> <item>