Additional placefile view/add enhancements

This commit is contained in:
Dan Paulat 2023-07-23 23:24:45 -05:00
parent 0064733679
commit 18c05b3a63
6 changed files with 119 additions and 22 deletions

View file

@ -5,6 +5,8 @@
#include <shared_mutex>
#include <vector>
#include <QDir>
#include <QUrl>
#include <boost/asio/post.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/asio/thread_pool.hpp>
@ -39,6 +41,7 @@ public:
std::string name_;
std::shared_ptr<gr::Placefile> placefile_;
bool enabled_;
bool thresholded_ {true};
boost::asio::thread_pool threadPool_ {1u};
boost::asio::steady_timer refreshTimer_ {threadPool_};
std::mutex refreshMutex_ {};
@ -54,13 +57,52 @@ public:
PlacefileManager* self_;
std::vector<std::unique_ptr<PlacefileRecord>> placefileRecords_ {};
std::shared_mutex placefileRecordLock_ {};
std::vector<std::shared_ptr<PlacefileRecord>> placefileRecords_ {};
std::unordered_map<std::string, std::shared_ptr<PlacefileRecord>>
placefileRecordMap_ {};
std::shared_mutex placefileRecordLock_ {};
};
PlacefileManager::PlacefileManager() : p(std::make_unique<Impl>(this)) {}
PlacefileManager::~PlacefileManager() = default;
bool PlacefileManager::PlacefileEnabled(const std::string& name)
{
std::shared_lock lock(p->placefileRecordLock_);
auto it = p->placefileRecordMap_.find(name);
if (it != p->placefileRecordMap_.cend())
{
return it->second->enabled_;
}
return false;
}
bool PlacefileManager::PlacefileThresholded(const std::string& name)
{
std::shared_lock lock(p->placefileRecordLock_);
auto it = p->placefileRecordMap_.find(name);
if (it != p->placefileRecordMap_.cend())
{
return it->second->thresholded_;
}
return false;
}
std::shared_ptr<const gr::Placefile>
PlacefileManager::Placefile(const std::string& name)
{
std::shared_lock lock(p->placefileRecordLock_);
auto it = p->placefileRecordMap_.find(name);
if (it != p->placefileRecordMap_.cend())
{
return it->second->placefile_;
}
return nullptr;
}
std::vector<std::shared_ptr<gr::Placefile>>
PlacefileManager::GetActivePlacefiles()
{
@ -79,6 +121,47 @@ PlacefileManager::GetActivePlacefiles()
return placefiles;
}
void PlacefileManager::AddUrl(const std::string& urlString)
{
std::string normalizedUrl;
// Normalize URL string
QUrl url = QUrl::fromUserInput(QString::fromStdString(urlString));
if (url.isLocalFile())
{
normalizedUrl = QDir::toNativeSeparators(url.toLocalFile()).toStdString();
}
else
{
normalizedUrl = urlString;
}
std::unique_lock lock(p->placefileRecordLock_);
// Determine if the placefile has been loaded previously
auto it = std::find_if(p->placefileRecords_.begin(),
p->placefileRecords_.end(),
[&normalizedUrl](auto& record)
{ return record->name_ == normalizedUrl; });
if (it != p->placefileRecords_.end())
{
logger_->debug("Placefile already added: {}", normalizedUrl);
return;
}
// Placefile is new, proceed with adding
logger_->info("AddUrl: {}", normalizedUrl);
// Add an empty placefile record for the new URL
auto placefileRecord = p->placefileRecords_.emplace_back(
std::make_shared<PlacefileRecord>(normalizedUrl, nullptr, false));
p->placefileRecordMap_.insert_or_assign(normalizedUrl, placefileRecord);
lock.unlock();
Q_EMIT PlacefileUpdated(normalizedUrl);
}
void PlacefileManager::LoadFile(const std::string& filename)
{
logger_->debug("LoadFile: {}", filename);
@ -99,14 +182,13 @@ void PlacefileManager::LoadFile(const std::string& filename)
std::unique_lock lock(p->placefileRecordLock_);
// Determine if the placefile has been loaded previously
auto it = std::find_if(p->placefileRecords_.begin(),
p->placefileRecords_.end(),
[&filename](auto& record)
{ return record->name_ == filename; });
if (it != p->placefileRecords_.end())
auto it = p->placefileRecordMap_.find(filename);
if (it != p->placefileRecordMap_.end())
{
// If the placefile has been loaded previously, update it
(*it)->Update(placefile);
it->second->Update(placefile);
lock.unlock();
Q_EMIT PlacefileUpdated(filename);
}
@ -114,7 +196,10 @@ void PlacefileManager::LoadFile(const std::string& filename)
{
// If this is a new placefile, add it
auto& record = p->placefileRecords_.emplace_back(
std::make_unique<PlacefileRecord>(filename, placefile));
std::make_shared<PlacefileRecord>(filename, placefile));
p->placefileRecordMap_.insert_or_assign(filename, record);
lock.unlock();
Q_EMIT PlacefileEnabled(filename, record->enabled_);
Q_EMIT PlacefileUpdated(filename);

View file

@ -19,6 +19,10 @@ public:
explicit PlacefileManager();
~PlacefileManager();
bool PlacefileEnabled(const std::string& name);
bool PlacefileThresholded(const std::string& name);
std::shared_ptr<const gr::Placefile> Placefile(const std::string& name);
/**
* @brief Gets a list of active placefiles
*
@ -26,6 +30,7 @@ public:
*/
std::vector<std::shared_ptr<gr::Placefile>> GetActivePlacefiles();
void AddUrl(const std::string& urlString);
void LoadFile(const std::string& filename);
static std::shared_ptr<PlacefileManager> Instance();

View file

@ -90,14 +90,14 @@ QVariant PlacefileModel::data(const QModelIndex& index, int role) const
case static_cast<int>(Column::Enabled):
if (role == types::ItemDataRole::SortRole)
{
return true; // TODO
return p->placefileManager_->PlacefileEnabled(placefileName);
}
break;
case static_cast<int>(Column::Thresholds):
if (role == types::ItemDataRole::SortRole)
{
return true; // TODO
return p->placefileManager_->PlacefileThresholded(placefileName);
}
break;
@ -105,7 +105,14 @@ QVariant PlacefileModel::data(const QModelIndex& index, int role) const
return QString::fromStdString(placefileName);
case static_cast<int>(Column::Description):
{
auto placefile = p->placefileManager_->Placefile(placefileName);
if (placefile != nullptr)
{
return QString::fromStdString(placefile->title());
}
return QString {};
}
default:
break;
@ -116,10 +123,10 @@ QVariant PlacefileModel::data(const QModelIndex& index, int role) const
switch (index.column())
{
case static_cast<int>(Column::Enabled):
return true; // TODO
return p->placefileManager_->PlacefileEnabled(placefileName);
case static_cast<int>(Column::Thresholds):
return true; // TODO
return p->placefileManager_->PlacefileThresholded(placefileName);
default:
break;
@ -133,7 +140,7 @@ QVariant PlacefileModel::headerData(int section,
Qt::Orientation orientation,
int role) const
{
if (role == Qt::DisplayRole)
if (role == Qt::ItemDataRole::DisplayRole)
{
if (orientation == Qt::Horizontal)
{

View file

@ -1,6 +1,7 @@
#include "placefile_settings_widget.hpp"
#include "ui_placefile_settings_widget.h"
#include <scwx/qt/manager/placefile_manager.hpp>
#include <scwx/qt/model/placefile_model.hpp>
#include <scwx/qt/types/qt_types.hpp>
#include <scwx/qt/ui/left_elided_item_delegate.hpp>
@ -41,6 +42,9 @@ public:
PlacefileSettingsWidget* self_;
OpenUrlDialog* openUrlDialog_;
std::shared_ptr<manager::PlacefileManager> placefileManager_ {
manager::PlacefileManager::Instance()};
model::PlacefileModel* placefileModel_;
QSortFilterProxyModel* placefileProxyModel_;
LeftElidedItemDelegate* leftElidedItemDelegate_;
@ -88,7 +92,7 @@ void PlacefileSettingsWidgetImpl::ConnectSignals()
&OpenUrlDialog::accepted,
self_,
[this]()
{ logger_->info("Add URL: {}", openUrlDialog_->url().toStdString()); });
{ placefileManager_->AddUrl(openUrlDialog_->url().toStdString()); });
QObject::connect(self_->ui->placefileFilter,
&QLineEdit::textChanged,