3 Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
10 1. Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #define DEBUG_KP_SELECTION 0
32 #include <kpTextSelection.h>
33 #include <kpTextSelectionPrivate.h>
35 #include <QFontMetrics>
41 #include <kpTextStyle.h>
45 kpTextSelection::kpTextSelection (const QRect
&rect
,
46 const kpTextStyle
&textStyle
)
47 : kpAbstractSelection (rect
),
48 d (new kpTextSelectionPrivate ())
50 d
->textStyle
= textStyle
;
54 kpTextSelection::kpTextSelection (const QRect
&rect
,
55 const QList
<QString
> &textLines
,
56 const kpTextStyle
&textStyle
)
57 : kpAbstractSelection (rect
),
58 d (new kpTextSelectionPrivate ())
60 d
->textLines
= textLines
;
61 d
->textStyle
= textStyle
;
65 kpTextSelection::kpTextSelection (const kpTextSelection
&rhs
)
66 : kpAbstractSelection (),
67 d (new kpTextSelectionPrivate ())
73 kpTextSelection
&kpTextSelection::operator= (const kpTextSelection
&rhs
)
75 kpAbstractSelection::operator= (rhs
);
77 d
->textLines
= rhs
.d
->textLines
;
78 d
->textStyle
= rhs
.d
->textStyle
;
83 // public virtual [base kpAbstractSelection]
84 kpTextSelection
*kpTextSelection::clone () const
86 kpTextSelection
*sel
= new kpTextSelection ();
92 kpTextSelection
*kpTextSelection::resized (int newWidth
, int newHeight
) const
94 return new kpTextSelection (QRect (x (), y (), newWidth
, newHeight
),
100 kpTextSelection::~kpTextSelection ()
106 // public virtual [kpAbstractSelection]
107 int kpTextSelection::serialID () const
109 Q_ASSERT (!"Marshalling not supported");
113 // public virtual [base kpAbstractSelection]
114 bool kpTextSelection::readFromStream (QDataStream
&stream
,
115 const kpPixmapFX::WarnAboutLossInfo
&wali
)
120 Q_ASSERT (!"Marshalling not supported");
124 // public virtual [base kpAbstractSelection]
125 void kpTextSelection::writeToStream (QDataStream
&stream
) const
129 Q_ASSERT (!"Marshalling not supported");
133 // public virtual [kpAbstractSelection]
134 QString
kpTextSelection::name () const
136 return i18n ("Text");
140 // public virtual [base kpAbstractSelection]
141 kpCommandSize::SizeType
kpTextSelection::size () const
143 return kpAbstractSelection::size () +
144 kpCommandSize::StringSize (text ());
148 // public virtual [kpAbstractSelection]
149 bool kpTextSelection::isRectangular () const
156 int kpTextSelection::MinimumWidthForTextStyle (const kpTextStyle
&)
158 return (kpTextSelection::TextBorderSize () * 2 + 5);
162 int kpTextSelection::MinimumHeightForTextStyle (const kpTextStyle
&)
164 return (kpTextSelection::TextBorderSize () * 2 + 5);
168 QSize
kpTextSelection::MinimumSizeForTextStyle (const kpTextStyle
&textStyle
)
170 return QSize (kpTextSelection::MinimumWidthForTextStyle (textStyle
),
171 kpTextSelection::MinimumHeightForTextStyle (textStyle
));
175 // public virtual [kpAbstractSelection]
176 int kpTextSelection::minimumWidth () const
178 return kpTextSelection::MinimumWidthForTextStyle (textStyle ());
181 // public virtual [kpAbstractSelection]
182 int kpTextSelection::minimumHeight () const
184 return kpTextSelection::MinimumHeightForTextStyle (textStyle ());
189 int kpTextSelection::PreferredMinimumWidthForTextStyle (const kpTextStyle
&textStyle
)
191 const int about15CharsWidth
=
192 textStyle
.fontMetrics ().width (
193 QLatin1String ("1234567890abcde"));
195 const int preferredMinWidth
=
197 kpTextSelection::TextBorderSize () * 2 + about15CharsWidth
);
199 return qMax (kpTextSelection::MinimumWidthForTextStyle (textStyle
),
200 qMin (250, preferredMinWidth
));
204 int kpTextSelection::PreferredMinimumHeightForTextStyle (const kpTextStyle
&textStyle
)
206 const int preferredMinHeight
=
207 kpTextSelection::TextBorderSize () * 2 + textStyle
.fontMetrics ().height ();
209 return qMax (kpTextSelection::MinimumHeightForTextStyle (textStyle
),
210 qMin (150, preferredMinHeight
));
214 QSize
kpTextSelection::PreferredMinimumSizeForTextStyle (const kpTextStyle
&textStyle
)
216 return QSize (kpTextSelection::PreferredMinimumWidthForTextStyle (textStyle
),
217 kpTextSelection::PreferredMinimumHeightForTextStyle (textStyle
));
222 int kpTextSelection::TextBorderSize ()
228 QRect
kpTextSelection::textAreaRect () const
230 return QRect (x () + kpTextSelection::TextBorderSize (),
231 y () + kpTextSelection::TextBorderSize (),
232 width () - kpTextSelection::TextBorderSize () * 2,
233 height () - kpTextSelection::TextBorderSize () * 2);
237 // public virtual [kpAbstractSelection]
238 QPolygon
kpTextSelection::calculatePoints () const
240 return kpAbstractSelection::CalculatePointsForRectangle (boundingRect ());
244 // public virtual [kpAbstractSelection]
245 bool kpTextSelection::contains (const QPoint
&point
) const
247 return boundingRect ().contains (point
);
252 bool kpTextSelection::pointIsInTextBorderArea (const QPoint
&point
) const
254 return (boundingRect ().contains (point
) && !pointIsInTextArea (point
));
258 bool kpTextSelection::pointIsInTextArea (const QPoint
&point
) const
260 return textAreaRect ().contains (point
);
264 // public virtual [kpAbstractSelection]
265 bool kpTextSelection::hasContent () const
267 return !d
->textLines
.isEmpty ();
270 // public virtual [kpAbstractSelection]
271 void kpTextSelection::deleteContent ()
276 setTextLines (QList
<QString
> ());
281 QList
<QString
> kpTextSelection::textLines () const
287 void kpTextSelection::setTextLines (const QList
<QString
> &textLines_
)
289 d
->textLines
= textLines_
;
291 emit
changed (boundingRect ());
296 QString
kpTextSelection::TextForTextLines (const QList
<QString
> &textLines
)
298 if (textLines
.isEmpty ())
301 QString bigString
= textLines
[0];
303 for (QList
<QString
>::const_iterator it
= textLines
.begin () + 1;
304 it
!= textLines
.end ();
307 bigString
+= QLatin1String ("\n");
315 QString
kpTextSelection::text () const
317 return kpTextSelection::TextForTextLines (d
->textLines
);
322 kpTextStyle
kpTextSelection::textStyle () const
328 void kpTextSelection::setTextStyle (const kpTextStyle
&textStyle
)
330 d
->textStyle
= textStyle
;
332 emit
changed (boundingRect ());
336 #include <kpTextSelection.moc>