File indexing completed on 2024-05-12 15:59:57

0001 /* This file is part of the KDE project
0002    SPDX-FileCopyrightText: 2002 Patrick Julien <freak@codepimps.org>
0003    SPDX-FileCopyrightText: 2007 Jan Hambrecht <jaham@gmx.net>
0004    SPDX-FileCopyrightText: 2007 Sven Langkamp <sven.langkamp@gmail.com>
0005    SPDX-FileCopyrightText: 2010 Boudewijn Rempt <boud@valdyas.org>
0006    SPDX-FileCopyrightText: 2011 Srikanth Tiyyagura <srikanth.tulasiram@gmail.com>
0007    SPDX-FileCopyrightText: 2011 José Luis Vergara <pentalis@gmail.com>
0008    SPDX-FileCopyrightText: 2013 Sascha Suelzer <s.suelzer@gmail.com>
0009    SPDX-FileCopyrightText: 2019 Boudewijn Rempt <boud@valdyas.org>
0010 
0011    SPDX-License-Identifier: LGPL-2.0-or-later
0012 */
0013 
0014 #ifndef KIS_RESOURCE_ITEM_CHOOSER
0015 #define KIS_RESOURCE_ITEM_CHOOSER
0016 
0017 #include <QWidget>
0018 
0019 #include <QModelIndex>
0020 
0021 #include <KoResource.h>
0022 #include <KisKineticScroller.h>
0023 #include "KisPopupButton.h"
0024 
0025 class QAbstractProxyModel;
0026 class QAbstractItemDelegate;
0027 class QAbstractButton;
0028 class QToolButton;
0029 class QSortFilterProxyModel;
0030 class KisResourceItemListView;
0031 class KisTagFilterResourceProxyModel;
0032 
0033 #include "kritaresourcewidgets_export.h"
0034 
0035 
0036 /**
0037  * A widget that contains a KoResourceChooser as well
0038  * as an import/export button
0039  */
0040 class KRITARESOURCEWIDGETS_EXPORT KisResourceItemChooser : public QWidget
0041 {
0042     Q_OBJECT
0043 public:
0044     enum Buttons { Button_Import, Button_Remove };
0045 
0046     /// \p usePreview shows the aside preview with the resource's image
0047     /// \p extraFilterProxy is an extra filter proxy model for additional filtering. KisResourceItemChooser will take over ownership
0048     explicit KisResourceItemChooser(const QString &resourceType, bool usePreview = false, QWidget *parent = 0);
0049     ~KisResourceItemChooser() override;
0050 
0051     KisTagFilterResourceProxyModel *tagFilterModel() const;
0052 
0053     /// return the number of rows in the view
0054     int rowCount() const;
0055 
0056     /// Sets the height of the view rows
0057     void setRowHeight(int rowHeight);
0058 
0059     /// Sets the width of the view columns
0060     void setColumnWidth(int columnWidth);
0061 
0062     /// Sets a custom delegate for the view
0063     void setItemDelegate(QAbstractItemDelegate *delegate);
0064 
0065     /**
0066      * @brief Gets the currently selected resource
0067      * @param includeHidden If true, return the remembered resource even if
0068      *        it is currently not visible in the item view
0069      * @return The selected resource, 0 is no resource is selected
0070      */
0071     KoResourceSP currentResource(bool includeHidden = false) const;
0072 
0073     /// Sets the item representing the resource as selected
0074     void setCurrentResource(KoResourceSP resource);
0075 
0076     /**
0077      * Sets the selected resource, does nothing if there is no valid item
0078      * @param row row of the item
0079      * @param column column of the item
0080      */
0081     void setCurrentItem(int row);
0082 
0083     void showButtons(bool show);
0084 
0085     void addCustomButton(QAbstractButton *button, int cell);
0086 
0087     /// determines whether the preview right or below the splitter
0088     void setPreviewOrientation(Qt::Orientation orientation);
0089 
0090     /// determines whether the preview should tile the resource's image or not
0091     void setPreviewTiled(bool tiled);
0092 
0093     /// shows the preview converted to grayscale
0094     void setGrayscalePreview(bool grayscale);
0095 
0096     /// sets the visibility of tagging KlineEdits.
0097     void showTaggingBar(bool show);
0098 
0099     QSize viewSize() const;
0100 
0101     KisResourceItemListView *itemView() const;
0102 
0103     void setStoragePopupButtonVisible(bool visible);
0104     
0105     void setViewModeButtonVisible(bool visible);
0106     KisPopupButton *viewModeButton() const;
0107 
0108     void setSynced(bool sync);
0109 
0110     bool eventFilter(QObject *object, QEvent *event) override;
0111 
0112 Q_SIGNALS:
0113     /// Emitted when a resource was selected
0114     void resourceSelected(KoResourceSP resource);
0115     /// Emitted when an *already selected* resource is clicked
0116     /// again
0117     void resourceClicked(KoResourceSP resource);
0118     void splitterMoved();
0119 
0120 public Q_SLOTS:
0121     void slotButtonClicked(int button);
0122     void slotScrollerStateChanged(QScroller::State state){ KisKineticScroller::updateCursor(this, state); }
0123     void updateView();
0124 
0125 private Q_SLOTS:
0126     void activate(const QModelIndex &index);
0127     void clicked(const QModelIndex &index);
0128     void contextMenuRequested(const QPoint &pos);
0129     void baseLengthChanged(int length);
0130     void afterFilterChanged();
0131 
0132 protected:
0133     void showEvent(QShowEvent *event) override;
0134     void resizeEvent(QResizeEvent *event) override;
0135 
0136 private:
0137     void updateButtonState();
0138     void updatePreview(const QModelIndex &idx);
0139 
0140     /// Resource for a given model index
0141     /// @returns the resource pointer, 0 is index not valid
0142     KoResourceSP resourceFromModelIndex(const QModelIndex &index) const;
0143 
0144     class Private;
0145     Private *const d;
0146 
0147 
0148 };
0149 
0150 #endif // KO_RESOURCE_ITEM_CHOOSER