window-basic-main-dropfiles.cpp 4.1 KB

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