File indexing completed on 2024-05-12 04:06:10

0001 /*
0002     This file is part of the KDE games library
0003     SPDX-FileCopyrightText: 2001 Andreas Beckermann <b_mann@gmx.de>
0004     SPDX-FileCopyrightText: 2007 Gael de Chalendar (aka Kleag) <kleag@free.fr>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-only
0007 */
0008 
0009 #ifndef __KCHATBASEMODEL_H__
0010 #define __KCHATBASEMODEL_H__
0011 
0012 // own
0013 #include "kdegamesprivate_export.h"
0014 // Qt
0015 #include <QAbstractListModel>
0016 #include <QPair>
0017 #include <QSharedDataPointer>
0018 // Std
0019 #include <memory>
0020 
0021 class KChatBaseModelPrivate;
0022 class KChatBaseMessagePrivate;
0023 class KConfig;
0024 
0025 /**
0026  * \class KChatBaseMessage kchatbasemodel.h <KChatBaseModel>
0027  *
0028  * @short The class of the elements stored in the chat list model
0029  *
0030  * It's a pair of strings where the first element is the sender name and the
0031  * second one is the actual message. It furthermore indicates the type of the
0032  * message: normal or system
0033  */
0034 class KDEGAMESPRIVATE_EXPORT KChatBaseMessage : public QPair<QString, QString>
0035 {
0036 public:
0037     /** The different types of messages */
0038     enum MessageType { Normal, System };
0039 
0040     /** Default constructor. Necessary for Qt metatypes */
0041     KChatBaseMessage();
0042 
0043     /** Initializing constructor */
0044     KChatBaseMessage(const QString &sender, const QString &message, MessageType type = Normal);
0045 
0046     /** Copy constructor. Necessary for Qt metatypes */
0047     KChatBaseMessage(const KChatBaseMessage &m);
0048 
0049     KChatBaseMessage &operator=(const KChatBaseMessage &m);
0050 
0051     /** Default destructor */
0052     virtual ~KChatBaseMessage();
0053 
0054 private:
0055     QSharedDataPointer<KChatBaseMessagePrivate> d;
0056 };
0057 Q_DECLARE_METATYPE(KChatBaseMessage)
0058 
0059 /**
0060  * \class KChatBaseModel kchatbasemodel.h <KChatBaseModel>
0061  *
0062  * The model used to store messages displayed in the chat dialog messages
0063  * list. This is a list model and thus derived from @ref QAbstractListModel
0064  * and implementing its abstract API.
0065  */
0066 class KDEGAMESPRIVATE_EXPORT KChatBaseModel : public QAbstractListModel
0067 {
0068     Q_OBJECT
0069 
0070 public:
0071     /** Default constructor */
0072     explicit KChatBaseModel(QObject *parent = nullptr);
0073 
0074     /** Default destructor */
0075     ~KChatBaseModel() override;
0076 
0077     /**
0078      * Reimplementation of the inherited method.
0079      * @return The current number of messages in the list
0080      */
0081     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0082 
0083     /**
0084      * Reimplementation of the inherited method.
0085      * @return The KChatBaseMessage at the given index as a QVariant
0086      */
0087     QVariant data(const QModelIndex &index, int role) const override;
0088 
0089     /**
0090      * Set the font that is used for the name part of a message. See also
0091      * nameFont and setBothFont
0092      */
0093     void setNameFont(const QFont &font);
0094 
0095     /**
0096      * Set the font that is used for the message part of a message.
0097      * @see messageFont, setBothFont
0098      */
0099     void setMessageFont(const QFont &font);
0100 
0101     /**
0102      * This sets both - nameFont and messageFont to font. You
0103      * probably want to use this if you don't wish to distinguish between
0104      * these parts of a message.
0105      * @param font A font used for both nameFont and messageFont
0106      */
0107     void setBothFont(const QFont &font);
0108 
0109     /**
0110      * Same as setNameFont but applies only to system messages.
0111      */
0112     void setSystemNameFont(const QFont &font);
0113 
0114     /**
0115      * Same as setMessageFont but applies only to system messages.
0116      */
0117     void setSystemMessageFont(const QFont &font);
0118 
0119     /**
0120      * Same as setBothFont but applies only to system messages.
0121      */
0122     void setSystemBothFont(const QFont &font);
0123 
0124     /**
0125      * This font should be used for the name (the "from: " part) of a
0126      * message. layoutMessage uses this to set the font using
0127      * KChatBaseItemDelegate::setNameFont but if you want to overwrite
0128      * layoutMessage you should do this yourself.
0129      * @return The font that is used for the name part of the message.
0130      */
0131     const QFont &nameFont() const;
0132 
0133     /**
0134      * This font should be used for a message. layoutMessage sets the
0135      * font of a message using KChatBaseItemDelegate::setMessageFont but if you
0136      * replace layoutMessage with your own function you should use
0137      * messageFont() yourself.
0138      * @return The font that is used for a message
0139      */
0140     const QFont &messageFont() const;
0141 
0142     /**
0143      * Same as systemNameFont but applies only to system messages.
0144      */
0145     const QFont &systemNameFont() const;
0146 
0147     /**
0148      * Same as systemMessageFont but applies only to system messages.
0149      */
0150     const QFont &systemMessageFont() const;
0151 
0152     /**
0153      * Save the configuration of the dialog to a KConfig object. If
0154      * the supplied KConfig pointer is NULL then KGlobal::config() is used
0155      * instead (and the group is changed to "KChatBase") butr the current
0156      * group is restored at the end.
0157      * @param conf A pointer to the KConfig object to save the config
0158      * to. If you use 0 then KGlobal::config() is used and the group is changed
0159      * to "KChatBase" (the current group is restored at the end).
0160      */
0161     virtual void saveConfig(KConfig *conf = nullptr);
0162 
0163     /**
0164      * Read the configuration from a KConfig object. If the pointer is
0165      * NULL KGlobal::config() is used and the group is changed to "KChatBase".
0166      * The current KConfig::group is restored after this call.
0167      */
0168     virtual void readConfig(KConfig *conf = nullptr);
0169 
0170     /**
0171      * Set the maximum number of items in the list. If the number of item
0172      * exceeds the maximum as many items are deleted (oldest first) as
0173      * necessary. The number of items will never exceed this value.
0174      * @param maxItems the maximum number of items. -1 (default) for
0175      * unlimited.
0176      */
0177     void setMaxItems(int maxItems);
0178 
0179     /**
0180      * Clear all messages in the list.
0181      */
0182     void clear();
0183 
0184     /**
0185      * @return The maximum number of messages in the list. -1 is unlimited. See also
0186      * setMaxItems
0187      */
0188     int maxItems() const;
0189 
0190 public Q_SLOTS:
0191     /**
0192      * Add a text in the listbox. See also signalSendMessage()
0193      *
0194      * Maybe you want to replace this with a function that creates a nicer text
0195      * than "fromName: text"
0196      *
0197      * Update: the function layoutMessage is called by this now. This
0198      * means that you will get user defined outlook on the messages :-)
0199      * @param fromName The player who sent this message
0200      * @param text The text to be added
0201      */
0202     virtual void addMessage(const QString &fromName, const QString &text);
0203 
0204     /**
0205      * This works just like addMessage but adds a system message. System
0206      * messages will have a different look than player messages.
0207      *
0208      * You may wish to  use this to display status information from your game.
0209      */
0210     virtual void addSystemMessage(const QString &fromName, const QString &text);
0211 
0212     /**
0213      * This clears all messages in the view. Note that only the messages are
0214      * cleared, not the sender names in the combo box!
0215      */
0216     void slotClear();
0217 
0218 private:
0219     std::unique_ptr<KChatBaseModelPrivate> const d;
0220 };
0221 
0222 #endif