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>
10 extern WeightDataModel *wdm;
12 MainWindow::MainWindow(QWidget *parent) :
16 setAttribute(Qt::WA_Maemo5StackedWindow);
19 setWindowTitle("WeightGraph");
21 QWidget *central = new QWidget(this);
22 QHBoxLayout *topLayout = new QHBoxLayout(central);
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);
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);
39 QLabel *todayLabel = new QLabel("Today's weight?", leftContainer);
40 leftTopLayout->addWidget(todayLabel, 0, 0);
42 weight = new WeightSpinBox(leftTopContainer);
44 weight->setValue(wdm->getWeights().last().weight);
45 leftTopLayout->addWidget(weight, 1, 0);
47 QPushButton *setButton = new QPushButton("Set", leftTopContainer);
48 connect(setButton, SIGNAL(clicked()), this, SLOT(setTodaysWeight()));
49 leftTopLayout->addWidget(setButton, 2, 0);
51 leftLayout->addWidget(leftTopContainer);
52 // ^^^ LEFT TOP ENDS ^^^
54 // QWidget *vspacer0 = new QWidget(leftContainer);
55 // vspacer0->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding);
56 // leftLayout->addWidget(vspacer0);
58 WeightStatsView *stats = new WeightStatsView(wdm, this);
59 leftLayout->addWidget(stats);
61 // QWidget *vspacer = new QWidget(leftContainer);
62 // vspacer->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding);
63 // leftLayout->addWidget(vspacer);
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);
70 topLayout->addWidget(leftContainer);
71 // ^^ LEFT SIDE ENDS ^^
73 smallGraph = new WeightGraphView(wdm, "Small", central);
74 bigGraph = new WeightGraphView(wdm, "Big", this);
76 bigGraph->setAttribute(Qt::WA_Maemo5StackedWindow);
77 bigGraph->grabZoomKeys(Settings::grabZoomKeys());
79 bigGraph->setWindowFlags(bigGraph->windowFlags() | Qt::Window);
80 connect(smallGraph, SIGNAL(clicked()), bigGraph, SLOT(show()));
81 topLayout->addWidget(smallGraph);
83 this->setCentralWidget(central);
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."
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 "
101 "The weights are stored in human readable (and "
102 "writeable) form in "
103 "MyDocs/WeightGraph/weightdata.txt");
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()));
112 grabZoomKeys(Settings::grabZoomKeys());
114 connect(Settings::self(), SIGNAL(settingChanged()), this, SLOT(update()));
117 void MainWindow::setTodaysWeight()
119 wdm->setWeightForDate(QDate::currentDate(), weight->value());
122 void MainWindow::keyPressEvent(QKeyEvent* event)
124 //qDebug() << "Main window: key pressed: " << event->key();
125 switch (event->key()) {
127 smallGraph->decPeriod();
132 smallGraph->incPeriod();
136 QWidget::keyPressEvent(event);
138 void MainWindow::grabZoomKeys(bool grab)
141 qWarning("Can't grab keys unless we have a window id");
145 unsigned long val = (grab) ? 1 : 0;
146 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
148 qWarning("Unable to obtain _HILDON_ZOOM_KEY_ATOM. This example will only work "
149 "on a Maemo 5 device!");
154 XChangeProperty (QX11Info::display(),
160 reinterpret_cast<unsigned char *>(&val),
163 //qDebug() << "Main window grabbed zoom keys: " << winId();