KSaneCore

pagesizeoption.cpp
1/*
2 * SPDX-FileCopyrightText: 2009 Kare Sars <kare dot sars at iki dot fi>
3 * SPDX-FileCopyrightText: 2014 Gregor Mitsch : port to KDE5 frameworks
4 * SPDX-FileCopyrightText: 2021 Alexander Stippich <a.stippich@gmx.net>
5 *
6 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8
9#include "pagesizeoption.h"
10
11#include <QPageSize>
12#include <QSizeF>
13
14#include <ksanecore_debug.h>
15
16static constexpr int PageSizeWiggleRoom = 2; // in mm
17
18namespace KSaneCore
19{
20
21PageSizeOption::PageSizeOption(BaseOption *optionTopLeftX,
22 BaseOption *optionTopLeftY,
23 BaseOption *optionBottomRightX,
24 BaseOption *optionBottomRightY,
25 BaseOption *optionResolution)
26 : BaseOption()
27{
28 if (optionTopLeftX == nullptr || optionTopLeftY == nullptr || optionBottomRightX == nullptr || optionBottomRightY == nullptr) {
29 m_optionType = Option::TypeDetectFail;
30 return;
31 }
32
33 connect(optionTopLeftX, &BaseOption::valueChanged, this, &PageSizeOption::optionTopLeftXUpdated);
34 connect(optionTopLeftY, &BaseOption::valueChanged, this, &PageSizeOption::optionTopLeftYUpdated);
35 connect(optionBottomRightX, &BaseOption::valueChanged, this, &PageSizeOption::optionBottomRightXUpdated);
36 connect(optionBottomRightY, &BaseOption::valueChanged, this, &PageSizeOption::optionBottomRightYUpdated);
37
38 m_optionTopLeftX = optionTopLeftX;
39 m_optionTopLeftY = optionTopLeftY;
40 m_optionBottomRightX = optionBottomRightX;
41 m_optionBottomRightY = optionBottomRightY;
42 m_optionResolution = optionResolution;
43
44 const QList<QPageSize::PageSizeId> possibleSizesList = {
66 };
67
69 m_availableSizesListNames << QPageSize::name(QPageSize::Custom);
70
71 double maxScannerWidth = ensureMilliMeter(m_optionBottomRightX, m_optionBottomRightX->maximumValue().toDouble());
72 double maxScannerHeight = ensureMilliMeter(m_optionBottomRightY, m_optionBottomRightY->maximumValue().toDouble());
73
74 // Add portrait page sizes
75 for (const auto sizeCode : possibleSizesList) {
77 if (size.width() - PageSizeWiggleRoom > maxScannerWidth) {
78 continue;
79 }
80 if (size.height() - PageSizeWiggleRoom > maxScannerHeight) {
81 continue;
82 }
83 m_availableSizesList << size;
84 m_availableSizesListNames << QPageSize::name(sizeCode);
85 }
86
87 // Add landscape page sizes
88 for (const auto sizeCode : possibleSizesList) {
90 size.transpose();
91 if (size.width() - PageSizeWiggleRoom > maxScannerWidth) {
92 continue;
93 }
94 if (size.height() - PageSizeWiggleRoom > maxScannerHeight) {
95 continue;
96 }
97 m_availableSizesList << size;
98 m_availableSizesListNames << i18nc("Page size landscape", "Landscape %1", QPageSize::name(sizeCode));
99 }
100
101 // Set custom as current
102 m_currentIndex = 0;
103 if (m_availableSizesList.count() > 1) {
104 m_state = Option::StateActive;
105 } else {
106 m_state = Option::StateHidden;
107 }
108 m_optionType = Option::TypeValueList;
109}
110
111bool PageSizeOption::setValue(const QVariant &value)
112{
113#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
114 if (static_cast<QMetaType::Type>(value.type()) == QMetaType::QString) {
115#else
116 if (value.userType() == QMetaType::QString) {
117#endif
118 QString newValue = value.toString();
119 if (newValue == m_availableSizesListNames.at(m_currentIndex)) {
120 return true;
121 }
122 for (int i = 0; i < m_availableSizesListNames.size(); i++) {
123 QString sizeEntry = m_availableSizesListNames.at(i).toString();
124 if (sizeEntry == newValue) {
125 m_currentIndex = i;
126
127 if (i != 0) {
128 const auto size = m_availableSizesList.at(i);
129 m_optionTopLeftX->setValue(0);
130 m_optionTopLeftY->setValue(0);
131 m_optionBottomRightX->setValue(size.width());
132 m_optionBottomRightY->setValue(size.height());
133 }
134 Q_EMIT valueChanged(sizeEntry);
135 return true;
136 }
137 }
138 }
139 return false;
140}
141
142QVariant PageSizeOption::value() const
143{
144 if (m_currentIndex >= 0 && m_currentIndex < m_availableSizesListNames.size()) {
145 return m_availableSizesListNames.at(m_currentIndex);
146 } else {
147 return QVariant();
148 }
149}
150
151QString PageSizeOption::valueAsString() const
152{
153 if (m_currentIndex >= 0 && m_currentIndex < m_availableSizesListNames.size()) {
154 return m_availableSizesListNames.at(m_currentIndex).toString();
155 } else {
156 return QString();
157 }
158}
159
160QVariantList PageSizeOption::valueList() const
161{
162 return m_availableSizesListNames;
163}
164
165Option::OptionState PageSizeOption::state() const
166{
167 return m_state;
168}
169
170QString PageSizeOption::name() const
171{
172 return PageSizeOptionName;
173}
174
175QString PageSizeOption::title() const
176{
177 return i18n("Scan area size");
178}
179
180QString PageSizeOption::description() const
181{
182 return i18n("Select a predefined page size for the scanning area.");
183}
184
185void PageSizeOption::optionTopLeftXUpdated()
186{
187 if (m_currentIndex > 0 && m_currentIndex < m_availableSizesList.size() && m_optionTopLeftY->value().toDouble() != 0) {
188 m_currentIndex = 0;
189 Q_EMIT valueChanged(QPageSize::name(QPageSize::Custom));
190 }
191}
192
193void PageSizeOption::optionTopLeftYUpdated()
194{
195 if (m_currentIndex > 0 && m_currentIndex < m_availableSizesList.size() && m_optionTopLeftY->value().toDouble() != 0) {
196 m_currentIndex = 0;
197 Q_EMIT valueChanged(QPageSize::name(QPageSize::Custom));
198 }
199}
200
201void PageSizeOption::optionBottomRightXUpdated()
202{
203 if (m_currentIndex > 0 && m_currentIndex < m_availableSizesList.size()
204 && ensureMilliMeter(m_optionBottomRightX, m_optionBottomRightX->value().toDouble()) != m_availableSizesList.at(m_currentIndex).width()) {
205 m_currentIndex = 0;
206 Q_EMIT valueChanged(QPageSize::name(QPageSize::Custom));
207 }
208}
209
210void PageSizeOption::optionBottomRightYUpdated()
211{
212 if (m_currentIndex > 0 && m_currentIndex < m_availableSizesList.size()
213 && ensureMilliMeter(m_optionBottomRightY, m_optionBottomRightY->value().toDouble()) != m_availableSizesList.at(m_currentIndex).height()) {
214 m_currentIndex = 0;
215 Q_EMIT valueChanged(QPageSize::name(QPageSize::Custom));
216 }
217}
218
219double PageSizeOption::ensureMilliMeter(BaseOption *option, double value)
220{
221 // convert if necessary with current DPI if available
222 if (option->valueUnit() == Option::UnitPixel && m_optionResolution != nullptr) {
223 double dpi = m_optionResolution->value().toDouble();
224 if (dpi > 1) {
225 return value / (dpi / 25.4);
226 }
227 }
228 return value;
229}
230
231} // namespace KSaneCore
232
233#include "moc_pagesizeoption.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
QString name() const const
QSizeF size(PageSizeId pageSizeId, Unit units)
qreal height() const const
void transpose()
qreal width() const const
const QChar at(qsizetype position) const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
Type type() const const
QString toString() const const
int userType() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 24 2024 11:49:00 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.