| 
									
										
										
										
											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-02-28 01:43:50 +01:00
										 |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 10:57:09 +01:00
										 |  |  | #include <AK/Badge.h>
 | 
					
						
							|  |  |  | #include <AK/Function.h>
 | 
					
						
							|  |  |  | #include <AK/HashTable.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-21 18:58:45 +02:00
										 |  |  | #include <AK/RefCounted.h>
 | 
					
						
							| 
									
										
										
										
											2020-01-07 22:43:31 +11:00
										 |  |  | #include <AK/String.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 20:33:02 +01:00
										 |  |  | #include <LibGUI/ModelIndex.h>
 | 
					
						
							|  |  |  | #include <LibGUI/Variant.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-15 00:10:34 +01:00
										 |  |  | #include <LibGfx/Forward.h>
 | 
					
						
							|  |  |  | #include <LibGfx/TextAlignment.h>
 | 
					
						
							| 
									
										
										
										
											2019-02-28 10:57:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | namespace GUI { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | enum class SortOrder { | 
					
						
							| 
									
										
										
										
											2019-05-28 11:53:16 +02:00
										 |  |  |     None, | 
					
						
							|  |  |  |     Ascending, | 
					
						
							|  |  |  |     Descending | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2019-03-09 13:33:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-11 06:47:26 -06:00
										 |  |  | class ModelClient { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     virtual ~ModelClient() { } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual void on_model_update(unsigned flags) = 0; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | class Model : public RefCounted<Model> { | 
					
						
							| 
									
										
										
										
											2019-02-28 01:43:50 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-04-12 12:03:33 +02:00
										 |  |  |     enum UpdateFlag { | 
					
						
							|  |  |  |         DontInvalidateIndexes = 0, | 
					
						
							|  |  |  |         InvalidateAllIndexes = 1 << 0, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-07 17:13:23 +02:00
										 |  |  |     enum class Role { | 
					
						
							| 
									
										
										
										
											2019-05-28 11:53:16 +02:00
										 |  |  |         Display, | 
					
						
							|  |  |  |         Sort, | 
					
						
							|  |  |  |         ForegroundColor, | 
					
						
							|  |  |  |         BackgroundColor, | 
					
						
							| 
									
										
										
										
											2019-10-22 21:38:04 +02:00
										 |  |  |         Icon, | 
					
						
							|  |  |  |         Font, | 
					
						
							| 
									
										
										
										
											2019-12-19 20:09:31 +01:00
										 |  |  |         DragData, | 
					
						
							| 
									
										
										
										
											2020-05-21 19:36:09 +02:00
										 |  |  |         TextAlignment, | 
					
						
							| 
									
										
										
										
											2020-07-04 22:36:23 +04:30
										 |  |  |         Search, | 
					
						
							| 
									
										
										
										
											2020-07-04 18:40:21 +02:00
										 |  |  |         Custom = 0x100, // Applications are free to use roles above this number as they please
 | 
					
						
							| 
									
										
										
										
											2019-05-28 11:53:16 +02:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2019-03-09 14:24:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     virtual ~Model(); | 
					
						
							| 
									
										
										
										
											2019-02-28 01:43:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     virtual int row_count(const ModelIndex& = ModelIndex()) const = 0; | 
					
						
							|  |  |  |     virtual int column_count(const ModelIndex& = ModelIndex()) const = 0; | 
					
						
							| 
									
										
										
										
											2019-05-28 11:53:16 +02:00
										 |  |  |     virtual String column_name(int) const { return {}; } | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     virtual Variant data(const ModelIndex&, Role = Role::Display) const = 0; | 
					
						
							| 
									
										
										
										
											2020-07-04 22:36:23 +04:30
										 |  |  |     virtual TriState data_matches(const ModelIndex&, Variant) const { return TriState::Unknown; } | 
					
						
							| 
									
										
										
										
											2019-02-28 01:43:50 +01:00
										 |  |  |     virtual void update() = 0; | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     virtual ModelIndex parent_index(const ModelIndex&) const { return {}; } | 
					
						
							| 
									
										
										
										
											2020-08-13 16:04:17 +02:00
										 |  |  |     virtual ModelIndex index(int row, int column = 0, const ModelIndex& parent = ModelIndex()) const; | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     virtual bool is_editable(const ModelIndex&) const { return false; } | 
					
						
							| 
									
										
										
										
											2020-07-04 22:36:23 +04:30
										 |  |  |     virtual void set_data(const ModelIndex&, const Variant&) { } | 
					
						
							| 
									
										
										
										
											2019-12-13 23:36:36 +01:00
										 |  |  |     virtual int tree_column() const { return 0; } | 
					
						
							| 
									
										
										
										
											2020-02-13 21:45:46 +01:00
										 |  |  |     virtual bool accepts_drag(const ModelIndex&, const StringView& data_type); | 
					
						
							| 
									
										
										
										
											2019-02-28 10:20:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-21 19:45:15 +02:00
										 |  |  |     virtual bool is_column_sortable([[maybe_unused]] int column_index) const { return true; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     bool is_valid(const ModelIndex& index) const | 
					
						
							| 
									
										
										
										
											2019-02-28 10:20:04 +01: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 10:20:04 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-02-28 10:57:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-09 13:33:52 +01:00
										 |  |  |     virtual int key_column() const { return -1; } | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     virtual SortOrder sort_order() const { return SortOrder::None; } | 
					
						
							| 
									
										
										
										
											2020-07-04 22:36:23 +04:30
										 |  |  |     virtual void set_key_column_and_sort_order(int, SortOrder) { } | 
					
						
							| 
									
										
										
										
											2019-03-09 13:33:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-22 21:15:46 +03:00
										 |  |  |     virtual StringView drag_data_type() const { return {}; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     void register_view(Badge<AbstractView>, AbstractView&); | 
					
						
							|  |  |  |     void unregister_view(Badge<AbstractView>, AbstractView&); | 
					
						
							| 
									
										
										
										
											2019-02-28 10:57:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-11 06:47:26 -06:00
										 |  |  |     void register_client(ModelClient&); | 
					
						
							|  |  |  |     void unregister_client(ModelClient&); | 
					
						
							| 
									
										
										
										
											2019-03-09 13:33:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 10:57:09 +01:00
										 |  |  | protected: | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     Model(); | 
					
						
							| 
									
										
										
										
											2019-02-28 10:57:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     void for_each_view(Function<void(AbstractView&)>); | 
					
						
							| 
									
										
										
										
											2020-04-12 12:03:33 +02:00
										 |  |  |     void did_update(unsigned flags = UpdateFlag::InvalidateAllIndexes); | 
					
						
							| 
									
										
										
										
											2019-02-28 10:57:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     ModelIndex create_index(int row, int column, const void* data = nullptr) const; | 
					
						
							| 
									
										
										
										
											2019-03-29 04:58:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 10:57:09 +01:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     HashTable<AbstractView*> m_views; | 
					
						
							| 
									
										
										
										
											2020-07-11 06:47:26 -06:00
										 |  |  |     HashTable<ModelClient*> m_clients; | 
					
						
							| 
									
										
										
										
											2019-02-28 01:43:50 +01:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2019-03-29 14:46:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | inline ModelIndex ModelIndex::parent() const | 
					
						
							| 
									
										
										
										
											2019-03-29 14:46:53 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     return m_model ? m_model->parent_index(*this) : ModelIndex(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-29 14:46:53 +01:00
										 |  |  | } |