scalewindow.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. Zint Barcode Generator - the open source barcode generator
  3. Copyright (C) 2022-2024 Robin Stuart <rstuart114@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. /* SPDX-License-Identifier: GPL-3.0-or-later */
  17. //#include <QDebug>
  18. #include <QSettings>
  19. #include <QUiLoader>
  20. #include <math.h>
  21. #include "scalewindow.h"
  22. // Shorthand
  23. #define QSL QStringLiteral
  24. /* "Standard" dpmm/dpi equivalents */
  25. static int resolution_standard(int inch, int val)
  26. {
  27. /* Note toss-up between rounding 2400 dpi to 94 or 95 (~94.48818897637795275591) */
  28. static int standards[9][2] = {
  29. { 4, 100 }, { 6, 150 }, { 8, 200 }, { 12, 300 }, { 16, 400 },
  30. { 24, 600 }, { 47, 1200 }, { 94, 2400 }, { 189, 4800 },
  31. };
  32. for (int i = 0; i < 8; i++) {
  33. if (standards[i][inch] == val) {
  34. return standards[i][inch ? 0 : 1];
  35. }
  36. }
  37. return 0;
  38. }
  39. ScaleWindow::ScaleWindow(BarcodeItem *bc, Zint::QZintXdimDpVars *vars, double originalScale)
  40. : m_bc(bc), Valid(false), m_vars(*vars), m_originalScale(originalScale), m_unset(false)
  41. {
  42. setupUi(this);
  43. if (m_bc->bc.noPng()) {
  44. cmbFileType->setItemText(0, QSL("Raster (BMP/GIF/PCX/TIF)"));
  45. }
  46. if (m_bc->bc.symbol() != BARCODE_MAXICODE) {
  47. cmbFileType->setItemText(1, QSL("Vector (EMF/EPS/SVG)"));
  48. cmbFileType->removeItem(2); // EMF
  49. }
  50. QSettings settings;
  51. #if QT_VERSION < 0x60000
  52. settings.setIniCodec("UTF-8");
  53. #endif
  54. QByteArray geometry = settings.value(QSL("studio/scale/window_geometry")).toByteArray();
  55. restoreGeometry(geometry);
  56. spnXdim->setValue(m_vars.x_dim);
  57. cmbXdimUnits->setCurrentIndex(m_vars.x_dim_units);
  58. spnResolution->setValue(m_vars.resolution);
  59. cmbResolutionUnits->setCurrentIndex(m_vars.resolution_units);
  60. if (m_bc->bc.symbol() == BARCODE_MAXICODE) {
  61. cmbFileType->setCurrentIndex(std::min(m_vars.filetype_maxicode, cmbFileType->count() - 1));
  62. } else {
  63. cmbFileType->setCurrentIndex(std::min(m_vars.filetype, cmbFileType->count() - 1));
  64. }
  65. if (cmbXdimUnits->currentIndex() == 1) { // Inches
  66. spnXdim->setSingleStep(0.001);
  67. } else {
  68. spnXdim->setSingleStep(0.01);
  69. }
  70. if (cmbResolutionUnits->currentIndex() == 1) { // Inches
  71. spnResolution->setSingleStep(50);
  72. } else {
  73. spnResolution->setSingleStep(1);
  74. }
  75. set_maxima();
  76. size_msg_ui_set();
  77. QIcon closeIcon(QIcon::fromTheme(QSL("window-close"), QIcon(QSL(":res/x.svg"))));
  78. QIcon unsetIcon(QSL(":res/delete.svg"));
  79. QIcon okIcon(QIcon(QSL(":res/check.svg")));
  80. btnCancel->setIcon(closeIcon);
  81. btnScaleUnset->setIcon(unsetIcon);
  82. btnScaleUnset->setEnabled(m_vars.set);
  83. btnOK->setIcon(okIcon);
  84. connect(btnCancel, SIGNAL(clicked(bool)), SLOT(close()));
  85. connect(btnScaleUnset, SIGNAL(clicked(bool)), SLOT(unset_scale()));
  86. connect(btnOK, SIGNAL(clicked(bool)), SLOT(okay()));
  87. connect(spnXdim, SIGNAL(valueChanged(double)), SLOT(update_scale()));
  88. connect(cmbXdimUnits, SIGNAL(currentIndexChanged(int)), SLOT(x_dim_units_change()));
  89. connect(btnXdimDefault, SIGNAL(clicked(bool)), SLOT(x_dim_default()));
  90. connect(spnResolution, SIGNAL(valueChanged(int)), SLOT(update_scale()));
  91. connect(cmbResolutionUnits, SIGNAL(currentIndexChanged(int)), SLOT(resolution_units_change()));
  92. connect(btnResolutionDefault, SIGNAL(clicked(bool)), SLOT(resolution_default()));
  93. connect(cmbFileType, SIGNAL(currentIndexChanged(int)), SLOT(update_scale()));
  94. }
  95. ScaleWindow::~ScaleWindow()
  96. {
  97. QSettings settings;
  98. #if QT_VERSION < 0x60000
  99. settings.setIniCodec("UTF-8");
  100. #endif
  101. settings.setValue(QSL("studio/scale/window_geometry"), saveGeometry());
  102. }
  103. void ScaleWindow::size_msg_ui_set()
  104. {
  105. msgPrintingScale->setText(QString::asprintf("%.2f", m_bc->bc.scale()));
  106. float width_x_dim, height_x_dim;
  107. if (m_bc->bc.getWidthHeightXdim((float) m_vars.x_dim, width_x_dim, height_x_dim)) {
  108. const char *fmt = cmbXdimUnits->currentIndex() == 1 ? "%.3f x %.3f in" : "%.2f x %.2f mm";
  109. msgWidthHeight->setText(QString::asprintf(fmt, width_x_dim, height_x_dim));
  110. } else {
  111. msgWidthHeight->clear();
  112. }
  113. bool defaultDisable;
  114. QWidget *focus = QApplication::focusWidget();
  115. // This doesn't work well due to rounding errors TODO: fix
  116. defaultDisable = m_bc->bc.defaultXdim() == (float) get_x_dim_mm();
  117. btnXdimDefault->setEnabled(!defaultDisable);
  118. if (focus == btnXdimDefault && defaultDisable) {
  119. spnXdim->setFocus();
  120. }
  121. defaultDisable = (cmbResolutionUnits->currentIndex() == 0 && spnResolution->value() == 12)
  122. || (cmbResolutionUnits->currentIndex() == 1 && spnResolution->value() == 300);
  123. btnResolutionDefault->setEnabled(!defaultDisable);
  124. if (focus == btnResolutionDefault && defaultDisable) {
  125. spnResolution->setFocus();
  126. }
  127. }
  128. void ScaleWindow::unset_scale()
  129. {
  130. m_vars.x_dim = std::min(m_bc->bc.getXdimDpFromScale(m_originalScale, get_dpmm(), getFileType()), 10.0f);
  131. m_vars.set = 0;
  132. if (cmbXdimUnits->currentIndex() == 1) { // Inches
  133. spnXdim->setValue(m_vars.x_dim / 25.4);
  134. } else {
  135. spnXdim->setValue(m_vars.x_dim);
  136. }
  137. m_unset = true;
  138. btnScaleUnset->setEnabled(false);
  139. }
  140. void ScaleWindow::okay()
  141. {
  142. if (update_vars()) {
  143. Valid = true;
  144. m_vars.set = m_unset ? 0 : 1;
  145. }
  146. close();
  147. }
  148. void ScaleWindow::update_scale()
  149. {
  150. float scale = update_vars();
  151. if (scale) {
  152. // Need up-to-date `vectorWidth()` and `vectorHeight()` to estimate size including borders, whitespace & text,
  153. // so tell main window to encode and it will update UI here via `size_msg_ui_set()`
  154. emit scaleChanged(scale);
  155. m_unset = false;
  156. btnScaleUnset->setEnabled(true);
  157. set_maxima();
  158. }
  159. }
  160. void ScaleWindow::x_dim_units_change()
  161. {
  162. if (cmbXdimUnits->currentIndex() == 1) { // Inches
  163. spnXdim->setValue(spnXdim->value() / 25.4);
  164. spnXdim->setSingleStep(0.001);
  165. } else {
  166. spnXdim->setValue(spnXdim->value() * 25.4);
  167. spnXdim->setSingleStep(0.01);
  168. }
  169. update_scale();
  170. }
  171. void ScaleWindow::x_dim_default()
  172. {
  173. if (cmbXdimUnits->currentIndex() == 1) { // Inches
  174. spnXdim->setValue(m_bc->bc.defaultXdim() / 25.4);
  175. } else {
  176. spnXdim->setValue(m_bc->bc.defaultXdim());
  177. }
  178. update_scale();
  179. }
  180. void ScaleWindow::resolution_units_change()
  181. {
  182. int value;
  183. if (cmbResolutionUnits->currentIndex() == 1) { // Inches
  184. if ((value = resolution_standard(0, spnResolution->value()))) {
  185. spnResolution->setValue(value);
  186. } else {
  187. spnResolution->setValue((int) roundf(spnResolution->value() * 25.4f));
  188. }
  189. spnResolution->setSingleStep(50);
  190. } else {
  191. if ((value = resolution_standard(1, spnResolution->value()))) {
  192. spnResolution->setValue(value);
  193. } else {
  194. spnResolution->setValue((int) roundf(spnResolution->value() / 25.4f));
  195. }
  196. spnResolution->setSingleStep(1);
  197. }
  198. update_scale();
  199. }
  200. void ScaleWindow::resolution_default()
  201. {
  202. spnResolution->setValue(cmbResolutionUnits->currentIndex() == 1 ? 300 : 12);
  203. update_scale();
  204. }
  205. float ScaleWindow::get_x_dim_mm() const
  206. {
  207. return (float) (cmbXdimUnits->currentIndex() == 1 ? spnXdim->value() * 25.4 : spnXdim->value());
  208. }
  209. float ScaleWindow::get_dpmm() const
  210. {
  211. return (float) (cmbResolutionUnits->currentIndex() == 1 ? spnResolution->value() / 25.4 : spnResolution->value());
  212. }
  213. const char *ScaleWindow::getFileType() const
  214. {
  215. static const char *filetypes[3] = { "GIF", "SVG", "EMF" };
  216. return filetypes[std::max(std::min(cmbFileType->currentIndex(), 2), 0)];
  217. }
  218. void ScaleWindow::set_maxima()
  219. {
  220. float maxXdim = std::min(m_bc->bc.getXdimDpFromScale(200.0f, get_dpmm(), getFileType()), 10.0f);
  221. if (cmbXdimUnits->currentIndex() == 1) { // Inches
  222. spnXdim->setMaximum(maxXdim / 25.4);
  223. } else {
  224. spnXdim->setMaximum(maxXdim);
  225. }
  226. float maxRes = m_bc->bc.getXdimDpFromScale(200.0f, get_x_dim_mm(), getFileType());
  227. if (cmbResolutionUnits->currentIndex() == 1) { // Inches
  228. spnResolution->setMaximum(maxRes * 25.4);
  229. } else {
  230. spnResolution->setMaximum(maxRes);
  231. }
  232. }
  233. double ScaleWindow::update_vars()
  234. {
  235. double scale = (double) m_bc->bc.getScaleFromXdimDp(get_x_dim_mm(), get_dpmm(), getFileType());
  236. if (scale != 0.0) {
  237. m_vars.x_dim = spnXdim->value();
  238. m_vars.x_dim_units = cmbXdimUnits->currentIndex();
  239. m_vars.resolution = spnResolution->value();
  240. m_vars.resolution_units = cmbResolutionUnits->currentIndex();
  241. if (m_bc->bc.symbol() == BARCODE_MAXICODE) {
  242. m_vars.filetype_maxicode = cmbFileType->currentIndex();
  243. } else {
  244. m_vars.filetype = cmbFileType->currentIndex();
  245. }
  246. }
  247. return scale;
  248. }
  249. /* vim: set ts=4 sw=4 et : */