Model.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/AbstractView.h>
  27. #include <LibGUI/Model.h>
  28. namespace GUI {
  29. Model::Model()
  30. {
  31. }
  32. Model::~Model()
  33. {
  34. }
  35. void Model::register_view(Badge<AbstractView>, AbstractView& view)
  36. {
  37. m_views.set(&view);
  38. m_clients.set(&view);
  39. }
  40. void Model::unregister_view(Badge<AbstractView>, AbstractView& view)
  41. {
  42. m_views.remove(&view);
  43. m_clients.remove(&view);
  44. }
  45. void Model::for_each_view(Function<void(AbstractView&)> callback)
  46. {
  47. for (auto* view : m_views)
  48. callback(*view);
  49. }
  50. void Model::did_update(unsigned flags)
  51. {
  52. for (auto* client : m_clients)
  53. client->model_did_update(flags);
  54. }
  55. ModelIndex Model::create_index(int row, int column, const void* data) const
  56. {
  57. return ModelIndex(*this, row, column, const_cast<void*>(data));
  58. }
  59. ModelIndex Model::index(int row, int column, const ModelIndex&) const
  60. {
  61. return create_index(row, column);
  62. }
  63. bool Model::accepts_drag(const ModelIndex&, const StringView&)
  64. {
  65. return false;
  66. }
  67. void Model::register_client(ModelClient& client)
  68. {
  69. m_clients.set(&client);
  70. }
  71. void Model::unregister_client(ModelClient& client)
  72. {
  73. m_clients.remove(&client);
  74. }
  75. RefPtr<Core::MimeData> Model::mime_data(const ModelSelection& selection) const
  76. {
  77. auto mime_data = Core::MimeData::construct();
  78. RefPtr<Gfx::Bitmap> bitmap;
  79. StringBuilder text_builder;
  80. StringBuilder data_builder;
  81. bool first = true;
  82. selection.for_each_index([&](auto& index) {
  83. auto text_data = index.data();
  84. if (!first)
  85. text_builder.append(", ");
  86. text_builder.append(text_data.to_string());
  87. if (!first)
  88. data_builder.append('\n');
  89. auto data = index.data(ModelRole::MimeData);
  90. data_builder.append(data.to_string());
  91. first = false;
  92. if (!bitmap) {
  93. Variant icon_data = index.data(ModelRole::Icon);
  94. if (icon_data.is_icon())
  95. bitmap = icon_data.as_icon().bitmap_for_size(32);
  96. }
  97. });
  98. mime_data->set_data(drag_data_type(), data_builder.to_byte_buffer());
  99. mime_data->set_text(text_builder.to_string());
  100. if (bitmap)
  101. mime_data->set_data("image/x-raw-bitmap", bitmap->serialize_to_byte_buffer());
  102. return mime_data;
  103. }
  104. }