1 
2 /*
3    Copyright (c) 2010 Tasuku Suzuki <stasuku@gmail.com>
4    All rights reserved.
5 
6    Redistribution and use in source and binary forms, with or without
7    modification, are permitted provided that the following conditions
8    are met:
9 
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.
15 
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.
26 */
27 
28 #include "layers/selections/text/kpPreeditText.h"
29 
30 //---------------------------------------------------------------------
31 
attributeLessThan(const QInputMethodEvent::Attribute & a1,const QInputMethodEvent::Attribute & a2)32 bool attributeLessThan (const QInputMethodEvent::Attribute &a1, const QInputMethodEvent::Attribute &a2)
33 {
34     return a1.start < a2.start;
35 }
36 
37 //---------------------------------------------------------------------
38 
kpPreeditText()39 kpPreeditText::kpPreeditText ()
40     : m_cursorPosition (0), m_cursorColor (Qt::transparent),
41       m_selectionStart (0), m_selectionLength (0),
42       m_position (-1, -1)
43 {
44 }
45 
46 //---------------------------------------------------------------------
47 
kpPreeditText(const QInputMethodEvent * event)48 kpPreeditText::kpPreeditText (const QInputMethodEvent *event)
49     : m_cursorPosition (0), m_cursorColor (Qt::transparent),
50       m_selectionStart (0), m_selectionLength (0),
51       m_position (-1, -1)
52 {
53     m_preeditString = event->preeditString ();
54     for (const auto &attr : event->attributes ())
55     {
56         switch (attr.type)
57         {
58         case QInputMethodEvent::TextFormat:
59             m_textFormatList.append (attr);
60             break;
61         case QInputMethodEvent::Cursor:
62             m_cursorPosition = attr.start;
63             if (attr.length > 0)
64             {
65                 m_cursorColor = attr.value.value<QColor> ();
66             }
67             break;
68         case QInputMethodEvent::Selection:
69             m_selectionStart = attr.start;
70             m_selectionLength = attr.length;
71             break;
72         default:
73             break;
74         }
75     }
76     std::sort(m_textFormatList.begin(), m_textFormatList.end(), attributeLessThan);
77 }
78 
79 //---------------------------------------------------------------------
80 
isEmpty() const81 bool kpPreeditText::isEmpty () const
82 {
83     return m_preeditString.isEmpty ();
84 }
85 
86 //---------------------------------------------------------------------
87 
preeditString() const88 const QString &kpPreeditText::preeditString () const
89 {
90     return m_preeditString;
91 }
92 
93 //---------------------------------------------------------------------
94 
cursorPosition() const95 int kpPreeditText::cursorPosition () const
96 {
97     return m_cursorPosition;
98 }
99 
100 //---------------------------------------------------------------------
101 
cursorColor() const102 const QColor &kpPreeditText::cursorColor () const
103 {
104     return m_cursorColor;
105 }
106 
107 //---------------------------------------------------------------------
108 
selectionStart() const109 int kpPreeditText::selectionStart () const
110 {
111     return m_selectionStart;
112 }
113 
114 //---------------------------------------------------------------------
115 
selectionLength() const116 int kpPreeditText::selectionLength () const
117 {
118     return m_selectionLength;
119 }
120 
121 //---------------------------------------------------------------------
122 
textFormatList() const123 const QList<QInputMethodEvent::Attribute> &kpPreeditText::textFormatList () const
124 {
125     return m_textFormatList;
126 }
127 
128 //---------------------------------------------------------------------
129 
position() const130 const QPoint &kpPreeditText::position () const
131 {
132     return m_position;
133 }
134 
135 //---------------------------------------------------------------------
136 
setPosition(const QPoint & position)137 void kpPreeditText::setPosition (const QPoint &position)
138 {
139     m_position = position;
140 }
141 
142 //---------------------------------------------------------------------
143