-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhellowidget.cc
113 lines (92 loc) · 3 KB
/
hellowidget.cc
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
#include "hellowidget.hpp"
#include <QtWidgets>
namespace Plugin {
class HelloWidget::HelloWidgetPrivate
{
public:
explicit HelloWidgetPrivate(HelloWidget *q)
: q_ptr(q)
{
stackedWidget = new QStackedWidget(q_ptr);
while (labels.size() < labelsCount) {
auto *label = new QLabel(q_ptr);
label->setObjectName("HomeLabel");
label->setAlignment(Qt::AlignCenter);
label->setWordWrap(true);
labels.append(label);
stackedWidget->addWidget(label);
}
previousButton = new QToolButton(q_ptr);
nextButton = new QToolButton(q_ptr);
}
void setupUI()
{
auto *buttonLayout = new QHBoxLayout;
buttonLayout->addWidget(previousButton);
buttonLayout->addStretch();
buttonLayout->addWidget(nextButton);
auto *layout = new QVBoxLayout(q_ptr);
layout->setContentsMargins(30, 30, 30, 30);
layout->addWidget(stackedWidget);
layout->addLayout(buttonLayout);
}
void enableButtons()
{
previousButton->setEnabled(stackedWidget->currentIndex() > 0);
nextButton->setEnabled(stackedWidget->currentIndex() < labelsCount - 1);
}
HelloWidget *q_ptr;
QStackedWidget *stackedWidget;
QVector<QLabel *> labels;
const int labelsCount = 4;
QToolButton *previousButton;
QToolButton *nextButton;
};
HelloWidget::HelloWidget(QWidget *parent)
: QWidget{parent}
, d_ptr(new HelloWidgetPrivate(this))
{
d_ptr->setupUI();
buildConnect();
setupTr();
QMetaObject::invokeMethod(this, &HelloWidget::onNext, Qt::QueuedConnection);
}
HelloWidget::~HelloWidget() = default;
void HelloWidget::onPrevious()
{
d_ptr->stackedWidget->setCurrentIndex((d_ptr->stackedWidget->currentIndex() - 1)
% d_ptr->labelsCount);
d_ptr->enableButtons();
}
void HelloWidget::onNext()
{
d_ptr->stackedWidget->setCurrentIndex((d_ptr->stackedWidget->currentIndex() + 1)
% d_ptr->labelsCount);
d_ptr->enableButtons();
}
void HelloWidget::changeEvent(QEvent *event)
{
QWidget::changeEvent(event);
switch (event->type()) {
case QEvent::LanguageChange: setupTr(); break;
default: break;
}
}
void HelloWidget::setupTr()
{
QStringList list{tr("Hello there! How's your day going so far?"),
tr("Hi, it's great to see you again!"),
tr("Good morning/afternoon/evening! How are you?"),
tr("Hey, hope you're having a wonderful day!")};
for (int i = 0; i < d_ptr->labelsCount; ++i) {
d_ptr->labels[i]->setText(list[i]);
}
d_ptr->previousButton->setText(tr("Previous"));
d_ptr->nextButton->setText(tr("Next"));
}
void HelloWidget::buildConnect()
{
connect(d_ptr->previousButton, &QToolButton::clicked, this, &HelloWidget::onPrevious);
connect(d_ptr->nextButton, &QToolButton::clicked, this, &HelloWidget::onNext);
}
} // namespace Plugin