Placefile view enhancements

- Size enabled/threshold columns for a checkbox
- Combine URL/description columns
- Tooltips
- Alternating row colors
This commit is contained in:
Dan Paulat 2023-07-24 22:27:42 -05:00
parent 18c05b3a63
commit 3ff34caa02
5 changed files with 117 additions and 58 deletions

View file

@ -164,15 +164,18 @@ void PlacefileManager::AddUrl(const std::string& urlString)
void PlacefileManager::LoadFile(const std::string& filename)
{
logger_->debug("LoadFile: {}", filename);
const std::string placefileName =
QDir::toNativeSeparators(QString::fromStdString(filename)).toStdString();
logger_->debug("LoadFile: {}", placefileName);
boost::asio::post(
p->threadPool_,
[=, this]()
[placefileName, this]()
{
// Load file
std::shared_ptr<gr::Placefile> placefile =
gr::Placefile::Load(filename);
gr::Placefile::Load(placefileName);
if (placefile == nullptr)
{
@ -182,7 +185,7 @@ void PlacefileManager::LoadFile(const std::string& filename)
std::unique_lock lock(p->placefileRecordLock_);
// Determine if the placefile has been loaded previously
auto it = p->placefileRecordMap_.find(filename);
auto it = p->placefileRecordMap_.find(placefileName);
if (it != p->placefileRecordMap_.end())
{
// If the placefile has been loaded previously, update it
@ -190,19 +193,19 @@ void PlacefileManager::LoadFile(const std::string& filename)
lock.unlock();
Q_EMIT PlacefileUpdated(filename);
Q_EMIT PlacefileUpdated(placefileName);
}
else
{
// If this is a new placefile, add it
auto& record = p->placefileRecords_.emplace_back(
std::make_shared<PlacefileRecord>(filename, placefile));
p->placefileRecordMap_.insert_or_assign(filename, record);
std::make_shared<PlacefileRecord>(placefileName, placefile));
p->placefileRecordMap_.insert_or_assign(placefileName, record);
lock.unlock();
Q_EMIT PlacefileEnabled(filename, record->enabled_);
Q_EMIT PlacefileUpdated(filename);
Q_EMIT PlacefileEnabled(placefileName, record->enabled_);
Q_EMIT PlacefileUpdated(placefileName);
}
});
}

View file

@ -4,7 +4,10 @@
#include <scwx/util/logger.hpp>
#include <QApplication>
#include <QCheckBox>
#include <QFontMetrics>
#include <QStyle>
#include <QStyleOption>
namespace scwx
{
@ -19,7 +22,7 @@ static const auto logger_ = scwx::util::Logger::Create(logPrefix_);
static constexpr int kFirstColumn =
static_cast<int>(PlacefileModel::Column::Enabled);
static constexpr int kLastColumn =
static_cast<int>(PlacefileModel::Column::Description);
static_cast<int>(PlacefileModel::Column::Placefile);
static constexpr int kNumColumns = kLastColumn - kFirstColumn + 1;
class PlacefileModelImpl
@ -82,38 +85,69 @@ QVariant PlacefileModel::data(const QModelIndex& index, int role) const
const auto& placefileName = p->placefileNames_.at(index.row());
if (role == Qt::ItemDataRole::DisplayRole ||
role == Qt::ItemDataRole::ToolTipRole ||
role == types::ItemDataRole::SortRole)
role == Qt::ItemDataRole::ToolTipRole)
{
static const QString enabledString = QObject::tr("Enabled");
static const QString disabledString = QObject::tr("Disabled");
static const QString thresholdsEnabledString =
QObject::tr("Thresholds Enabled");
static const QString thresholdsDisabledString =
QObject::tr("Thresholds Disabled");
switch (index.column())
{
case static_cast<int>(Column::Enabled):
if (role == types::ItemDataRole::SortRole)
if (role == Qt::ItemDataRole::ToolTipRole)
{
return p->placefileManager_->PlacefileEnabled(placefileName);
return p->placefileManager_->PlacefileEnabled(placefileName) ?
enabledString :
disabledString;
}
break;
case static_cast<int>(Column::Thresholds):
if (role == types::ItemDataRole::SortRole)
if (role == Qt::ItemDataRole::ToolTipRole)
{
return p->placefileManager_->PlacefileThresholded(placefileName);
return p->placefileManager_->PlacefileThresholded(placefileName) ?
thresholdsEnabledString :
thresholdsDisabledString;
}
break;
case static_cast<int>(Column::Url):
return QString::fromStdString(placefileName);
case static_cast<int>(Column::Description):
case static_cast<int>(Column::Placefile):
{
std::string description = placefileName;
auto placefile = p->placefileManager_->Placefile(placefileName);
if (placefile != nullptr)
{
return QString::fromStdString(placefile->title());
std::string title = placefile->title();
if (!title.empty())
{
description = title + '\n' + description;
}
return QString {};
}
return QString::fromStdString(description);
}
default:
break;
}
}
else if (role == types::ItemDataRole::SortRole)
{
switch (index.column())
{
case static_cast<int>(Column::Enabled):
return p->placefileManager_->PlacefileEnabled(placefileName);
case static_cast<int>(Column::Thresholds):
return p->placefileManager_->PlacefileThresholded(placefileName);
case static_cast<int>(Column::Placefile):
return QString::fromStdString(placefileName);
default:
break;
}
@ -123,10 +157,16 @@ QVariant PlacefileModel::data(const QModelIndex& index, int role) const
switch (index.column())
{
case static_cast<int>(Column::Enabled):
return p->placefileManager_->PlacefileEnabled(placefileName);
return static_cast<int>(
p->placefileManager_->PlacefileEnabled(placefileName) ?
Qt::CheckState::Checked :
Qt::CheckState::Unchecked);
case static_cast<int>(Column::Thresholds):
return p->placefileManager_->PlacefileThresholded(placefileName);
return static_cast<int>(
p->placefileManager_->PlacefileThresholded(placefileName) ?
Qt::CheckState::Checked :
Qt::CheckState::Unchecked);
default:
break;
@ -143,6 +183,24 @@ QVariant PlacefileModel::headerData(int section,
if (role == Qt::ItemDataRole::DisplayRole)
{
if (orientation == Qt::Horizontal)
{
switch (section)
{
case static_cast<int>(Column::Enabled):
return tr("E");
case static_cast<int>(Column::Thresholds):
return tr("T");
case static_cast<int>(Column::Placefile):
return tr("Placefile");
default:
break;
}
}
}
else if (role == Qt::ItemDataRole::ToolTipRole)
{
switch (section)
{
@ -152,37 +210,31 @@ QVariant PlacefileModel::headerData(int section,
case static_cast<int>(Column::Thresholds):
return tr("Thresholds");
case static_cast<int>(Column::Url):
return tr("URL");
case static_cast<int>(Column::Description):
return tr("Description");
default:
break;
}
}
}
else if (role == Qt::ItemDataRole::SizeHintRole)
{
static const QFontMetrics fontMetrics(QApplication::font());
QSize contentsSize {};
switch (section)
{
case static_cast<int>(Column::Url):
contentsSize = fontMetrics.size(0, QString(15, 'W'));
break;
case static_cast<int>(Column::Enabled):
case static_cast<int>(Column::Thresholds):
{
static const QCheckBox checkBox {};
QStyleOptionButton option {};
option.initFrom(&checkBox);
// Width values from QCheckBox
return QApplication::style()->sizeFromContents(
QStyle::ContentsType::CT_CheckBox,
&option,
{option.iconSize.width() + 4, 0});
}
default:
break;
}
if (contentsSize != QSize {})
{
return contentsSize;
}
}
return QVariant();

View file

@ -23,8 +23,7 @@ public:
{
Enabled = 0,
Thresholds = 1,
Url = 2,
Description = 3
Placefile = 2
};
explicit PlacefileModel(QObject* parent = nullptr);

View file

@ -58,19 +58,21 @@ PlacefileSettingsWidget::PlacefileSettingsWidget(QWidget* parent) :
ui->setupUi(this);
ui->placefileView->setModel(p->placefileProxyModel_);
ui->placefileView->header()->setSortIndicator(
static_cast<int>(model::PlacefileModel::Column::Url), Qt::AscendingOrder);
ui->placefileView->resizeColumnToContents(
static_cast<int>(model::PlacefileModel::Column::Enabled));
ui->placefileView->resizeColumnToContents(
static_cast<int>(model::PlacefileModel::Column::Thresholds));
ui->placefileView->resizeColumnToContents(
static_cast<int>(model::PlacefileModel::Column::Url));
auto placefileViewHeader = ui->placefileView->header();
ui->placefileView->setItemDelegateForColumn(
static_cast<int>(model::PlacefileModel::Column::Url),
p->leftElidedItemDelegate_);
placefileViewHeader->setMinimumSectionSize(10);
placefileViewHeader->setSortIndicator(
static_cast<int>(model::PlacefileModel::Column::Placefile),
Qt::AscendingOrder);
// Enabled and Thresholds columns have a fixed size (checkbox)
placefileViewHeader->setSectionResizeMode(
static_cast<int>(model::PlacefileModel::Column::Enabled),
QHeaderView::ResizeMode::ResizeToContents);
placefileViewHeader->setSectionResizeMode(
static_cast<int>(model::PlacefileModel::Column::Thresholds),
QHeaderView::ResizeMode::ResizeToContents);
p->ConnectSignals();
}

View file

@ -16,6 +16,9 @@
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTreeView" name="placefileView">
<property name="alternatingRowColors">
<bool>true</bool>
</property>
<property name="indentation">
<number>0</number>
</property>