CriticalPowerWindow::CriticalPowerWindow(const QDir &home, MainWindow *parent, bool rangemode) :
    GcWindow(parent), _dateRange("{00000000-0000-0000-0000-000000000001}"), home(home), mainWindow(parent), currentRide(NULL), rangemode(rangemode), stale(true), useCustom(false)
{
    setInstanceName("Critical Power Window");

    // main plot area
    QVBoxLayout *vlayout = new QVBoxLayout;
    cpintPlot = new CpintPlot(mainWindow, home.path(), mainWindow->zones());
    vlayout->addWidget(cpintPlot);
    setLayout(vlayout);

    // controls
    QWidget *c = new QWidget;
    QFormLayout *cl = new QFormLayout(c);
    setControls(c);

#ifdef GC_HAVE_LUCENE
    // searchbox
    searchBox = new SearchFilterBox(this, parent);
    connect(searchBox, SIGNAL(searchClear()), cpintPlot, SLOT(clearFilter()));
    connect(searchBox, SIGNAL(searchResults(QStringList)), cpintPlot, SLOT(setFilter(QStringList)));
    connect(searchBox, SIGNAL(searchClear()), this, SLOT(filterChanged()));
    connect(searchBox, SIGNAL(searchResults(QStringList)), this, SLOT(filterChanged()));
    cl->addRow(new QLabel(tr("Filter")), searchBox);
    cl->addWidget(new QLabel("")); //spacing
#endif

    // picker details
    QLabel *cpintTimeLabel = new QLabel(tr("Duration:"), this);
    cpintTimeValue = new QLineEdit("0 s");
    QLabel *cpintTodayLabel = new QLabel(tr("Today:"), this);
    cpintTodayValue = new QLabel(tr("no data"));
    QLabel *cpintAllLabel = new QLabel(tr("Best:"), this);
    cpintAllValue = new QLabel(tr("no data"));
    QLabel *cpintCPLabel = new QLabel(tr("CP Curve:"), this);
    cpintCPValue = new QLabel(tr("no data"));

    //QFontMetrics metrics(QApplication::font());
    //int width = metrics.width("8888 watts (88/88/8888)") + 10;
    //cpintAllValue->setFixedWidth(width);
    //cpintCPValue->setFixedWidth(width); // so lines up nicely

    cpintTimeValue->setReadOnly(false);
    //cpintTodayValue->setReadOnly(true);
    //cpintAllValue->setReadOnly(true);
    //cpintCPValue->setReadOnly(true);

    QFont font = cpintTimeValue->font();
    font.setPointSize(font.pointSize());
    cpintTodayValue->setFont(font);
    cpintAllValue->setFont(font);
    cpintCPValue->setFont(font);

    cl->addRow(cpintTimeLabel, cpintTimeValue);
    cl->addRow(cpintTodayLabel, cpintTodayValue);
    cl->addRow(cpintAllLabel, cpintAllValue);
    cl->addRow(cpintCPLabel, cpintCPValue);
    cl->addWidget(new QLabel("")); //spacing

    // tools /properties
    seriesCombo = new QComboBox(this);
    addSeries();
    cComboSeason = new QComboBox(this);
    seasons = parent->seasons;
    resetSeasons();
    QLabel *label = new QLabel(tr("Date range"));
    QLabel *label2 = new QLabel(tr("Date range"));
    if (rangemode) {
        cComboSeason->hide();
        label2->hide();
    }

    cpintSetCPButton = new QPushButton(tr("&Save CP value"), this);
    cpintSetCPButton->setEnabled(false);
    cl->addRow(label2, cComboSeason);

    dateSetting = new DateSettingsEdit(this);
    cl->addRow(label, dateSetting);

    if (rangemode == false) {
        dateSetting->hide();
        label->hide();
    }

    cl->addWidget(new QLabel("")); //spacing
    cl->addRow(new QLabel(tr("Data series")), seriesCombo);
    cl->addRow(new QLabel(""), cpintSetCPButton);

    picker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft,
                               QwtPicker::VLineRubberBand,
                               QwtPicker::AlwaysOff, cpintPlot->canvas());
    picker->setStateMachine(new QwtPickerDragPointMachine);
    picker->setRubberBandPen(GColor(CPLOTTRACKER));

    connect(picker, SIGNAL(moved(const QPoint &)), SLOT(pickerMoved(const QPoint &)));
    connect(cpintTimeValue, SIGNAL(editingFinished()), this, SLOT(cpintTimeValueEntered()));
    connect(cpintSetCPButton, SIGNAL(clicked()), this, SLOT(cpintSetCPButtonClicked()));
    if (rangemode) {
        connect(this, SIGNAL(dateRangeChanged(DateRange)), SLOT(dateRangeChanged(DateRange)));
    } else {
        connect(cComboSeason, SIGNAL(currentIndexChanged(int)), this, SLOT(seasonSelected(int)));
    }

    connect(seriesCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(setSeries(int)));
    //connect(mainWindow, SIGNAL(rideSelected()), this, SLOT(rideSelected()));
    connect(this, SIGNAL(rideItemChanged(RideItem*)), this, SLOT(rideSelected()));
    connect(mainWindow, SIGNAL(configChanged()), cpintPlot, SLOT(configChanged()));

    // redraw on config change -- this seems the simplest approach
    connect(mainWindow, SIGNAL(configChanged()), this, SLOT(rideSelected()));
    connect(mainWindow, SIGNAL(rideAdded(RideItem*)), this, SLOT(newRideAdded(RideItem*)));
    connect(mainWindow, SIGNAL(rideDeleted(RideItem*)), this, SLOT(newRideAdded(RideItem*)));
    connect(seasons, SIGNAL(seasonsChanged()), this, SLOT(resetSeasons()));

    connect(dateSetting, SIGNAL(useCustomRange(DateRange)), this, SLOT(useCustomRange(DateRange)));
    connect(dateSetting, SIGNAL(useThruToday()), this, SLOT(useThruToday()));
    connect(dateSetting, SIGNAL(useStandardRange()), this, SLOT(useStandardRange()));
}
CriticalPowerWindow::CriticalPowerWindow(const QDir &home, MainWindow *parent) :
    QWidget(parent), home(home), mainWindow(parent)
{
    QVBoxLayout *vlayout = new QVBoxLayout;

    cpintPlot = new CpintPlot(home.path());
    vlayout->addWidget(cpintPlot);

    QFormLayout *cpintPickerLayout = new QFormLayout;
    QFormLayout *cpintPickerLayout2 = new QFormLayout;
    QLabel *cpintTimeLabel = new QLabel(tr("Interval Duration:"), this);
    cpintTimeValue = new QLineEdit("0 s");
    QLabel *cpintTodayLabel = new QLabel(tr("Today:"), this);
    cpintTodayValue = new QLineEdit(tr("no data"));
    QLabel *cpintAllLabel = new QLabel(tr("Best:"), this);
    cpintAllValue = new QLineEdit(tr("no data"));
    QLabel *cpintCPLabel = new QLabel(tr("CP Curve:"), this);
    cpintCPValue = new QLineEdit(tr("no data"));

    QFontMetrics metrics(QApplication::font());
    int width = metrics.width("8888 watts (88/88/8888)") + 10;
    cpintAllValue->setFixedWidth(width);
    cpintCPValue->setFixedWidth(width); // so lines up nicely

    cpintTimeValue->setReadOnly(true);
    cpintTodayValue->setReadOnly(true);
    cpintAllValue->setReadOnly(true);
    cpintCPValue->setReadOnly(true);
    cpintPickerLayout->addRow(cpintTimeLabel, cpintTimeValue);
    cpintPickerLayout->addRow(cpintTodayLabel, cpintTodayValue);
    cpintPickerLayout2->addRow(cpintAllLabel, cpintAllValue);
    cpintPickerLayout2->addRow(cpintCPLabel, cpintCPValue);

    QHBoxLayout *bottomLayout = new QHBoxLayout;

    bottomLayout->addLayout(cpintPickerLayout);
    bottomLayout->addLayout(cpintPickerLayout2);

    QVBoxLayout *otherLayout = new QVBoxLayout;
    cComboSeason = new QComboBox(this);
    addSeasons();
    cpintSetCPButton = new QPushButton(tr("&Save CP value"), this);
    cpintSetCPButton->setEnabled(false);
    otherLayout->addWidget(cpintSetCPButton);
    otherLayout->addWidget(cComboSeason);

    bottomLayout->addLayout(otherLayout);

    vlayout->addLayout(bottomLayout);

    setLayout(vlayout);

    picker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft,
                               QwtPicker::PointSelection,
                               QwtPicker::VLineRubberBand,
                               QwtPicker::AlwaysOff, cpintPlot->canvas());
    picker->setRubberBandPen(QColor(Qt::blue));

    connect(picker, SIGNAL(moved(const QPoint &)),
            SLOT(pickerMoved(const QPoint &)));
    connect(cpintSetCPButton, SIGNAL(clicked()),
	    this, SLOT(cpintSetCPButtonClicked()));
    connect(cComboSeason, SIGNAL(currentIndexChanged(int)),
    this, SLOT(seasonSelected(int)));

}