Create LayerModel singleton

This commit is contained in:
Dan Paulat 2023-10-28 05:54:38 -05:00
parent dd7bfc7a6f
commit b6aef07af4
3 changed files with 26 additions and 6 deletions

View file

@ -1117,6 +1117,24 @@ LayerInfo tag_invoke(boost::json::value_to_tag<LayerInfo>,
boost::json::value_to<std::array<bool, 4>>(jv.at(kDisplayedName_))}; boost::json::value_to<std::array<bool, 4>>(jv.at(kDisplayedName_))};
} }
std::shared_ptr<LayerModel> LayerModel::Instance()
{
static std::weak_ptr<LayerModel> layerModelReference_ {};
static std::mutex instanceMutex_ {};
std::unique_lock lock(instanceMutex_);
std::shared_ptr<LayerModel> layerModel = layerModelReference_.lock();
if (layerModel == nullptr)
{
layerModel = std::make_shared<LayerModel>();
layerModelReference_ = layerModel;
}
return layerModel;
}
} // namespace model } // namespace model
} // namespace qt } // namespace qt
} // namespace scwx } // namespace scwx

View file

@ -74,6 +74,8 @@ public:
const QModelIndex& destinationParent, const QModelIndex& destinationParent,
int destinationChild) override; int destinationChild) override;
static std::shared_ptr<LayerModel> Instance();
public slots: public slots:
void HandlePlacefileRemoved(const std::string& name); void HandlePlacefileRemoved(const std::string& name);
void HandlePlacefileRenamed(const std::string& oldName, void HandlePlacefileRenamed(const std::string& oldName,

View file

@ -22,10 +22,10 @@ class LayerDialogImpl
public: public:
explicit LayerDialogImpl(LayerDialog* self) : explicit LayerDialogImpl(LayerDialog* self) :
self_ {self}, self_ {self},
layerModel_ {new model::LayerModel(self)}, layerModel_ {model::LayerModel::Instance()},
layerProxyModel_ {new QSortFilterProxyModel(self_)} layerProxyModel_ {new QSortFilterProxyModel(self_)}
{ {
layerProxyModel_->setSourceModel(layerModel_); layerProxyModel_->setSourceModel(layerModel_.get());
layerProxyModel_->setFilterCaseSensitivity( layerProxyModel_->setFilterCaseSensitivity(
Qt::CaseSensitivity::CaseInsensitive); Qt::CaseSensitivity::CaseInsensitive);
layerProxyModel_->setFilterKeyColumn(-1); layerProxyModel_->setFilterKeyColumn(-1);
@ -38,9 +38,9 @@ public:
std::vector<int> GetSelectedRows(); std::vector<int> GetSelectedRows();
std::vector<std::vector<int>> GetContiguousRows(); std::vector<std::vector<int>> GetContiguousRows();
LayerDialog* self_; LayerDialog* self_;
model::LayerModel* layerModel_; std::shared_ptr<model::LayerModel> layerModel_;
QSortFilterProxyModel* layerProxyModel_; QSortFilterProxyModel* layerProxyModel_;
}; };
LayerDialog::LayerDialog(QWidget* parent) : LayerDialog::LayerDialog(QWidget* parent) :
@ -101,7 +101,7 @@ void LayerDialogImpl::ConnectSignals()
const QItemSelection& /* deselected */) const QItemSelection& /* deselected */)
{ UpdateMoveButtonsEnabled(); }); { UpdateMoveButtonsEnabled(); });
QObject::connect(layerModel_, QObject::connect(layerModel_.get(),
&QAbstractItemModel::rowsMoved, &QAbstractItemModel::rowsMoved,
self_, self_,
[this]() [this]()