GPLv2 license
[scorecard] / src / score-dialog.cpp
index 64eb517..443bfd2 100644 (file)
@@ -1,3 +1,11 @@
+/*
+ * Copyright (C) 2009 Sakari Poussa
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 2.
+ */
+
 #include <QtGui>
 #include <QInputContext>
 
@@ -13,19 +21,19 @@ SelectDialog::SelectDialog(QWidget *parent) : QDialog(parent)
 
 void SelectDialog::createLayout(QWidget *parent)
 {
-  labelClub = new QLabel(tr("Club"));
-  labelCourse = new QLabel(tr("Course"));
   listClub = new QListWidget(parent);
   lineEditDate = new QLineEdit;
   pushButtonNext = new QPushButton(tr("Next"));
 
   QDate today(QDate::currentDate());
   lineEditDate->setText(today.toString("yyyy-MM-dd"));
+  date = new QDateEdit;
 
   connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
 
   leftLayout = new QVBoxLayout;
   leftLayout->addWidget(listClub);
+  //leftLayout->addWidget(date);
   leftLayout->addWidget(lineEditDate);
 
   rightLayout = new QVBoxLayout;
@@ -133,7 +141,6 @@ void ScoreDialog::createLayout(QWidget *parent)
   QHBoxLayout *mainLayout = new QHBoxLayout(parent);
   mainLayout->addLayout(leftLayout);
   mainLayout->addLayout(rightLayout);
-
   setLayout(mainLayout);
 }
 
@@ -148,16 +155,10 @@ void ScoreDialog::createTable(QWidget *parent)
   QStringList headers;
   headers << "" << "Par" << "HCP" << "Score" << "" << "Par" << "HCP" << "Score";
   table->setVerticalHeaderLabels(headers);
-
-  //connect(table, SIGNAL(itemChanged(QTableWidgetItem *)), this, SLOT(itemChanged(QTableWidgetItem *)));
-  //connect(table, SIGNAL(itemPressed(QTableWidgetItem *)), this, SLOT(itemChanged(QTableWidgetItem *)));
 }
 
 void ScoreDialog::createButton(QWidget *parent)
 {
-  pushButtonFinish = new QPushButton(tr("Finish"));
-  connect(pushButtonFinish, SIGNAL(clicked()), this, SLOT(finish()));
-
   pushButtonUp = new QPushButton(tr("+"));
   connect(pushButtonUp, SIGNAL(clicked()), this, SLOT(up()));
 
@@ -166,45 +167,52 @@ void ScoreDialog::createButton(QWidget *parent)
 
   pushButtonNext = new QPushButton(tr("Next"));
   connect(pushButtonNext, SIGNAL(clicked()), this, SLOT(next()));
+
+  pushButtonFinish = new QPushButton(tr("Finish"));
+  connect(pushButtonFinish, SIGNAL(clicked()), this, SLOT(finish()));
 }
 
-void ScoreDialog::init(Course *course)
+void ScoreDialog::init(Course *course, Score *score)
 {
-  QTableWidgetItem *par, *hcp, *score;
+  QTableWidgetItem *par, *hcp, *scoreItem, *holeNum;
+  QColor fgColor(Qt::white);
+  QColor bgColor(Qt::darkGray);
 
   for (int i = 0; i < 18; i++) {
     par = new QTableWidgetItem(course->getPar(i));
     hcp = new QTableWidgetItem(course->getHcp(i));
-    score = new QTableWidgetItem("");
-    //score->setInputMask("9");
+    if (score)
+      scoreItem = new QTableWidgetItem(score->getScore(i));
+    else
+      scoreItem = new QTableWidgetItem("");
+    holeNum = new QTableWidgetItem(QString::number(i+1));
 
-    QTableWidgetItem *holeNum = new QTableWidgetItem(QString::number(i+1));
-    QColor bgColor(Qt::gray);
+    holeNum->setTextColor(fgColor);
     holeNum->setBackgroundColor(bgColor);
 
-    holeNum->setTextAlignment(Qt::AlignCenter);
-
     par->setTextAlignment(Qt::AlignCenter);
     hcp->setTextAlignment(Qt::AlignCenter);
-    score->setTextAlignment(Qt::AlignCenter);
+    scoreItem->setTextAlignment(Qt::AlignCenter);
+    holeNum->setTextAlignment(Qt::AlignCenter);
 
     if (i < 9) {
       table->setItem(ROW_HOLE, i, holeNum);
       table->setItem(ROW_PAR, i, par);
       table->setItem(ROW_HCP, i, hcp);
-      table->setItem(ROW_SCORE, i, score);
+      table->setItem(ROW_SCORE, i, scoreItem);
     }
     else {
       table->setItem(ROW_HOLE_2, i-9, holeNum);
       table->setItem(ROW_PAR_2, i-9, par);
       table->setItem(ROW_HCP_2, i-9, hcp);
-      table->setItem(ROW_SCORE_2, i-9, score);
+      table->setItem(ROW_SCORE_2, i-9, scoreItem);
     }
   }
 
   // Set focus to 1st cell
   table->setCurrentCell(ROW_SCORE, 0);
-  setDefaultScore(table);
+  if (!score)
+    setDefaultScore(table);
 }
 
 // Set default score to par if not set
@@ -230,28 +238,30 @@ void ScoreDialog::setDefaultScore(QTableWidget *table)
   }
 }
 
-void ScoreDialog::moveToNextCell(QTableWidgetItem *item)
+void ScoreDialog::up(void)
 {
-  if (!item)
+  QTableWidgetItem *item = table->currentItem();
+
+  if (!item) {
+    qWarning() << "ERROR: no current item";
     return;
+  }
 
-  QTableWidget *table = item->tableWidget();
+  int i = (item->text()).toInt();
+  QVariant value(i+1);
+  item->setData(Qt::DisplayRole, value);
+}
 
-  if (!table)
-    return;
+void ScoreDialog::down(void)
+{
+  QTableWidgetItem *item = table->currentItem();
 
-  int row = table->currentRow();
-  int col = table->currentColumn();
+  if (!item)
+    return;
 
-  if (col < (COLS-1)) {
-    col++;
-  }
-  else if (col == (COLS-1)) {
-    col = 0;
-    row = (row == ROW_SCORE_2) ? ROW_SCORE : ROW_SCORE_2;
-  }
-  //qDebug() << "new cell: " << row << "/" << col;
-  table->setCurrentCell(row, col);
+  int i = (item->text()).toInt();
+  QVariant value(i-1);
+  item->setData(Qt::DisplayRole, value);
 }
 
 void ScoreDialog::next(void)
@@ -263,32 +273,27 @@ void ScoreDialog::next(void)
   }
 }
 
-void ScoreDialog::up(void)
+void ScoreDialog::moveToNextCell(QTableWidgetItem *item)
 {
-  QTableWidgetItem *item = table->currentItem();
-
   if (!item)
     return;
 
-  int i = (item->text()).toInt();
-  QVariant value(i+1);
-  item->setData(Qt::DisplayRole, value);
-}
-
-void ScoreDialog::down(void)
-{
-  QTableWidgetItem *item = table->currentItem();
+  QTableWidget *table = item->tableWidget();
 
-  if (!item)
+  if (!table)
     return;
 
-  int i = (item->text()).toInt();
-  QVariant value(i-1);
-  item->setData(Qt::DisplayRole, value);
-}
+  int row = table->currentRow();
+  int col = table->currentColumn();
 
-void ScoreDialog::itemChanged(QTableWidgetItem *item)
-{
+  if (col < (COLS-1)) {
+    col++;
+  }
+  else if (col == (COLS-1)) {
+    col = 0;
+    row = (row == ROW_SCORE_2) ? ROW_SCORE : ROW_SCORE_2;
+  }
+  table->setCurrentCell(row, col);
 }
 
 void ScoreDialog::results(QVector<QString> &scores)