File indexing completed on 2024-11-24 04:48:39

0001 /*
0002  * Copyright (c) 2019 Alexander Potashev <aspotashev@gmail.com>
0003  *
0004  * This program is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU General Public License as
0006  * published by the Free Software Foundation; either version 2 of
0007  * the License or (at your option) version 3 or any later version
0008  * accepted by the membership of KDE e.V. (or its successor approved
0009  * by the membership of KDE e.V.), which shall act as a proxy
0010  * defined in Section 14 of version 3 of the license.
0011  *
0012  * This program is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0015  * GNU General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU General Public License
0018  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0019  */
0020 
0021 #include "edittimedialog.h"
0022 
0023 #include <QDialogButtonBox>
0024 #include <QGridLayout>
0025 #include <QGroupBox>
0026 #include <QLabel>
0027 #include <QLineEdit>
0028 #include <QPlainTextEdit>
0029 #include <QPushButton>
0030 
0031 #include <KLocalizedString>
0032 #include <KPluralHandlingSpinBox>
0033 
0034 #include "ktimetrackerutility.h"
0035 
0036 EditTimeDialog::EditTimeDialog(QWidget *parent, const QString &name, const QString &description, int64_t minutes)
0037     : QDialog(parent)
0038     , m_initialMinutes(minutes)
0039     , m_changeMinutes(0)
0040     , m_editHistoryRequested(false)
0041 {
0042     setWindowTitle(i18nc("@title:window", "Edit Task Time"));
0043     setModal(true);
0044     setMinimumSize(500, 330);
0045 
0046     auto *mainLayout = new QVBoxLayout(this);
0047     setLayout(mainLayout);
0048 
0049     // Info group
0050     auto *infoGroup = new QGroupBox(i18nc("@title:group", "Task"), this);
0051     auto *infoLayout = new QGridLayout(infoGroup);
0052     infoGroup->setLayout(infoLayout);
0053 
0054     QPalette roPalette;
0055     roPalette.setColor(QPalette::Base, palette().color(QPalette::Window));
0056 
0057     auto *nameText = new QLineEdit(name, infoGroup);
0058     nameText->setReadOnly(true);
0059     nameText->setPalette(roPalette);
0060     infoLayout->addWidget(nameText, 0, 1);
0061     infoLayout->addWidget(new QLabel(i18n("Task Name:")), 0, 0);
0062 
0063     auto *descText = new QPlainTextEdit(description, infoGroup);
0064     descText->setReadOnly(true);
0065     descText->setPalette(roPalette);
0066     descText->setWordWrapMode(QTextOption::WordWrap);
0067     infoLayout->addWidget(descText, 1, 1);
0068     infoLayout->addWidget(new QLabel(i18n("Task Description:")), 1, 0);
0069 
0070     // Edit group
0071     auto *editGroup = new QGroupBox(i18nc("@title:group", "Time Editing"), this);
0072     auto *editLayout = new QGridLayout(editGroup);
0073     editGroup->setLayout(editLayout);
0074 
0075     editLayout->setColumnStretch(0, 1);
0076     editLayout->setColumnStretch(4, 1);
0077 
0078     editLayout->addWidget(new QLabel(formatTime(minutes)), 0, 2); // TODO increment over time while dialog is open
0079     editLayout->addWidget(new QLabel(i18n("Current Time:")), 0, 1);
0080 
0081     m_timeEditor = new KPluralHandlingSpinBox(editGroup);
0082     m_timeEditor->setSuffix(ki18ncp("@item:valuesuffix Change Time By: ... minute(s)", " minute", " minutes"));
0083     m_timeEditor->setRange(-24 * 3600, 24 * 3600);
0084     m_timeEditor->setMinimumWidth(200);
0085     m_timeEditor->setFocus();
0086     connect(m_timeEditor, QOverload<int>::of(&QSpinBox::valueChanged), this, &EditTimeDialog::update);
0087     editLayout->addWidget(m_timeEditor, 1, 2);
0088     editLayout->addWidget(new QLabel(i18n("Change Time By:")), 1, 1);
0089 
0090     m_timePreview = new QLabel(editGroup);
0091     editLayout->addWidget(m_timePreview, 2, 2); // TODO increment over time while dialog is open
0092     editLayout->addWidget(new QLabel(i18n("Time After Change:")), 2, 1);
0093 
0094     m_buttonBox = new QDialogButtonBox(this);
0095     m_buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0096 
0097     auto *historyButton =
0098         new QPushButton(QIcon::fromTheme(QStringLiteral("document-edit")), i18nc("@action:button", "Edit History..."), m_buttonBox);
0099     historyButton->setToolTip(i18n("To change this task's time, you have to edit its event history"));
0100     m_buttonBox->addButton(historyButton, QDialogButtonBox::HelpRole);
0101 
0102     connect(m_buttonBox, &QDialogButtonBox::accepted, this, &EditTimeDialog::accept);
0103     connect(m_buttonBox, &QDialogButtonBox::helpRequested, [=]() {
0104         m_editHistoryRequested = true;
0105         accept();
0106     });
0107     connect(m_buttonBox, &QDialogButtonBox::rejected, this, &EditTimeDialog::reject);
0108 
0109     mainLayout->addWidget(infoGroup);
0110     mainLayout->addWidget(editGroup);
0111     mainLayout->addStretch(1);
0112     mainLayout->addWidget(m_buttonBox);
0113 
0114     update(0);
0115 }
0116 
0117 void EditTimeDialog::update(int newValue)
0118 {
0119     m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(newValue != 0);
0120     m_timePreview->setText(formatTime(m_initialMinutes + newValue));
0121     m_changeMinutes = newValue;
0122 }
0123 
0124 #include "moc_edittimedialog.cpp"