1 /* This file is part of the KDE project
2 *
3 * Copyright (C) 2008 Peter Penz <peter.penz19@gmail.com>
4 * Copyright (C) 2011 Paul Mendez <paulestebanms@gmail.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB.  If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21 
22 #ifndef KOVIEWITEMCONTEXTBAR_H
23 #define KOVIEWITEMCONTEXTBAR_H
24 
25 #include <QObject>
26 #include "kowidgets_export.h"
27 #include <QModelIndex>
28 
29 class QAbstractItemView;
30 class QToolButton;
31 class QHBoxLayout;
32 class QRect;
33 
34 /**
35  * @brief Add context buttons to items of QAbstractView subclasses
36  *
37  * Whenever an item is hovered by the mouse, a toggle button is shown
38  * which allows to select/deselect the current item, other buttons for
39  * custom actions could be added using addContextButton method.
40  */
41 class KOWIDGETS_EXPORT KoViewItemContextBar : public QObject
42 {
43     Q_OBJECT
44 
45 public:
46     explicit KoViewItemContextBar(QAbstractItemView *parent);
47     ~KoViewItemContextBar() override = default;
48     bool eventFilter(QObject *watched, QEvent *event) override;
49 
50     /**
51      * Add a button to the context bar
52      * @param text to be used for button tool tip
53      * @param iconName or name of the icon displayed on the button
54      * @return a QToolButton, so it could be connected to a slot.
55      */
56     QToolButton *addContextButton(const QString &text, const QString &iconName);
57     //Returns the index of the item under the mouse cursor
58     QModelIndex currentIndex();
59 
60     int preferredWidth();
61     void setShowSelectionToggleButton(bool enabled);
62 
63 Q_SIGNALS:
64     /** Is emitted if the selection has been changed by the toggle button. */
65     void selectionChanged();
66 
67 public Q_SLOTS:
68     /** Hide context bar */
69     void reset();
70     void enableContextBar();
71     void disableContextBar();
72 
73 private Q_SLOTS:
74     void slotEntered(const QModelIndex &index);
75     void slotViewportEntered();
76     void setItemSelected();
77     /** Hide context bar if the selectem item has been removed */
78     void slotRowsRemoved(const QModelIndex &parent, int start, int end);
79     /** Updates contex bar buttons state*/
80     void updateHoverUi(const QModelIndex& index);
81     void showContextBar(const QRect &rect);
82     /** Updates Selection Button state*/
83     void updateToggleSelectionButton();
84     /** Update Bar */
85     void update();
86     /** Called when model resets */
87     void slotModelReset();
88 
89 private:
90     QAbstractItemView *m_view;
91     bool m_enabled;
92     QModelIndex m_IndexUnderCursor;
93     QWidget *m_ContextBar;
94     QToolButton *m_ToggleSelectionButton;
95     QHBoxLayout *m_Layout;
96     QList <QToolButton*> m_contextBarButtons;
97     bool m_showToggleButton;
98 };
99 
100 #endif // KOVIEWITEMCONTEXTBAR_H
101