there is no moc file generated for this class
[kdegraphics.git] / kolourpaint / layers / selections / text / kpTextSelection.cpp
blob8b9e81a3ec173d8b1d68f52ce335ed01aeed91dc
2 /*
3 Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
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>
36 #include <QList>
38 #include <KDebug>
40 #include <kpDefs.h>
41 #include <kpTextStyle.h>
44 // public
45 kpTextSelection::kpTextSelection (const QRect &rect,
46 const kpTextStyle &textStyle)
47 : kpAbstractSelection (rect),
48 d (new kpTextSelectionPrivate ())
50 d->textStyle = textStyle;
53 // public
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;
64 // public
65 kpTextSelection::kpTextSelection (const kpTextSelection &rhs)
66 : kpAbstractSelection (),
67 d (new kpTextSelectionPrivate ())
69 *this = rhs;
72 // public
73 kpTextSelection &kpTextSelection::operator= (const kpTextSelection &rhs)
75 kpAbstractSelection::operator= (rhs);
77 d->textLines = rhs.d->textLines;
78 d->textStyle = rhs.d->textStyle;
80 return *this;
83 // public virtual [base kpAbstractSelection]
84 kpTextSelection *kpTextSelection::clone () const
86 kpTextSelection *sel = new kpTextSelection ();
87 *sel = *this;
88 return sel;
91 // public
92 kpTextSelection *kpTextSelection::resized (int newWidth, int newHeight) const
94 return new kpTextSelection (QRect (x (), y (), newWidth, newHeight),
95 d->textLines,
96 d->textStyle);
99 // public
100 kpTextSelection::~kpTextSelection ()
102 delete d;
106 // public virtual [kpAbstractSelection]
107 int kpTextSelection::serialID () const
109 Q_ASSERT (!"Marshalling not supported");
110 return -1;
113 // public virtual [base kpAbstractSelection]
114 bool kpTextSelection::readFromStream (QDataStream &stream,
115 const kpPixmapFX::WarnAboutLossInfo &wali)
117 (void) stream;
118 (void) wali;
120 Q_ASSERT (!"Marshalling not supported");
121 return false;
124 // public virtual [base kpAbstractSelection]
125 void kpTextSelection::writeToStream (QDataStream &stream) const
127 (void) stream;
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
151 return true;
155 // public static
156 int kpTextSelection::MinimumWidthForTextStyle (const kpTextStyle &)
158 return (kpTextSelection::TextBorderSize () * 2 + 5);
161 // public static
162 int kpTextSelection::MinimumHeightForTextStyle (const kpTextStyle &)
164 return (kpTextSelection::TextBorderSize () * 2 + 5);
167 // public static
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 ());
188 // public static
189 int kpTextSelection::PreferredMinimumWidthForTextStyle (const kpTextStyle &textStyle)
191 const int about15CharsWidth =
192 textStyle.fontMetrics ().width (
193 QLatin1String ("1234567890abcde"));
195 const int preferredMinWidth =
196 qMax (150,
197 kpTextSelection::TextBorderSize () * 2 + about15CharsWidth);
199 return qMax (kpTextSelection::MinimumWidthForTextStyle (textStyle),
200 qMin (250, preferredMinWidth));
203 // public static
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));
213 // public static
214 QSize kpTextSelection::PreferredMinimumSizeForTextStyle (const kpTextStyle &textStyle)
216 return QSize (kpTextSelection::PreferredMinimumWidthForTextStyle (textStyle),
217 kpTextSelection::PreferredMinimumHeightForTextStyle (textStyle));
221 // public static
222 int kpTextSelection::TextBorderSize ()
224 return 1;
227 // public
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);
251 // public
252 bool kpTextSelection::pointIsInTextBorderArea (const QPoint &point) const
254 return (boundingRect ().contains (point) && !pointIsInTextArea (point));
257 // public
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 ()
273 if (!hasContent ())
274 return;
276 setTextLines (QList <QString> ());
280 // public
281 QList <QString> kpTextSelection::textLines () const
283 return d->textLines;
286 // public
287 void kpTextSelection::setTextLines (const QList <QString> &textLines_)
289 d->textLines = textLines_;
291 emit changed (boundingRect ());
295 // public static
296 QString kpTextSelection::TextForTextLines (const QList <QString> &textLines)
298 if (textLines.isEmpty ())
299 return QString ();
301 QString bigString = textLines [0];
303 for (QList <QString>::const_iterator it = textLines.begin () + 1;
304 it != textLines.end ();
305 it++)
307 bigString += QLatin1String ("\n");
308 bigString += (*it);
311 return bigString;
314 // public
315 QString kpTextSelection::text () const
317 return kpTextSelection::TextForTextLines (d->textLines);
321 // public
322 kpTextStyle kpTextSelection::textStyle () const
324 return d->textStyle;
327 // public
328 void kpTextSelection::setTextStyle (const kpTextStyle &textStyle)
330 d->textStyle = textStyle;
332 emit changed (boundingRect ());
336 #include <kpTextSelection.moc>