window-basic-main-dropfiles.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include <QDragEnterEvent>
  2. #include <QDragLeaveEvent>
  3. #include <QDragMoveEvent>
  4. #include <QDropEvent>
  5. #include <QFileInfo>
  6. #include <QMimeData>
  7. #include <string>
  8. #include "window-basic-main.hpp"
  9. #include "qt-wrappers.hpp"
  10. using namespace std;
  11. static const char *textExtensions[] = {
  12. "txt", "log", nullptr
  13. };
  14. static const char *imageExtensions[] = {
  15. "bmp", "tga", "png", "jpg", "jpeg", "gif", nullptr
  16. };
  17. static const char *mediaExtensions[] = {
  18. "3ga", "669", "a52", "aac", "ac3", "adt", "adts", "aif", "aifc",
  19. "aiff", "amb", "amr", "aob", "ape", "au", "awb", "caf", "dts",
  20. "flac", "it", "kar", "m4a", "m4b", "m4p", "m5p", "mid", "mka",
  21. "mlp", "mod", "mpa", "mp1", "mp2", "mp3", "mpc", "mpga", "mus",
  22. "oga", "ogg", "oma", "opus", "qcp", "ra", "rmi", "s3m", "sid",
  23. "spx", "tak", "thd", "tta", "voc", "vqf", "w64", "wav", "wma",
  24. "wv", "xa", "xm" "3g2", "3gp", "3gp2", "3gpp", "amv", "asf", "avi",
  25. "bik", "crf", "divx", "drc", "dv", "evo", "f4v", "flv", "gvi",
  26. "gxf", "iso", "m1v", "m2v", "m2t", "m2ts", "m4v", "mkv", "mov",
  27. "mp2", "mp2v", "mp4", "mp4v", "mpe", "mpeg", "mpeg1", "mpeg2",
  28. "mpeg4", "mpg", "mpv2", "mts", "mtv", "mxf", "mxg", "nsv", "nuv",
  29. "ogg", "ogm", "ogv", "ogx", "ps", "rec", "rm", "rmvb", "rpl", "thp",
  30. "tod", "ts", "tts", "txd", "vob", "vro", "webm", "wm", "wmv", "wtv",
  31. nullptr
  32. };
  33. static string GenerateSourceName(const char *base)
  34. {
  35. string name;
  36. int inc = 0;
  37. for (;; inc++) {
  38. name = base;
  39. if (inc) {
  40. name += " (";
  41. name += to_string(inc+1);
  42. name += ")";
  43. }
  44. obs_source_t *source = obs_get_source_by_name(name.c_str());
  45. if (!source)
  46. return name;
  47. }
  48. }
  49. void OBSBasic::AddDropSource(const char *data, DropType image)
  50. {
  51. OBSBasic *main = reinterpret_cast<OBSBasic*>(App()->GetMainWindow());
  52. obs_data_t *settings = obs_data_create();
  53. obs_source_t *source = nullptr;
  54. const char *type = nullptr;
  55. QString name;
  56. switch (image) {
  57. case DropType_RawText:
  58. obs_data_set_string(settings, "text", data);
  59. type = "text_gdiplus";
  60. break;
  61. case DropType_Text:
  62. obs_data_set_bool(settings, "read_from_file", true);
  63. obs_data_set_string(settings, "file", data);
  64. name = QUrl::fromLocalFile(QString(data)).fileName();
  65. type = "text_gdiplus";
  66. break;
  67. case DropType_Image:
  68. obs_data_set_string(settings, "file", data);
  69. name = QUrl::fromLocalFile(QString(data)).fileName();
  70. type = "image_source";
  71. break;
  72. case DropType_Media:
  73. obs_data_set_string(settings, "local_file", data);
  74. name = QUrl::fromLocalFile(QString(data)).fileName();
  75. type = "ffmpeg_source";
  76. break;
  77. }
  78. if (name.isEmpty())
  79. name = obs_source_get_display_name(type);
  80. source = obs_source_create(type,
  81. GenerateSourceName(QT_TO_UTF8(name)).c_str(),
  82. settings, nullptr);
  83. if (source) {
  84. OBSScene scene = main->GetCurrentScene();
  85. obs_scene_add(scene, source);
  86. obs_source_release(source);
  87. }
  88. obs_data_release(settings);
  89. }
  90. void OBSBasic::dragEnterEvent(QDragEnterEvent *event)
  91. {
  92. event->acceptProposedAction();
  93. }
  94. void OBSBasic::dragLeaveEvent(QDragLeaveEvent *event)
  95. {
  96. event->accept();
  97. }
  98. void OBSBasic::dragMoveEvent(QDragMoveEvent *event)
  99. {
  100. event->acceptProposedAction();
  101. }
  102. void OBSBasic::dropEvent(QDropEvent *event)
  103. {
  104. const QMimeData* mimeData = event->mimeData();
  105. if (mimeData->hasUrls()) {
  106. QList<QUrl> urls = mimeData->urls();
  107. for (int i = 0; i < urls.size() && i < 5; i++) {
  108. QString file = urls.at(i).toLocalFile();
  109. QFileInfo fileInfo(file);
  110. if (!fileInfo.exists())
  111. continue;
  112. QString suffixQStr = fileInfo.suffix();
  113. QByteArray suffixArray = suffixQStr.toUtf8();
  114. const char *suffix = suffixArray.constData();
  115. bool found = false;
  116. const char **cmp;
  117. cmp = textExtensions;
  118. while (*cmp) {
  119. if (strcmp(*cmp, suffix) == 0) {
  120. AddDropSource(QT_TO_UTF8(file),
  121. DropType_Text);
  122. found = true;
  123. break;
  124. }
  125. cmp++;
  126. }
  127. if (found)
  128. continue;
  129. cmp = imageExtensions;
  130. while (*cmp) {
  131. if (strcmp(*cmp, suffix) == 0) {
  132. AddDropSource(QT_TO_UTF8(file),
  133. DropType_Image);
  134. found = true;
  135. break;
  136. }
  137. cmp++;
  138. }
  139. if (found)
  140. continue;
  141. cmp = mediaExtensions;
  142. while (*cmp) {
  143. if (strcmp(*cmp, suffix) == 0) {
  144. AddDropSource(QT_TO_UTF8(file),
  145. DropType_Media);
  146. break;
  147. }
  148. cmp++;
  149. }
  150. }
  151. } else if (mimeData->hasText()) {
  152. AddDropSource(QT_TO_UTF8(mimeData->text()), DropType_RawText);
  153. }
  154. }