-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsheet.cpp
More file actions
135 lines (124 loc) · 4.57 KB
/
sheet.cpp
File metadata and controls
135 lines (124 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "sheet.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPalette>
#include <QTimer>
#include <QResizeEvent>
#include <QPushButton>
#include <QIcon>
#include <QShortcut>
#include <QKeySequence>
Sheet::Sheet(QWidget *content, QWidget *parent)
: QWidget(parent),
m_content(content)
{
m_destination = nullptr;
m_backdrop = new AnimatedWidget(this);
m_sidepanel = new AnimatedWidget(this);
resize(0, 0);
if (m_content) {
m_content->hide();
}
m_backdrop->setBackgroundColor(QColor::fromRgbF(0, 0, 0, 0.0));
m_backdrop->hide();
// Set sidepanel to white background
auto sidepanelPalette = m_sidepanel->palette();
sidepanelPalette.setColor(QPalette::Window, Qt::white);
m_sidepanel->setAutoFillBackground(true);
m_sidepanel->setPalette(sidepanelPalette);
auto contentLayout = new QVBoxLayout(m_sidepanel);
contentLayout->setContentsMargins(QMargins(0, 0, 0, 9));
m_sidepanel->hide();
// buttonsbar
m_buttonBar = new QWidget(this);
auto buttonBarLayout = new QHBoxLayout(m_buttonBar);
buttonBarLayout->addStretch();
buttonBarLayout->setContentsMargins(0, 2, 0, 1);
auto closeButton = new QPushButton(this);
connect(closeButton, &QPushButton::clicked, this, [this] {
this->hideSheet(true);
});
closeButton->setFlat(true);
closeButton->setIcon(QIcon::fromTheme("window-close-symbolic"));
buttonBarLayout->addWidget(closeButton);
QShortcut* shortcut = new QShortcut(QKeySequence("Escape"), this);
connect(shortcut, &QShortcut::activated, this, [this] {
if (m_backdrop->isVisible()) {
hideSheet(true);
}
});
}
Sheet::~Sheet() {
}
void Sheet::showSheet(QWidget *destination, Side side) {
if (!destination) {
hideSheet(false);
return;
}
m_side = side;
m_content->show();
// remove all children from the sidepanel
while (auto child = m_sidepanel->findChild<QWidget *>()) {
child->setParent(nullptr);
}
m_buttonBar->setParent(m_sidepanel);
auto contentLayout = dynamic_cast<QVBoxLayout*>(m_sidepanel->layout());
contentLayout->addWidget(m_buttonBar, 0);
if (m_content) {
m_content->setParent(m_sidepanel);
contentLayout->addWidget(m_content, 1);
} else {
contentLayout->addStretch();
}
// show the panel backdeop all over the destination widget
// animate the color, also slide in the sidepanel on top
// of the backdrop
m_destination = destination;
m_backdrop->setParent(destination);
m_backdrop->show();
m_sidepanel->setParent(destination);
m_sidepanel->show();
m_buttonBar->setVisible(m_showTopRightCloseButton);
connect(destination, &QWidget::destroyed, [this] {
hideSheet(false);
});
QTimer::singleShot(0, [this]() {
layout(false);
m_sidepanel->setPosition(QPoint(m_side == Side::Left ? -m_width : m_backdrop->width(), 0));
layout(true);
m_sidepanel->setPosition(QPoint(m_side == Side::Left ? -m_width : m_backdrop->width(), 0));
m_backdrop->setBackgroundColor(QColor::fromRgb(0, 0, 0, 0));
});
}
void Sheet::hideSheet(bool animated) {
m_destination = nullptr;
if (animated) {
m_sidepanel->setPositionA(QPoint(m_side == Side::Left ? -m_width : m_backdrop->width(), 0), m_hideDurationMs, [this] {
m_backdrop->hide();
m_sidepanel->hide();
});
m_backdrop->setBackgroundColorA(QColor::fromRgb(0, 0, 0, 0), m_hideDurationMs);
} else {
m_sidepanel->setPosition(QPoint(m_side == Side::Left ? -m_width : m_backdrop->width(), 0));
m_backdrop->hide();
m_sidepanel->hide();
m_backdrop->setBackgroundColor(QColor::fromRgb(0, 0, 0, 0));
}
}
void Sheet::layout(bool animated) {
if (m_destination) {
// resize the backdrop to full size of the destination widget
m_backdrop->resize(m_destination->size());
m_backdrop->move(0, 0);
// layout the sidepanel on the outside so that it can slide in
const auto panelWidth = m_width;
m_sidepanel->resize(panelWidth, m_backdrop->size().height());
if (animated) {
m_sidepanel->setPositionA(QPoint(m_side == Side::Left ? 0 : m_backdrop->width() - panelWidth, 0), m_showDurationMs);
m_backdrop->setBackgroundColorA(QColor::fromRgb(0, 0, 0, 192), m_showDurationMs);
} else {
m_sidepanel->setPosition(QPoint(m_side == Side::Left ? 0 : m_backdrop->width() - panelWidth, 0));
m_backdrop->setBackgroundColor(QColor::fromRgb(0, 0, 0, 192));
}
}
}