aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/welcomemodetreewidget.cpp
blob: 08f890909b3880d29ca621943cd1651ca5671c57 (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (info@qt.nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/

#include "welcomemodetreewidget.h"

#include <QtGui/QLabel>
#include <QtGui/QPixmap>
#include <QtGui/QAction>
#include <QtGui/QVBoxLayout>
#include <QtGui/QMouseEvent>
#include <QtGui/QResizeEvent>
#include <QtGui/QImage>

enum { leftContentsMargin = 2,
       topContentsMargin = 2,
       bottomContentsMargin = 1,
       pixmapWidth = 24 };

/*!
    \class Utils::WelcomeModeLabel
    \brief Label usable for headers of a Welcome page.
*/

namespace Utils {

WelcomeModeLabel::WelcomeModeLabel(QWidget *parent) :
    QLabel(parent), m_unused(0)
{
    // Bold/enlarged font slightly gray. Force color on by stylesheet as it is used
    // as a child of widgets that have stylesheets.
    QFont f = font();
#ifndef Q_OS_WIN
    f.setWeight(QFont::DemiBold);
#endif
    f.setPointSizeF(f.pointSizeF() * 1.2);
    setFont(f);
    setStyleSheet(QLatin1String("color : rgb(85, 85, 85);"));
    setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop);
}

WelcomeModeLabel::~WelcomeModeLabel()
{
}

// NewsLabel for the WelcomeModeTreeWidget:
// Shows a news article as "Bold Title!\nElided Start of article...."
// Adapts the eliding when resizing.
class NewsLabel : public QLabel {
public:
    explicit NewsLabel(const QString &htmlTitle,
                       const QString &htmlText,
                       QWidget *parent = 0);

    virtual void resizeEvent(QResizeEvent *);

private:
    const QString m_title;
    const QString m_text;
    const int m_minWidth;

    int m_lastWidth;
    int m_contentsMargin;
};

NewsLabel::NewsLabel(const QString &htmlTitle,
                   const QString &htmlText,
                   QWidget *parent) :
    QLabel(parent),
    m_title(htmlTitle),
    m_text(htmlText),
    m_minWidth(100),
    m_lastWidth(-1)
{
    int dummy, left, right;
    getContentsMargins(&left, &dummy, &right, &dummy);
    m_contentsMargin = left + right;
}

void NewsLabel::resizeEvent(QResizeEvent *e)
{
    enum { epsilon = 10 };
    QLabel::resizeEvent(e);
    // Resized: Adapt to new size (if it is >  m_minWidth)
    const int newWidth = qMax(e->size().width() - m_contentsMargin, m_minWidth) - epsilon;
    if (newWidth == m_lastWidth)
        return;
    m_lastWidth = newWidth;
    QFont f = font();
    const QString elidedText = QFontMetrics(f).elidedText(m_text, Qt::ElideRight, newWidth);
    f.setBold(true);
    const QString elidedTitle = QFontMetrics(f).elidedText(m_title, Qt::ElideRight, newWidth);
    QString labelText = QLatin1String("<b>");
    labelText += elidedTitle;
    labelText += QLatin1String("</b><br /><font color='gray'>");
    labelText += elidedText;
    labelText += QLatin1String("</font>");
    setText(labelText);
}

// Item label of WelcomeModeTreeWidget: Horizontal widget consisting
// of arrow and clickable label.
class WelcomeModeItemWidget : public QWidget {
    Q_OBJECT
public:
    // Normal items
    explicit WelcomeModeItemWidget(const QPixmap &pix,
                                   const QString &text,
                                   const QString &tooltip,
                                   const QString &data,
                                   QWidget *parent = 0);
    // News items with title and start of article (see NewsLabel)
    explicit WelcomeModeItemWidget(const QPixmap &pix,
                                   QString htmlTitle,
                                   QString htmlText,
                                   const QString &tooltip,
                                   const QString &data,
                                   QWidget *parent = 0);

signals:
    void clicked(const QString &);

protected:
    virtual void mousePressEvent(QMouseEvent *);

private:
    static inline void initLabel(QLabel *);

    void init(const QPixmap &pix, QLabel *itemLabel, const QString &tooltip);

    const QString m_data;
};

WelcomeModeItemWidget::WelcomeModeItemWidget(const QPixmap &pix,
                                             const QString &text,
                                             const QString &tooltipText,
                                             const QString &data,
                                             QWidget *parent) :
    QWidget(parent),
    m_data(data)
{
    QLabel *label = new QLabel(text);
    WelcomeModeItemWidget::initLabel(label);
    init(pix, label, tooltipText);
}

static inline void fixHtml(QString &s)
{
    s.replace(QLatin1String("&#8217;"), QString(QLatin1Char('\''))); // Quote
    s.replace(QLatin1Char('\n'), QLatin1Char(' '));
}

WelcomeModeItemWidget::WelcomeModeItemWidget(const QPixmap &pix,
                                             QString title,
                                             QString text,
                                             const QString &tooltipText,
                                             const QString &data,
                                             QWidget *parent) :
    QWidget(parent),
    m_data(data)
{
    fixHtml(text);
    fixHtml(title);
    QLabel *newsLabel = new NewsLabel(title, text);
    WelcomeModeItemWidget::initLabel(newsLabel);
    init(pix, newsLabel, tooltipText);
}

void WelcomeModeItemWidget::initLabel(QLabel *label)
{
    label->setTextInteractionFlags(Qt::NoTextInteraction);
    label->setCursor(QCursor(Qt::PointingHandCursor));
}

void WelcomeModeItemWidget::init(const QPixmap &pix, QLabel *itemLabel,
                                 const QString &tooltipText)
{
    QHBoxLayout *hBoxLayout = new QHBoxLayout;
    hBoxLayout->setContentsMargins(topContentsMargin, leftContentsMargin,
                                   0, bottomContentsMargin);

    QLabel *pxLabel = new QLabel;
    QPixmap pixmap = pix;
    if (layoutDirection() == Qt::RightToLeft){
        QImage image = pixmap.toImage();
        pixmap = QPixmap::fromImage(image.mirrored(1, 0));
    }
    pxLabel->setPixmap(pixmap);

    pxLabel->setFixedWidth(pixmapWidth);
    hBoxLayout->addWidget(pxLabel);

    hBoxLayout->addWidget(itemLabel);
    if (!tooltipText.isEmpty()) {
        setToolTip(tooltipText);
        pxLabel->setToolTip(tooltipText);
        itemLabel->setToolTip(tooltipText);
    }
    setLayout(hBoxLayout);
}

void WelcomeModeItemWidget::mousePressEvent(QMouseEvent *e)
{
    e->accept();
    emit clicked(m_data);
}

// ----------------- WelcomeModeTreeWidget
struct WelcomeModeTreeWidgetPrivate
{
    WelcomeModeTreeWidgetPrivate();

    const QPixmap bullet;
    QVBoxLayout *layout;
    QVBoxLayout *itemLayout;
};

WelcomeModeTreeWidgetPrivate::WelcomeModeTreeWidgetPrivate() :
    bullet(QLatin1String(":/welcome/images/list_bullet_arrow.png")),
    layout(new QVBoxLayout),
    itemLayout(new QVBoxLayout)
{
    layout->setMargin(0);
}

/*!
    \class Utils::WelcomeModeTreeWidget
    \brief Show an itemized list with arrows and emits a signal on click.
*/

WelcomeModeTreeWidget::WelcomeModeTreeWidget(QWidget *parent) :
        QWidget(parent), m_d(new WelcomeModeTreeWidgetPrivate)
{
    setLayout(m_d->layout);
    m_d->layout->addLayout(m_d->itemLayout);
    m_d->layout->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding));
}

WelcomeModeTreeWidget::~WelcomeModeTreeWidget()
{
    delete m_d;
}

void WelcomeModeTreeWidget::addItem(const QString &label, const QString &data, const QString &toolTip)
{
    addItemWidget(new WelcomeModeItemWidget(m_d->bullet, label, toolTip, data));
}

void WelcomeModeTreeWidget::addNewsItem(const QString &title,
                                        const QString &description,
                                        const QString &link)
{
    addItemWidget(new WelcomeModeItemWidget(m_d->bullet, title, description, link, link));
}

void WelcomeModeTreeWidget::addItemWidget(WelcomeModeItemWidget *w)
{
    connect(w, SIGNAL(clicked(QString)), this, SIGNAL(activated(QString)));
    m_d->itemLayout->addWidget(w);
}

void WelcomeModeTreeWidget::clear()
{
    for (int i = m_d->itemLayout->count() - 1; i >= 0; i--) {
        QLayoutItem *item = m_d->itemLayout->takeAt(i);
        delete item->widget();
        delete item;
    }
}

} // namespace Utils

#include "welcomemodetreewidget.moc"