From 1d6683f3e07d9a73af5fab702bc3a551ec7dabc9 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Thu, 3 Sep 2015 15:56:37 -0300 Subject: Move Profile widget out of desktop-widgets The reason for that is, even if profile widget is made with qpainter and for that reason it should be a desktop widget, it's being used on the mobile version because of a lack of QML plotting library that is fast and reliable. We discovered that it was faster just to encapsulate our Profile in a QML class and call it directly. Signed-off-by: Tomaz Canabrava Signed-off-by: Dirk Hohndel --- profile-widget/ruleritem.cpp | 179 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 profile-widget/ruleritem.cpp (limited to 'profile-widget/ruleritem.cpp') diff --git a/profile-widget/ruleritem.cpp b/profile-widget/ruleritem.cpp new file mode 100644 index 000000000..830985552 --- /dev/null +++ b/profile-widget/ruleritem.cpp @@ -0,0 +1,179 @@ +#include "ruleritem.h" +#include "preferences.h" +#include "mainwindow.h" +#include "profilewidget2.h" +#include "display.h" + +#include + +#include "profile.h" + +RulerNodeItem2::RulerNodeItem2() : + entry(NULL), + ruler(NULL), + timeAxis(NULL), + depthAxis(NULL) +{ + memset(&pInfo, 0, sizeof(pInfo)); + setRect(-8, -8, 16, 16); + setBrush(QColor(0xff, 0, 0, 127)); + setPen(QColor(Qt::red)); + setFlag(ItemIsMovable); + setFlag(ItemSendsGeometryChanges); + setFlag(ItemIgnoresTransformations); +} + +void RulerNodeItem2::setPlotInfo(plot_info &info) +{ + pInfo = info; + entry = pInfo.entry; +} + +void RulerNodeItem2::setRuler(RulerItem2 *r) +{ + ruler = r; +} + +void RulerNodeItem2::recalculate() +{ + struct plot_data *data = pInfo.entry + (pInfo.nr - 1); + uint16_t count = 0; + if (x() < 0) { + setPos(0, y()); + } else if (x() > timeAxis->posAtValue(data->sec)) { + setPos(timeAxis->posAtValue(data->sec), depthAxis->posAtValue(data->depth)); + } else { + data = pInfo.entry; + count = 0; + while (timeAxis->posAtValue(data->sec) < x() && count < pInfo.nr) { + data = pInfo.entry + count; + count++; + } + setPos(timeAxis->posAtValue(data->sec), depthAxis->posAtValue(data->depth)); + entry = data; + } +} + +void RulerNodeItem2::mouseMoveEvent(QGraphicsSceneMouseEvent *event) +{ + qreal x = event->scenePos().x(); + if (x < 0.0) + x = 0.0; + setPos(x, event->scenePos().y()); + recalculate(); + ruler->recalculate(); +} + +RulerItem2::RulerItem2() : source(new RulerNodeItem2()), + dest(new RulerNodeItem2()), + timeAxis(NULL), + depthAxis(NULL), + textItemBack(new QGraphicsRectItem(this)), + textItem(new QGraphicsSimpleTextItem(this)) +{ + memset(&pInfo, 0, sizeof(pInfo)); + source->setRuler(this); + dest->setRuler(this); + textItem->setFlag(QGraphicsItem::ItemIgnoresTransformations); + textItemBack->setBrush(QColor(0xff, 0xff, 0xff, 190)); + textItemBack->setPen(QColor(Qt::white)); + textItemBack->setFlag(QGraphicsItem::ItemIgnoresTransformations); + setPen(QPen(QColor(Qt::black), 0.0)); + connect(PreferencesDialog::instance(), SIGNAL(settingsChanged()), this, SLOT(settingsChanged())); +} + +void RulerItem2::settingsChanged() +{ + ProfileWidget2 *profWidget = NULL; + if (scene() && scene()->views().count()) + profWidget = qobject_cast(scene()->views().first()); + + if (profWidget && profWidget->currentState == ProfileWidget2::PROFILE) + setVisible(prefs.rulergraph); + else + setVisible(false); +} + +void RulerItem2::recalculate() +{ + char buffer[500]; + QPointF tmp; + QFont font; + QFontMetrics fm(font); + + if (timeAxis == NULL || depthAxis == NULL || pInfo.nr == 0) + return; + + prepareGeometryChange(); + startPoint = mapFromItem(source, 0, 0); + endPoint = mapFromItem(dest, 0, 0); + + if (startPoint.x() > endPoint.x()) { + tmp = endPoint; + endPoint = startPoint; + startPoint = tmp; + } + QLineF line(startPoint, endPoint); + setLine(line); + compare_samples(source->entry, dest->entry, buffer, 500, 1); + text = QString(buffer); + + // draw text + QGraphicsView *view = scene()->views().first(); + QPoint begin = view->mapFromScene(mapToScene(startPoint)); + textItem->setText(text); + qreal tgtX = startPoint.x(); + const qreal diff = begin.x() + textItem->boundingRect().width(); + // clamp so that the text doesn't go out of the screen to the right + if (diff > view->width()) { + begin.setX(begin.x() - (diff - view->width())); + tgtX = mapFromScene(view->mapToScene(begin)).x(); + } + // always show the text bellow the lowest of the start and end points + qreal tgtY = (startPoint.y() >= endPoint.y()) ? startPoint.y() : endPoint.y(); + // this isn't exactly optimal, since we want to scale the 1.0, 4.0 distances as well + textItem->setPos(tgtX - 1.0, tgtY + 4.0); + + // setup the text background + textItemBack->setVisible(startPoint.x() != endPoint.x()); + textItemBack->setPos(textItem->x(), textItem->y()); + textItemBack->setRect(0, 0, textItem->boundingRect().width(), textItem->boundingRect().height()); +} + +RulerNodeItem2 *RulerItem2::sourceNode() const +{ + return source; +} + +RulerNodeItem2 *RulerItem2::destNode() const +{ + return dest; +} + +void RulerItem2::setPlotInfo(plot_info info) +{ + pInfo = info; + dest->setPlotInfo(info); + source->setPlotInfo(info); + dest->recalculate(); + source->recalculate(); + recalculate(); +} + +void RulerItem2::setAxis(DiveCartesianAxis *time, DiveCartesianAxis *depth) +{ + timeAxis = time; + depthAxis = depth; + dest->depthAxis = depth; + dest->timeAxis = time; + source->depthAxis = depth; + source->timeAxis = time; + recalculate(); +} + +void RulerItem2::setVisible(bool visible) +{ + QGraphicsLineItem::setVisible(visible); + source->setVisible(visible); + dest->setVisible(visible); +} -- cgit v1.2.3-70-g09d2 From ff57881265a305fe06691b065bc3f0efae88e6b3 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Sat, 31 Oct 2015 21:02:16 -0200 Subject: Preferences: Remove the old dialog and use the new one The new preferences dialog still needs a bit of fine tuning but should already work. Signed-off-by: Tomaz Canabrava Signed-off-by: Dirk Hohndel --- desktop-widgets/CMakeLists.txt | 1 - desktop-widgets/mainwindow.cpp | 4 +- desktop-widgets/preferences.h | 43 ---------------------- .../preferences/preferences_network.cpp | 1 - desktop-widgets/preferences/preferencesdialog.cpp | 29 ++++++++++----- desktop-widgets/preferences/preferencesdialog.h | 8 ++-- profile-widget/divecartesianaxis.cpp | 2 +- profile-widget/divepixmapitem.cpp | 2 +- profile-widget/diveprofileitem.cpp | 2 +- profile-widget/profilewidget2.cpp | 2 +- profile-widget/ruleritem.cpp | 2 +- 11 files changed, 31 insertions(+), 65 deletions(-) delete mode 100644 desktop-widgets/preferences.h (limited to 'profile-widget/ruleritem.cpp') diff --git a/desktop-widgets/CMakeLists.txt b/desktop-widgets/CMakeLists.txt index 62be916cc..0a0059ee3 100644 --- a/desktop-widgets/CMakeLists.txt +++ b/desktop-widgets/CMakeLists.txt @@ -30,7 +30,6 @@ set(SUBSURFACE_INTERFACE mainwindow.cpp modeldelegates.cpp notificationwidget.cpp - preferences.cpp simplewidgets.cpp starwidget.cpp subsurfacewebservices.cpp diff --git a/desktop-widgets/mainwindow.cpp b/desktop-widgets/mainwindow.cpp index 23b4fb9b0..dc45d1d8d 100644 --- a/desktop-widgets/mainwindow.cpp +++ b/desktop-widgets/mainwindow.cpp @@ -15,7 +15,6 @@ #include "version.h" #include "divelistview.h" #include "downloadfromdivecomputer.h" -#include "preferences.h" #include "subsurfacewebservices.h" #include "divecomputermanagementdialog.h" #include "about.h" @@ -278,8 +277,6 @@ MainWindow::MainWindow() : QMainWindow(), ui.menubar->show(); set_git_update_cb(&updateProgress); - PreferencesDialogV2 *d = new PreferencesDialogV2(); - d->show(); } MainWindow::~MainWindow() @@ -1785,6 +1782,7 @@ void MainWindow::editCurrentDive() } } +// TODO: Remove the dependency to the PreferencesDialog here. #define PREF_PROFILE(QT_PREFS) \ QSettings s; \ s.beginGroup("TecDetails"); \ diff --git a/desktop-widgets/preferences.h b/desktop-widgets/preferences.h deleted file mode 100644 index 4b619dde4..000000000 --- a/desktop-widgets/preferences.h +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef PREFERENCES_H -#define PREFERENCES_H - -#include -#include "pref.h" - -#include "ui_preferences.h" - -#ifndef Q_OS_ANDROID - class QWebView; -#endif - -class QAbstractButton; - -class PreferencesDialog : public QDialog { - Q_OBJECT -public: - static PreferencesDialog *instance(); - void showEvent(QShowEvent *); - void emitSettingsChanged(); - -signals: - void settingsChanged(); -public -slots: - void buttonClicked(QAbstractButton *button); - void syncSettings(); - void loadSettings(); - void restorePrefs(); - void rememberPrefs(); - void facebookLoggedIn(); - void facebookDisconnect(); -private: - explicit PreferencesDialog(QWidget *parent = 0, Qt::WindowFlags f = 0); - void setUiFromPrefs(); - Ui::PreferencesDialog ui; - struct preferences oldPrefs; - #ifndef Q_OS_ANDROID - QWebView *facebookWebView; - #endif -}; - -#endif // PREFERENCES_H diff --git a/desktop-widgets/preferences/preferences_network.cpp b/desktop-widgets/preferences/preferences_network.cpp index 000df1e7f..3780a6c91 100644 --- a/desktop-widgets/preferences/preferences_network.cpp +++ b/desktop-widgets/preferences/preferences_network.cpp @@ -1,6 +1,5 @@ #include "preferences_network.h" #include "ui_preferences_network.h" -#include "preferences.h" #include "dive.h" #include "subsurfacewebservices.h" #include "subsurface-core/prefs-macros.h" diff --git a/desktop-widgets/preferences/preferencesdialog.cpp b/desktop-widgets/preferences/preferencesdialog.cpp index 5b4cc560e..d59296519 100644 --- a/desktop-widgets/preferences/preferencesdialog.cpp +++ b/desktop-widgets/preferences/preferencesdialog.cpp @@ -16,7 +16,18 @@ #include #include -PreferencesDialogV2::PreferencesDialogV2() +PreferencesDialog* PreferencesDialog::instance() +{ + PreferencesDialog *self = new PreferencesDialog(); + return self; +} + +void PreferencesDialog::emitSettingsChanged() +{ + emit settingsChanged(); +} + +PreferencesDialog::PreferencesDialog() { pagesList = new QListWidget(); pagesStack = new QStackedWidget(); @@ -48,14 +59,14 @@ PreferencesDialogV2::PreferencesDialogV2() connect(pagesList, &QListWidget::currentRowChanged, pagesStack, &QStackedWidget::setCurrentIndex); connect(buttonBox, &QDialogButtonBox::clicked, - this, &PreferencesDialogV2::buttonClicked); + this, &PreferencesDialog::buttonClicked); } -PreferencesDialogV2::~PreferencesDialogV2() +PreferencesDialog::~PreferencesDialog() { } -void PreferencesDialogV2::buttonClicked(QAbstractButton* btn) +void PreferencesDialog::buttonClicked(QAbstractButton* btn) { QDialogButtonBox::ButtonRole role = buttonBox->buttonRole(btn); switch(role) { @@ -70,13 +81,13 @@ bool abstractpreferenceswidget_lessthan(AbstractPreferencesWidget *p1, AbstractP return p1->positionHeight() <= p2->positionHeight(); } -void PreferencesDialogV2::addPreferencePage(AbstractPreferencesWidget *page) +void PreferencesDialog::addPreferencePage(AbstractPreferencesWidget *page) { pages.push_back(page); qSort(pages.begin(), pages.end(), abstractpreferenceswidget_lessthan); } -void PreferencesDialogV2::refreshPages() +void PreferencesDialog::refreshPages() { // Remove things pagesList->clear(); @@ -95,7 +106,7 @@ void PreferencesDialogV2::refreshPages() } } -void PreferencesDialogV2::applyRequested() +void PreferencesDialog::applyRequested() { Q_FOREACH(AbstractPreferencesWidget *page, pages) { page->syncSettings(); @@ -104,7 +115,7 @@ void PreferencesDialogV2::applyRequested() accept(); } -void PreferencesDialogV2::cancelRequested() +void PreferencesDialog::cancelRequested() { Q_FOREACH(AbstractPreferencesWidget *page, pages) { page->refreshSettings(); @@ -112,7 +123,7 @@ void PreferencesDialogV2::cancelRequested() reject(); } -void PreferencesDialogV2::defaultsRequested() +void PreferencesDialog::defaultsRequested() { prefs = default_prefs; Q_FOREACH(AbstractPreferencesWidget *page, pages) { diff --git a/desktop-widgets/preferences/preferencesdialog.h b/desktop-widgets/preferences/preferencesdialog.h index 720b94c25..611bd5fac 100644 --- a/desktop-widgets/preferences/preferencesdialog.h +++ b/desktop-widgets/preferences/preferencesdialog.h @@ -10,16 +10,18 @@ class QStackedWidget; class QDialogButtonBox; class QAbstractButton; -class PreferencesDialogV2 : public QDialog { +class PreferencesDialog : public QDialog { Q_OBJECT public: - PreferencesDialogV2(); - virtual ~PreferencesDialogV2(); + static PreferencesDialog* instance(); + virtual ~PreferencesDialog(); void addPreferencePage(AbstractPreferencesWidget *page); void refreshPages(); + void emitSettingsChanged(); signals: void settingsChanged(); private: + PreferencesDialog(); void cancelRequested(); void applyRequested(); void defaultsRequested(); diff --git a/profile-widget/divecartesianaxis.cpp b/profile-widget/divecartesianaxis.cpp index bf5a5380c..f40e1c3e5 100644 --- a/profile-widget/divecartesianaxis.cpp +++ b/profile-widget/divecartesianaxis.cpp @@ -1,7 +1,7 @@ #include "divecartesianaxis.h" #include "divetextitem.h" #include "helpers.h" -#include "preferences.h" +#include "preferences/preferencesdialog.h" #include "diveplotdatamodel.h" #include "animationfunctions.h" #include "mainwindow.h" diff --git a/profile-widget/divepixmapitem.cpp b/profile-widget/divepixmapitem.cpp index 581f6f9b4..627473c2f 100644 --- a/profile-widget/divepixmapitem.cpp +++ b/profile-widget/divepixmapitem.cpp @@ -1,7 +1,7 @@ #include "divepixmapitem.h" #include "animationfunctions.h" #include "divepicturemodel.h" -#include +#include "preferences/preferencesdialog.h" #include #include diff --git a/profile-widget/diveprofileitem.cpp b/profile-widget/diveprofileitem.cpp index 7cdccee32..14efa9123 100644 --- a/profile-widget/diveprofileitem.cpp +++ b/profile-widget/diveprofileitem.cpp @@ -5,7 +5,7 @@ #include "animationfunctions.h" #include "dive.h" #include "profile.h" -#include "preferences.h" +#include "preferences/preferencesdialog.h" #include "diveplannermodel.h" #include "helpers.h" #include "libdivecomputer/parser.h" diff --git a/profile-widget/profilewidget2.cpp b/profile-widget/profilewidget2.cpp index 3ccd1bb6d..8ff8e8669 100644 --- a/profile-widget/profilewidget2.cpp +++ b/profile-widget/profilewidget2.cpp @@ -29,7 +29,7 @@ #include #endif #include "mainwindow.h" -#include +#include "preferences/preferencesdialog.h" /* This is the global 'Item position' variable. * it should tell you where to position things up diff --git a/profile-widget/ruleritem.cpp b/profile-widget/ruleritem.cpp index 830985552..a5a61c0fe 100644 --- a/profile-widget/ruleritem.cpp +++ b/profile-widget/ruleritem.cpp @@ -1,5 +1,5 @@ #include "ruleritem.h" -#include "preferences.h" +#include "preferences/preferencesdialog.h" #include "mainwindow.h" #include "profilewidget2.h" #include "display.h" -- cgit v1.2.3-70-g09d2