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 "entrydelegate.h"
0010 
0011 #include "entrydialog.h"
0012 
0013 #include <KGanttConstraintModel>
0014 #include <KGanttGlobal>
0015 #include <QEvent>
0016 #include <QModelIndex>
0017 #include <QStandardItemModel>
0018 #include <QPointer>
0019 
0020 EntryDelegate::EntryDelegate( KGantt::ConstraintModel* constraintModel, QObject* parent )
0021     : QItemDelegate( parent )
0022 {
0023     this->constraintModel = constraintModel;
0024 }
0025 
0026 bool EntryDelegate::editorEvent( QEvent* event, QAbstractItemModel *model, const QStyleOptionViewItem& option, const QModelIndex& index )
0027 {
0028     if ( event->type() != QEvent::MouseButtonDblClick )
0029         return false;
0030 
0031     if ( !index.isValid() )
0032         return QItemDelegate::editorEvent( event, model, option, index );
0033 
0034     QPointer<EntryDialog> dialog = new EntryDialog( model );
0035     dialog->initFrom( index, constraintModel );
0036     dialog->setWindowTitle( tr( "Edit Entry" ) );
0037     dialog->exec();  
0038     if ( !dialog )
0039         return false;
0040     
0041     int row = index.row();
0042     const QModelIndex parent = index.parent();
0043     model->setData( model->index( row, 0, parent ), dialog->name() );
0044     model->setData( model->index( row, 1, parent ), dialog->type() );
0045     if ( dialog->type() != KGantt::TypeSummary ) {
0046         model->setData( model->index( row, 2, parent ), dialog->startDate(), KGantt::StartTimeRole );
0047         model->setData( model->index( row, 3, parent ), dialog->endDate(), KGantt::EndTimeRole );
0048     }
0049     model->setData( model->index( row, 4, parent ), dialog->completion() );
0050     model->setData( model->index( row, 5, parent ), dialog->legend() );
0051     
0052     addConstraint( dialog->depends(), model->index( row, 0, parent ) );
0053     setReadOnly( model->index( row, 0, parent ), dialog->readOnly() );
0054 
0055     delete dialog;
0056     return true;
0057 }
0058 
0059 void EntryDelegate::setReadOnly(const QModelIndex & index, bool readOnly)
0060 {
0061     int row = index.row();
0062     QModelIndex parent = index.parent();
0063     QStandardItem* item;
0064     const QStandardItemModel* model = qobject_cast<const QStandardItemModel*>( index.model() );
0065     if ( !model )
0066         return;
0067     
0068     item = model->itemFromIndex( model->index( row, 0, parent ) );
0069     item->setFlags( readOnly ? item->flags() & ~Qt::ItemIsEditable : item->flags() | Qt::ItemIsEditable );
0070     
0071     item = model->itemFromIndex( model->index( row, 1, parent ) );
0072     item->setFlags( readOnly ? item->flags() & ~Qt::ItemIsEditable : item->flags() | Qt::ItemIsEditable );
0073     
0074     item = model->itemFromIndex( model->index( row, 2, parent ) );
0075     item->setFlags( readOnly ? item->flags() & ~Qt::ItemIsEditable : item->flags() | Qt::ItemIsEditable );
0076     
0077     item = model->itemFromIndex( model->index( row, 3, parent ) );
0078     item->setFlags( readOnly ? item->flags() & ~Qt::ItemIsEditable : item->flags() | Qt::ItemIsEditable );
0079     
0080     item = model->itemFromIndex( model->index( row, 4, parent ) );
0081     item->setFlags( readOnly ? item->flags() & ~Qt::ItemIsEditable : item->flags() | Qt::ItemIsEditable );
0082 }
0083 
0084 void EntryDelegate::addConstraint(const QModelIndex & index1, const QModelIndex & index2)
0085 {
0086     if ( !index1.isValid() || !index2.isValid() )
0087         return;
0088 
0089     KGantt::Constraint c( index1, index2 );
0090     constraintModel->addConstraint( c );
0091 }