From b0f374c5c9764fea8ed9f170576714f58366b4df Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sun, 20 Dec 2020 18:29:57 +0100 Subject: profile: move allocation of DiveProfileItems into a template Instead of typing out the same arguments again and again, do the allocation of DiveProfileItems in a templated function. Signed-off-by: Berthold Stoeger --- profile-widget/profilewidget2.cpp | 46 +++++++++++++++++++++++---------------- profile-widget/profilewidget2.h | 1 + 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/profile-widget/profilewidget2.cpp b/profile-widget/profilewidget2.cpp index 464cb515a..ef81f5bb0 100644 --- a/profile-widget/profilewidget2.cpp +++ b/profile-widget/profilewidget2.cpp @@ -96,6 +96,14 @@ static struct _ItemPos { static const double thumbnailBaseZValue = 100.0; #endif +template +T *ProfileWidget2::createItem(const DiveCartesianAxis &vAxis, int vColumn, Args&&... args) +{ + T *res = new T(*dataModel, *timeAxis, DivePlotDataModel::TIME, vAxis, vColumn, + std::forward(args)...); + return res; +} + ProfileWidget2::ProfileWidget2(QWidget *parent) : QGraphicsView(parent), currentState(INVALID), dataModel(new DivePlotDataModel(this)), @@ -111,29 +119,29 @@ ProfileWidget2::ProfileWidget2(QWidget *parent) : QGraphicsView(parent), gasYAxis(new PartialGasPressureAxis(this)), temperatureAxis(new TemperatureAxis(this)), timeAxis(new TimeAxis(this)), - diveProfileItem(new DiveProfileItem(*dataModel, *timeAxis, DivePlotDataModel::TIME, *profileYAxis, DivePlotDataModel::DEPTH)), - temperatureItem(new DiveTemperatureItem(*dataModel, *timeAxis, DivePlotDataModel::TIME, *temperatureAxis, DivePlotDataModel::TEMPERATURE)), - meanDepthItem(new DiveMeanDepthItem(*dataModel, *timeAxis, DivePlotDataModel::TIME, *profileYAxis, DivePlotDataModel::INSTANT_MEANDEPTH)), + diveProfileItem(createItem(*profileYAxis, DivePlotDataModel::DEPTH)), + temperatureItem(createItem(*temperatureAxis, DivePlotDataModel::TEMPERATURE)), + meanDepthItem(createItem(*profileYAxis, DivePlotDataModel::INSTANT_MEANDEPTH)), cylinderPressureAxis(new DiveCartesianAxis(this)), - gasPressureItem(new DiveGasPressureItem(*dataModel, *timeAxis, DivePlotDataModel::TIME, *cylinderPressureAxis, DivePlotDataModel::TEMPERATURE)), + gasPressureItem(createItem(*cylinderPressureAxis, DivePlotDataModel::TEMPERATURE)), diveComputerText(new DiveTextItem()), - reportedCeiling(new DiveReportedCeiling(*dataModel, *timeAxis, DivePlotDataModel::TIME, *profileYAxis, DivePlotDataModel::CEILING)), - pn2GasItem(new PartialPressureGasItem(*dataModel, *timeAxis, DivePlotDataModel::TIME, *gasYAxis, DivePlotDataModel::PN2)), - pheGasItem(new PartialPressureGasItem(*dataModel, *timeAxis, DivePlotDataModel::TIME, *gasYAxis, DivePlotDataModel::PHE)), - po2GasItem(new PartialPressureGasItem(*dataModel, *timeAxis, DivePlotDataModel::TIME, *gasYAxis, DivePlotDataModel::PO2)), - o2SetpointGasItem(new PartialPressureGasItem(*dataModel, *timeAxis, DivePlotDataModel::TIME, *gasYAxis, DivePlotDataModel::O2SETPOINT)), - ccrsensor1GasItem(new PartialPressureGasItem(*dataModel, *timeAxis, DivePlotDataModel::TIME, *gasYAxis, DivePlotDataModel::CCRSENSOR1)), - ccrsensor2GasItem(new PartialPressureGasItem(*dataModel, *timeAxis, DivePlotDataModel::TIME, *gasYAxis, DivePlotDataModel::CCRSENSOR2)), - ccrsensor3GasItem(new PartialPressureGasItem(*dataModel, *timeAxis, DivePlotDataModel::TIME, *gasYAxis, DivePlotDataModel::CCRSENSOR3)), - ocpo2GasItem(new PartialPressureGasItem(*dataModel, *timeAxis, DivePlotDataModel::TIME, *gasYAxis, DivePlotDataModel::SCR_OC_PO2)), + reportedCeiling(createItem(*profileYAxis, DivePlotDataModel::CEILING)), + pn2GasItem(createItem(*gasYAxis, DivePlotDataModel::PN2)), + pheGasItem(createItem(*gasYAxis, DivePlotDataModel::PHE)), + po2GasItem(createItem(*gasYAxis, DivePlotDataModel::PO2)), + o2SetpointGasItem(createItem(*gasYAxis, DivePlotDataModel::O2SETPOINT)), + ccrsensor1GasItem(createItem(*gasYAxis, DivePlotDataModel::CCRSENSOR1)), + ccrsensor2GasItem(createItem(*gasYAxis, DivePlotDataModel::CCRSENSOR2)), + ccrsensor3GasItem(createItem(*gasYAxis, DivePlotDataModel::CCRSENSOR3)), + ocpo2GasItem(createItem(*gasYAxis, DivePlotDataModel::SCR_OC_PO2)), #ifndef SUBSURFACE_MOBILE - diveCeiling(new DiveCalculatedCeiling(*dataModel, *timeAxis, DivePlotDataModel::TIME, *profileYAxis, DivePlotDataModel::CEILING, this)), + diveCeiling(createItem(*profileYAxis, DivePlotDataModel::CEILING, this)), decoModelParameters(new DiveTextItem()), heartBeatAxis(new DiveCartesianAxis(this)), - heartBeatItem(new DiveHeartrateItem(*dataModel, *timeAxis, DivePlotDataModel::TIME, *heartBeatAxis, DivePlotDataModel::HEARTBEAT)), + heartBeatItem(createItem(*heartBeatAxis, DivePlotDataModel::HEARTBEAT)), percentageAxis(new DiveCartesianAxis(this)), - ambPressureItem(new DiveAmbPressureItem(*dataModel, *timeAxis, DivePlotDataModel::TIME, *percentageAxis, DivePlotDataModel::AMBPRESSURE)), - gflineItem(new DiveGFLineItem(*dataModel, *timeAxis, DivePlotDataModel::TIME, *percentageAxis, DivePlotDataModel::GFLINE)), + ambPressureItem(createItem(*percentageAxis, DivePlotDataModel::AMBPRESSURE)), + gflineItem(createItem(*percentageAxis, DivePlotDataModel::GFLINE)), mouseFollowerVertical(new DiveLineItem()), mouseFollowerHorizontal(new DiveLineItem()), rulerItem(new RulerItem2()), @@ -342,10 +350,10 @@ void ProfileWidget2::setupItemOnScene() decoModelParameters->setAlignment(Qt::AlignHCenter | Qt::AlignBottom); setupItem(diveCeiling, 1); for (int i = 0; i < 16; i++) { - DiveCalculatedTissue *tissueItem = new DiveCalculatedTissue(*dataModel, *timeAxis, DivePlotDataModel::TIME, *profileYAxis, DivePlotDataModel::TISSUE_1 + i, this); + DiveCalculatedTissue *tissueItem = createItem(*profileYAxis, DivePlotDataModel::TISSUE_1 + i, this); setupItem(tissueItem, 1 + i); allTissues.append(tissueItem); - DivePercentageItem *percentageItem = new DivePercentageItem(*dataModel, *timeAxis, DivePlotDataModel::TIME, *percentageAxis, DivePlotDataModel::PERCENTAGE_1 + i, i); + DivePercentageItem *percentageItem = createItem(*percentageAxis, DivePlotDataModel::PERCENTAGE_1 + i, i); setupItem(percentageItem, 1 + i); allPercentages.append(percentageItem); } diff --git a/profile-widget/profilewidget2.h b/profile-widget/profilewidget2.h index ff70c4c0e..19fac368b 100644 --- a/profile-widget/profilewidget2.h +++ b/profile-widget/profilewidget2.h @@ -152,6 +152,7 @@ private: void fixBackgroundPos(); void scrollViewTo(const QPoint &pos); void setupSceneAndFlags(); + template T *createItem(const DiveCartesianAxis &vAxis, int vColumn, Args&&... args); void setupItemSizes(); void addItemsToScene(); void setupItemOnScene(); -- cgit v1.2.3-70-g09d2