File indexing completed on 2024-11-24 03:57:48

0001 /**
0002  * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
0003  *
0004  * This file is part of the KGantt library.
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #include "entrydialog.h"
0010 
0011 #include "ui_entrydialog.h"
0012 
0013 #include <KGanttConstraintModel>
0014 #include <KGanttGlobal>
0015 #include <QAbstractItemModel>
0016 #include <QPersistentModelIndex>
0017 
0018 Q_DECLARE_METATYPE( QPersistentModelIndex )
0019 
0020 EntryDialog::EntryDialog( const QAbstractItemModel* model, QWidget* parent, Qt::WindowFlags f )
0021     : QDialog( parent, f ),
0022       indexList( QList<QPersistentModelIndex>() ),
0023       ui( new Ui::EntryDialog )
0024 {
0025     ui->setupUi( this );
0026     this->model = model;
0027     init();
0028 }
0029 
0030 void EntryDialog::init()
0031 {
0032     ui->type->addItem( tr( "Event" ), KGantt::TypeEvent );
0033     ui->type->addItem( tr( "Task" ), KGantt::TypeTask );
0034     ui->type->addItem( tr( "Summary" ), KGantt::TypeSummary );
0035     ui->type->addItem( tr( "Multi" ), KGantt::TypeMulti );
0036     
0037     for (int row = 0; row < model->rowCount(); ++row )
0038         addDependItem( model, model->index( row, 0 ) );
0039     
0040     connect( ui->startDate, SIGNAL(dateTimeChanged(QDateTime)), this, SLOT(updateEndDate(QDateTime)) );
0041     connect( ui->readOnly, SIGNAL(toggled(bool)), this, SLOT(disableEditing(bool)) );
0042     connect( ui->type, SIGNAL(currentIndexChanged(int)), this, SLOT(typeChanged(int)) );
0043     
0044     ui->startDate->setDateTime( QDateTime::currentDateTime() );
0045     typeChanged( 0 );
0046 }
0047 
0048 void EntryDialog::initFrom( const QModelIndex & index, const KGantt::ConstraintModel* constraintModel )
0049 {
0050     int row = index.row();
0051     const QModelIndex parent = index.parent();
0052     
0053     ui->name->setText( model->data( model->index( row, 0, parent ) ).toString() );
0054     ui->legend->setText( model->data( model->index( row, 5, parent ) ).toString() );
0055     int idx = ui->type->findData( model->data( model->index( row, 1, parent ) ).toInt() );
0056     ui->type->setCurrentIndex( idx );
0057     ui->startDate->setDateTime( model->data( model->index( row, 2, parent ), KGantt::StartTimeRole ).toDateTime() );
0058     ui->endDate->setDateTime( model->data( model->index( row, 3, parent ), KGantt::EndTimeRole ).toDateTime() );
0059     ui->completion->setValue( model->data( model->index( row, 4, parent ) ).toInt() );
0060     ui->readOnly->setChecked( !(model->flags( model->index( row, 0, parent ) ) & Qt::ItemIsEditable) );
0061     
0062     QList<KGantt::Constraint> constraints = constraintModel->constraintsForIndex( model->index( row, 0, parent ) );
0063     if ( constraints.isEmpty() )
0064         return;
0065     
0066     QModelIndex constraintIndex;
0067     for ( int i = 0; i < constraints.size(); ++i ) {
0068         KGantt::Constraint constraint = constraints[i];
0069         if ( constraint.endIndex() == index ) {
0070             constraintIndex = constraint.startIndex();
0071             break;
0072         }
0073     }
0074     
0075     if ( !constraintIndex.isValid() )
0076         return;
0077     
0078     ui->depends->setCurrentIndex( indexList.indexOf( constraintIndex ) + 1 );
0079 }
0080 
0081 void EntryDialog::addDependItem( const QAbstractItemModel* model, const QModelIndex & index, int indent)
0082 {
0083     indexList << QPersistentModelIndex( index );
0084     QString str = QString( "%1%2" ).arg( QString().fill( ' ', indent ) ).arg( model->data( index ).toString() );
0085     ui->depends->addItem( str );
0086     
0087     for (int row = 0; row < model->rowCount( index ); ++row )
0088         addDependItem( model, model->index( row, 0, index ), indent + 2 );
0089 }
0090 
0091 QString EntryDialog::name() const
0092 {
0093     return ui->name->text();
0094 }
0095 
0096 int EntryDialog::type() const
0097 {
0098     return ui->type->itemData( ui->type->currentIndex() ).toInt();
0099 }
0100 
0101 QDateTime EntryDialog::startDate() const
0102 {
0103     return ui->startDate->dateTime();
0104 }
0105 
0106 QDateTime EntryDialog::endDate() const
0107 {
0108     return ui->endDate->dateTime();
0109 }
0110 
0111 int EntryDialog::completion() const
0112 {
0113     return ui->completion->value();
0114 }
0115 
0116 void EntryDialog::updateEndDate(const QDateTime & startDate)
0117 {
0118     ui->endDate->setMinimumDate( startDate.date() );
0119     ui->endDate->setMinimumTime( startDate.time() );
0120 }
0121 
0122 bool EntryDialog::readOnly() const
0123 {
0124     return ui->readOnly->isChecked();
0125 }
0126 
0127 QModelIndex EntryDialog::depends() const
0128 {
0129     if ( ui->depends->currentIndex() == 0 )
0130         return QModelIndex();
0131 
0132     QPersistentModelIndex index = indexList[ ui->depends->currentIndex() - 1 ];
0133     if ( index.isValid() )
0134         return index;
0135     
0136     return QModelIndex();
0137 }
0138 
0139 QString EntryDialog::legend() const
0140 {
0141     return ui->legend->text();
0142 }
0143 
0144 void EntryDialog::disableEditing(bool disable)
0145 {
0146     ui->name->setEnabled( !disable );
0147     ui->type->setEnabled( !disable );
0148     ui->completion->setEnabled( !disable );
0149     ui->startDate->setEnabled( !disable );
0150     ui->endDate->setEnabled( !disable );
0151     ui->depends->setEnabled( !disable );
0152 }
0153 
0154 void EntryDialog::typeChanged(int index)
0155 {
0156     if ( ! index ) {
0157         ui->label_EndDate->hide();
0158         ui->endDate->hide();
0159     } else {
0160         ui->label_EndDate->show();
0161         ui->endDate->show();
0162     }
0163 }