OSDN Git Service

abdc0f8c9db0161f06a847c39e95bc9e58496ccf
[qt-creator-jp/qt-creator-jp.git] / src / libs / utils / welcomemodetreewidget.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "welcomemodetreewidget.h"
35
36 #include <QtGui/QLabel>
37 #include <QtGui/QPixmap>
38 #include <QtGui/QAction>
39 #include <QtGui/QVBoxLayout>
40 #include <QtGui/QMouseEvent>
41 #include <QtGui/QResizeEvent>
42 #include <QtGui/QImage>
43
44 enum { leftContentsMargin = 2,
45        topContentsMargin = 2,
46        bottomContentsMargin = 1,
47        pixmapWidth = 24 };
48
49 /*!
50     \class Utils::WelcomeModeLabel
51     \brief Label usable for headers of a Welcome page.
52 */
53
54 namespace Utils {
55
56 WelcomeModeLabel::WelcomeModeLabel(QWidget *parent) :
57     QLabel(parent), m_unused(0)
58 {
59     // Bold/enlarged font slightly gray. Force color on by stylesheet as it is used
60     // as a child of widgets that have stylesheets.
61     QFont f = font();
62 #ifndef Q_OS_WIN
63     f.setWeight(QFont::DemiBold);
64 #endif
65     f.setPointSizeF(f.pointSizeF() * 1.2);
66     setFont(f);
67     setStyleSheet(QLatin1String("color : rgb(85, 85, 85);"));
68     setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop);
69 }
70
71 WelcomeModeLabel::~WelcomeModeLabel()
72 {
73 }
74
75 // NewsLabel for the WelcomeModeTreeWidget:
76 // Shows a news article as "Bold Title!\nElided Start of article...."
77 // Adapts the eliding when resizing.
78 class NewsLabel : public QLabel {
79 public:
80     explicit NewsLabel(const QString &htmlTitle,
81                        const QString &htmlText,
82                        QWidget *parent = 0);
83
84     virtual void resizeEvent(QResizeEvent *);
85
86 private:
87     const QString m_title;
88     const QString m_text;
89     const int m_minWidth;
90
91     int m_lastWidth;
92     int m_contentsMargin;
93 };
94
95 NewsLabel::NewsLabel(const QString &htmlTitle,
96                    const QString &htmlText,
97                    QWidget *parent) :
98     QLabel(parent),
99     m_title(htmlTitle),
100     m_text(htmlText),
101     m_minWidth(100),
102     m_lastWidth(-1)
103 {
104     int dummy, left, right;
105     getContentsMargins(&left, &dummy, &right, &dummy);
106     m_contentsMargin = left + right;
107 }
108
109 void NewsLabel::resizeEvent(QResizeEvent *e)
110 {
111     enum { epsilon = 10 };
112     QLabel::resizeEvent(e);
113     // Resized: Adapt to new size (if it is >  m_minWidth)
114     const int newWidth = qMax(e->size().width() - m_contentsMargin, m_minWidth) - epsilon;
115     if (newWidth == m_lastWidth)
116         return;
117     m_lastWidth = newWidth;
118     QFont f = font();
119     const QString elidedText = QFontMetrics(f).elidedText(m_text, Qt::ElideRight, newWidth);
120     f.setBold(true);
121     const QString elidedTitle = QFontMetrics(f).elidedText(m_title, Qt::ElideRight, newWidth);
122     QString labelText = QLatin1String("<b>");
123     labelText += elidedTitle;
124     labelText += QLatin1String("</b><br /><font color='gray'>");
125     labelText += elidedText;
126     labelText += QLatin1String("</font>");
127     setText(labelText);
128 }
129
130 // Item label of WelcomeModeTreeWidget: Horizontal widget consisting
131 // of arrow and clickable label.
132 class WelcomeModeItemWidget : public QWidget {
133     Q_OBJECT
134 public:
135     // Normal items
136     explicit WelcomeModeItemWidget(const QPixmap &pix,
137                                    const QString &text,
138                                    const QString &tooltip,
139                                    const QString &data,
140                                    QWidget *parent = 0);
141     // News items with title and start of article (see NewsLabel)
142     explicit WelcomeModeItemWidget(const QPixmap &pix,
143                                    QString htmlTitle,
144                                    QString htmlText,
145                                    const QString &tooltip,
146                                    const QString &data,
147                                    QWidget *parent = 0);
148
149 signals:
150     void clicked(const QString &);
151
152 protected:
153     virtual void mousePressEvent(QMouseEvent *);
154
155 private:
156     static inline void initLabel(QLabel *);
157
158     void init(const QPixmap &pix, QLabel *itemLabel, const QString &tooltip);
159
160     const QString m_data;
161 };
162
163 WelcomeModeItemWidget::WelcomeModeItemWidget(const QPixmap &pix,
164                                              const QString &text,
165                                              const QString &tooltipText,
166                                              const QString &data,
167                                              QWidget *parent) :
168     QWidget(parent),
169     m_data(data)
170 {
171     QLabel *label = new QLabel(text);
172     WelcomeModeItemWidget::initLabel(label);
173     init(pix, label, tooltipText);
174 }
175
176 static inline void fixHtml(QString &s)
177 {
178     s.replace(QLatin1String("&#8217;"), QString(QLatin1Char('\''))); // Quote
179     s.replace(QLatin1Char('\n'), QLatin1Char(' '));
180 }
181
182 WelcomeModeItemWidget::WelcomeModeItemWidget(const QPixmap &pix,
183                                              QString title,
184                                              QString text,
185                                              const QString &tooltipText,
186                                              const QString &data,
187                                              QWidget *parent) :
188     QWidget(parent),
189     m_data(data)
190 {
191     fixHtml(text);
192     fixHtml(title);
193     QLabel *newsLabel = new NewsLabel(title, text);
194     WelcomeModeItemWidget::initLabel(newsLabel);
195     init(pix, newsLabel, tooltipText);
196 }
197
198 void WelcomeModeItemWidget::initLabel(QLabel *label)
199 {
200     label->setTextInteractionFlags(Qt::NoTextInteraction);
201     label->setCursor(QCursor(Qt::PointingHandCursor));
202 }
203
204 void WelcomeModeItemWidget::init(const QPixmap &pix, QLabel *itemLabel,
205                                  const QString &tooltipText)
206 {
207     QHBoxLayout *hBoxLayout = new QHBoxLayout;
208     hBoxLayout->setContentsMargins(topContentsMargin, leftContentsMargin,
209                                    0, bottomContentsMargin);
210
211     QLabel *pxLabel = new QLabel;
212     QPixmap pixmap = pix;
213     if (layoutDirection() == Qt::RightToLeft){
214         QImage image = pixmap.toImage();
215         pixmap = QPixmap::fromImage(image.mirrored(1, 0));
216     }
217     pxLabel->setPixmap(pixmap);
218
219     pxLabel->setFixedWidth(pixmapWidth);
220     hBoxLayout->addWidget(pxLabel);
221
222     hBoxLayout->addWidget(itemLabel);
223     if (!tooltipText.isEmpty()) {
224         setToolTip(tooltipText);
225         pxLabel->setToolTip(tooltipText);
226         itemLabel->setToolTip(tooltipText);
227     }
228     setLayout(hBoxLayout);
229 }
230
231 void WelcomeModeItemWidget::mousePressEvent(QMouseEvent *e)
232 {
233     e->accept();
234     emit clicked(m_data);
235 }
236
237 // ----------------- WelcomeModeTreeWidget
238 struct WelcomeModeTreeWidgetPrivate
239 {
240     WelcomeModeTreeWidgetPrivate();
241
242     const QPixmap bullet;
243     QVBoxLayout *layout;
244     QVBoxLayout *itemLayout;
245 };
246
247 WelcomeModeTreeWidgetPrivate::WelcomeModeTreeWidgetPrivate() :
248     bullet(QLatin1String(":/welcome/images/list_bullet_arrow.png")),
249     layout(new QVBoxLayout),
250     itemLayout(new QVBoxLayout)
251 {
252     layout->setMargin(0);
253 }
254
255 /*!
256     \class Utils::WelcomeModeTreeWidget
257     \brief Show an itemized list with arrows and emits a signal on click.
258 */
259
260 WelcomeModeTreeWidget::WelcomeModeTreeWidget(QWidget *parent) :
261         QWidget(parent), m_d(new WelcomeModeTreeWidgetPrivate)
262 {
263     setLayout(m_d->layout);
264     m_d->layout->addLayout(m_d->itemLayout);
265     m_d->layout->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding));
266 }
267
268 WelcomeModeTreeWidget::~WelcomeModeTreeWidget()
269 {
270     delete m_d;
271 }
272
273 void WelcomeModeTreeWidget::addItem(const QString &label, const QString &data, const QString &toolTip)
274 {
275     addItemWidget(new WelcomeModeItemWidget(m_d->bullet, label, toolTip, data));
276 }
277
278 void WelcomeModeTreeWidget::addNewsItem(const QString &title,
279                                         const QString &description,
280                                         const QString &link)
281 {
282     addItemWidget(new WelcomeModeItemWidget(m_d->bullet, title, description, link, link));
283 }
284
285 void WelcomeModeTreeWidget::addItemWidget(WelcomeModeItemWidget *w)
286 {
287     connect(w, SIGNAL(clicked(QString)), this, SIGNAL(activated(QString)));
288     m_d->itemLayout->addWidget(w);
289 }
290
291 void WelcomeModeTreeWidget::clear()
292 {
293     for (int i = m_d->itemLayout->count() - 1; i >= 0; i--) {
294         QLayoutItem *item = m_d->itemLayout->takeAt(i);
295         delete item->widget();
296         delete item;
297     }
298 }
299
300 } // namespace Utils
301
302 #include "welcomemodetreewidget.moc"