/****************************************************************************
**
** Copyright (C) 1992-2004 Trolltech AS. All rights reserved.
**
** This file is part of the widgets module of the Qt Toolkit.
**
** Licensees holding valid Qt Preview licenses may use this file in
** accordance with the Qt Preview License Agreement provided with the
** Software.
**
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
** information about Qt Commercial License Agreements.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#ifndef QCOMBOBOX_P_H
#define QCOMBOBOX_P_H

#include <qlistview.h>
#include <qlineedit.h>
#include <qcombobox.h>
#include <qbasictimer.h>
#include <qabstractslider.h>
#include <qstyle.h>
#include <qstyleoption.h>
#include <qpainter.h>
#include <qitemdelegate.h>
#include <qapplication.h>
#include <private/qwidget_p.h>
#include <limits.h>

class Scroller : public QWidget
{
    Q_OBJECT

public:
    Scroller(QAbstractSlider::SliderAction action, QWidget *parent)
        : QWidget(parent), sliderAction(action) {
        setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
    }
    QSize sizeHint() const {
        return QSize(20, style().pixelMetric(QStyle::PM_MenuScrollerHeight));
    }

protected:
    void enterEvent(QEvent *) {
        timer.start(100, this);
    }
    void leaveEvent(QEvent *) {
        timer.stop();
    }
    void timerEvent(QTimerEvent *e) {
        if (e->timerId() == timer.timerId())
            emit doScroll(sliderAction);
    }
    void hideEvent(QHideEvent *) {
        timer.stop();
    }
    void paintEvent(QPaintEvent *) {
        QPainter p(this);
        QStyleOptionMenuItem menuOpt;
        menuOpt.palette = palette();
        menuOpt.state = QStyle::Style_None;
        menuOpt.checkType = QStyleOptionMenuItem::NotCheckable;
        menuOpt.menuRect = rect();
        menuOpt.rect = rect();
        menuOpt.maxIconWidth = 0;
        menuOpt.tabWidth = 0;
        menuOpt.menuItemType = QStyleOptionMenuItem::Scroller;
        if (sliderAction == QAbstractSlider::SliderSingleStepAdd)
            menuOpt.state = QStyle::Style_Down;
        p.eraseRect(rect());
        style().drawControl(QStyle::CE_MenuScroller, &menuOpt, &p);
    }

signals:
    void doScroll(int action);

private:
    QAbstractSlider::SliderAction sliderAction;
    QBasicTimer timer;
};

class ListViewContainer : public QFrame
{
    Q_OBJECT

public:
    ListViewContainer(QListView *listView, QWidget *parent = 0);
    QListView *listView() const;

public slots:
    void scrollListView(int action);
    void updateScrollers();
    void setCurrentIndex(const QModelIndex &index, Qt::ButtonState bstate);

protected:
    bool eventFilter(QObject *o, QEvent *e);
    void keyPressEvent(QKeyEvent *e);
    void mousePressEvent(QMouseEvent *e);
    void hideEvent(QHideEvent *e);

signals:
    void itemSelected(const QModelIndex &);
    void containerDisappearing();

private:
    QListView *list;
    Scroller *top;
    Scroller *bottom;
};

class MenuDelegate : public QAbstractItemDelegate
{
public:
    MenuDelegate(QObject *parent, QComboBox *cmb) : QAbstractItemDelegate(parent), mCombo(cmb) {}

protected:
    void paint(QPainter *painter, const QStyleOptionViewItem &option,
                       const QAbstractItemModel *model, const QModelIndex &index) const {
        QStyleOptionMenuItem opt = getStyleOption(option, model, index);
        painter->eraseRect(option.rect);
        QApplication::style().drawControl(QStyle::CE_MenuItem, &opt, painter, 0);
    }
    QSize sizeHint(const QStyleOptionViewItem &option,
                           const QAbstractItemModel *model, const QModelIndex &index) const {
        QStyleOptionMenuItem opt = getStyleOption(option, model, index);
        QVariant value = model->data(index, QAbstractItemModel::FontRole);
        QFont fnt = value.isValid() ? value.toFont() : option.font;
        return QApplication::style().sizeFromContents(
            QStyle::CT_MenuItem, &opt, option.rect.size(), QFontMetrics(fnt), 0);
    }

private:
    QStyleOptionMenuItem getStyleOption(const QStyleOptionViewItem &option,
                                        const QAbstractItemModel *model,
                                        const QModelIndex &index) const {
        QStyleOptionMenuItem menuOption;
        menuOption.palette = option.palette;
        menuOption.state = QStyle::Style_None;
        if (option.state & QStyle::Style_Enabled)
            menuOption.state |= QStyle::Style_Enabled;
        if (option.state & QStyle::Style_Selected)
            menuOption.state |= QStyle::Style_Active;
        menuOption.checkType = QStyleOptionMenuItem::NonExclusive;
        menuOption.checked = mCombo->currentItem() == index.row();
        menuOption.menuItemType = QStyleOptionMenuItem::Normal;
        menuOption.icon = model->data(index, QAbstractItemModel::DecorationRole).toIconSet();
        menuOption.text = model->data(index, QAbstractItemModel::DisplayRole).toString();
        menuOption.tabWidth = 0;
        menuOption.maxIconWidth = 0;
        if (!menuOption.icon.isNull())
            menuOption.maxIconWidth
                = menuOption.icon.pixmap(QIcon::Small, QIcon::Normal).width() + 4;
        menuOption.menuRect = option.rect;
        menuOption.rect = option.rect;
        return menuOption;
    }

    QComboBox *mCombo;
};

class ComboModel : public QAbstractListModel
{
public:
    QVariant data(const QModelIndex &index, int role = DisplayRole) const {
        if ((role == DisplayRole || role == EditRole || role == DecorationRole)
            && index.isValid()
            && index.row() < rowCount()
            && index.column() < columnCount()) {
            if (role == DisplayRole || role == EditRole)
                return list.at(index.row()).first;
            if (role == DecorationRole)
                return list.at(index.row()).second;
        }
        return QVariant::Invalid;
    }

    int rowCount() const {
        return list.count();
    }

    bool setData(const QModelIndex &index, int role, const QVariant &value) {
        if (!index.isValid())
            return false;
        if (role == DisplayRole || role == EditRole) {
            list[index.row()].first = value.toString();
            emit dataChanged(index, index);
            return true;
        } else if (role == DecorationRole) {
            list[index.row()].second = value.toIconSet();
            emit dataChanged(index, index);
            return true;
        }
        return false;
    }

    QAbstractItemModel::ItemFlags flags(const QModelIndex &index) const {
        return QAbstractItemModel::flags(index) | QAbstractItemModel::ItemIsEditable;
    }

    bool insertRows(int row, const QModelIndex &parent, int count) {
        // this model only allows a 1D list
        if (parent.isValid() || count < 1 || row < 0 || row > rowCount())
            return false;

        QPair<QString, QIcon> emptyPair;
        while (count--)
            list.insert(row, emptyPair);
        emit rowsInserted(parent, row, row+count-1);
        return true;
    }

    bool removeRows(int row, const QModelIndex &parent, int count) {
        // this model only allows a 1D list
        if (parent.isValid() || count < 1 || row < 0 || row+count > rowCount())
            return false;

        emit rowsRemoved(parent, row, row+count-1);
        for (int i=row+count-1; i >= row; --i)
            list.removeAt(i);
        return true;
    }

private:
    QList<QPair<QString,QIcon> > list;
};

class QComboBoxPrivate: public QWidgetPrivate
{
    Q_DECLARE_PUBLIC(QComboBox)
public:
    QComboBoxPrivate()
        : QWidgetPrivate(),
          model(0),
          lineEdit(0),
          container(0),
          insertionPolicy(QComboBox::AtBottom),
          autoCompletion(true),
          duplicatesEnabled(false),
          autoHide(true),
          arrowDown(false),
          sizeLimit(10),
          maxCount(INT_MAX),
          skipCompletion(false) {}
    ~QComboBoxPrivate() {}
    void init();
    QStyleOptionComboBox getStyleOption() const;
    void updateLineEditGeometry();
    void returnPressed();
    void complete();
    void itemSelected(const QModelIndex &item);
    bool contains(const QString &text, int role);
    void emitActivated(const QModelIndex&);
    void emitHighlighted(const QModelIndex&);
    void resetButton();

    QAbstractItemModel *model;
    QLineEdit *lineEdit;
    ListViewContainer *container;
    QComboBox::InsertionPolicy insertionPolicy;
    bool autoCompletion;
    bool duplicatesEnabled;
    bool autoHide;
    bool arrowDown;
    int sizeLimit;
    int maxCount;
    bool skipCompletion;
    mutable QSize sizeHint;
    QPersistentModelIndex currentIndex;
    QPersistentModelIndex root;
};

#endif //QCOMBOBOX_P_H
