1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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
16 ** GNU Lesser General Public License Usage
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.
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.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "welcomemodetreewidget.h"
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>
44 enum { leftContentsMargin = 2,
45 topContentsMargin = 2,
46 bottomContentsMargin = 1,
50 \class Utils::WelcomeModeLabel
51 \brief Label usable for headers of a Welcome page.
56 WelcomeModeLabel::WelcomeModeLabel(QWidget *parent) :
57 QLabel(parent), m_unused(0)
59 // Bold/enlarged font slightly gray. Force color on by stylesheet as it is used
60 // as a child of widgets that have stylesheets.
63 f.setWeight(QFont::DemiBold);
65 f.setPointSizeF(f.pointSizeF() * 1.2);
67 setStyleSheet(QLatin1String("color : rgb(85, 85, 85);"));
68 setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop);
71 WelcomeModeLabel::~WelcomeModeLabel()
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 {
80 explicit NewsLabel(const QString &htmlTitle,
81 const QString &htmlText,
84 virtual void resizeEvent(QResizeEvent *);
87 const QString m_title;
95 NewsLabel::NewsLabel(const QString &htmlTitle,
96 const QString &htmlText,
104 int dummy, left, right;
105 getContentsMargins(&left, &dummy, &right, &dummy);
106 m_contentsMargin = left + right;
109 void NewsLabel::resizeEvent(QResizeEvent *e)
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)
117 m_lastWidth = newWidth;
119 const QString elidedText = QFontMetrics(f).elidedText(m_text, Qt::ElideRight, newWidth);
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>");
130 // Item label of WelcomeModeTreeWidget: Horizontal widget consisting
131 // of arrow and clickable label.
132 class WelcomeModeItemWidget : public QWidget {
136 explicit WelcomeModeItemWidget(const QPixmap &pix,
138 const QString &tooltip,
140 QWidget *parent = 0);
141 // News items with title and start of article (see NewsLabel)
142 explicit WelcomeModeItemWidget(const QPixmap &pix,
145 const QString &tooltip,
147 QWidget *parent = 0);
150 void clicked(const QString &);
153 virtual void mousePressEvent(QMouseEvent *);
156 static inline void initLabel(QLabel *);
158 void init(const QPixmap &pix, QLabel *itemLabel, const QString &tooltip);
160 const QString m_data;
163 WelcomeModeItemWidget::WelcomeModeItemWidget(const QPixmap &pix,
165 const QString &tooltipText,
171 QLabel *label = new QLabel(text);
172 WelcomeModeItemWidget::initLabel(label);
173 init(pix, label, tooltipText);
176 static inline void fixHtml(QString &s)
178 s.replace(QLatin1String("’"), QString(QLatin1Char('\''))); // Quote
179 s.replace(QLatin1Char('\n'), QLatin1Char(' '));
182 WelcomeModeItemWidget::WelcomeModeItemWidget(const QPixmap &pix,
185 const QString &tooltipText,
193 QLabel *newsLabel = new NewsLabel(title, text);
194 WelcomeModeItemWidget::initLabel(newsLabel);
195 init(pix, newsLabel, tooltipText);
198 void WelcomeModeItemWidget::initLabel(QLabel *label)
200 label->setTextInteractionFlags(Qt::NoTextInteraction);
201 label->setCursor(QCursor(Qt::PointingHandCursor));
204 void WelcomeModeItemWidget::init(const QPixmap &pix, QLabel *itemLabel,
205 const QString &tooltipText)
207 QHBoxLayout *hBoxLayout = new QHBoxLayout;
208 hBoxLayout->setContentsMargins(topContentsMargin, leftContentsMargin,
209 0, bottomContentsMargin);
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));
217 pxLabel->setPixmap(pixmap);
219 pxLabel->setFixedWidth(pixmapWidth);
220 hBoxLayout->addWidget(pxLabel);
222 hBoxLayout->addWidget(itemLabel);
223 if (!tooltipText.isEmpty()) {
224 setToolTip(tooltipText);
225 pxLabel->setToolTip(tooltipText);
226 itemLabel->setToolTip(tooltipText);
228 setLayout(hBoxLayout);
231 void WelcomeModeItemWidget::mousePressEvent(QMouseEvent *e)
234 emit clicked(m_data);
237 // ----------------- WelcomeModeTreeWidget
238 struct WelcomeModeTreeWidgetPrivate
240 WelcomeModeTreeWidgetPrivate();
242 const QPixmap bullet;
244 QVBoxLayout *itemLayout;
247 WelcomeModeTreeWidgetPrivate::WelcomeModeTreeWidgetPrivate() :
248 bullet(QLatin1String(":/welcome/images/list_bullet_arrow.png")),
249 layout(new QVBoxLayout),
250 itemLayout(new QVBoxLayout)
252 layout->setMargin(0);
256 \class Utils::WelcomeModeTreeWidget
257 \brief Show an itemized list with arrows and emits a signal on click.
260 WelcomeModeTreeWidget::WelcomeModeTreeWidget(QWidget *parent) :
261 QWidget(parent), m_d(new WelcomeModeTreeWidgetPrivate)
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));
268 WelcomeModeTreeWidget::~WelcomeModeTreeWidget()
273 void WelcomeModeTreeWidget::addItem(const QString &label, const QString &data, const QString &toolTip)
275 addItemWidget(new WelcomeModeItemWidget(m_d->bullet, label, toolTip, data));
278 void WelcomeModeTreeWidget::addNewsItem(const QString &title,
279 const QString &description,
282 addItemWidget(new WelcomeModeItemWidget(m_d->bullet, title, description, link, link));
285 void WelcomeModeTreeWidget::addItemWidget(WelcomeModeItemWidget *w)
287 connect(w, SIGNAL(clicked(QString)), this, SIGNAL(activated(QString)));
288 m_d->itemLayout->addWidget(w);
291 void WelcomeModeTreeWidget::clear()
293 for (int i = m_d->itemLayout->count() - 1; i >= 0; i--) {
294 QLayoutItem *item = m_d->itemLayout->takeAt(i);
295 delete item->widget();
302 #include "welcomemodetreewidget.moc"