Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to specify the default page #38

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/icons/visibility.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions assets/resources.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
<file>icons/climate_ref.svg</file>
<file>icons/chevron_right.svg</file>
<file>icons/defrost.svg</file>
<file>icons/home.svg</file>
<file>icons/visibility.svg</file>
<file>icons/light/check.svg</file>
<file>icons/dark/check.svg</file>
<file>stylesheets/light.qss</file>
Expand Down
5 changes: 5 additions & 0 deletions include/app/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ class Config : public QObject {
// emit scale_changed(this->scale);
}

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

inline QMap<QString, bool> get_pages() { return this->pages; }
inline bool get_page(QWidget *page) { return this->pages.value(page->objectName(), true); }
inline void set_page(QWidget *page, bool enabled)
{
Expand Down Expand Up @@ -216,6 +220,7 @@ class Config : public QObject {
QVideoFrame::PixelFormat cam_local_format_override;
bool cam_autoconnect;
int cam_autoconnect_time_secs;
QString home_page;
QMap<QString, bool> pages;
QString vehicle_plugin;
bool vehicle_can_bus;
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(), bool dynamic = false);
void update();

Expand Down
3 changes: 3 additions & 0 deletions src/app/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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->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 @@ -123,6 +124,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->home_page != this->settings.value("home_page").toString())
this->settings.setValue("home_page", this->home_page);
for (auto id : this->pages.keys()) {
QString key = QString("Pages/%1").arg(id);
bool page_enabled = this->pages[id];
Expand Down
60 changes: 56 additions & 4 deletions src/app/pages/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,17 +350,69 @@ QWidget *LayoutSettingsTab::pages_widget()
QGroupBox *group = new QGroupBox(widget);
QVBoxLayout *group_layout = new QVBoxLayout(group);

QWidget *icon_row = new QWidget(group);
QHBoxLayout *icon_row_layout = new QHBoxLayout(icon_row);
group_layout->addWidget(icon_row);

QLabel *home_icon = new QLabel(icon_row);
home_icon->setPixmap(this->theme->make_icon("home").pixmap(home_icon->size()));
icon_row_layout->addWidget(home_icon);

QLabel *visibility_icon = new QLabel(icon_row);
visibility_icon->setPixmap(this->theme->make_icon("visibility").pixmap(visibility_icon->size()));
icon_row_layout->addWidget(visibility_icon, 1);

DashWindow *window = qobject_cast<DashWindow *>(this->window());

QButtonGroup *button_group = new QButtonGroup();
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) {
QString page_name = page->property("page").value<QWidget *>()->objectName();

QWidget *row = new QWidget(this);
QHBoxLayout *row_layout = new QHBoxLayout(row);

QRadioButton *is_home = new QRadioButton(row);
is_home->setEnabled(!page->isHidden());
is_home->setChecked(this->config->get_home_page() == page_name);
connect(is_home, &QCheckBox::toggled, [page_name, config = this->config](bool checked) {
if (checked) {
config->set_home_page(page_name);
}
});
row_layout->addWidget(is_home);
button_group->addButton(is_home);

QCheckBox *is_enabled = new QCheckBox(page_name, row);
is_enabled->setChecked(!page->isHidden());
connect(is_enabled, &QCheckBox::toggled, [page, is_home, button_group, config = this->config](bool checked) {
config->set_page(page->property("page").value<QWidget *>(), checked);
is_home->setEnabled(checked);

// If the page becomes disabled and is set as the home then replace the home with the fist visible page
if (!checked && is_home->isChecked()) {
for (QAbstractButton *button : button_group->buttons()) {
if (button->isEnabled()) {
button->setChecked(true);
break;
}
}
}
});
group_layout->addWidget(button);
row_layout->addWidget(is_enabled, 1);
group_layout->addWidget(row);
}

// Note: To allow for the radio buttons to all become unchecked if all pages become disabled we need to have an
// additional hidden radio button. We use this hidden radio button for making the settings page the home.
QRadioButton *is_settings_home = new QRadioButton();
is_settings_home->setChecked(this->config->get_home_page() == "Settings");
connect(is_settings_home, &QCheckBox::toggled, [config = this->config](bool checked) {
if (checked) {
config->set_home_page("Settings");
}
});
button_group->addButton(is_settings_home);

layout->addWidget(group, 1, Qt::AlignHCenter);

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, bool dynamic)
{
if (!alt_name.isNull())
Expand Down
5 changes: 3 additions & 2 deletions src/app/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,10 @@ void DashWindow::add_pages()
this->add_page("Launcher", new LauncherPage(this), "widgets");
this->add_page("Settings", new SettingsPage(this), "tune");

// toggle initial page
// Toggle home page
QString home_page = this->config->get_home_page();
for (QAbstractButton *button : this->rail_group->buttons()) {
if (!button->isHidden()) {
if (!button->isHidden() && home_page == button->property("page").value<QWidget *>()->objectName()) {
button->setChecked(true);
break;
}
Expand Down