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 <shared_mutex>
#include <vector> #include <vector>
#include <QDir>
#include <QUrl>
#include <boost/asio/post.hpp> #include <boost/asio/post.hpp>
#include <boost/asio/steady_timer.hpp> #include <boost/asio/steady_timer.hpp>
#include <boost/asio/thread_pool.hpp> #include <boost/asio/thread_pool.hpp>
@ -39,6 +41,7 @@ public:
std::string name_; std::string name_;
std::shared_ptr<gr::Placefile> placefile_; std::shared_ptr<gr::Placefile> placefile_;
bool enabled_; bool enabled_;
bool thresholded_ {true};
boost::asio::thread_pool threadPool_ {1u}; boost::asio::thread_pool threadPool_ {1u};
boost::asio::steady_timer refreshTimer_ {threadPool_}; boost::asio::steady_timer refreshTimer_ {threadPool_};
std::mutex refreshMutex_ {}; std::mutex refreshMutex_ {};
@ -54,13 +57,52 @@ public:
PlacefileManager* self_; PlacefileManager* self_;
std::vector<std::unique_ptr<PlacefileRecord>> placefileRecords_ {}; std::vector<std::shared_ptr<PlacefileRecord>> placefileRecords_ {};
std::unordered_map<std::string, std::shared_ptr<PlacefileRecord>>
placefileRecordMap_ {};
std::shared_mutex placefileRecordLock_ {}; std::shared_mutex placefileRecordLock_ {};
}; };
PlacefileManager::PlacefileManager() : p(std::make_unique<Impl>(this)) {} PlacefileManager::PlacefileManager() : p(std::make_unique<Impl>(this)) {}
PlacefileManager::~PlacefileManager() = default; 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>> std::vector<std::shared_ptr<gr::Placefile>>
PlacefileManager::GetActivePlacefiles() PlacefileManager::GetActivePlacefiles()
{ {
@ -79,6 +121,47 @@ PlacefileManager::GetActivePlacefiles()
return placefiles; 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) void PlacefileManager::LoadFile(const std::string& filename)
{ {
logger_->debug("LoadFile: {}", filename); logger_->debug("LoadFile: {}", filename);
@ -99,14 +182,13 @@ void PlacefileManager::LoadFile(const std::string& filename)
std::unique_lock lock(p->placefileRecordLock_); std::unique_lock lock(p->placefileRecordLock_);
// Determine if the placefile has been loaded previously // Determine if the placefile has been loaded previously
auto it = std::find_if(p->placefileRecords_.begin(), auto it = p->placefileRecordMap_.find(filename);
p->placefileRecords_.end(), if (it != p->placefileRecordMap_.end())
[&filename](auto& record)
{ return record->name_ == filename; });
if (it != p->placefileRecords_.end())
{ {
// If the placefile has been loaded previously, update it // If the placefile has been loaded previously, update it
(*it)->Update(placefile); it->second->Update(placefile);
lock.unlock();
Q_EMIT PlacefileUpdated(filename); Q_EMIT PlacefileUpdated(filename);
} }
@ -114,7 +196,10 @@ void PlacefileManager::LoadFile(const std::string& filename)
{ {
// If this is a new placefile, add it // If this is a new placefile, add it
auto& record = p->placefileRecords_.emplace_back( 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 PlacefileEnabled(filename, record->enabled_);
Q_EMIT PlacefileUpdated(filename); Q_EMIT PlacefileUpdated(filename);

View file

@ -19,6 +19,10 @@ public:
explicit PlacefileManager(); explicit PlacefileManager();
~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 * @brief Gets a list of active placefiles
* *
@ -26,6 +30,7 @@ public:
*/ */
std::vector<std::shared_ptr<gr::Placefile>> GetActivePlacefiles(); std::vector<std::shared_ptr<gr::Placefile>> GetActivePlacefiles();
void AddUrl(const std::string& urlString);
void LoadFile(const std::string& filename); void LoadFile(const std::string& filename);
static std::shared_ptr<PlacefileManager> Instance(); 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): case static_cast<int>(Column::Enabled):
if (role == types::ItemDataRole::SortRole) if (role == types::ItemDataRole::SortRole)
{ {
return true; // TODO return p->placefileManager_->PlacefileEnabled(placefileName);
} }
break; break;
case static_cast<int>(Column::Thresholds): case static_cast<int>(Column::Thresholds):
if (role == types::ItemDataRole::SortRole) if (role == types::ItemDataRole::SortRole)
{ {
return true; // TODO return p->placefileManager_->PlacefileThresholded(placefileName);
} }
break; break;
@ -105,7 +105,14 @@ QVariant PlacefileModel::data(const QModelIndex& index, int role) const
return QString::fromStdString(placefileName); return QString::fromStdString(placefileName);
case static_cast<int>(Column::Description): case static_cast<int>(Column::Description):
{
auto placefile = p->placefileManager_->Placefile(placefileName);
if (placefile != nullptr)
{
return QString::fromStdString(placefile->title());
}
return QString {}; return QString {};
}
default: default:
break; break;
@ -116,10 +123,10 @@ QVariant PlacefileModel::data(const QModelIndex& index, int role) const
switch (index.column()) switch (index.column())
{ {
case static_cast<int>(Column::Enabled): case static_cast<int>(Column::Enabled):
return true; // TODO return p->placefileManager_->PlacefileEnabled(placefileName);
case static_cast<int>(Column::Thresholds): case static_cast<int>(Column::Thresholds):
return true; // TODO return p->placefileManager_->PlacefileThresholded(placefileName);
default: default:
break; break;
@ -133,7 +140,7 @@ QVariant PlacefileModel::headerData(int section,
Qt::Orientation orientation, Qt::Orientation orientation,
int role) const int role) const
{ {
if (role == Qt::DisplayRole) if (role == Qt::ItemDataRole::DisplayRole)
{ {
if (orientation == Qt::Horizontal) if (orientation == Qt::Horizontal)
{ {

View file

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

View file

@ -89,8 +89,7 @@ public:
std::shared_ptr<Font> font(std::size_t i); std::shared_ptr<Font> font(std::size_t i);
static std::shared_ptr<Placefile> Load(const std::string& filename); static std::shared_ptr<Placefile> Load(const std::string& filename);
static std::shared_ptr<Placefile> Load(const std::string& name, static std::shared_ptr<Placefile> Load(std::istream& is);
std::istream& is);
private: private:
class Impl; class Impl;

View file

@ -110,16 +110,13 @@ std::shared_ptr<Placefile> Placefile::Load(const std::string& filename)
{ {
logger_->debug("Loading placefile: {}", filename); logger_->debug("Loading placefile: {}", filename);
std::ifstream f(filename, std::ios_base::in); std::ifstream f(filename, std::ios_base::in);
return Load(filename, f); return Load(f);
} }
std::shared_ptr<Placefile> Placefile::Load(const std::string& name, std::shared_ptr<Placefile> Placefile::Load(std::istream& is)
std::istream& is)
{ {
std::shared_ptr<Placefile> placefile = std::make_shared<Placefile>(); std::shared_ptr<Placefile> placefile = std::make_shared<Placefile>();
placefile->p->title_ = name;
std::string line; std::string line;
while (scwx::util::getline(is, line)) while (scwx::util::getline(is, line))
{ {