File indexing completed on 2024-06-16 04:09:58

0001 
0002 /*
0003    Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
0004    Copyright (c) 2010 Tasuku Suzuki <stasuku@gmail.com>
0005    All rights reserved.
0006 
0007    Redistribution and use in source and binary forms, with or without
0008    modification, are permitted provided that the following conditions
0009    are met:
0010 
0011    1. Redistributions of source code must retain the above copyright
0012       notice, this list of conditions and the following disclaimer.
0013    2. Redistributions in binary form must reproduce the above copyright
0014       notice, this list of conditions and the following disclaimer in the
0015       documentation and/or other materials provided with the distribution.
0016 
0017    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0018    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0019    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0020    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0021    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0022    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0023    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0024    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0025    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0026    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0027 */
0028 
0029 
0030 #define DEBUG_KP_SELECTION 0
0031 
0032 
0033 #include "kpTextSelection.h"
0034 #include "kpTextSelectionPrivate.h"
0035 
0036 #include "kpDefs.h"
0037 #include "kpTextStyle.h"
0038 
0039 #include "kpLogCategories.h"
0040 
0041 #include <QFontMetrics>
0042 #include <QList>
0043 
0044 
0045 // public
0046 kpTextSelection::kpTextSelection (const QRect &rect,
0047         const kpTextStyle &textStyle)
0048     : kpAbstractSelection (rect),
0049       d (new kpTextSelectionPrivate ())
0050 {
0051     d->textStyle = textStyle;
0052 }
0053 
0054 // public
0055 kpTextSelection::kpTextSelection (const QRect &rect,
0056         const QList <QString> &textLines,
0057         const kpTextStyle &textStyle)
0058     : kpAbstractSelection (rect),
0059       d (new kpTextSelectionPrivate ())
0060 {
0061     d->textLines = textLines;
0062     d->textStyle = textStyle;
0063 }
0064 
0065 // public
0066 kpTextSelection::kpTextSelection (const kpTextSelection &rhs)
0067     : kpAbstractSelection (),
0068       d (new kpTextSelectionPrivate ())
0069 {
0070     *this = rhs;
0071 }
0072 
0073 // public
0074 kpTextSelection &kpTextSelection::operator= (const kpTextSelection &rhs)
0075 {
0076     kpAbstractSelection::operator= (rhs);
0077 
0078     d->textLines = rhs.d->textLines;
0079     d->textStyle = rhs.d->textStyle;
0080     d->preeditText = rhs.d->preeditText;
0081 
0082     return *this;
0083 }
0084 
0085 // public virtual [base kpAbstractSelection]
0086 kpTextSelection *kpTextSelection::clone () const
0087 {
0088     kpTextSelection *sel = new kpTextSelection ();
0089     *sel = *this;
0090     return sel;
0091 }
0092 
0093 // public
0094 kpTextSelection *kpTextSelection::resized (int newWidth, int newHeight) const
0095 {
0096     return new kpTextSelection (QRect (x (), y (), newWidth, newHeight),
0097         d->textLines,
0098         d->textStyle);
0099 }
0100 
0101 // public
0102 kpTextSelection::~kpTextSelection ()
0103 {
0104     delete d;
0105 }
0106 
0107 
0108 // public virtual [kpAbstractSelection]
0109 int kpTextSelection::serialID () const
0110 {
0111     Q_ASSERT (!"Marshalling not supported");
0112     return -1;
0113 }
0114 
0115 // public virtual [base kpAbstractSelection]
0116 bool kpTextSelection::readFromStream (QDataStream &stream)
0117 {
0118     (void) stream;
0119 
0120     Q_ASSERT (!"Marshalling not supported");
0121     return false;
0122 }
0123 
0124 // public virtual [base kpAbstractSelection]
0125 void kpTextSelection::writeToStream (QDataStream &stream) const
0126 {
0127     (void) stream;
0128 
0129     Q_ASSERT (!"Marshalling not supported");
0130 }
0131 
0132 
0133 // public virtual [kpAbstractSelection]
0134 QString kpTextSelection::name () const
0135 {
0136     return i18n ("Text");
0137 }
0138 
0139 
0140 // public virtual [base kpAbstractSelection]
0141 kpCommandSize::SizeType kpTextSelection::size () const
0142 {
0143     return kpAbstractSelection::size () +
0144         kpCommandSize::StringSize (text ());
0145 }
0146 
0147 
0148 // public virtual [kpAbstractSelection]
0149 bool kpTextSelection::isRectangular () const
0150 {
0151     return true;
0152 }
0153 
0154 
0155 // public static
0156 int kpTextSelection::MinimumWidthForTextStyle (const kpTextStyle &)
0157 {
0158     return (kpTextSelection::TextBorderSize () * 2 + 5);
0159 }
0160 
0161 // public static
0162 int kpTextSelection::MinimumHeightForTextStyle (const kpTextStyle &)
0163 {
0164     return (kpTextSelection::TextBorderSize () * 2 + 5);
0165 }
0166 
0167 // public static
0168 QSize kpTextSelection::MinimumSizeForTextStyle (const kpTextStyle &textStyle)
0169 {
0170     return  {kpTextSelection::MinimumWidthForTextStyle (textStyle),
0171                 kpTextSelection::MinimumHeightForTextStyle (textStyle)};
0172 }
0173 
0174 
0175 // public virtual [kpAbstractSelection]
0176 int kpTextSelection::minimumWidth () const
0177 {
0178     return kpTextSelection::MinimumWidthForTextStyle (textStyle ());
0179 }
0180 
0181 // public virtual [kpAbstractSelection]
0182 int kpTextSelection::minimumHeight () const
0183 {
0184     return kpTextSelection::MinimumHeightForTextStyle (textStyle ());
0185 }
0186 
0187 
0188 // public static
0189 int kpTextSelection::PreferredMinimumWidthForTextStyle (const kpTextStyle &textStyle)
0190 {
0191     const int about15CharsWidth =
0192         textStyle.fontMetrics().horizontalAdvance(QLatin1String("1234567890abcde"));
0193 
0194     const int preferredMinWidth =
0195         qMax (150,
0196               kpTextSelection::TextBorderSize () * 2 + about15CharsWidth);
0197 
0198     return qMax (kpTextSelection::MinimumWidthForTextStyle (textStyle),
0199                  qMin (250, preferredMinWidth));
0200 }
0201 
0202 // public static
0203 int kpTextSelection::PreferredMinimumHeightForTextStyle (const kpTextStyle &textStyle)
0204 {
0205     const int preferredMinHeight =
0206         kpTextSelection::TextBorderSize () * 2 + textStyle.fontMetrics ().height ();
0207 
0208     return qMax (kpTextSelection::MinimumHeightForTextStyle (textStyle),
0209                  qMin (150, preferredMinHeight));
0210 }
0211 
0212 // public static
0213 QSize kpTextSelection::PreferredMinimumSizeForTextStyle (const kpTextStyle &textStyle)
0214 {
0215     return  {kpTextSelection::PreferredMinimumWidthForTextStyle (textStyle),
0216                 kpTextSelection::PreferredMinimumHeightForTextStyle (textStyle)};
0217 }
0218 
0219 
0220 // public static
0221 int kpTextSelection::TextBorderSize ()
0222 {
0223     return 1;
0224 }
0225 
0226 // public
0227 QRect kpTextSelection::textAreaRect () const
0228 {
0229     return  {x () + kpTextSelection::TextBorderSize (),
0230                 y () + kpTextSelection::TextBorderSize (),
0231                 width () - kpTextSelection::TextBorderSize () * 2,
0232                 height () - kpTextSelection::TextBorderSize () * 2};
0233 }
0234 
0235 
0236 // public virtual [kpAbstractSelection]
0237 QPolygon kpTextSelection::calculatePoints () const
0238 {
0239     return kpAbstractSelection::CalculatePointsForRectangle (boundingRect ());
0240 }
0241 
0242 
0243 // public virtual [kpAbstractSelection]
0244 bool kpTextSelection::contains (const QPoint &point) const
0245 {
0246     return boundingRect ().contains (point);
0247 }
0248 
0249 
0250 // public
0251 bool kpTextSelection::pointIsInTextBorderArea (const QPoint &point) const
0252 {
0253     return (boundingRect ().contains (point) && !pointIsInTextArea (point));
0254 }
0255 
0256 // public
0257 bool kpTextSelection::pointIsInTextArea (const QPoint &point) const
0258 {
0259     return textAreaRect ().contains (point);
0260 }
0261 
0262 
0263 // public virtual [kpAbstractSelection]
0264 bool kpTextSelection::hasContent () const
0265 {
0266     return !d->textLines.isEmpty ();
0267 }
0268 
0269 // public virtual [kpAbstractSelection]
0270 void kpTextSelection::deleteContent ()
0271 {
0272     if (!hasContent ()) {
0273         return;
0274     }
0275 
0276     setTextLines (QList <QString> ());
0277 }
0278 
0279 
0280 // public
0281 QList <QString> kpTextSelection::textLines () const
0282 {
0283     return d->textLines;
0284 }
0285 
0286 // public
0287 void kpTextSelection::setTextLines (const QList <QString> &textLines_)
0288 {
0289     d->textLines = textLines_;
0290 
0291     Q_EMIT changed (boundingRect ());
0292 }
0293 
0294 //--------------------------------------------------------------------------------
0295 
0296 // public static
0297 QString kpTextSelection::textForTextLines(const QList<QString> &textLines)
0298 {
0299     if (textLines.isEmpty ())
0300       return QString();
0301 
0302     QString bigString = textLines[0];
0303 
0304     for (int i = 1; i < textLines.count(); i++)
0305     {
0306         bigString += QLatin1String("\n");
0307         bigString += textLines[i];
0308     }
0309 
0310     return bigString;
0311 }
0312 
0313 //--------------------------------------------------------------------------------
0314 
0315 // public
0316 QString kpTextSelection::text () const
0317 {
0318     return kpTextSelection::textForTextLines (d->textLines);
0319 }
0320 
0321 
0322 // public
0323 kpTextStyle kpTextSelection::textStyle () const
0324 {
0325     return d->textStyle;
0326 }
0327 
0328 // public
0329 void kpTextSelection::setTextStyle (const kpTextStyle &textStyle)
0330 {
0331     d->textStyle = textStyle;
0332 
0333     Q_EMIT changed (boundingRect ());
0334 }
0335 
0336 kpPreeditText kpTextSelection::preeditText () const
0337 {
0338     return d->preeditText;
0339 }
0340 
0341 void kpTextSelection::setPreeditText (const kpPreeditText &preeditText)
0342 {
0343     d->preeditText = preeditText;
0344     Q_EMIT changed (boundingRect ());
0345 }
0346 
0347 #include "moc_kpTextSelection.cpp"