Implement workaround map widget

This commit is contained in:
Alex Spataru 2021-10-19 01:20:28 -05:00
parent 39b605b128
commit e92aa3cce7
4 changed files with 209 additions and 13 deletions

View File

@ -147,7 +147,7 @@ DataGroup::DataGroup(const int index)
setLayout(m_mainLayout);
// React to dashboard events
connect(dash, SIGNAL(updated()), this, SLOT(update()));
connect(dash, SIGNAL(updated()), this, SLOT(updateData()));
}
/**
@ -164,6 +164,9 @@ DataGroup::~DataGroup()
foreach (auto value, m_values)
delete value;
foreach (auto units, m_units)
delete units;
delete m_gridLayout;
delete m_scrollArea;
delete m_mainLayout;
@ -176,7 +179,7 @@ DataGroup::~DataGroup()
* If the widget is disabled (e.g. the user hides it, or the external
* window is hidden), then the widget shall ignore the update request.
*/
void DataGroup::update()
void DataGroup::updateData()
{
// Widget not enabled, do nothing
if (!isEnabled())

View File

@ -40,7 +40,7 @@ public:
~DataGroup();
private slots:
void update();
void updateData();
protected:
void resizeEvent(QResizeEvent *event);

View File

@ -24,6 +24,10 @@
#include "UI/Dashboard.h"
#include "Misc/ThemeManager.h"
#include <QMouseEvent>
#include <QResizeEvent>
#include <QDesktopServices>
using namespace Widgets;
/**
@ -31,6 +35,8 @@ using namespace Widgets;
*/
Map::Map(const int index)
: m_index(index)
, m_lat(0)
, m_lon(0)
{
// Get pointers to serial studio modules
auto dash = UI::Dashboard::getInstance();
@ -40,20 +46,137 @@ Map::Map(const int index)
if (m_index < 0 || m_index >= dash->mapCount())
return;
// Set window palette
QPalette palette;
palette.setColor(QPalette::Base, theme->widgetWindowBackground());
palette.setColor(QPalette::Window, theme->widgetWindowBackground());
setPalette(palette);
// Generate widget stylesheets
auto labelQSS = QSS("color:%1", theme->placeholderText());
auto unitsQSS = QSS("color:%1", theme->widgetTextSecondary());
auto titleQSS = QSS("color:%1", theme->widgetTextPrimary());
auto valueQSS = QSS("color:%1", theme->widgetForegroundPrimary());
auto iconsQSS = QSS("color:%1; font-weight:600;", theme->widgetTextSecondary());
// Configure layout
m_layout.setContentsMargins(12, 12, 12, 12);
// Set window palette
QPalette windowPalette;
windowPalette.setColor(QPalette::Base, theme->widgetWindowBackground());
windowPalette.setColor(QPalette::Window, theme->widgetWindowBackground());
setPalette(windowPalette);
// Configure scroll area container
m_dataContainer = new QWidget(this);
// Make the value label larger
auto valueFont = dash->monoFont();
valueFont.setPixelSize(dash->monoFont().pixelSize() * 1.3);
// Configure grid layout
m_gridLayout = new QGridLayout(m_dataContainer);
for (int i = 0; i < 2; ++i)
{
// Create labels
m_units.append(new QLabel(m_dataContainer));
m_icons.append(new QLabel(m_dataContainer));
m_titles.append(new QLabel(m_dataContainer));
m_values.append(new QLabel(m_dataContainer));
// Get pointers to labels
auto dicon = m_icons.last();
auto units = m_units.last();
auto title = m_titles.last();
auto value = m_values.last();
// Set label alignments
units->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
value->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
title->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
dicon->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
// Set label styles & fonts
value->setFont(valueFont);
title->setFont(dash->monoFont());
title->setStyleSheet(titleQSS);
value->setStyleSheet(valueQSS);
dicon->setStyleSheet(iconsQSS);
units->setStyleSheet(unitsQSS);
// Set icon & units text
dicon->setText("");
units->setText("[°]");
// Add labels to grid layout
m_gridLayout->addWidget(title, i, 0);
m_gridLayout->addWidget(dicon, i, 1);
m_gridLayout->addWidget(value, i, 2);
m_gridLayout->addWidget(units, i, 3);
// Set latitude values
if (i == 0)
{
title->setText(tr("Latitude"));
value->setText(QString::number(m_lat, 'f', 4));
}
// Set longitude values
else if (i == 1)
{
title->setText(tr("Longitude"));
value->setText(QString::number(m_lon, 'f', 4));
}
}
// Load layout into container widget
m_gridLayout->setColumnStretch(0, 2);
m_gridLayout->setColumnStretch(1, 1);
m_gridLayout->setColumnStretch(2, 2);
m_gridLayout->setColumnStretch(3, 0);
m_dataContainer->setLayout(m_gridLayout);
// Configure map labeL
m_mapLabel.setStyleSheet(labelQSS);
m_mapLabel.setText(tr("Double-click to open map"));
// Load grid layout into main layout
m_layout.addWidget(m_dataContainer);
m_layout.addWidget(&m_mapLabel);
m_layout.setStretch(0, 0);
m_layout.setStretch(1, 1);
m_layout.setAlignment(&m_mapLabel, Qt::AlignHCenter | Qt::AlignVCenter);
m_layout.setContentsMargins(24, 48, 24, 24);
setLayout(&m_layout);
// React to dashboard events
// React to Qt signals
connect(dash, SIGNAL(updated()), this, SLOT(updateData()));
}
/**
* Frees the memory allocated for each label that represents a dataset
*/
Map::~Map()
{
foreach (auto icon, m_icons)
delete icon;
foreach (auto title, m_titles)
delete title;
foreach (auto value, m_values)
delete value;
foreach (auto units, m_units)
delete units;
delete m_gridLayout;
}
/**
* Opens the current latitude/longitude in Google Maps
*/
void Map::openMap()
{
QString lat = QString::number(m_lat, 'f', 6);
QString lon = QString::number(m_lon, 'f', 6);
auto url
= QString("https://www.google.com/maps/search/?api=1&query=%1,%2").arg(lat, lon);
QDesktopServices::openUrl(QUrl(url));
}
/**
* Checks if the widget is enabled, if so, the widget shall be updated
* to display the latest data frame.
@ -69,7 +192,7 @@ void Map::updateData()
// Get group pointer
auto dash = UI::Dashboard::getInstance();
auto group = dash->getGroups(m_index);
auto group = dash->getMap(m_index);
if (!group)
return;
@ -88,5 +211,57 @@ void Map::updateData()
}
}
// Update map coordinates
// Update latitude/longitude labels
if (m_lat != lat || m_lon != lon)
{
m_lat = lat;
m_lon = lon;
auto latLabel = m_values[0];
auto lonLabel = m_values[1];
latLabel->setText(QString::number(lat, 'f', 4));
lonLabel->setText(QString::number(lon, 'f', 4));
}
}
/**
* Changes the size of the labels when the widget is resized
*/
void Map::resizeEvent(QResizeEvent *event)
{
auto width = event->size().width();
QFont font = UI::Dashboard::getInstance()->monoFont();
QFont icon = font;
QFont valueFont = font;
QFont labelFont = font;
icon.setPixelSize(qMax(8, width / 16));
font.setPixelSize(qMax(8, width / 24));
valueFont.setPixelSize(font.pixelSize() * 1.3);
labelFont.setPixelSize(font.pixelSize() * 1.1);
m_mapLabel.setFont(labelFont);
for (int i = 0; i < m_titles.count(); ++i)
{
m_icons.at(i)->setFont(icon);
m_units.at(i)->setFont(font);
m_titles.at(i)->setFont(font);
m_values.at(i)->setFont(valueFont);
}
event->accept();
}
void Map::mousePressEvent(QMouseEvent *event)
{
event->accept();
}
void Map::mouseReleaseEvent(QMouseEvent *event)
{
event->accept();
}
void Map::mouseDoubleClickEvent(QMouseEvent *event)
{
openMap();
event->accept();
}

View File

@ -23,6 +23,7 @@
#ifndef WIDGETS_MAP_H
#define WIDGETS_MAP_H
#include <QLabel>
#include <QWidget>
#include <QVBoxLayout>
@ -34,13 +35,30 @@ class Map : public QWidget
public:
Map(const int index = -1);
~Map();
private slots:
void openMap();
void updateData();
protected:
void resizeEvent(QResizeEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void mouseDoubleClickEvent(QMouseEvent *event);
private:
int m_index;
qreal m_lat;
qreal m_lon;
QLabel m_mapLabel;
QVBoxLayout m_layout;
QWidget *m_dataContainer;
QGridLayout *m_gridLayout;
QVector<QLabel *> m_icons;
QVector<QLabel *> m_units;
QVector<QLabel *> m_titles;
QVector<QLabel *> m_values;
};
}