2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2019-03-23 02:04:31 +01:00
|
|
|
#pragma once
|
|
|
|
|
2019-05-28 11:53:16 +02:00
|
|
|
#include <AK/Function.h>
|
2019-03-23 02:04:31 +01:00
|
|
|
#include <LibGUI/GModel.h>
|
2019-09-07 19:14:59 +02:00
|
|
|
#include <LibGUI/GModelSelection.h>
|
2019-03-23 02:04:31 +01:00
|
|
|
#include <LibGUI/GScrollableWidget.h>
|
|
|
|
|
2019-06-23 08:18:28 +02:00
|
|
|
class GModelEditingDelegate;
|
2019-04-18 22:27:14 +02:00
|
|
|
|
2019-03-23 02:04:31 +01:00
|
|
|
class GAbstractView : public GScrollableWidget {
|
2019-07-25 19:49:28 +02:00
|
|
|
C_OBJECT(GAbstractView)
|
2019-03-23 02:04:31 +01:00
|
|
|
friend class GModel;
|
2019-09-07 19:14:59 +02:00
|
|
|
|
2019-03-23 02:04:31 +01:00
|
|
|
public:
|
2019-06-21 18:37:47 +02:00
|
|
|
void set_model(RefPtr<GModel>&&);
|
2019-03-23 02:04:31 +01:00
|
|
|
GModel* model() { return m_model.ptr(); }
|
|
|
|
const GModel* model() const { return m_model.ptr(); }
|
|
|
|
|
2019-09-07 19:14:59 +02:00
|
|
|
GModelSelection& selection() { return m_selection; }
|
|
|
|
const GModelSelection& selection() const { return m_selection; }
|
2020-01-07 22:28:40 +13:00
|
|
|
void select_all();
|
2019-09-07 19:14:59 +02:00
|
|
|
|
2019-04-18 22:27:14 +02:00
|
|
|
bool is_editable() const { return m_editable; }
|
|
|
|
void set_editable(bool editable) { m_editable = editable; }
|
|
|
|
|
2019-03-23 02:04:31 +01:00
|
|
|
virtual bool accepts_focus() const override { return true; }
|
|
|
|
virtual void did_update_model();
|
2019-03-30 03:27:25 +01:00
|
|
|
virtual void did_update_selection();
|
2019-03-23 02:04:31 +01:00
|
|
|
|
2019-05-28 11:53:16 +02:00
|
|
|
virtual Rect content_rect(const GModelIndex&) const { return {}; }
|
2019-04-19 00:07:33 +02:00
|
|
|
void begin_editing(const GModelIndex&);
|
|
|
|
void stop_editing();
|
|
|
|
|
2019-05-09 04:56:52 +02:00
|
|
|
void set_activates_on_selection(bool b) { m_activates_on_selection = b; }
|
|
|
|
bool activates_on_selection() const { return m_activates_on_selection; }
|
|
|
|
|
2019-09-07 21:34:26 +02:00
|
|
|
Function<void()> on_selection_change;
|
2019-05-09 04:56:52 +02:00
|
|
|
Function<void(const GModelIndex&)> on_activation;
|
2019-06-22 10:40:35 +02:00
|
|
|
Function<void(const GModelIndex&)> on_selection;
|
2019-06-30 08:13:41 +02:00
|
|
|
Function<void(const GModelIndex&, const GContextMenuEvent&)> on_context_menu_request;
|
2019-03-23 03:53:51 +01:00
|
|
|
|
2019-06-23 08:18:28 +02:00
|
|
|
Function<OwnPtr<GModelEditingDelegate>(const GModelIndex&)> aid_create_editing_delegate;
|
|
|
|
|
2019-09-07 19:14:59 +02:00
|
|
|
void notify_selection_changed(Badge<GModelSelection>);
|
|
|
|
|
2019-10-22 21:38:04 +02:00
|
|
|
NonnullRefPtr<Font> font_for_index(const GModelIndex&) const;
|
|
|
|
|
2019-03-23 02:04:31 +01:00
|
|
|
protected:
|
2019-12-13 20:54:40 +01:00
|
|
|
explicit GAbstractView(GWidget* parent);
|
|
|
|
virtual ~GAbstractView() override;
|
|
|
|
|
2019-04-18 23:57:07 +02:00
|
|
|
virtual void did_scroll() override;
|
2019-05-09 04:56:52 +02:00
|
|
|
void activate(const GModelIndex&);
|
2020-01-10 18:50:26 +03:00
|
|
|
void activate_selected();
|
2019-04-18 23:57:07 +02:00
|
|
|
void update_edit_widget_position();
|
2019-03-23 02:04:31 +01:00
|
|
|
|
2019-04-18 22:27:14 +02:00
|
|
|
bool m_editable { false };
|
|
|
|
GModelIndex m_edit_index;
|
2019-09-22 00:31:54 +02:00
|
|
|
RefPtr<GWidget> m_edit_widget;
|
2019-04-18 23:57:07 +02:00
|
|
|
Rect m_edit_widget_content_rect;
|
2019-04-18 22:27:14 +02:00
|
|
|
|
2019-03-23 02:04:31 +01:00
|
|
|
private:
|
2019-06-21 18:37:47 +02:00
|
|
|
RefPtr<GModel> m_model;
|
2019-06-23 08:18:28 +02:00
|
|
|
OwnPtr<GModelEditingDelegate> m_editing_delegate;
|
2019-09-07 19:14:59 +02:00
|
|
|
GModelSelection m_selection;
|
2019-05-09 04:56:52 +02:00
|
|
|
bool m_activates_on_selection { false };
|
2019-03-23 02:04:31 +01:00
|
|
|
};
|