window-basic-main-dropfiles.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. #ifdef _WIN32
  60. type = "text_gdiplus";
  61. #else
  62. type = "text_ft2_source";
  63. #endif
  64. break;
  65. case DropType_Text:
  66. #ifdef _WIN32
  67. obs_data_set_bool(settings, "read_from_file", true);
  68. obs_data_set_string(settings, "file", data);
  69. name = QUrl::fromLocalFile(QString(data)).fileName();
  70. type = "text_gdiplus";
  71. #else
  72. obs_data_set_bool(settings, "from_file", true);
  73. obs_data_set_string(settings, "text_file", data);
  74. type = "text_ft2_source";
  75. #endif
  76. break;
  77. case DropType_Image:
  78. obs_data_set_string(settings, "file", data);
  79. name = QUrl::fromLocalFile(QString(data)).fileName();
  80. type = "image_source";
  81. break;
  82. case DropType_Media:
  83. obs_data_set_string(settings, "local_file", data);
  84. name = QUrl::fromLocalFile(QString(data)).fileName();
  85. type = "ffmpeg_source";
  86. break;
  87. }
  88. if (name.isEmpty())
  89. name = obs_source_get_display_name(type);
  90. source = obs_source_create(type,
  91. GenerateSourceName(QT_TO_UTF8(name)).c_str(),
  92. settings, nullptr);
  93. if (source) {
  94. OBSScene scene = main->GetCurrentScene();
  95. obs_scene_add(scene, source);
  96. obs_source_release(source);
  97. }
  98. obs_data_release(settings);
  99. }
  100. void OBSBasic::dragEnterEvent(QDragEnterEvent *event)
  101. {
  102. event->acceptProposedAction();
  103. }
  104. void OBSBasic::dragLeaveEvent(QDragLeaveEvent *event)
  105. {
  106. event->accept();
  107. }
  108. void OBSBasic::dragMoveEvent(QDragMoveEvent *event)
  109. {
  110. event->acceptProposedAction();
  111. }
  112. void OBSBasic::dropEvent(QDropEvent *event)
  113. {
  114. const QMimeData* mimeData = event->mimeData();
  115. if (mimeData->hasUrls()) {
  116. QList<QUrl> urls = mimeData->urls();
  117. for (int i = 0; i < urls.size() && i < 5; i++) {
  118. QString file = urls.at(i).toLocalFile();
  119. QFileInfo fileInfo(file);
  120. if (!fileInfo.exists())
  121. continue;
  122. QString suffixQStr = fileInfo.suffix();
  123. QByteArray suffixArray = suffixQStr.toUtf8();
  124. const char *suffix = suffixArray.constData();
  125. bool found = false;
  126. const char **cmp;
  127. cmp = textExtensions;
  128. while (*cmp) {
  129. if (strcmp(*cmp, suffix) == 0) {
  130. AddDropSource(QT_TO_UTF8(file),
  131. DropType_Text);
  132. found = true;
  133. break;
  134. }
  135. cmp++;
  136. }
  137. if (found)
  138. continue;
  139. cmp = imageExtensions;
  140. while (*cmp) {
  141. if (strcmp(*cmp, suffix) == 0) {
  142. AddDropSource(QT_TO_UTF8(file),
  143. DropType_Image);
  144. found = true;
  145. break;
  146. }
  147. cmp++;
  148. }
  149. if (found)
  150. continue;
  151. cmp = mediaExtensions;
  152. while (*cmp) {
  153. if (strcmp(*cmp, suffix) == 0) {
  154. AddDropSource(QT_TO_UTF8(file),
  155. DropType_Media);
  156. break;
  157. }
  158. cmp++;
  159. }
  160. }
  161. } else if (mimeData->hasText()) {
  162. AddDropSource(QT_TO_UTF8(mimeData->text()), DropType_RawText);
  163. }
  164. }