Skip to content

Commit 6ab1039

Browse files
committed
ui improve. window position and size remembering behavior fixed
1 parent bfe3dc4 commit 6ab1039

File tree

12 files changed

+489
-405
lines changed

12 files changed

+489
-405
lines changed

DayTithiWidget.h

Lines changed: 131 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,75 +7,167 @@
77
#include <QIcon>
88
#include <QPixmap>
99
#include <QPainter>
10+
#include <QResizeEvent>
11+
#include <QFont>
12+
#include <QFontDatabase>
13+
#include <QtGlobal>
1014

1115
class DayTithiWidget : public QWidget {
1216
Q_OBJECT
1317

1418
public:
15-
explicit DayTithiWidget(const QString &day, const QString &tithi, QWidget *parent = nullptr)
16-
: QWidget(parent) {
17-
QVBoxLayout *mainLayout = new QVBoxLayout(this);
18-
mainLayout->setSpacing(0);
19-
mainLayout->setContentsMargins(0, 0, 0, 0); // Remove margins
19+
explicit DayTithiWidget(const QString &day, const QString &tithi, const QString &englishDay, QWidget *parent = nullptr)
20+
: QWidget(parent), dayLabelText(day), tithiLabelText(tithi), englishDayLabelText(englishDay) {
2021

22+
// Day number (centered large text)
2123
dayLabel = new QLabel(day, this);
22-
dayLabel->setObjectName("dayLabel"); // Set object name for styling
23-
dayLabel->setStyleSheet("font-size: 19px; color: black; background-color: transparent;");
24+
dayLabel->setObjectName("dayLabel");
25+
dayLabel->setAlignment(Qt::AlignCenter);
26+
dayLabel->setStyleSheet("color: black; background-color: transparent;");
2427

28+
// English day number (top-right, small, gray)
29+
englishDayLabel = new QLabel(englishDay, this);
30+
englishDayLabel->setObjectName("englishDayLabel");
31+
englishDayLabel->setAlignment(Qt::AlignRight | Qt::AlignTop);
32+
englishDayLabel->setStyleSheet("color:rgb(74, 32, 240); background-color: transparent;");
33+
englishDayLabel->adjustSize();
34+
35+
// Tithi label (bottom left)
2536
tithiLabel = new QLabel(tithi, this);
26-
tithiLabel->setObjectName("tithiLabel"); // Set object name for styling
27-
tithiLabel->setStyleSheet("font-size: 10px; color: blue; background-color: transparent;");
37+
tithiLabel->setObjectName("tithiLabel");
38+
tithiLabel->setStyleSheet("color: #2563eb; background-color: transparent;");
39+
tithiLabel->setVisible(true);
40+
tithiLabel->adjustSize();
2841

42+
// Icon label (bottom right)
2943
iconLabel = new QLabel(this);
30-
iconLabel->setObjectName("iconLabel");
31-
iconLabel->setAlignment(Qt::AlignJustify);
44+
iconLabel->setAttribute(Qt::WA_TransparentForMouseEvents);
3245
iconLabel->setStyleSheet("background-color: transparent;");
33-
iconLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
34-
35-
mainLayout->addWidget(dayLabel);
36-
mainLayout->addWidget(iconLabel);
37-
mainLayout->addWidget(tithiLabel);
38-
adjustIconSize();
46+
iconLabel->adjustSize();
3947
}
4048

4149
void setTodayStyle() {
42-
dayLabel->setStyleSheet("background-color: transparent; font-size: 20px; font-weight: bold; text-decoration: underline; color: green;");
50+
QFont font = dayLabel->font();
51+
font.setBold(true);
52+
dayLabel->setFont(font);
53+
dayLabel->adjustSize();
54+
int contentWidth = dayLabel->width();
55+
int contentHeight = dayLabel->height();
56+
int maxDimension = qMax(contentWidth, contentHeight);
57+
int dynamicPadding = maxDimension / 3;
58+
int finalPadding = qBound(10, dynamicPadding, 30);
59+
int circleDiameter = maxDimension + (finalPadding * 2);
60+
if (circleDiameter < 40) circleDiameter = 40;
61+
dayLabel->setFixedSize(circleDiameter, circleDiameter);
62+
QString styleSheet = QString(
63+
"QLabel {"
64+
" background-color:rgb(91, 240, 156);"
65+
" color: white;"
66+
" border-radius: %1px;" // Half of the diameter for a circle
67+
" qproperty-alignment: AlignCenter;" // Center the text within the circular area
68+
"}"
69+
).arg(circleDiameter / 2);
70+
71+
dayLabel->setStyleSheet(styleSheet);
4372
}
4473

74+
4575
void setSaturdayStyle() {
46-
dayLabel->setStyleSheet("background-color: transparent; font-size: 19px; color: red;");
76+
dayLabel->setStyleSheet("color: red;");
4777
}
4878

49-
void setIcon(const QIcon &icon, qreal opacity = 1.0) {
50-
if (!icon.isNull()) {
51-
QPixmap pixmap = icon.pixmap(iconSize);
79+
private:
80+
QLabel *dayLabel;
81+
QLabel *tithiLabel;
82+
QLabel *iconLabel;
83+
QLabel *englishDayLabel;
84+
QString dayLabelText;
85+
QString tithiLabelText;
86+
QString englishDayLabelText;
87+
QSize iconSize = QSize(24, 24);
88+
QIcon storedIcon;
89+
qreal storedIconOpacity = 1.0;
90+
void adjustIconSize() {
91+
int minSize = qMin(width(), height()) / 2;
92+
iconSize = QSize(minSize + 18, minSize + 18);
93+
iconLabel->setFixedSize(iconSize);
94+
iconLabel->setScaledContents(true);
95+
iconLabel->move(width() - iconSize.width() - 4, height() - iconSize.height() - 4);
96+
// Always re-render from original icon for sharpness
97+
if (!storedIcon.isNull()) {
98+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
99+
QPixmap pixmap = storedIcon.pixmap(iconSize);
100+
#else
101+
QPixmap pixmap = storedIcon.pixmap(iconSize, QIcon::Normal, QIcon::On);
102+
#endif
52103
QPixmap transparentPixmap(pixmap.size());
53104
transparentPixmap.fill(Qt::transparent);
54-
55105
QPainter painter(&transparentPixmap);
56-
painter.setOpacity(opacity);
106+
painter.setOpacity(storedIconOpacity);
57107
painter.drawPixmap(0, 0, pixmap);
58108
painter.end();
59-
60-
iconLabel->setPixmap(transparentPixmap); // Set the transparent pixmap
61-
} else {
62-
iconLabel->clear(); // Clear the icon if no icon is provided
109+
iconLabel->setPixmap(transparentPixmap);
63110
}
64111
}
112+
public:
113+
void setIcon(const QIcon &icon, qreal opacity = 1.0) {
114+
storedIcon = icon;
115+
storedIconOpacity = opacity;
116+
adjustIconSize();
117+
}
65118

66-
private:
67-
QLabel *dayLabel;
68-
QLabel *tithiLabel;
69-
QLabel *iconLabel;
70-
QSize iconSize = QSize(15, 15);
71-
void adjustIconSize() {
72-
// Calculate available space and adjust icon size if necessary
73-
int availableHeight = iconLabel->height()-3; // Adjust for borders
74-
int availableWidth = iconLabel->width()-3; // Adjust for borders
119+
protected:
120+
void resizeEvent(QResizeEvent *event) override {
121+
QWidget::resizeEvent(event);
122+
123+
int w = width();
124+
int h = height();
125+
126+
int minDim = qMin(w, h);
127+
128+
// Font sizes
129+
int dayFontSize = std::max(12, static_cast<int>(minDim * 0.14));
130+
int tithiFontSize = std::max(8, static_cast<int>(minDim * 0.05));
131+
int englishDayFontSize = std::max(8, static_cast<int>(minDim * 0.05));
132+
133+
// --- Update Fonts ---
134+
int fontId = QFontDatabase::addApplicationFont(":/resources/Martel-Bold.ttf");
135+
QString fontFamily = QFontDatabase::applicationFontFamilies(fontId).at(0);
136+
QFont dayFont = dayLabel->font();
137+
dayFont.setPointSize(dayFontSize);
138+
dayFont.setFamily(fontFamily);
139+
dayFont.setBold(true);
140+
dayLabel->setFont(dayFont);
141+
dayLabel->repaint();
75142

76-
int minSize = qMin(availableWidth, availableHeight);
77-
iconSize = QSize(minSize, minSize);
143+
QFont tithiFont = tithiLabel->font();
144+
tithiFont.setPointSize(tithiFontSize);
145+
tithiFont.setItalic(true);
146+
tithiLabel->setFont(tithiFont);
147+
148+
149+
dayLabel->adjustSize();
150+
int dayX = 20;
151+
int dayY = (h / 3) - (dayLabel->height() / 2); // Move up
152+
dayLabel->move(dayX, dayY);
153+
154+
// --- Reposition English Day Label (top-right) ---
155+
englishDayLabel->adjustSize();
156+
englishDayLabel->move(width() - englishDayLabel->width() - 6, 6);
157+
158+
159+
// --- Reposition Tithi Label (bottom-left) ---
160+
tithiLabel->adjustSize();
161+
tithiLabel->move(6, h - tithiLabel->height() - 6);
162+
163+
164+
QFont englishDayFont = englishDayLabel->font();
165+
englishDayFont.setPointSize(englishDayFontSize);
166+
englishDayLabel->setFont(englishDayFont);
167+
englishDayLabel->adjustSize();
168+
adjustIconSize(); // Should reposition icon manually too
78169
}
170+
79171
};
80172

81173
#endif // DAYTITHIWIDGET_H

calendarwindow.cpp

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@
1313
#include <QIcon>
1414
#include <QFile>
1515
#include <QtWidgets>
16+
#include <QSettings>
17+
#include <QCloseEvent>
1618

1719
CalendarWindow::CalendarWindow(QWidget *parent) :
1820
QMainWindow(parent),
1921
ui(new Ui::CalendarWindow),
2022
blockSignals(false)
2123
{
22-
ui->setupUi(this);
24+
ui->setupUi(this);
25+
setWindowPosition();
26+
27+
2328

2429
// Apply the custom font
2530
int fontId = QFontDatabase::addApplicationFont(":/resources/Laila-Medium.ttf");
@@ -119,6 +124,61 @@ CalendarWindow::CalendarWindow(QWidget *parent) :
119124
adjustCellSizes();
120125
adjustCalendarTableSize();
121126
}
127+
128+
#include <QTimer>
129+
130+
void CalendarWindow::setWindowPosition() {
131+
QSettings settings("Nepdate", "Nepdatecalendar");
132+
133+
// Load saved size
134+
QSize savedSize = settings.value("MainWindow/size", QSize(-1, -1)).toSize();
135+
if (savedSize != QSize(-1, -1)) {
136+
resize(savedSize);
137+
} else {
138+
QRect screenGeometry = QApplication::primaryScreen()->availableGeometry();
139+
int width = screenGeometry.width() * 0.7;
140+
int height = screenGeometry.height() * 0.6;
141+
resize(width, height);
142+
}
143+
144+
// Defer restoring position until after window has been shown
145+
QTimer::singleShot(0, this, [this]() { // <-- No captured QSettings
146+
QSettings settings("Nepdate", "Nepdatecalendar"); // Create new instance inside lambda
147+
148+
QPoint savedPos = settings.value("MainWindow/pos", QPoint(-1, -1)).toPoint();
149+
150+
if (savedPos != QPoint(-1, -1)) {
151+
// Make sure position is on a valid screen
152+
bool foundValidScreen = false;
153+
for (QScreen *screen : QGuiApplication::screens()) {
154+
if (screen->geometry().contains(savedPos)) {
155+
move(savedPos);
156+
foundValidScreen = true;
157+
break;
158+
}
159+
}
160+
161+
// If not found on any screen, center it
162+
if (!foundValidScreen) {
163+
QRect screenGeometry = QApplication::primaryScreen()->availableGeometry();
164+
int x = (screenGeometry.width() - width()) / 2;
165+
int y = (screenGeometry.height() - height()) / 2;
166+
move(x, y);
167+
}
168+
}
169+
});
170+
}
171+
172+
void CalendarWindow::closeEvent(QCloseEvent *event) {
173+
// Save window geometry
174+
QSettings settings("Nepdate", "Nepdatecalendar");
175+
settings.setValue("MainWindow/pos", pos());
176+
settings.setValue("MainWindow/size", size());
177+
settings.setValue("MainWindow/maximized", isMaximized());
178+
179+
QMainWindow::closeEvent(event);
180+
}
181+
122182
bool CalendarWindow::eventFilter(QObject *object, QEvent *event) {
123183
if (object == this && event->type() == QEvent::Show) {
124184
// Perform action when the window is shown
@@ -592,7 +652,8 @@ void CalendarWindow::updateCalendar(int year, int month) {
592652
QString tithiName = QString::fromStdString(tithi[(int)panchang.tithi_index]);
593653

594654
// Create custom widget for day and tithi
595-
DayTithiWidget *customWidget = new DayTithiWidget(convertToNepaliNumerals(day), tithiName);
655+
QString englishDayStr = QString::number(gDay); // gDay is the Gregorian day for this cell
656+
DayTithiWidget *customWidget = new DayTithiWidget(convertToNepaliNumerals(day), tithiName, englishDayStr);
596657

597658
// Set tooltip
598659
QString paksha = QString::fromStdString(panchang.paksha);
@@ -612,17 +673,16 @@ void CalendarWindow::updateCalendar(int year, int month) {
612673
customWidget->setTodayStyle(); // defined in DayTithiWidget.h
613674
} else if (isToday) {
614675
// If it's just today, apply the "today" style
615-
item->setBackground(QColor(153, 255, 204)); // light green
616676
customWidget->setTodayStyle(); // defined in DayTithiWidget.h
617677
} else if (isSaturday) {
618678
// If it's just Saturday, apply the "Saturday" style
619679
customWidget->setSaturdayStyle();
620680
}
621681

622682
if (panchang.tithi_index == 14) {
623-
customWidget->setIcon(purnimaIcon, 0.8); // Example opacity set to 0.8
683+
customWidget->setIcon(purnimaIcon, 0.9);
624684
} else if (panchang.tithi_index == 29) {
625-
customWidget->setIcon(amavasyaIcon, 0.8); // Example opacity set to 0.8
685+
customWidget->setIcon(amavasyaIcon, 0.9);
626686
} else {
627687
customWidget->setIcon(QIcon(), 0.0); // Clear icon and set transparency
628688
}

calendarwindow.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ private slots:
4545
void showMenu();
4646
void showAbout();
4747
void openSourceCode();
48+
void setWindowPosition();
4849

4950
protected:
5051
void resizeEvent(QResizeEvent* event) override;
5152
bool eventFilter(QObject *object, QEvent *event) override;
53+
void closeEvent(QCloseEvent *event) override;
5254

5355
private:
5456
Ui::CalendarWindow *ui;

0 commit comments

Comments
 (0)