|  | 
| 7 | 7 | #include <QIcon> | 
| 8 | 8 | #include <QPixmap> | 
| 9 | 9 | #include <QPainter> | 
|  | 10 | +#include <QResizeEvent> | 
|  | 11 | +#include <QFont> | 
|  | 12 | +#include <QFontDatabase> | 
|  | 13 | +#include <QtGlobal> | 
| 10 | 14 | 
 | 
| 11 | 15 | class DayTithiWidget : public QWidget { | 
| 12 | 16 |     Q_OBJECT | 
| 13 | 17 | 
 | 
| 14 | 18 | 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) { | 
| 20 | 21 | 
 | 
|  | 22 | +        // Day number (centered large text) | 
| 21 | 23 |         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;"); | 
| 24 | 27 | 
 | 
|  | 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) | 
| 25 | 36 |         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(); | 
| 28 | 41 | 
 | 
|  | 42 | +        // Icon label (bottom right) | 
| 29 | 43 |         iconLabel = new QLabel(this); | 
| 30 |  | -        iconLabel->setObjectName("iconLabel"); | 
| 31 |  | -        iconLabel->setAlignment(Qt::AlignJustify); | 
|  | 44 | +        iconLabel->setAttribute(Qt::WA_TransparentForMouseEvents); | 
| 32 | 45 |         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(); | 
| 39 | 47 |     } | 
| 40 | 48 | 
 | 
| 41 | 49 |     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); | 
| 43 | 72 |     } | 
| 44 | 73 | 
 | 
|  | 74 | + | 
| 45 | 75 |     void setSaturdayStyle() { | 
| 46 |  | -        dayLabel->setStyleSheet("background-color: transparent; font-size: 19px; color: red;"); | 
|  | 76 | +        dayLabel->setStyleSheet("color: red;"); | 
| 47 | 77 |     } | 
| 48 | 78 | 
 | 
| 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 | 
| 52 | 103 |             QPixmap transparentPixmap(pixmap.size()); | 
| 53 | 104 |             transparentPixmap.fill(Qt::transparent); | 
| 54 |  | - | 
| 55 | 105 |             QPainter painter(&transparentPixmap); | 
| 56 |  | -            painter.setOpacity(opacity); | 
|  | 106 | +            painter.setOpacity(storedIconOpacity); | 
| 57 | 107 |             painter.drawPixmap(0, 0, pixmap); | 
| 58 | 108 |             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); | 
| 63 | 110 |         } | 
| 64 | 111 |     } | 
|  | 112 | +public: | 
|  | 113 | +    void setIcon(const QIcon &icon, qreal opacity = 1.0) { | 
|  | 114 | +        storedIcon = icon; | 
|  | 115 | +        storedIconOpacity = opacity; | 
|  | 116 | +        adjustIconSize(); | 
|  | 117 | +    } | 
| 65 | 118 | 
 | 
| 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(); | 
| 75 | 142 | 
 | 
| 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 | 
| 78 | 169 |     } | 
|  | 170 | + | 
| 79 | 171 | }; | 
| 80 | 172 | 
 | 
| 81 | 173 | #endif // DAYTITHIWIDGET_H | 
0 commit comments