Qt: use the AbstractControl for multiple types of controls (#20417)
* class ClickableLabel
* scrollable widget
* rename to controls.cc
* rename to AbstractControl
* cleanup
* remove useless stylesheets
* change button stylesheet
* cleanup
* better margin
* cleanup
cleanup
* remove bottom line from AbstractControl
* no scrolling for now
* add those back
* style
* clean up
* don't need a function for that
Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
old-commit-hash: 8be2e07f4f
commatwo_master
parent
ed23f0778a
commit
d7cf75fc64
5 changed files with 166 additions and 141 deletions
@ -0,0 +1,36 @@ |
||||
#include "controls.hpp" |
||||
|
||||
QFrame *horizontal_line(QWidget *parent) { |
||||
QFrame *line = new QFrame(parent); |
||||
line->setFrameShape(QFrame::StyledPanel); |
||||
line->setStyleSheet(R"( |
||||
margin-left: 40px; |
||||
margin-right: 40px; |
||||
border-width: 1px; |
||||
border-bottom-style: solid; |
||||
border-color: gray; |
||||
)"); |
||||
line->setFixedHeight(2); |
||||
return line; |
||||
} |
||||
|
||||
AbstractControl::AbstractControl(const QString &title, const QString &desc, const QString &icon) : QFrame() { |
||||
hlayout = new QHBoxLayout; |
||||
hlayout->setSpacing(50); |
||||
|
||||
// left icon
|
||||
if (!icon.isEmpty()) { |
||||
QPixmap pix(icon); |
||||
QLabel *icon = new QLabel(); |
||||
icon->setPixmap(pix.scaledToWidth(80, Qt::SmoothTransformation)); |
||||
icon->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)); |
||||
hlayout->addWidget(icon); |
||||
} |
||||
|
||||
// title
|
||||
title_label = new QLabel(title); |
||||
title_label->setStyleSheet("font-size: 50px; font-weight: 400;"); |
||||
hlayout->addWidget(title_label); |
||||
|
||||
setLayout(hlayout); |
||||
} |
@ -0,0 +1,91 @@ |
||||
#pragma once |
||||
|
||||
#include <QFrame> |
||||
#include <QHBoxLayout> |
||||
#include <QLabel> |
||||
#include <QPushButton> |
||||
#include <QVBoxLayout> |
||||
|
||||
#include "common/params.h" |
||||
#include "toggle.hpp" |
||||
|
||||
QFrame *horizontal_line(QWidget *parent = nullptr); |
||||
|
||||
class AbstractControl : public QFrame { |
||||
Q_OBJECT |
||||
|
||||
protected: |
||||
AbstractControl(const QString &title, const QString &desc = "", const QString &icon = ""); |
||||
|
||||
QSize minimumSizeHint() const override { |
||||
QSize size = QFrame::minimumSizeHint(); |
||||
size.setHeight(120); |
||||
return size; |
||||
}; |
||||
|
||||
QHBoxLayout *hlayout; |
||||
QLabel *title_label; |
||||
}; |
||||
|
||||
class LabelControl : public AbstractControl { |
||||
Q_OBJECT |
||||
|
||||
public: |
||||
LabelControl(const QString &title, const QString &text, const QString &desc = "") : AbstractControl(title, desc, "") { |
||||
label.setText(text); |
||||
label.setAlignment(Qt::AlignRight | Qt::AlignVCenter); |
||||
hlayout->addWidget(&label); |
||||
} |
||||
void setText(const QString &text) { label.setText(text); } |
||||
|
||||
private: |
||||
QLabel label; |
||||
}; |
||||
|
||||
class ButtonControl : public AbstractControl { |
||||
Q_OBJECT |
||||
|
||||
public: |
||||
template <typename Functor> |
||||
ButtonControl(const QString &title, const QString &text, const QString &desc, Functor functor, const QString &icon = "") : AbstractControl(title, desc, icon) { |
||||
btn.setText(text); |
||||
btn.setStyleSheet(R"( |
||||
padding: 0; |
||||
border-radius: 40px; |
||||
font-size: 30px; |
||||
font-weight: 500; |
||||
color: #E4E4E4; |
||||
background-color: #393939; |
||||
)"); |
||||
btn.setFixedSize(200, 80); |
||||
QObject::connect(&btn, &QPushButton::released, functor); |
||||
hlayout->addWidget(&btn); |
||||
} |
||||
void setText(const QString &text) { btn.setText(text); } |
||||
|
||||
private: |
||||
QPushButton btn; |
||||
}; |
||||
|
||||
class ToggleControl : public AbstractControl { |
||||
Q_OBJECT |
||||
|
||||
public: |
||||
ToggleControl(const QString ¶m, const QString &title, const QString &desc, const QString &icon) : AbstractControl(title, desc, icon) { |
||||
toggle.setFixedSize(150, 100); |
||||
// set initial state from param
|
||||
if (Params().read_db_bool(param.toStdString().c_str())) { |
||||
toggle.togglePosition(); |
||||
} |
||||
QObject::connect(&toggle, &Toggle::stateChanged, [=](int state) { |
||||
char value = state ? '1' : '0'; |
||||
Params().write_db_value(param.toStdString().c_str(), &value, 1); |
||||
}); |
||||
hlayout->addWidget(&toggle); |
||||
} |
||||
|
||||
void setEnabled(bool enabled) { toggle.setEnabled(enabled); } |
||||
|
||||
private: |
||||
Toggle toggle; |
||||
}; |
Loading…
Reference in new issue