-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
100 lines (85 loc) · 4.59 KB
/
Copy pathmainwindow.cpp
File metadata and controls
100 lines (85 loc) · 4.59 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QScreen>
#include <QScrollArea>
#include <QSysInfo>
#include <QTabBar>
#include <QTimer>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle("4G/5G Theoretical Throughput Calculator");
// To address the poor high-DPI handling in Qt 5, we need to prevent
// text from being displayed incompletely within the tabs.
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QTabBar* tabBar = ui->tabWidget->tabBar();
if (tabBar) {
// Total horizontal spacing = (15*2) + (2*2) = 34px
const int horizontalPaddingAndBorder = 34;
// Use QFontMetrics to accurately calculate text width
QFontMetrics fm(tabBar->font());
int totalTabsWidth = 0;
for (int i = 0; i < tabBar->count(); ++i) {
QString tabText = tabBar->tabText(i);
int textWidth = fm.horizontalAdvance(tabText);
totalTabsWidth += (textWidth + horizontalPaddingAndBorder) * 2;
}
// Forcefully set the minimum width of the TabBar
tabBar->setMinimumWidth(totalTabsWidth);
}
#endif
// should be performed only on desktop systems
//#if defined(Q_OS_WIN) || defined(Q_OS_LINUX) || defined(Q_OS_MACOS) // or this one, statically
QTimer::singleShot(0, this, [=]() {
const QString productType = QSysInfo::productType();
if (productType == "windows" || productType == "linux" || productType == "macos") {
// Find the content widget inside the scroll area for both tabs
QWidget* content4g = ui->tab4g->findChild<QScrollArea*>("scrollArea")->widget();
QWidget* content5g = ui->tab5g->findChild<QScrollArea*>("scrollArea")->widget();
if (content4g && content5g) {
// Calculate the required width based on the content's size hint
int width4g = content4g->sizeHint().width();
int width5g = content5g->sizeHint().width();
const QRect screenGeometry = QGuiApplication::primaryScreen()->availableGeometry();
// qDebug() << "[INFO] Available screen geometry: " << screenGeometry;
// Use the maximum required width and add some padding (20%) for the window frame,
// layout margins, and the vertical scrollbar.
int origWidth = qMax(width4g, width5g);
int requiredWidth = (int)((float)origWidth * 1.2f);
// Set a fixed ratio (5/3 or 3/5) based on whether the width is greater than the height
const float ratio = ((screenGeometry.height() / screenGeometry.width()) >= 1) ? 1.33f : 0.6f;
// Set a reasonable default height to avoid compression
int requiredHeight = (int)((float)requiredWidth * ratio);
// IMPORTANT: If the required width is larger than the available screen width, it should be clamped
if (requiredWidth > screenGeometry.width()) {
// qDebug() << "[WARNING] Required width" << requiredWidth << "exceeds screen width" << screenGeometry.width() << ". Clamping width.";
requiredWidth = screenGeometry.width();
requiredHeight = (int)((float)requiredWidth * ratio); // maintain the ratio of height/width
}
// Similarily, check and correct the height
if (requiredHeight > screenGeometry.height()) {
// qDebug() << "[WARNING] Required height" << requiredHeight << "exceeds screen height" << screenGeometry.height() << ". Clamping height.";
requiredHeight = screenGeometry.height();
requiredWidth = (int)((float)requiredHeight / ratio); // maintain the ratio of height/width
}
// qDebug() << "[INFO] Final validated size to be set: " << requiredWidth << "x" << requiredHeight;
resize(requiredWidth, requiredHeight);
// Use a valid window width that the operating system will definitely accept to centralize the window
const int x = screenGeometry.x() + (screenGeometry.width() - this->width()) / 2;
const int y = screenGeometry.y() + (screenGeometry.height() - this->height()) / 2;
move(x, y);
// qDebug() << "[INFO] Final position set to:" << this->pos();
}
/*else {
qDebug() << "[FATAL] Could not find one or both content widgets. Aborting geometry adjustment.";
}*/
}
});
//#endif
}
MainWindow::~MainWindow()
{
delete ui;
}