Skip to content

Commit

Permalink
Divide info_profile_lines in different modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed Nov 16, 2017
1 parent a4c2138 commit faeb148
Show file tree
Hide file tree
Showing 15 changed files with 1,315 additions and 1,066 deletions.
142 changes: 142 additions & 0 deletions Telegram/SourceFiles/info/profile/info_profile_button.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
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.
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
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
*/
#include "info/profile/info_profile_button.h"

#include <rpl/never.h>
#include "ui/widgets/checkbox.h"
#include "styles/style_info.h"

namespace Info {
namespace Profile {

Button::Button(
QWidget *parent,
rpl::producer<QString> &&text)
: Button(parent, std::move(text), st::infoProfileButton) {
}

Button::Button(
QWidget *parent,
rpl::producer<QString> &&text,
const style::InfoProfileButton &st)
: RippleButton(parent, st.ripple)
, _st(st) {
std::move(text)
| rpl::start([this](QString &&value) {
setText(std::move(value));
}, lifetime());
}

Button *Button::toggleOn(rpl::producer<bool> &&toggled) {
_toggleOnLifetime.destroy();
_toggle = std::make_unique<Ui::ToggleView>(
isOver() ? _st.toggleOver : _st.toggle,
false,
[this] { rtlupdate(toggleRect()); });
clicks()
| rpl::start([this](auto) {
_toggle->setCheckedAnimated(!_toggle->checked());
}, _toggleOnLifetime);
std::move(toggled)
| rpl::start([this](bool toggled) {
_toggle->setCheckedAnimated(toggled);
}, _toggleOnLifetime);
_toggle->finishAnimation();
return this;
}

rpl::producer<bool> Button::toggledValue() const {
return _toggle ? _toggle->checkedValue() : rpl::never<bool>();
}

void Button::paintEvent(QPaintEvent *e) {
Painter p(this);

auto ms = getms();
auto paintOver = (isOver() || isDown());
p.fillRect(e->rect(), paintOver ? _st.textBgOver : _st.textBg);

paintRipple(p, 0, 0, ms);

auto outerw = width();
p.setFont(_st.font);
p.setPen(paintOver ? _st.textFgOver : _st.textFg);
p.drawTextLeft(
_st.padding.left(),
_st.padding.top(),
outerw,
_text,
_textWidth);

if (_toggle) {
auto rect = toggleRect();
_toggle->paint(p, rect.left(), rect.top(), outerw, ms);
}
}

QRect Button::toggleRect() const {
Expects(_toggle != nullptr);
auto size = _toggle->getSize();
auto left = width() - _st.toggleSkip - size.width();
auto top = (height() - size.height()) / 2;
return { QPoint(left, top), size };
}

int Button::resizeGetHeight(int newWidth) {
updateVisibleText(newWidth);
return _st.padding.top() + _st.height + _st.padding.bottom();
}

void Button::onStateChanged(
State was,
StateChangeSource source) {
RippleButton::onStateChanged(was, source);
if (_toggle) {
_toggle->setStyle(isOver() ? _st.toggleOver : _st.toggle);
}
}

void Button::setText(QString &&text) {
_original = std::move(text);
_originalWidth = _st.font->width(_original);
updateVisibleText(width());
}

void Button::updateVisibleText(int newWidth) {
auto availableWidth = newWidth
- _st.padding.left()
- _st.padding.right();
if (_toggle) {
availableWidth -= (width() - toggleRect().x());
}
accumulate_max(availableWidth, 0);
if (availableWidth < _originalWidth) {
_text = _st.font->elided(_original, availableWidth);
_textWidth = _st.font->width(_text);
} else {
_text = _original;
_textWidth = _originalWidth;
}
update();
}

} // namespace Profile
} // namespace Info
69 changes: 69 additions & 0 deletions Telegram/SourceFiles/info/profile/info_profile_button.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
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.
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
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
*/
#pragma once

#include "ui/widgets/buttons.h"

namespace Ui {
class ToggleView;
} // namespace Ui

namespace Info {
namespace Profile {

class Button : public Ui::RippleButton {
public:
Button(
QWidget *parent,
rpl::producer<QString> &&text);
Button(
QWidget *parent,
rpl::producer<QString> &&text,
const style::InfoProfileButton &st);

Button *toggleOn(rpl::producer<bool> &&toggled);
rpl::producer<bool> toggledValue() const;

protected:
int resizeGetHeight(int newWidth) override;
void onStateChanged(
State was,
StateChangeSource source) override;

void paintEvent(QPaintEvent *e) override;

private:
void setText(QString &&text);
QRect toggleRect() const;
void updateVisibleText(int newWidth);

const style::InfoProfileButton &_st;
QString _original;
QString _text;
int _originalWidth = 0;
int _textWidth = 0;
std::unique_ptr<Ui::ToggleView> _toggle;
rpl::lifetime _toggleOnLifetime;

};

} // namespace Profile
} // namespace Info
Loading

0 comments on commit faeb148

Please sign in to comment.