QSWidget.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 <LibGfx/Bitmap.h>
  29. #include <LibGUI/GMessageBox.h>
  30. #include <LibGUI/GPainter.h>
  31. #include <LibGUI/GWindow.h>
  32. QSWidget::QSWidget(GUI::Widget* parent)
  33. : GUI::Frame(parent)
  34. {
  35. set_frame_shape(Gfx::FrameShape::Container);
  36. set_frame_shadow(Gfx::FrameShadow::Sunken);
  37. set_frame_thickness(2);
  38. set_fill_with_background_color(true);
  39. set_background_color(Color::Black);
  40. }
  41. QSWidget::~QSWidget()
  42. {
  43. }
  44. void QSWidget::set_bitmap(NonnullRefPtr<Gfx::Bitmap> bitmap)
  45. {
  46. m_bitmap = move(bitmap);
  47. }
  48. void QSWidget::relayout()
  49. {
  50. Gfx::Size new_size;
  51. float scale_factor = (float)m_scale / 100.0f;
  52. new_size.set_width(m_bitmap->width() * scale_factor);
  53. new_size.set_height(m_bitmap->height() * scale_factor);
  54. m_bitmap_rect.set_size(new_size);
  55. update();
  56. }
  57. void QSWidget::resize_event(GUI::ResizeEvent& event)
  58. {
  59. relayout();
  60. GUI::Widget::resize_event(event);
  61. }
  62. void QSWidget::paint_event(GUI::PaintEvent& event)
  63. {
  64. GUI::Painter painter(*this);
  65. painter.add_clip_rect(event.rect());
  66. painter.draw_scaled_bitmap(m_bitmap_rect, *m_bitmap, m_bitmap->rect());
  67. }
  68. void QSWidget::mousedown_event(GUI::MouseEvent& event)
  69. {
  70. if (event.button() != GUI::MouseButton::Left)
  71. return;
  72. m_pan_origin = event.position();
  73. m_pan_bitmap_origin = m_bitmap_rect.location();
  74. }
  75. void QSWidget::mouseup_event(GUI::MouseEvent& event)
  76. {
  77. UNUSED_PARAM(event);
  78. }
  79. void QSWidget::mousemove_event(GUI::MouseEvent& event)
  80. {
  81. if (!(event.buttons() & GUI::MouseButton::Left))
  82. return;
  83. auto delta = event.position() - m_pan_origin;
  84. m_bitmap_rect.set_location(m_pan_bitmap_origin.translated(delta));
  85. update();
  86. }
  87. void QSWidget::mousewheel_event(GUI::MouseEvent& event)
  88. {
  89. auto old_scale = m_scale;
  90. auto old_scale_factor = (float)m_scale / 100.0f;
  91. auto zoom_point = event.position().translated(-m_bitmap_rect.location());
  92. zoom_point.set_x((float)zoom_point.x() / old_scale_factor);
  93. zoom_point.set_y((float)zoom_point.y() / old_scale_factor);
  94. m_scale += -event.wheel_delta() * 10;
  95. if (m_scale < 10)
  96. m_scale = 10;
  97. if (m_scale > 1000)
  98. m_scale = 1000;
  99. relayout();
  100. auto new_scale_factor = (float)m_scale / 100.0f;
  101. auto scale_factor_change = new_scale_factor - old_scale_factor;
  102. m_bitmap_rect.move_by(-Gfx::Point((float)zoom_point.x() * scale_factor_change, (float)zoom_point.y() * scale_factor_change));
  103. if (old_scale != m_scale) {
  104. if (on_scale_change)
  105. on_scale_change(m_scale);
  106. }
  107. }
  108. void QSWidget::set_path(const String& path)
  109. {
  110. m_path = path;
  111. }
  112. void QSWidget::drop_event(GUI::DropEvent& event)
  113. {
  114. event.accept();
  115. window()->move_to_front();
  116. if (event.data_type() == "url-list") {
  117. auto lines = event.data().split_view('\n');
  118. if (lines.is_empty())
  119. return;
  120. if (lines.size() > 1) {
  121. 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());
  122. return;
  123. }
  124. URL url(lines[0]);
  125. auto bitmap = Gfx::Bitmap::load_from_file(url.path());
  126. if (!bitmap) {
  127. 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());
  128. return;
  129. }
  130. m_path = url.path();
  131. m_bitmap = bitmap;
  132. m_scale = 100;
  133. if (on_scale_change)
  134. on_scale_change(m_scale);
  135. relayout();
  136. m_bitmap_rect.center_within(rect());
  137. }
  138. }