argdialog.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #include <include/argdialog.h>
  2. #include <ui_argdialog.h>
  3. #include <QCalendarWidget>
  4. #include <QDateTime>
  5. #include <QDebug>
  6. #include <QIODevice>
  7. #include <QMimeDatabase>
  8. #include <QStringList>
  9. #include <QTableWidgetItem>
  10. #include <algorithm>
  11. #include <vector>
  12. ArgDialog::ArgDialog(QWidget *parent)
  13. : QDialog(parent),
  14. ui(new Ui::ArgDialog),
  15. m_task(Task{}),
  16. m_ig_post(IGPost{}) {
  17. ui->setupUi(this);
  18. ui->argCommandButtons->button(QDialogButtonBox::Cancel)
  19. ->setStyleSheet(QString("background:%1").arg("#2f535f"));
  20. QObject::connect(ui->addFile, &QPushButton::clicked, this, [this]() {
  21. KFileDialog file_dialog{};
  22. auto file_path = file_dialog.openFileDialog(m_file_path);
  23. qDebug() << "Selected file:" << file_path;
  24. if (file_path.size() > 0) {
  25. auto slash_index = file_path.lastIndexOf("/") + 1;
  26. QString file_name = file_path.right(file_path.size() - slash_index);
  27. QString dir = file_path.left(slash_index);
  28. qDebug() << "Dir is " << dir;
  29. QMimeDatabase db;
  30. auto is_video = db.mimeTypeForFile(file_path).name().contains("video");
  31. addItem(file_name, "file");
  32. m_ig_post.files.push_back(KFile{.name = file_name,
  33. .path = file_path,
  34. .type = is_video ? FileType::VIDEO : FileType::IMAGE});
  35. if (is_video) {
  36. qDebug() << "File discovered to be video";
  37. m_ig_post.is_video = true; // rename to "sending_video"
  38. QString preview_filename = FileUtils::generatePreview(file_path, file_name);
  39. // TODO: create some way of verifying preview generation was successful
  40. addFile("assets/previews/" + preview_filename);
  41. addItem(preview_filename, "file");
  42. addFile("assets/previews/" + preview_filename);
  43. m_ig_post.files.push_back(KFile{.name = preview_filename,
  44. .path = QCoreApplication::applicationDirPath()
  45. + "/assets/previews/" + preview_filename,
  46. .type = is_video ? FileType::VIDEO : FileType::IMAGE});
  47. } else {
  48. addFile(file_path);
  49. }
  50. }
  51. });
  52. ui->argList->setHorizontalHeaderLabels(QStringList{"Value", "Type", "Preview", "Delete"});
  53. ui->argList->setColumnWidth(0, 300);
  54. ui->argList->setColumnWidth(1, 40);
  55. ui->argList->setColumnWidth(2, 100);
  56. ui->argList->setColumnWidth(3, 30);
  57. ui->argList->verticalHeader()->setDefaultSectionSize(100);
  58. QObject::connect(ui->addArgument, &QPushButton::clicked, this, [this]() {
  59. QString text = ui->argInput->toPlainText();
  60. auto type = ui->argType->currentText();
  61. if (text.size() > 0) {
  62. if (type == Args::HASHTAG_TYPE) {
  63. addHashtag(text);
  64. } else if (type == Args::DESCRIPTION_TYPE) {
  65. addItem(text, type);
  66. m_ig_post.description = escapeText(text).toUtf8().constData();
  67. } else if (type == Args::PROMOTE_TYPE) {
  68. addOrReplaceInArgList(text, "promote/share");
  69. m_ig_post.promote_share = text.toUtf8().constData();
  70. } else if (type == Args::LINK_BIO_TYPE) {
  71. addOrReplaceInArgList(text, "link/bio");
  72. m_ig_post.link_in_bio = text.toUtf8().constData();
  73. } else if (type == Args::REQUESTED_BY_TYPE) {
  74. addRequestedBy(text);
  75. }
  76. ui->argInput->clear();
  77. }
  78. });
  79. ui->dateTime->setDateTime(QDateTime::currentDateTime());
  80. QObject::connect(ui->dateTime, &QDateTimeEdit::dateTimeChanged, this, [this]() {
  81. auto date_time = ui->dateTime->dateTime();
  82. m_ig_post.datetime = std::string{std::to_string(date_time.toTime_t())};
  83. qDebug() << "Time changed to" << date_time;
  84. });
  85. QObject::connect(ui->argCommandButtons,
  86. static_cast<void (QDialogButtonBox::*)(QAbstractButton *)>(
  87. &QDialogButtonBox::clicked),
  88. this,
  89. [this](QAbstractButton *button) {
  90. if (button->text() == "Save") {
  91. if (m_ig_post.isReady()) {
  92. setTaskArguments();
  93. QVector<KFileData> k_file_v{};
  94. k_file_v.reserve(m_ig_post.files.size());
  95. for (const auto &kfile : m_ig_post.files) {
  96. QFile file(kfile.path);
  97. if (file.open(QIODevice::ReadOnly)) {
  98. k_file_v.push_back(KFileData{.type = kfile.type,
  99. .name = kfile.name,
  100. .bytes = file.readAll()});
  101. } else {
  102. QMessageBox::warning(this, tr("File Error"), tr("Unable to read file"));
  103. }
  104. }
  105. if (!k_file_v.empty()) {
  106. emit ArgDialog::uploadFiles(k_file_v);
  107. }
  108. emit ArgDialog::taskRequestReady(m_task, true);
  109. }
  110. clearPost(); // reset m_ig_post to default values
  111. }
  112. });
  113. QObject::connect(ui->clear, &QPushButton::clicked, this, [this]() {
  114. clearPost();
  115. ui->argList->setRowCount(0);
  116. qDebug() << "Task cleared and restored to default values";
  117. });
  118. }
  119. void ArgDialog::setTaskArguments() {
  120. m_task.args.clear();
  121. std::string hashtags{};
  122. for (const auto & tag : m_ig_post.hashtags) {
  123. hashtags += "#" + tag + " ";
  124. }
  125. hashtags.pop_back();
  126. std::string requested_by{};
  127. for (const auto & name : m_ig_post.requested_by) {
  128. requested_by += "@" + name + "";
  129. }
  130. if (m_ig_post.requested_by.size() > 1) {
  131. requested_by.pop_back();
  132. }
  133. // m_task.args.push_back(m_ig_post.file.name.toUtf8().constData());
  134. m_task.args.push_back(m_ig_post.datetime);
  135. m_task.args.push_back(m_ig_post.description);
  136. m_task.args.push_back(hashtags);
  137. m_task.args.push_back(requested_by);
  138. m_task.args.push_back(m_ig_post.requested_by_phrase);
  139. m_task.args.push_back(m_ig_post.promote_share);
  140. m_task.args.push_back(m_ig_post.link_in_bio);
  141. m_task.args.push_back(std::to_string(m_ig_post.is_video));
  142. m_task.args.push_back(m_ig_post.header);
  143. }
  144. void ArgDialog::addItem(QString value, QString type) {
  145. QTableWidgetItem *item = new QTableWidgetItem(value);
  146. QTableWidgetItem *item2 = new QTableWidgetItem(type);
  147. auto row = ui->argList->rowCount();
  148. ui->argList->insertRow(row);
  149. QPushButton *q_pb = new QPushButton();
  150. q_pb->setText("Delete");
  151. q_pb->setIcon(std::move(QIcon(":/icons/icons/quit.png")));
  152. QObject::connect(q_pb, &QPushButton::clicked, this, [this, row]() {
  153. ui->argList->removeRow(ui->argList->currentRow());
  154. });
  155. ui->argList->setItem(row, 0, item);
  156. ui->argList->setItem(row, 1, item2);
  157. ui->argList->setCellWidget(row, 3, q_pb);
  158. }
  159. void ArgDialog::addFile(QString path) {
  160. auto row_count = ui->argList->rowCount();
  161. QTableWidgetItem *file_item = new QTableWidgetItem();
  162. QPixmap pm{path};
  163. file_item->setData(
  164. Qt::DecorationRole,
  165. pm.scaledToHeight(ui->argList->rowHeight(0),
  166. Qt::TransformationMode::SmoothTransformation));
  167. ui->argList->setItem(row_count - 1, 2, file_item);
  168. }
  169. void ArgDialog::clearPost() {
  170. m_ig_post.files.clear();
  171. m_ig_post.header = "Learn to speak like native Korean speakers 🙆‍♀️🇰🇷";
  172. QDateTime date_time = QDateTime::currentDateTime();
  173. ui->dateTime->setDateTime(date_time);
  174. m_ig_post.datetime = date_time.toTime_t();
  175. m_ig_post.hashtags.clear();
  176. m_ig_post.description = "";
  177. m_ig_post.link_in_bio = "Subscribe to my YouTube channel (link 🔗in bio) to learn more about "
  178. "Korean language and culture ❤";
  179. m_ig_post.requested_by.clear();
  180. m_ig_post.promote_share = "Share the post through IG story if you enjoy the phrase 🙋‍♀️";
  181. m_ig_post.requested_by_phrase = "The phrase was requested by ";
  182. ui->argType->setCurrentIndex(0);
  183. }
  184. void ArgDialog::clearTask() {
  185. m_task.args = {};
  186. m_task.mask = -1;
  187. }
  188. void ArgDialog::addRequestedBy(QString value) {
  189. if (std::find(m_ig_post.requested_by.begin(), m_ig_post.requested_by.end(), value.toUtf8().constData()) == m_ig_post.requested_by.end()) {
  190. m_ig_post.requested_by.push_back(value.toUtf8().constData());
  191. addToArgList(value, "requested_by");
  192. } else {
  193. const char* message = "You have already inputed this name under \"requested_by\"";
  194. qDebug() << message;
  195. QMessageBox::warning(
  196. this,
  197. tr("Requested By"),
  198. tr(message)
  199. );
  200. }
  201. }
  202. void ArgDialog::addToArgList(QString value, QString type) {
  203. for (int i = 0; i < ui->argList->rowCount(); i++) {
  204. auto item = ui->argList->item(i, 1);
  205. if (item) {
  206. if (QString::compare(item->text(), type) == 0) {
  207. auto text = ui->argList->item(i, 0)->text();
  208. text.append("\n");
  209. text += value;
  210. ui->argList->item(i, 0)->setText(text);
  211. return;
  212. }
  213. }
  214. }
  215. addItem(value, type);
  216. }
  217. void ArgDialog::addOrReplaceInArgList(QString value, QString type) {
  218. for (int i = 0; i < ui->argList->rowCount(); i++) {
  219. auto item = ui->argList->item(i, 1);
  220. if (item) {
  221. if (QString::compare(item->text(), type) == 0) {
  222. ui->argList->item(i, 0)->setText(value);
  223. return;
  224. }
  225. }
  226. }
  227. addItem(value, type);
  228. }
  229. void ArgDialog::addHashtag(QString tag) {
  230. QStringList tags = tag.split(" ");
  231. for (const auto& tag : tags) {
  232. if (std::find(m_ig_post.hashtags.begin(), m_ig_post.hashtags.end(), tag.toUtf8().constData()) == m_ig_post.hashtags.end()) {
  233. m_ig_post.hashtags.push_back(tag.toUtf8().constData());
  234. addToArgList(tag, "hashtag");
  235. } else {
  236. const char* message = "Can't add the same hashtag twice";
  237. qDebug() << message;
  238. QMessageBox::warning(
  239. this,
  240. tr("Hashtags"),
  241. tr(message)
  242. );
  243. }
  244. }
  245. }
  246. void ArgDialog::keyPressEvent(QKeyEvent *e) {
  247. if (Qt::ControlModifier) {
  248. if(e->key()==Qt::Key_Return || e->key()==Qt::Key_Enter) {
  249. ui->addArgument->clicked();
  250. auto idx = ui->argType->currentIndex();
  251. if (idx != (ui->argType->count() - 1)) {
  252. ui->argType->setCurrentIndex(idx + 1);
  253. }
  254. }
  255. }
  256. }
  257. void ArgDialog::setFilePath(QString path) { m_file_path = path; }
  258. ArgDialog::~ArgDialog()
  259. {
  260. delete ui;
  261. }