/src/gui/src/settings/filename-window.cpp

https://github.com/Bionus/imgbrd-grabber · C++ · 154 lines · 131 code · 23 blank · 0 comment · 35 complexity · f9f8d4a6accd2f3e04951a4e34c681ce MD5 · raw file

  1. #include "settings/filename-window.h"
  2. #include <QDesktopServices>
  3. #include <QMessageBox>
  4. #include <QRegExp>
  5. #include <QRegularExpression>
  6. #include <ui_filename-window.h>
  7. #include "models/filename.h"
  8. #include "models/image.h"
  9. #include "models/profile.h"
  10. #include "models/site.h"
  11. FilenameWindow::FilenameWindow(Profile *profile, QString value, QWidget *parent)
  12. : QDialog(parent), ui(new Ui::FilenameWindow), m_profile(profile)
  13. {
  14. ui->setupUi(this);
  15. #if defined(USE_QSCINTILLA)
  16. m_scintilla = new QsciScintilla(this);
  17. QsciLexerJavaScript *lexer = new QsciLexerJavaScript(this);
  18. m_scintilla->setLexer(lexer);
  19. #else
  20. m_scintilla = new QTextEdit(this);
  21. #endif
  22. connect(ui->radioJavascript, &QRadioButton::toggled, m_scintilla, &QWidget::setEnabled);
  23. ui->verticalLayout->insertWidget(ui->verticalLayout->count() - 1, m_scintilla);
  24. if (value.startsWith("javascript:")) {
  25. value = value.right(value.length() - 11);
  26. m_scintilla->setText(value);
  27. ui->lineClassic->setEnabled(false);
  28. ui->radioJavascript->toggle();
  29. } else {
  30. ui->lineClassic->setText(value);
  31. m_scintilla->setEnabled(false);
  32. ui->radioClassic->toggle();
  33. }
  34. connect(this, &QDialog::accepted, this, &FilenameWindow::send);
  35. }
  36. FilenameWindow::~FilenameWindow()
  37. {
  38. delete ui;
  39. }
  40. void FilenameWindow::on_lineClassic_textChanged(QString text)
  41. {
  42. QString message;
  43. Filename fn(text);
  44. fn.isValid(m_profile, &message);
  45. ui->labelValidator->setText(message);
  46. text = text.replace("\\", "\\\\").replace("'", "\\'");
  47. static const QRegExp date("%date:format=([^%]+)%");
  48. int pos = 0;
  49. while ((pos = date.indexIn(text, pos)) != -1) {
  50. QString cap = date.cap(1);
  51. QString format;
  52. for (const QChar &c : cap) {
  53. if (c == 'Y') {
  54. format += "' + date.getFullYear() + '";
  55. } else if (c == 'M') {
  56. format += "' + date.getMonth() + '";
  57. } else if (c == 'd') {
  58. format += "' + date.getDate() + '";
  59. } else if (c == 'h') {
  60. format += "' + date.getHours() + '";
  61. } else if (c == 'm') {
  62. format += "' + date.getMinutes() + '";
  63. } else if (c == 's') {
  64. format += "' + date.getSeconds() + '";
  65. } else {
  66. format += c;
  67. }
  68. }
  69. text = text.left(pos) + format + text.mid(pos + date.matchedLength());
  70. pos += date.matchedLength();
  71. }
  72. QString value = "'" + text.replace(QRegularExpression("%([^%]+)%"), "' + \\1 + '").remove(" + '' + ").trimmed() + "'";
  73. if (value.startsWith("' + ")) {
  74. value = value.right(value.length() - 4);
  75. }
  76. if (value.startsWith("'' + ")) {
  77. value = value.right(value.length() - 5);
  78. }
  79. if (value.endsWith(" + '")) {
  80. value = value.left(value.length() - 4);
  81. }
  82. if (value.endsWith(" + ''")) {
  83. value = value.left(value.length() - 5);
  84. }
  85. m_scintilla->setText(value);
  86. }
  87. void FilenameWindow::on_buttonHelpClassic_clicked()
  88. {
  89. QDesktopServices::openUrl(QUrl(QString(PROJECT_WEBSITE_URL) + "/docs/filename.html"));
  90. }
  91. void FilenameWindow::on_buttonHelpJavascript_clicked()
  92. {
  93. QDesktopServices::openUrl(QUrl(QString(PROJECT_WEBSITE_URL) + "/docs/filename.html#javascript"));
  94. }
  95. QString FilenameWindow::format() const
  96. {
  97. if (ui->radioJavascript->isChecked()) {
  98. #if defined(USE_QSCINTILLA)
  99. return "javascript:" + m_scintilla->text();
  100. #else
  101. return "javascript:" + m_scintilla->toPlainText();
  102. #endif
  103. }
  104. return ui->lineClassic->text();
  105. }
  106. void FilenameWindow::done(int r)
  107. {
  108. QMap<QString, Site*> sites = m_profile->getSites();
  109. if (QDialog::Accepted == r && ui->radioJavascript->isChecked() && !sites.isEmpty()) {
  110. Site *site = sites.first();
  111. QMap<QString, QString> info;
  112. info.insert("tags_general", "general_1 general_2");
  113. info.insert("tags_artist", "artist_1 artist_2");
  114. info.insert("tags_model", "model_1 model_2");
  115. info.insert("tags_character", "character_1 character_2");
  116. info.insert("tags_copyright", "copyright_1 copyright_2");
  117. Image image(site, info, m_profile);
  118. QStringList det = image.paths(format(), QString(), 0);
  119. if (det.isEmpty()) {
  120. const int reply = QMessageBox::question(this, tr("Warning"), tr("You script contains error, are you sure you want to save it?"), QMessageBox::Yes | QMessageBox::Cancel);
  121. if (reply == QMessageBox::Cancel) {
  122. return;
  123. }
  124. }
  125. }
  126. QDialog::done(r);
  127. }
  128. void FilenameWindow::send()
  129. {
  130. emit validated(format());
  131. }