2020-01-18 11:38:21 +03: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-02-28 03:43:50 +03:00
|
|
|
#pragma once
|
|
|
|
|
2019-02-28 12:57:09 +03:00
|
|
|
#include <AK/Badge.h>
|
|
|
|
#include <AK/Function.h>
|
|
|
|
#include <AK/HashTable.h>
|
2019-06-21 19:58:45 +03:00
|
|
|
#include <AK/RefCounted.h>
|
2020-01-07 14:43:31 +03:00
|
|
|
#include <AK/String.h>
|
2020-02-06 14:04:00 +03:00
|
|
|
#include <LibGfx/TextAlignment.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/ModelIndex.h>
|
|
|
|
#include <LibGUI/Variant.h>
|
2019-02-28 03:43:50 +03:00
|
|
|
|
2020-02-06 13:56:38 +03:00
|
|
|
namespace Gfx {
|
2019-03-15 19:37:13 +03:00
|
|
|
class Font;
|
2020-02-06 13:56:38 +03:00
|
|
|
}
|
2019-02-28 12:57:09 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
namespace GUI {
|
|
|
|
|
|
|
|
class AbstractView;
|
|
|
|
|
|
|
|
enum class SortOrder {
|
2019-05-28 12:53:16 +03:00
|
|
|
None,
|
|
|
|
Ascending,
|
|
|
|
Descending
|
|
|
|
};
|
2019-03-09 15:33:52 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
class Model : public RefCounted<Model> {
|
2019-02-28 03:43:50 +03:00
|
|
|
public:
|
2019-02-28 13:27:04 +03:00
|
|
|
struct ColumnMetadata {
|
|
|
|
int preferred_width { 0 };
|
2020-02-06 13:56:38 +03:00
|
|
|
Gfx::TextAlignment text_alignment { Gfx::TextAlignment::CenterLeft };
|
|
|
|
const Gfx::Font* font { nullptr };
|
2020-01-07 14:43:31 +03:00
|
|
|
enum class Sortable {
|
|
|
|
False,
|
|
|
|
True,
|
|
|
|
};
|
|
|
|
Sortable sortable { Sortable::True };
|
2019-02-28 13:27:04 +03:00
|
|
|
};
|
|
|
|
|
2019-06-07 18:13:23 +03:00
|
|
|
enum class Role {
|
2019-05-28 12:53:16 +03:00
|
|
|
Display,
|
|
|
|
Sort,
|
|
|
|
Custom,
|
|
|
|
ForegroundColor,
|
|
|
|
BackgroundColor,
|
2019-10-22 22:38:04 +03:00
|
|
|
Icon,
|
|
|
|
Font,
|
2019-12-19 22:09:31 +03:00
|
|
|
DragData,
|
2019-05-28 12:53:16 +03:00
|
|
|
};
|
2019-03-09 16:24:34 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
virtual ~Model();
|
2019-02-28 03:43:50 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
virtual int row_count(const ModelIndex& = ModelIndex()) const = 0;
|
|
|
|
virtual int column_count(const ModelIndex& = ModelIndex()) const = 0;
|
2019-05-28 12:53:16 +03:00
|
|
|
virtual String row_name(int) const { return {}; }
|
|
|
|
virtual String column_name(int) const { return {}; }
|
|
|
|
virtual ColumnMetadata column_metadata(int) const { return {}; }
|
2020-02-02 17:07:41 +03:00
|
|
|
virtual Variant data(const ModelIndex&, Role = Role::Display) const = 0;
|
2019-02-28 03:43:50 +03:00
|
|
|
virtual void update() = 0;
|
2020-02-02 17:07:41 +03:00
|
|
|
virtual ModelIndex parent_index(const ModelIndex&) const { return {}; }
|
|
|
|
virtual ModelIndex index(int row, int column = 0, const ModelIndex& = ModelIndex()) const { return create_index(row, column); }
|
|
|
|
virtual ModelIndex sibling(int row, int column, const ModelIndex& parent) const;
|
|
|
|
virtual bool is_editable(const ModelIndex&) const { return false; }
|
|
|
|
virtual void set_data(const ModelIndex&, const Variant&) {}
|
2019-12-14 01:36:36 +03:00
|
|
|
virtual int tree_column() const { return 0; }
|
2020-02-13 23:45:46 +03:00
|
|
|
virtual bool accepts_drag(const ModelIndex&, const StringView& data_type);
|
2019-02-28 12:20:04 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
bool is_valid(const ModelIndex& index) const
|
2019-02-28 12:20:04 +03:00
|
|
|
{
|
2020-01-10 18:48:27 +03:00
|
|
|
auto parent_index = this->parent_index(index);
|
|
|
|
return index.row() >= 0 && index.row() < row_count(parent_index) && index.column() >= 0 && index.column() < column_count(parent_index);
|
2019-02-28 12:20:04 +03:00
|
|
|
}
|
2019-02-28 12:57:09 +03:00
|
|
|
|
2019-03-09 15:33:52 +03:00
|
|
|
virtual int key_column() const { return -1; }
|
2020-02-02 17:07:41 +03:00
|
|
|
virtual SortOrder sort_order() const { return SortOrder::None; }
|
|
|
|
virtual void set_key_column_and_sort_order(int, SortOrder) {}
|
2019-03-09 15:33:52 +03:00
|
|
|
|
2020-01-22 21:15:46 +03:00
|
|
|
virtual StringView drag_data_type() const { return {}; }
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void register_view(Badge<AbstractView>, AbstractView&);
|
|
|
|
void unregister_view(Badge<AbstractView>, AbstractView&);
|
2019-02-28 12:57:09 +03:00
|
|
|
|
2019-08-20 20:45:08 +03:00
|
|
|
Function<void()> on_update;
|
2019-03-09 15:33:52 +03:00
|
|
|
|
2019-02-28 12:57:09 +03:00
|
|
|
protected:
|
2020-02-02 17:07:41 +03:00
|
|
|
Model();
|
2019-02-28 12:57:09 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void for_each_view(Function<void(AbstractView&)>);
|
2019-02-28 12:57:09 +03:00
|
|
|
void did_update();
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
ModelIndex create_index(int row, int column, const void* data = nullptr) const;
|
2019-03-29 06:58:15 +03:00
|
|
|
|
2019-02-28 12:57:09 +03:00
|
|
|
private:
|
2020-02-02 17:07:41 +03:00
|
|
|
HashTable<AbstractView*> m_views;
|
2019-02-28 03:43:50 +03:00
|
|
|
};
|
2019-03-29 16:46:53 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
inline ModelIndex ModelIndex::parent() const
|
2019-03-29 16:46:53 +03:00
|
|
|
{
|
2020-02-02 17:07:41 +03:00
|
|
|
return m_model ? m_model->parent_index(*this) : ModelIndex();
|
|
|
|
}
|
|
|
|
|
2019-03-29 16:46:53 +03:00
|
|
|
}
|