Chart Dates: Part 2 of 3 fixup

Refactor of the date settings into its own
widget so we can re-use it across any chart
that supports date ranges.
This commit is contained in:
Mark Liversedge
2013-01-03 14:29:45 +00:00
parent b33aec2cc0
commit 8b3a30b232
5 changed files with 278 additions and 206 deletions

View File

@@ -81,68 +81,8 @@ LTMTool::LTMTool(MainWindow *parent, const QDir &home, bool multi) : QWidget(par
basicLayout->addLayout(presetrow);
basicLayout->addStretch();
radioSelected = new QRadioButton(tr("Current selection"), this);
radioSelected->setChecked(true);
radioSelected->setFont(sameFont);
QHBoxLayout *selected = new QHBoxLayout; // use same layout mechanism as custom so they align
selected->addWidget(radioSelected);
selected->addStretch();
basicsettingsLayout->addRow(new QLabel(tr("Date Range")), selected);
radioToday = new QRadioButton(tr("Current selection thru today"), this);
radioToday->setChecked(false);
radioToday->setFont(sameFont);
QHBoxLayout *today = new QHBoxLayout; // use same layout mechanism as custom so they align
today->addWidget(radioToday);
today->addStretch();
basicsettingsLayout->addRow(new QLabel(""), today);
radioFrom = new QRadioButton(tr("From"), this);
radioFrom->setChecked(false);
radioFrom->setFont(sameFont);
startDateEdit = new QDateEdit(this);
startDateEdit->setDate(QDate::currentDate().addMonths(-3));
QHBoxLayout *from = new QHBoxLayout;
from->addWidget(radioFrom);
from->addWidget(startDateEdit);
from->addWidget(new QLabel(tr("to today")));
from->addStretch();
basicsettingsLayout->addRow(new QLabel(""), from);
radioCustom = new QRadioButton(tr("Between"), this);
radioCustom->setFont(sameFont);
radioCustom->setChecked(false);
fromDateEdit = new QDateEdit(this);
toDateEdit = new QDateEdit(this);
QHBoxLayout *custom = new QHBoxLayout;
custom->addWidget(radioCustom);
custom->addWidget(fromDateEdit);
custom->addWidget(new QLabel(tr("and")));
custom->addWidget(toDateEdit);
custom->addStretch();
basicsettingsLayout->addRow(new QLabel(""), custom);
radioLast = new QRadioButton(tr("Last"), this);
radioLast->setFont(sameFont);
radioLast->setChecked(false);
lastn = new QDoubleSpinBox(this);
lastn->setSingleStep(1.0);
lastn->setDecimals(0);
lastn->setMinimum(0);
lastn->setMaximum(999);
lastn->setValue(7);
lastnx = new QComboBox(this);
lastnx->addItem(tr("days"));
lastnx->addItem(tr("weeks"));
lastnx->addItem(tr("months"));
lastnx->addItem(tr("years"));
lastnx->setCurrentIndex(0);
QHBoxLayout *last = new QHBoxLayout;
last->addWidget(radioLast);
last->addWidget(lastn);
last->addWidget(lastnx);
last->addStretch();
basicsettingsLayout->addRow(new QLabel(""), last);
dateSetting = new DateSettingsEdit(this);
basicsettingsLayout->addRow(new QLabel(tr("Date range")), dateSetting);
basicsettingsLayout->addRow(new QLabel(tr(""))); // spacing
groupBy = new QComboBox;
@@ -623,58 +563,9 @@ LTMTool::LTMTool(MainWindow *parent, const QDir &home, bool multi) : QWidget(par
this, SLOT(metricTreePopup(const QPoint &)));
// switched between one or other
connect(radioSelected, SIGNAL(toggled(bool)), this, SLOT(setDateSettings()));
connect(radioToday, SIGNAL(toggled(bool)), this, SLOT(setDateSettings()));
connect(radioCustom, SIGNAL(toggled(bool)), this, SLOT(setDateSettings()));
connect(radioLast, SIGNAL(toggled(bool)), this, SLOT(setDateSettings()));
connect(radioFrom, SIGNAL(toggled(bool)), this, SLOT(setDateSettings()));
connect(fromDateEdit, SIGNAL(dateChanged(QDate)), this, SLOT(setDateSettings()));
connect(toDateEdit, SIGNAL(dateChanged(QDate)), this, SLOT(setDateSettings()));
connect(startDateEdit, SIGNAL(dateChanged(QDate)), this, SLOT(setDateSettings()));
connect(lastn, SIGNAL(valueChanged(double)), this, SLOT(setDateSettings()));
connect(lastnx, SIGNAL(currentIndexChanged(int)), this, SLOT(setDateSettings()));
}
int
LTMTool::useSelected()
{
if (radioSelected->isChecked()) return 0;
if (radioFrom->isChecked()) return 1;
if (radioCustom->isChecked()) return 2;
if (radioLast->isChecked()) return 3;
if (radioToday->isChecked()) return 4;
return 0; // keep compiler happy
}
void
LTMTool::setUseSelected(int x)
{
active = true;
radioSelected->setChecked(false);
radioFrom->setChecked(false);
radioCustom->setChecked(false);
radioLast->setChecked(false);
radioToday->setChecked(false);
active = false;
switch(x) {
case 0:
radioSelected->setChecked(true);
break;
case 1:
radioFrom->setChecked(true);
break;
case 2:
radioCustom->setChecked(true);
break;
case 3:
radioLast->setChecked(true);
break;
case 4:
radioToday->setChecked(true);
break;
}
connect(dateSetting, SIGNAL(useStandardRange()), this, SIGNAL(useStandardRange()));
connect(dateSetting, SIGNAL(useCustomRange(DateRange)), this, SIGNAL(useCustomRange(DateRange)));
connect(dateSetting, SIGNAL(useThruToday()), this, SIGNAL(useThruToday()));
}
QwtPlotCurve::CurveStyle
@@ -1054,80 +945,6 @@ LTMTool::setFilter(QStringList files)
emit filterChanged();
}
void
LTMTool::setDateSettings()
{
if (active) return;
// first lets disable everything
active = true;
fromDateEdit->setEnabled(false);
toDateEdit->setEnabled(false);
startDateEdit->setEnabled(false);
lastn->setEnabled(false);
lastnx->setEnabled(false);
// the date selection types have changed
if (radioSelected->isChecked()) {
// current selection
emit useStandardRange();
} else if (radioCustom->isChecked()) {
// between x and y
fromDateEdit->setEnabled(true);
toDateEdit->setEnabled(true);
// set date range using custom values
emit useCustomRange(DateRange(fromDateEdit->date(), toDateEdit->date()));
} else if (radioToday->isChecked()) {
// current selected thru to today
emit useThruToday();
} else if (radioLast->isChecked()) {
// last n 'weeks etc'
lastn->setEnabled(true);
lastnx->setEnabled(true);
QDate from;
QDate today = QDate::currentDate();
// calculate range up to today...
switch(lastnx->currentIndex()) {
case 0 : // days
from = today.addDays(lastn->value() * -1);
break;
case 1 : // weeks
from = today.addDays(lastn->value() * -7);
break;
case 2 : // months
from = today.addMonths(lastn->value() * -1);
break;
case 3 : // years
from = today.addYears(lastn->value() * -1);
break;
}
emit useCustomRange(DateRange(from, today));
} else if (radioFrom->isChecked()) {
// from date - today
startDateEdit->setEnabled(true);
emit useCustomRange(DateRange(startDateEdit->date(), QDate::currentDate()));
}
active = false;
}
void
LTMTool::translateDefaultCharts(QList<LTMSettings>&charts)
{

View File

@@ -93,10 +93,8 @@ class LTMTool : public QWidget
QCheckBox *showLegend;
QPushButton *saveButton;
QPushButton *manageButton;
QRadioButton *radioSelected, *radioToday, *radioCustom, *radioLast, *radioFrom;
QDateEdit *fromDateEdit, *toDateEdit, *startDateEdit;
QDoubleSpinBox *lastn;
QComboBox *lastnx;
DateSettingsEdit *dateSetting;
signals:
@@ -123,7 +121,6 @@ class LTMTool : public QWidget
void clearFilter();
void setFilter(QStringList);
void setDateSettings(); // when settings are updated wrt date selections
private:

View File

@@ -120,20 +120,20 @@ class LTMWindow : public LTMPlotContainer
bool legend() const { return ltmTool->showLegend->isChecked(); }
void setLegend(bool x) { ltmTool->showLegend->setChecked(x); }
int useSelected() { return ltmTool->useSelected(); }
void setUseSelected(int x) { ltmTool->setUseSelected(x); }
int useSelected() { return ltmTool->dateSetting->mode(); }
void setUseSelected(int x) { ltmTool->dateSetting->setMode(x); }
QDate fromDate() { return ltmTool->fromDateEdit->date(); }
void setFromDate(QDate date) { return ltmTool->fromDateEdit->setDate(date); }
QDate toDate() { return ltmTool->toDateEdit->date(); }
void setToDate(QDate date) { return ltmTool->toDateEdit->setDate(date); }
QDate startDate() { return ltmTool->startDateEdit->date(); }
void setStartDate(QDate date) { return ltmTool->startDateEdit->setDate(date); }
QDate fromDate() { return ltmTool->dateSetting->fromDate(); }
void setFromDate(QDate date) { return ltmTool->dateSetting->setFromDate(date); }
QDate toDate() { return ltmTool->dateSetting->toDate(); }
void setToDate(QDate date) { return ltmTool->dateSetting->setToDate(date); }
QDate startDate() { return ltmTool->dateSetting->startDate(); }
void setStartDate(QDate date) { return ltmTool->dateSetting->setStartDate(date); }
int lastN() { return ltmTool->lastn->value(); }
void setLastN(int x) { ltmTool->lastn->setValue(x); }
int lastNX() { return ltmTool->lastnx->currentIndex(); }
void setLastNX(int x) { ltmTool->lastnx->setCurrentIndex(x); }
int lastN() { return ltmTool->dateSetting->lastN(); }
void setLastN(int x) { ltmTool->dateSetting->setLastN(x); }
int lastNX() { return ltmTool->dateSetting->lastNX(); }
void setLastNX(int x) { ltmTool->dateSetting->setLastNX(x); }
#ifdef GC_HAVE_LUCENE
QString filter() const { return ltmTool->searchBox->filter(); }

View File

@@ -19,6 +19,8 @@
#include "TimeUtils.h"
#include <math.h>
#include <QRegExpValidator>
#include <QFormLayout>
#include <QLabel>
QString time_to_string(double secs)
{
@@ -120,3 +122,208 @@ DateRange& DateRange::operator=(const DateRange &other)
return *this;
}
DateSettingsEdit::DateSettingsEdit(QWidget *parent) : parent(parent), active(true)
{
setContentsMargins(0,0,0,0);
QFormLayout *mainLayout = new QFormLayout(this);
mainLayout->setContentsMargins(0,0,0,0);
mainLayout->setSpacing(0);
QFont sameFont;
#ifdef Q_OS_MAC
sameFont.setPointSize(sameFont.pointSize() + 2);
#endif
radioSelected = new QRadioButton(tr("Current selection"), this);
radioSelected->setChecked(true);
radioSelected->setFont(sameFont);
QHBoxLayout *selected = new QHBoxLayout; // use same layout mechanism as custom so they align
selected->addWidget(radioSelected);
selected->addStretch();
mainLayout->addRow(selected);
radioToday = new QRadioButton(tr("Current selection thru today"), this);
radioToday->setChecked(false);
radioToday->setFont(sameFont);
QHBoxLayout *today = new QHBoxLayout; // use same layout mechanism as custom so they align
today->addWidget(radioToday);
today->addStretch();
mainLayout->addRow(today);
radioFrom = new QRadioButton(tr("From"), this);
radioFrom->setChecked(false);
radioFrom->setFont(sameFont);
startDateEdit = new QDateEdit(this);
startDateEdit->setDate(QDate::currentDate().addMonths(-3));
QHBoxLayout *from = new QHBoxLayout;
from->addWidget(radioFrom);
from->addWidget(startDateEdit);
from->addWidget(new QLabel(tr("to today")));
from->addStretch();
mainLayout->addRow(from);
radioCustom = new QRadioButton(tr("Between"), this);
radioCustom->setFont(sameFont);
radioCustom->setChecked(false);
fromDateEdit = new QDateEdit(this);
toDateEdit = new QDateEdit(this);
QHBoxLayout *custom = new QHBoxLayout;
custom->addWidget(radioCustom);
custom->addWidget(fromDateEdit);
custom->addWidget(new QLabel(tr("and")));
custom->addWidget(toDateEdit);
custom->addStretch();
mainLayout->addRow(custom);
radioLast = new QRadioButton(tr("Last"), this);
radioLast->setFont(sameFont);
radioLast->setChecked(false);
lastn = new QDoubleSpinBox(this);
lastn->setSingleStep(1.0);
lastn->setDecimals(0);
lastn->setMinimum(0);
lastn->setMaximum(999);
lastn->setValue(7);
lastnx = new QComboBox(this);
lastnx->addItem(tr("days"));
lastnx->addItem(tr("weeks"));
lastnx->addItem(tr("months"));
lastnx->addItem(tr("years"));
lastnx->setCurrentIndex(0);
QHBoxLayout *last = new QHBoxLayout;
last->addWidget(radioLast);
last->addWidget(lastn);
last->addWidget(lastnx);
last->addStretch();
mainLayout->addRow(last);
// switched between one or other
connect(radioSelected, SIGNAL(toggled(bool)), this, SLOT(setDateSettings()));
connect(radioToday, SIGNAL(toggled(bool)), this, SLOT(setDateSettings()));
connect(radioCustom, SIGNAL(toggled(bool)), this, SLOT(setDateSettings()));
connect(radioLast, SIGNAL(toggled(bool)), this, SLOT(setDateSettings()));
connect(radioFrom, SIGNAL(toggled(bool)), this, SLOT(setDateSettings()));
connect(fromDateEdit, SIGNAL(dateChanged(QDate)), this, SLOT(setDateSettings()));
connect(toDateEdit, SIGNAL(dateChanged(QDate)), this, SLOT(setDateSettings()));
connect(startDateEdit, SIGNAL(dateChanged(QDate)), this, SLOT(setDateSettings()));
connect(lastn, SIGNAL(valueChanged(double)), this, SLOT(setDateSettings()));
connect(lastnx, SIGNAL(currentIndexChanged(int)), this, SLOT(setDateSettings()));
}
void
DateSettingsEdit::setDateSettings()
{
if (active) return;
// first lets disable everything
active = true;
fromDateEdit->setEnabled(false);
toDateEdit->setEnabled(false);
startDateEdit->setEnabled(false);
lastn->setEnabled(false);
lastnx->setEnabled(false);
// the date selection types have changed
if (radioSelected->isChecked()) {
// current selection
emit useStandardRange();
} else if (radioCustom->isChecked()) {
// between x and y
fromDateEdit->setEnabled(true);
toDateEdit->setEnabled(true);
// set date range using custom values
emit useCustomRange(DateRange(fromDateEdit->date(), toDateEdit->date()));
} else if (radioToday->isChecked()) {
// current selected thru to today
emit useThruToday();
} else if (radioLast->isChecked()) {
// last n 'weeks etc'
lastn->setEnabled(true);
lastnx->setEnabled(true);
QDate from;
QDate today = QDate::currentDate();
// calculate range up to today...
switch(lastnx->currentIndex()) {
case 0 : // days
from = today.addDays(lastn->value() * -1);
break;
case 1 : // weeks
from = today.addDays(lastn->value() * -7);
break;
case 2 : // months
from = today.addMonths(lastn->value() * -1);
break;
case 3 : // years
from = today.addYears(lastn->value() * -1);
break;
}
emit useCustomRange(DateRange(from, today));
} else if (radioFrom->isChecked()) {
// from date - today
startDateEdit->setEnabled(true);
emit useCustomRange(DateRange(startDateEdit->date(), QDate::currentDate()));
}
active = false;
}
int
DateSettingsEdit::mode()
{
if (radioSelected->isChecked()) return 0;
if (radioFrom->isChecked()) return 1;
if (radioCustom->isChecked()) return 2;
if (radioLast->isChecked()) return 3;
if (radioToday->isChecked()) return 4;
return 0; // keep compiler happy
}
void
DateSettingsEdit::setMode(int x)
{
active = true;
radioSelected->setChecked(false);
radioFrom->setChecked(false);
radioCustom->setChecked(false);
radioLast->setChecked(false);
radioToday->setChecked(false);
active = false;
switch(x) {
case 0:
radioSelected->setChecked(true);
break;
case 1:
radioFrom->setChecked(true);
break;
case 2:
radioCustom->setChecked(true);
break;
case 3:
radioLast->setChecked(true);
break;
case 4:
radioToday->setChecked(true);
break;
}
}

View File

@@ -20,9 +20,13 @@
#define _TimeUtils_h
#include <QObject>
#include <QDateTime>
#include <QDate>
#include <QDateEdit>
#include <QDateTime>
#include <QString>
#include <QRadioButton>
#include <QDoubleSpinBox>
#include <QComboBox>
QString interval_to_str(double secs); // output like 1h 2m 3s
double str_to_interval(QString s); // convert 1h 2m 3s -> 3123.0 , e.g.
@@ -47,4 +51,51 @@ class DateRange : QObject
void changed(QDate from, QDate to);
};
class DateSettingsEdit : public QWidget
{
Q_OBJECT
private:
QWidget *parent;
bool active;
// editing components
QRadioButton *radioSelected, *radioToday, *radioCustom, *radioLast, *radioFrom;
QDateEdit *fromDateEdit, *toDateEdit, *startDateEdit;
QDoubleSpinBox *lastn;
QComboBox *lastnx;
public:
DateSettingsEdit(QWidget *parent);
void setMode(int);
int mode();
// betweem from and to
void setFromDate(QDate x) { fromDateEdit->setDate(x); }
QDate fromDate() { return fromDateEdit->date(); }
void setToDate(QDate x) { toDateEdit->setDate(x); }
QDate toDate() { return toDateEdit->date(); }
// start date till today
void setStartDate(QDate x) { startDateEdit->setDate(x); }
QDate startDate() { return startDateEdit->date(); }
// last n of days/weeks/months/years
int lastN() { return lastn->value(); }
void setLastN(int x) { lastn->setValue(x); }
int lastNX() { return lastnx->currentIndex(); }
void setLastNX(int x) { lastnx->setCurrentIndex(x); }
private slots:
void setDateSettings();
signals:
void useStandardRange();
void useThruToday();
void useCustomRange(DateRange);
};
#endif // _TimeUtils_h