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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
/***
Olive - Non-Linear Video Editor
Copyright (C) 2019 Olive Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include "sliderbase.h"
#include <QDebug>
#include <QEvent>
#include <QMessageBox>
#include "common/qtutils.h"
#include "core.h"
#include "window/mainwindow/mainwindow.h"
OLIVE_NAMESPACE_ENTER
SliderBase::SliderBase(Mode mode, QWidget *parent) :
QStackedWidget(parent),
drag_multiplier_(1.0),
has_min_(false),
has_max_(false),
mode_(mode),
dragged_diff_(0),
require_valid_input_(true),
tristate_(false),
drag_ladder_(nullptr),
ladder_element_count_(0),
dragged_(false)
{
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
label_ = new SliderLabel(this);
addWidget(label_);
editor_ = new FocusableLineEdit(this);
addWidget(editor_);
connect(label_, &SliderLabel::LabelPressed, this, &SliderBase::LabelPressed);
connect(label_, &SliderLabel::focused, this, &SliderBase::ShowEditor);
connect(label_, &SliderLabel::RequestReset, this, &SliderBase::ResetValue);
connect(editor_, &FocusableLineEdit::Confirmed, this, &SliderBase::LineEditConfirmed);
connect(editor_, &FocusableLineEdit::Cancelled, this, &SliderBase::LineEditCancelled);
// Set valid cursor based on mode
switch (mode_) {
case kString:
setCursor(Qt::PointingHandCursor);
SetValue("");
break;
case kInteger:
case kFloat:
setCursor(Qt::SizeHorCursor);
SetValue(0);
break;
}
}
void SliderBase::SetDragMultiplier(const double &d)
{
drag_multiplier_ = d;
}
void SliderBase::SetRequireValidInput(bool e)
{
require_valid_input_ = e;
}
void SliderBase::SetAlignment(Qt::Alignment alignment)
{
label_->setAlignment(alignment);
}
bool SliderBase::IsTristate() const
{
return tristate_;
}
void SliderBase::SetTristate()
{
tristate_ = true;
UpdateLabel(0);
}
bool SliderBase::IsDragging() const
{
return drag_ladder_;
}
void SliderBase::SetFormat(const QString &s)
{
custom_format_ = s;
ForceLabelUpdate();
}
void SliderBase::ClearFormat()
{
custom_format_.clear();
ForceLabelUpdate();
}
void SliderBase::ForceLabelUpdate()
{
UpdateLabel(Value());
}
const QVariant &SliderBase::Value() const
{
if (IsDragging()) {
return temp_dragged_value_;
}
return value_;
}
void SliderBase::SetValue(const QVariant &v)
{
if (IsDragging()) {
return;
}
value_ = ClampValue(v);
// Disable tristate
tristate_ = false;
UpdateLabel(value_);
}
void SliderBase::SetDefaultValue(const QVariant &v)
{
default_value_ = v;
}
void SliderBase::SetMinimumInternal(const QVariant &v)
{
min_value_ = v;
has_min_ = true;
// Limit value by this new minimum value
if (value_.toDouble() < min_value_.toDouble()) {
SetValue(min_value_);
}
}
void SliderBase::SetMaximumInternal(const QVariant &v)
{
max_value_ = v;
has_max_ = true;
// Limit value by this new maximum value
if (value_.toDouble() > max_value_.toDouble()) {
SetValue(max_value_);
}
}
void SliderBase::changeEvent(QEvent *e)
{
if (e->type() == QEvent::LanguageChange) {
UpdateLabel(value_);
}
QStackedWidget::changeEvent(e);
}
const QVariant &SliderBase::ClampValue(const QVariant &v)
{
if (has_min_ && v.toDouble() < min_value_.toDouble()) {
return min_value_;
}
if (has_max_ && v.toDouble() > max_value_.toDouble()) {
return max_value_;
}
return v;
}
QString SliderBase::GetFormat() const
{
if (custom_format_.isEmpty()) {
return QStringLiteral("%1");
} else {
return custom_format_;
}
}
void SliderBase::RepositionLadder()
{
QPoint label_global_pos = label_->mapToGlobal(label_->pos());
int text_width = QFontMetricsWidth(label_->fontMetrics(), label_->text());
QPoint ladder_pos(label_global_pos.x(),
label_global_pos.y() + label_->height() / 2 - drag_ladder_->height() / 2);
if (ladder_element_count_ > 0) {
ladder_pos.setX(ladder_pos.x() + text_width + QFontMetricsWidth(label_->fontMetrics(), QStringLiteral("H")));
} else {
ladder_pos.setX(ladder_pos.x() + text_width / 2 - drag_ladder_->width() / 2);
}
drag_ladder_->move(ladder_pos);
}
void SliderBase::UpdateLabel(const QVariant &v)
{
if (tristate_) {
label_->setText("---");
} else {
label_->setText(GetFormat().arg(ValueToString(v)));
}
}
double SliderBase::AdjustDragDistanceInternal(const double &start, const double &drag)
{
return start + drag;
}
QString SliderBase::ValueToString(const QVariant &v)
{
return v.toString();
}
QVariant SliderBase::StringToValue(const QString &s, bool *ok)
{
*ok = true;
return s;
}
void SliderBase::ShowEditor()
{
// This was a simple click
// Load label's text into editor
editor_->setText(ValueToString(value_));
// Show editor
setCurrentWidget(editor_);
// Select all text in the editor
editor_->setFocus();
editor_->selectAll();
}
void SliderBase::LabelPressed()
{
switch (mode_) {
case kString:
// No dragging supported for strings
break;
case kInteger:
case kFloat:
{
drag_ladder_ = new SliderLadder(drag_multiplier_, ladder_element_count_);
drag_ladder_->SetValue(ValueToString(value_));
drag_ladder_->show();
RepositionLadder();
connect(drag_ladder_, &SliderLadder::DraggedByValue, this, &SliderBase::LadderDragged);
connect(drag_ladder_, &SliderLadder::Released, this, &SliderBase::LadderReleased);
break;
}
}
}
void SliderBase::LadderDragged(int value, double multiplier)
{
dragged_ = true;
switch (mode_) {
case kString:
// No dragging supported for strings
break;
case kInteger:
case kFloat:
{
dragged_diff_ += value * drag_multiplier_ * multiplier;
double drag_val = AdjustDragDistanceInternal(value_.toDouble(), dragged_diff_);
// Update temporary value
if (mode_ == kInteger) {
temp_dragged_value_ = qRound(drag_val);
} else {
temp_dragged_value_ = drag_val;
}
QVariant clamped = ClampValue(temp_dragged_value_);
if (clamped != temp_dragged_value_) {
temp_dragged_value_ = clamped;
dragged_diff_ = temp_dragged_value_.toDouble() - value_.toDouble();
}
UpdateLabel(temp_dragged_value_);
drag_ladder_->SetValue(ValueToString(temp_dragged_value_));
RepositionLadder();
emit ValueChanged(temp_dragged_value_);
break;
}
}
}
void SliderBase::LadderReleased()
{
drag_ladder_->deleteLater();
drag_ladder_ = nullptr;
dragged_diff_ = 0;
if (dragged_) {
// This was a drag
switch (mode_) {
case kString:
// No-op
break;
case kInteger:
SetValue(temp_dragged_value_.toInt());
break;
case kFloat:
SetValue(temp_dragged_value_.toDouble());
break;
}
emit ValueChanged(value_);
dragged_ = false;
} else {
ShowEditor();
}
}
void SliderBase::LineEditConfirmed()
{
bool is_valid = true;
QVariant test_val = StringToValue(editor_->text(), &is_valid);
// Ensure editor doesn't signal that the focus is lost
editor_->blockSignals(true);
label_->blockSignals(true);
if (is_valid) {
SetValue(test_val);
setCurrentWidget(label_);
emit ValueChanged(value_);
} else if (require_valid_input_ && !IsTristate()) {
QMessageBox::critical(this,
tr("Invalid Value"),
tr("The entered value is not valid for this field."),
QMessageBox::Ok);
// Refocus editor
editor_->setFocus();
}
editor_->blockSignals(false);
label_->blockSignals(false);
}
void SliderBase::LineEditCancelled()
{
// Ensure editor doesn't signal that the focus is lost
editor_->blockSignals(true);
label_->blockSignals(true);
// Set widget back to label
setCurrentWidget(label_);
editor_->blockSignals(false);
label_->blockSignals(false);
}
void SliderBase::ResetValue()
{
if (!default_value_.isNull()) {
SetValue(default_value_);
emit ValueChanged(value_);
}
}
OLIVE_NAMESPACE_EXIT
|