File indexing completed on 2022-11-29 14:16:03

0001 /*
0002     Qt item view for images - delegate additions
0003 
0004     Copyright (C) 2009 Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
0005     Copyright (C) 2011 Martin Klapetek <martin dot klapetek at gmail dot com>
0006 
0007     This library is free software; you can redistribute it and/or
0008     modify it under the terms of the GNU Lesser General Public
0009     License as published by the Free Software Foundation; either
0010     version 2.1 of the License, or (at your option) any later version.
0011 
0012     This library is distributed in the hope that it will be useful,
0013     but WITHOUT ANY WARRANTY; without even the implied warranty of
0014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015     Lesser General Public License for more details.
0016 
0017     You should have received a copy of the GNU Lesser General Public
0018     License along with this library; if not, write to the Free Software
0019     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0020 */
0021 
0022 #ifndef CONTACTDELEGATEOVERLAY_H
0023 #define CONTACTDELEGATEOVERLAY_H
0024 
0025 // Qt includes
0026 
0027 #include <QAbstractItemView>
0028 #include <QPointer>
0029 
0030 class ContactViewHoverButton;
0031 
0032 class ContactDelegateOverlay : public QObject
0033 {
0034     Q_OBJECT
0035 
0036 public:
0037     ContactDelegateOverlay(QObject *parent = 0);
0038     ~ContactDelegateOverlay();
0039 
0040     /** If active is true, this will call createWidget(), initialize the widget for use,
0041      *  and setup connections for the virtual slots.
0042      *  If active is false, this will delete the widget and
0043      *  disconnect all signal from model and view to this object (!) */
0044     virtual void setActive(bool active);
0045 
0046     ContactViewHoverButton* button() const;
0047 
0048     void setView(QAbstractItemView *view);
0049     QAbstractItemView* view() const;
0050     void setDelegate(QAbstractItemDelegate *delegate);
0051     QAbstractItemDelegate* delegate() const;
0052     virtual bool acceptsDelegate(QAbstractItemDelegate*) const { return true; }
0053 
0054 Q_SIGNALS:
0055     /// Emitted when the overlay is shown
0056     void overlayActivated(QModelIndex);
0057 
0058     /// Emitted when the overlay is hidden
0059     void overlayHidden();
0060 
0061     void update(const QModelIndex &index);
0062 
0063 protected Q_SLOTS:
0064     /** Called when any change from the delegate occurs - when the overlay is installed,
0065      *  when size hints, styles or fonts change */
0066     virtual void visualChange();
0067 
0068     /** Default implementation shows the widget iff the index is valid and checkIndex returns true. */
0069     virtual void slotEntered(const QModelIndex &index);
0070 
0071     /** Hides and resets the button */
0072     virtual void slotReset();
0073 
0074     /** Called when the widget shall be hidden (mouse cursor left index, viewport, uninstalled etc.).
0075      *  Default implementation hide()s m_widget. */
0076     virtual void slotHideButton();
0077 
0078 protected:
0079     /** Return true here if you want to show the overlay for the given index.
0080      *  The default implementation returns true. */
0081     virtual bool checkIndex(const QModelIndex &index) const;
0082 
0083     /** Create your widget here. Pass view() as parent. */
0084     virtual ContactViewHoverButton* createButton() = 0;
0085 
0086     /** Called when a new index is entered. Reposition your button here,
0087      *  adjust and store state. */
0088     virtual void updateButton(const QModelIndex &index) = 0;
0089 
0090     bool eventFilter(QObject *obj, QEvent *event);
0091 
0092     QAbstractItemView     *m_view;
0093     QAbstractItemDelegate *m_delegate;
0094     QPointer<ContactViewHoverButton> m_button;
0095     bool m_mouseButtonPressedOnWidget;
0096 };
0097 
0098 #define REQUIRE_DELEGATE(Delegate) \
0099 public: \
0100     void setDelegate(Delegate* delegate) { ContactDelegateOverlay::setDelegate(delegate); } \
0101     Delegate* delegate() const { return static_cast<Delegate*>(ContactDelegateOverlay::delegate()); } \
0102     virtual bool acceptsDelegate(QAbstractItemDelegate*d) const { return dynamic_cast<Delegate*>(d); } \
0103 private:
0104 
0105 
0106 // -------------------------------------------------------------------------------------------
0107 
0108 class ContactDelegateOverlayContainer
0109 {
0110 public:
0111     /**
0112      * This is a sample implementation for
0113      * delegate management methods, to be inherited by a delegate.
0114      * Does not inherit QObject, the delegate already does.
0115      */
0116 
0117     virtual ~ContactDelegateOverlayContainer();
0118 
0119     void installOverlay(ContactDelegateOverlay *overlay);
0120     void removeOverlay(ContactDelegateOverlay *overlay);
0121     void setAllOverlaysActive(bool active);
0122     void setViewOnAllOverlays(QAbstractItemView *view);
0123     void removeAllOverlays();
0124 
0125 protected:
0126     /// Declare as slot in the derived class calling this method
0127     virtual void overlayDestroyed(QObject *o);
0128 
0129     /// Returns the delegate, typically, the derived class
0130     virtual QAbstractItemDelegate* asDelegate() = 0;
0131 
0132 protected:
0133     QList<ContactDelegateOverlay*> m_overlays;
0134 
0135 };
0136 
0137 #endif /* CONTACTDELEGATEOVERLAY_H */