2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-03-23 05:53:51 +03:00
|
|
|
#pragma once
|
|
|
|
|
2020-07-11 16:02:03 +03:00
|
|
|
#include <AK/IterationDecision.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/AbstractView.h>
|
2020-02-16 11:17:49 +03:00
|
|
|
#include <LibGUI/Forward.h>
|
2020-07-11 16:02:03 +03:00
|
|
|
#include <LibGUI/Variant.h>
|
2019-03-23 05:53:51 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
namespace GUI {
|
|
|
|
|
2020-05-01 03:09:04 +03:00
|
|
|
class IconView : public AbstractView {
|
|
|
|
C_OBJECT(IconView)
|
2019-03-23 05:53:51 +03:00
|
|
|
public:
|
2020-05-01 03:09:04 +03:00
|
|
|
virtual ~IconView() override;
|
2019-03-23 05:53:51 +03:00
|
|
|
|
2020-12-27 16:44:49 +03:00
|
|
|
enum class FlowDirection {
|
|
|
|
LeftToRight,
|
|
|
|
TopToBottom,
|
|
|
|
};
|
|
|
|
|
|
|
|
FlowDirection flow_direction() const { return m_flow_direction; }
|
|
|
|
void set_flow_direction(FlowDirection);
|
|
|
|
|
2019-03-23 05:53:51 +03:00
|
|
|
int horizontal_padding() const { return m_horizontal_padding; }
|
|
|
|
|
2020-09-01 16:41:56 +03:00
|
|
|
virtual void scroll_into_view(const ModelIndex&, bool scroll_horizontally = true, bool scroll_vertically = true) override;
|
|
|
|
|
2020-06-10 11:57:59 +03:00
|
|
|
Gfx::IntSize effective_item_size() const { return m_effective_item_size; }
|
2019-03-23 05:53:51 +03:00
|
|
|
|
2021-01-29 08:26:04 +03:00
|
|
|
bool always_wrap_item_labels() const { return m_always_wrap_item_labels; }
|
|
|
|
void set_always_wrap_item_labels(bool value) { m_always_wrap_item_labels = value; }
|
|
|
|
|
2019-03-23 05:53:51 +03:00
|
|
|
int model_column() const { return m_model_column; }
|
|
|
|
void set_model_column(int column) { m_model_column = column; }
|
|
|
|
|
2020-06-10 11:57:59 +03:00
|
|
|
virtual ModelIndex index_at_event_position(const Gfx::IntPoint&) const override;
|
2020-09-24 12:40:19 +03:00
|
|
|
virtual Gfx::IntRect content_rect(const ModelIndex&) const override;
|
2020-01-22 18:13:01 +03:00
|
|
|
|
2020-02-25 18:03:15 +03:00
|
|
|
virtual void select_all() override;
|
2020-07-11 16:02:03 +03:00
|
|
|
|
2019-03-23 05:53:51 +03:00
|
|
|
private:
|
2020-05-01 03:09:04 +03:00
|
|
|
IconView();
|
2019-09-21 16:47:58 +03:00
|
|
|
|
2020-11-26 10:40:14 +03:00
|
|
|
virtual void model_did_update(unsigned flags) override;
|
2020-02-02 17:07:41 +03:00
|
|
|
virtual void paint_event(PaintEvent&) override;
|
|
|
|
virtual void second_paint_event(PaintEvent&) override;
|
|
|
|
virtual void resize_event(ResizeEvent&) override;
|
|
|
|
virtual void mousedown_event(MouseEvent&) override;
|
|
|
|
virtual void mousemove_event(MouseEvent&) override;
|
|
|
|
virtual void mouseup_event(MouseEvent&) override;
|
2020-10-27 18:10:30 +03:00
|
|
|
virtual void did_change_hovered_index(const ModelIndex& old_index, const ModelIndex& new_index) override;
|
|
|
|
virtual void did_change_cursor_index(const ModelIndex& old_index, const ModelIndex& new_index) override;
|
2019-03-23 05:53:51 +03:00
|
|
|
|
2020-09-01 15:43:16 +03:00
|
|
|
virtual void move_cursor(CursorMovement, SelectionUpdate) override;
|
|
|
|
|
2020-07-11 16:02:03 +03:00
|
|
|
struct ItemData {
|
|
|
|
Gfx::IntRect text_rect;
|
|
|
|
Gfx::IntRect icon_rect;
|
|
|
|
int icon_offset_y;
|
|
|
|
int text_offset_y;
|
2020-10-27 18:10:30 +03:00
|
|
|
String text;
|
|
|
|
Vector<StringView> wrapped_text_lines;
|
2020-07-11 16:02:03 +03:00
|
|
|
ModelIndex index;
|
|
|
|
bool valid { false };
|
|
|
|
bool selected { false }; // always valid
|
|
|
|
bool selection_toggled; // only used as a temporary marker
|
|
|
|
|
|
|
|
bool is_valid() const { return valid; }
|
|
|
|
void invalidate()
|
|
|
|
{
|
|
|
|
valid = false;
|
2020-10-27 18:10:30 +03:00
|
|
|
text = {};
|
2020-07-11 16:02:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool is_intersecting(const Gfx::IntRect& rect) const
|
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(valid);
|
2020-07-11 16:02:03 +03:00
|
|
|
return icon_rect.intersects(rect) || text_rect.intersects(rect);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_containing(const Gfx::IntPoint& point) const
|
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(valid);
|
2020-07-11 16:02:03 +03:00
|
|
|
return icon_rect.contains(point) || text_rect.contains(point);
|
|
|
|
}
|
2021-02-16 05:30:47 +03:00
|
|
|
|
|
|
|
Gfx::IntRect rect() const
|
|
|
|
{
|
|
|
|
return text_rect.united(icon_rect);
|
|
|
|
}
|
2020-07-11 16:02:03 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Function>
|
2020-12-27 13:34:38 +03:00
|
|
|
IterationDecision for_each_item_intersecting_rect(const Gfx::IntRect&, Function) const;
|
2020-07-11 16:02:03 +03:00
|
|
|
|
|
|
|
template<typename Function>
|
2020-12-27 13:34:38 +03:00
|
|
|
IterationDecision for_each_item_intersecting_rects(const Vector<Gfx::IntRect>&, Function) const;
|
2020-07-11 16:02:03 +03:00
|
|
|
|
|
|
|
void column_row_from_content_position(const Gfx::IntPoint& content_position, int& row, int& column) const
|
|
|
|
{
|
|
|
|
row = max(0, min(m_visual_row_count - 1, content_position.y() / effective_item_size().height()));
|
|
|
|
column = max(0, min(m_visual_column_count - 1, content_position.x() / effective_item_size().width()));
|
|
|
|
}
|
|
|
|
|
2019-03-23 05:53:51 +03:00
|
|
|
int item_count() const;
|
2020-06-10 11:57:59 +03:00
|
|
|
Gfx::IntRect item_rect(int item_index) const;
|
2019-03-23 05:53:51 +03:00
|
|
|
void update_content_size();
|
2020-07-11 16:02:03 +03:00
|
|
|
void update_item_rects(int item_index, ItemData& item_data) const;
|
|
|
|
void get_item_rects(int item_index, ItemData& item_data, const Gfx::Font&) const;
|
2020-07-08 20:42:32 +03:00
|
|
|
bool update_rubber_banding(const Gfx::IntPoint&);
|
|
|
|
void scroll_out_of_view_timer_fired();
|
2020-12-27 16:44:49 +03:00
|
|
|
int items_per_page() const;
|
2019-03-23 05:53:51 +03:00
|
|
|
|
2021-04-23 18:40:10 +03:00
|
|
|
void rebuild_item_cache() const;
|
2020-07-11 16:02:03 +03:00
|
|
|
int model_index_to_item_index(const ModelIndex& model_index) const
|
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(model_index.row() < item_count());
|
2020-07-11 16:02:03 +03:00
|
|
|
return model_index.row();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void did_update_selection() override;
|
|
|
|
virtual void clear_selection() override;
|
|
|
|
virtual void add_selection(const ModelIndex& new_index) override;
|
|
|
|
virtual void set_selection(const ModelIndex& new_index) override;
|
|
|
|
virtual void toggle_selection(const ModelIndex& new_index) override;
|
|
|
|
|
|
|
|
ItemData& get_item_data(int) const;
|
|
|
|
ItemData* item_data_from_content_position(const Gfx::IntPoint&) const;
|
|
|
|
void do_clear_selection();
|
|
|
|
bool do_add_selection(ItemData&);
|
|
|
|
void add_selection(ItemData&);
|
|
|
|
void remove_selection(ItemData&);
|
|
|
|
void toggle_selection(ItemData&);
|
|
|
|
|
2019-03-23 05:53:51 +03:00
|
|
|
int m_horizontal_padding { 5 };
|
|
|
|
int m_model_column { 0 };
|
|
|
|
int m_visual_column_count { 0 };
|
|
|
|
int m_visual_row_count { 0 };
|
|
|
|
|
2020-06-10 11:57:59 +03:00
|
|
|
Gfx::IntSize m_effective_item_size { 80, 80 };
|
2020-01-04 23:18:48 +03:00
|
|
|
|
2021-01-29 08:26:04 +03:00
|
|
|
bool m_always_wrap_item_labels { false };
|
|
|
|
|
2020-01-04 23:18:48 +03:00
|
|
|
bool m_rubber_banding { false };
|
2020-07-08 20:42:32 +03:00
|
|
|
RefPtr<Core::Timer> m_out_of_view_timer;
|
|
|
|
Gfx::IntPoint m_out_of_view_position;
|
2020-06-10 11:57:59 +03:00
|
|
|
Gfx::IntPoint m_rubber_band_origin;
|
|
|
|
Gfx::IntPoint m_rubber_band_current;
|
2020-02-13 23:50:50 +03:00
|
|
|
|
2020-12-27 16:44:49 +03:00
|
|
|
FlowDirection m_flow_direction { FlowDirection::LeftToRight };
|
|
|
|
|
2020-07-11 16:02:03 +03:00
|
|
|
mutable Vector<ItemData> m_item_data_cache;
|
|
|
|
mutable int m_selected_count_cache { 0 };
|
|
|
|
mutable int m_first_selected_hint { 0 };
|
|
|
|
mutable bool m_item_data_cache_valid { false };
|
|
|
|
|
|
|
|
bool m_changing_selection { false };
|
2021-03-02 00:32:27 +03:00
|
|
|
|
|
|
|
bool m_had_valid_size { false };
|
2019-03-23 05:53:51 +03:00
|
|
|
};
|
2020-02-02 17:07:41 +03:00
|
|
|
|
|
|
|
}
|