From: Visa Putkinen Date: Sun, 16 Dec 2012 23:34:49 +0000 (+0200) Subject: Move "List/Edit" button to menu bar and add min/max weight stats X-Git-Url: http://git.maemo.org/git/?p=weightgraph;a=commitdiff_plain;h=049783b3f55169effddeb1485791528e4c8a4bd5 Move "List/Edit" button to menu bar and add min/max weight stats --- diff --git a/weightgraph/mainwindow.cpp b/weightgraph/mainwindow.cpp index f4211e1..08f0b3a 100644 --- a/weightgraph/mainwindow.cpp +++ b/weightgraph/mainwindow.cpp @@ -10,7 +10,7 @@ extern WeightDataModel *wdm; MainWindow::MainWindow(QWidget *parent) : - QMainWindow(parent) + QMainWindow(parent), ew(new EditWindow(this)) { #ifdef Q_WS_MAEMO_5 setAttribute(Qt::WA_Maemo5StackedWindow); @@ -21,14 +21,24 @@ MainWindow::MainWindow(QWidget *parent) : QWidget *central = new QWidget(this); QHBoxLayout *topLayout = new QHBoxLayout(central); - // vv LEFT SIDE BEGINS vv + topLayout->addWidget(createLeftColumn(central)); + topLayout->addWidget(createRightColumn(central)); + createAboutDialog(); + createMenuBar(); + + this->setCentralWidget(central); + + grabZoomKeys(Settings::grabZoomKeys()); + connect(Settings::self(), SIGNAL(settingChanged()), this, SLOT(update())); +} + +QWidget *MainWindow::createLeftColumn(QWidget *central) { QWidget *leftContainer = new QWidget(central); QVBoxLayout *leftLayout = new QVBoxLayout(leftContainer); leftLayout->setSpacing(1); leftLayout->setMargin(1); // vvv LEFT TOP BEGINS vvv - //QGroupBox * leftTopContainer = new QGroupBox("Today's weight?", leftContainer); QFrame *leftTopContainer = new QFrame(leftContainer); leftTopContainer->setFrameShadow(QFrame::Sunken); leftTopContainer->setFrameStyle(QFrame::StyledPanel); @@ -51,43 +61,34 @@ MainWindow::MainWindow(QWidget *parent) : leftLayout->addWidget(leftTopContainer); // ^^^ LEFT TOP ENDS ^^^ -// QWidget *vspacer0 = new QWidget(leftContainer); -// vspacer0->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding); -// leftLayout->addWidget(vspacer0); - WeightStatsView *stats = new WeightStatsView(wdm, this); leftLayout->addWidget(stats); -// QWidget *vspacer = new QWidget(leftContainer); -// vspacer->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding); -// leftLayout->addWidget(vspacer); - - ew = new EditWindow(this); - QPushButton *listButton = new QPushButton("List / edit", leftContainer); - connect(listButton, SIGNAL(clicked()), ew, SLOT(show())); - leftLayout->addWidget(listButton); - - topLayout->addWidget(leftContainer); - // ^^ LEFT SIDE ENDS ^^ + return leftContainer; +} +QWidget *MainWindow::createRightColumn(QWidget *central) { + createBigGraph(); smallGraph = new WeightGraphView(wdm, "Small", central); + connect(smallGraph, SIGNAL(clicked()), bigGraph, SLOT(show())); + return smallGraph; +} + +void MainWindow::createBigGraph() { bigGraph = new WeightGraphView(wdm, "Big", this); #ifdef Q_WS_MAEMO_5 bigGraph->setAttribute(Qt::WA_Maemo5StackedWindow); bigGraph->grabZoomKeys(Settings::grabZoomKeys()); #endif bigGraph->setWindowFlags(bigGraph->windowFlags() | Qt::Window); - connect(smallGraph, SIGNAL(clicked()), bigGraph, SLOT(show())); - topLayout->addWidget(smallGraph); - - this->setCentralWidget(central); - +} +void MainWindow::createAboutDialog() { aboutDialog = new QMessageBox(QMessageBox::NoIcon, "About WeightGraph", - "Copyright (C) 2011 Visa Putkinen. Licence: GPLv2", + "Copyright (C) 2011-2012 Visa Putkinen. Licence: GPLv2", QMessageBox::Close, this); aboutDialog->setIconPixmap(QPixmap(":/img/icon48")); - aboutDialog->setInformativeText("Usage: enter your weight every day using " + aboutDialog->setInformativeText("Usage: enter your weight daily using " "the main screen's \"Today's weight?\" box " "or the List / edit window. You may enter " "at most one weight per day." @@ -101,19 +102,16 @@ MainWindow::MainWindow(QWidget *parent) : "The weights are stored in human readable (and " "writeable) form in " "MyDocs/WeightGraph/weightdata.txt"); +} - - //Important: SettingsWindow must be created after all graph - //views are created or settings won't show all graphs +void MainWindow::createMenuBar() { settingsWindow = new SettingsWindow(this); menuBar()->addAction(tr("Settings"), settingsWindow, SLOT(show())); - menuBar()->addAction(tr("About"), aboutDialog, SLOT(exec())); - - grabZoomKeys(Settings::grabZoomKeys()); - - connect(Settings::self(), SIGNAL(settingChanged()), this, SLOT(update())); + menuBar()->addAction(tr("List / edit"), ew, SLOT(show())); + menuBar()->addAction(tr("About / help"), aboutDialog, SLOT(exec())); } + void MainWindow::setTodaysWeight() { wdm->setWeightForDate(QDate::currentDate(), weight->value()); @@ -121,7 +119,6 @@ void MainWindow::setTodaysWeight() void MainWindow::keyPressEvent(QKeyEvent* event) { - //qDebug() << "Main window: key pressed: " << event->key(); switch (event->key()) { case Qt::Key_F7: smallGraph->decPeriod(); @@ -145,20 +142,10 @@ void MainWindow::grabZoomKeys(bool grab) unsigned long val = (grab) ? 1 : 0; Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False); if (!atom) { - qWarning("Unable to obtain _HILDON_ZOOM_KEY_ATOM. This example will only work " - "on a Maemo 5 device!"); + qWarning("Unable to obtain _HILDON_ZOOM_KEY_ATOM"); return; } - - XChangeProperty (QX11Info::display(), - winId(), - atom, - XA_INTEGER, - 32, - PropModeReplace, - reinterpret_cast(&val), - 1); - - //qDebug() << "Main window grabbed zoom keys: " << winId(); + XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER, 32, + PropModeReplace, reinterpret_cast(&val), 1); } diff --git a/weightgraph/mainwindow.h b/weightgraph/mainwindow.h index ecb8c10..c7cc35a 100644 --- a/weightgraph/mainwindow.h +++ b/weightgraph/mainwindow.h @@ -36,6 +36,12 @@ private: WeightGraphView *bigGraph; SettingsWindow *settingsWindow; QMessageBox *aboutDialog; + + QWidget *createLeftColumn(QWidget *central); + QWidget *createRightColumn(QWidget *central); + void createBigGraph(); + void createAboutDialog(); + void createMenuBar(); }; #endif // MAINWINDOW_H diff --git a/weightgraph/mainwindow.ui b/weightgraph/mainwindow.ui deleted file mode 100644 index 2c067ca..0000000 --- a/weightgraph/mainwindow.ui +++ /dev/null @@ -1,144 +0,0 @@ - - - MainWindow - - - - 0 - 0 - 800 - 480 - - - - MainWindow - - - - - - 10 - 10 - 741 - 461 - - - - - - - - - - - - 0 - 0 - - - - Today: - - - - - - - - 0 - 0 - - - - QAbstractSpinBox::UpDownArrows - - - kg - - - 1 - - - 30.000000000000000 - - - 1000.000000000000000 - - - 0.100000000000000 - - - 70.000000000000000 - - - - - - - Kg - - - - - - - - - TextLabel1 - - - - - - - TextLabel2 - - - - - - - Qt::Vertical - - - - 20 - 40 - - - - - - - - - - true - - - true - - - QFrame::Box - - - - - - :/img/graph.jpg - - - true - - - - - - - - - - - - - diff --git a/weightgraph/weightstatsview.cpp b/weightgraph/weightstatsview.cpp index 0ab0362..cb3505f 100644 --- a/weightgraph/weightstatsview.cpp +++ b/weightgraph/weightstatsview.cpp @@ -1,7 +1,8 @@ -#include "weightstatsview.h" #include "settings.h" -#include +#include "weightstatsview.h" +#include #include +#include #include @@ -17,65 +18,97 @@ WeightStatsView::WeightStatsView(WeightDataModel *wdm, QWidget *parent) : connect(Settings::self(), SIGNAL(settingChanged()), this, SLOT(updateStats())); - QVBoxLayout *lo = new QVBoxLayout(this); + QWidget *container = new QWidget(this); + container->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); + QVBoxLayout *lo = new QVBoxLayout(container); + + QFont smallerFont = QApplication::font(); + smallerFont.setPointSize(smallerFont.pointSize() - 4); + + QWidget *lastContainer = new QWidget(container); + QHBoxLayout *lastLayout = new QHBoxLayout(lastContainer); + lastLayout->setMargin(0); + last = new QLabel(lastContainer); + lastLayout->addWidget(last, 0, Qt::AlignVCenter); + lastNote = new QLabel(lastContainer); + lastNote->setFont(smallerFont); + lastLayout->addWidget(lastNote, 0, Qt::AlignVCenter); + lo->addWidget(lastContainer); + + QWidget *changeContainer = new QWidget(container); + QHBoxLayout *changeLayout = new QHBoxLayout(changeContainer); + changeLayout->setMargin(0); + change = new QLabel(changeContainer); + changeLayout->addWidget(change, 0, Qt::AlignVCenter); + changeNote = new QLabel(changeContainer); + changeNote->setFont(smallerFont); + changeLayout->addWidget(changeNote, 0, Qt::AlignVCenter); + lo->addWidget(changeContainer); - last = new QLabel(this); - lo->addWidget(last); + min = new QLabel(container); + lo->addWidget(min); - change = new QLabel(this); - lo->addWidget(change); + max = new QLabel(container); + lo->addWidget(max); updateStats(); } -static const char* wdays[] = {"Monday", "Tuesday", "Wednesday", "Thursday", - "Friday", "Saturday", "Sunday"}; -QString dateString(QDate date) -{ - int days = date.daysTo(QDate::currentDate()); - if (days < 0) - return date.toString(Qt::ISODate); - else if (days == 0) - return "Today"; - else if (days == 1) - return "Yesterday"; - else if (days < 7) - // return date.toString("dddd"); (Use this with l10n!) - // The following is used because the system locale might not be English - return wdays[date.dayOfWeek()-1]; - else - return date.toString(Qt::ISODate); -} +namespace { + static const char* wdays[] = {"Monday", "Tuesday", "Wednesday", "Thursday", + "Friday", "Saturday", "Sunday"}; + QString dateString(QDate date) + { + int days = date.daysTo(QDate::currentDate()); + if (days < 0) + return date.toString(Qt::ISODate); + else if (days == 0) + return "Today"; + else if (days == 1) + return "Yesterday"; + else if (days < 7) + return wdays[date.dayOfWeek()-1]; // Because locale might not be English + else + return date.toString(Qt::ISODate); + } -QString dateIntervalString(int days) -{ - if (days < 30) - return QString("%1 days").arg(days); - else if (days < 360) - return QString("%1m %2d").arg(days/30).arg(days%30); - else if (days < 365) - return QString("1y 0m"); - else - return QString("%1y %2m").arg(days/365).arg((days%365)/30); + QString dateIntervalString(int days) + { + if (days < 30) + return QString("%1 days").arg(days); + else if (days < 360) + return QString("%1m %2d").arg(days/30).arg(days%30); + else if (days < 365) + return QString("1y 0m"); + else + return QString("%1y %2m").arg(days/365).arg((days%365)/30); + } } void WeightStatsView::updateStats() { if (wdm->size() == 0) { last->setText("Last: No data"); + lastNote->setText(""); change->setText("Change: No data"); + changeNote->setText(""); + min->setText("Min: No data"); + max->setText("Max: No data"); return; } QString unit = Settings::weightUnit(); const DW &f = wdm->getWeights().first(); const DW &l = wdm->getWeights().last(); - last->setText(tr("Last: %1 %2\n (%3)") + last->setText(tr("Last: %1 %2") .arg(l.weight,0,'f',1) - .arg(unit) - .arg(dateString(l.date))); - change->setText(tr("Change: %1 %2\n" - " (in %3)") + .arg(unit)); + lastNote->setText(tr("(%1)").arg(dateString(l.date))); + + change->setText(tr("Change: %1 %2") .arg(l.weight-f.weight,0,'f',1) - .arg(unit) - .arg(dateIntervalString(f.date.daysTo(l.date)))); + .arg(unit)); + changeNote->setText(tr("(%1)").arg(dateIntervalString(f.date.daysTo(l.date)))); + + min->setText(tr("Min: %1 %2").arg(wdm->minWeight()).arg(unit)); + max->setText(tr("Max: %1 %2").arg(wdm->maxWeight()).arg(unit)); } diff --git a/weightgraph/weightstatsview.h b/weightgraph/weightstatsview.h index ac4c834..68913b3 100644 --- a/weightgraph/weightstatsview.h +++ b/weightgraph/weightstatsview.h @@ -18,8 +18,9 @@ public slots: void updateStats(); private: WeightDataModel *wdm; - QLabel *last; - QLabel *change; + QLabel *last, *lastNote; + QLabel *change, *changeNote; + QLabel *min, *max; }; #endif // WEIGHTSTATSVIEW_H