Move "List/Edit" button to menu bar and add min/max weight stats
authorVisa Putkinen <visa.putkinen@iki.fi>
Sun, 16 Dec 2012 23:34:49 +0000 (01:34 +0200)
committerVisa Putkinen <visa.putkinen@iki.fi>
Sun, 16 Dec 2012 23:34:49 +0000 (01:34 +0200)
weightgraph/mainwindow.cpp
weightgraph/mainwindow.h
weightgraph/mainwindow.ui [deleted file]
weightgraph/weightstatsview.cpp
weightgraph/weightstatsview.h

index f4211e1..08f0b3a 100644 (file)
@@ -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<unsigned char *>(&val),
-                   1);
-
-  //qDebug() << "Main window grabbed zoom keys: " << winId();
+  XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER, 32,
+                  PropModeReplace, reinterpret_cast<unsigned char *>(&val), 1);
 }
index ecb8c10..c7cc35a 100644 (file)
@@ -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 (file)
index 2c067ca..0000000
+++ /dev/null
@@ -1,144 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<ui version="4.0">
- <class>MainWindow</class>
- <widget class="QMainWindow" name="MainWindow">
-  <property name="geometry">
-   <rect>
-    <x>0</x>
-    <y>0</y>
-    <width>800</width>
-    <height>480</height>
-   </rect>
-  </property>
-  <property name="windowTitle">
-   <string>MainWindow</string>
-  </property>
-  <widget class="QWidget" name="centralWidget">
-   <widget class="QWidget" name="horizontalLayoutWidget">
-    <property name="geometry">
-     <rect>
-      <x>10</x>
-      <y>10</y>
-      <width>741</width>
-      <height>461</height>
-     </rect>
-    </property>
-    <layout class="QHBoxLayout" name="horizontalLayout">
-     <item>
-      <layout class="QVBoxLayout" name="verticalLayout">
-       <item>
-        <layout class="QHBoxLayout" name="horizontalLayout_2">
-         <item>
-          <widget class="QLabel" name="todayLabel">
-           <property name="sizePolicy">
-            <sizepolicy hsizetype="Maximum" vsizetype="Preferred">
-             <horstretch>0</horstretch>
-             <verstretch>0</verstretch>
-            </sizepolicy>
-           </property>
-           <property name="text">
-            <string>Today:</string>
-           </property>
-          </widget>
-         </item>
-         <item>
-          <widget class="QDoubleSpinBox" name="todayWeight">
-           <property name="sizePolicy">
-            <sizepolicy hsizetype="Maximum" vsizetype="Maximum">
-             <horstretch>0</horstretch>
-             <verstretch>0</verstretch>
-            </sizepolicy>
-           </property>
-           <property name="buttonSymbols">
-            <enum>QAbstractSpinBox::UpDownArrows</enum>
-           </property>
-           <property name="specialValueText">
-            <string>kg</string>
-           </property>
-           <property name="decimals">
-            <number>1</number>
-           </property>
-           <property name="minimum">
-            <double>30.000000000000000</double>
-           </property>
-           <property name="maximum">
-            <double>1000.000000000000000</double>
-           </property>
-           <property name="singleStep">
-            <double>0.100000000000000</double>
-           </property>
-           <property name="value">
-            <double>70.000000000000000</double>
-           </property>
-          </widget>
-         </item>
-         <item>
-          <widget class="QLabel" name="kgLabel">
-           <property name="text">
-            <string>Kg</string>
-           </property>
-          </widget>
-         </item>
-        </layout>
-       </item>
-       <item>
-        <widget class="QLabel" name="label_2">
-         <property name="text">
-          <string>TextLabel1</string>
-         </property>
-        </widget>
-       </item>
-       <item>
-        <widget class="QLabel" name="label_3">
-         <property name="text">
-          <string>TextLabel2</string>
-         </property>
-        </widget>
-       </item>
-       <item>
-        <spacer name="verticalSpacer">
-         <property name="orientation">
-          <enum>Qt::Vertical</enum>
-         </property>
-         <property name="sizeHint" stdset="0">
-          <size>
-           <width>20</width>
-           <height>40</height>
-          </size>
-         </property>
-        </spacer>
-       </item>
-      </layout>
-     </item>
-     <item>
-      <widget class="QLabel" name="label">
-       <property name="enabled">
-        <bool>true</bool>
-       </property>
-       <property name="autoFillBackground">
-        <bool>true</bool>
-       </property>
-       <property name="frameShape">
-        <enum>QFrame::Box</enum>
-       </property>
-       <property name="text">
-        <string/>
-       </property>
-       <property name="pixmap">
-        <pixmap resource="res.qrc">:/img/graph.jpg</pixmap>
-       </property>
-       <property name="scaledContents">
-        <bool>true</bool>
-       </property>
-      </widget>
-     </item>
-    </layout>
-   </widget>
-  </widget>
- </widget>
- <layoutdefault spacing="6" margin="11"/>
- <resources>
-  <include location="res.qrc"/>
- </resources>
- <connections/>
-</ui>
index 0ab0362..cb3505f 100644 (file)
@@ -1,7 +1,8 @@
-#include "weightstatsview.h"
 #include "settings.h"
-#include <QVBoxLayout>
+#include "weightstatsview.h"
+#include <QApplication>
 #include <QGroupBox>
+#include <QVBoxLayout>
 
 #include <QDebug>
 
@@ -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));
 }
index ac4c834..68913b3 100644 (file)
@@ -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