summaryrefslogtreecommitdiffstats
path: root/src/linklabel.h
blob: 3e964c1ce2f1198aefc008b097f045d31ed77c72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/***************************************************************************
 *   Copyright (C) 2003 by S�astien Laot                                 *
 *   slaout@linux62.org                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/

#ifndef LINKLABEL_H
#define LINKLABEL_H

#include <qframe.h>

class QString;
class KURL;
class QColor;
class QLabel;
class QBoxLayout;
class QSpacerItem;
class QPushButton;
class QCheckBox;
class QComboBox;

class KColorCombo2;
class IconSizeCombo;

class HTMLExporter;
class HelpLabel;
class KCModule;

/** Store the style of links
  * @author S�astien Laot
  */
class LinkLook
{
  public:
	enum Underlining { Always = 0, Never, OnMouseHover, OnMouseOutside };
	enum Preview { None = 0, IconSize, TwiceIconSize, ThreeIconSize };
	LinkLook(bool useLinkColor = true, bool canPreview = true);
	LinkLook(const LinkLook &other);
	void setLook( bool italic, bool bold, int underlining,
	              QColor color, QColor hoverColor,
	              int iconSize, int preview /*= None*/ );
	inline bool   italic()       const { return m_italic;       }
	inline bool   bold()         const { return m_bold;         }
	inline int    underlining()  const { return m_underlining;  }
	inline QColor color()        const { return m_color;        }
	inline QColor hoverColor()   const { return m_hoverColor;   }
	inline int    iconSize()     const { return m_iconSize;     }
	inline int    preview()      const { return m_preview;      }
	inline bool   useLinkColor() const { return m_useLinkColor; }
	inline bool   canPreview()   const { return m_canPreview;   }
	/* Helpping Functions */
	bool underlineOutside() const { return underlining() == Always || underlining() == OnMouseOutside; }
	bool underlineInside()  const { return underlining() == Always || underlining() == OnMouseHover;   }
	bool previewEnabled()   const { return canPreview() && preview() > None;                           }
	int  previewSize() const;
	QColor effectiveColor() const;
	QColor effectiveHoverColor() const;
	QColor defaultColor() const;
	QColor defaultHoverColor() const;
	QString toCSS(const QString &cssClass, const QColor &defaultTextColor) const;
  private:
	bool   m_italic;
	bool   m_bold;
	int    m_underlining;
	QColor m_color;
	QColor m_hoverColor;
	int    m_iconSize;
	int    m_preview;
	bool   m_useLinkColor;
	bool   m_canPreview;
  public:
	/* Global Looks */
	static LinkLook *soundLook;
	static LinkLook *fileLook;
	static LinkLook *localLinkLook;
	static LinkLook *networkLinkLook;
	static LinkLook *launcherLook;
	/* Static method to get a LinkLook from an URL */
	static LinkLook* lookForURL(const KURL &url);
};

/** Used to represent links with icon and specific look
  * Note : This label will appear blank while LinkLook willn't be set
  * @author S�astien Laot
  */
class LinkLabel : public QFrame
{
  Q_OBJECT
  public:
	LinkLabel(int hAlign, int vAlign, QWidget *parent = 0, const char *name = 0, WFlags f = 0);
	LinkLabel(const QString &title, const QString &icon, LinkLook *look, int hAlign, int vAlign,
	          QWidget *parent = 0, const char *name = 0, WFlags f = 0);
	~LinkLabel();
  public:
	void setLink(const QString &title, const QString &icon, LinkLook *look = 0);
	void setLook(LinkLook *look);
	void setAlign(int hAlign, int vAlign);
	void setSelected(bool selected);
	void setPaletteBackgroundColor(const QColor &color);
	int  heightForWidth(int w = -1) const;
	QString toHtml(const QString &imageName);
  protected:
	void initLabel(int hAlign, int vAlign);
	void enterEvent(QEvent*);
	void leaveEvent(QEvent*);
  private:
	QBoxLayout  *m_layout;
	QLabel      *m_icon;
	QLabel      *m_title;
	QSpacerItem *m_spacer1;
	QSpacerItem *m_spacer2;

	bool         m_isSelected;
	bool         m_isHovered;

	LinkLook    *m_look;
	int          m_hAlign;
	int          m_vAlign;
};

/** THE NEW CLASS TO DISPLAY Links FOR THE NEW BASKET ENGINE.
 * We should get ride of class LinkLabel soon.
 * And LinkLabel will be entirely rewritten to use this LinkDisplay as the drawing primitives.
 * @author S�astien Laot
 */
class LinkDisplay
{
  public:
	LinkDisplay();                                                               /// << Create a new empty unselected LinkDisplay. Please then call setLink() to make sense.
	// Configure the link displayer:
	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)
	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)
	void    setWidth(int width);                                                 /// << Set a new width. @see height() that will be computed.
	// Get its properties:
	int     minWidth() const { return m_minWidth; }                              /// << @return the minimum width to display this link.
	int     maxWidth() const { return m_maxWidth; }                              /// << @return the maximum width to display this link.
	int     width()    const { return m_width;    }                              /// << @return the width of the link. It is never less than minWidth()!
	int     height()   const { return m_height;   }                              /// << @return the height if the link after having set it a width.
	// And finaly, use it:
	void    paint(QPainter *painter, int x, int y, int width, int height, const QColorGroup &colorGroup, 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!
	QPixmap feedbackPixmap(int width, int height, const QColorGroup &colorGroup, bool isDefaultColor); /// << @return the pixmap to put under the cursor while dragging this object.
	// Eventually get some information about the link display:
	bool    iconButtonAt(const QPoint &pos) const;                               /// << @return true if the icon button is under point @p pos.
	QRect   iconButtonRect() const;                                              /// << @return the rectangle of the icon button.
	// Utility function:
	QFont   labelFont(QFont font, bool isIconButtonHovered) const;               /// << @return the font for this link, according to parent font AND LinkLook!
	int     heightForWidth(int width) const;                                     /// << @return the needed height to display the link in function of a width.
	QString toHtml(const QString &imageName) const;                              /// << Convert the link to HTML code, using the LinkLook to style it.
	QString toHtml(HTMLExporter *exporter, const KURL &url, const QString &title = "");
  private:
	QString   m_title;
	QString   m_icon;
	QPixmap   m_preview;
	LinkLook *m_look;
	QFont     m_font;
	int       m_minWidth;
	int       m_maxWidth;
	int       m_width;
	int       m_height;
};

/** A widget to edit a LinkLook, showing a live example to the user.
  * @author S�astien Laot
  */
class LinkLookEditWidget : public QWidget
{
  Q_OBJECT
  public:
	LinkLookEditWidget(KCModule* module, const QString exTitle, const QString exIcon,
					   QWidget *parent = 0, const char *name = 0, WFlags fl = 0);
	~LinkLookEditWidget();
	void saveChanges();
	void saveToLook(LinkLook *look);
	void set(LinkLook *look);
  private slots:
	void slotChangeLook();
  protected:
	LinkLook      *m_look;
	QCheckBox     *m_italic;
	QCheckBox     *m_bold;
	QComboBox     *m_underlining;
	KColorCombo2  *m_color;
	KColorCombo2  *m_hoverColor;
	IconSizeCombo *m_iconSize;
	QComboBox     *m_preview;
	LinkLook      *m_exLook;
	LinkLabel     *m_example;
	QString        m_exTitle;
	QString        m_exIcon;
	HelpLabel     *m_hLabel;
	QLabel        *m_label;
};

#endif // LINKLABEL_H