summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Studio/Controls/StringEdit.cpp
blob: 34b5f521957631083fc9375976b8eae3f374b09b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/****************************************************************************
**
** Copyright (C) 2002 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

//==============================================================================
//	Prefix
//==============================================================================
#include "stdafx.h"

//==============================================================================
//	Include
//==============================================================================
#include "StringEdit.h"
#include "OffscreenRenderer.h"
#include "CoreUtils.h"

//==============================================================================
/**
 * Constructor
 */
CStringEdit::CStringEdit()
    : m_AutoSize(false)
{
    // StringEdit is going to have to handle its own RevertText.
    // This can't be done in TextEdit because FloatEdit ( VectorEdit, etc ) are using the command
    // stack for undo/redo
    // StringEdit doesn't have command stack set until you hit enter or lose focus.
    // We can't make CTextEdit::RegisterCommands a virtual function since its being called in the
    // constructor,
    // therefore this is a pretty lame way to register for a hotkey.
    m_CommandHandler->RegisterKeyEvent(
        new CDynHotKeyConsumer<CStringEdit>(this, &CStringEdit::RevertText),
        Qt::ControlModifier, Qt::Key_Z);
}

//==============================================================================
/**
 * Destructor
 */
CStringEdit::~CStringEdit()
{
}

//==============================================================================
/**
 * Returns the string value of the control.
 */
Q3DStudio::CString CStringEdit::GetString()
{
    return m_Value;
}

//==============================================================================
/**
 * Sets the string value of the control.
 */
void CStringEdit::SetData(const Q3DStudio::CString &inValue, bool inFireEvent /*= true*/)
{
    if (m_Value != inValue) {
        m_Value = inValue;
        SetDisplayString(inValue, inFireEvent);

        if (inFireEvent)
            SetDirty(true);
    }
}

//==============================================================================
/**
 * Commits changes to the value of this control when the control loses focus.
 */
void CStringEdit::OnLoseFocus()
{
    CTextEdit::OnLoseFocus();
    FireCommitEvent();
}

//==============================================================================
/**
 * Commits changes to the value of this control when the Enter button is pressed.
 * @param inHighlight true to highlight the text after committing it
 */
void CStringEdit::EnterText(bool inHighlight)
{
    CTextEdit::EnterText(inHighlight);
    FireCommitEvent();
}

void CStringEdit::RefreshDisplayFromData()
{
    SetDisplayString(GetString());
    Invalidate();
}

bool CStringEdit::CanPaste()
{
    return true;
}

//==============================================================================
/**
 * Enables or disables auto-sizing of this control.  If auto-sizing is enabled
 * the size of this control will be automatically set to fit the text that it
 * contains.
 * @param inAllow true to enable auto-sizing, false to disable auto-sizing
 */
void CStringEdit::AllowAutoSize(bool inAllow)
{
    m_AutoSize = inAllow;
}

//==============================================================================
/**
 * @return true if auto-resizing is enabled, otherwise false
 */
bool CStringEdit::GetAllowAutoSize()
{
    return m_AutoSize;
}

//==============================================================================
/**
 * If auto-resizing is enabled, this function will resize this control so that
 * it is the same size as the text that it is displaying.  Text size is calculated
 * with an offscreen buffer, so this can be done outside of a draw operation.
 */
void CStringEdit::ResetSize()
{
    // If auto-resizing of the text field is enabled
    if (m_AutoSize) {
        // Resize the control to fit the text, plus the buffer gap
        COffscreenRenderer theRenderer(CRct(0, 0, 100, 16));
        CPt theSize;
        const auto textSize = theRenderer.GetTextSize(GetDisplayString().toQString());
        theSize.x = textSize.width() + GetBufferLength() * 3;
        theSize.y = textSize.height() + 1;
        SetMinimumSize(theSize);
        SetPreferredSize(theSize);
        SetMaximumSize(theSize);
    }
}

//==============================================================================
/**
 *	Primarily delegates up to the parent class, but responds by recalculating
 *	size of the text box for auto-sized strings.
 *
 *	@param inDirty true to mark this control as dirty, which causes the string to be redrawn
 *	false to mark the control as not needing to reevaluate its text during next draw cycle
 */
void CStringEdit::SetDirty(bool inDirty)
{
    // Allow the parent to handle this situation
    CTextEdit::SetDirty(inDirty);

    ResetSize();
}

//==============================================================================
/**
 * Reverts the displayed text to the previous text.
 */
void CStringEdit::RevertText()
{
    SetData(m_PreviousValue);
    SetDisplayString(m_PreviousValue);

    FireCommitEvent();
    SelectAllText();
}

//==============================================================================
/**
 * Handles any non-character keys that were pressed and need to be handled.
 *
 * @param inChar The key that was pressed
 * @param inFlags Indicates which modifier keys were down when event occurred
 * @return true if this function handled the character, otherwise false
 */
bool CStringEdit::HandleSpecialChar(unsigned int inChar, Qt::KeyboardModifiers inFlags)
{
    bool theMessageWasHandled = false;

    switch (inChar) {
    // Escape and Ctrl+Z both do basically the same thing for StringEdits,
    // They are special cased in TextEditInPlace though.
    case Qt::Key_Escape:
        RevertText();
        theMessageWasHandled = true;
        break;

    default:
        theMessageWasHandled = CTextEdit::HandleSpecialChar(inChar, inFlags);
    }

    return theMessageWasHandled;
}

//==============================================================================
/**
 * Called when this control gains focus.  Shows the caret, clears the current
 * selection and invalidates the control so that it will get redrawn.
 */
void CStringEdit::OnGainFocus()
{
    CTextEdit::OnGainFocus();
    m_PreviousValue = GetString();
}

//=============================================================================
/**
 * Handles character input from the keyboard.
 *
 * @param inChar Character that was pressed
 * @return true if the character was handled, false if this control does not
 * care about the character that was pressed
 */
bool CStringEdit::OnChar(const QString &inChar, Qt::KeyboardModifiers inFlags)
{
    return CTextEdit::OnChar(inChar, inFlags);
}