BibleTime
btcopybyreferencesdialog.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 
14 
15 #include <QComboBox>
16 #include <QDialogButtonBox>
17 #include <QFormLayout>
18 #include <QKeyEvent>
19 #include <QLabel>
20 #include <QPushButton>
21 #include <QRadioButton>
22 #include <QHBoxLayout>
23 #include <QVBoxLayout>
24 #include "../backend/keys/cswordkey.h"
25 #include "../backend/models/btmoduletextmodel.h"
26 #include "../backend/drivers/cswordmoduleinfo.h"
27 #include "../frontend/displaywindow/cdisplaywindow.h"
28 #include "../util/btassert.h"
29 #include "../util/btconnect.h"
30 #include "keychooser/ckeychooser.h"
31 #include "messagedialog.h"
32 
33 
35  BtModuleTextModel const * model,
36  std::optional<BtQmlInterface::Selection> const & selection,
37  CDisplayWindow * parent)
38  : QDialog(parent)
39  , m_workLabel(new QLabel(this))
40  , m_workCombo(new QComboBox(this))
41  , m_firstKeyLabel(new QLabel(this))
42  , m_lastKeyLabel(new QLabel(this))
43  , m_sizeTooLargeLabel(new QLabel(this))
44  , m_buttonBox(new QDialogButtonBox(QDialogButtonBox::Ok
45  | QDialogButtonBox::Cancel, this))
46 {
47  setMinimumWidth(400);
48 
49  auto * const vLayout = new QVBoxLayout(this);
50 
51  auto * const formLayout = new QFormLayout();
52  formLayout->setHorizontalSpacing(15);
53  formLayout->setVerticalSpacing(15);
54  formLayout->setContentsMargins(11, 11, 11, 16);
55  vLayout->addLayout(formLayout);
56 
57  auto const modules = parent->constModules();
58 
59  for (auto const * const m : modules)
60  m_workCombo->addItem(m->name(),
61  QVariant::fromValue(
62  const_cast<void *>(
63  static_cast<void const *>(m))));
64  m_workLabel->setBuddy(m_workCombo);
65  BT_CONNECT(m_workCombo, qOverload<int>(&QComboBox::currentIndexChanged),
67  formLayout->addRow(m_workLabel, m_workCombo);
68 
69  auto const parentKey = parent->key();
70 
72  CKeyChooser::createInstance(modules, parentKey->copy(), this);
74  formLayout->addRow(m_firstKeyLabel, m_firstKeyChooser);
75 
76  auto * const hLayout = new QHBoxLayout;
77  vLayout->addLayout(hLayout);
78 
80  CKeyChooser::createInstance(modules, parentKey->copy(), this);
82  formLayout->addRow(m_lastKeyLabel, m_lastKeyChooser);
83 
84  m_sizeTooLargeLabel->setVisible(false);
85  hLayout->addWidget(m_sizeTooLargeLabel);
86 
88  hLayout->addWidget(m_buttonBox);
89 
90  // Apply selection, if present:
91  if (selection.has_value()) {
92  BT_ASSERT(selection->column < modules.size());
94  model->indexToKey(selection->startIndex, 0));
95  m_lastKeyChooser->setKey(model->indexToKey(selection->endIndex, 0));
96  m_workCombo->setCurrentIndex(selection->column);
97  } // else default to top of view.
98 
99  auto const handleKeyChanged = [this, model]{
100  // Calculate result:
105  if (m_result.index1 > m_result.index2) { // ensure order:
106  std::swap(m_result.key1, m_result.key2);
107  std::swap(m_result.index1, m_result.index2);
108  }
109  resetThreshold();
110  };
111 
114 
115  BT_CONNECT(m_buttonBox, &QDialogButtonBox::accepted,
116  [this] {
117  auto const userData = m_workCombo->currentData();
118  m_result.module =
119  static_cast<CSwordModuleInfo const *>(
120  userData.value<void *>());
121  accept();
122  });
123  BT_CONNECT(m_buttonBox, &QDialogButtonBox::rejected,
124  this, &BtCopyByReferencesDialog::reject);
125 
126  retranslateUi();
127  handleKeyChanged();
128 }
129 
131  setWindowTitle(tr("Copy by References"));
132  m_workLabel->setText(tr("&Work:"));
133  m_firstKeyLabel->setText(tr("&First:"));
134  m_lastKeyLabel->setText(tr("&Last:"));
135  m_sizeTooLargeLabel->setText(tr("Copy size is too large!"));
136 }
137 
139  auto const type =
140  static_cast<CSwordModuleInfo const *>(
141  m_workCombo->currentData().value<void *>())->type();
142  auto const copyThreshold = (type == CSwordModuleInfo::Bible
143  || type == CSwordModuleInfo::Commentary)
144  ? 2700
145  : 100;
146  bool const tooLarge =
147  m_result.index2 - m_result.index1 > copyThreshold;
148  m_sizeTooLargeLabel->setVisible(tooLarge);
149  m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!tooLarge);
150 }
#define BT_ASSERT(...)
Definition: btassert.h:17
#define BT_CONNECT(...)
Definition: btconnect.h:20
BtCopyByReferencesDialog(BtModuleTextModel const *model, std::optional< BtQmlInterface::Selection > const &selection, CDisplayWindow *parent)
QDialogButtonBox *const m_buttonBox
Model that represents the entire text of a given module.
CSwordKey * indexToKey(int index, int moduleNum) const
int keyToIndex(CSwordKey const &key) const
The base class for all display windows of BibleTime.
BtConstModuleList constModules() const
CSwordKey * key() const
void keyChanged(CSwordKey *newKey)
static CKeyChooser * createInstance(const BtConstModuleList &modules, CSwordKey *key, QWidget *parent)
Definition: ckeychooser.cpp:26
virtual void setKey(CSwordKey *key)=0
virtual CSwordKey * key()=0
ModuleType type() const
void prepareDialogBox(QDialogButtonBox *box)