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 **************************************************************************/
35 #include "tipcontents.h"
38 #include <QtCore/QRect>
39 #include <QtGui/QColor>
40 #include <QtGui/QPainter>
42 #include <QtGui/QPixmap>
43 #include <QtGui/QStyle>
44 #include <QtGui/QFontMetrics>
45 #include <QtGui/QTextDocument>
46 #include <QtGui/QStylePainter>
47 #include <QtGui/QStyleOptionFrame>
48 #include <QtGui/QResizeEvent>
49 #include <QtGui/QPaintEvent>
51 namespace TextEditor {
56 QPixmap tilePixMap(int size)
58 const int checkerbordSize= size;
59 QPixmap tilePixmap(checkerbordSize * 2, checkerbordSize * 2);
60 tilePixmap.fill(Qt::white);
61 QPainter tilePainter(&tilePixmap);
62 QColor color(220, 220, 220);
63 tilePainter.fillRect(0, 0, checkerbordSize, checkerbordSize, color);
64 tilePainter.fillRect(checkerbordSize, checkerbordSize, checkerbordSize, checkerbordSize, color);
69 QTipLabel::QTipLabel(QWidget *parent) :
70 QLabel(parent, Qt::ToolTip | Qt::BypassGraphicsProxyWidget),
74 QTipLabel::~QTipLabel()
80 void QTipLabel::setContent(const TipContent &content)
84 m_tipContent = content.clone();
87 const TipContent &QTipLabel::content() const
88 { return *m_tipContent; }
90 ColorTip::ColorTip(QWidget *parent) : QTipLabel(parent)
92 resize(QSize(40, 40));
93 m_tilePixMap = tilePixMap(10);
99 void ColorTip::configure(const QPoint &pos, QWidget *w)
107 bool ColorTip::handleContentReplacement(const TipContent &content) const
109 if (content.typeId() == ColorContent::COLOR_CONTENT_ID)
114 void ColorTip::paintEvent(QPaintEvent *event)
116 QTipLabel::paintEvent(event);
118 const QColor &color = static_cast<const ColorContent &>(content()).color();
122 if (color.value() > 100)
123 pen.setColor(color.darker());
125 pen.setColor(color.lighter());
127 QPainter painter(this);
129 painter.setBrush(color);
130 QRect r(1, 1, rect().width() - 2, rect().height() - 2);
131 painter.drawTiledPixmap(r, m_tilePixMap);
135 TextTip::TextTip(QWidget *parent) : QTipLabel(parent)
137 setForegroundRole(QPalette::ToolTipText);
138 setBackgroundRole(QPalette::ToolTipBase);
140 setMargin(1 + style()->pixelMetric(QStyle::PM_ToolTipLabelFrameWidth, 0, this));
141 setFrameStyle(QFrame::NoFrame);
142 setAlignment(Qt::AlignLeft);
144 setWindowOpacity(style()->styleHint(QStyle::SH_ToolTipLabel_Opacity, 0, this) / 255.0);
150 void TextTip::configure(const QPoint &pos, QWidget *w)
152 const QString &text = static_cast<const TextContent &>(content()).text();
155 // Make it look good with the default ToolTip font on Mac, which has a small descent.
156 QFontMetrics fm(font());
158 if (fm.descent() == 2 && fm.ascent() >= 11)
161 // Try to find a nice width without unnecessary wrapping.
163 int tipWidth = sizeHint().width();
164 const int screenWidth = screenGeometry(pos, w).width();
165 const int maxDesiredWidth = int(screenWidth * .5);
166 if (tipWidth > maxDesiredWidth) {
168 tipWidth = sizeHint().width();
169 // If the width is still too large (maybe due to some extremely long word which prevents
170 // wrapping), the tip is truncated according to the screen.
171 if (tipWidth > screenWidth)
172 tipWidth = screenWidth - 10;
175 resize(tipWidth, heightForWidth(tipWidth) + extraHeight);
178 bool TextTip::handleContentReplacement(const TipContent &content) const
180 if (content.typeId() == TextContent::TEXT_CONTENT_ID)
185 void TextTip::paintEvent(QPaintEvent *event)
187 QStylePainter p(this);
188 QStyleOptionFrame opt;
190 p.drawPrimitive(QStyle::PE_PanelTipLabel, opt);
193 QLabel::paintEvent(event);
196 void TextTip::resizeEvent(QResizeEvent *event)
198 QStyleHintReturnMask frameMask;
201 if (style()->styleHint(QStyle::SH_ToolTip_Mask, &option, this, &frameMask))
202 setMask(frameMask.region);
204 QLabel::resizeEvent(event);
207 // need to include it here to force it to be inside the namespaces
208 #include "moc_tips.cpp"
210 } // namespace Internal
211 } // namespace TextEditor