2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2014-12-01 10:47:38 +00:00
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
Telegram Desktop 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.
|
|
|
|
|
|
|
|
It 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.
|
|
|
|
|
2015-10-03 13:16:42 +00:00
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2017-01-11 18:31:31 +00:00
|
|
|
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
2016-11-11 13:46:04 +00:00
|
|
|
#include "ui/abstract_button.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-11-11 13:46:04 +00:00
|
|
|
namespace Ui {
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-02-11 11:24:37 +00:00
|
|
|
void AbstractButton::leaveEventHook(QEvent *e) {
|
2016-12-05 11:01:08 +00:00
|
|
|
if (_state & StateFlag::Down) return;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-11-11 13:46:04 +00:00
|
|
|
setOver(false, StateChangeSource::ByHover);
|
2017-02-11 11:24:37 +00:00
|
|
|
return TWidget::leaveEventHook(e);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2017-02-11 11:24:37 +00:00
|
|
|
void AbstractButton::enterEventHook(QEvent *e) {
|
2016-12-05 11:01:08 +00:00
|
|
|
checkIfOver(mapFromGlobal(QCursor::pos()));
|
2017-02-11 11:24:37 +00:00
|
|
|
return TWidget::enterEventHook(e);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-11 13:46:04 +00:00
|
|
|
void AbstractButton::setAcceptBoth(bool acceptBoth) {
|
2014-05-30 08:53:19 +00:00
|
|
|
_acceptBoth = acceptBoth;
|
|
|
|
}
|
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
void AbstractButton::checkIfOver(QPoint localPos) {
|
|
|
|
auto over = rect().marginsRemoved(getMargins()).contains(localPos);
|
|
|
|
setOver(over, StateChangeSource::ByHover);
|
|
|
|
}
|
|
|
|
|
2016-11-11 13:46:04 +00:00
|
|
|
void AbstractButton::mousePressEvent(QMouseEvent *e) {
|
2016-12-05 11:01:08 +00:00
|
|
|
checkIfOver(e->pos());
|
2016-12-02 19:16:35 +00:00
|
|
|
if (_acceptBoth || (e->buttons() & Qt::LeftButton)) {
|
2016-12-05 11:01:08 +00:00
|
|
|
if ((_state & StateFlag::Over) && !(_state & StateFlag::Down)) {
|
|
|
|
auto was = _state;
|
|
|
|
_state |= StateFlag::Down;
|
|
|
|
onStateChanged(was, StateChangeSource::ByPress);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
e->accept();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-11 13:46:04 +00:00
|
|
|
void AbstractButton::mouseMoveEvent(QMouseEvent *e) {
|
2016-12-02 19:16:35 +00:00
|
|
|
if (rect().marginsRemoved(getMargins()).contains(e->pos())) {
|
2016-11-11 13:46:04 +00:00
|
|
|
setOver(true, StateChangeSource::ByHover);
|
2014-05-30 08:53:19 +00:00
|
|
|
} else {
|
2016-11-11 13:46:04 +00:00
|
|
|
setOver(false, StateChangeSource::ByHover);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-11 13:46:04 +00:00
|
|
|
void AbstractButton::mouseReleaseEvent(QMouseEvent *e) {
|
2016-12-05 11:01:08 +00:00
|
|
|
if (_state & StateFlag::Down) {
|
|
|
|
auto was = _state;
|
|
|
|
_state &= ~State(StateFlag::Down);
|
|
|
|
onStateChanged(was, StateChangeSource::ByPress);
|
|
|
|
if (was & StateFlag::Over) {
|
2014-12-20 21:33:08 +00:00
|
|
|
_modifiers = e->modifiers();
|
2016-08-17 15:14:08 +00:00
|
|
|
if (_clickedCallback) {
|
|
|
|
_clickedCallback();
|
2016-11-24 19:28:23 +00:00
|
|
|
} else {
|
|
|
|
emit clicked();
|
2016-08-17 15:14:08 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
} else {
|
2016-12-05 11:01:08 +00:00
|
|
|
setOver(false, StateChangeSource::ByHover);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-31 13:34:41 +00:00
|
|
|
void AbstractButton::setPointerCursor(bool enablePointerCursor) {
|
|
|
|
if (_enablePointerCursor != enablePointerCursor) {
|
|
|
|
_enablePointerCursor = enablePointerCursor;
|
|
|
|
updateCursor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-11 13:46:04 +00:00
|
|
|
void AbstractButton::setOver(bool over, StateChangeSource source) {
|
2016-12-05 11:01:08 +00:00
|
|
|
if (over && !(_state & StateFlag::Over)) {
|
|
|
|
auto was = _state;
|
|
|
|
_state |= StateFlag::Over;
|
|
|
|
onStateChanged(was, source);
|
|
|
|
} else if (!over && (_state & StateFlag::Over)) {
|
|
|
|
auto was = _state;
|
|
|
|
_state &= ~State(StateFlag::Over);
|
|
|
|
onStateChanged(was, source);
|
2014-09-29 02:47:30 +00:00
|
|
|
}
|
2016-12-31 13:34:41 +00:00
|
|
|
updateCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractButton::updateCursor() {
|
|
|
|
auto pointerCursor = _enablePointerCursor && (_state & StateFlag::Over);
|
|
|
|
setCursor(pointerCursor ? style::cur_pointer : style::cur_default);
|
2014-09-29 02:47:30 +00:00
|
|
|
}
|
|
|
|
|
2016-11-11 13:46:04 +00:00
|
|
|
void AbstractButton::setDisabled(bool disabled) {
|
2016-12-05 11:01:08 +00:00
|
|
|
auto was = _state;
|
|
|
|
if (disabled && !(_state & StateFlag::Disabled)) {
|
|
|
|
_state |= StateFlag::Disabled;
|
|
|
|
onStateChanged(was, StateChangeSource::ByUser);
|
|
|
|
} else if (!disabled && (_state & StateFlag::Disabled)) {
|
|
|
|
_state &= ~State(StateFlag::Disabled);
|
|
|
|
onStateChanged(was, StateChangeSource::ByUser);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-11 13:46:04 +00:00
|
|
|
void AbstractButton::clearState() {
|
2016-12-05 11:01:08 +00:00
|
|
|
auto was = _state;
|
|
|
|
_state = StateFlag::None;
|
|
|
|
onStateChanged(was, StateChangeSource::ByUser);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-11-11 13:46:04 +00:00
|
|
|
|
|
|
|
} // namespace Ui
|