1 /***************************************************************************
2  *   Copyright (C) 2003 by Sébastien Laoût                                 *
3  *   slaout@linux62.org                                                    *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 
21 #ifndef LINKLABEL_H
22 #define LINKLABEL_H
23 
24 #include <QFrame>
25 
26 class QPixmap;
27 class QString;
28 class QUrl;
29 class QColor;
30 class QLabel;
31 class QBoxLayout;
32 class QSpacerItem;
33 class QPushButton;
34 class QCheckBox;
35 class QEvent;
36 
37 class KComboBox;
38 class KColorCombo2;
39 class IconSizeCombo;
40 
41 class HTMLExporter;
42 class HelpLabel;
43 class KCModule;
44 
45 /** Store the style of links
46   * @author Sébastien Laoût
47   */
48 class LinkLook
49 {
50 public:
51     enum Underlining { Always = 0, Never, OnMouseHover, OnMouseOutside };
52     enum Preview { None = 0, IconSize, TwiceIconSize, ThreeIconSize };
53     explicit LinkLook(bool useLinkColor = true, bool canPreview = true);
54     LinkLook(const LinkLook &other);
55     void setLook(bool italic, bool bold, int underlining,
56                  QColor color, QColor hoverColor,
57                  int iconSize, int preview /*= None*/);
italic()58     inline bool   italic()       const {
59         return m_italic;
60     }
bold()61     inline bool   bold()         const {
62         return m_bold;
63     }
underlining()64     inline int    underlining()  const {
65         return m_underlining;
66     }
color()67     inline QColor color()        const {
68         return m_color;
69     }
hoverColor()70     inline QColor hoverColor()   const {
71         return m_hoverColor;
72     }
iconSize()73     inline int    iconSize()     const {
74         return m_iconSize;
75     }
preview()76     inline int    preview()      const {
77         return m_preview;
78     }
useLinkColor()79     inline bool   useLinkColor() const {
80         return m_useLinkColor;
81     }
canPreview()82     inline bool   canPreview()   const {
83         return m_canPreview;
84     }
85     /* Helpping Functions */
underlineOutside()86     bool underlineOutside() const {
87         return underlining() == Always || underlining() == OnMouseOutside;
88     }
underlineInside()89     bool underlineInside()  const {
90         return underlining() == Always || underlining() == OnMouseHover;
91     }
previewEnabled()92     bool previewEnabled()   const {
93         return canPreview() && preview() > None;
94     }
95     int  previewSize() const;
96     QColor effectiveColor() const;
97     QColor effectiveHoverColor() const;
98     QColor defaultColor() const;
99     QColor defaultHoverColor() const;
100     QString toCSS(const QString &cssClass, const QColor &defaultTextColor) const;
101 private:
102     bool   m_italic;
103     bool   m_bold;
104     int    m_underlining;
105     QColor m_color;
106     QColor m_hoverColor;
107     int    m_iconSize;
108     int    m_preview;
109     bool   m_useLinkColor;
110     bool   m_canPreview;
111 public:
112     /* Global Looks */
113     static LinkLook *soundLook;
114     static LinkLook *fileLook;
115     static LinkLook *localLinkLook;
116     static LinkLook *networkLinkLook;
117     static LinkLook *launcherLook;
118     static LinkLook *crossReferenceLook;
119     /* Static method to get a LinkLook from an URL */
120     static LinkLook* lookForURL(const QUrl &url);
121 };
122 
123 /** Used to represent links with icon and specific look
124   * Note : This label will appear blank while LinkLook willn't be set
125   * @author Sébastien Laoût
126   */
127 class LinkLabel : public QFrame
128 {
129     Q_OBJECT
130 public:
131     LinkLabel(int hAlign, int vAlign, QWidget *parent = 0, Qt::WindowFlags f = 0);
132     LinkLabel(const QString &title, const QString &icon, LinkLook *look, int hAlign, int vAlign,
133               QWidget *parent = 0, Qt::WindowFlags f = 0);
134     ~LinkLabel();
135 public:
136     void setLink(const QString &title, const QString &icon, LinkLook *look = 0);
137     void setLook(LinkLook *look);
138     void setAlign(int hAlign, int vAlign);
139     void setSelected(bool selected);
140     void setPaletteBackgroundColor(const QColor &color);
141     int  heightForWidth(int w = -1) const;
142 protected:
143     void initLabel(int hAlign, int vAlign);
144     void enterEvent(QEvent*);
145     void leaveEvent(QEvent*);
146 private:
147     QBoxLayout  *m_layout;
148     QLabel      *m_icon;
149     QLabel      *m_title;
150     QSpacerItem *m_spacer1;
151     QSpacerItem *m_spacer2;
152 
153     bool         m_isSelected;
154     bool         m_isHovered;
155 
156     LinkLook    *m_look;
157     int          m_hAlign;
158     int          m_vAlign;
159 };
160 
161 /** THE NEW CLASS TO DISPLAY Links FOR THE NEW BASKET ENGINE.
162  * We should get ride of class LinkLabel soon.
163  * And LinkLabel will be entirely rewritten to use this LinkDisplay as the drawing primitives.
164  * @author Sébastien Laoût
165  */
166 class LinkDisplay
167 {
168 public:
169     LinkDisplay();                                                               /// << Create a new empty unselected LinkDisplay. Please then call setLink() to make sense.
170     // Configure the link displayer:
171     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)
172     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)
173     void    setWidth(qreal width);                                                 /// << Set a new width. @see height() that will be computed.
174     // Get its properties:
minWidth()175     qreal     minWidth() const {
176         return m_minWidth;
177     }                              /// << @return the minimum width to display this link.
maxWidth()178     qreal     maxWidth() const {
179         return m_maxWidth;
180     }                              /// << @return the maximum width to display this link.
width()181     qreal     width()    const {
182         return m_width;
183     }                              /// << @return the width of the link. It is never less than minWidth()!
height()184     qreal     height()   const {
185         return m_height;
186     }                              /// << @return the height if the link after having set it a width.
187     // And finaly, use it:
188     void    paint(QPainter *painter, qreal x, qreal y, qreal width, qreal height, const QPalette &palette, bool isDefaultColor, bool isSelected, bool isHovered, bool isIconButtonHovered) const; /// << Draw the link on a painter. Set textColor to be !isValid() to use the LinkLook color. Otherwise it will use this color!
189     QPixmap feedbackPixmap(qreal width, qreal height, const QPalette &palette, bool isDefaultColor); /// << @return the pixmap to put under the cursor while dragging this object.
190     // Eventually get some information about the link display:
191     bool    iconButtonAt(const QPointF &pos) const;                               /// << @return true if the icon button is under point @p pos.
192     QRectF   iconButtonRect() const;                                              /// << @return the rectangle of the icon button.
193     // Utility function:
194     QFont   labelFont(QFont font, bool isIconButtonHovered) const;               /// << @return the font for this link, according to parent font AND LinkLook!
195     qreal     heightForWidth(qreal width) const;                                     /// << @return the needed height to display the link in function of a width.
196     QString toHtml(const QString &imageName) const;                              /// << Convert the link to HTML code, using the LinkLook to style it.
197     QString toHtml(HTMLExporter *exporter, const QUrl &url, const QString &title = "");
198 private:
199     QString   m_title;
200     QString   m_icon;
201     QPixmap   m_preview;
202     LinkLook *m_look;
203     QFont     m_font;
204     qreal       m_minWidth;
205     qreal       m_maxWidth;
206     qreal       m_width;
207     qreal       m_height;
208 };
209 
210 /** A widget to edit a LinkLook, showing a live example to the user.
211   * @author Sébastien Laoût
212   */
213 class LinkLookEditWidget : public QWidget
214 {
215     Q_OBJECT
216 public:
217     LinkLookEditWidget(KCModule* module, const QString exTitle, const QString exIcon,
218                        QWidget *parent = 0, Qt::WindowFlags fl = 0);
219     ~LinkLookEditWidget();
220     void saveChanges();
221     void saveToLook(LinkLook *look);
222     void set(LinkLook *look);
223 private slots:
224     void slotChangeLook();
225 protected:
226     LinkLook      *m_look;
227     QCheckBox     *m_italic;
228     QCheckBox     *m_bold;
229     KComboBox     *m_underlining;
230     KColorCombo2  *m_color;
231     KColorCombo2  *m_hoverColor;
232     IconSizeCombo *m_iconSize;
233     KComboBox     *m_preview;
234     LinkLook      *m_exLook;
235     LinkLabel     *m_example;
236     QString        m_exTitle;
237     QString        m_exIcon;
238     HelpLabel     *m_hLabel;
239     QLabel        *m_label;
240 };
241 
242 #endif // LINKLABEL_H
243