File: TaskEditor.cpp

package info (click to toggle)
charmtimetracker 1.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,340 kB
  • sloc: cpp: 19,176; xml: 284; python: 120; makefile: 14
file content (195 lines) | stat: -rw-r--r-- 6,831 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
  TaskEditor.cpp

  This file is part of Charm, a task-based time tracking application.

  Copyright (C) 2008-2018 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com

  Author: Mirko Boehm <mirko.boehm@kdab.com>
  Author: Frank Osterfeld <frank.osterfeld@kdab.com>

  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, either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "TaskEditor.h"
#include "SelectTaskDialog.h"
#include "ViewHelpers.h"

#include "Core/CharmConstants.h"
#include "Core/CharmDataModel.h"
#include "Core/TaskTreeItem.h"

#include <QCalendarWidget>
#include <QCheckBox>
#include <QPushButton>
#include <QMessageBox>

#include "ui_TaskEditor.h"

TaskEditor::TaskEditor(QWidget *parent)
    : QDialog(parent)
    , m_ui(new Ui::TaskEditor)
{
    m_ui->setupUi(this);
    m_ui->dateEditFrom->calendarWidget()->setFirstDayOfWeek(Qt::Monday);
    m_ui->dateEditFrom->calendarWidget()->setVerticalHeaderFormat(QCalendarWidget::ISOWeekNumbers);
    m_ui->dateEditTo->calendarWidget()->setFirstDayOfWeek(Qt::Monday);
    m_ui->dateEditTo->calendarWidget()->setVerticalHeaderFormat(QCalendarWidget::ISOWeekNumbers);
    connect(m_ui->pushButtonParent, &QPushButton::clicked, this, &TaskEditor::slotSelectParent);
    connect(m_ui->dateEditFrom, &QDateEdit::dateChanged, this, &TaskEditor::slotDateChanged);
    connect(m_ui->dateEditTo, &QDateEdit::dateChanged, this, &TaskEditor::slotDateChanged);
    connect(m_ui->checkBoxFrom, &QCheckBox::clicked, this, &TaskEditor::slotCheckBoxChecked);
    connect(m_ui->checkBoxUntil, &QCheckBox::clicked, this, &TaskEditor::slotCheckBoxChecked);
}

TaskEditor::~TaskEditor()
{
}

void TaskEditor::setTask(const Task &task)
{
    Q_ASSERT(m_ui);
    m_task = task;
    const TaskTreeItem &taskTreeItem
        = MODEL.charmDataModel()->taskTreeItem(task.id());
    m_ui->labelTaskName->setText(MODEL.charmDataModel()->fullTaskName(taskTreeItem.task()));
    m_ui->lineEditName->setText(task.name());
    m_ui->checkBoxTrackable->setChecked(task.trackable());
    if (task.parent() != 0) {
        const TaskTreeItem &parentItem
            = MODEL.charmDataModel()->taskTreeItem(task.parent());
        const QString name = parentItem.task().name();
        m_ui->pushButtonParent->setText(name);
    } else {
        m_ui->pushButtonParent->setText(tr("Choose Parent Task"));
    }
    if (task.parent() == 0) {
        m_ui->checkBoxTopLevel->setChecked(true);
    } else {
        m_ui->checkBoxTopLevel->setChecked(false);
    }
    QDate start = task.validFrom().date();
    if (start.isValid()) {
        m_ui->dateEditFrom->setDate(start);
        m_ui->checkBoxFrom->setChecked(false);
    } else {
        m_ui->checkBoxFrom->setChecked(true);
        m_ui->dateEditFrom->setDate(QDate::currentDate());
    }
    QDate end = task.validUntil().date();
    if (end.isValid()) {
        m_ui->dateEditTo->setDate(end);
        m_ui->checkBoxUntil->setChecked(false);
    } else {
        m_ui->checkBoxUntil->setChecked(true);
        m_ui->dateEditTo->setDate(QDate::currentDate());
    }
    m_ui->lineEditComment->setText(task.comment());
    checkInvariants();
}

Task TaskEditor::getTask() const
{
    Task newTask = m_task;
    newTask.setName(m_ui->lineEditName->text());
    newTask.setTrackable(m_ui->checkBoxTrackable->isChecked());
    if (m_ui->checkBoxTopLevel->isChecked())
        newTask.setParent(0);
    if (m_ui->checkBoxFrom->isChecked()) {
        newTask.setValidFrom(QDateTime());
    } else {
        newTask.setValidFrom(m_ui->dateEditFrom->dateTime());
    }
    if (m_ui->checkBoxUntil->isChecked()) {
        newTask.setValidUntil(QDateTime());
    } else {
        newTask.setValidUntil(m_ui->dateEditTo->dateTime());
    }
    newTask.setComment(m_ui->lineEditComment->text());
    return newTask;
}

bool parentTreeIsTree(TaskId newParent, TaskId task)
{
    QList<TaskId> parents;
    TaskId parent = newParent;
    while (parent != 0) {
        parents << parent;
        const TaskTreeItem &parentItem = MODEL.charmDataModel()->taskTreeItem(parent);
        parent = parentItem.task().parent();
    }
    return !parents.contains(task);
}

void TaskEditor::slotSelectParent()
{
    SelectTaskDialog dialog(this);
    dialog.setNonTrackableSelectable();
    Q_FOREVER {
        if (dialog.exec()) {
            TaskId newParentId = dialog.selectedTask();
            const TaskTreeItem &parentItem
                = MODEL.charmDataModel()->taskTreeItem(newParentId);
            QString name = m_task.name();
            QString parent = parentItem.task().name();
            if (parentTreeIsTree(newParentId, m_task.id())) {
                m_task.setParent(dialog.selectedTask());
                if (newParentId != 0) {
                    m_ui->pushButtonParent->setText(parent);
                } else {
                    m_ui->pushButtonParent->setText(tr("Choose Parent Task"));
                }

                break;
            }
            QMessageBox::information(this, tr("Please choose another task"),
                                     tr(
                                         "The task \"%1\" cannot be selected as the parent task for \"%2\","
                                         " because they are the same, or \"%3\" is a direct or indirect subtask of \"%4\".")
                                     .arg(parent, name, parent, name));
        } else {
            break;
        }
    }
}

void TaskEditor::slotDateChanged(const QDate &)
{
    checkInvariants();
}

void TaskEditor::slotCheckBoxChecked(bool)
{
    checkInvariants();
}

void TaskEditor::checkInvariants()
{
    bool acceptable;
    // the start and end dates are acceptable if
    // * both start and end date are deactivated,
    // * if only one date is set, or
    // * if none are set, and the start date is before the end date
    if (m_ui->checkBoxFrom->isChecked() && m_ui->checkBoxUntil->isChecked()) {
        acceptable = true;
    } else if (m_ui->checkBoxFrom->isChecked() || m_ui->checkBoxUntil->isChecked()) {
        acceptable = true;
    } else if (m_ui->dateEditFrom->dateTime() < m_ui->dateEditTo->dateTime()) {
        acceptable = true;
    } else {
        acceptable = false;
    }

    m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(acceptable);
}