| 1 |
/* |
| 2 |
* Audacity: A Digital Audio Editor |
| 3 |
*/ |
| 4 |
#include "fieldsinteractioncontroller.h" |
| 5 |
|
| 6 |
#include <QKeyEvent> |
| 7 |
|
| 8 |
#include "log.h" |
| 9 |
|
| 10 |
using namespace au::playback; |
| 11 |
|
| 12 |
static bool isFieldEditable(const QChar& fieldSymbol) |
| 13 |
{ |
| 14 |
return fieldSymbol.isDigit(); |
| 15 |
} |
| 16 |
|
| 17 |
FieldsInteractionController::FieldsInteractionController(QObject* parent) |
| 18 |
: QObject(parent) |
| 19 |
{ |
| 20 |
} |
| 21 |
|
| 22 |
void FieldsInteractionController::setFormatter(const std::shared_ptr<TimecodeFormatter>& formatter) |
| 23 |
{ |
| 24 |
m_formatter = formatter; |
| 25 |
} |
| 26 |
|
| 27 |
QString FieldsInteractionController::valueString() const |
| 28 |
{ |
| 29 |
return m_valueString; |
| 30 |
} |
| 31 |
|
| 32 |
void FieldsInteractionController::setValueString(const QString& valueString) |
| 33 |
{ |
| 34 |
m_valueString = valueString; |
| 35 |
} |
| 36 |
|
| 37 |
int FieldsInteractionController::currentEditedFieldIndex() const |
| 38 |
{ |
| 39 |
return m_currentEditedFieldIndex; |
| 40 |
} |
| 41 |
|
| 42 |
void FieldsInteractionController::setCurrentEditedFieldIndex(int index) |
| 43 |
{ |
| 44 |
if (m_currentEditedFieldIndex == index) { |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
if (index >= 0) { |
| 49 |
qApp->installEventFilter(this); |
| 50 |
} else { |
| 51 |
qApp->removeEventFilter(this); |
| 52 |
} |
| 53 |
|
| 54 |
m_currentEditedFieldIndex = index; |
| 55 |
emit currentEditedFieldIndexChanged(); |
| 56 |
} |
| 57 |
|
| 58 |
QQuickItem* FieldsInteractionController::visualItem() const |
| 59 |
{ |
| 60 |
return m_visualItem; |
| 61 |
} |
| 62 |
|
| 63 |
void FieldsInteractionController::setVisualItem(QQuickItem* newVisualItem) |
| 64 |
{ |
| 65 |
if (m_visualItem == newVisualItem) { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
m_visualItem = newVisualItem; |
| 70 |
emit visualItemChanged(); |
| 71 |
} |
| 72 |
|
| 73 |
int FieldsInteractionController::rowCount() const |
| 74 |
{ |
| 75 |
return m_valueString.size(); |
| 76 |
} |
| 77 |
|
| 78 |
bool FieldsInteractionController::eventFilter(QObject* watched, QEvent* event) |
| 79 |
{ |
| 80 |
if (event->type() == QEvent::ShortcutOverride) { |
| 81 |
bool needOverride = needOverrideShortcut(event); |
| 82 |
if (needOverride) { |
| 83 |
event->accept(); |
| 84 |
return true; |
| 85 |
} else { |
| 86 |
finishEditing(); |
| 87 |
return false; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
if (event->type() == QEvent::KeyPress) { |
| 92 |
QKeyEvent* keyEvent = dynamic_cast<QKeyEvent*>(event); |
| 93 |
int key = keyEvent->key(); |
| 94 |
if (key == Qt::Key_Escape) { |
| 95 |
finishEditing(); |
| 96 |
return true; |
| 97 |
} else if (key >= Qt::Key_0 && key <= Qt::Key_9) { |
| 98 |
QString newValueStr = m_valueString; |
| 99 |
newValueStr.replace(m_currentEditedFieldIndex, 1, QChar('0' + (key - Qt::Key_0))); |
| 100 |
|
| 101 |
emit valueChanged(m_formatter->stringToValue(newValueStr).value()); |
| 102 |
|
| 103 |
moveCurrentEditedField(Qt::Key_Right); |
| 104 |
|
| 105 |
return true; |
| 106 |
} else if (key == Qt::Key_Left || key == Qt::Key_Right) { |
| 107 |
moveCurrentEditedField(key); |
| 108 |
return true; |
| 109 |
} else if (key == Qt::Key_Up || key == Qt::Key_Down) { |
| 110 |
adjustCurrentEditedField(key); |
| 111 |
return true; |
| 112 |
} |
| 113 |
} else if (event->type() == QEvent::MouseButtonPress) { |
| 114 |
if (!isMouseWithinBoundaries(QCursor::pos())) { |
| 115 |
finishEditing(); |
| 116 |
} |
| 117 |
} else if (event->type() == QEvent::Wheel && isMouseWithinBoundaries(QCursor::pos())) { |
| 118 |
QWheelEvent* wheelEvent = dynamic_cast<QWheelEvent*>(event); |
| 119 |
scrollCurrentEditedField(wheelEvent->pixelDelta().y(), wheelEvent->angleDelta().y()); |
| 120 |
return true; |
| 121 |
} |
| 122 |
|
| 123 |
return QObject::eventFilter(watched, event); |
| 124 |
} |
| 125 |
|
| 126 |
bool FieldsInteractionController::needOverrideShortcut(QEvent* event) const |
| 127 |
{ |
| 128 |
int key = dynamic_cast<QKeyEvent*>(event)->key(); |
| 129 |
|
| 130 |
bool isDigit = key >= Qt::Key_0 && key <= Qt::Key_9; |
| 131 |
if (isDigit) { |
| 132 |
return true; |
| 133 |
} |
| 134 |
|
| 135 |
static const std::set<int> navigationKeys { |
| 136 |
Qt::Key_Left, |
| 137 |
Qt::Key_Right, |
| 138 |
Qt::Key_Up, |
| 139 |
Qt::Key_Down, |
| 140 |
|
| 141 |
Qt::Key_Escape, |
| 142 |
Qt::Key_Enter, |
| 143 |
Qt::Key_Return |
| 144 |
}; |
| 145 |
|
| 146 |
return muse::contains(navigationKeys, key); |
| 147 |
} |
| 148 |
|
| 149 |
bool FieldsInteractionController::isMouseWithinBoundaries(const QPoint& mousePos) const |
| 150 |
{ |
| 151 |
if (!m_visualItem) { |
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
QRectF viewRect = m_visualItem->boundingRect(); |
| 156 |
viewRect.moveTopLeft(m_visualItem->mapToGlobal(QPoint(0, 0))); |
| 157 |
|
| 158 |
return viewRect.contains(mousePos); |
| 159 |
} |
| 160 |
|
| 161 |
void FieldsInteractionController::moveCurrentEditedField(int moveKey) |
| 162 |
{ |
| 163 |
int newIndex = m_currentEditedFieldIndex; |
| 164 |
while (true) { |
| 165 |
if (moveKey == Qt::Key_Left) { |
| 166 |
newIndex--; |
| 167 |
} else { |
| 168 |
newIndex++; |
| 169 |
} |
| 170 |
|
| 171 |
if (newIndex < 0) { |
| 172 |
newIndex = rowCount() - 1; |
| 173 |
} else if (newIndex >= rowCount()) { |
| 174 |
newIndex = 0; |
| 175 |
} |
| 176 |
|
| 177 |
if (isFieldEditable(m_valueString[newIndex])) { |
| 178 |
break; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
setCurrentEditedFieldIndex(newIndex); |
| 183 |
} |
| 184 |
|
| 185 |
void FieldsInteractionController::adjustCurrentEditedField(int adjustKey) |
| 186 |
{ |
| 187 |
int digitIndex = 0; |
| 188 |
|
| 189 |
for (int i = 0; i < m_valueString.size(); ++i) { |
| 190 |
if (i == m_currentEditedFieldIndex) { |
| 191 |
break; |
| 192 |
} |
| 193 |
|
| 194 |
if (isFieldEditable(m_valueString[i])) { |
| 195 |
digitIndex++; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
emit valueChanged(m_formatter->singleStep(m_valueString, digitIndex, adjustKey == Qt::Key_Up)); |
| 200 |
} |
| 201 |
|
| 202 |
void FieldsInteractionController::scrollCurrentEditedField(int pixelsYScrolled, int stepsYScrolled) |
| 203 |
{ |
| 204 |
int oneScroll = -1; |
| 205 |
|
| 206 |
if (pixelsYScrolled != 0) { |
| 207 |
m_scrolled += pixelsYScrolled; |
| 208 |
oneScroll = QWheelEvent::DefaultDeltasPerStep / 2; |
| 209 |
} else if (stepsYScrolled != 0) { |
| 210 |
m_scrolled += stepsYScrolled; |
| 211 |
oneScroll = QWheelEvent::DefaultDeltasPerStep; |
| 212 |
} |
| 213 |
|
| 214 |
if (oneScroll == -1) { |
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
if (m_scrolled >= oneScroll) { |
| 219 |
adjustCurrentEditedField(Qt::Key_Up); |
| 220 |
m_scrolled = 0; |
| 221 |
} else if (m_scrolled <= -oneScroll) { |
| 222 |
adjustCurrentEditedField(Qt::Key_Down); |
| 223 |
m_scrolled = 0; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
void FieldsInteractionController::finishEditing() |
| 228 |
{ |
| 229 |
setCurrentEditedFieldIndex(-1); |
| 230 |
} |