Mercurial > minori
diff src/gui/dialog/information.cpp @ 68:2417121d894e
*: normalize usage of layouts
before, I used them two ways, once was by setting the layout later
by using setLayout(QWidget), and the other was just using the constructor.
I find the constructor to be easier to read, so I chose that one.
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Mon, 02 Oct 2023 21:33:25 -0400 |
parents | 6481c5aed3e1 |
children | 27a19dd6cba1 |
line wrap: on
line diff
--- a/src/gui/dialog/information.cpp Mon Oct 02 07:06:44 2023 -0400 +++ b/src/gui/dialog/information.cpp Mon Oct 02 21:33:25 2023 -0400 @@ -57,7 +57,6 @@ sidebar_layout->addWidget(poster); sidebar_layout->setContentsMargins(0, 0, 0, 0); sidebar_layout->addStretch(); - sidebar->setFixedWidth(175); /* main widget */ QWidget* main_widget = new QWidget(widget); @@ -83,18 +82,17 @@ AnimeInfoWidget* main_information_widget = new AnimeInfoWidget(anime, tabbed_widget); QWidget* settings_widget = new QWidget(tabbed_widget); - settings_widget->setLayout(new QVBoxLayout); settings_widget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); - settings_widget->layout()->addWidget(new TextWidgets::Header(tr("Anime list"), settings_widget)); + QVBoxLayout* settings_layout = new QVBoxLayout(settings_widget); + settings_layout->addWidget(new TextWidgets::Header(tr("Anime list"), settings_widget)); QWidget* sg_anime_list_content = new QWidget(settings_widget); - settings_widget->layout()->addWidget(sg_anime_list_content); - sg_anime_list_content->setLayout(new QVBoxLayout); - sg_anime_list_content->layout()->setSpacing(5); - sg_anime_list_content->layout()->setContentsMargins(12, 0, 0, 0); -/* these macros make this a lot easier to edit */ + QVBoxLayout* al_layout = new QVBoxLayout(sg_anime_list_content); + al_layout->setSpacing(5); + al_layout->setContentsMargins(12, 0, 0, 0); + #define LAYOUT_HORIZ_SPACING 25 #define LAYOUT_VERT_SPACING 5 #define LAYOUT_ITEM_WIDTH 175 @@ -102,17 +100,14 @@ #define CREATE_FULL_WIDTH_SUBSECTION(x) \ { \ QWidget* subsection = new QWidget(section); \ - subsection->setLayout(new QVBoxLayout); \ subsection->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); \ - subsection->layout()->setSpacing(LAYOUT_VERT_SPACING); \ - subsection->layout()->setContentsMargins(0, 0, 0, 0); \ + QVBoxLayout* subsection_layout = new QVBoxLayout(subsection); \ + subsection_layout->setSpacing(LAYOUT_VERT_SPACING); \ + subsection_layout->setContentsMargins(0, 0, 0, 0); \ x; \ layout->addWidget(subsection); \ } -/* Creates a subsection with a width of 175 */ -#define CREATE_SUBSECTION(x) CREATE_FULL_WIDTH_SUBSECTION(x subsection->setFixedWidth(LAYOUT_ITEM_WIDTH);) - /* Creates a section in the parent `a` */ #define CREATE_FULL_WIDTH_SECTION(a, x) \ { \ @@ -124,35 +119,37 @@ a->layout()->addWidget(section); \ } +/* Creates a subsection with a width of 175 */ +#define CREATE_SUBSECTION(x) CREATE_FULL_WIDTH_SUBSECTION(x subsection->setFixedWidth(LAYOUT_ITEM_WIDTH);) /* Creates a section in the parent `a` */ #define CREATE_SECTION(a, x) CREATE_FULL_WIDTH_SECTION(a, x layout->addStretch();) CREATE_SECTION(sg_anime_list_content, { /* Episodes watched section */ CREATE_SUBSECTION({ - subsection->layout()->addWidget(new QLabel(tr("Episodes watched:"), subsection)); + subsection_layout->addWidget(new QLabel(tr("Episodes watched:"), subsection)); QSpinBox* spin_box = new QSpinBox(subsection); connect(spin_box, QOverload<int>::of(&QSpinBox::valueChanged), this, [this](int i) { progress = i; }); spin_box->setRange(0, anime.GetEpisodes()); spin_box->setSingleStep(1); spin_box->setValue(progress = anime.GetUserProgress()); - subsection->layout()->addWidget(spin_box); + subsection_layout->addWidget(spin_box); }); CREATE_SUBSECTION({ - subsection->layout()->addWidget(new QLabel(tr(" "), subsection)); + subsection_layout->addWidget(new QLabel(tr(" "), subsection)); QCheckBox* checkbox = new QCheckBox(tr("Rewatching")); connect(checkbox, QOverload<int>::of(&QCheckBox::stateChanged), this, [this](int state) { rewatching = (state == Qt::Checked); }); checkbox->setCheckState(anime.GetUserIsRewatching() ? Qt::Checked : Qt::Unchecked); - subsection->layout()->addWidget(checkbox); + subsection_layout->addWidget(checkbox); }); }); CREATE_SECTION(sg_anime_list_content, { /* Status & score section */ CREATE_SUBSECTION({ - subsection->layout()->addWidget(new QLabel(tr("Status:"), subsection)); + subsection_layout->addWidget(new QLabel(tr("Status:"), subsection)); QStringList string_list; for (unsigned int i = 0; i < ARRAYSIZE(Anime::ListStatuses); i++) @@ -163,23 +160,23 @@ connect(combo_box, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [this](int i) { status = Anime::ListStatuses[i]; }); combo_box->setCurrentIndex(static_cast<int>(status = anime.GetUserStatus()) - 1); - subsection->layout()->addWidget(combo_box); + subsection_layout->addWidget(combo_box); }); CREATE_SUBSECTION({ - subsection->layout()->addWidget(new QLabel(tr("Score:"), subsection)); + subsection_layout->addWidget(new QLabel(tr("Score:"), subsection)); QSpinBox* spin_box = new QSpinBox(subsection); connect(spin_box, QOverload<int>::of(&QSpinBox::valueChanged), this, [this](int i) { score = i; }); spin_box->setRange(0, 100); spin_box->setSingleStep(5); spin_box->setValue(score = anime.GetUserScore()); - subsection->layout()->addWidget(spin_box); + subsection_layout->addWidget(spin_box); }); }); CREATE_FULL_WIDTH_SECTION(sg_anime_list_content, { /* Notes section */ CREATE_FULL_WIDTH_SUBSECTION({ - subsection->layout()->addWidget(new QLabel(tr("Notes:"), subsection)); + subsection_layout->addWidget(new QLabel(tr("Notes:"), subsection)); QLineEdit* line_edit = new QLineEdit(subsection); connect(line_edit, &QLineEdit::textChanged, this, [this](const QString& text) { @@ -188,13 +185,13 @@ }); line_edit->setText(Strings::ToQString(notes = anime.GetUserNotes())); line_edit->setPlaceholderText(tr("Enter your notes about this anime")); - subsection->layout()->addWidget(line_edit); + subsection_layout->addWidget(line_edit); }); }); CREATE_SECTION(sg_anime_list_content, { /* Dates section */ CREATE_SUBSECTION({ - subsection->layout()->addWidget(new QLabel(tr("Date started:"), subsection)); + subsection_layout->addWidget(new QLabel(tr("Date started:"), subsection)); OptionalDate* date = new OptionalDate(true, subsection); connect(date, &OptionalDate::DataChanged, this, @@ -205,10 +202,10 @@ started = anime.GetAirDate(); } date->SetDate(started); - subsection->layout()->addWidget(date); + subsection_layout->addWidget(date); }); CREATE_SUBSECTION({ - subsection->layout()->addWidget(new QLabel(tr("Date completed:"), subsection)); + subsection_layout->addWidget(new QLabel(tr("Date completed:"), subsection)); OptionalDate* date = new OptionalDate(true, subsection); connect(date, &OptionalDate::DataChanged, this, @@ -219,30 +216,31 @@ completed = anime.GetAirDate(); } date->SetDate(completed); - subsection->layout()->addWidget(date); + subsection_layout->addWidget(date); }); }); - settings_widget->layout()->addWidget(new TextWidgets::Header(tr("Local settings"), settings_widget)); + settings_layout->addWidget(sg_anime_list_content); + + settings_layout->addWidget(new TextWidgets::Header(tr("Local settings"), settings_widget)); QWidget* sg_local_content = new QWidget(settings_widget); - settings_widget->layout()->addWidget(sg_local_content); - sg_local_content->setLayout(new QVBoxLayout); - sg_local_content->layout()->setSpacing(5); - sg_local_content->layout()->setContentsMargins(12, 0, 0, 0); + QVBoxLayout* sg_local_layout = new QVBoxLayout(sg_local_content); + sg_local_layout->setSpacing(5); + sg_local_layout->setContentsMargins(12, 0, 0, 0); CREATE_FULL_WIDTH_SECTION(sg_local_content, { /* Alternative titles */ CREATE_FULL_WIDTH_SUBSECTION({ - subsection->layout()->addWidget(new QLabel(tr("Alternative titles:"), subsection)); + subsection_layout->addWidget(new QLabel(tr("Alternative titles:"), subsection)); QLineEdit* line_edit = new QLineEdit(Strings::ToQString(anime.GetUserNotes()), subsection); line_edit->setPlaceholderText( tr("Enter alternative titles here, separated by a semicolon (i.e. Title 1; Title 2)")); - subsection->layout()->addWidget(line_edit); + subsection_layout->addWidget(line_edit); QCheckBox* checkbox = new QCheckBox(tr("Use the first alternative title to search for torrents")); - subsection->layout()->addWidget(checkbox); + subsection_layout->addWidget(checkbox); }); }); #undef CREATE_SECTION @@ -250,21 +248,21 @@ #undef CREATE_FULL_WIDTH_SECTION #undef CREATE_FULL_WIDTH_SUBSECTION - reinterpret_cast<QBoxLayout*>(settings_widget->layout())->addStretch(); + settings_layout->addWidget(sg_local_content); + settings_layout->addStretch(); tabbed_widget->addTab(main_information_widget, tr("Main information")); tabbed_widget->addTab(settings_widget, tr("My list and settings")); - QVBoxLayout* main_layout = new QVBoxLayout; + QVBoxLayout* main_layout = new QVBoxLayout(main_widget); main_layout->addWidget(anime_title); main_layout->addWidget(tabbed_widget); main_layout->setContentsMargins(0, 0, 0, 0); - main_widget->setLayout(main_layout); - QHBoxLayout* layout = new QHBoxLayout; + QHBoxLayout* layout = new QHBoxLayout(widget); layout->addWidget(sidebar); layout->addWidget(main_widget); - widget->setLayout(layout); + layout->setSpacing(12); QDialogButtonBox* button_box = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this); connect(button_box, &QDialogButtonBox::accepted, this, [this, accept] { @@ -274,10 +272,9 @@ }); connect(button_box, &QDialogButtonBox::rejected, this, &QDialog::reject); - QVBoxLayout* buttons_layout = new QVBoxLayout; + QVBoxLayout* buttons_layout = new QVBoxLayout(this); buttons_layout->addWidget(widget); buttons_layout->addWidget(button_box, 0, Qt::AlignBottom); - setLayout(buttons_layout); } #include "gui/dialog/moc_information.cpp"