File indexing completed on 2022-09-27 16:29:17

0001 /**
0002  * SPDX-FileCopyrightText: (C) 2003 Sébastien Laoût <slaout@linux62.org>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #ifndef LINKLABEL_H
0008 #define LINKLABEL_H
0009 
0010 #include <QFrame>
0011 
0012 class QPixmap;
0013 class QString;
0014 class QUrl;
0015 class QColor;
0016 class QLabel;
0017 class QBoxLayout;
0018 class QSpacerItem;
0019 class QPushButton;
0020 class QCheckBox;
0021 class QEvent;
0022 
0023 class KComboBox;
0024 class KColorCombo2;
0025 class IconSizeCombo;
0026 
0027 class HTMLExporter;
0028 class HelpLabel;
0029 class KCModule;
0030 
0031 /** Store the style of links
0032  * @author Sébastien Laoût
0033  */
0034 class LinkLook
0035 {
0036 public:
0037     enum Underlining { Always = 0, Never, OnMouseHover, OnMouseOutside };
0038     enum Preview { None = 0, IconSize, TwiceIconSize, ThreeIconSize };
0039     explicit LinkLook(bool useLinkColor = true, bool canPreview = true);
0040     LinkLook(const LinkLook &other);
0041     void setLook(bool italic, bool bold, int underlining, QColor color, QColor hoverColor, int iconSize, int preview /*= None*/);
0042     inline bool italic() const
0043     {
0044         return m_italic;
0045     }
0046     inline bool bold() const
0047     {
0048         return m_bold;
0049     }
0050     inline int underlining() const
0051     {
0052         return m_underlining;
0053     }
0054     inline QColor color() const
0055     {
0056         return m_color;
0057     }
0058     inline QColor hoverColor() const
0059     {
0060         return m_hoverColor;
0061     }
0062     inline int iconSize() const
0063     {
0064         return m_iconSize;
0065     }
0066     inline int preview() const
0067     {
0068         return m_preview;
0069     }
0070     inline bool useLinkColor() const
0071     {
0072         return m_useLinkColor;
0073     }
0074     inline bool canPreview() const
0075     {
0076         return m_canPreview;
0077     }
0078     /* Helping Functions */
0079     bool underlineOutside() const
0080     {
0081         return underlining() == Always || underlining() == OnMouseOutside;
0082     }
0083     bool underlineInside() const
0084     {
0085         return underlining() == Always || underlining() == OnMouseHover;
0086     }
0087     bool previewEnabled() const
0088     {
0089         return canPreview() && preview() > None;
0090     }
0091     int previewSize() const;
0092     QColor effectiveColor() const;
0093     QColor effectiveHoverColor() const;
0094     QColor defaultColor() const;
0095     QColor defaultHoverColor() const;
0096     QString toCSS(const QString &cssClass, const QColor &defaultTextColor) const;
0097 
0098 private:
0099     bool m_italic;
0100     bool m_bold;
0101     int m_underlining;
0102     QColor m_color;
0103     QColor m_hoverColor;
0104     int m_iconSize;
0105     int m_preview;
0106     bool m_useLinkColor;
0107     bool m_canPreview;
0108 
0109 public:
0110     /* Global Looks */
0111     static LinkLook *soundLook;
0112     static LinkLook *fileLook;
0113     static LinkLook *localLinkLook;
0114     static LinkLook *networkLinkLook;
0115     static LinkLook *launcherLook;
0116     static LinkLook *crossReferenceLook;
0117     /* Static method to get a LinkLook from an URL */
0118     static LinkLook *lookForURL(const QUrl &url);
0119 };
0120 
0121 /** Used to represent links with icon and specific look
0122  * Note : This label will appear blank while LinkLook willn't be set
0123  * @author Sébastien Laoût
0124  */
0125 class LinkLabel : public QFrame
0126 {
0127     Q_OBJECT
0128 public:
0129     LinkLabel(int hAlign, int vAlign, QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
0130     LinkLabel(const QString &title, const QString &icon, LinkLook *look, int hAlign, int vAlign, QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
0131     ~LinkLabel() override;
0132 
0133 public:
0134     void setLink(const QString &title, const QString &icon, LinkLook *look = nullptr);
0135     void setLook(LinkLook *look);
0136     void setAlign(int hAlign, int vAlign);
0137     void setSelected(bool selected);
0138     void setPaletteBackgroundColor(const QColor &color);
0139     int heightForWidth(int w = -1) const override;
0140 
0141 protected:
0142     void initLabel(int hAlign, int vAlign);
0143     void enterEvent(QEvent *) override;
0144     void leaveEvent(QEvent *) override;
0145 
0146 private:
0147     QBoxLayout *m_layout;
0148     QLabel *m_icon;
0149     QLabel *m_title;
0150     QSpacerItem *m_spacer1;
0151     QSpacerItem *m_spacer2;
0152 
0153     bool m_isSelected;
0154     bool m_isHovered;
0155 
0156     LinkLook *m_look;
0157     int m_hAlign;
0158     int m_vAlign;
0159 };
0160 
0161 /** THE NEW CLASS TO DISPLAY Links FOR THE NEW BASKET ENGINE.
0162  * We should get ride of class LinkLabel soon.
0163  * And LinkLabel will be entirely rewritten to use this LinkDisplay as the drawing primitives.
0164  * @author Sébastien Laoût
0165  */
0166 class LinkDisplay
0167 {
0168 public:
0169     LinkDisplay(); /// << Create a new empty unselected LinkDisplay. Please then call setLink() to make sense.
0170     // Configure the link displayer:
0171     void setLink(const QString &title, const QString &icon, LinkLook *look, const QFont &font); /// << Change the content and disposition. minWidth(), width() & height() can have changed. Keep the old preview (if any)
0172     void setLink(const QString &title, const QString &icon, const QPixmap &preview, LinkLook *look, const QFont &font); /// << Idem but change the preview too (or remove it if it is invalid)
0173     void setWidth(qreal width);                                                                                         /// << Set a new width. @see height() that will be computed.
0174     // Get its properties:
0175     qreal minWidth() const
0176     {
0177         return m_minWidth;
0178     } /// << @return the minimum width to display this link.
0179     qreal maxWidth() const
0180     {
0181         return m_maxWidth;
0182     } /// << @return the maximum width to display this link.
0183     qreal width() const
0184     {
0185         return m_width;
0186     } /// << @return the width of the link. It is never less than minWidth()!
0187     qreal height() const
0188     {
0189         return m_height;
0190     } /// << @return the height if the link after having set it a width.
0191     // And finally, use it:
0192     void paint(QPainter *painter, qreal x, qreal y, qreal width, qreal height, const QPalette &palette, bool isDefaultColor, bool isSelected, bool isHovered, bool isIconButtonHovered)
0193         const;                                                                                       /// << Draw the link on a painter. Set textColor to be !isValid() to use the LinkLook color. Otherwise it will use this color!
0194     QPixmap feedbackPixmap(qreal width, qreal height, const QPalette &palette, bool isDefaultColor); /// << @return the pixmap to put under the cursor while dragging this object.
0195     // Eventually get some information about the link display:
0196     bool iconButtonAt(const QPointF &pos) const; /// << @return true if the icon button is under point @p pos.
0197     QRectF iconButtonRect() const;               /// << @return the rectangle of the icon button.
0198     // Utility function:
0199     QFont labelFont(QFont font, bool isIconButtonHovered) const; /// << @return the font for this link, according to parent font AND LinkLook!
0200     qreal heightForWidth(qreal width) const;                     /// << @return the needed height to display the link in function of a width.
0201     QString toHtml(const QString &imageName) const;              /// << Convert the link to HTML code, using the LinkLook to style it.
0202     QString toHtml(HTMLExporter *exporter, const QUrl &url, const QString &title = QString());
0203 
0204 private:
0205     QString m_title;
0206     QString m_icon;
0207     QPixmap m_preview;
0208     LinkLook *m_look;
0209     QFont m_font;
0210     qreal m_minWidth;
0211     qreal m_maxWidth;
0212     qreal m_width;
0213     qreal m_height;
0214 };
0215 
0216 /** A widget to edit a LinkLook, showing a live example to the user.
0217  * @author Sébastien Laoût
0218  */
0219 class LinkLookEditWidget : public QWidget
0220 {
0221     Q_OBJECT
0222 public:
0223     LinkLookEditWidget(KCModule *module, const QString exTitle, const QString exIcon, QWidget *parent = nullptr, Qt::WindowFlags fl = Qt::WindowFlags());
0224     ~LinkLookEditWidget() override;
0225     void saveChanges();
0226     void saveToLook(LinkLook *look);
0227     void set(LinkLook *look);
0228 private Q_SLOTS:
0229     void slotChangeLook();
0230 
0231 protected:
0232     LinkLook *m_look;
0233     QCheckBox *m_italic;
0234     QCheckBox *m_bold;
0235     KComboBox *m_underlining;
0236     KColorCombo2 *m_color;
0237     KColorCombo2 *m_hoverColor;
0238     IconSizeCombo *m_iconSize;
0239     KComboBox *m_preview;
0240     LinkLook *m_exLook;
0241     LinkLabel *m_example;
0242     QString m_exTitle;
0243     QString m_exIcon;
0244     HelpLabel *m_hLabel;
0245     QLabel *m_label;
0246 };
0247 
0248 #endif // LINKLABEL_H