BibleTime
btconfigdialog.cpp
Go to the documentation of this file.
1 /*********
2 *
3 * In the name of the Father, and of the Son, and of the Holy Spirit.
4 *
5 * This file is part of BibleTime's source code, https://bibletime.info/
6 *
7 * Copyright 1999-2021 by the BibleTime developers.
8 * The BibleTime source code is licensed under the GNU General Public License
9 * version 2.0.
10 *
11 **********/
12 
13 #include "btconfigdialog.h"
14 
15 #include <QDialogButtonBox>
16 #include <QFrame>
17 #include <QHBoxLayout>
18 #include <QListView>
19 #include <QListWidget>
20 #include <QListWidgetItem>
21 #include <QPushButton>
22 #include <QRect>
23 #include <QSizePolicy>
24 #include <QStackedWidget>
25 #include <QVBoxLayout>
26 #include "../../util/btassert.h"
27 #include "../../util/btconnect.h"
28 #include "../messagedialog.h"
29 
30 
31 namespace {
32 
33 void resizeListWidget(QListWidget & listWidget) {
34  BT_ASSERT(listWidget.count() > 0);
35  listWidget.setFixedWidth(
36  listWidget.visualItemRect(listWidget.item(0)).width()
37  + listWidget.frameWidth() * 2);
38 }
39 
40 } // anonymous namespace
41 
42 BtConfigDialog::Page::Page(QIcon const & icon, QWidget * parent)
43  : QWidget(parent)
44  , m_listWidgetItem(new QListWidgetItem)
45 {
46  try {
47  m_listWidgetItem->setIcon(icon);
48  m_listWidgetItem->setTextAlignment(Qt::AlignHCenter);
49  m_listWidgetItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
50  } catch (...) {
51  delete m_listWidgetItem;
52  throw;
53  }
54 }
55 
57  if (m_ownsListWidgetItem)
58  delete m_listWidgetItem;
59 }
60 
61 void BtConfigDialog::Page::setHeaderText(QString const & headerText)
62 { m_listWidgetItem->setText(headerText); }
63 
64 
66  Qt::WindowFlags const flags)
67  : QDialog(parent, flags)
68  , m_contentsList(new QListWidget(this))
69  , m_pageWidget(new QStackedWidget(this))
70 {
71  QHBoxLayout * const mainLayout = new QHBoxLayout(this);
72 
73  m_contentsList->setUniformItemSizes(true);
74  m_contentsList->setViewMode(QListView::IconMode);
75  m_contentsList->setMovement(QListView::Static);
76  BT_CONNECT(m_contentsList, &QListWidget::currentRowChanged,
77  m_pageWidget, &QStackedWidget::setCurrentIndex);
78  BT_CONNECT(m_contentsList, &QListWidget::itemChanged,
79  [this]{ resizeListWidget(*m_contentsList); });
80  mainLayout->addWidget(m_contentsList);
81 
82  auto * const pageLayout = new QVBoxLayout;
83  try {
84  mainLayout->addLayout(pageLayout);
85  } catch (...) {
86  delete pageLayout;
87  throw;
88  }
89 
90  m_pageWidget->setSizePolicy(QSizePolicy::MinimumExpanding,
91  QSizePolicy::MinimumExpanding);
92  pageLayout->addWidget(m_pageWidget);
93 
94  QFrame * const buttonBoxRuler = new QFrame(this);
95  buttonBoxRuler->setGeometry(QRect(1, 1, 1, 3));
96  buttonBoxRuler->setFrameShape(QFrame::HLine);
97  buttonBoxRuler->setFrameShadow(QFrame::Sunken);
98  buttonBoxRuler->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
99  pageLayout->addWidget(buttonBoxRuler);
100 
101  // Add button box:
102  auto * const bbox = new QDialogButtonBox(this);
103  BT_CONNECT(bbox->addButton(QDialogButtonBox::Ok),
104  &QPushButton::clicked,
105  [this] { save(); close(); });
106  BT_CONNECT(bbox->addButton(QDialogButtonBox::Apply),
107  &QPushButton::clicked,
108  [this] { save(); });
109  BT_CONNECT(bbox->addButton(QDialogButtonBox::Cancel),
110  &QPushButton::clicked,
111  [this]{ close(); });
113  pageLayout->addWidget(bbox);
114 }
115 
116 void BtConfigDialog::addPage(Page * const pageWidget) {
117  m_pageWidget->addWidget(pageWidget);
118 
119  m_contentsList->addItem(pageWidget->m_listWidgetItem);
120  pageWidget->m_ownsListWidgetItem = false;
121 
123  if (m_pageWidget->count() == 1)
124  m_contentsList->setCurrentRow(0);
125 }
126 
128  for (int i = 0; i < m_pageWidget->count(); ++i) {
129  BT_ASSERT(dynamic_cast<Page *>(m_pageWidget->widget(i)));
130  static_cast<Page *>(m_pageWidget->widget(i))->save();
131  }
132  Q_EMIT signalSettingsChanged();
133 }
#define BT_ASSERT(...)
Definition: btassert.h:17
#define BT_CONNECT(...)
Definition: btconnect.h:20
void setHeaderText(QString const &headerText)
QListWidgetItem *const m_listWidgetItem
Page(QIcon const &icon, QWidget *const parent=nullptr)
~Page() noexcept override
void addPage(Page *const pageWidget)
Adds the page to this dialog, taking ownership.
QListWidget *const m_contentsList
QStackedWidget *const m_pageWidget
void signalSettingsChanged()
BtConfigDialog(QWidget *const parent=nullptr, Qt::WindowFlags const flags=Qt::WindowFlags())
void resizeListWidget(QListWidget &listWidget)
void prepareDialogBox(QDialogButtonBox *box)