File indexing completed on 2019-07-28 10:35:11

0001 /*
0002 This file is part of Choqok, the KDE micro-blogging client
0003 
0004 Copyright (C) 2008-2012 Mehrdad Momeny <mehrdad.momeny@gmail.com>
0005 
0006 This program is free software; you can redistribute it and/or
0007 modify it under the terms of the GNU General Public License as
0008 published by the Free Software Foundation; either version 2 of
0009 the License or (at your option) version 3 or any later version
0010 accepted by the membership of KDE e.V. (or its successor approved
0011 by the membership of KDE e.V.), which shall act as a proxy
0012 defined in Section 14 of version 3 of the license.
0013 
0014 This program is distributed in the hope that it will be useful,
0015 but WITHOUT ANY WARRANTY; without even the implied warranty of
0016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0017 GNU General Public License for more details.
0018 
0019 You should have received a copy of the GNU General Public License
0020 along with this program; if not, see http://www.gnu.org/licenses/
0021 */
0022 #ifndef POSTWIDGET_H
0023 #define POSTWIDGET_H
0024 
0025 #include <QDateTime>
0026 
0027 #include "account.h"
0028 #include "choqoktypes.h"
0029 #include "microblog.h"
0030 
0031 class QAction;
0032 class QPushButton;
0033 
0034 namespace Choqok
0035 {
0036 namespace UI
0037 {
0038 
0039 class TextBrowser;
0040 
0041 /**
0042 Post Widget!
0043 Attribute "Qt::WA_DeleteOnClose" is enabled at construtor! So please use close() for deleting an object, instead of deleteLater() or delete
0044 
0045 @author Mehrdad Momeny \<mehrdad.momeny@gmail.com\>
0046 */
0047 class CHOQOK_EXPORT PostWidget : public QWidget
0048 {
0049     Q_OBJECT
0050     Q_PROPERTY(bool read READ isRead)
0051 public:
0052     explicit PostWidget(Choqok::Account *account, Choqok::Post *post, QWidget *parent = nullptr);
0053     virtual ~PostWidget();
0054     Post *currentPost() const;
0055     virtual void setRead(bool read = true);
0056 
0057     /**
0058      Sets post widget as read, and emits postReaded() signal
0059      */
0060     void setReadWithSignal();
0061 
0062     virtual bool isRead() const;
0063 
0064     Account *currentAccount();
0065     /**
0066     Setup UI elements
0067     @Note Should call from outside of class, To initialize drived class items too!
0068     */
0069     virtual void initUi();
0070 
0071     /**
0072     Set stylesheet data with new color data! to use later.
0073 
0074     @see setUiStyle()
0075     */
0076     static void setStyle(const QColor &unreadColor, const QColor &unreadBack,
0077                          const QColor &readColor, const QColor &readBack,
0078                          const QColor &ownColor, const QColor &ownBack,
0079                          const QFont &font);
0080 
0081     /**
0082     @brief Set current post
0083 
0084     @note Use with care!
0085 
0086     After changing current post, Don't forget to call @ref initUi() to update post UI.
0087     */
0088     void setCurrentPost(Post *post);
0089 
0090     /**
0091     @brief Sets Post sign
0092     sign is the text that showed as sign of this post under post content.
0093     */
0094     void setSign(const QString &sign);
0095 
0096     /**
0097     @return post sign
0098     sign is an html text that showed as sign of this post under post content.
0099     */
0100     QString sign() const;
0101 
0102     /**
0103     @brief Sets post content
0104     Post content is an html text that showed as post text.
0105     */
0106     void setContent(const QString &content);
0107 
0108     /**
0109     @return post content
0110     Post content is an html text that showed as post text.
0111     */
0112     QString content() const;
0113 
0114     void deleteLater();
0115 
0116     TextBrowser *mainWidget();
0117 
0118     QStringList urls();
0119 
0120     TimelineWidget *timelineWidget() const;
0121 
0122     /**
0123      * Plugins can add status specific actions and process them internally
0124      *
0125      */
0126     static void addAction(QAction *action);
0127 
0128     static QString getBaseStyle();
0129 
0130 public Q_SLOTS:
0131     /**
0132     Set Style sheet of widget to corresponding data->
0133     @see setStyle()
0134     */
0135     void setUiStyle();
0136 
0137 Q_SIGNALS:
0138     /**
0139     Emit and contain text to resend.
0140     */
0141     void resendPost(const QString &text);
0142     /**
0143     Emit when this post has been readed by pressing mouse on it, And to notify TimelineWidget about it.
0144     */
0145     void postReaded();
0146     /**
0147     Carry reply information, to reply to a post.
0148     */
0149     void reply(const QString &txt, const QString &replyToId, const QString &replyToUsername);
0150 
0151     /**
0152     Emitted when this widget is about to close!
0153     postId and this returned!
0154     */
0155     void aboutClosing(const QString &postId, PostWidget *widget);
0156 
0157 protected Q_SLOTS:
0158 
0159     virtual void checkAnchor(const QUrl &url);
0160     /**
0161     Set height of widget related to text contents
0162     */
0163     virtual void setHeight();
0164 
0165     /**
0166     Update UI after changes, such as timestamp
0167     */
0168     virtual void updateUi();
0169 
0170     /**
0171     Call microblog() to remove this post!
0172     */
0173     virtual void removeCurrentPost();
0174     /**
0175     Prepare text to send for resending this post.
0176     */
0177     virtual void slotResendPost();
0178     /**
0179     Internal slot to remove/close/destroy this post after bing deleted
0180     */
0181     void slotCurrentPostRemoved(Choqok::Account *theAccount, Choqok::Post *post);
0182 
0183     virtual void slotPostError(Choqok::Account *theAccount, Choqok::Post *post,
0184                                Choqok::MicroBlog::ErrorType error, const QString &errorMessage);
0185 
0186     void avatarFetchError(const QUrl &remoteUrl, const QString &errMsg);
0187     void avatarFetched(const QUrl &remoteUrl, const QPixmap &pixmap);
0188 
0189     void slotImageFetched(const QUrl &remoteUrl, const QPixmap &pixmap);
0190     virtual void mousePressEvent(QMouseEvent *ev) override;
0191 
0192 protected:
0193     virtual void setupUi();
0194     virtual void closeEvent(QCloseEvent *event) override;
0195     virtual void setupAvatar();
0196     virtual void fetchImage();
0197     virtual void wheelEvent(QWheelEvent *) override;
0198     virtual void resizeEvent(QResizeEvent *event) override;
0199     virtual void enterEvent(QEvent *event) override;
0200     virtual void leaveEvent(QEvent *event) override;
0201     virtual QString prepareStatus(const QString &text);
0202     QLatin1String getDirection(QString text);
0203     virtual QString generateSign();
0204     virtual QString formatDateTime(const QDateTime &time);
0205     virtual bool isResendAvailable() ;
0206     virtual bool isRemoveAvailable() ;
0207     virtual bool isOwnPost();
0208     virtual QString removeTags(const QString &text) const;
0209     /**
0210     @brief Create and Add a new button to widget
0211     This function will add button to UI!
0212     @return added button, for some managements such as connect to a slot
0213     */
0214     QPushButton *addButton(const QString &objName, const QString &toolTip, const QString &icon);
0215     QPushButton *addButton(const QString &objName, const QString &toolTip, const QIcon &icon);
0216     QMap<QString, QPushButton *> &buttons();
0217 
0218     QString getUsernameHyperlink(const Choqok::User &user) const;
0219 
0220 protected:
0221     TextBrowser *_mainWidget;
0222     static const QString baseStyle;
0223     static QString readStyle;
0224     static QString unreadStyle;
0225     static QString ownStyle;
0226     static const QString webIconText;
0227     static const QString hrefTemplate;
0228     static const QString baseTextTemplate;
0229     static const QRegExp dirRegExp;
0230 
0231     void setAvatarText(const QString &text);
0232     QString avatarText() const;
0233     void setExtraContents(const QString &text);
0234     QString extraContents() const;
0235     virtual QString generateResendText();
0236     void updatePostImage(int width);
0237 
0238 private:
0239     class Private;
0240     Private *const d;
0241 };
0242 
0243 class CHOQOK_EXPORT PostWidgetUserData : public QObjectUserData
0244 {
0245 public:
0246     PostWidgetUserData(PostWidget *postWidget);
0247     virtual ~PostWidgetUserData();
0248     PostWidget *postWidget();
0249     void setPostWidget(PostWidget *widget);
0250 
0251 private:
0252     class Private;
0253     Private *const d;
0254 };
0255 
0256 }
0257 }
0258 #endif // POSTWIDGET_H