Skip to content

Commit

Permalink
Rework pages settings
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeroErrors committed Dec 21, 2020
1 parent 8d84a7c commit 5d4fcfc
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 67 deletions.
1 change: 1 addition & 0 deletions assets/icons/home.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions assets/resources.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<file>icons/climate_ref.svg</file>
<file>icons/chevron_right.svg</file>
<file>icons/defrost.svg</file>
<file>icons/home.svg</file>
<file>icons/light/check.svg</file>
<file>icons/dark/check.svg</file>
<file>stylesheets/light.qss</file>
Expand Down
17 changes: 9 additions & 8 deletions include/app/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,15 @@ class Config : public QObject {
// emit scale_changed(this->scale);
}

inline QString get_default_page() { return this->default_page; }
inline void set_default_page(QString default_page) { this->default_page = default_page; }
inline QString get_home_page() { return this->home_page; }
inline void set_home_page(QString home_page) { this->home_page = home_page; }

inline bool get_page(QWidget *page) { return this->pages.value(page->objectName(), true); }
inline void set_page(QWidget *page, bool enabled)
inline QMap<QString, bool> get_pages() { return this->pages; }
inline bool get_page(QString page_name) { return this->pages.value(page_name, true); }
inline void set_page(QString page_name, bool enabled)
{
this->pages[page->objectName()] = enabled;
emit page_changed(page, enabled);
this->pages[page_name] = enabled;
emit page_changed(page_name, enabled);
}

inline QString get_cam_network_url() { return this->cam_network_url; }
Expand Down Expand Up @@ -222,7 +223,7 @@ class Config : public QObject {
QVideoFrame::PixelFormat cam_local_format_override;
bool cam_autoconnect;
int cam_autoconnect_time_secs;
QString default_page;
QString home_page;
QMap<QString, bool> pages;
QString vehicle_plugin;
bool vehicle_can_bus;
Expand All @@ -241,7 +242,7 @@ class Config : public QObject {
void quick_view_changed(QString quick_view);
void controls_bar_changed(bool controls_bar);
void scale_changed(double scale);
void page_changed(QWidget *page, bool enabled);
void page_changed(QString page_name, bool enabled);
void cam_autoconnect_changed(bool enabled);
void vehicle_can_bus_changed(bool state);
void vehicle_interface_changed(QString interface);
Expand Down
16 changes: 14 additions & 2 deletions include/app/pages/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <QMap>
#include <QtWidgets>
#include <QStyledItemDelegate>
#include "openauto/Configuration/Configuration.hpp"

#include "app/bluetooth.hpp"
Expand Down Expand Up @@ -41,6 +42,19 @@ class MainSettingsTab : public QWidget {
Shortcuts *shortcuts;
};

class PageItemStyledDelegate: public QStyledItemDelegate {
Q_OBJECT

public:
using QStyledItemDelegate::QStyledItemDelegate;

void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override {
QStyleOptionViewItem myOpt(option);
myOpt.decorationPosition = QStyleOptionViewItem::Right;
QStyledItemDelegate::paint(painter, myOpt, index);
}
};

class LayoutSettingsTab : public QWidget {
Q_OBJECT

Expand All @@ -49,8 +63,6 @@ class LayoutSettingsTab : public QWidget {

private:
QWidget *settings_widget();
QWidget *default_page_widget();
QWidget *default_page_select_widget();
QWidget *pages_widget();
QWidget *controls_bar_widget();
QWidget *quick_view_row_widget();
Expand Down
1 change: 1 addition & 0 deletions include/app/theme.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class Theme : public QObject {

void set_scale(double scale);

QIcon make_icon(QString name);
QIcon make_button_icon(QString name, QPushButton *button, QString alt_name = QString());
void update();

Expand Down
2 changes: 1 addition & 1 deletion include/app/window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class DashWindow : public QMainWindow {
void init_shortcuts();
QLayout *body();
void add_pages();
void add_page(QString name, QWidget *page, QString icon);
void add_page(QString name, QWidget *page, QString icon, bool can_disable = true);

QWidget *controls_bar();
QLayout *quick_views();
Expand Down
6 changes: 3 additions & 3 deletions src/app/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Config::Config()
this->vehicle_plugin = this->settings.value("Vehicle/plugin", QString()).toString();
this->vehicle_can_bus = this->settings.value("Vehicle/can_bus", false).toBool();
this->vehicle_interface = this->settings.value("Vehicle/interface", QString()).toString();
this->default_page = this->settings.value("default_page", "Android Auto").toString();
this->home_page = this->settings.value("home_page", "Android Auto").toString();
this->settings.beginGroup("Pages");
for (auto key : this->settings.childKeys())
this->pages[key] = this->settings.value(key, true).toBool();
Expand Down Expand Up @@ -129,8 +129,8 @@ void Config::save()
this->settings.setValue("Vehicle/can_bus", this->vehicle_can_bus);
if (this->vehicle_interface != this->settings.value("Vehicle/interface").toString())
this->settings.setValue("Vehicle/interface", this->vehicle_interface);
if (this->default_page != this->settings.value("default_page").toString())
this->settings.setValue("default_page", this->default_page);
if (this->home_page != this->settings.value("home_page").toString())
this->settings.setValue("home_page", this->home_page);
for (auto id : this->pages.keys()) {
QString config_key = QString("Pages/%1").arg(id);
bool page_enabled = this->pages[id];
Expand Down
202 changes: 159 additions & 43 deletions src/app/pages/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <BluezQt/PendingCall>
#include <QLabel>
#include <QScrollArea>
#include <QSizePolicy>
#include <QSpacerItem>

#include <aasdk_proto/ButtonCodeEnum.pb.h>
#include <aasdk_proto/VideoFPSEnum.pb.h>
Expand Down Expand Up @@ -278,8 +280,6 @@ QWidget *LayoutSettingsTab::settings_widget()
QWidget *widget = new QWidget(this);
QVBoxLayout *layout = new QVBoxLayout(widget);

layout->addWidget(this->default_page_widget());
layout->addWidget(Theme::br(widget), 1);
layout->addWidget(this->pages_widget());
layout->addWidget(Theme::br(widget), 1);
layout->addWidget(this->controls_bar_widget(), 1);
Expand All @@ -300,64 +300,180 @@ QWidget *LayoutSettingsTab::settings_widget()
return scroll_area;
}

QWidget *LayoutSettingsTab::default_page_widget()
QWidget *LayoutSettingsTab::pages_widget()
{
QWidget *widget = new QWidget(this);
QHBoxLayout *layout = new QHBoxLayout(widget);
QVBoxLayout *layout = new QVBoxLayout(widget);

QLabel *label = new QLabel("Default Page", widget);
label->setFont(Theme::font_14);
layout->addWidget(label, 1);
QLabel *pages_label = new QLabel("Pages", widget);
pages_label->setFont(Theme::font_14);
layout->addWidget(pages_label, 1);

layout->addWidget(this->default_page_select_widget(), 1);
QWidget *horizontal_widget = new QWidget(widget);
QHBoxLayout *horizontal_layout = new QHBoxLayout(horizontal_widget);
layout->addWidget(horizontal_widget, 1);

return widget;
}
// Left: Disabled pages list view
QListWidget *disabled_pages;
{
QWidget *container = new QWidget(horizontal_widget);
QVBoxLayout *container_layout = new QVBoxLayout(container);
horizontal_layout->addWidget(container, 1);

QWidget *LayoutSettingsTab::default_page_select_widget()
{
QWidget *widget = new QWidget(this);
QHBoxLayout *layout = new QHBoxLayout(widget);
QLabel *label = new QLabel("Disabled", container);
label->setFont(Theme::font_14);
container_layout->addWidget(label, 1);

DashWindow *window = qobject_cast<DashWindow *>(this->window());
disabled_pages = new QListWidget(container);
disabled_pages->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
container_layout->addWidget(disabled_pages, 1);
}

QList<QString> pages;
for (QAbstractButton *page : window->get_pages()) {
pages.append(page->property("page").value<QWidget *>()->objectName());
// Middle: control buttons
QPushButton *enable_button;
QPushButton *set_home_button;
QPushButton *disable_button;
{
QWidget *container = new QWidget(horizontal_widget);
QVBoxLayout *container_layout = new QVBoxLayout(container);
horizontal_layout->addWidget(container);

// Add a spacer at the top with the same hiehgt as the labels so the buttons align with the list views
QFontMetrics font_metrics(Theme::font_14);
container_layout->addSpacerItem(new QSpacerItem(0, font_metrics.height()));

enable_button = new QPushButton(">", container);
enable_button->setEnabled(false);
container_layout->addWidget(enable_button);

set_home_button = new QPushButton("", container);
set_home_button->setEnabled(false);
set_home_button->setIcon(this->theme->make_button_icon("home", set_home_button));
container_layout->addWidget(set_home_button, 1);

disable_button = new QPushButton("<", container);
disable_button->setEnabled(false);
container_layout->addWidget(disable_button);
}
Selector *selector = new Selector(pages, this->config->get_default_page(), Theme::font_14, widget);
connect(selector, &Selector::item_changed, [config = this->config](QString item) { config->set_default_page(item); });

layout->addStretch(1);
layout->addWidget(selector, 10);
layout->addStretch(1);
// Right: Enabled pages list view
QListWidget *enabled_pages;
{
QWidget *container = new QWidget(horizontal_widget);
QVBoxLayout *container_layout = new QVBoxLayout(container);
horizontal_layout->addWidget(container, 1);

QLabel *label = new QLabel("Enabled", container);
label->setFont(Theme::font_14);
container_layout->addWidget(label, 1);

enabled_pages = new QListWidget(container);
enabled_pages->setItemDelegate(new PageItemStyledDelegate());
enabled_pages->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
container_layout->addWidget(enabled_pages, 1);
}

return widget;
}
// Connect signals

// Only allow one selected between both disabled and enabled list views.
// Enable and disable buttons depnding on what list the item is in.
connect(disabled_pages, &QListWidget::itemSelectionChanged,
[enabled_pages, disabled_pages, enable_button, set_home_button, disable_button]() {
QList<QListWidgetItem *> selected = disabled_pages->selectedItems();
if (!selected.isEmpty()) {
enabled_pages->clearSelection();
enable_button->setEnabled(true);
set_home_button->setEnabled(false);
disable_button->setEnabled(false);
}
});
connect(enabled_pages, &QListWidget::itemSelectionChanged,
[enabled_pages, disabled_pages, enable_button, set_home_button, disable_button, this]() {
QList<QListWidgetItem *> selected = enabled_pages->selectedItems();
if (!selected.isEmpty()) {
disabled_pages->clearSelection();
enable_button->setEnabled(false);
disable_button->setEnabled(true);
}

// Only have the set home button enabled when only one item in the enabled pages is selected
// and that selected item isn't already the setup page.
set_home_button->setEnabled(selected.size() == 1 && selected[0]->text() != this->config->get_home_page());
});

QWidget *LayoutSettingsTab::pages_widget()
{
QWidget *widget = new QWidget(this);
QHBoxLayout *layout = new QHBoxLayout(widget);
// Enable and disable pages
connect(enable_button, &QPushButton::pressed, [disabled_pages, config = this->config]() {
QListWidgetItem *current = disabled_pages->currentItem();
if (current != nullptr) {
config->set_page(current->text(), true);
}
});
connect(disable_button, &QPushButton::pressed, [enabled_pages, this]() {
QListWidgetItem *current = enabled_pages->currentItem();
if (current != nullptr) {
QString page_name = current->text();

// If the page was disabled and that page is the current home page,
// then replace the home page with the first enabled.
if (this->config->get_home_page() == page_name) {
DashWindow *window = qobject_cast<DashWindow *>(this->window());

for (QAbstractButton *page : window->get_pages()) {
if (!page->isHidden() && page->property("can_disable").value<bool>()) {
QString page_name = page->property("page").value<QWidget *>()->objectName();
config->set_home_page(page_name);
break;
}
}
}

QLabel *label = new QLabel("Pages", widget);
layout->addWidget(label, 1);
this->config->set_page(page_name, false);
}
});

QGroupBox *group = new QGroupBox(widget);
QVBoxLayout *group_layout = new QVBoxLayout(group);
QIcon startup_icon = this->theme->make_icon("home");

DashWindow *window = qobject_cast<DashWindow *>(this->window());
// Update the items in the disabled and enabled list views
std::function<void()> update_lists = [disabled_pages, enabled_pages, enable_button, set_home_button, disable_button, startup_icon, this]() {
disabled_pages->clear();
enabled_pages->clear();

for (QAbstractButton *page : window->get_pages()) {
QCheckBox *button = new QCheckBox(page->property("page").value<QWidget *>()->objectName(), group);
button->setChecked(!page->isHidden());
connect(button, &QCheckBox::toggled, [page, config = this->config](bool checked) {
config->set_page(page->property("page").value<QWidget *>(), checked);
});
group_layout->addWidget(button);
}
DashWindow *window = qobject_cast<DashWindow *>(this->window());

for (QAbstractButton *page : window->get_pages()) {
if (page->property("can_disable").value<bool>()) {
bool enabled = !page->isHidden();
QString page_name = page->property("page").value<QWidget *>()->objectName();

QListWidgetItem *item = new QListWidgetItem(page_name, enabled ? enabled_pages : disabled_pages);

if (enabled && this->config->get_home_page() == page_name) {
item->setIcon(startup_icon);
}
}
}

enable_button->setEnabled(false);
set_home_button->setEnabled(false);
disable_button->setEnabled(false);
};

connect(set_home_button, &QPushButton::pressed, [enabled_pages, disabled_pages, this, update_lists]() {
QListWidgetItem *disabled_current = disabled_pages->currentItem();
if (disabled_current != nullptr) {
this->config->set_home_page(disabled_current->text());
update_lists();
}

QListWidgetItem *enabled_current = enabled_pages->currentItem();
if (enabled_current != nullptr) {
this->config->set_home_page(enabled_current->text());
update_lists();
}
});

layout->addWidget(group, 1, Qt::AlignHCenter);
connect(this->config, &Config::page_changed, update_lists);
update_lists();

return widget;
}
Expand Down
5 changes: 5 additions & 0 deletions src/app/theme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ QIcon Theme::themed_button_icon(QIcon icon, QAbstractButton *button)
return themed_icon;
}

QIcon Theme::make_icon(QString name)
{
return QIcon(QString(":/icons/%1.svg").arg(name));
}

QIcon Theme::make_button_icon(QString name, QPushButton *button, QString alt_name)
{
if (!alt_name.isNull())
Expand Down
Loading

0 comments on commit 5d4fcfc

Please sign in to comment.