Serenity Operating System
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
27#include <AK/StringBuilder.h>
28#include <Kernel/KeyCode.h>
29#include <LibGfx/Palette.h>
30#include <LibGUI/DragOperation.h>
31#include <LibGUI/ItemView.h>
32#include <LibGUI/Model.h>
33#include <LibGUI/Painter.h>
34#include <LibGUI/ScrollBar.h>
35
36//#define DRAGDROP_DEBUG
37
38namespace GUI {
39
40ItemView::ItemView()
41{
42 set_background_role(ColorRole::Base);
43 set_foreground_role(ColorRole::BaseText);
44 horizontal_scrollbar().set_visible(false);
45}
46
47ItemView::~ItemView()
48{
49}
50
51void ItemView::select_all()
52{
53 selection().clear();
54 for (int item_index = 0; item_index < item_count(); ++item_index) {
55 auto index = model()->index(item_index, model_column());
56 selection().add(index);
57 }
58}
59
60void ItemView::scroll_into_view(const ModelIndex& index, Orientation orientation)
61{
62 ScrollableWidget::scroll_into_view(item_rect(index.row()), orientation);
63}
64
65void ItemView::resize_event(ResizeEvent& event)
66{
67 AbstractView::resize_event(event);
68 update_content_size();
69}
70
71void ItemView::did_update_model()
72{
73 AbstractView::did_update_model();
74 update_content_size();
75 update();
76}
77
78void ItemView::update_content_size()
79{
80 if (!model())
81 return set_content_size({});
82
83 m_visual_column_count = available_size().width() / effective_item_size().width();
84 if (m_visual_column_count)
85 m_visual_row_count = ceil_div(model()->row_count(), m_visual_column_count);
86 else
87 m_visual_row_count = 0;
88
89 int content_width = available_size().width();
90 int content_height = m_visual_row_count * effective_item_size().height();
91
92 set_content_size({ content_width, content_height });
93}
94
95Gfx::Rect ItemView::item_rect(int item_index) const
96{
97 if (!m_visual_row_count || !m_visual_column_count)
98 return {};
99 int visual_row_index = item_index / m_visual_column_count;
100 int visual_column_index = item_index % m_visual_column_count;
101 return {
102 visual_column_index * effective_item_size().width(),
103 visual_row_index * effective_item_size().height(),
104 effective_item_size().width(),
105 effective_item_size().height()
106 };
107}
108
109Vector<int> ItemView::items_intersecting_rect(const Gfx::Rect& rect) const
110{
111 ASSERT(model());
112 const auto& column_metadata = model()->column_metadata(model_column());
113 const auto& font = column_metadata.font ? *column_metadata.font : this->font();
114 Vector<int> item_indexes;
115 for (int item_index = 0; item_index < item_count(); ++item_index) {
116 Gfx::Rect item_rect;
117 Gfx::Rect icon_rect;
118 Gfx::Rect text_rect;
119 auto item_text = model()->data(model()->index(item_index, model_column()));
120 get_item_rects(item_index, font, item_text, item_rect, icon_rect, text_rect);
121 if (icon_rect.intersects(rect) || text_rect.intersects(rect))
122 item_indexes.append(item_index);
123 }
124 return item_indexes;
125}
126
127ModelIndex ItemView::index_at_event_position(const Gfx::Point& position) const
128{
129 ASSERT(model());
130 // FIXME: Since all items are the same size, just compute the clicked item index
131 // instead of iterating over everything.
132 auto adjusted_position = position.translated(0, vertical_scrollbar().value());
133 const auto& column_metadata = model()->column_metadata(model_column());
134 const auto& font = column_metadata.font ? *column_metadata.font : this->font();
135 for (int item_index = 0; item_index < item_count(); ++item_index) {
136 Gfx::Rect item_rect;
137 Gfx::Rect icon_rect;
138 Gfx::Rect text_rect;
139 auto index = model()->index(item_index, model_column());
140 auto item_text = model()->data(index);
141 get_item_rects(item_index, font, item_text, item_rect, icon_rect, text_rect);
142 if (icon_rect.contains(adjusted_position) || text_rect.contains(adjusted_position))
143 return index;
144 }
145 return {};
146}
147
148void ItemView::mousedown_event(MouseEvent& event)
149{
150 if (!model())
151 return AbstractView::mousedown_event(event);
152
153 if (event.button() != MouseButton::Left)
154 return AbstractView::mousedown_event(event);
155
156 auto index = index_at_event_position(event.position());
157 if (index.is_valid()) {
158 // We might start dragging this item, but not rubber-banding.
159 return AbstractView::mousedown_event(event);
160 }
161
162 ASSERT(m_rubber_band_remembered_selection.is_empty());
163
164 if (event.modifiers() & Mod_Ctrl) {
165 selection().for_each_index([&](auto& index) {
166 m_rubber_band_remembered_selection.append(index);
167 });
168 } else {
169 selection().clear();
170 }
171
172 m_might_drag = false;
173 m_rubber_banding = true;
174 m_rubber_band_origin = event.position();
175 m_rubber_band_current = event.position();
176}
177
178void ItemView::mouseup_event(MouseEvent& event)
179{
180 if (m_rubber_banding && event.button() == MouseButton::Left) {
181 m_rubber_banding = false;
182 m_rubber_band_remembered_selection.clear();
183 update();
184 }
185 AbstractView::mouseup_event(event);
186}
187
188void ItemView::drag_move_event(DragEvent& event)
189{
190 auto index = index_at_event_position(event.position());
191 ModelIndex new_drop_candidate_index;
192 if (index.is_valid()) {
193 bool acceptable = model()->accepts_drag(index, event.data_type());
194#ifdef DRAGDROP_DEBUG
195 dbg() << "Drag of type '" << event.data_type() << "' moving over " << index << ", acceptable: " << acceptable;
196#endif
197 if (acceptable)
198 new_drop_candidate_index = index;
199 }
200 if (m_drop_candidate_index != new_drop_candidate_index) {
201 m_drop_candidate_index = new_drop_candidate_index;
202 update();
203 }
204 event.accept();
205}
206
207void ItemView::mousemove_event(MouseEvent& event)
208{
209 if (!model())
210 return AbstractView::mousemove_event(event);
211
212 if (m_rubber_banding) {
213 if (m_rubber_band_current != event.position()) {
214 m_rubber_band_current = event.position();
215 auto rubber_band_rect = Gfx::Rect::from_two_points(m_rubber_band_origin, m_rubber_band_current);
216 selection().clear();
217 for (auto item_index : items_intersecting_rect(rubber_band_rect)) {
218 selection().add(model()->index(item_index, model_column()));
219 }
220 if (event.modifiers() & Mod_Ctrl) {
221 for (auto stored_item : m_rubber_band_remembered_selection) {
222 selection().add(stored_item);
223 }
224 }
225 update();
226 return;
227 }
228 }
229
230 AbstractView::mousemove_event(event);
231}
232
233void ItemView::get_item_rects(int item_index, const Gfx::Font& font, const Variant& item_text, Gfx::Rect& item_rect, Gfx::Rect& icon_rect, Gfx::Rect& text_rect) const
234{
235 item_rect = this->item_rect(item_index);
236 icon_rect = { 0, 0, 32, 32 };
237 icon_rect.center_within(item_rect);
238 icon_rect.move_by(0, -font.glyph_height() - 6);
239 text_rect = { 0, icon_rect.bottom() + 6 + 1, font.width(item_text.to_string()), font.glyph_height() };
240 text_rect.center_horizontally_within(item_rect);
241 text_rect.inflate(6, 4);
242 text_rect.intersect(item_rect);
243}
244
245void ItemView::second_paint_event(PaintEvent& event)
246{
247 if (!m_rubber_banding)
248 return;
249
250 Painter painter(*this);
251 painter.add_clip_rect(event.rect());
252
253 auto rubber_band_rect = Gfx::Rect::from_two_points(m_rubber_band_origin, m_rubber_band_current);
254 painter.fill_rect(rubber_band_rect, parent_widget()->palette().rubber_band_fill());
255 painter.draw_rect(rubber_band_rect, parent_widget()->palette().rubber_band_border());
256}
257
258void ItemView::paint_event(PaintEvent& event)
259{
260 Color widget_background_color = palette().color(background_role());
261 Frame::paint_event(event);
262
263 Painter painter(*this);
264 painter.add_clip_rect(widget_inner_rect());
265 painter.add_clip_rect(event.rect());
266 painter.fill_rect(event.rect(), widget_background_color);
267 painter.translate(frame_thickness(), frame_thickness());
268 painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
269
270 auto column_metadata = model()->column_metadata(m_model_column);
271 const Gfx::Font& font = column_metadata.font ? *column_metadata.font : this->font();
272
273 for (int item_index = 0; item_index < model()->row_count(); ++item_index) {
274 auto model_index = model()->index(item_index, m_model_column);
275 bool is_selected_item = selection().contains(model_index);
276 Color background_color;
277 if (is_selected_item) {
278 background_color = is_focused() ? palette().selection() : palette().inactive_selection();
279 } else {
280 background_color = widget_background_color;
281 }
282
283 auto icon = model()->data(model_index, Model::Role::Icon);
284 auto item_text = model()->data(model_index, Model::Role::Display);
285
286 Gfx::Rect item_rect;
287 Gfx::Rect icon_rect;
288 Gfx::Rect text_rect;
289 get_item_rects(item_index, font, item_text, item_rect, icon_rect, text_rect);
290
291 if (icon.is_icon()) {
292 if (auto bitmap = icon.as_icon().bitmap_for_size(icon_rect.width()))
293 painter.draw_scaled_bitmap(icon_rect, *bitmap, bitmap->rect());
294 }
295
296 Color text_color;
297 if (is_selected_item)
298 text_color = is_focused() ? palette().selection_text() : palette().inactive_selection_text();
299 else
300 text_color = model()->data(model_index, Model::Role::ForegroundColor).to_color(palette().color(foreground_role()));
301 painter.fill_rect(text_rect, background_color);
302 painter.draw_text(text_rect, item_text.to_string(), font, Gfx::TextAlignment::Center, text_color, Gfx::TextElision::Right);
303
304 if (model_index == m_drop_candidate_index) {
305 // FIXME: This visualization is not great, as it's also possible to drop things on the text label..
306 painter.draw_rect(icon_rect.inflated(8, 8), palette().selection(), true);
307 }
308 };
309}
310
311int ItemView::item_count() const
312{
313 if (!model())
314 return 0;
315 return model()->row_count();
316}
317
318void ItemView::keydown_event(KeyEvent& event)
319{
320 if (!model())
321 return;
322 if (!m_visual_row_count || !m_visual_column_count)
323 return;
324
325 auto& model = *this->model();
326 if (event.key() == KeyCode::Key_Return) {
327 activate_selected();
328 return;
329 }
330 if (event.key() == KeyCode::Key_Home) {
331 auto new_index = model.index(0, 0);
332 if (model.is_valid(new_index)) {
333 selection().set(new_index);
334 scroll_into_view(new_index, Orientation::Vertical);
335 update();
336 }
337 return;
338 }
339 if (event.key() == KeyCode::Key_End) {
340 auto new_index = model.index(model.row_count() - 1, 0);
341 if (model.is_valid(new_index)) {
342 selection().set(new_index);
343 scroll_into_view(new_index, Orientation::Vertical);
344 update();
345 }
346 return;
347 }
348 if (event.key() == KeyCode::Key_Up) {
349 ModelIndex new_index;
350 if (!selection().is_empty()) {
351 auto old_index = selection().first();
352 new_index = model.index(old_index.row() - m_visual_column_count, old_index.column());
353 } else {
354 new_index = model.index(0, 0);
355 }
356 if (model.is_valid(new_index)) {
357 selection().set(new_index);
358 scroll_into_view(new_index, Orientation::Vertical);
359 update();
360 }
361 return;
362 }
363 if (event.key() == KeyCode::Key_Down) {
364 ModelIndex new_index;
365 if (!selection().is_empty()) {
366 auto old_index = selection().first();
367 new_index = model.index(old_index.row() + m_visual_column_count, old_index.column());
368 } else {
369 new_index = model.index(0, 0);
370 }
371 if (model.is_valid(new_index)) {
372 selection().set(new_index);
373 scroll_into_view(new_index, Orientation::Vertical);
374 update();
375 }
376 return;
377 }
378 if (event.key() == KeyCode::Key_Left) {
379 ModelIndex new_index;
380 if (!selection().is_empty()) {
381 auto old_index = selection().first();
382 new_index = model.index(old_index.row() - 1, old_index.column());
383 } else {
384 new_index = model.index(0, 0);
385 }
386 if (model.is_valid(new_index)) {
387 selection().set(new_index);
388 scroll_into_view(new_index, Orientation::Vertical);
389 update();
390 }
391 return;
392 }
393 if (event.key() == KeyCode::Key_Right) {
394 ModelIndex new_index;
395 if (!selection().is_empty()) {
396 auto old_index = selection().first();
397 new_index = model.index(old_index.row() + 1, old_index.column());
398 } else {
399 new_index = model.index(0, 0);
400 }
401 if (model.is_valid(new_index)) {
402 selection().set(new_index);
403 scroll_into_view(new_index, Orientation::Vertical);
404 update();
405 }
406 return;
407 }
408 if (event.key() == KeyCode::Key_PageUp) {
409 int items_per_page = (visible_content_rect().height() / effective_item_size().height()) * m_visual_column_count;
410 auto old_index = selection().first();
411 auto new_index = model.index(max(0, old_index.row() - items_per_page), old_index.column());
412 if (model.is_valid(new_index)) {
413 selection().set(new_index);
414 scroll_into_view(new_index, Orientation::Vertical);
415 update();
416 }
417 return;
418 }
419 if (event.key() == KeyCode::Key_PageDown) {
420 int items_per_page = (visible_content_rect().height() / effective_item_size().height()) * m_visual_column_count;
421 auto old_index = selection().first();
422 auto new_index = model.index(min(model.row_count() - 1, old_index.row() + items_per_page), old_index.column());
423 if (model.is_valid(new_index)) {
424 selection().set(new_index);
425 scroll_into_view(new_index, Orientation::Vertical);
426 update();
427 }
428 return;
429 }
430 return Widget::keydown_event(event);
431}
432
433}