1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-18 08:55:07 +00:00
serenity/Libraries/LibGUI/Model.h
Andreas Kling 67b91d51a7 WindowServer+LibGUI: Pass the set of mime types being dragged to client
Previously the client would only learn the mime type of what was being
dropped on it once the drop occurred. To enable more sophisticated
filtering of drag & drop, we now pass along the list of mime types being
dragged to the client with each MouseMove event.

(Note that MouseMove is translated to the various Drag* events in LibGUI
on the client side.)
2021-01-09 12:02:07 +01:00

132 lines
4.8 KiB
C++

/*
* 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.
*/
#pragma once
#include <AK/Badge.h>
#include <AK/Function.h>
#include <AK/HashTable.h>
#include <AK/RefCounted.h>
#include <AK/String.h>
#include <LibCore/MimeData.h>
#include <LibGUI/ModelIndex.h>
#include <LibGUI/ModelRole.h>
#include <LibGUI/ModelSelection.h>
#include <LibGUI/Variant.h>
#include <LibGfx/Forward.h>
#include <LibGfx/TextAlignment.h>
namespace GUI {
enum class SortOrder {
None,
Ascending,
Descending
};
class ModelClient {
public:
virtual ~ModelClient() { }
virtual void model_did_update(unsigned flags) = 0;
};
class Model : public RefCounted<Model> {
public:
enum UpdateFlag {
DontInvalidateIndexes = 0,
InvalidateAllIndexes = 1 << 0,
};
enum MatchesFlag {
AllMatching = 0,
FirstMatchOnly = 1 << 0,
CaseInsensitive = 1 << 1,
MatchAtStart = 1 << 2,
};
virtual ~Model();
virtual int row_count(const ModelIndex& = ModelIndex()) const = 0;
virtual int column_count(const ModelIndex& = ModelIndex()) const = 0;
virtual String column_name(int) const { return {}; }
virtual Variant data(const ModelIndex&, ModelRole = ModelRole::Display) const = 0;
virtual TriState data_matches(const ModelIndex&, Variant) const { return TriState::Unknown; }
virtual void update() = 0;
virtual ModelIndex parent_index(const ModelIndex&) const { return {}; }
virtual ModelIndex index(int row, int column = 0, const ModelIndex& parent = ModelIndex()) const;
virtual bool is_editable(const ModelIndex&) const { return false; }
virtual bool is_searchable() const { return false; }
virtual void set_data(const ModelIndex&, const Variant&) { }
virtual int tree_column() const { return 0; }
virtual bool accepts_drag(const ModelIndex&, const Vector<String>& mime_types);
virtual Vector<ModelIndex, 1> matches(const StringView&, unsigned = MatchesFlag::AllMatching, const ModelIndex& = ModelIndex()) { return {}; }
virtual bool is_column_sortable([[maybe_unused]] int column_index) const { return true; }
virtual void sort([[maybe_unused]] int column, SortOrder) { }
bool is_valid(const ModelIndex& index) const
{
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);
}
virtual StringView drag_data_type() const { return {}; }
virtual RefPtr<Core::MimeData> mime_data(const ModelSelection&) const;
void register_view(Badge<AbstractView>, AbstractView&);
void unregister_view(Badge<AbstractView>, AbstractView&);
void register_client(ModelClient&);
void unregister_client(ModelClient&);
protected:
Model();
void for_each_view(Function<void(AbstractView&)>);
void did_update(unsigned flags = UpdateFlag::InvalidateAllIndexes);
static bool string_matches(const StringView& str, const StringView& needle, unsigned flags)
{
auto case_sensitivity = (flags & CaseInsensitive) ? CaseSensitivity::CaseInsensitive : CaseSensitivity::CaseSensitive;
if (flags & MatchAtStart)
return str.starts_with(needle, case_sensitivity);
return str.contains(needle, case_sensitivity);
}
ModelIndex create_index(int row, int column, const void* data = nullptr) const;
private:
HashTable<AbstractView*> m_views;
HashTable<ModelClient*> m_clients;
};
inline ModelIndex ModelIndex::parent() const
{
return m_model ? m_model->parent_index(*this) : ModelIndex();
}
}