Some minor refactor for the UI Models code.

This commit is contained in:
Martín Lucas Golini
2020-07-18 01:40:56 -03:00
parent b6f2106f06
commit fe1efd15c2
16 changed files with 166 additions and 143 deletions

View File

@@ -1,15 +1,17 @@
#ifndef EE_UI_UIABSTRACTVIEW_HPP
#define EE_UI_UIABSTRACTVIEW_HPP
#include <eepp/ui/abstract/model.hpp>
#include <eepp/ui/abstract/modeleditingdelegate.hpp>
#include <eepp/ui/abstract/modelselection.hpp>
#include <eepp/ui/models/model.hpp>
#include <eepp/ui/models/modeleditingdelegate.hpp>
#include <eepp/ui/models/modelselection.hpp>
#include <eepp/ui/uiscrollablewidget.hpp>
#include <memory>
using namespace EE::UI::Models;
namespace EE { namespace UI { namespace Abstract {
class ModelEvent : public Event {
class EE_API ModelEvent : public Event {
public:
ModelEvent( Model* model, const ModelIndex& index, Node* node ) :
Event( node, Event::OnModelEvent ), model( model ), index( index ) {}
@@ -52,7 +54,7 @@ class EE_API UIAbstractView : public UIScrollableWidget {
void setOnSelection( const std::function<void( const ModelIndex& )>& onSelection );
protected:
friend class Model;
friend class EE::UI::Models::Model;
virtual void onModelUpdate( unsigned flags );

View File

@@ -0,0 +1,103 @@
#ifndef EE_UI_MODEL_MODEL_HPP
#define EE_UI_MODEL_MODEL_HPP
#include <eepp/ui/models/modelindex.hpp>
#include <eepp/ui/models/variant.hpp>
#include <functional>
#include <string>
#include <unordered_set>
using namespace EE::Graphics;
using namespace EE::Math;
namespace EE { namespace UI { namespace Abstract {
class UIAbstractView;
}}} // namespace EE::UI::Abstract
using namespace EE::UI::Abstract;
namespace EE { namespace UI { namespace Models {
enum class SortOrder { None, Ascending, Descending };
class EE_API Model {
public:
enum UpdateFlag {
DontInvalidateIndexes = 0,
InvalidateAllIndexes = 1 << 0,
};
enum class Role {
Display,
Icon,
};
virtual ~Model(){};
virtual size_t rowCount( const ModelIndex& = ModelIndex() ) const = 0;
virtual size_t columnCount( const ModelIndex& = ModelIndex() ) const = 0;
virtual std::string columnName( const size_t& /*column*/ ) const { return {}; }
virtual Variant data( const ModelIndex&, Role = Role::Display ) const = 0;
virtual void update() = 0;
virtual ModelIndex parentIndex( const ModelIndex& ) const { return {}; }
virtual ModelIndex index( int row, int column = 0, const ModelIndex& = ModelIndex() ) const {
return createIndex( row, column );
}
virtual ModelIndex sibling( int row, int column, const ModelIndex& parent ) const;
virtual void setData( const ModelIndex&, const Variant& ) {}
virtual size_t treeColumn() const { return 0; }
virtual bool acceptsDrag( const ModelIndex&, const std::string& dataType );
virtual bool isColumnSortable( const size_t& /*columnIndex*/ ) const { return true; }
virtual std::string dragDataType() const { return {}; }
bool isValid( const ModelIndex& index ) const {
auto parentIndex = this->parentIndex( index );
return index.row() >= 0 && index.row() < (Int64)rowCount( parentIndex ) &&
index.column() >= 0 && index.column() < (Int64)columnCount( parentIndex );
}
virtual int keyColumn() const { return -1; }
virtual SortOrder sortOrder() const { return SortOrder::None; }
virtual void setKeyColumnAndSortOrder( const size_t& /*column*/, const SortOrder& /*order*/ ) {}
void registerView( UIAbstractView* );
void unregisterView( UIAbstractView* );
void setOnUpdate( const std::function<void()>& onUpdate );
protected:
Model(){};
void forEachView( std::function<void( UIAbstractView* )> );
void onModelUpdate( unsigned flags = UpdateFlag::InvalidateAllIndexes );
ModelIndex createIndex( int row, int column, const void* data = nullptr ) const;
private:
std::unordered_set<UIAbstractView*> mViews;
std::function<void()> mOnUpdate;
};
inline ModelIndex ModelIndex::parent() const {
return mModel ? mModel->parentIndex( *this ) : ModelIndex();
}
}}} // namespace EE::UI::Model
#endif // EE_UI_MODEL_MODEL_HPP

View File

@@ -1,11 +1,11 @@
#ifndef EE_UI_MODELEDITINGDELEGATE_HPP
#define EE_UI_MODELEDITINGDELEGATE_HPP
#include <eepp/ui/abstract/model.hpp>
#include <eepp/ui/models/model.hpp>
#include <eepp/ui/uiwidget.hpp>
#include <memory>
namespace EE { namespace UI { namespace Abstract {
namespace EE { namespace UI { namespace Models {
class EE_API ModelEditingDelegate {
public:
@@ -43,6 +43,6 @@ class EE_API ModelEditingDelegate {
UIWidget* mWidget;
};
}}} // namespace EE::UI::Abstract
}}} // namespace EE::UI::Model
#endif // EE_UI_MODELEDITINGDELEGATE_HPP

View File

@@ -3,7 +3,7 @@
#include <eepp/config.hpp>
namespace EE { namespace UI { namespace Abstract {
namespace EE { namespace UI { namespace Models {
class Model;
@@ -45,6 +45,6 @@ class EE_API ModelIndex {
mModel( &model ), mRow( row ), mColumn( column ), mData( internalData ) {}
};
}}} // namespace EE::UI::Abstract
}}} // namespace EE::UI::Model
#endif // EE_UI_MODEL_MODELINDEX_HPP

View File

@@ -2,13 +2,17 @@
#define EE_UI_MODEL_MODELSELECTION_HPP
#include <algorithm>
#include <eepp/ui/abstract/modelindex.hpp>
#include <eepp/ui/models/modelindex.hpp>
#include <functional>
#include <unordered_set>
namespace EE { namespace UI { namespace Abstract {
class UIAbstractView;
}}} // namespace EE::UI::Abstract
using namespace EE::UI::Abstract;
namespace EE { namespace UI { namespace Models {
class EE_API ModelSelection {
public:
@@ -63,6 +67,6 @@ class EE_API ModelSelection {
std::vector<ModelIndex> mIndexes;
};
}}} // namespace EE::UI::Abstract
}}} // namespace EE::UI::Model
#endif // EE_UI_MODEL_MODELSELECTION_HPP

View File

@@ -1,24 +1,17 @@
#ifndef EE_UI_MODEL_MODEL_HPP
#define EE_UI_MODEL_MODEL_HPP
#ifndef EE_UI_MODEL_VARIANT_HPP
#define EE_UI_MODEL_VARIANT_HPP
#include <eepp/core/core.hpp>
#include <eepp/graphics/drawable.hpp>
#include <eepp/math/rect.hpp>
#include <eepp/ui/abstract/modelindex.hpp>
#include <functional>
#include <string>
#include <unordered_set>
using namespace EE::Graphics;
using namespace EE::Math;
namespace EE { namespace UI { namespace Abstract {
namespace EE { namespace UI { namespace Models {
enum class SortOrder { None, Ascending, Descending };
class UIAbstractView;
class Variant {
class EE_API Variant {
public:
enum class Type {
Invalid,
@@ -101,84 +94,6 @@ class Variant {
bool mOwnsObject{false};
};
class EE_API Model {
public:
enum UpdateFlag {
DontInvalidateIndexes = 0,
InvalidateAllIndexes = 1 << 0,
};
}}}
enum class Role {
Display,
Icon,
};
virtual ~Model(){};
virtual size_t rowCount( const ModelIndex& = ModelIndex() ) const = 0;
virtual size_t columnCount( const ModelIndex& = ModelIndex() ) const = 0;
virtual std::string columnName( const size_t& /*column*/ ) const { return {}; }
virtual Variant data( const ModelIndex&, Role = Role::Display ) const = 0;
virtual void update() = 0;
virtual ModelIndex parentIndex( const ModelIndex& ) const { return {}; }
virtual ModelIndex index( int row, int column = 0, const ModelIndex& = ModelIndex() ) const {
return createIndex( row, column );
}
virtual ModelIndex sibling( int row, int column, const ModelIndex& parent ) const;
virtual void setData( const ModelIndex&, const Variant& ) {}
virtual size_t treeColumn() const { return 0; }
virtual bool acceptsDrag( const ModelIndex&, const std::string& dataType );
virtual bool isColumnSortable( const size_t& /*columnIndex*/ ) const { return true; }
virtual std::string dragDataType() const { return {}; }
bool isValid( const ModelIndex& index ) const {
auto parentIndex = this->parentIndex( index );
return index.row() >= 0 && index.row() < (Int64)rowCount( parentIndex ) &&
index.column() >= 0 && index.column() < (Int64)columnCount( parentIndex );
}
virtual int keyColumn() const { return -1; }
virtual SortOrder sortOrder() const { return SortOrder::None; }
virtual void setKeyColumnAndSortOrder( const size_t& /*column*/, const SortOrder& /*order*/ ) {}
void registerView( UIAbstractView* );
void unregisterView( UIAbstractView* );
void setOnUpdate( const std::function<void()>& onUpdate );
protected:
Model(){};
void forEachView( std::function<void( UIAbstractView* )> );
void onModelUpdate( unsigned flags = UpdateFlag::InvalidateAllIndexes );
ModelIndex createIndex( int row, int column, const void* data = nullptr ) const;
private:
std::unordered_set<UIAbstractView*> mViews;
std::function<void()> mOnUpdate;
};
inline ModelIndex ModelIndex::parent() const {
return mModel ? mModel->parentIndex( *this ) : ModelIndex();
}
}}} // namespace EE::UI::Abstract
#endif // EE_UI_MODEL_MODEL_HPP
#endif // EE_UI_MODEL_VARIANT_HPP

View File

@@ -749,6 +749,7 @@ function build_eepp( build_name )
"src/eepp/scene/actions/*.cpp",
"src/eepp/ui/*.cpp",
"src/eepp/ui/abstract/*.cpp",
"src/eepp/ui/models/*.cpp",
"src/eepp/ui/css/*.cpp",
"src/eepp/ui/doc/*.cpp",
"src/eepp/ui/tools/*.cpp",

View File

@@ -470,6 +470,7 @@ function build_eepp( build_name )
"src/eepp/scene/actions/*.cpp",
"src/eepp/ui/*.cpp",
"src/eepp/ui/abstract/*.cpp",
"src/eepp/ui/models/*.cpp",
"src/eepp/ui/css/*.cpp",
"src/eepp/ui/doc/*.cpp",
"src/eepp/ui/tools/*.cpp",

View File

@@ -82,7 +82,8 @@ CODE_SRCS := \
ui/*.cpp \
ui/css/*.cpp \
ui/doc/*.cpp \
ui/model/*.cpp \
ui/abstract/*.cpp \
ui/models/*.cpp \
ui/tools/*.cpp \
maps/*.cpp \
maps/mapeditor/*.cpp

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.12.2, 2020-07-04T20:24:45. -->
<!-- Written by QtCreator 4.12.2, 2020-07-18T01:31:47. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
@@ -89,7 +89,7 @@
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">{6d057187-158a-4883-8d5b-d470a6b6b025}</value>
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">10</value>
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">19</value>
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">18</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">../../make/linux</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">

View File

@@ -295,14 +295,14 @@
../../include/eepp/thirdparty/chipmunk/cpSpatialIndex.h
../../include/eepp/thirdparty/chipmunk/cpVect.h
../../include/eepp/thirdparty/PlusCallback/callback.hpp
../../include/eepp/ui/abstract/model.hpp
../../include/eepp/ui/models/model.hpp
../../include/eepp/ui/abstract/uiabstracttableview.hpp
../../include/eepp/ui/abstract/uiabstractview.hpp
../../include/eepp/ui/base.hpp
../../include/eepp/ui.hpp
../../include/eepp/ui/abstract/modeleditingdelegate.hpp
../../include/eepp/ui/abstract/modelindex.hpp
../../include/eepp/ui/abstract/modelselection.hpp
../../include/eepp/ui/models/modeleditingdelegate.hpp
../../include/eepp/ui/models/modelindex.hpp
../../include/eepp/ui/models/modelselection.hpp
../../include/eepp/ui/border.hpp
../../include/eepp/ui/css/animationdefinition.hpp
../../include/eepp/ui/css/drawableimageparser.hpp
@@ -338,9 +338,9 @@
../../include/eepp/ui/doc/undostack.hpp
../../include/eepp/ui/keyboardshortcut.hpp
../../include/eepp/ui/marginmove/scale.hpp
../../include/eepp/ui/abstract/model.hpp
../../include/eepp/ui/abstract/modelindex.hpp
../../include/eepp/ui/abstract/modelselection.hpp
../../include/eepp/ui/models/model.hpp
../../include/eepp/ui/models/modelindex.hpp
../../include/eepp/ui/models/modelselection.hpp
../../include/eepp/ui/tools/textureatlaseditor.hpp
../../include/eepp/ui/tools/uicodeeditorsplitter.hpp
../../include/eepp/ui/tools/uicolorpicker.hpp
@@ -770,8 +770,8 @@
../../src/eepp/system/virtualfilesystem.cpp
../../src/eepp/system/zip.cpp
../../src/eepp/ui/abstract/filesystemmodel.hpp
../../src/eepp/ui/abstract/model.cpp
../../src/eepp/ui/abstract/modelselection.cpp
../../src/eepp/ui/models/model.cpp
../../src/eepp/ui/models/modelselection.cpp
../../src/eepp/ui/abstract/uiabstracttableview.cpp
../../src/eepp/ui/abstract/uiabstractview.cpp
../../src/eepp/ui/border.cpp
@@ -804,8 +804,8 @@
../../src/eepp/ui/doc/textdocument.cpp
../../src/eepp/ui/doc/undostack.cpp
../../src/eepp/ui/keyboardshortcut.cpp
../../src/eepp/ui/abstract/model.cpp
../../src/eepp/ui/abstract/modelselection.cpp
../../src/eepp/ui/models/model.cpp
../../src/eepp/ui/models/modelselection.cpp
../../src/eepp/ui/tools/textureatlaseditor.cpp
../../src/eepp/ui/tools/textureatlasnew.cpp
../../src/eepp/ui/tools/textureatlasnew.hpp

View File

@@ -1,7 +1,7 @@
#include <eepp/ui/abstract/model.hpp>
#include <eepp/ui/abstract/uiabstractview.hpp>
#include <eepp/ui/models/model.hpp>
namespace EE { namespace UI { namespace Abstract {
namespace EE { namespace UI { namespace Models {
void Model::onModelUpdate( unsigned flags ) {
if ( mOnUpdate )
@@ -43,4 +43,4 @@ bool Model::acceptsDrag( const ModelIndex&, const std::string& ) {
return false;
}
}}} // namespace EE::UI::Abstract
}}} // namespace EE::UI::Model

View File

@@ -1,8 +1,8 @@
#include <eepp/core.hpp>
#include <eepp/ui/abstract/modelselection.hpp>
#include <eepp/ui/models/modelselection.hpp>
#include <eepp/ui/abstract/uiabstractview.hpp>
namespace EE { namespace UI { namespace Abstract {
namespace EE { namespace UI { namespace Models {
void ModelSelection::removeMatching( std::function<bool( const ModelIndex& )> filter ) {
std::vector<std::vector<ModelIndex>::iterator> toRemove;
@@ -59,4 +59,4 @@ void ModelSelection::clear() {
mView->notifySelectionChange();
}
}}} // namespace EE::UI::Abstract
}}} // namespace EE::UI::Model

View File

@@ -1839,26 +1839,23 @@ void UICodeEditor::drawWhitespaces( const std::pair<int, int>& lineRange,
cpoint->setColor( color );
for ( int index = lineRange.first; index <= lineRange.second; index++ ) {
Vector2f position( {startScroll.x, startScroll.y + lineHeight * index} );
auto& tokens = mHighlighter.getLine( index );
for ( auto& token : tokens ) {
Float textWidth = getTextWidth( token.text );
if ( position.x + textWidth >= mScreenPos.x &&
const auto& text = mDoc->line( index ).getText();
for ( size_t i = 0; i < text.size(); i++ ) {
if ( position.x + glyphW >= mScreenPos.x &&
position.x <= mScreenPos.x + mSize.getWidth() ) {
for ( size_t i = 0; i < token.text.size(); i++ ) {
if ( ' ' == token.text[i] ) {
cpoint->draw( Vector2f( position.x, position.y ) );
position.x += glyphW;
} else if ( '\t' == token.text[i] ) {
adv->draw( Vector2f( position.x + tabCenter, position.y ) );
position.x += tabWidth;
} else {
position.x += glyphW;
}
if ( ' ' == text[i] ) {
cpoint->draw( Vector2f( position.x, position.y ) );
position.x += glyphW;
} else if ( '\t' == text[i] ) {
adv->draw( Vector2f( position.x + tabCenter, position.y ) );
position.x += tabWidth;
} else {
position.x += glyphW;
}
} else if ( position.x > mScreenPos.x + mSize.getWidth() ) {
break;
} else {
position.x += textWidth;
position.x += glyphW;
}
}
}

View File

@@ -30,9 +30,8 @@ UITreeView::MetadataForIndex& UITreeView::getIndexMetadata( const ModelIndex& in
if ( it != mViewMetadata.end() )
return it->second;
auto newMetadata = MetadataForIndex();
auto* ref = &newMetadata;
mViewMetadata.insert( {index.data(), std::move( newMetadata )} );
return *ref;
return mViewMetadata[index.data()];
}
template <typename Callback> void UITreeView::traverseTree( Callback callback ) const {
@@ -183,8 +182,8 @@ UIWidget* UITreeView::createCell( UIWidget* rowWidget, const ModelIndex&, const
if ( pos >= Vector2f::Zero && pos <= icon->getPixelsSize() ) {
auto idx =
mouseEvent->getNode()->getParent()->asType<UITableRow>()->getCurIndex();
auto& data = getIndexMetadata( idx );
if ( getModel()->rowCount( idx ) ) {
auto& data = getIndexMetadata( idx );
data.open = !data.open;
createOrUpdateColumns();
}

View File

@@ -1,5 +1,5 @@
#include <eepp/ee.hpp>
#include <eepp/ui/abstract/model.hpp>
#include <eepp/ui/models/model.hpp>
#include <eepp/ui/abstract/uiabstracttableview.hpp>
#include <eepp/ui/uitreeview.hpp>