/****************************************************************************
**
** Copyright (C) 2011 Digia Plc
** All rights reserved.
** For any questions to Digia, please use contact form at http://qt.digia.com
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of Digia nor the names of its contributors
**     may be used to endorse or promote products derived from this 
**     software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
** 
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
****************************************************************************/

#include <QtGui>

#include "My_TreeView.h"
#include "mysortfilterproxymodel.h"
#include "window.h"

//
// <<<< Window::Window
//
Window::Window()
{
    _proxyModel = new MySortFilterProxyModel(this);
    _proxyModel->setDynamicSortFilter(true);
    //! [0]

    _sourceView = new My_TreeView( _proxyModel, true );  // ************
    _sourceView->setRootIsDecorated(false);
    _sourceView->setAlternatingRowColors(true);
    //_sourceView->setSortingEnabled(true);


    QHBoxLayout *sourceLayout = new QHBoxLayout;
    sourceLayout->addWidget( _sourceView );

    _sourceGroupBox = new QGroupBox(tr("Original Model"));
    _sourceGroupBox->setLayout( sourceLayout );

    _filterCaseSensitivityCheckBox = new QCheckBox(tr("Case sensitive filter"));
    _filterCaseSensitivityCheckBox->setChecked(true);

    _filterPatternLineEdit = new QLineEdit;
    _filterPatternLineEdit->setText("Grace|Sports");

    _filterPatternLabel = new QLabel(tr("&Filter pattern:"));
    _filterPatternLabel->setBuddy(_filterPatternLineEdit);

    _filterSyntaxComboBox = new QComboBox;
    _filterSyntaxComboBox->addItem(tr("Regular expression"), QRegExp::RegExp);
    _filterSyntaxComboBox->addItem(tr("Wildcard"), QRegExp::Wildcard);
    _filterSyntaxComboBox->addItem(tr("Fixed string"), QRegExp::FixedString);

    _fromDateEdit = new QDateEdit;
    _fromDateEdit->setDate(QDate(1970, 01, 01));
    _fromLabel = new QLabel(tr("F&rom:"));
    _fromLabel->setBuddy(_fromDateEdit);

    _toDateEdit = new QDateEdit;
    _toDateEdit->setDate(QDate(2099, 12, 31));
    _toLabel = new QLabel(tr("&To:"));
    _toLabel->setBuddy(_toDateEdit);

    connect( _filterPatternLineEdit, SIGNAL( textChanged      ( QString ))
           , this                  , SLOT  ( textFilterChanged() ) );

    connect( _filterSyntaxComboBox, SIGNAL( currentIndexChanged( int ) )
           , this                 , SLOT  ( textFilterChanged  () ) );

    connect( _filterCaseSensitivityCheckBox, SIGNAL( toggled          ( bool ) )
           , this                          , SLOT  ( textFilterChanged() ) );

    connect( _fromDateEdit, SIGNAL( dateChanged     ( QDate ) )
           , this         , SLOT  ( dateFilterChanged() ) );

    connect( _toDateEdit, SIGNAL( dateChanged      ( QDate ))
           , this       , SLOT  ( dateFilterChanged()));

    _proxyView = new My_TreeView( _proxyModel, false ); // ********
    _proxyView->setRootIsDecorated( false );
    _proxyView->setAlternatingRowColors( true );
    _proxyView->setModel( _proxyModel );
    _proxyView->setSortingEnabled( true );
    _proxyView->sortByColumn( 1, Qt::AscendingOrder );

    QGridLayout *proxyLayout = new QGridLayout;
    proxyLayout->addWidget( _proxyView            , 0, 0, 1, 3 );
    proxyLayout->addWidget( _filterPatternLabel   , 1, 0);
    proxyLayout->addWidget( _filterPatternLineEdit, 1, 1);
    proxyLayout->addWidget( _filterSyntaxComboBox , 1, 2);
    proxyLayout->addWidget( _filterCaseSensitivityCheckBox, 2, 0, 1, 3);
    proxyLayout->addWidget( _fromLabel            , 3, 0);
    proxyLayout->addWidget( _fromDateEdit         , 3, 1, 1, 2);
    proxyLayout->addWidget( _toLabel              , 4, 0);
    proxyLayout->addWidget( _toDateEdit           , 4, 1, 1, 2);

    _proxyGroupBox = new QGroupBox( tr("Sorted/Filtered Model") );
    _proxyGroupBox->setLayout( proxyLayout );

    _Progress_GroupBox = new QGroupBox( tr("ProgressBar" ) );
    _ProgressBar = new QProgressBar;
    _ProgressBar->setRange( 0, 99 ); 
    _ProgressBar->setValue( 49 ); 
    _ProgressBar->resize( _ProgressBar->sizeHint() );

    QHBoxLayout * Progress_Layout = new QHBoxLayout;
    Progress_Layout->addWidget( _ProgressBar );

    _Progress_GroupBox->setLayout( Progress_Layout );

    QVBoxLayout *mainLayout = new QVBoxLayout;

    mainLayout->addWidget( _sourceGroupBox );
    mainLayout->addWidget( _proxyGroupBox  );
    mainLayout->addWidget( _Progress_GroupBox, 1 );
    setLayout( mainLayout );

    setWindowTitle( tr("Custom Sort/Filter Model") );
    resize( 500, 600 );

    _MainTimer = new QTimer( this );
    _MainTimer->start( 500 );  // 0,5 s

     connect( _MainTimer, SIGNAL(    timeout() )
            , this      , SLOT  ( slot_Timer() ) );
}
// >>>> Window::Window


//
// <<<< Window::setSourceModel
//
void Window::setSourceModel( QAbstractItemModel *p_Model )
{
  _proxyModel->setSourceModel( p_Model );
  _sourceView->setModel( p_Model );
}
// >>>> Window::setSourceModel


//
// <<<< Window::Adjust_ColumnSize
//
void Window::Adjust_ColumnSize()
{
  int KC, NC;
  
  NC = _sourceView->header()->count();
  for ( KC = 0; KC < NC; KC++ ) {
    _sourceView->resizeColumnToContents( KC );
  }

  NC = _proxyView->header()->count();
  for ( KC = 0; KC < NC; KC++ ) {
    _proxyView->resizeColumnToContents( KC );
  }
}
// >>>> Window::Adjust_ColumnSize


//
// <<<< Window::textFilterChanged
//
void Window::textFilterChanged()
{
    QRegExp::PatternSyntax syntax =
            QRegExp::PatternSyntax(_filterSyntaxComboBox->itemData(
                    _filterSyntaxComboBox->currentIndex()).toInt() );
    Qt::CaseSensitivity caseSensitivity =
            _filterCaseSensitivityCheckBox->isChecked() ? Qt::CaseSensitive
                : Qt::CaseInsensitive;

    QRegExp regExp(_filterPatternLineEdit->text(), caseSensitivity, syntax);
    _proxyModel->setFilterRegExp(regExp);
}
// >>>> Window::textFilterChanged


//
// <<<< Window::dateFilterChanged
//
void Window::dateFilterChanged()
{
    _proxyModel->setFilterMinimumDate( _fromDateEdit->date() );
    _proxyModel->setFilterMaximumDate( _toDateEdit->date() );
}
// <<<< Window::dateFilterChanged


//
// <<<< Window::slot_Timer
//
void Window::slot_Timer()
{
  int VLR = _ProgressBar->value() +1;
  int MAX = _ProgressBar->maximum();

  if ( VLR > MAX ) {
    VLR = _ProgressBar->minimum();
  }

  _ProgressBar->setValue( VLR );
}
// >>>> Window::slot_Timer
