Replacing ImGui Manager with ImGui Model

This commit is contained in:
Dan Paulat 2022-11-27 23:47:55 -06:00
parent 0f8b8d73f9
commit 9684aa4cdc
7 changed files with 156 additions and 113 deletions

View file

@ -60,14 +60,12 @@ set(HDR_GL_DRAW source/scwx/qt/gl/draw/draw_item.hpp
set(SRC_GL_DRAW source/scwx/qt/gl/draw/draw_item.cpp set(SRC_GL_DRAW source/scwx/qt/gl/draw/draw_item.cpp
source/scwx/qt/gl/draw/geo_line.cpp source/scwx/qt/gl/draw/geo_line.cpp
source/scwx/qt/gl/draw/rectangle.cpp) source/scwx/qt/gl/draw/rectangle.cpp)
set(HDR_MANAGER source/scwx/qt/manager/imgui_manager.hpp set(HDR_MANAGER source/scwx/qt/manager/radar_product_manager.hpp
source/scwx/qt/manager/radar_product_manager.hpp
source/scwx/qt/manager/radar_product_manager_notifier.hpp source/scwx/qt/manager/radar_product_manager_notifier.hpp
source/scwx/qt/manager/resource_manager.hpp source/scwx/qt/manager/resource_manager.hpp
source/scwx/qt/manager/settings_manager.hpp source/scwx/qt/manager/settings_manager.hpp
source/scwx/qt/manager/text_event_manager.hpp) source/scwx/qt/manager/text_event_manager.hpp)
set(SRC_MANAGER source/scwx/qt/manager/imgui_manager.cpp set(SRC_MANAGER source/scwx/qt/manager/radar_product_manager.cpp
source/scwx/qt/manager/radar_product_manager.cpp
source/scwx/qt/manager/radar_product_manager_notifier.cpp source/scwx/qt/manager/radar_product_manager_notifier.cpp
source/scwx/qt/manager/resource_manager.cpp source/scwx/qt/manager/resource_manager.cpp
source/scwx/qt/manager/settings_manager.cpp source/scwx/qt/manager/settings_manager.cpp
@ -95,12 +93,14 @@ set(SRC_MAP source/scwx/qt/map/alert_layer.cpp
source/scwx/qt/map/radar_range_layer.cpp) source/scwx/qt/map/radar_range_layer.cpp)
set(HDR_MODEL source/scwx/qt/model/alert_model.hpp set(HDR_MODEL source/scwx/qt/model/alert_model.hpp
source/scwx/qt/model/alert_proxy_model.hpp source/scwx/qt/model/alert_proxy_model.hpp
source/scwx/qt/model/imgui_context_model.hpp
source/scwx/qt/model/radar_product_model.hpp source/scwx/qt/model/radar_product_model.hpp
source/scwx/qt/model/radar_site_model.hpp source/scwx/qt/model/radar_site_model.hpp
source/scwx/qt/model/tree_item.hpp source/scwx/qt/model/tree_item.hpp
source/scwx/qt/model/tree_model.hpp) source/scwx/qt/model/tree_model.hpp)
set(SRC_MODEL source/scwx/qt/model/alert_model.cpp set(SRC_MODEL source/scwx/qt/model/alert_model.cpp
source/scwx/qt/model/alert_proxy_model.cpp source/scwx/qt/model/alert_proxy_model.cpp
source/scwx/qt/model/imgui_context_model.cpp
source/scwx/qt/model/radar_product_model.cpp source/scwx/qt/model/radar_product_model.cpp
source/scwx/qt/model/radar_site_model.cpp source/scwx/qt/model/radar_site_model.cpp
source/scwx/qt/model/tree_item.cpp source/scwx/qt/model/tree_item.cpp

View file

@ -1,92 +0,0 @@
#include <scwx/qt/manager/imgui_manager.hpp>
#include <scwx/util/logger.hpp>
#include <imgui.h>
namespace scwx
{
namespace qt
{
namespace manager
{
static const std::string logPrefix_ = "scwx::qt::manager::imgui_manager";
static const auto logger_ = scwx::util::Logger::Create(logPrefix_);
class ImGuiManagerImpl
{
public:
explicit ImGuiManagerImpl() {}
~ImGuiManagerImpl() = default;
std::vector<ImGuiContextInfo> contexts_ {};
};
ImGuiManager::ImGuiManager() :
QObject(nullptr), p {std::make_unique<ImGuiManagerImpl>()}
{
}
ImGuiManager::~ImGuiManager() {}
ImGuiContext* ImGuiManager::CreateContext(const std::string& name)
{
ImGuiContext* context = ImGui::CreateContext();
ImGui::SetCurrentContext(context);
// ImGui Configuration
auto& io = ImGui::GetIO();
// Disable automatic configuration loading/saving
io.IniFilename = nullptr;
// Style
auto& style = ImGui::GetStyle();
style.WindowMinSize = {10.0f, 10.0f};
// Register context
static size_t nextId_ {0};
p->contexts_.emplace_back(ImGuiContextInfo {nextId_++, name, context});
// Inform observers contexts have been updated
emit ContextsUpdated();
return context;
}
void ImGuiManager::DestroyContext(const std::string& name)
{
// Find context from registry
auto it = std::find_if(p->contexts_.begin(),
p->contexts_.end(),
[&](auto& info) { return info.name_ == name; });
if (it != p->contexts_.end())
{
// Destroy context and erase from index
ImGui::SetCurrentContext(it->context_);
ImGui::DestroyContext();
p->contexts_.erase(it);
// Inform observers contexts have been updated
emit ContextsUpdated();
}
}
std::vector<ImGuiContextInfo> ImGuiManager::contexts() const
{
return p->contexts_;
}
ImGuiManager& ImGuiManager::Instance()
{
static ImGuiManager instance_;
return instance_;
}
bool ImGuiContextInfo::operator==(const ImGuiContextInfo& o) const = default;
} // namespace manager
} // namespace qt
} // namespace scwx

View file

@ -1,6 +1,5 @@
#include <scwx/qt/map/map_widget.hpp> #include <scwx/qt/map/map_widget.hpp>
#include <scwx/qt/gl/gl.hpp> #include <scwx/qt/gl/gl.hpp>
#include <scwx/qt/manager/imgui_manager.hpp>
#include <scwx/qt/manager/radar_product_manager.hpp> #include <scwx/qt/manager/radar_product_manager.hpp>
#include <scwx/qt/manager/settings_manager.hpp> #include <scwx/qt/manager/settings_manager.hpp>
#include <scwx/qt/map/alert_layer.hpp> #include <scwx/qt/map/alert_layer.hpp>
@ -9,6 +8,7 @@
#include <scwx/qt/map/overlay_layer.hpp> #include <scwx/qt/map/overlay_layer.hpp>
#include <scwx/qt/map/radar_product_layer.hpp> #include <scwx/qt/map/radar_product_layer.hpp>
#include <scwx/qt/map/radar_range_layer.hpp> #include <scwx/qt/map/radar_range_layer.hpp>
#include <scwx/qt/model/imgui_context_model.hpp>
#include <scwx/qt/view/radar_product_view_factory.hpp> #include <scwx/qt/view/radar_product_view_factory.hpp>
#include <scwx/util/logger.hpp> #include <scwx/util/logger.hpp>
#include <scwx/util/threads.hpp> #include <scwx/util/threads.hpp>
@ -87,7 +87,7 @@ public:
static size_t currentMapId_ {0u}; static size_t currentMapId_ {0u};
imGuiContextName_ = std::format("Map {}", ++currentMapId_); imGuiContextName_ = std::format("Map {}", ++currentMapId_);
imGuiContext_ = imGuiContext_ =
manager::ImGuiManager::Instance().CreateContext(imGuiContextName_); model::ImGuiContextModel::Instance().CreateContext(imGuiContextName_);
// Initialize ImGui Qt backend // Initialize ImGui Qt backend
ImGui_ImplQt_Init(); ImGui_ImplQt_Init();
@ -107,7 +107,7 @@ public:
ImGui_ImplQt_Shutdown(); ImGui_ImplQt_Shutdown();
// Destroy ImGui Context // Destroy ImGui Context
manager::ImGuiManager::Instance().DestroyContext(imGuiContextName_); model::ImGuiContextModel::Instance().DestroyContext(imGuiContextName_);
} }
void AddLayer(const std::string& id, void AddLayer(const std::string& id,

View file

@ -0,0 +1,125 @@
#include <scwx/qt/model/imgui_context_model.hpp>
#include <scwx/util/logger.hpp>
#include <imgui.h>
namespace scwx
{
namespace qt
{
namespace model
{
static const std::string logPrefix_ = "scwx::qt::model::imgui_context_model";
static const auto logger_ = scwx::util::Logger::Create(logPrefix_);
class ImGuiContextModelImpl
{
public:
explicit ImGuiContextModelImpl() {}
~ImGuiContextModelImpl() = default;
std::vector<ImGuiContextInfo> contexts_ {};
};
ImGuiContextModel::ImGuiContextModel() :
QAbstractListModel(nullptr), p {std::make_unique<ImGuiContextModelImpl>()}
{
}
ImGuiContextModel::~ImGuiContextModel() {}
int ImGuiContextModel::rowCount(const QModelIndex& parent) const
{
return parent.isValid() ? 0 : static_cast<int>(p->contexts_.size());
}
QVariant ImGuiContextModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid())
{
return {};
}
const int row = index.row();
if (row >= p->contexts_.size() || row < 0)
{
return {};
}
switch (role)
{
case Qt::ItemDataRole::DisplayRole:
return QString("%1: %2")
.arg(p->contexts_[row].id_)
.arg(p->contexts_[row].name_.c_str());
}
return {};
}
ImGuiContext* ImGuiContextModel::CreateContext(const std::string& name)
{
static size_t nextId_ {0};
ImGuiContext* context = ImGui::CreateContext();
ImGui::SetCurrentContext(context);
// ImGui Configuration
auto& io = ImGui::GetIO();
// Disable automatic configuration loading/saving
io.IniFilename = nullptr;
// Style
auto& style = ImGui::GetStyle();
style.WindowMinSize = {10.0f, 10.0f};
// Register context
const int nextPosition = static_cast<int>(p->contexts_.size());
beginInsertRows(QModelIndex(), nextPosition, nextPosition);
p->contexts_.emplace_back(ImGuiContextInfo {nextId_++, name, context});
endInsertRows();
return context;
}
void ImGuiContextModel::DestroyContext(const std::string& name)
{
// Find context from registry
auto it = std::find_if(p->contexts_.begin(),
p->contexts_.end(),
[&](auto& info) { return info.name_ == name; });
if (it != p->contexts_.end())
{
const int position = it - p->contexts_.begin();
// Destroy context
ImGui::SetCurrentContext(it->context_);
ImGui::DestroyContext();
// Erase context from index
beginRemoveRows(QModelIndex(), position, position);
p->contexts_.erase(it);
endRemoveRows();
}
}
std::vector<ImGuiContextInfo> ImGuiContextModel::contexts() const
{
return p->contexts_;
}
ImGuiContextModel& ImGuiContextModel::Instance()
{
static ImGuiContextModel instance_ {};
return instance_;
}
bool ImGuiContextInfo::operator==(const ImGuiContextInfo& o) const = default;
} // namespace model
} // namespace qt
} // namespace scwx

View file

@ -3,7 +3,7 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include <QObject> #include <QAbstractListModel>
struct ImGuiContext; struct ImGuiContext;
@ -11,10 +11,10 @@ namespace scwx
{ {
namespace qt namespace qt
{ {
namespace manager namespace model
{ {
class ImGuiManagerImpl; class ImGuiContextModelImpl;
struct ImGuiContextInfo struct ImGuiContextInfo
{ {
@ -25,31 +25,36 @@ struct ImGuiContextInfo
bool operator==(const ImGuiContextInfo& o) const; bool operator==(const ImGuiContextInfo& o) const;
}; };
class ImGuiManager : public QObject class ImGuiContextModel : public QAbstractListModel
{ {
private: private:
Q_OBJECT Q_OBJECT
Q_DISABLE_COPY(ImGuiManager) Q_DISABLE_COPY(ImGuiContextModel)
public: public:
explicit ImGuiManager(); explicit ImGuiContextModel();
~ImGuiManager(); ~ImGuiContextModel();
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
QVariant data(const QModelIndex& index,
int role = Qt::DisplayRole) const override;
ImGuiContext* CreateContext(const std::string& name); ImGuiContext* CreateContext(const std::string& name);
void DestroyContext(const std::string& name); void DestroyContext(const std::string& name);
std::vector<ImGuiContextInfo> contexts() const; std::vector<ImGuiContextInfo> contexts() const;
static ImGuiManager& Instance(); static ImGuiContextModel& Instance();
signals: signals:
void ContextsUpdated(); void ContextsUpdated();
private: private:
friend class ImGuiManagerImpl; friend class ImGuiContextModelImpl;
std::unique_ptr<ImGuiManagerImpl> p; std::unique_ptr<ImGuiContextModelImpl> p;
}; };
} // namespace manager } // namespace model
} // namespace qt } // namespace qt
} // namespace scwx } // namespace scwx

View file

@ -1,6 +1,7 @@
#include "imgui_debug_dialog.hpp" #include "imgui_debug_dialog.hpp"
#include "ui_imgui_debug_dialog.h" #include "ui_imgui_debug_dialog.h"
#include <scwx/qt/model/imgui_context_model.hpp>
#include <scwx/qt/ui/imgui_debug_widget.hpp> #include <scwx/qt/ui/imgui_debug_widget.hpp>
namespace scwx namespace scwx
@ -33,6 +34,9 @@ ImGuiDebugDialog::ImGuiDebugDialog(QWidget* parent) :
p->imGuiDebugWidget_->setSizePolicy(QSizePolicy::Policy::Expanding, p->imGuiDebugWidget_->setSizePolicy(QSizePolicy::Policy::Expanding,
QSizePolicy::Policy::Expanding); QSizePolicy::Policy::Expanding);
ui->verticalLayout->insertWidget(0, p->imGuiDebugWidget_); ui->verticalLayout->insertWidget(0, p->imGuiDebugWidget_);
// Context Combo Box
ui->contextComboBox->setModel(&model::ImGuiContextModel::Instance());
} }
ImGuiDebugDialog::~ImGuiDebugDialog() ImGuiDebugDialog::~ImGuiDebugDialog()

View file

@ -1,5 +1,5 @@
#include <scwx/qt/ui/imgui_debug_widget.hpp> #include <scwx/qt/ui/imgui_debug_widget.hpp>
#include <scwx/qt/manager/imgui_manager.hpp> #include <scwx/qt/model/imgui_context_model.hpp>
#include <imgui.h> #include <imgui.h>
#include <backends/imgui_impl_opengl3.h> #include <backends/imgui_impl_opengl3.h>
@ -22,7 +22,8 @@ public:
// Create ImGui Context // Create ImGui Context
static size_t currentIndex_ {0u}; static size_t currentIndex_ {0u};
contextName_ = std::format("ImGui Debug {}", ++currentIndex_); contextName_ = std::format("ImGui Debug {}", ++currentIndex_);
context_ = manager::ImGuiManager::Instance().CreateContext(contextName_); context_ =
model::ImGuiContextModel::Instance().CreateContext(contextName_);
// Initialize ImGui Qt backend // Initialize ImGui Qt backend
ImGui_ImplQt_Init(); ImGui_ImplQt_Init();
@ -42,7 +43,7 @@ public:
ImGui_ImplQt_Shutdown(); ImGui_ImplQt_Shutdown();
// Destroy ImGui Context // Destroy ImGui Context
manager::ImGuiManager::Instance().DestroyContext(contextName_); model::ImGuiContextModel::Instance().DestroyContext(contextName_);
} }
ImGuiDebugWidget* self_; ImGuiDebugWidget* self_;