smartlist.cpp 3.11 KB
Newer Older
1
/***************************************************************************
Nicolas Jager's avatar
Nicolas Jager committed
2
 * Copyright (C) 2015-2016 by Savoir-faire Linux                           *
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com>              *
 *                                                                         *
 * This program is free software; you can redistribute it and/or modify    *
 * it under the terms of the GNU General Public License as published by    *
 * the Free Software Foundation; either version 3 of the License, or       *
 * (at your option) any later version.                                     *
 *                                                                         *
 * This program is distributed in the hope that it will be useful,         *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 * GNU General Public License for more details.                            *
 *                                                                         *
 * You should have received a copy of the GNU General Public License       *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.   *
 **************************************************************************/

#include <QStyledItemDelegate>
#include <qevent.h>
#include <QTreeWidgetItem>
Nicolas Jager's avatar
Nicolas Jager committed
22
#include <QScrollBar>
23 24 25 26 27 28 29

#include "smartlistdelegate.h"
#include "combar.h"
#include "smartlist.h"

SmartList::SmartList(QWidget *parent) :
    QTreeView(parent),
Nicolas Jager's avatar
Nicolas Jager committed
30
    comBar_(new ComBar(this))
31
{
Nicolas Jager's avatar
Nicolas Jager committed
32
    verticalScrollBar()->hide();
33 34 35 36 37 38 39 40 41 42 43
}

SmartList::~SmartList()
{
    delete comBar_;
}

void
SmartList::enterEvent(QEvent* event)
{
    Q_UNUSED(event);
Nicolas Jager's avatar
Nicolas Jager committed
44
    verticalScrollBar()->show();
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

    repaint(0, 0, width(), height());
}

void
SmartList::leaveEvent(QEvent* event)
{
    Q_UNUSED(event);

    smartListDelegate_->setRowHighlighted(-1);

    currentRow_ = -1;

    if (smartListDelegate_)
        smartListDelegate_->setRowHighlighted(currentRow_);

Nicolas Jager's avatar
Nicolas Jager committed
61
    verticalScrollBar()->hide();
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

    comBar_->hide();
}

void
SmartList::wheelEvent(QWheelEvent* event)
{
    currentRow_ = -1;

    comBar_->hide();

    smartListDelegate_->setRowHighlighted(currentRow_);

    repaint(0, 0, width(), height());

    QTreeView::wheelEvent(event);
}

void
SmartList::paintEvent(QPaintEvent* event)
{
    QTreeView::paintEvent(event);

    if (currentRow_ > -1)
        comBar_->show();
    else
        comBar_->hide();
}

void
SmartList::mouseMoveEvent(QMouseEvent* event)
{
    QModelIndex index = indexAt(event->pos());

    repaint(0, 0, width(), height());

    currentRow_ = index.row();

    if (smartListDelegate_)
    {
        smartListDelegate_->setRowHighlighted(currentRow_);

        if (currentRow_ > -1)
            comBar_->show();
        else
            comBar_->hide();
    }
    QTreeView::mouseMoveEvent(event);
}

void
SmartList::setSmartListItemDelegate(SmartListDelegate* delegate)
{
    if (delegate)
    {
        setItemDelegate(delegate);
        smartListDelegate_ = delegate;
        connect(smartListDelegate_ , &SmartListDelegate::rowSelected , comBar_, &ComBar::moveToRow);
    }
}