Mercurial > minori
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 67:442065432549 | 68:2417121d894e |
|---|---|
| 55 QVBoxLayout* sidebar_layout = new QVBoxLayout(sidebar); | 55 QVBoxLayout* sidebar_layout = new QVBoxLayout(sidebar); |
| 56 Poster* poster = new Poster(anime.GetId(), sidebar); | 56 Poster* poster = new Poster(anime.GetId(), sidebar); |
| 57 sidebar_layout->addWidget(poster); | 57 sidebar_layout->addWidget(poster); |
| 58 sidebar_layout->setContentsMargins(0, 0, 0, 0); | 58 sidebar_layout->setContentsMargins(0, 0, 0, 0); |
| 59 sidebar_layout->addStretch(); | 59 sidebar_layout->addStretch(); |
| 60 sidebar->setFixedWidth(175); | |
| 61 | 60 |
| 62 /* main widget */ | 61 /* main widget */ |
| 63 QWidget* main_widget = new QWidget(widget); | 62 QWidget* main_widget = new QWidget(widget); |
| 64 | 63 |
| 65 { | 64 { |
| 81 | 80 |
| 82 /* main info tab */ | 81 /* main info tab */ |
| 83 AnimeInfoWidget* main_information_widget = new AnimeInfoWidget(anime, tabbed_widget); | 82 AnimeInfoWidget* main_information_widget = new AnimeInfoWidget(anime, tabbed_widget); |
| 84 | 83 |
| 85 QWidget* settings_widget = new QWidget(tabbed_widget); | 84 QWidget* settings_widget = new QWidget(tabbed_widget); |
| 86 settings_widget->setLayout(new QVBoxLayout); | |
| 87 settings_widget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); | 85 settings_widget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); |
| 88 | 86 |
| 89 settings_widget->layout()->addWidget(new TextWidgets::Header(tr("Anime list"), settings_widget)); | 87 QVBoxLayout* settings_layout = new QVBoxLayout(settings_widget); |
| 88 settings_layout->addWidget(new TextWidgets::Header(tr("Anime list"), settings_widget)); | |
| 90 | 89 |
| 91 QWidget* sg_anime_list_content = new QWidget(settings_widget); | 90 QWidget* sg_anime_list_content = new QWidget(settings_widget); |
| 92 settings_widget->layout()->addWidget(sg_anime_list_content); | 91 |
| 93 sg_anime_list_content->setLayout(new QVBoxLayout); | 92 QVBoxLayout* al_layout = new QVBoxLayout(sg_anime_list_content); |
| 94 sg_anime_list_content->layout()->setSpacing(5); | 93 al_layout->setSpacing(5); |
| 95 sg_anime_list_content->layout()->setContentsMargins(12, 0, 0, 0); | 94 al_layout->setContentsMargins(12, 0, 0, 0); |
| 96 | 95 |
| 97 /* these macros make this a lot easier to edit */ | |
| 98 #define LAYOUT_HORIZ_SPACING 25 | 96 #define LAYOUT_HORIZ_SPACING 25 |
| 99 #define LAYOUT_VERT_SPACING 5 | 97 #define LAYOUT_VERT_SPACING 5 |
| 100 #define LAYOUT_ITEM_WIDTH 175 | 98 #define LAYOUT_ITEM_WIDTH 175 |
| 101 /* Creates a subsection that takes up whatever space is necessary */ | 99 /* Creates a subsection that takes up whatever space is necessary */ |
| 102 #define CREATE_FULL_WIDTH_SUBSECTION(x) \ | 100 #define CREATE_FULL_WIDTH_SUBSECTION(x) \ |
| 103 { \ | 101 { \ |
| 104 QWidget* subsection = new QWidget(section); \ | 102 QWidget* subsection = new QWidget(section); \ |
| 105 subsection->setLayout(new QVBoxLayout); \ | |
| 106 subsection->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); \ | 103 subsection->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); \ |
| 107 subsection->layout()->setSpacing(LAYOUT_VERT_SPACING); \ | 104 QVBoxLayout* subsection_layout = new QVBoxLayout(subsection); \ |
| 108 subsection->layout()->setContentsMargins(0, 0, 0, 0); \ | 105 subsection_layout->setSpacing(LAYOUT_VERT_SPACING); \ |
| 106 subsection_layout->setContentsMargins(0, 0, 0, 0); \ | |
| 109 x; \ | 107 x; \ |
| 110 layout->addWidget(subsection); \ | 108 layout->addWidget(subsection); \ |
| 111 } | 109 } |
| 112 | |
| 113 /* Creates a subsection with a width of 175 */ | |
| 114 #define CREATE_SUBSECTION(x) CREATE_FULL_WIDTH_SUBSECTION(x subsection->setFixedWidth(LAYOUT_ITEM_WIDTH);) | |
| 115 | 110 |
| 116 /* Creates a section in the parent `a` */ | 111 /* Creates a section in the parent `a` */ |
| 117 #define CREATE_FULL_WIDTH_SECTION(a, x) \ | 112 #define CREATE_FULL_WIDTH_SECTION(a, x) \ |
| 118 { \ | 113 { \ |
| 119 QWidget* section = new QWidget(a); \ | 114 QWidget* section = new QWidget(a); \ |
| 122 layout->setContentsMargins(0, 0, 0, 0); \ | 117 layout->setContentsMargins(0, 0, 0, 0); \ |
| 123 x; \ | 118 x; \ |
| 124 a->layout()->addWidget(section); \ | 119 a->layout()->addWidget(section); \ |
| 125 } | 120 } |
| 126 | 121 |
| 122 /* Creates a subsection with a width of 175 */ | |
| 123 #define CREATE_SUBSECTION(x) CREATE_FULL_WIDTH_SUBSECTION(x subsection->setFixedWidth(LAYOUT_ITEM_WIDTH);) | |
| 127 /* Creates a section in the parent `a` */ | 124 /* Creates a section in the parent `a` */ |
| 128 #define CREATE_SECTION(a, x) CREATE_FULL_WIDTH_SECTION(a, x layout->addStretch();) | 125 #define CREATE_SECTION(a, x) CREATE_FULL_WIDTH_SECTION(a, x layout->addStretch();) |
| 129 | 126 |
| 130 CREATE_SECTION(sg_anime_list_content, { | 127 CREATE_SECTION(sg_anime_list_content, { |
| 131 /* Episodes watched section */ | 128 /* Episodes watched section */ |
| 132 CREATE_SUBSECTION({ | 129 CREATE_SUBSECTION({ |
| 133 subsection->layout()->addWidget(new QLabel(tr("Episodes watched:"), subsection)); | 130 subsection_layout->addWidget(new QLabel(tr("Episodes watched:"), subsection)); |
| 134 | 131 |
| 135 QSpinBox* spin_box = new QSpinBox(subsection); | 132 QSpinBox* spin_box = new QSpinBox(subsection); |
| 136 connect(spin_box, QOverload<int>::of(&QSpinBox::valueChanged), this, [this](int i) { progress = i; }); | 133 connect(spin_box, QOverload<int>::of(&QSpinBox::valueChanged), this, [this](int i) { progress = i; }); |
| 137 spin_box->setRange(0, anime.GetEpisodes()); | 134 spin_box->setRange(0, anime.GetEpisodes()); |
| 138 spin_box->setSingleStep(1); | 135 spin_box->setSingleStep(1); |
| 139 spin_box->setValue(progress = anime.GetUserProgress()); | 136 spin_box->setValue(progress = anime.GetUserProgress()); |
| 140 subsection->layout()->addWidget(spin_box); | 137 subsection_layout->addWidget(spin_box); |
| 141 }); | 138 }); |
| 142 CREATE_SUBSECTION({ | 139 CREATE_SUBSECTION({ |
| 143 subsection->layout()->addWidget(new QLabel(tr(" "), subsection)); | 140 subsection_layout->addWidget(new QLabel(tr(" "), subsection)); |
| 144 | 141 |
| 145 QCheckBox* checkbox = new QCheckBox(tr("Rewatching")); | 142 QCheckBox* checkbox = new QCheckBox(tr("Rewatching")); |
| 146 connect(checkbox, QOverload<int>::of(&QCheckBox::stateChanged), this, | 143 connect(checkbox, QOverload<int>::of(&QCheckBox::stateChanged), this, |
| 147 [this](int state) { rewatching = (state == Qt::Checked); }); | 144 [this](int state) { rewatching = (state == Qt::Checked); }); |
| 148 checkbox->setCheckState(anime.GetUserIsRewatching() ? Qt::Checked : Qt::Unchecked); | 145 checkbox->setCheckState(anime.GetUserIsRewatching() ? Qt::Checked : Qt::Unchecked); |
| 149 subsection->layout()->addWidget(checkbox); | 146 subsection_layout->addWidget(checkbox); |
| 150 }); | 147 }); |
| 151 }); | 148 }); |
| 152 CREATE_SECTION(sg_anime_list_content, { | 149 CREATE_SECTION(sg_anime_list_content, { |
| 153 /* Status & score section */ | 150 /* Status & score section */ |
| 154 CREATE_SUBSECTION({ | 151 CREATE_SUBSECTION({ |
| 155 subsection->layout()->addWidget(new QLabel(tr("Status:"), subsection)); | 152 subsection_layout->addWidget(new QLabel(tr("Status:"), subsection)); |
| 156 | 153 |
| 157 QStringList string_list; | 154 QStringList string_list; |
| 158 for (unsigned int i = 0; i < ARRAYSIZE(Anime::ListStatuses); i++) | 155 for (unsigned int i = 0; i < ARRAYSIZE(Anime::ListStatuses); i++) |
| 159 string_list.append(Strings::ToQString(Translate::ToString(Anime::ListStatuses[i]))); | 156 string_list.append(Strings::ToQString(Translate::ToString(Anime::ListStatuses[i]))); |
| 160 | 157 |
| 161 QComboBox* combo_box = new QComboBox(subsection); | 158 QComboBox* combo_box = new QComboBox(subsection); |
| 162 combo_box->addItems(string_list); | 159 combo_box->addItems(string_list); |
| 163 connect(combo_box, QOverload<int>::of(&QComboBox::currentIndexChanged), this, | 160 connect(combo_box, QOverload<int>::of(&QComboBox::currentIndexChanged), this, |
| 164 [this](int i) { status = Anime::ListStatuses[i]; }); | 161 [this](int i) { status = Anime::ListStatuses[i]; }); |
| 165 combo_box->setCurrentIndex(static_cast<int>(status = anime.GetUserStatus()) - 1); | 162 combo_box->setCurrentIndex(static_cast<int>(status = anime.GetUserStatus()) - 1); |
| 166 subsection->layout()->addWidget(combo_box); | 163 subsection_layout->addWidget(combo_box); |
| 167 }); | 164 }); |
| 168 CREATE_SUBSECTION({ | 165 CREATE_SUBSECTION({ |
| 169 subsection->layout()->addWidget(new QLabel(tr("Score:"), subsection)); | 166 subsection_layout->addWidget(new QLabel(tr("Score:"), subsection)); |
| 170 | 167 |
| 171 QSpinBox* spin_box = new QSpinBox(subsection); | 168 QSpinBox* spin_box = new QSpinBox(subsection); |
| 172 connect(spin_box, QOverload<int>::of(&QSpinBox::valueChanged), this, [this](int i) { score = i; }); | 169 connect(spin_box, QOverload<int>::of(&QSpinBox::valueChanged), this, [this](int i) { score = i; }); |
| 173 spin_box->setRange(0, 100); | 170 spin_box->setRange(0, 100); |
| 174 spin_box->setSingleStep(5); | 171 spin_box->setSingleStep(5); |
| 175 spin_box->setValue(score = anime.GetUserScore()); | 172 spin_box->setValue(score = anime.GetUserScore()); |
| 176 subsection->layout()->addWidget(spin_box); | 173 subsection_layout->addWidget(spin_box); |
| 177 }); | 174 }); |
| 178 }); | 175 }); |
| 179 CREATE_FULL_WIDTH_SECTION(sg_anime_list_content, { | 176 CREATE_FULL_WIDTH_SECTION(sg_anime_list_content, { |
| 180 /* Notes section */ | 177 /* Notes section */ |
| 181 CREATE_FULL_WIDTH_SUBSECTION({ | 178 CREATE_FULL_WIDTH_SUBSECTION({ |
| 182 subsection->layout()->addWidget(new QLabel(tr("Notes:"), subsection)); | 179 subsection_layout->addWidget(new QLabel(tr("Notes:"), subsection)); |
| 183 | 180 |
| 184 QLineEdit* line_edit = new QLineEdit(subsection); | 181 QLineEdit* line_edit = new QLineEdit(subsection); |
| 185 connect(line_edit, &QLineEdit::textChanged, this, [this](const QString& text) { | 182 connect(line_edit, &QLineEdit::textChanged, this, [this](const QString& text) { |
| 186 /* this sucks but I don't really want to implement anything smarter :) */ | 183 /* this sucks but I don't really want to implement anything smarter :) */ |
| 187 notes = Strings::ToUtf8String(text); | 184 notes = Strings::ToUtf8String(text); |
| 188 }); | 185 }); |
| 189 line_edit->setText(Strings::ToQString(notes = anime.GetUserNotes())); | 186 line_edit->setText(Strings::ToQString(notes = anime.GetUserNotes())); |
| 190 line_edit->setPlaceholderText(tr("Enter your notes about this anime")); | 187 line_edit->setPlaceholderText(tr("Enter your notes about this anime")); |
| 191 subsection->layout()->addWidget(line_edit); | 188 subsection_layout->addWidget(line_edit); |
| 192 }); | 189 }); |
| 193 }); | 190 }); |
| 194 CREATE_SECTION(sg_anime_list_content, { | 191 CREATE_SECTION(sg_anime_list_content, { |
| 195 /* Dates section */ | 192 /* Dates section */ |
| 196 CREATE_SUBSECTION({ | 193 CREATE_SUBSECTION({ |
| 197 subsection->layout()->addWidget(new QLabel(tr("Date started:"), subsection)); | 194 subsection_layout->addWidget(new QLabel(tr("Date started:"), subsection)); |
| 198 | 195 |
| 199 OptionalDate* date = new OptionalDate(true, subsection); | 196 OptionalDate* date = new OptionalDate(true, subsection); |
| 200 connect(date, &OptionalDate::DataChanged, this, | 197 connect(date, &OptionalDate::DataChanged, this, |
| 201 [this](bool enabled, Date date) { started = (enabled) ? date : Date(); }); | 198 [this](bool enabled, Date date) { started = (enabled) ? date : Date(); }); |
| 202 started = anime.GetUserDateStarted(); | 199 started = anime.GetUserDateStarted(); |
| 203 if (!started.IsValid()) { | 200 if (!started.IsValid()) { |
| 204 date->SetEnabled(false); | 201 date->SetEnabled(false); |
| 205 started = anime.GetAirDate(); | 202 started = anime.GetAirDate(); |
| 206 } | 203 } |
| 207 date->SetDate(started); | 204 date->SetDate(started); |
| 208 subsection->layout()->addWidget(date); | 205 subsection_layout->addWidget(date); |
| 209 }); | 206 }); |
| 210 CREATE_SUBSECTION({ | 207 CREATE_SUBSECTION({ |
| 211 subsection->layout()->addWidget(new QLabel(tr("Date completed:"), subsection)); | 208 subsection_layout->addWidget(new QLabel(tr("Date completed:"), subsection)); |
| 212 | 209 |
| 213 OptionalDate* date = new OptionalDate(true, subsection); | 210 OptionalDate* date = new OptionalDate(true, subsection); |
| 214 connect(date, &OptionalDate::DataChanged, this, | 211 connect(date, &OptionalDate::DataChanged, this, |
| 215 [this](bool enabled, Date date) { completed = (enabled) ? date : Date(); }); | 212 [this](bool enabled, Date date) { completed = (enabled) ? date : Date(); }); |
| 216 completed = anime.GetUserDateCompleted(); | 213 completed = anime.GetUserDateCompleted(); |
| 217 if (!completed.IsValid()) { | 214 if (!completed.IsValid()) { |
| 218 date->SetEnabled(false); | 215 date->SetEnabled(false); |
| 219 completed = anime.GetAirDate(); | 216 completed = anime.GetAirDate(); |
| 220 } | 217 } |
| 221 date->SetDate(completed); | 218 date->SetDate(completed); |
| 222 subsection->layout()->addWidget(date); | 219 subsection_layout->addWidget(date); |
| 223 }); | 220 }); |
| 224 }); | 221 }); |
| 225 | 222 |
| 226 settings_widget->layout()->addWidget(new TextWidgets::Header(tr("Local settings"), settings_widget)); | 223 settings_layout->addWidget(sg_anime_list_content); |
| 224 | |
| 225 settings_layout->addWidget(new TextWidgets::Header(tr("Local settings"), settings_widget)); | |
| 227 | 226 |
| 228 QWidget* sg_local_content = new QWidget(settings_widget); | 227 QWidget* sg_local_content = new QWidget(settings_widget); |
| 229 settings_widget->layout()->addWidget(sg_local_content); | 228 QVBoxLayout* sg_local_layout = new QVBoxLayout(sg_local_content); |
| 230 sg_local_content->setLayout(new QVBoxLayout); | 229 sg_local_layout->setSpacing(5); |
| 231 sg_local_content->layout()->setSpacing(5); | 230 sg_local_layout->setContentsMargins(12, 0, 0, 0); |
| 232 sg_local_content->layout()->setContentsMargins(12, 0, 0, 0); | |
| 233 | 231 |
| 234 CREATE_FULL_WIDTH_SECTION(sg_local_content, { | 232 CREATE_FULL_WIDTH_SECTION(sg_local_content, { |
| 235 /* Alternative titles */ | 233 /* Alternative titles */ |
| 236 CREATE_FULL_WIDTH_SUBSECTION({ | 234 CREATE_FULL_WIDTH_SUBSECTION({ |
| 237 subsection->layout()->addWidget(new QLabel(tr("Alternative titles:"), subsection)); | 235 subsection_layout->addWidget(new QLabel(tr("Alternative titles:"), subsection)); |
| 238 | 236 |
| 239 QLineEdit* line_edit = new QLineEdit(Strings::ToQString(anime.GetUserNotes()), subsection); | 237 QLineEdit* line_edit = new QLineEdit(Strings::ToQString(anime.GetUserNotes()), subsection); |
| 240 line_edit->setPlaceholderText( | 238 line_edit->setPlaceholderText( |
| 241 tr("Enter alternative titles here, separated by a semicolon (i.e. Title 1; Title 2)")); | 239 tr("Enter alternative titles here, separated by a semicolon (i.e. Title 1; Title 2)")); |
| 242 subsection->layout()->addWidget(line_edit); | 240 subsection_layout->addWidget(line_edit); |
| 243 | 241 |
| 244 QCheckBox* checkbox = new QCheckBox(tr("Use the first alternative title to search for torrents")); | 242 QCheckBox* checkbox = new QCheckBox(tr("Use the first alternative title to search for torrents")); |
| 245 subsection->layout()->addWidget(checkbox); | 243 subsection_layout->addWidget(checkbox); |
| 246 }); | 244 }); |
| 247 }); | 245 }); |
| 248 #undef CREATE_SECTION | 246 #undef CREATE_SECTION |
| 249 #undef CREATE_SUBSECTION | 247 #undef CREATE_SUBSECTION |
| 250 #undef CREATE_FULL_WIDTH_SECTION | 248 #undef CREATE_FULL_WIDTH_SECTION |
| 251 #undef CREATE_FULL_WIDTH_SUBSECTION | 249 #undef CREATE_FULL_WIDTH_SUBSECTION |
| 252 | 250 |
| 253 reinterpret_cast<QBoxLayout*>(settings_widget->layout())->addStretch(); | 251 settings_layout->addWidget(sg_local_content); |
| 252 settings_layout->addStretch(); | |
| 254 | 253 |
| 255 tabbed_widget->addTab(main_information_widget, tr("Main information")); | 254 tabbed_widget->addTab(main_information_widget, tr("Main information")); |
| 256 tabbed_widget->addTab(settings_widget, tr("My list and settings")); | 255 tabbed_widget->addTab(settings_widget, tr("My list and settings")); |
| 257 | 256 |
| 258 QVBoxLayout* main_layout = new QVBoxLayout; | 257 QVBoxLayout* main_layout = new QVBoxLayout(main_widget); |
| 259 main_layout->addWidget(anime_title); | 258 main_layout->addWidget(anime_title); |
| 260 main_layout->addWidget(tabbed_widget); | 259 main_layout->addWidget(tabbed_widget); |
| 261 main_layout->setContentsMargins(0, 0, 0, 0); | 260 main_layout->setContentsMargins(0, 0, 0, 0); |
| 262 main_widget->setLayout(main_layout); | 261 |
| 263 | 262 QHBoxLayout* layout = new QHBoxLayout(widget); |
| 264 QHBoxLayout* layout = new QHBoxLayout; | |
| 265 layout->addWidget(sidebar); | 263 layout->addWidget(sidebar); |
| 266 layout->addWidget(main_widget); | 264 layout->addWidget(main_widget); |
| 267 widget->setLayout(layout); | 265 layout->setSpacing(12); |
| 268 | 266 |
| 269 QDialogButtonBox* button_box = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this); | 267 QDialogButtonBox* button_box = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this); |
| 270 connect(button_box, &QDialogButtonBox::accepted, this, [this, accept] { | 268 connect(button_box, &QDialogButtonBox::accepted, this, [this, accept] { |
| 271 SaveData(); | 269 SaveData(); |
| 272 accept(); | 270 accept(); |
| 273 QDialog::accept(); | 271 QDialog::accept(); |
| 274 }); | 272 }); |
| 275 connect(button_box, &QDialogButtonBox::rejected, this, &QDialog::reject); | 273 connect(button_box, &QDialogButtonBox::rejected, this, &QDialog::reject); |
| 276 | 274 |
| 277 QVBoxLayout* buttons_layout = new QVBoxLayout; | 275 QVBoxLayout* buttons_layout = new QVBoxLayout(this); |
| 278 buttons_layout->addWidget(widget); | 276 buttons_layout->addWidget(widget); |
| 279 buttons_layout->addWidget(button_box, 0, Qt::AlignBottom); | 277 buttons_layout->addWidget(button_box, 0, Qt::AlignBottom); |
| 280 setLayout(buttons_layout); | |
| 281 } | 278 } |
| 282 | 279 |
| 283 #include "gui/dialog/moc_information.cpp" | 280 #include "gui/dialog/moc_information.cpp" |
