window-basic-main-dropfiles.cpp 3.3 KB

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