cliwindow.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. Zint Barcode Generator - the open source barcode generator
  3. Copyright (C) 2021-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 <QClipboard>
  19. #include <QMimeData>
  20. #include <QSettings>
  21. #include "cliwindow.h"
  22. // Shorthand
  23. #define QSL QStringLiteral
  24. #define QSEmpty QLatin1String("")
  25. static const int tempMessageTimeout = 2000;
  26. CLIWindow::CLIWindow(BarcodeItem *bc, const bool autoHeight, const double heightPerRow,
  27. const struct Zint::QZintXdimDpVars* xdimdpVars)
  28. : m_bc(bc), m_autoHeight(autoHeight), m_heightPerRow(heightPerRow), m_xdimdpVars(xdimdpVars)
  29. {
  30. setupUi(this);
  31. QSettings settings;
  32. #if QT_VERSION < 0x60000
  33. settings.setIniCodec("UTF-8");
  34. #endif
  35. QByteArray geometry = settings.value(QSL("studio/cli/window_geometry")).toByteArray();
  36. restoreGeometry(geometry);
  37. #ifdef _WIN32
  38. const int index = settings.value(QSL("studio/cli/rad_unix_win"), 1).toInt();
  39. #else
  40. const int index = settings.value(QSL("studio/cli/rad_unix_win"), 0).toInt();
  41. #endif
  42. if (index == 1) {
  43. radCLIWin->setChecked(true);
  44. chkCLINoEXE->setEnabled(true);
  45. } else {
  46. radCLIUnix->setChecked(true);
  47. chkCLINoEXE->setEnabled(false);
  48. }
  49. chkCLILongOpts->setChecked(settings.value(QSL("studio/cli/chk_long_opts"), 0).toInt() ? true : false);
  50. chkCLIBarcodeName->setChecked(settings.value(QSL("studio/cli/chk_barcode_name"), 0).toInt() ? true : false);
  51. chkCLINoEXE->setChecked(settings.value(QSL("studio/cli/chk_no_exe"), 0).toInt() ? true : false);
  52. QIcon copyIcon(QIcon::fromTheme(QSL("edit-copy"), QIcon(QSL(":res/copy.svg"))));
  53. QIcon closeIcon(QIcon::fromTheme(QSL("window-close"), QIcon(QSL(":res/x.svg"))));
  54. btnCLICopy->setIcon(copyIcon);
  55. btnCLIClose->setIcon(closeIcon);
  56. connect(btnCLIClose, SIGNAL(clicked(bool)), SLOT(close()));
  57. connect(btnCLICopy, SIGNAL(clicked(bool)), SLOT(copy_to_clipboard()));
  58. connect(radCLIUnix, SIGNAL(toggled(bool)), SLOT(generate_cli()));
  59. connect(radCLIWin, SIGNAL(toggled(bool)), SLOT(generate_cli()));
  60. connect(chkCLILongOpts, SIGNAL(toggled(bool)), SLOT(generate_cli()));
  61. connect(chkCLIBarcodeName, SIGNAL(toggled(bool)), SLOT(generate_cli()));
  62. connect(chkCLINoEXE, SIGNAL(toggled(bool)), SLOT(generate_cli()));
  63. generate_cli();
  64. }
  65. CLIWindow::~CLIWindow()
  66. {
  67. QSettings settings;
  68. #if QT_VERSION < 0x60000
  69. settings.setIniCodec("UTF-8");
  70. #endif
  71. settings.setValue(QSL("studio/cli/window_geometry"), saveGeometry());
  72. settings.setValue(QSL("studio/cli/rad_unix_win"), radCLIWin->isChecked() ? 1 : 0);
  73. settings.setValue(QSL("studio/cli/chk_long_opts"), chkCLILongOpts->isChecked() ? 1 : 0);
  74. settings.setValue(QSL("studio/cli/chk_barcode_name"), chkCLIBarcodeName->isChecked() ? 1 : 0);
  75. settings.setValue(QSL("studio/cli/chk_no_exe"), chkCLINoEXE->isChecked() ? 1 : 0);
  76. }
  77. void CLIWindow::copy_to_clipboard()
  78. {
  79. QClipboard *clipboard = QGuiApplication::clipboard();
  80. QMimeData *mdata = new QMimeData;
  81. mdata->setData("text/plain", txtCLICmd->toPlainText().toUtf8());
  82. clipboard->setMimeData(mdata, QClipboard::Clipboard);
  83. statusBarCLI->showMessage(tr("Copied to clipboard"), tempMessageTimeout);
  84. }
  85. void CLIWindow::generate_cli()
  86. {
  87. bool noEXE = false;
  88. if (radCLIWin->isChecked()) {
  89. noEXE = chkCLINoEXE->isChecked();
  90. chkCLINoEXE->setEnabled(true);
  91. } else {
  92. chkCLINoEXE->setEnabled(false);
  93. }
  94. QString cmd = m_bc->bc.getAsCLI(radCLIWin->isChecked(), chkCLILongOpts->isChecked(),
  95. chkCLIBarcodeName->isChecked(), noEXE, m_autoHeight, m_heightPerRow, QSEmpty /*outfile*/,
  96. m_xdimdpVars);
  97. txtCLICmd->setPlainText(cmd);
  98. statusBarCLI->clearMessage();
  99. }
  100. /* vim: set ts=4 sw=4 et : */