myEnsureResized() now forces recursive create.

This commit is contained in:
John Preston 2017-09-11 11:55:36 +03:00
parent 487ddb5694
commit 21d136e224
2 changed files with 99 additions and 19 deletions

View File

@ -120,29 +120,65 @@ QString GetOverride(const QString &familyName) {
namespace { namespace {
void _sendResizeEvents(QWidget *target) { class WidgetCreator : public QWidget {
QResizeEvent e(target->size(), QSize()); public:
QApplication::sendEvent(target, &e); static void Create(not_null<QWidget*> widget) {
volatile auto unknown = widget.get();
static_cast<WidgetCreator*>(unknown)->create();
}
const QObjectList children = target->children(); };
for (int i = 0; i < children.size(); ++i) {
QWidget *child = static_cast<QWidget*>(children.at(i)); void CreateWidgetStateRecursive(not_null<QWidget*> target) {
if (child->isWidgetType() && !child->isWindow() && child->testAttribute(Qt::WA_PendingResizeEvent)) { if (!target->testAttribute(Qt::WA_WState_Created)) {
_sendResizeEvents(child); if (!target->isWindow()) {
CreateWidgetStateRecursive(target->parentWidget());
} }
WidgetCreator::Create(target);
}
}
void SendPendingEventsRecursive(QWidget *target) {
auto wasVisible = target->isVisible();
if (!wasVisible) {
target->setAttribute(Qt::WA_WState_Visible, true);
}
if (target->testAttribute(Qt::WA_PendingMoveEvent)) {
target->setAttribute(Qt::WA_PendingMoveEvent, false);
QMoveEvent e(target->pos(), QPoint());
QApplication::sendEvent(target, &e);
}
if (target->testAttribute(Qt::WA_PendingResizeEvent)) {
target->setAttribute(Qt::WA_PendingResizeEvent, false);
QResizeEvent e(target->size(), QSize());
QApplication::sendEvent(target, &e);
}
auto &children = target->children();
for (auto i = 0; i < children.size(); ++i) {
auto child = children[i];
if (child->isWidgetType()) {
auto widget = static_cast<QWidget*>(child);
if (!widget->isWindow()) {
if (!widget->testAttribute(Qt::WA_WState_Created)) {
WidgetCreator::Create(widget);
}
SendPendingEventsRecursive(widget);
}
}
}
if (!wasVisible) {
target->setAttribute(Qt::WA_WState_Visible, false);
} }
} }
} // namespace } // namespace
bool TWidget::inFocusChain() const {
return !isHidden() && App::wnd() && (App::wnd()->focusWidget() == this || isAncestorOf(App::wnd()->focusWidget()));
}
void myEnsureResized(QWidget *target) { void myEnsureResized(QWidget *target) {
if (target && (target->testAttribute(Qt::WA_PendingResizeEvent) || !target->testAttribute(Qt::WA_WState_Created))) { if (!target) {
_sendResizeEvents(target); return;
} }
CreateWidgetStateRecursive(target);
SendPendingEventsRecursive(target);
} }
QPixmap myGrab(TWidget *target, QRect rect, QColor bg) { QPixmap myGrab(TWidget *target, QRect rect, QColor bg) {

View File

@ -29,6 +29,48 @@ QString GetOverride(const QString &familyName);
} // namespace } // namespace
template <typename Object>
class object_ptr;
namespace Ui {
inline bool InFocusChain(not_null<const QWidget*> widget) {
if (auto top = widget->window()) {
if (auto focused = top->focusWidget()) {
return !widget->isHidden()
&& (focused == widget
|| widget->isAncestorOf(focused));
}
}
return false;
}
template <typename ChildWidget>
inline ChildWidget *AttachParentChild(
not_null<QWidget*> parent,
const object_ptr<ChildWidget> &child) {
if (auto raw = child.data()) {
raw->setParent(parent);
raw->show();
return raw;
}
return nullptr;
}
template <typename ChildWidget>
inline ChildWidget *AttachParentChildToBottom(
not_null<QWidget*> parent,
const object_ptr<ChildWidget> &child) {
if (auto raw = AttachParentChild(parent, child)) {
raw->resizeToWidth(parent->width());
raw->move(0, parent->height());
return raw;
}
return nullptr;
}
} // namespace Ui
enum class RectPart { enum class RectPart {
None = 0, None = 0,
@ -303,19 +345,21 @@ public:
virtual void grabFinish() { virtual void grabFinish() {
} }
bool inFocusChain() const; bool inFocusChain() const {
return Ui::InFocusChain(this);
}
void hideChildren() { void hideChildren() {
for (auto child : children()) { for (auto child : children()) {
if (auto widget = qobject_cast<QWidget*>(child)) { if (child->isWidgetType()) {
widget->hide(); static_cast<QWidget*>(child)->hide();
} }
} }
} }
void showChildren() { void showChildren() {
for (auto child : children()) { for (auto child : children()) {
if (auto widget = qobject_cast<QWidget*>(child)) { if (child->isWidgetType()) {
widget->show(); static_cast<QWidget*>(child)->show();
} }
} }
} }