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 <LibGUI/DragOperation.h>
30#include <LibGUI/ItemView.h>
31#include <LibGUI/Model.h>
32#include <LibGUI/Painter.h>
33#include <LibGUI/ScrollBar.h>
34#include <LibGfx/Palette.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 = this->adjusted_position(position);
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
148Gfx::Point ItemView::adjusted_position(const Gfx::Point& position) const
149{
150 return position.translated(0, vertical_scrollbar().value());
151}
152
153void ItemView::mousedown_event(MouseEvent& event)
154{
155 if (!model())
156 return AbstractView::mousedown_event(event);
157
158 if (event.button() != MouseButton::Left)
159 return AbstractView::mousedown_event(event);
160
161 auto index = index_at_event_position(event.position());
162 if (index.is_valid()) {
163 // We might start dragging this item, but not rubber-banding.
164 return AbstractView::mousedown_event(event);
165 }
166
167 ASSERT(m_rubber_band_remembered_selection.is_empty());
168
169 if (event.modifiers() & Mod_Ctrl) {
170 selection().for_each_index([&](auto& index) {
171 m_rubber_band_remembered_selection.append(index);
172 });
173 } else {
174 selection().clear();
175 }
176
177 auto adjusted_position = this->adjusted_position(event.position());
178
179 m_might_drag = false;
180 m_rubber_banding = true;
181 m_rubber_band_origin = adjusted_position;
182 m_rubber_band_current = adjusted_position;
183}
184
185void ItemView::mouseup_event(MouseEvent& event)
186{
187 if (m_rubber_banding && event.button() == MouseButton::Left) {
188 m_rubber_banding = false;
189 m_rubber_band_remembered_selection.clear();
190 update();
191 }
192 AbstractView::mouseup_event(event);
193}
194
195void ItemView::drag_move_event(DragEvent& event)
196{
197 auto index = index_at_event_position(event.position());
198 ModelIndex new_drop_candidate_index;
199 if (index.is_valid()) {
200 bool acceptable = model()->accepts_drag(index, event.data_type());
201#ifdef DRAGDROP_DEBUG
202 dbg() << "Drag of type '" << event.data_type() << "' moving over " << index << ", acceptable: " << acceptable;
203#endif
204 if (acceptable)
205 new_drop_candidate_index = index;
206 }
207 if (m_drop_candidate_index != new_drop_candidate_index) {
208 m_drop_candidate_index = new_drop_candidate_index;
209 update();
210 }
211 event.accept();
212}
213
214void ItemView::mousemove_event(MouseEvent& event)
215{
216 if (!model())
217 return AbstractView::mousemove_event(event);
218
219 if (m_rubber_banding) {
220 auto adjusted_position = this->adjusted_position(event.position());
221 if (m_rubber_band_current != adjusted_position) {
222 m_rubber_band_current = adjusted_position;
223 auto rubber_band_rect = Gfx::Rect::from_two_points(m_rubber_band_origin, m_rubber_band_current);
224 selection().clear();
225 for (auto item_index : items_intersecting_rect(rubber_band_rect)) {
226 selection().add(model()->index(item_index, model_column()));
227 }
228 if (event.modifiers() & Mod_Ctrl) {
229 for (auto stored_item : m_rubber_band_remembered_selection) {
230 selection().add(stored_item);
231 }
232 }
233 update();
234 return;
235 }
236 }
237
238 AbstractView::mousemove_event(event);
239}
240
241void 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
242{
243 item_rect = this->item_rect(item_index);
244 icon_rect = { 0, 0, 32, 32 };
245 icon_rect.center_within(item_rect);
246 icon_rect.move_by(0, -font.glyph_height() - 6);
247 text_rect = { 0, icon_rect.bottom() + 6 + 1, font.width(item_text.to_string()), font.glyph_height() };
248 text_rect.center_horizontally_within(item_rect);
249 text_rect.inflate(6, 4);
250 text_rect.intersect(item_rect);
251}
252
253void ItemView::second_paint_event(PaintEvent& event)
254{
255 if (!m_rubber_banding)
256 return;
257
258 Painter painter(*this);
259 painter.add_clip_rect(event.rect());
260 painter.translate(frame_thickness(), frame_thickness());
261 painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
262
263 auto rubber_band_rect = Gfx::Rect::from_two_points(m_rubber_band_origin, m_rubber_band_current);
264 painter.fill_rect(rubber_band_rect, parent_widget()->palette().rubber_band_fill());
265 painter.draw_rect(rubber_band_rect, parent_widget()->palette().rubber_band_border());
266}
267
268void ItemView::paint_event(PaintEvent& event)
269{
270 Color widget_background_color = palette().color(background_role());
271 Frame::paint_event(event);
272
273 Painter painter(*this);
274 painter.add_clip_rect(widget_inner_rect());
275 painter.add_clip_rect(event.rect());
276 painter.fill_rect(event.rect(), widget_background_color);
277 painter.translate(frame_thickness(), frame_thickness());
278 painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
279
280 auto column_metadata = model()->column_metadata(m_model_column);
281 const Gfx::Font& font = column_metadata.font ? *column_metadata.font : this->font();
282
283 for (int item_index = 0; item_index < model()->row_count(); ++item_index) {
284 auto model_index = model()->index(item_index, m_model_column);
285 bool is_selected_item = selection().contains(model_index);
286 Color background_color;
287 if (is_selected_item) {
288 background_color = is_focused() ? palette().selection() : palette().inactive_selection();
289 } else {
290 background_color = widget_background_color;
291 }
292
293 auto icon = model()->data(model_index, Model::Role::Icon);
294 auto item_text = model()->data(model_index, Model::Role::Display);
295
296 Gfx::Rect item_rect;
297 Gfx::Rect icon_rect;
298 Gfx::Rect text_rect;
299 get_item_rects(item_index, font, item_text, item_rect, icon_rect, text_rect);
300
301 if (icon.is_icon()) {
302 if (auto bitmap = icon.as_icon().bitmap_for_size(icon_rect.width())) {
303 Gfx::Rect destination = bitmap->rect();
304 destination.center_within(icon_rect);
305
306 if (m_hovered_index.is_valid() && m_hovered_index == model_index) {
307 painter.blit_brightened(destination.location(), *bitmap, bitmap->rect());
308 } else {
309 painter.blit(destination.location(), *bitmap, bitmap->rect());
310 }
311 }
312 }
313
314 Color text_color;
315 if (is_selected_item)
316 text_color = is_focused() ? palette().selection_text() : palette().inactive_selection_text();
317 else
318 text_color = model()->data(model_index, Model::Role::ForegroundColor).to_color(palette().color(foreground_role()));
319 painter.fill_rect(text_rect, background_color);
320 painter.draw_text(text_rect, item_text.to_string(), font, Gfx::TextAlignment::Center, text_color, Gfx::TextElision::Right);
321
322 if (model_index == m_drop_candidate_index) {
323 // FIXME: This visualization is not great, as it's also possible to drop things on the text label..
324 painter.draw_rect(icon_rect.inflated(8, 8), palette().selection(), true);
325 }
326 };
327}
328
329int ItemView::item_count() const
330{
331 if (!model())
332 return 0;
333 return model()->row_count();
334}
335
336void ItemView::keydown_event(KeyEvent& event)
337{
338 if (!model())
339 return;
340 if (!m_visual_row_count || !m_visual_column_count)
341 return;
342
343 auto& model = *this->model();
344 if (event.key() == KeyCode::Key_Return) {
345 activate_selected();
346 return;
347 }
348 if (event.key() == KeyCode::Key_Home) {
349 auto new_index = model.index(0, 0);
350 if (model.is_valid(new_index)) {
351 selection().set(new_index);
352 scroll_into_view(new_index, Orientation::Vertical);
353 update();
354 }
355 return;
356 }
357 if (event.key() == KeyCode::Key_End) {
358 auto new_index = model.index(model.row_count() - 1, 0);
359 if (model.is_valid(new_index)) {
360 selection().set(new_index);
361 scroll_into_view(new_index, Orientation::Vertical);
362 update();
363 }
364 return;
365 }
366 if (event.key() == KeyCode::Key_Up) {
367 ModelIndex new_index;
368 if (!selection().is_empty()) {
369 auto old_index = selection().first();
370 new_index = model.index(old_index.row() - m_visual_column_count, old_index.column());
371 } else {
372 new_index = model.index(0, 0);
373 }
374 if (model.is_valid(new_index)) {
375 selection().set(new_index);
376 scroll_into_view(new_index, Orientation::Vertical);
377 update();
378 }
379 return;
380 }
381 if (event.key() == KeyCode::Key_Down) {
382 ModelIndex new_index;
383 if (!selection().is_empty()) {
384 auto old_index = selection().first();
385 new_index = model.index(old_index.row() + m_visual_column_count, old_index.column());
386 } else {
387 new_index = model.index(0, 0);
388 }
389 if (model.is_valid(new_index)) {
390 selection().set(new_index);
391 scroll_into_view(new_index, Orientation::Vertical);
392 update();
393 }
394 return;
395 }
396 if (event.key() == KeyCode::Key_Left) {
397 ModelIndex new_index;
398 if (!selection().is_empty()) {
399 auto old_index = selection().first();
400 new_index = model.index(old_index.row() - 1, old_index.column());
401 } else {
402 new_index = model.index(0, 0);
403 }
404 if (model.is_valid(new_index)) {
405 selection().set(new_index);
406 scroll_into_view(new_index, Orientation::Vertical);
407 update();
408 }
409 return;
410 }
411 if (event.key() == KeyCode::Key_Right) {
412 ModelIndex new_index;
413 if (!selection().is_empty()) {
414 auto old_index = selection().first();
415 new_index = model.index(old_index.row() + 1, old_index.column());
416 } else {
417 new_index = model.index(0, 0);
418 }
419 if (model.is_valid(new_index)) {
420 selection().set(new_index);
421 scroll_into_view(new_index, Orientation::Vertical);
422 update();
423 }
424 return;
425 }
426 if (event.key() == KeyCode::Key_PageUp) {
427 int items_per_page = (visible_content_rect().height() / effective_item_size().height()) * m_visual_column_count;
428 auto old_index = selection().first();
429 auto new_index = model.index(max(0, old_index.row() - items_per_page), old_index.column());
430 if (model.is_valid(new_index)) {
431 selection().set(new_index);
432 scroll_into_view(new_index, Orientation::Vertical);
433 update();
434 }
435 return;
436 }
437 if (event.key() == KeyCode::Key_PageDown) {
438 int items_per_page = (visible_content_rect().height() / effective_item_size().height()) * m_visual_column_count;
439 auto old_index = selection().first();
440 auto new_index = model.index(min(model.row_count() - 1, old_index.row() + items_per_page), old_index.column());
441 if (model.is_valid(new_index)) {
442 selection().set(new_index);
443 scroll_into_view(new_index, Orientation::Vertical);
444 update();
445 }
446 return;
447 }
448 return Widget::keydown_event(event);
449}
450
451}