File indexing completed on 2024-12-15 04:02:51

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 <QApplication>
0010 #include <QDateTime>
0011 #include <QStandardItemModel>
0012 #include <QDebug>
0013 #include <QSlider>
0014 #include <QVBoxLayout>
0015 #include <QListView>
0016 
0017 #include <KGanttView>
0018 #include <KGanttConstraintModel>
0019 #include <KGanttDateTimeGrid>
0020 #include <KGanttListViewRowController>
0021 
0022 class MyStandardItem : public QStandardItem {
0023 public:
0024     MyStandardItem( const QVariant& v, int role = Qt::DisplayRole ) : QStandardItem()
0025     {
0026         setData( v, role );
0027     }
0028     MyStandardItem( const QString& v ) : QStandardItem()
0029     {
0030         setData( v, Qt::DisplayRole );
0031     }
0032 };
0033 
0034 class MyListView : public QListView {
0035 public:
0036   MyListView(QWidget* parent = nullptr) : QListView(parent)
0037   {
0038     // Make some space for the header
0039     setViewportMargins(0, fontMetrics().height()*2+4*frameWidth(), 0, 0);
0040     setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
0041     setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
0042   }
0043 };
0044 
0045 class MyWidget : public QWidget {
0046     Q_OBJECT
0047 public:
0048     MyWidget() : QWidget( nullptr )
0049     {
0050         qDebug() << "Building data";
0051         //proxyModel.setSourceModel( &model );
0052     for ( int i = 0; i < 10; ++i ) {
0053       QStandardItem* item = new MyStandardItem( QString::fromLatin1("Multi Item %1").arg(i) );
0054       for ( int j=0; j < 20; j+=3 ) {
0055         item->appendRow( QList<QStandardItem*>()
0056                  << new MyStandardItem( QString::fromLatin1( "Item %1" ).arg( j ) )
0057                  << new MyStandardItem( KGantt::TypeTask )
0058                  << new MyStandardItem( QDateTime::currentDateTime().addDays( j ), KGantt::StartTimeRole )
0059                  << new MyStandardItem( QDateTime::currentDateTime().addDays( j+1+i/7 ), KGantt::EndTimeRole )
0060                  << new MyStandardItem( 50 ) );
0061       }
0062       item->appendRow( QList<QStandardItem*>()
0063                << new MyStandardItem( QString::fromLatin1( "Event" ) )
0064                << new MyStandardItem( KGantt::TypeEvent )
0065                << new MyStandardItem( QDateTime::currentDateTime(), KGantt::StartTimeRole )
0066                << new MyStandardItem( QDateTime(), KGantt::EndTimeRole )
0067                << new MyStandardItem( QString() ) );
0068 
0069       model.appendRow( QList<QStandardItem*>()
0070                << item
0071                << new MyStandardItem( KGantt::TypeMulti )
0072                << new MyStandardItem( QString() )
0073                << new MyStandardItem( QString() )
0074                << new MyStandardItem( QString() ) );
0075     }
0076 
0077         //cmodel.addConstraint( KGantt::Constraint( proxyModel.index( 0, 3 ), proxyModel.index( 10, 3 ) ) );
0078         //cmodel.addConstraint( KGantt::Constraint( proxyModel.index( 10, 3 ), proxyModel.index( 5, 3 ) ) );
0079         QModelIndex pidx = model.index( 0, 0 );
0080         pidx = model.index( 0, 0, pidx );
0081         cmodel.addConstraint( KGantt::Constraint( model.index( 0, 0, pidx), model.index( 1, 0, pidx ) ) );
0082         cmodel.addConstraint( KGantt::Constraint( model.index( 1, 0, pidx), model.index( 0, 0, pidx ) ) );
0083         cmodel.addConstraint( KGantt::Constraint( model.index( 1, 0, pidx), model.index( 10, 0, pidx ) ) );
0084         cmodel.addConstraint( KGantt::Constraint( model.index( 3, 0, pidx), model.index( 5, 0, pidx ) ) );
0085         cmodel.addConstraint( KGantt::Constraint( model.index( 7, 0, pidx), model.index( 4, 0, pidx ) ) );
0086 
0087         qDebug() << "Creating view";
0088         slider.setOrientation( Qt::Horizontal );
0089         slider.setRange( 1, 1000 );
0090         slider.setValue( 100 );
0091         QVBoxLayout* l = new QVBoxLayout( this );
0092         l->addWidget( &view );
0093         l->addWidget( &slider );
0094         grid.setStartDateTime( QDateTime::currentDateTime().addDays( -3 ) );
0095         grid.setDayWidth( 100 );
0096         grid.setFreeDays( QSet<Qt::DayOfWeek>() << Qt::Saturday << Qt::Sunday );
0097         grid.setFreeDaysBrush( QBrush( Qt::red ) );
0098     QListView* lv = new MyListView;
0099     view.setLeftView(lv);
0100     view.setRowController( new KGantt::ListViewRowController(lv,view.ganttProxyModel()));
0101         view.setGrid( &grid );
0102         view.setModel(&model);
0103         //view.setConstraintModel( &cmodel );
0104         connect( &slider, SIGNAL(valueChanged(int)),
0105                  this, SLOT(slotZoom(int)) );
0106     }
0107 
0108 public Q_SLOTS:
0109     void slotZoom( int z )
0110     {
0111         grid.setDayWidth( z );
0112     }
0113 private:
0114     KGantt::View view;
0115     KGantt::DateTimeGrid grid;
0116     QSlider slider;
0117     QStandardItemModel model;
0118     KGantt::ConstraintModel cmodel;
0119 };
0120 
0121 int main( int argc, char** argv ) {
0122     QApplication app( argc, argv );
0123 
0124     MyWidget w;
0125     w.show();
0126 
0127     return app.exec();
0128 }
0129 
0130 #include "main.moc"