Release 1.0.4
[weightgraph] / weightgraph / mainwindow.cpp
1 //#include "ui_mainwindow.h"
2 #include "mainwindow.h"
3 #include "weightdata.h"
4 #include "weightstatsview.h"
5 #include "weightgraphview.h"
6 #include <QtGui/QX11Info>
7 #include <X11/Xlib.h>
8 #include <X11/Xatom.h>
9
10 extern WeightDataModel *wdm;
11
12 MainWindow::MainWindow(QWidget *parent) :
13     QMainWindow(parent), ew(new EditWindow(this))
14 {
15 #ifdef Q_WS_MAEMO_5
16   setAttribute(Qt::WA_Maemo5StackedWindow);
17 #endif
18
19   setWindowTitle("WeightGraph");
20
21   QWidget *central = new QWidget(this);
22   QHBoxLayout *topLayout = new QHBoxLayout(central);
23
24   topLayout->addWidget(createLeftColumn(central));
25   topLayout->addWidget(createRightColumn(central));
26   createAboutDialog();
27   createMenuBar();
28
29   this->setCentralWidget(central);
30
31   grabZoomKeys(Settings::grabZoomKeys());
32   connect(Settings::self(), SIGNAL(settingChanged()), this, SLOT(update()));
33 }
34
35 QWidget *MainWindow::createLeftColumn(QWidget *central) {
36   QWidget *leftContainer = new QWidget(central);
37   QVBoxLayout *leftLayout = new QVBoxLayout(leftContainer);
38   leftLayout->setSpacing(1);
39   leftLayout->setMargin(1);
40
41   // vvv LEFT TOP BEGINS vvv
42   QFrame *leftTopContainer = new QFrame(leftContainer);
43   leftTopContainer->setFrameShadow(QFrame::Sunken);
44   leftTopContainer->setFrameStyle(QFrame::StyledPanel);
45   leftTopContainer->setLineWidth(2);
46   leftTopContainer->setMidLineWidth(2);
47   QGridLayout *leftTopLayout = new QGridLayout(leftTopContainer);
48
49   QLabel *todayLabel = new QLabel("Today's weight?", leftContainer);
50   leftTopLayout->addWidget(todayLabel, 0, 0);
51
52   weight = new WeightSpinBox(leftTopContainer);
53   if(wdm->size() > 0)
54     weight->setValue(wdm->getWeights().last().weight);
55   leftTopLayout->addWidget(weight, 1, 0);
56
57   QPushButton *setButton = new QPushButton("Set", leftTopContainer);
58   connect(setButton, SIGNAL(clicked()), this, SLOT(setTodaysWeight()));
59   leftTopLayout->addWidget(setButton, 2, 0);
60
61   leftLayout->addWidget(leftTopContainer);
62   // ^^^ LEFT TOP ENDS ^^^
63
64   WeightStatsView *stats = new WeightStatsView(wdm, this);
65   leftLayout->addWidget(stats);
66
67   return leftContainer;
68 }
69
70 QWidget *MainWindow::createRightColumn(QWidget *central) {
71   createBigGraph();
72   smallGraph = new WeightGraphView(wdm, "Small", central);
73   connect(smallGraph, SIGNAL(clicked()), bigGraph, SLOT(show()));
74   return smallGraph;
75 }
76
77 void MainWindow::createBigGraph() {
78   bigGraph = new WeightGraphView(wdm, "Big", this);
79 #ifdef Q_WS_MAEMO_5
80   bigGraph->setAttribute(Qt::WA_Maemo5StackedWindow);
81   bigGraph->grabZoomKeys(Settings::grabZoomKeys());
82 #endif
83   bigGraph->setWindowFlags(bigGraph->windowFlags() | Qt::Window);
84 }
85
86 void MainWindow::createAboutDialog() {
87   aboutDialog = new QMessageBox(QMessageBox::NoIcon, "About WeightGraph",
88                                 "Copyright (C) 2011-2012 Visa Putkinen. Licence: GPLv2",
89                                 QMessageBox::Close, this);
90   aboutDialog->setIconPixmap(QPixmap(":/img/icon48"));
91   aboutDialog->setInformativeText("Usage: enter your weight daily using "
92                                   "the main screen's \"Today's weight?\" box "
93                                   "or the List / edit window. You may enter "
94                                   "at most one weight per day."
95                                   "\n\n"
96                                   "A graph of the weights will be drawn when "
97                                   "two or more weights are entered. Tap the "
98                                   "graph to open a larger graph view. Use the "
99                                   "external zoom buttons to adjust the shown "
100                                   "time period."
101                                   "\n\n"
102                                   "The weights are stored in human readable (and "
103                                   "writeable) form in "
104                                   "MyDocs/WeightGraph/weightdata.txt");
105 }
106
107 void MainWindow::createMenuBar() {
108   settingsWindow = new SettingsWindow(this);
109   menuBar()->addAction(tr("Settings"), settingsWindow, SLOT(show()));
110   menuBar()->addAction(tr("List / edit"), ew, SLOT(show()));
111   menuBar()->addAction(tr("About / help"), aboutDialog, SLOT(exec()));
112 }
113
114
115 void MainWindow::setTodaysWeight()
116 {
117   wdm->setWeightForDate(QDate::currentDate(), weight->value());
118 }
119
120 void MainWindow::keyPressEvent(QKeyEvent* event)
121 {
122   switch (event->key()) {
123   case Qt::Key_F7:
124       smallGraph->decPeriod();
125       event->accept();
126       break;
127
128   case Qt::Key_F8:
129       smallGraph->incPeriod();
130       event->accept();
131       break;
132   }
133   QWidget::keyPressEvent(event);
134 }
135 void MainWindow::grabZoomKeys(bool grab)
136 {
137   WeightGraphView::grabZoomKeysForWindow(winId(), grab);
138 }