File: MonthlyTimesheetConfigurationDialog.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 (187 lines) | stat: -rw-r--r-- 6,483 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
/*
  MonthlyTimesheetConfigurationDialog.cpp

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

  Copyright (C) 2014-2018 Klarälvdalens Datakonsult AB, a KDAB Group company, info@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 "MonthlyTimesheetConfigurationDialog.h"

#include <QSettings>

#include "DateEntrySyncer.h"
#include "SelectTaskDialog.h"
#include "ViewHelpers.h"

#include "CharmCMake.h"

#include "MonthlyTimesheet.h"

#include "ui_MonthlyTimesheetConfigurationDialog.h"

MonthlyTimesheetConfigurationDialog::MonthlyTimesheetConfigurationDialog(QWidget *parent)
    : ReportConfigurationDialog(parent)
    , m_ui(new Ui::MonthlyTimesheetConfigurationDialog)
{
    setWindowTitle(tr("Monthly Timesheet"));

    m_ui->setupUi(this);
    connect(m_ui->buttonBox, &QDialogButtonBox::accepted,
            this, &MonthlyTimesheetConfigurationDialog::accept);
    connect(m_ui->buttonBox, &QDialogButtonBox::rejected,
            this, &MonthlyTimesheetConfigurationDialog::reject);

    connect(m_ui->comboBoxMonth, SIGNAL(currentIndexChanged(int)),
            SLOT(slotMonthComboItemSelected(int)));
    connect(m_ui->toolButtonSelectTask, &QToolButton::clicked,
            this, &MonthlyTimesheetConfigurationDialog::slotSelectTask);
    connect(m_ui->checkBoxSubTasksOnly, &QCheckBox::toggled,
            this, &MonthlyTimesheetConfigurationDialog::slotCheckboxSubtasksOnlyChecked);
    m_ui->comboBoxMonth->setCurrentIndex(1);
    slotCheckboxSubtasksOnlyChecked(m_ui->checkBoxSubTasksOnly->isChecked());

    slotStandardTimeSpansChanged();
    connect(ApplicationCore::instance().dateChangeWatcher(), &DateChangeWatcher::dateChanged,
            this, &MonthlyTimesheetConfigurationDialog::slotStandardTimeSpansChanged);

    // set current month and year:
    m_ui->spinBoxMonth->setValue(QDate::currentDate().month());
    m_ui->spinBoxYear->setValue(QDate::currentDate().year());

    // load settings:
    QSettings settings;
    if (settings.contains(MetaKey_TimesheetActiveOnly)) {
        m_ui->checkBoxActiveOnly->setChecked(settings.value(MetaKey_TimesheetActiveOnly).toBool());
    } else {
        m_ui->checkBoxActiveOnly->setChecked(true);
    }
}

MonthlyTimesheetConfigurationDialog::~MonthlyTimesheetConfigurationDialog()
{
}

void MonthlyTimesheetConfigurationDialog::setDefaultMonth(int yearOfMonth, int month)
{
    m_ui->spinBoxMonth->setValue(month);
    m_ui->spinBoxYear->setValue(yearOfMonth);
    m_ui->comboBoxMonth->setCurrentIndex(4);
}

void MonthlyTimesheetConfigurationDialog::accept()
{
    // save settings:
    QSettings settings;
    settings.setValue(MetaKey_TimesheetActiveOnly,
                      m_ui->checkBoxActiveOnly->isChecked());
    settings.setValue(MetaKey_TimesheetRootTask,
                      m_rootTask);

    QDialog::accept();
}

void MonthlyTimesheetConfigurationDialog::showReportPreviewDialog()
{
    QDate start, end;
    int index = m_ui->comboBoxMonth->currentIndex();
    if (index == m_monthInfo.size() -1) {
        // manual selection
        start = QDate(m_ui->spinBoxYear->value(), m_ui->spinBoxMonth->value(), 1);
        end = start.addMonths(1);
    } else {
        start = m_monthInfo[index].timespan.first;
        end = m_monthInfo[index].timespan.second;
    }
    bool activeOnly = m_ui->checkBoxActiveOnly->isChecked();
    auto report = new MonthlyTimeSheetReport();
    report->setReportProperties(start, end, m_rootTask, activeOnly);
    report->show();
}

void MonthlyTimesheetConfigurationDialog::showEvent(QShowEvent *)
{
    QSettings settings;

    // we only want to do this once a backend is loaded, and we ignore
    // the saved root task if it does not exist anymore
    if (settings.contains(MetaKey_TimesheetRootTask)) {
        TaskId root = settings.value(MetaKey_TimesheetRootTask).toInt();
        const TaskTreeItem &item = DATAMODEL->taskTreeItem(root);
        if (item.isValid()) {
            m_rootTask = root;
            m_ui->labelTaskName->setText(DATAMODEL->fullTaskName(item.task()));
            m_ui->checkBoxSubTasksOnly->setChecked(true);
        }
    }
}

void MonthlyTimesheetConfigurationDialog::slotCheckboxSubtasksOnlyChecked(bool checked)
{
    if (checked && m_rootTask == 0)
        slotSelectTask();

    if (!checked) {
        m_rootTask = 0;
        m_ui->labelTaskName->setText(tr("(All Tasks)"));
    }
}

void MonthlyTimesheetConfigurationDialog::slotStandardTimeSpansChanged()
{
    const TimeSpans timeSpans;
    m_monthInfo = timeSpans.last4Months();
    NamedTimeSpan custom = {
        tr("Manual Selection"),
        timeSpans.thisMonth().timespan,
        Range
    };
    m_monthInfo << custom;
    m_ui->comboBoxMonth->clear();
    for (int i = 0; i < m_monthInfo.size(); ++i)
        m_ui->comboBoxMonth->addItem(m_monthInfo[i].name);
    // Set current index to "Last Month" as that's what you'll usually want
    m_ui->comboBoxMonth->setCurrentIndex(1);
}

void MonthlyTimesheetConfigurationDialog::slotMonthComboItemSelected(int index)
{
    // wait for the next update, in this case:
    if (m_ui->comboBoxMonth->count() == 0 || index == -1) return;
    Q_ASSERT(m_ui->comboBoxMonth->count() > index);

    if (index == m_monthInfo.size() - 1) {   // manual selection
        m_ui->groupBox->setEnabled(true);
    } else {
        m_ui->groupBox->setEnabled(false);
    }
}

void MonthlyTimesheetConfigurationDialog::slotSelectTask()
{
    SelectTaskDialog dialog(this);
    dialog.setNonTrackableSelectable();
    if (dialog.exec()) {
        m_rootTask = dialog.selectedTask();
        const TaskTreeItem &item = DATAMODEL->taskTreeItem(m_rootTask);
        m_ui->labelTaskName->setText(DATAMODEL->fullTaskName(item.task()));
    } else {
        if (m_rootTask == 0)
            m_ui->checkBoxSubTasksOnly->setChecked(false);
    }
}