1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
|
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/StringBuilder.h>
#include <AK/Vector.h>
#include <LibGUI/AbstractView.h>
#include <LibGUI/DragOperation.h>
#include <LibGUI/Model.h>
#include <LibGUI/ModelEditingDelegate.h>
#include <LibGUI/Painter.h>
#include <LibGUI/ScrollBar.h>
#include <LibGUI/TextBox.h>
namespace GUI {
AbstractView::AbstractView()
: m_sort_order(SortOrder::Ascending)
, m_selection(*this)
{
}
AbstractView::~AbstractView()
{
}
void AbstractView::set_model(RefPtr<Model> model)
{
if (model == m_model)
return;
if (m_model)
m_model->unregister_view({}, *this);
m_model = move(model);
if (m_model)
m_model->register_view({}, *this);
did_update_model(GUI::Model::InvalidateAllIndexes);
scroll_to_top();
}
void AbstractView::did_update_model(unsigned flags)
{
// FIXME: It's unfortunate that we lose so much view state when the model updates in any way.
stop_editing();
m_edit_index = {};
m_hovered_index = {};
if (!model() || (flags & GUI::Model::InvalidateAllIndexes)) {
clear_selection();
} else {
selection().remove_matching([this](auto& index) { return !model()->is_valid(index); });
}
}
void AbstractView::clear_selection()
{
m_selection.clear();
}
void AbstractView::set_selection(const ModelIndex& new_index)
{
m_selection.set(new_index);
}
void AbstractView::add_selection(const ModelIndex& new_index)
{
m_selection.add(new_index);
}
void AbstractView::remove_selection(const ModelIndex& new_index)
{
m_selection.remove(new_index);
}
void AbstractView::toggle_selection(const ModelIndex& new_index)
{
m_selection.toggle(new_index);
}
void AbstractView::did_update_selection()
{
if (!model() || selection().first() != m_edit_index)
stop_editing();
if (model() && on_selection && selection().first().is_valid())
on_selection(selection().first());
}
void AbstractView::did_scroll()
{
update_edit_widget_position();
}
void AbstractView::update_edit_widget_position()
{
if (!m_edit_widget)
return;
m_edit_widget->set_relative_rect(m_edit_widget_content_rect.translated(-horizontal_scrollbar().value(), -vertical_scrollbar().value()));
}
void AbstractView::begin_editing(const ModelIndex& index)
{
ASSERT(is_editable());
ASSERT(model());
if (m_edit_index == index)
return;
if (!model()->is_editable(index))
return;
if (m_edit_widget) {
remove_child(*m_edit_widget);
m_edit_widget = nullptr;
}
m_edit_index = index;
ASSERT(aid_create_editing_delegate);
m_editing_delegate = aid_create_editing_delegate(index);
m_editing_delegate->bind(*model(), index);
m_editing_delegate->set_value(index.data());
m_edit_widget = m_editing_delegate->widget();
add_child(*m_edit_widget);
m_edit_widget->move_to_back();
m_edit_widget_content_rect = content_rect(index).translated(frame_thickness(), frame_thickness());
update_edit_widget_position();
m_edit_widget->set_focus(true);
m_editing_delegate->will_begin_editing();
m_editing_delegate->on_commit = [this] {
ASSERT(model());
model()->set_data(m_edit_index, m_editing_delegate->value());
stop_editing();
};
}
void AbstractView::stop_editing()
{
bool take_back_focus = false;
m_edit_index = {};
if (m_edit_widget) {
take_back_focus = m_edit_widget->is_focused();
remove_child(*m_edit_widget);
m_edit_widget = nullptr;
}
if (take_back_focus)
set_focus(true);
}
void AbstractView::activate(const ModelIndex& index)
{
if (on_activation)
on_activation(index);
}
void AbstractView::activate_selected()
{
if (!on_activation)
return;
selection().for_each_index([this](auto& index) {
on_activation(index);
});
}
void AbstractView::notify_selection_changed(Badge<ModelSelection>)
{
did_update_selection();
if (on_selection_change)
on_selection_change();
update();
}
NonnullRefPtr<Gfx::Font> AbstractView::font_for_index(const ModelIndex& index) const
{
if (!model())
return font();
auto font_data = index.data(ModelRole::Font);
if (font_data.is_font())
return font_data.as_font();
return font();
}
void AbstractView::mousedown_event(MouseEvent& event)
{
ScrollableWidget::mousedown_event(event);
if (!model())
return;
if (event.button() == MouseButton::Left)
m_left_mousedown_position = event.position();
auto index = index_at_event_position(event.position());
m_might_drag = false;
if (!index.is_valid()) {
clear_selection();
} else if (event.modifiers() & Mod_Ctrl) {
set_cursor(index, SelectionUpdate::Ctrl);
} else if (event.button() == MouseButton::Left && m_selection.contains(index) && !m_model->drag_data_type().is_null()) {
// We might be starting a drag, so don't throw away other selected items yet.
m_might_drag = true;
} else {
set_cursor(index, SelectionUpdate::Set);
}
update();
}
void AbstractView::set_hovered_index(const ModelIndex& index)
{
if (m_hovered_index == index)
return;
m_hovered_index = index;
if (m_hovered_index.is_valid())
m_last_valid_hovered_index = m_hovered_index;
update();
}
void AbstractView::set_last_valid_hovered_index(const ModelIndex& index)
{
if (m_last_valid_hovered_index == index)
return;
m_last_valid_hovered_index = index;
update();
}
void AbstractView::leave_event(Core::Event& event)
{
ScrollableWidget::leave_event(event);
set_hovered_index({});
}
void AbstractView::mousemove_event(MouseEvent& event)
{
if (!model())
return ScrollableWidget::mousemove_event(event);
auto hovered_index = index_at_event_position(event.position());
set_hovered_index(hovered_index);
if (!m_might_drag)
return ScrollableWidget::mousemove_event(event);
if (!(event.buttons() & MouseButton::Left) || m_selection.is_empty()) {
m_might_drag = false;
return ScrollableWidget::mousemove_event(event);
}
auto diff = event.position() - m_left_mousedown_position;
auto distance_travelled_squared = diff.x() * diff.x() + diff.y() * diff.y();
constexpr int drag_distance_threshold = 5;
if (distance_travelled_squared <= drag_distance_threshold)
return ScrollableWidget::mousemove_event(event);
auto data_type = m_model->drag_data_type();
ASSERT(!data_type.is_null());
dbg() << "Initiate drag!";
auto drag_operation = DragOperation::construct();
RefPtr<Gfx::Bitmap> bitmap;
StringBuilder text_builder;
StringBuilder data_builder;
bool first = true;
m_selection.for_each_index([&](auto& index) {
auto text_data = index.data();
if (!first)
text_builder.append(", ");
text_builder.append(text_data.to_string());
auto drag_data = index.data(ModelRole::DragData);
data_builder.append(drag_data.to_string());
data_builder.append('\n');
first = false;
if (!bitmap) {
Variant icon_data = index.data(ModelRole::Icon);
if (icon_data.is_icon())
bitmap = icon_data.as_icon().bitmap_for_size(32);
}
});
drag_operation->set_text(text_builder.to_string());
drag_operation->set_bitmap(bitmap);
drag_operation->set_data(data_type, data_builder.to_string());
auto outcome = drag_operation->exec();
switch (outcome) {
case DragOperation::Outcome::Accepted:
dbg() << "Drag was accepted!";
break;
case DragOperation::Outcome::Cancelled:
dbg() << "Drag was cancelled!";
break;
default:
ASSERT_NOT_REACHED();
break;
}
}
void AbstractView::mouseup_event(MouseEvent& event)
{
ScrollableWidget::mouseup_event(event);
if (!model())
return;
if (m_might_drag) {
// We were unsure about unselecting items other than the current one
// in mousedown_event(), because we could be seeing a start of a drag.
// Since we're here, it was not that; so fix up the selection now.
auto index = index_at_event_position(event.position());
if (index.is_valid())
set_selection(index);
else
clear_selection();
m_might_drag = false;
update();
}
if (activates_on_selection())
activate_selected();
}
void AbstractView::doubleclick_event(MouseEvent& event)
{
if (!model())
return;
if (event.button() != MouseButton::Left)
return;
m_might_drag = false;
auto index = index_at_event_position(event.position());
if (!index.is_valid())
clear_selection();
else if (!m_selection.contains(index))
set_selection(index);
activate_or_edit_selected();
}
void AbstractView::activate_or_edit_selected()
{
if (is_editable())
begin_editing(selection().first());
else
activate_selected();
}
void AbstractView::context_menu_event(ContextMenuEvent& event)
{
if (!model())
return;
auto index = index_at_event_position(event.position());
if (index.is_valid())
add_selection(index);
else
clear_selection();
if (on_context_menu_request)
on_context_menu_request(index, event);
}
void AbstractView::drop_event(DropEvent& event)
{
event.accept();
if (!model())
return;
auto index = index_at_event_position(event.position());
if (on_drop)
on_drop(index, event);
}
void AbstractView::set_multi_select(bool multi_select)
{
if (m_multi_select == multi_select)
return;
m_multi_select = multi_select;
if (!multi_select && m_selection.size() > 1) {
auto first_selected = m_selection.first();
m_selection.clear();
m_selection.set(first_selected);
}
}
void AbstractView::set_key_column_and_sort_order(int column, SortOrder sort_order)
{
m_key_column = column;
m_sort_order = sort_order;
if (model())
model()->sort(column, sort_order);
update();
}
void AbstractView::set_cursor(ModelIndex index, SelectionUpdate selection_update)
{
if (m_cursor_index == index)
return;
if (!model()) {
m_cursor_index = {};
return;
}
if (model()->is_valid(index)) {
if (selection_update == SelectionUpdate::Set)
selection().set(index);
else if (selection_update == SelectionUpdate::Ctrl)
toggle_selection(index);
// FIXME: Support the other SelectionUpdate types
m_cursor_index = index;
// FIXME: We should scroll into view both vertically *and* horizontally.
scroll_into_view(index, false, true);
update();
}
}
}
|