OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / texteditor / tooltip / tips.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 "tips.h"
35 #include "tipcontents.h"
36 #include "reuse.h"
37
38 #include <QtCore/QRect>
39 #include <QtGui/QColor>
40 #include <QtGui/QPainter>
41 #include <QtGui/QPen>
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>
50
51 namespace TextEditor {
52     namespace Internal {
53
54 namespace {
55     // @todo: Reuse...
56     QPixmap tilePixMap(int size)
57     {
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);
65         return tilePixmap;
66     }
67 }
68
69 QTipLabel::QTipLabel(QWidget *parent) :
70     QLabel(parent, Qt::ToolTip | Qt::BypassGraphicsProxyWidget),
71     m_tipContent(0)
72 {}
73
74 QTipLabel::~QTipLabel()
75 {
76     if (m_tipContent)
77         delete m_tipContent;
78 }
79
80 void QTipLabel::setContent(const TipContent &content)
81 {
82     if (m_tipContent)
83         delete m_tipContent;
84     m_tipContent = content.clone();
85 }
86
87 const TipContent &QTipLabel::content() const
88 { return *m_tipContent; }
89
90 ColorTip::ColorTip(QWidget *parent) : QTipLabel(parent)
91 {
92     resize(QSize(40, 40));
93     m_tilePixMap = tilePixMap(10);
94 }
95
96 ColorTip::~ColorTip()
97 {}
98
99 void ColorTip::configure(const QPoint &pos, QWidget *w)
100 {
101     Q_UNUSED(pos)
102     Q_UNUSED(w)
103
104     update();
105 }
106
107 bool ColorTip::handleContentReplacement(const TipContent &content) const
108 {
109     if (content.typeId() == ColorContent::COLOR_CONTENT_ID)
110         return true;
111     return false;
112 }
113
114 void ColorTip::paintEvent(QPaintEvent *event)
115 {
116     QTipLabel::paintEvent(event);
117
118     const QColor &color = static_cast<const ColorContent &>(content()).color();
119
120     QPen pen;
121     pen.setWidth(1);
122     if (color.value() > 100)
123         pen.setColor(color.darker());
124     else
125         pen.setColor(color.lighter());
126
127     QPainter painter(this);
128     painter.setPen(pen);
129     painter.setBrush(color);
130     QRect r(1, 1, rect().width() - 2, rect().height() - 2);
131     painter.drawTiledPixmap(r, m_tilePixMap);
132     painter.drawRect(r);
133 }
134
135 TextTip::TextTip(QWidget *parent) : QTipLabel(parent)
136 {
137     setForegroundRole(QPalette::ToolTipText);
138     setBackgroundRole(QPalette::ToolTipBase);
139     ensurePolished();
140     setMargin(1 + style()->pixelMetric(QStyle::PM_ToolTipLabelFrameWidth, 0, this));
141     setFrameStyle(QFrame::NoFrame);
142     setAlignment(Qt::AlignLeft);
143     setIndent(1);
144     setWindowOpacity(style()->styleHint(QStyle::SH_ToolTipLabel_Opacity, 0, this) / 255.0);
145 }
146
147 TextTip::~TextTip()
148 {}
149
150 void TextTip::configure(const QPoint &pos, QWidget *w)
151 {
152     const QString &text = static_cast<const TextContent &>(content()).text();
153     setText(text);
154
155     // Make it look good with the default ToolTip font on Mac, which has a small descent.
156     QFontMetrics fm(font());
157     int extraHeight = 0;
158     if (fm.descent() == 2 && fm.ascent() >= 11)
159         ++extraHeight;
160
161     // Try to find a nice width without unnecessary wrapping.
162     setWordWrap(false);
163     int tipWidth = sizeHint().width();
164     const int screenWidth = screenGeometry(pos, w).width();
165     const int maxDesiredWidth = int(screenWidth * .5);
166     if (tipWidth > maxDesiredWidth) {
167         setWordWrap(true);
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;
173     }
174
175     resize(tipWidth, heightForWidth(tipWidth) + extraHeight);
176 }
177
178 bool TextTip::handleContentReplacement(const TipContent &content) const
179 {
180     if (content.typeId() == TextContent::TEXT_CONTENT_ID)
181         return true;
182     return false;
183 }
184
185 void TextTip::paintEvent(QPaintEvent *event)
186 {
187     QStylePainter p(this);
188     QStyleOptionFrame opt;
189     opt.init(this);
190     p.drawPrimitive(QStyle::PE_PanelTipLabel, opt);
191     p.end();
192
193     QLabel::paintEvent(event);
194 }
195
196 void TextTip::resizeEvent(QResizeEvent *event)
197 {
198     QStyleHintReturnMask frameMask;
199     QStyleOption option;
200     option.init(this);
201     if (style()->styleHint(QStyle::SH_ToolTip_Mask, &option, this, &frameMask))
202         setMask(frameMask.region);
203
204     QLabel::resizeEvent(event);
205 }
206
207 // need to include it here to force it to be inside the namespaces
208 #include "moc_tips.cpp"
209
210 } // namespace Internal
211 } // namespace TextEditor