File indexing completed on 2024-05-19 04:27:44

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 #include <QListView>
0021 
0022 #include <KoResource.h>
0023 #include <KisKineticScroller.h>
0024 #include "KisPopupButton.h"
0025 #include "ResourceListViewModes.h"
0026 
0027 class QAbstractProxyModel;
0028 class QAbstractItemDelegate;
0029 class QAbstractButton;
0030 class QToolButton;
0031 class QSortFilterProxyModel;
0032 class KisResourceItemListView;
0033 class KisTagFilterResourceProxyModel;
0034 
0035 #include "kritaresourcewidgets_export.h"
0036 
0037 
0038 /**
0039  * A widget that contains a KisResourceItemListView with filters for resource and tags.
0040  */
0041 class KRITARESOURCEWIDGETS_EXPORT KisResourceItemChooser : public QWidget
0042 {
0043     Q_OBJECT
0044 public:
0045     enum Buttons { Button_Import, Button_Remove };
0046     enum class Layout {
0047         NotSet,
0048         Vertical,
0049         Horizontal2Rows,
0050         Horizontal1Row
0051     };
0052 
0053     /**
0054      * @param resourceType Type of resource to choose from.
0055      * @param usePreview Displays the selected resource icon to the right side of the resources view.
0056      *        It looks bad, should be deleted.
0057      */
0058     explicit KisResourceItemChooser(const QString &resourceType, bool usePreview = false, QWidget *parent = 0);
0059     ~KisResourceItemChooser() override;
0060 
0061     /// Enable or disable changing the layout based on size.
0062     /// Default is false no responsiveness, layout is vertical.
0063     void setResponsiveness(bool isResponsive);
0064 
0065     /// Set's the desired view mode for the resource list.
0066     /// Caller should use this instead of directly tampering with the KisResourceItemListView.
0067     void setListViewMode(ListViewMode viewMode);
0068 
0069     /// Sets the visibility of tagging KlineEdits.
0070     /// Default is false.
0071     void showTaggingBar(bool show);
0072 
0073     KisTagFilterResourceProxyModel *tagFilterModel() const;
0074 
0075     /// Show the button for changing the view mode.
0076     /// Default is false.
0077     void showViewModeBtn(bool visible);
0078 
0079     KisPopupButton *viewModeButton() const;
0080 
0081     /// Shows or hides the storage button.
0082     /// Default is true.
0083     void showStorageBtn(bool visible);
0084 
0085     /// Sets the height of the view rows
0086     void setRowHeight(int rowHeight);
0087 
0088     /// Sets the width of the view columns
0089     void setColumnWidth(int columnWidth);
0090 
0091     /// Sets a custom delegate for the view
0092     void setItemDelegate(QAbstractItemDelegate *delegate);
0093 
0094     /**
0095      * @brief Gets the currently selected resource
0096      * @param includeHidden If true, return the remembered resource even if
0097      *        it is currently not visible in the item view
0098      * @return The selected resource, 0 is no resource is selected
0099      */
0100     KoResourceSP currentResource(bool includeHidden = false) const;
0101 
0102     /// Sets the item representing the resource as selected
0103     void setCurrentResource(KoResourceSP resource);
0104     void setCurrentResource(QString resourceName);
0105 
0106     /**
0107      * Sets the selected resource, does nothing if there is no valid item
0108      * @param row row of the item
0109      * @param column column of the item
0110      */
0111     void setCurrentItem(int row);
0112 
0113     /// Shows the import and export buttons for the resource.
0114     /// Default is true.
0115     void showImportExportBtns(bool show);
0116 
0117     /// determines whether the preview right or below the splitter
0118     void setPreviewOrientation(Qt::Orientation orientation);
0119 
0120     /// determines whether the preview should tile the resource's image or not
0121     void setPreviewTiled(bool tiled);
0122 
0123     /// shows the preview converted to grayscale
0124     void setGrayscalePreview(bool grayscale);
0125 
0126     /// View size for the resources view.
0127     QSize viewSize() const;
0128 
0129     /// Do not use this to change the view mode and flow directly.
0130     /// Use the requestViewMode() and requestFlow() methods so as to not
0131     /// intervene in the responsive design layout.
0132     KisResourceItemListView *itemView() const;
0133 
0134     void setSynced(bool sync);
0135 
0136     /// Allows zooming with Ctrl + Mouse Wheel
0137     bool eventFilter(QObject *object, QEvent *event) override;
0138 
0139 Q_SIGNALS:
0140     /// Emitted when the view mode for the internal KisResourceItemListView changes
0141     void listViewModeChanged(ListViewMode newViewMode);
0142 
0143     /// Emitted when a resource was selected
0144     void resourceSelected(KoResourceSP resource);
0145 
0146     /// Emitted when an *already selected* resource is clicked
0147     /// again
0148     void resourceClicked(KoResourceSP resource);
0149 
0150 public Q_SLOTS:
0151     void slotButtonClicked(int button);
0152     void slotScrollerStateChanged(QScroller::State state){ KisKineticScroller::updateCursor(this, state); }
0153     void updateView();
0154 
0155 private Q_SLOTS:
0156     void scrollBackwards();
0157     void scrollForwards();
0158     void activate(const QModelIndex &index);
0159     void clicked(const QModelIndex &index);
0160     void contextMenuRequested(const QPoint &pos);
0161     void baseLengthChanged(int length);
0162     void afterFilterChanged();
0163     void slotSaveSplitterState();
0164 
0165 protected:
0166     void showEvent(QShowEvent *event) override;
0167     void resizeEvent(QResizeEvent *event) override;
0168 
0169 private:
0170     void updateButtonState();
0171     void updatePreview(const QModelIndex &idx);
0172 
0173     void hideEverything();
0174     void applyVerticalLayout();
0175     void changeLayoutBasedOnSize();
0176 
0177     /// Resource for a given model index
0178     /// @returns the resource pointer, 0 is index not valid
0179     KoResourceSP resourceFromModelIndex(const QModelIndex &index) const;
0180 
0181     class Private;
0182     Private *const d;
0183 };
0184 
0185 #endif // KO_RESOURCE_ITEM_CHOOSER