there is no moc file generated for this class
[kdegraphics.git] / kolourpaint / layers / selections / text / kpTextSelection_Cursor.cpp
blob927f164fdd80d0aa932a57b1cdc08746bca36681
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 int kpTextSelection::closestTextRowForPoint (const QPoint &point) const
47 if (!pointIsInTextArea (point))
48 return -1;
50 const QFontMetrics fontMetrics (d->textStyle.fontMetrics ());
52 int row = (point.y () - textAreaRect ().y ()) /
53 fontMetrics.lineSpacing ();
54 if (row >= (int) d->textLines.size ())
55 row = d->textLines.size () - 1;
57 return row;
60 // public
61 int kpTextSelection::closestTextColForPoint (const QPoint &point) const
63 int row = closestTextRowForPoint (point);
64 if (row < 0 || row >= (int) d->textLines.size ())
65 return -1;
67 const int localX = point.x () - textAreaRect ().x ();
69 const QFontMetrics fontMetrics (d->textStyle.fontMetrics ());
71 // (should be 0 but call just in case)
72 int charLocalLeft = fontMetrics.width (d->textLines [row], 0);
74 // OPT: binary search or guess location then move
75 for (int col = 0; col < (int) d->textLines [row].length (); col++)
77 // OPT: fontMetrics::charWidth() might be faster
78 const int nextCharLocalLeft = fontMetrics.width (d->textLines [row], col + 1);
79 if (localX <= (charLocalLeft + nextCharLocalLeft) / 2)
80 return col;
82 charLocalLeft = nextCharLocalLeft;
85 return d->textLines [row].length ()/*past end of line*/;
89 // public
90 QPoint kpTextSelection::pointForTextRowCol (int row, int col) const
92 if (row < 0 || row >= (int) d->textLines.size () ||
93 col < 0 || col > (int) d->textLines [row].length ())
95 #if DEBUG_KP_SELECTION && 1
96 kDebug () << "kpTextSelection::pointForTextRowCol("
97 << row << ","
98 << col << ") out of range"
99 << " textLines='"
100 << text ()
101 << "'"
102 << endl;
103 #endif
104 return KP_INVALID_POINT;
107 const QFontMetrics fontMetrics (d->textStyle.fontMetrics ());
109 const int x = fontMetrics.width (d->textLines [row], col);
110 const int y = row * fontMetrics.height () +
111 (row >= 1 ? row * fontMetrics.leading () : 0);
113 return textAreaRect ().topLeft () + QPoint (x, y);