QSWidget.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 "QSWidget.h"
  27. #include <AK/URL.h>
  28. #include <LibCore/MimeData.h>
  29. #include <LibGUI/MessageBox.h>
  30. #include <LibGUI/Painter.h>
  31. #include <LibGUI/Window.h>
  32. #include <LibGfx/Bitmap.h>
  33. QSWidget::QSWidget()
  34. {
  35. set_fill_with_background_color(true);
  36. set_background_color(Color::Black);
  37. }
  38. QSWidget::~QSWidget()
  39. {
  40. }
  41. void QSWidget::set_bitmap(NonnullRefPtr<Gfx::Bitmap> bitmap)
  42. {
  43. m_bitmap = move(bitmap);
  44. }
  45. void QSWidget::relayout()
  46. {
  47. Gfx::Size new_size;
  48. float scale_factor = (float)m_scale / 100.0f;
  49. new_size.set_width(m_bitmap->width() * scale_factor);
  50. new_size.set_height(m_bitmap->height() * scale_factor);
  51. m_bitmap_rect.set_size(new_size);
  52. update();
  53. }
  54. void QSWidget::resize_event(GUI::ResizeEvent& event)
  55. {
  56. relayout();
  57. GUI::Widget::resize_event(event);
  58. }
  59. void QSWidget::paint_event(GUI::PaintEvent& event)
  60. {
  61. GUI::Painter painter(*this);
  62. painter.add_clip_rect(event.rect());
  63. painter.draw_scaled_bitmap(m_bitmap_rect, *m_bitmap, m_bitmap->rect());
  64. }
  65. void QSWidget::mousedown_event(GUI::MouseEvent& event)
  66. {
  67. if (event.button() != GUI::MouseButton::Left)
  68. return;
  69. m_pan_origin = event.position();
  70. m_pan_bitmap_origin = m_bitmap_rect.location();
  71. }
  72. void QSWidget::mouseup_event(GUI::MouseEvent& event)
  73. {
  74. UNUSED_PARAM(event);
  75. }
  76. void QSWidget::mousemove_event(GUI::MouseEvent& event)
  77. {
  78. if (!(event.buttons() & GUI::MouseButton::Left))
  79. return;
  80. auto delta = event.position() - m_pan_origin;
  81. m_bitmap_rect.set_location(m_pan_bitmap_origin.translated(delta));
  82. update();
  83. }
  84. void QSWidget::mousewheel_event(GUI::MouseEvent& event)
  85. {
  86. auto old_scale = m_scale;
  87. auto old_scale_factor = (float)m_scale / 100.0f;
  88. auto zoom_point = event.position().translated(-m_bitmap_rect.location());
  89. zoom_point.set_x((float)zoom_point.x() / old_scale_factor);
  90. zoom_point.set_y((float)zoom_point.y() / old_scale_factor);
  91. m_scale += -event.wheel_delta() * 10;
  92. if (m_scale < 10)
  93. m_scale = 10;
  94. if (m_scale > 1000)
  95. m_scale = 1000;
  96. relayout();
  97. auto new_scale_factor = (float)m_scale / 100.0f;
  98. auto scale_factor_change = new_scale_factor - old_scale_factor;
  99. m_bitmap_rect.move_by(-Gfx::Point((float)zoom_point.x() * scale_factor_change, (float)zoom_point.y() * scale_factor_change));
  100. if (old_scale != m_scale) {
  101. if (on_scale_change)
  102. on_scale_change(m_scale);
  103. }
  104. }
  105. void QSWidget::set_path(const String& path)
  106. {
  107. m_path = path;
  108. }
  109. void QSWidget::drop_event(GUI::DropEvent& event)
  110. {
  111. event.accept();
  112. window()->move_to_front();
  113. if (event.mime_data().has_urls()) {
  114. auto urls = event.mime_data().urls();
  115. if (urls.is_empty())
  116. return;
  117. if (urls.size() > 1) {
  118. GUI::MessageBox::show("QuickShow can only open one file at a time!", "One at a time please!", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window());
  119. return;
  120. }
  121. auto url = urls.first();
  122. auto bitmap = Gfx::Bitmap::load_from_file(url.path());
  123. if (!bitmap) {
  124. GUI::MessageBox::show(String::format("Failed to open %s", url.to_string().characters()), "Cannot open image", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window());
  125. return;
  126. }
  127. m_path = url.path();
  128. m_bitmap = bitmap;
  129. m_scale = 100;
  130. if (on_scale_change)
  131. on_scale_change(m_scale);
  132. relayout();
  133. m_bitmap_rect.center_within(rect());
  134. }
  135. }