tdesktop/Telegram/SourceFiles/ui/abstract_button.cpp

116 lines
3.2 KiB
C++
Raw Normal View History

/*
This file is part of Telegram Desktop,
the official desktop version of Telegram messaging app, see https://telegram.org
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.
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
2016-02-08 10:56:18 +00:00
Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
*/
#include "stdafx.h"
#include "ui/abstract_button.h"
namespace Ui {
void AbstractButton::leaveEvent(QEvent *e) {
if (_state & StateDown) return;
setOver(false, StateChangeSource::ByHover);
return TWidget::leaveEvent(e);
}
void AbstractButton::enterEvent(QEvent *e) {
2016-12-02 19:16:35 +00:00
auto over = rect().marginsRemoved(getMargins()).contains(mapFromGlobal(QCursor::pos()));
setOver(over, StateChangeSource::ByHover);
return TWidget::enterEvent(e);
}
void AbstractButton::setAcceptBoth(bool acceptBoth) {
_acceptBoth = acceptBoth;
}
void AbstractButton::mousePressEvent(QMouseEvent *e) {
2016-12-02 19:16:35 +00:00
if (_acceptBoth || (e->buttons() & Qt::LeftButton)) {
if ((_state & StateOver) && !(_state & StateDown)) {
int oldState = _state;
_state |= StateDown;
onStateChanged(oldState, StateChangeSource::ByPress);
e->accept();
}
}
}
void AbstractButton::mouseMoveEvent(QMouseEvent *e) {
2016-12-02 19:16:35 +00:00
if (rect().marginsRemoved(getMargins()).contains(e->pos())) {
setOver(true, StateChangeSource::ByHover);
} else {
setOver(false, StateChangeSource::ByHover);
}
}
void AbstractButton::mouseReleaseEvent(QMouseEvent *e) {
if (_state & StateDown) {
int oldState = _state;
_state &= ~StateDown;
onStateChanged(oldState, StateChangeSource::ByPress);
if (oldState & StateOver) {
_modifiers = e->modifiers();
if (_clickedCallback) {
_clickedCallback();
2016-11-24 19:28:23 +00:00
} else {
emit clicked();
}
} else {
leaveEvent(e);
}
}
}
void AbstractButton::setOver(bool over, StateChangeSource source) {
2016-12-02 19:16:35 +00:00
setCursor(over ? style::cur_pointer : style::cur_default);
if (over && !(_state & StateOver)) {
int oldState = _state;
_state |= StateOver;
onStateChanged(oldState, source);
} else if (!over && (_state & StateOver)) {
int oldState = _state;
_state &= ~StateOver;
onStateChanged(oldState, source);
}
}
void AbstractButton::setDisabled(bool disabled) {
int oldState = _state;
if (disabled && !(_state & StateDisabled)) {
_state |= StateDisabled;
onStateChanged(oldState, StateChangeSource::ByUser);
} else if (!disabled && (_state & StateDisabled)) {
_state &= ~StateDisabled;
onStateChanged(oldState, StateChangeSource::ByUser);
}
}
void AbstractButton::clearState() {
int oldState = _state;
_state = StateNone;
onStateChanged(oldState, StateChangeSource::ByUser);
}
int AbstractButton::getState() const {
return _state;
}
} // namespace Ui