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