/***************************** LICENSE START ***********************************

 Copyright 2012 ECMWF and INPE. This software is distributed under the terms
 of the Apache License version 2.0. In applying this license, ECMWF does not
 waive the privileges and immunities granted to it by virtue of its status as
 an Intergovernmental Organization or submit itself to any jurisdiction.

 ***************************** LICENSE END *************************************/

#ifndef MvQTreeView_H
#define MvQTreeView_H

#include <QPen>
#include <QBrush>
#include <QPoint>
#include <QTreeView>
#include <QStyledItemDelegate>

using namespace std;

class MvQTreeViewDelegate : public QStyledItemDelegate
{
    friend class MvQTreeView;

public:
    explicit MvQTreeViewDelegate(QTreeView* parent);
    void setStyleId(int id);
    void paint(QPainter* painter, const QStyleOptionViewItem& option,
               const QModelIndex& index) const;
    QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const;

protected:
    QPen selectPen_;
    QBrush selectBrush_;
    QPen errorSelectPen_;
    QBrush errorSelectBrush_;
    QBrush errorBrush_;
    QBrush blockBrush_;
    QPen blockBorderPen_;
    QBrush selectBrushBlock_;
    QPen borderPen_;
    QTreeView* view_;
};

class MvQTreeView : public QTreeView
{
    Q_OBJECT

public:
    MvQTreeView(QWidget* parent = 0);
    void setStyleId(int);
    void keyboardSearch(const QString&);
    void setActvatedByKeyNavigation(bool b) { activatedByKeyNavigation_ = b; }
    void setColumnToDrag(int i) { columnToDrag_ = i; }

signals:
    void selectionChanged(const QModelIndex&);

protected:
    void keyReleaseEvent(QKeyEvent*);
    void selectionChanged(const QItemSelection& selected, const QItemSelection& deselected);
    void drawBranches(QPainter* painter, const QRect& rect, const QModelIndex& index) const;

    bool dragEnabled();
    bool dropEnabled();

    MvQTreeViewDelegate* delegate_;
    bool activatedByKeyNavigation_;
    int columnToDrag_;
};

#endif
