From 34a2a9bf174210fb7d762e12e2a8026b80ec8434 Mon Sep 17 00:00:00 2001 From: grauser Date: Sat, 17 Dec 2016 11:33:22 +0100 Subject: [PATCH] Train view : add web page (not enabled) --- src/Gui/GcWindowRegistry.cpp | 4 + src/Gui/GcWindowRegistry.h | 3 +- src/Train/WebPageWindow.cpp | 228 +++++++++++++++++++++++++++++++++++ src/Train/WebPageWindow.h | 132 ++++++++++++++++++++ src/src.pro | 8 +- 5 files changed, 370 insertions(+), 5 deletions(-) create mode 100644 src/Train/WebPageWindow.cpp create mode 100644 src/Train/WebPageWindow.h diff --git a/src/Gui/GcWindowRegistry.cpp b/src/Gui/GcWindowRegistry.cpp index 1dae04276..1aecfee2d 100644 --- a/src/Gui/GcWindowRegistry.cpp +++ b/src/Gui/GcWindowRegistry.cpp @@ -57,6 +57,7 @@ #include "GoogleMapControl.h" #include "BingMap.h" #include "RideMapWindow.h" +#include "WebPageWindow.h" #ifdef GC_WANT_R #include "RChart.h" #endif @@ -112,6 +113,7 @@ GcWindowRegistry::initialize() { VIEW_TRAIN, tr("StreetView"), GcWindowTypes::StreetViewWindow }, { VIEW_TRAIN, tr("Video Player"),GcWindowTypes::VideoPlayer }, { VIEW_TRAIN, tr("Workout Editor"),GcWindowTypes::WorkoutWindow }, + //{ VIEW_TRAIN, tr("Web page"),GcWindowTypes::WebPageWindow }, { 0, "", GcWindowTypes::None }}; // initialize the global registry GcWindows = GcWindowsInit; @@ -234,6 +236,8 @@ GcWindowRegistry::newGcWindow(GcWinID id, Context *context) case GcWindowTypes::ActivityNavigator: returning = new RideNavigator(context); break; case GcWindowTypes::WorkoutWindow: returning = new WorkoutWindow(context); break; + + case GcWindowTypes::WebPageWindow: returning = new WebPageWindow(context); break; #if 0 // not till v4.0 case GcWindowTypes::RouteSegment: returning = new RouteWindow(context); break; #else diff --git a/src/Gui/GcWindowRegistry.h b/src/Gui/GcWindowRegistry.h index 863874d66..899360747 100644 --- a/src/Gui/GcWindowRegistry.h +++ b/src/Gui/GcWindowRegistry.h @@ -66,7 +66,8 @@ enum gcwinid { RideMapWindow = 37, RConsole = 38, RConsoleSeason = 39, - SeasonPlan = 40 + SeasonPlan = 40, + WebPageWindow = 41 }; }; typedef enum GcWindowTypes::gcwinid GcWinID; diff --git a/src/Train/WebPageWindow.cpp b/src/Train/WebPageWindow.cpp new file mode 100644 index 000000000..328496c44 --- /dev/null +++ b/src/Train/WebPageWindow.cpp @@ -0,0 +1,228 @@ +/* + * Copyright (c) 2016 Damien Grauser (Damien.Grauser@gmail.com) + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "WebPageWindow.h" + +#include "MainWindow.h" +#include "RideItem.h" +#include "RideFile.h" +#include "IntervalItem.h" +#include "IntervalTreeView.h" +#include "SmallPlot.h" +#include "Context.h" +#include "Athlete.h" +#include "Zones.h" +#include "Settings.h" +#include "Colors.h" +#include "Units.h" +#include "TimeUtils.h" +#include "HelpWhatsThis.h" + +#ifdef NOWEBKIT +#include +#endif + +// overlay helper +#include "TabView.h" +#include "GcOverlayWidget.h" +#include "IntervalSummaryWindow.h" +#include + +WebPageWindow::WebPageWindow(Context *context) : GcChartWindow(context), context(context), firstShow(true) +{ + + // + // Chart settings + // + + QWidget *settingsWidget = new QWidget(this); + settingsWidget->setContentsMargins(0,0,0,0); + //HelpWhatsThis *helpSettings = new HelpWhatsThis(settingsWidget); + //settingsWidget->setWhatsThis(helpSettings->getWhatsThisText(HelpWhatsThis::ChartRides_Critical_MM_Config_Settings)); + + + QFormLayout *commonLayout = new QFormLayout(settingsWidget); + + + + customUrlLabel = new QLabel(tr("URL")); + customUrl = new QLineEdit(""); + customUrl->setFixedWidth(250); + customUrl->setText(QString("http://www.youtube.com")); + + commonLayout->addRow(customUrlLabel, customUrl); + + connect(customUrl, SIGNAL(editingFinished()), this, SLOT(customURLEditingFinished())); + connect(customUrl, SIGNAL(textChanged(QString)), this, SLOT(customURLTextChanged(QString))); + + setControls(settingsWidget); + + setContentsMargins(0,0,0,0); + layout = new QVBoxLayout(); + layout->setSpacing(0); + layout->setContentsMargins(2,0,2,2); + setChartLayout(layout); + +#ifdef NOWEBKIT + view = new QWebEngineView(this); +#else + view = new QWebView(); +#endif + view->setPage(new simpleWebPage()); + view->setContentsMargins(0,0,0,0); + view->page()->view()->setContentsMargins(0,0,0,0); + view->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + view->setAcceptDrops(false); + layout->addWidget(view); + + HelpWhatsThis *help = new HelpWhatsThis(view); + view->setWhatsThis(help->getWhatsThisText(HelpWhatsThis::ChartRides_Map)); + + first = true; + configChanged(CONFIG_APPEARANCE); + +#ifdef NOWEBKIT + view->page()->setHtml(currentPage); +#else + + QString urlstr = ""; + + urlstr = QString("https://www.strava.com/oauth/authorize?"); + urlstr.append("client_id=").append(GC_STRAVA_CLIENT_ID).append("&"); + urlstr.append("scope=view_private,write&"); + urlstr.append("redirect_uri=http://www.goldencheetah.org/&"); + urlstr.append("response_type=code&"); + urlstr.append("approval_prompt=force"); + + QUrl url = QUrl(urlstr); + view->setUrl(url); + + // connects + connect(view, SIGNAL(urlChanged(const QUrl&)), this, + SLOT(urlChanged(const QUrl&))); + connect(view, SIGNAL(loadFinished(bool)), this, + SLOT(loadFinished(bool))); + + QNetworkAccessManager*nam=view->page()->networkAccessManager(); + + connect(nam,SIGNAL(finished(QNetworkReply*)),this, + SLOT(finished(QNetworkReply*))); + +#endif +} + +WebPageWindow::~WebPageWindow() +{ +} + +void +WebPageWindow::customURLTextChanged(QString text) +{ + if (currentUrl != text) { + if (first) { + forceReplot(); + } + } +} + +void +WebPageWindow::customURLEditingFinished() +{ + if (currentUrl != customUrl->text()) { + currentUrl = customUrl->text(); + forceReplot(); + } +} + +void +WebPageWindow::configChanged(qint32) +{ + setProperty("color", GColor(CPLOTBACKGROUND)); +#ifndef Q_OS_MAC + overlayIntervals->setStyleSheet(TabView::ourStyleSheet()); +#endif +} + +void +WebPageWindow::forceReplot() +{ +#ifdef NOWEBKIT + view->page()->setHtml(currentPage); +#else + view->page()->mainFrame()->load(QUrl(currentUrl)); +#endif + + +} + +void WebPageWindow::updateFrame() +{ +} + +void +WebPageWindow::urlChanged(const QUrl &url) +{ + qDebug() << "urlChanged" << url; +} + +void +WebPageWindow::loadFinished(bool ok) { + qDebug() << "loadFinished" << ok; +} + +void +WebPageWindow::finished(QNetworkReply * reply) +{ + if(reply->error() !=QNetworkReply::NoError) { + QMessageBox::warning(0, QString("NetworkAccessManager NetworkReply"), + QString("QNetworkReply error string: \n") + reply->errorString(), + QMessageBox::Ok); + } +} + +void +WebPageWindow::onError(QNetworkReply::NetworkError err) +{ + if(err !=QNetworkReply::NoError) { + QMessageBox::warning(0, QString("Error!"), + QString("Error loading webpage: \n") + QString::number(err), + QMessageBox::Ok); + } +} + +bool +WebPageWindow::event(QEvent *event) +{ + // nasty nasty nasty hack to move widgets as soon as the widget geometry + // is set properly by the layout system, by default the width is 100 and + // we wait for it to be set properly then put our helper widget on the RHS + if (event->type() == QEvent::Resize && geometry().width() != 100) { + + // put somewhere nice on first show + if (firstShow) { + firstShow = false; + //helperWidget()->move(mainWidget()->geometry().width()-275, 50); + } + + // if off the screen move on screen + /*if (helperWidget()->geometry().x() > geometry().width()) { + helperWidget()->move(mainWidget()->geometry().width()-275, 50); + }*/ + } + return QWidget::event(event); +} diff --git a/src/Train/WebPageWindow.h b/src/Train/WebPageWindow.h new file mode 100644 index 000000000..0b3922d5b --- /dev/null +++ b/src/Train/WebPageWindow.h @@ -0,0 +1,132 @@ +/* + * Copyright (c) 2009 Greg Lonnon (greg.lonnon@gmail.com) + * 2011 Mark Liversedge (liversedge@gmail.com) + * 2016 Damien Grauser (Damien.Grauser@gmail.com) + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef _GC_WebPageWindow_h +#define _GC_WebPageWindow_h +#include "GoldenCheetah.h" + +#include +#include + +#include +#include +#include +#include +#include "RideFile.h" +#include "IntervalItem.h" +#include "Context.h" + +#include +#include +#ifdef NOWEBKIT +#include +#include +#else +#include +#include +#include +#include +#endif + +class QMouseEvent; +class RideItem; +class Context; +class QColor; +class QVBoxLayout; +class QTabWidget; +class WebPageWindow; +class IntervalSummaryWindow; +class SmallPlot; + +// trick the maps api into ignoring gestures by +// pretending to be chrome. see: http://developer.qt.nokia.com/forums/viewthread/1643/P15 +#ifdef NOWEBKIT +class mapWebPage : public QWebEnginePage +{ +}; +#else +class simpleWebPage : public QWebPage +{ +#if 0 + virtual QString userAgentForUrl(const QUrl&) const { + return "Mozilla/5.0"; + } +#endif +}; +#endif + +class WebPageWindow : public GcChartWindow +{ + Q_OBJECT + G_OBJECT + + // properties can be saved/restored/set by the layout manager + Q_PROPERTY(QString url READ url WRITE setUrl USER true) + + + public: + WebPageWindow(Context *); + ~WebPageWindow(); + bool first; + + QLabel *customUrlLabel; + QLineEdit *customUrl; + + // set/get properties + QString url() const { return customUrl->text(); } + void setUrl(QString x) { customUrl->setText(x) ;} + + + public slots: + void customURLEditingFinished(); + void customURLTextChanged(QString text); + + + void forceReplot(); + void configChanged(qint32); + + private: + Context *context; + QVBoxLayout *layout; + +#ifdef NOWEBKIT + QWebEngineView *view; +#else + QWebView *view; +#endif + + WebPageWindow(); // default ctor + QString currentPage; + bool firstShow; + + QString defaultUrl; + QString currentUrl; + private slots: + void updateFrame(); + void urlChanged(const QUrl& url); + void loadFinished(bool ok); + void finished(QNetworkReply*reply); + void onError(QNetworkReply::NetworkError err); + + protected: + bool event(QEvent *event); +}; + +#endif diff --git a/src/src.pro b/src/src.pro index 4e1ac9036..a705fe3f3 100644 --- a/src/src.pro +++ b/src/src.pro @@ -708,8 +708,8 @@ HEADERS += Train/AddDeviceWizard.h Train/ComputrainerController.h Train/Computra Train/Library.h Train/LibraryParser.h Train/MeterWidget.h Train/NullController.h Train/RealtimeController.h \ Train/RealtimeData.h Train/RealtimePlot.h Train/RealtimePlotWindow.h Train/RemoteControl.h Train/SpinScanPlot.h \ Train/SpinScanPlotWindow.h Train/SpinScanPolarPlot.h Train/TrainBottom.h Train/TrainDB.h Train/TrainSidebar.h \ - Train/VideoLayoutParser.h Train/VideoSyncFile.h Train/WorkoutPlotWindow.h Train/WorkoutWidget.h Train/WorkoutWidgetItems.h \ - Train/WorkoutWindow.h Train/WorkoutWizard.h Train/ZwoParser.h + Train/VideoLayoutParser.h Train/VideoSyncFile.h Train/WorkoutPlotWindow.h Train/WebPageWindow.h \ + Train/WorkoutWidget.h Train/WorkoutWidgetItems.h Train/WorkoutWindow.h Train/WorkoutWizard.h Train/ZwoParser.h ###============= @@ -796,8 +796,8 @@ SOURCES += Train/AddDeviceWizard.cpp Train/ComputrainerController.cpp Train/Comp Train/Library.cpp Train/LibraryParser.cpp Train/MeterWidget.cpp Train/NullController.cpp Train/RealtimeController.cpp \ Train/RealtimeData.cpp Train/RealtimePlot.cpp Train/RealtimePlotWindow.cpp Train/RemoteControl.cpp Train/SpinScanPlot.cpp \ Train/SpinScanPlotWindow.cpp Train/SpinScanPolarPlot.cpp Train/TrainBottom.cpp Train/TrainDB.cpp Train/TrainSidebar.cpp \ - Train/VideoLayoutParser.cpp Train/VideoSyncFile.cpp Train/WorkoutPlotWindow.cpp Train/WorkoutWidget.cpp Train/WorkoutWidgetItems.cpp \ - Train/WorkoutWindow.cpp Train/WorkoutWizard.cpp Train/ZwoParser.cpp + Train/VideoLayoutParser.cpp Train/VideoSyncFile.cpp Train/WorkoutPlotWindow.cpp Train/WebPageWindow.cpp \ + Train/WorkoutWidget.cpp Train/WorkoutWidgetItems.cpp Train/WorkoutWindow.cpp Train/WorkoutWizard.cpp Train/ZwoParser.cpp ###======================================