TextSourceToolbar.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "TextSourceToolbar.hpp"
  2. #include "ui_text-source-toolbar.h"
  3. #include <OBSApp.hpp>
  4. #include <qt-wrappers.hpp>
  5. #include <QColorDialog>
  6. #include <QFontDialog>
  7. #include "moc_TextSourceToolbar.cpp"
  8. extern void MakeQFont(obs_data_t *font_obj, QFont &font, bool limit = false);
  9. extern QColor color_from_int(long long val);
  10. extern long long color_to_int(QColor color);
  11. TextSourceToolbar::TextSourceToolbar(QWidget *parent, OBSSource source)
  12. : SourceToolbar(parent, source),
  13. ui(new Ui_TextSourceToolbar)
  14. {
  15. ui->setupUi(this);
  16. OBSDataAutoRelease settings = obs_source_get_settings(source);
  17. const char *id = obs_source_get_unversioned_id(source);
  18. bool ft2 = strcmp(id, "text_ft2_source") == 0;
  19. bool read_from_file = obs_data_get_bool(settings, ft2 ? "from_file" : "read_from_file");
  20. OBSDataAutoRelease font_obj = obs_data_get_obj(settings, "font");
  21. MakeQFont(font_obj, font);
  22. // Use "color1" if it's a freetype source and "color" elsewise
  23. unsigned int val = (unsigned int)obs_data_get_int(
  24. settings, (strncmp(obs_source_get_id(source), "text_ft2_source", 15) == 0) ? "color1" : "color");
  25. color = color_from_int(val);
  26. const char *text = obs_data_get_string(settings, "text");
  27. bool single_line = !read_from_file && (!text || (strchr(text, '\n') == nullptr));
  28. ui->emptySpace->setVisible(!single_line);
  29. ui->text->setVisible(single_line);
  30. if (single_line)
  31. ui->text->setText(text);
  32. }
  33. TextSourceToolbar::~TextSourceToolbar() {}
  34. void TextSourceToolbar::on_selectFont_clicked()
  35. {
  36. OBSSource source = GetSource();
  37. if (!source) {
  38. return;
  39. }
  40. QFontDialog::FontDialogOptions options;
  41. uint32_t flags;
  42. bool success;
  43. #ifndef _WIN32
  44. options = QFontDialog::DontUseNativeDialog;
  45. #endif
  46. font = QFontDialog::getFont(&success, font, this, QTStr("Basic.PropertiesWindow.SelectFont.WindowTitle"),
  47. options);
  48. if (!success) {
  49. return;
  50. }
  51. OBSDataAutoRelease font_obj = obs_data_create();
  52. obs_data_set_string(font_obj, "face", QT_TO_UTF8(font.family()));
  53. obs_data_set_string(font_obj, "style", QT_TO_UTF8(font.styleName()));
  54. obs_data_set_int(font_obj, "size", font.pointSize());
  55. flags = font.bold() ? OBS_FONT_BOLD : 0;
  56. flags |= font.italic() ? OBS_FONT_ITALIC : 0;
  57. flags |= font.underline() ? OBS_FONT_UNDERLINE : 0;
  58. flags |= font.strikeOut() ? OBS_FONT_STRIKEOUT : 0;
  59. obs_data_set_int(font_obj, "flags", flags);
  60. SaveOldProperties(source);
  61. OBSDataAutoRelease settings = obs_data_create();
  62. obs_data_set_obj(settings, "font", font_obj);
  63. obs_source_update(source, settings);
  64. SetUndoProperties(source);
  65. }
  66. void TextSourceToolbar::on_selectColor_clicked()
  67. {
  68. OBSSource source = GetSource();
  69. if (!source) {
  70. return;
  71. }
  72. bool freetype = strncmp(obs_source_get_id(source), "text_ft2_source", 15) == 0;
  73. obs_property_t *p = obs_properties_get(props.get(), freetype ? "color1" : "color");
  74. const char *desc = obs_property_description(p);
  75. QColorDialog::ColorDialogOptions options;
  76. options |= QColorDialog::ShowAlphaChannel;
  77. #ifdef __linux__
  78. // TODO: Revisit hang on Ubuntu with native dialog
  79. options |= QColorDialog::DontUseNativeDialog;
  80. #endif
  81. QColor newColor = QColorDialog::getColor(color, this, desc, options);
  82. if (!newColor.isValid()) {
  83. return;
  84. }
  85. color = newColor;
  86. SaveOldProperties(source);
  87. OBSDataAutoRelease settings = obs_data_create();
  88. if (freetype) {
  89. obs_data_set_int(settings, "color1", color_to_int(color));
  90. obs_data_set_int(settings, "color2", color_to_int(color));
  91. } else {
  92. obs_data_set_int(settings, "color", color_to_int(color));
  93. }
  94. obs_source_update(source, settings);
  95. SetUndoProperties(source);
  96. }
  97. void TextSourceToolbar::on_text_textChanged()
  98. {
  99. OBSSource source = GetSource();
  100. if (!source) {
  101. return;
  102. }
  103. std::string newText = QT_TO_UTF8(ui->text->text());
  104. OBSDataAutoRelease settings = obs_source_get_settings(source);
  105. if (newText == obs_data_get_string(settings, "text")) {
  106. return;
  107. }
  108. SaveOldProperties(source);
  109. obs_data_set_string(settings, "text", newText.c_str());
  110. obs_source_update(source, nullptr);
  111. SetUndoProperties(source, true);
  112. }