1 #include "editwindow.h"
7 #include <QMaemo5DatePickSelector>
9 extern WeightDataModel *wdm;
11 EditWindow::EditWindow(QWidget *parent) :
12 QMainWindow(parent), shown(false)
15 setAttribute(Qt::WA_Maemo5StackedWindow);
18 setWindowTitle("WeightGraph");
20 QWidget *centralContainer = new QWidget(parent);
21 QHBoxLayout *horiz = new QHBoxLayout(centralContainer);
23 weightView.setModel(wdm);
24 weightView.setMinimumWidth(540);
25 horiz->addWidget(&weightView);
27 QWidget *rightContainer = new QWidget(centralContainer);
28 QVBoxLayout *right = new QVBoxLayout(rightContainer);
30 addButton.setText("Add");
31 addButton.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
32 connect(&addButton, SIGNAL(clicked()), this, SLOT(addWeight()));
33 right->addWidget(&addButton);
35 removeButton.setText("Remove");
36 removeButton.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
37 connect(&removeButton, SIGNAL(clicked()), this, SLOT(removeSelected()));
38 right->addWidget(&removeButton);
40 editButton.setText("Edit");
41 editButton.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
42 connect(&editButton, SIGNAL(clicked()), this, SLOT(editSelected()));
43 right->addWidget(&editButton);
45 horiz->addWidget(rightContainer);
47 setCentralWidget(centralContainer);
49 connect(weightView.selectionModel(),
50 SIGNAL(selectionChanged(const QItemSelection &,const QItemSelection &)),
51 this, SLOT(updateButtons()));
55 AddWeightDialog::AddWeightDialog(QWidget *parent)
59 QGridLayout *layout = new QGridLayout(this);
61 QLabel *dateLabel = new QLabel("Date:", this);
62 //dateLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Maximum);
63 layout->addWidget(dateLabel, 0, 0);
66 date = new QMaemo5ValueButton(this);
67 QMaemo5DatePickSelector *picker = new QMaemo5DatePickSelector(this);
68 picker->setCurrentDate(QDate::currentDate());
69 date->setPickSelector(picker);
71 date = new QDateEdit(this);
72 date->setDate(QDate::currentDate());
73 date->setDisplayFormat("yyyy-MM-dd");
75 layout->addWidget(date, 1, 0);
77 QLabel *weightLabel = new QLabel("Weight:", this);
78 //weightLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Maximum);
79 layout->addWidget(weightLabel, 0, 1);
81 weight = new WeightSpinBox(this);
82 layout->addWidget(weight, 1, 1);
84 QPushButton *cancelButton = new QPushButton("Cancel", this);
85 connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
86 layout->addWidget(cancelButton, 2, 0);
88 QPushButton *addButton = new QPushButton("Add", this);
89 connect(addButton, SIGNAL(clicked()), this, SLOT(accept()));
90 layout->addWidget(addButton, 2, 1);
92 WeightDataModel::DateWeight AddWeightDialog::getDateWeight()
94 WeightDataModel::DateWeight ret;
96 ret.date = dynamic_cast<QMaemo5DatePickSelector*>(date->pickSelector())->currentDate();
98 ret.date = date->date();
100 ret.weight = weight->value();
104 void EditWindow::updateButtons()
106 if (weightView.selectionModel()->selection().indexes().size() == 1) {
107 removeButton.setEnabled(true);
108 editButton.setEnabled(true);
111 removeButton.setEnabled(false);
112 editButton.setEnabled(false);
116 extern WeightDataModel *wdm;
117 void EditWindow::addWeight()
121 if (code == QDialog::Accepted) {
122 WeightDataModel::DateWeight dw = d.getDateWeight();
123 if (wdm->dateExists(dw.date)) {
124 QMessageBox::warning(this, "Duplicate", "A weight for this date already exists.\n"
125 "Only one weight per date allowed.");
128 wdm->setWeightForDate(dw);
129 weightView.scrollTo(wdm->indexOfDate(dw.date), QAbstractItemView::PositionAtCenter);
133 void EditWindow::removeSelected()
136 QMessageBox::question(this, "Remove?", "Really remove selected entry?",
137 QMessageBox::No, QMessageBox::Yes);
138 if (answer == QMessageBox::Yes) {
139 QModelIndexList indexes = weightView.selectionModel()->selection().indexes();
140 foreach(QModelIndex i, indexes)
141 wdm->removeRows(i.row(), 1);
145 void EditWindow::editSelected()
147 QModelIndexList indexes = weightView.selectionModel()->selection().indexes();
148 if (indexes.size() == 1)
149 weightView.edit(wdm->index(indexes.first().row(), 1));
152 void EditWindow::show()
155 // scrollToBottom must be here: it will not scroll all the way
156 // to the bottom when called in the constructor because it'll
157 // receive the wrong window geometry while hidden.
159 weightView.scrollToBottom();