Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 1 | /*************************************************************************** |
Nicolas Jager | 540a511 | 2016-01-15 15:54:34 -0500 | [diff] [blame^] | 2 | * Copyright (C) 2015-2016 by Savoir-faire Linux * |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 3 | * Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com> * |
| 4 | * * |
| 5 | * This program is free software; you can redistribute it and/or modify * |
| 6 | * it under the terms of the GNU General Public License as published by * |
| 7 | * the Free Software Foundation; either version 3 of the License, or * |
| 8 | * (at your option) any later version. * |
| 9 | * * |
| 10 | * This program is distributed in the hope that it will be useful, * |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | * GNU General Public License for more details. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU General Public License * |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. * |
| 17 | **************************************************************************/ |
| 18 | |
| 19 | #include <QStyledItemDelegate> |
| 20 | #include <qevent.h> |
| 21 | #include <QTreeWidgetItem> |
Nicolas Jager | 540a511 | 2016-01-15 15:54:34 -0500 | [diff] [blame^] | 22 | #include <QScrollBar> |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 23 | |
| 24 | #include "smartlistdelegate.h" |
| 25 | #include "combar.h" |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 26 | #include "smartlist.h" |
| 27 | |
| 28 | SmartList::SmartList(QWidget *parent) : |
| 29 | QTreeView(parent), |
Nicolas Jager | 540a511 | 2016-01-15 15:54:34 -0500 | [diff] [blame^] | 30 | comBar_(new ComBar(this)) |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 31 | { |
Nicolas Jager | 540a511 | 2016-01-15 15:54:34 -0500 | [diff] [blame^] | 32 | verticalScrollBar()->hide(); |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | SmartList::~SmartList() |
| 36 | { |
| 37 | delete comBar_; |
| 38 | } |
| 39 | |
| 40 | void |
| 41 | SmartList::enterEvent(QEvent* event) |
| 42 | { |
| 43 | Q_UNUSED(event); |
Nicolas Jager | 540a511 | 2016-01-15 15:54:34 -0500 | [diff] [blame^] | 44 | verticalScrollBar()->show(); |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 45 | |
| 46 | repaint(0, 0, width(), height()); |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | SmartList::leaveEvent(QEvent* event) |
| 51 | { |
| 52 | Q_UNUSED(event); |
| 53 | |
| 54 | smartListDelegate_->setRowHighlighted(-1); |
| 55 | |
| 56 | currentRow_ = -1; |
| 57 | |
| 58 | if (smartListDelegate_) |
| 59 | smartListDelegate_->setRowHighlighted(currentRow_); |
| 60 | |
Nicolas Jager | 540a511 | 2016-01-15 15:54:34 -0500 | [diff] [blame^] | 61 | verticalScrollBar()->hide(); |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 62 | |
| 63 | comBar_->hide(); |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | SmartList::wheelEvent(QWheelEvent* event) |
| 68 | { |
| 69 | currentRow_ = -1; |
| 70 | |
| 71 | comBar_->hide(); |
| 72 | |
| 73 | smartListDelegate_->setRowHighlighted(currentRow_); |
| 74 | |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 75 | repaint(0, 0, width(), height()); |
| 76 | |
| 77 | QTreeView::wheelEvent(event); |
| 78 | } |
| 79 | |
| 80 | void |
| 81 | SmartList::paintEvent(QPaintEvent* event) |
| 82 | { |
| 83 | QTreeView::paintEvent(event); |
| 84 | |
| 85 | if (currentRow_ > -1) |
| 86 | comBar_->show(); |
| 87 | else |
| 88 | comBar_->hide(); |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | SmartList::mouseMoveEvent(QMouseEvent* event) |
| 93 | { |
| 94 | QModelIndex index = indexAt(event->pos()); |
| 95 | |
Nicolas Jager | 97a21b4 | 2015-12-03 16:55:45 -0500 | [diff] [blame] | 96 | repaint(0, 0, width(), height()); |
| 97 | |
| 98 | currentRow_ = index.row(); |
| 99 | |
| 100 | if (smartListDelegate_) |
| 101 | { |
| 102 | smartListDelegate_->setRowHighlighted(currentRow_); |
| 103 | |
| 104 | if (currentRow_ > -1) |
| 105 | comBar_->show(); |
| 106 | else |
| 107 | comBar_->hide(); |
| 108 | } |
| 109 | QTreeView::mouseMoveEvent(event); |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | SmartList::setSmartListItemDelegate(SmartListDelegate* delegate) |
| 114 | { |
| 115 | if (delegate) |
| 116 | { |
| 117 | setItemDelegate(delegate); |
| 118 | smartListDelegate_ = delegate; |
| 119 | connect(smartListDelegate_ , &SmartListDelegate::rowSelected , comBar_, &ComBar::moveToRow); |
| 120 | } |
| 121 | } |