-
Notifications
You must be signed in to change notification settings - Fork 0
/
homewidget.cpp
66 lines (56 loc) · 2.24 KB
/
homewidget.cpp
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
#include "homewidget.h"
class SquareButton : public QPushButton {
public:
SquareButton(const QString& text, QWidget* parent = nullptr)
: QPushButton(text, parent) {
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
setMinimumSize(100, 100);
}
QSize sizeHint() const override {
int length = qMin(width(), height());
return QSize(length, length);
}
void paintEvent(QPaintEvent* event) override {
QRectF rect = QRectF(contentsRect()).adjusted(4, 4, -4, -4); // Adjust the margins
QFont font = this->font();
int fontSize = calculateFontSize(rect);
font.setPointSize(fontSize);
this->setFont(font);
QPushButton::paintEvent(event);
}
private:
int calculateFontSize(const QRectF& rect) const {
QFont font = QApplication::font();
int fontSize = 100;
while (true) {
font.setPointSize(fontSize);
QFontMetrics metrics(font);
QRect textRect = metrics.boundingRect(text());
if (textRect.width() <= rect.width() && textRect.height() <= rect.height()) {
// Text fits within the rectangle
return fontSize;
}
// Reduce the font size
--fontSize;
// Break the loop if the font size becomes too small
if (fontSize <= 0) {
break;
}
}
// Return a default font size if the loop doesn't find a suitable size
return QApplication::font().pointSize();
}
};
HomeWidget::HomeWidget(QWidget *parent) : QWidget(parent)
{
viewAllFlashcardsButton = new SquareButton("View All FlashCards");
addNewFlashcardsButton = new SquareButton("Add a New FlashCard");
reviewFlashcardsButton = new SquareButton("Review Todays FlashCards");
syncFlashcardsButton = new SquareButton("Sync Flashcards");
QGridLayout * mainLayout = new QGridLayout;
mainLayout->addWidget(reviewFlashcardsButton, 0, 0);
mainLayout->addWidget(addNewFlashcardsButton, 0, 1);
mainLayout->addWidget(viewAllFlashcardsButton, 1, 0);
mainLayout->addWidget(syncFlashcardsButton, 1,1);
setLayout(mainLayout);
}