File indexing completed on 2024-05-05 04:49:28

0001 /****************************************************************************************
0002  * Copyright (c) 2008 Nikolaj Hald Nielsen <nhn@kde.org>                                *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #ifndef AMAROK_PRETTYTREEVIEW_H
0018 #define AMAROK_PRETTYTREEVIEW_H
0019 
0020 #include "amarok_export.h"
0021 
0022 #include <QTreeView>
0023 
0024 namespace Amarok
0025 {
0026     /**
0027      * A utility QTreeView subcass that handles:
0028      * - drawing nice (svg themed) rows
0029      * - palette changes
0030      * - nicer expanding/collapsing interaction even when single click is used
0031      * - decorator actions for root level items when isRootDecorated() is false
0032      *
0033      * If you use decorator actions, don't forget to set mouseTracking to true as
0034      * PrettyTreeView doesn't do it automatically as it would be too costly for models
0035      * that don't use the actions.
0036      *
0037      * @author: Nikolaj Hald Nielsen <nhn@kde.org>
0038      */
0039     class AMAROK_EXPORT PrettyTreeView : public QTreeView
0040     {
0041         Q_OBJECT
0042 
0043         public:
0044             explicit PrettyTreeView( QWidget *parent = nullptr );
0045             ~PrettyTreeView() override;
0046         /**
0047          * Return pointer to decorator action which was most recently mouse-pressed
0048          * or null it mouse button was released since then. Used by PrettyTreeDelegate.
0049          */
0050         QAction *pressedDecoratorAction() const;
0051 
0052         public Q_SLOTS:
0053             /* There is a need to overload even this edit() variant, otherwise it hides
0054              * QAbstractItemView's implementation. Note that it is NOT safe to do anything
0055              * special in this method, as it is not virtual.
0056              * bool edit( const QModelIndex &index, EditTrigger trigger, QEvent *event )
0057              * IS virtual. */
0058             void edit( const QModelIndex &index );
0059 
0060 
0061         protected:
0062             bool edit( const QModelIndex &index, EditTrigger trigger, QEvent *event ) override;
0063             void drawRow( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
0064 
0065             /**
0066              * Reimplemented to trigger item redraw in case mouse is over an item which
0067              * has decorator actions.
0068              */
0069             void mouseMoveEvent( QMouseEvent *event ) override;
0070 
0071             /**
0072              * Reimplemented to handle expanding with single-click mouse setting event
0073              * when it is clicked outside the arrow and for consistency with
0074              * mouseReleaseEvent() in case of decorator actions.
0075              */
0076             void mousePressEvent( QMouseEvent *event ) override;
0077 
0078             /**
0079              * Reimplemented to handle expanding with single-click mouse setting event
0080              * when it is clicked outside the arrow and to handle clicking on decorator
0081              * actions */
0082             void mouseReleaseEvent( QMouseEvent *event ) override;
0083 
0084             /**
0085              * Reimplemented to show proper tooltips for decorator actions.
0086              */
0087             bool viewportEvent( QEvent *event ) override;
0088 
0089             /**
0090              * Get dectorator action (little action icon as seen for example in collection
0091              * items in collection browser) of index @p idx under mouse position @p pos.
0092              */
0093             QAction *decoratorActionAt( const QModelIndex &idx, const QPoint &pos );
0094 
0095         protected Q_SLOTS:
0096             virtual void newPalette( const QPalette &palette );
0097 
0098         private:
0099             /**
0100              * Position (relative to this widget) where the mouse button was pressed to
0101              * trigger expand/collapse, or null pointer where expand/collapse shouldn't
0102              * be handled in mouseReleaseEvent()
0103              */
0104             QScopedPointer<QPoint> m_expandCollapsePressedAt;
0105 
0106             /**
0107              * Pointer to decorator action which was pressed in mousePressEvent() or null
0108              * pointer if no action was pressed in the most recent mouse press
0109              */
0110             QAction *m_decoratorActionPressed;
0111     };
0112 }
0113 
0114 #endif