Completed home page.
[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)
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   // vv LEFT SIDE BEGINS vv
25   QWidget *leftContainer = new QWidget(central);
26   QVBoxLayout *leftLayout = new QVBoxLayout(leftContainer);
27   leftLayout->setSpacing(1);
28   leftLayout->setMargin(1);
29
30   // vvv LEFT TOP BEGINS vvv
31   //QGroupBox * leftTopContainer = new QGroupBox("Today's weight?", leftContainer);
32   QFrame *leftTopContainer = new QFrame(leftContainer);
33   leftTopContainer->setFrameShadow(QFrame::Sunken);
34   leftTopContainer->setFrameStyle(QFrame::StyledPanel);
35   leftTopContainer->setLineWidth(2);
36   leftTopContainer->setMidLineWidth(2);
37   QGridLayout *leftTopLayout = new QGridLayout(leftTopContainer);
38
39   QLabel *todayLabel = new QLabel("Today's weight?", leftContainer);
40   leftTopLayout->addWidget(todayLabel, 0, 0);
41
42   weight = new WeightSpinBox(leftTopContainer);
43   if(wdm->size() > 0)
44     weight->setValue(wdm->getWeights().last().weight);
45   leftTopLayout->addWidget(weight, 1, 0);
46
47   QPushButton *setButton = new QPushButton("Set", leftTopContainer);
48   connect(setButton, SIGNAL(clicked()), this, SLOT(setTodaysWeight()));
49   leftTopLayout->addWidget(setButton, 2, 0);
50
51   leftLayout->addWidget(leftTopContainer);
52   // ^^^ LEFT TOP ENDS ^^^
53
54 //  QWidget *vspacer0 = new QWidget(leftContainer);
55 //  vspacer0->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding);
56 //  leftLayout->addWidget(vspacer0);
57
58   WeightStatsView *stats = new WeightStatsView(wdm, this);
59   leftLayout->addWidget(stats);
60
61 //  QWidget *vspacer = new QWidget(leftContainer);
62 //  vspacer->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding);
63 //  leftLayout->addWidget(vspacer);
64
65   ew = new EditWindow(this);
66   QPushButton *listButton = new QPushButton("List / edit", leftContainer);
67   connect(listButton, SIGNAL(clicked()), ew, SLOT(show()));
68   leftLayout->addWidget(listButton);
69
70   topLayout->addWidget(leftContainer);
71   // ^^ LEFT SIDE ENDS ^^
72
73   smallGraph = new WeightGraphView(wdm, "Small", central);
74   bigGraph = new WeightGraphView(wdm, "Big", this);
75 #ifdef Q_WS_MAEMO_5
76   bigGraph->setAttribute(Qt::WA_Maemo5StackedWindow);
77   bigGraph->grabZoomKeys(Settings::grabZoomKeys());
78 #endif
79   bigGraph->setWindowFlags(bigGraph->windowFlags() | Qt::Window);
80   connect(smallGraph, SIGNAL(clicked()), bigGraph, SLOT(show()));
81   topLayout->addWidget(smallGraph);
82
83   this->setCentralWidget(central);
84
85
86   aboutDialog = new QMessageBox(QMessageBox::NoIcon, "About WeightGraph",
87                                 "Copyright (C) 2011 Visa Putkinen. Licence: GPLv2",
88                                 QMessageBox::Close, this);
89   aboutDialog->setIconPixmap(QPixmap(":/img/icon48"));
90   aboutDialog->setInformativeText("Usage: enter your weight every day using "
91                                   "the main screen's \"Today's weight?\" box "
92                                   "or the List / edit window. You may enter "
93                                   "at most one weight per day."
94                                   "\n\n"
95                                   "A graph of the weights will be drawn when "
96                                   "two or more weights are entered. Tap the "
97                                   "graph to open a larger graph view. Use the "
98                                   "external zoom buttons to adjust the shown "
99                                   "time period."
100                                   "\n\n"
101                                   "The weights are stored in human readable (and "
102                                   "writeable) form in "
103                                   "MyDocs/WeightGraph/weightdata.txt");
104
105
106   //Important: SettingsWindow must be created after all graph
107   //views are created or settings won't show all graphs
108   settingsWindow = new SettingsWindow(this);
109   menuBar()->addAction(tr("Settings"), settingsWindow, SLOT(show()));
110   menuBar()->addAction(tr("About"), aboutDialog, SLOT(exec()));
111
112   grabZoomKeys(Settings::grabZoomKeys());
113
114   connect(Settings::self(), SIGNAL(settingChanged()), this, SLOT(update()));
115 }
116
117 void MainWindow::setTodaysWeight()
118 {
119   wdm->setWeightForDate(QDate::currentDate(), weight->value());
120 }
121
122 void MainWindow::keyPressEvent(QKeyEvent* event)
123 {
124   //qDebug() << "Main window: key pressed: " << event->key();
125   switch (event->key()) {
126   case Qt::Key_F7:
127       smallGraph->decPeriod();
128       event->accept();
129       break;
130
131   case Qt::Key_F8:
132       smallGraph->incPeriod();
133       event->accept();
134       break;
135   }
136   QWidget::keyPressEvent(event);
137 }
138 void MainWindow::grabZoomKeys(bool grab)
139 {
140   if (!winId()) {
141     qWarning("Can't grab keys unless we have a window id");
142     return;
143   }
144
145   unsigned long val = (grab) ? 1 : 0;
146   Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
147   if (!atom) {
148     qWarning("Unable to obtain _HILDON_ZOOM_KEY_ATOM. This example will only work "
149              "on a Maemo 5 device!");
150     return;
151   }
152
153
154   XChangeProperty (QX11Info::display(),
155                    winId(),
156                    atom,
157                    XA_INTEGER,
158                    32,
159                    PropModeReplace,
160                    reinterpret_cast<unsigned char *>(&val),
161                    1);
162
163   //qDebug() << "Main window grabbed zoom keys: " << winId();
164 }