File indexing completed on 2025-01-05 04:47:08

0001 /*
0002     SPDX-FileCopyrightText: 2008 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "akonadiwidgets_export.h"
0010 
0011 #include <QWidget>
0012 
0013 #include <memory>
0014 
0015 namespace Akonadi
0016 {
0017 class Collection;
0018 class CollectionPropertiesPagePrivate;
0019 
0020 /**
0021  * @short A single page in a collection properties dialog.
0022  *
0023  * The collection properties dialog can be extended by custom
0024  * collection properties pages, which provide gui elements for
0025  * viewing and changing collection attributes.
0026  *
0027  * The following example shows how to create a simple collection
0028  * properties page for the secrecy attribute from the Akonadi::Attribute
0029  * example.
0030  *
0031  * @code
0032  *
0033  * class SecrecyPage : public CollectionPropertiesPage
0034  * {
0035  *    public:
0036  *      SecrecyPage( QWidget *parent = nullptr )
0037  *        : CollectionPropertiesPage( parent )
0038  *      {
0039  *        QVBoxLayout *layout = new QVBoxLayout( this );
0040  *
0041  *        mSecrecy = new QComboBox( this );
0042  *        mSecrecy->addItem( "Public" );
0043  *        mSecrecy->addItem( "Private" );
0044  *        mSecrecy->addItem( "Confidential" );
0045  *
0046  *        layout->addWidget( new QLabel( "Secrecy:" ) );
0047  *        layout->addWidget( mSecrecy );
0048  *
0049  *        setPageTitle( i18n( "Secrecy" ) );
0050  *      }
0051  *
0052  *      void load( const Collection &collection )
0053  *      {
0054  *        SecrecyAttribute *attr = collection.attribute( "secrecy" );
0055  *
0056  *        switch ( attr->secrecy() ) {
0057  *          case SecrecyAttribute::Public: mSecrecy->setCurrentIndex( 0 ); break;
0058  *          case SecrecyAttribute::Private: mSecrecy->setCurrentIndex( 1 ); break;
0059  *          case SecrecyAttribute::Confidential: mSecrecy->setCurrentIndex( 2 ); break;
0060  *        }
0061  *      }
0062  *
0063  *      void save( Collection &collection )
0064  *      {
0065  *        SecrecyAttribute *attr = collection.attribute( "secrecy" );
0066  *
0067  *        switch ( mSecrecy->currentIndex() ) {
0068  *          case 0: attr->setSecrecy( SecrecyAttribute::Public ); break;
0069  *          case 1: attr->setSecrecy( SecrecyAttribute::Private ); break;
0070  *          case 2: attr->setSecrecy( SecrecyAttribute::Confidential ); break;
0071  *        }
0072  *      }
0073  *
0074  *      bool canHandle( const Collection &collection ) const
0075  *      {
0076  *        return collection.hasAttribute( "secrecy" );
0077  *      }
0078  * };
0079  *
0080  * AKONADI_COLLECTION_PROPERTIES_PAGE_FACTORY( SecrecyPageFactory, SecrecyPage )
0081  *
0082  * @endcode
0083  *
0084  * @see Akonadi::CollectionPropertiesDialog, Akonadi::CollectionPropertiesPageFactory
0085  *
0086  * @author Volker Krause <vkrause@kde.org>
0087  */
0088 class AKONADIWIDGETS_EXPORT CollectionPropertiesPage : public QWidget
0089 {
0090     Q_OBJECT
0091 public:
0092     /**
0093      * Creates a new collection properties page.
0094      *
0095      * @param parent The parent widget.
0096      */
0097     explicit CollectionPropertiesPage(QWidget *parent = nullptr);
0098 
0099     /**
0100      * Destroys the collection properties page.
0101      */
0102     ~CollectionPropertiesPage() override;
0103 
0104     /**
0105      * Loads the page content from the given collection.
0106      *
0107      * @param collection The collection to load.
0108      */
0109     virtual void load(const Collection &collection) = 0;
0110 
0111     /**
0112      * Saves page content to the given collection.
0113      *
0114      * @param collection Reference to the collection to save to.
0115      */
0116     virtual void save(Collection &collection) = 0;
0117 
0118     /**
0119      * Checks if this page can actually handle the given collection.
0120      *
0121      * Returns @c true if the collection can be handled, @c false otherwise
0122      * The default implementation returns always @c true. When @c false is returned
0123      * this page is not shown in the properties dialog.
0124      * @param collection The collection to check.
0125      */
0126     virtual bool canHandle(const Collection &collection) const;
0127 
0128     /**
0129      * Sets the page title.
0130      *
0131      * @param title Translated, preferably short tab title.
0132      */
0133     void setPageTitle(const QString &title);
0134 
0135     /**
0136      * Returns the page title.
0137      */
0138     [[nodiscard]] QString pageTitle() const;
0139 
0140 private:
0141     /// @cond PRIVATE
0142     std::unique_ptr<CollectionPropertiesPagePrivate> const d;
0143     /// @endcond
0144 };
0145 
0146 /**
0147  * @short A factory class for collection properties dialog pages.
0148  *
0149  * The factory encapsulates the creation of the collection properties
0150  * dialog page.
0151  * You can use the AKONADI_COLLECTION_PROPERTIES_PAGE_FACTORY macro
0152  * to create a factory class automatically.
0153  *
0154  * @author Volker Krause <vkrause@kde.org>
0155  */
0156 class AKONADIWIDGETS_EXPORT CollectionPropertiesPageFactory
0157 {
0158 public:
0159     /**
0160      * Destroys the collection properties page factory.
0161      */
0162     virtual ~CollectionPropertiesPageFactory();
0163 
0164     /**
0165      * Returns the actual page widget.
0166      *
0167      * @param parent The parent widget.
0168      */
0169     virtual CollectionPropertiesPage *createWidget(QWidget *parent = nullptr) const = 0;
0170 
0171 protected:
0172     explicit CollectionPropertiesPageFactory() = default;
0173 
0174 private:
0175     Q_DISABLE_COPY_MOVE(CollectionPropertiesPageFactory)
0176 };
0177 
0178 /**
0179  * @def AKONADI_COLLECTION_PROPERTIES_PAGE_FACTORY
0180  *
0181  * The AKONADI_COLLECTION_PROPERTIES_PAGE_FACTORY macro can be used to
0182  * create a factory for a custom collection properties page.
0183  *
0184  * @code
0185  *
0186  * class MyPage : public Akonadi::CollectionPropertiesPage
0187  * {
0188  *   ...
0189  * }
0190  *
0191  * AKONADI_COLLECTION_PROPERTIES_PAGE_FACTORY( MyPageFactory, MyPage )
0192  *
0193  * @endcode
0194  *
0195  * The macro takes two arguments, where the first one is the name of the
0196  * factory class that shall be created and the second arguments is the name
0197  * of the custom collection properties page class.
0198  *
0199  * @ingroup AkonadiMacros
0200  */
0201 #define AKONADI_COLLECTION_PROPERTIES_PAGE_FACTORY(factoryName, className)                                                                                     \
0202     class factoryName : public Akonadi::CollectionPropertiesPageFactory                                                                                        \
0203     {                                                                                                                                                          \
0204     public:                                                                                                                                                    \
0205         inline Akonadi::CollectionPropertiesPage *createWidget(QWidget *parent = nullptr) const override                                                       \
0206         {                                                                                                                                                      \
0207             return new className(parent);                                                                                                                      \
0208         }                                                                                                                                                      \
0209     };
0210 
0211 }