10#include <QDesktopServices>
11#include <QInputDialog>
13#include <QWebEngineHistory>
14#include <QWebEngineSettings>
28 QUrl baseUrl(QWebEnginePage::url());
29 if (baseUrl.isEmpty() || baseUrl.scheme() ==
"about")
38 if (url.scheme() !=
"http" && url.scheme() !=
"https")
40 QDesktopServices::openUrl(url);
45 if (url.host() == baseUrl.host())
49 QString host = url.host();
50 if (host.endsWith(
"home-assistant.io") || host.endsWith(
"nabucasa.com") ||
51 host.endsWith(
"accounts.google.com"))
55 QDesktopServices::openUrl(url);
63 auto* tempPage =
new QWebEnginePage(profile(),
this);
64 connect(tempPage, &QWebEnginePage::urlChanged,
this,
65 [tempPage](
const QUrl& url)
67 if (url.isValid() && !url.isEmpty() && url.scheme() !=
"about")
69 QDesktopServices::openUrl(url);
70 tempPage->deleteLater();
81 : QWidget(parent), themeManager_(themeManager ? themeManager : &
ThemeManager::instance())
95 const auto& colors = themeManager_->colors();
98 QString(
"QPushButton { background: transparent; border: none; padding: 2px 6px; "
99 "font-size: 12px; color: %1; border-radius: 3px; }"
100 "QPushButton:hover { background: %2; }"
101 "QPushButton:focus { border: 1px solid %4; }"
102 "QPushButton:disabled { color: %3; }")
103 .arg(colors.textPrimary.name(), colors.hoverBackground.name(), colors.textMuted.name(),
104 colors.accent.name());
106 for (QPushButton* btn : {backBtn_, forwardBtn_, refreshBtn_, homeBtn_, settingsBtn_})
109 btn->setStyleSheet(btnStyle);
114 navWidget_->setStyleSheet(QString(
"background: %1; border-bottom: 1px solid %2;")
115 .arg(colors.panelBackground.name(), colors.border.name()));
123void HomeAssistantPanel::ensureWebView()
129 profile_ =
new QWebEngineProfile(
"homeassistant",
this);
132 auto* settings = profile_->settings();
133 settings->setAttribute(QWebEngineSettings::JavascriptEnabled,
true);
134 settings->setAttribute(QWebEngineSettings::LocalStorageEnabled,
true);
135 settings->setAttribute(QWebEngineSettings::WebGLEnabled,
true);
136 settings->setAttribute(QWebEngineSettings::ScrollAnimatorEnabled,
true);
137 settings->setAttribute(QWebEngineSettings::PluginsEnabled,
false);
141 webView_ =
new QWebEngineView(
this);
142 webView_->setPage(page);
143 webView_->setFocusPolicy(Qt::ClickFocus);
146 connect(webView_, &QWebEngineView::urlChanged,
this, &HomeAssistantPanel::onUrlChanged);
147 connect(webView_, &QWebEngineView::loadStarted,
this, &HomeAssistantPanel::onLoadStarted);
148 connect(webView_, &QWebEngineView::loadFinished,
this, &HomeAssistantPanel::onLoadFinished);
151 layout_->addWidget(webView_, 1);
157 if (webView_ && !haUrl_.isEmpty() && !dashboardLoaded_)
159 webView_->load(QUrl(haUrl_));
160 dashboardLoaded_ =
true;
167 dashboardLoaded_ =
false;
175void HomeAssistantPanel::onUrlChanged(
const QUrl& )
178 backBtn_->setEnabled(webView_->history()->canGoBack());
180 forwardBtn_->setEnabled(webView_->history()->canGoForward());
183void HomeAssistantPanel::onLoadStarted()
187 refreshBtn_->setText(
"\u2716");
188 refreshBtn_->setToolTip(
"Stop loading");
192void HomeAssistantPanel::onLoadFinished(
bool )
196 refreshBtn_->setText(
"\u21BB");
197 refreshBtn_->setToolTip(
"Reload page");
201void HomeAssistantPanel::onBackClicked()
207void HomeAssistantPanel::onForwardClicked()
213void HomeAssistantPanel::onRefreshClicked()
218 if (refreshBtn_ && refreshBtn_->text() ==
"\u2716")
224void HomeAssistantPanel::onHomeClicked()
226 if (webView_ && !haUrl_.isEmpty())
228 webView_->load(QUrl(haUrl_));
232void HomeAssistantPanel::onSettingsClicked()
236 QInputDialog::getText(
this,
"Home Assistant URL",
237 "Enter your Home Assistant URL:", QLineEdit::Normal, haUrl_, &ok);
238 if (ok && !newUrl.isEmpty())
240 if (!newUrl.startsWith(
"http"))
241 newUrl =
"https://" + newUrl;
244 if (!parsed.isValid() || (parsed.scheme() !=
"http" && parsed.scheme() !=
"https"))
248 webView_->load(QUrl(haUrl_));
256void HomeAssistantPanel::setupUI()
258 layout_ =
new QVBoxLayout(
this);
259 layout_->setContentsMargins(0, 0, 0, 0);
260 layout_->setSpacing(0);
263 auto* navLayout =
new QHBoxLayout();
264 navLayout->setContentsMargins(4, 1, 4, 1);
265 navLayout->setSpacing(2);
267 const auto& colors = themeManager_->colors();
269 QString(
"QPushButton { background: transparent; border: none; padding: 2px 6px; "
270 "font-size: 12px; color: %1; border-radius: 3px; }"
271 "QPushButton:hover { background: %2; }"
272 "QPushButton:focus { border: 1px solid %4; }"
273 "QPushButton:disabled { color: %3; }")
274 .arg(colors.textPrimary.name(), colors.hoverBackground.name(), colors.textMuted.name(),
275 colors.accent.name());
277 backBtn_ =
new QPushButton(
"\u25C0",
this);
278 backBtn_->setToolTip(
"Back");
279 backBtn_->setFixedSize(24, 20);
280 backBtn_->setStyleSheet(btnStyle);
281 backBtn_->setEnabled(
false);
282 connect(backBtn_, &QPushButton::clicked,
this, &HomeAssistantPanel::onBackClicked);
284 forwardBtn_ =
new QPushButton(
"\u25B6",
this);
285 forwardBtn_->setToolTip(
"Forward");
286 forwardBtn_->setFixedSize(24, 20);
287 forwardBtn_->setStyleSheet(btnStyle);
288 forwardBtn_->setEnabled(
false);
289 connect(forwardBtn_, &QPushButton::clicked,
this, &HomeAssistantPanel::onForwardClicked);
291 refreshBtn_ =
new QPushButton(
"\u21BB",
this);
292 refreshBtn_->setToolTip(
"Reload page");
293 refreshBtn_->setFixedSize(24, 20);
294 refreshBtn_->setStyleSheet(btnStyle);
295 connect(refreshBtn_, &QPushButton::clicked,
this, &HomeAssistantPanel::onRefreshClicked);
297 homeBtn_ =
new QPushButton(
"\u2302",
this);
298 homeBtn_->setToolTip(
"Home Assistant dashboard");
299 homeBtn_->setFixedSize(24, 20);
300 homeBtn_->setStyleSheet(btnStyle);
301 connect(homeBtn_, &QPushButton::clicked,
this, &HomeAssistantPanel::onHomeClicked);
303 settingsBtn_ =
new QPushButton(
"\u2699",
this);
304 settingsBtn_->setToolTip(
"Change Home Assistant URL");
305 settingsBtn_->setFixedSize(24, 20);
306 settingsBtn_->setStyleSheet(btnStyle);
307 connect(settingsBtn_, &QPushButton::clicked,
this, &HomeAssistantPanel::onSettingsClicked);
309 navLayout->addWidget(backBtn_);
310 navLayout->addWidget(forwardBtn_);
311 navLayout->addWidget(refreshBtn_);
312 navLayout->addWidget(homeBtn_);
313 navLayout->addStretch();
314 navLayout->addWidget(settingsBtn_);
316 navWidget_ =
new QWidget(
this);
317 navWidget_->setLayout(navLayout);
318 navWidget_->setFixedHeight(24);
319 navWidget_->setStyleSheet(QString(
"background: %1; border-bottom: 1px solid %2;")
320 .arg(colors.panelBackground.name(), colors.border.name()));
321 layout_->addWidget(navWidget_);
329void HomeAssistantPanel::loadSettings()
332 QString
url = settings.value(SETTINGS_KEY_URL, DEFAULT_HA_URL).toString();
335 haUrl_ = (parsed.isValid() && (parsed.scheme() ==
"http" || parsed.scheme() ==
"https"))
340void HomeAssistantPanel::saveSettings()
343 settings.setValue(SETTINGS_KEY_URL, haUrl_);
QWebEnginePage * createWindow(WebWindowType type) override
bool acceptNavigationRequest(const QUrl &url, NavigationType type, bool isMainFrame) override
HomeAssistantPanel(QWidget *parent=nullptr, ThemeManager *themeManager=nullptr)
void titleChanged(const QString &title)
~HomeAssistantPanel() override
void setUrl(const QString &url)
void themeChanged()
Emitted when theme or accent color changes.