|  | 
|  | 1 | +#include "calendartable.h" | 
|  | 2 | +#include "DayTithiWidget.h" | 
|  | 3 | +#include "calendarlogic.h" | 
|  | 4 | +#include "panchanga.h" | 
|  | 5 | +#include <QComboBox> | 
|  | 6 | +#include <QHeaderView> | 
|  | 7 | +#include <QIcon> | 
|  | 8 | +#include <QDate> | 
|  | 9 | +#include <QTableWidgetItem> | 
|  | 10 | +#include <QTableWidget> | 
|  | 11 | + | 
|  | 12 | +void CalendarTableHelper::updateCalendar(QTableWidget* table, Bikram& converter, int year, int month, int& gYear, int& gMonth, int& gDay) { | 
|  | 13 | +    int daysInMonth = converter.daysInMonth(year, month); | 
|  | 14 | +    table->clear(); | 
|  | 15 | +    table->setRowCount(6); | 
|  | 16 | +    table->setColumnCount(7); | 
|  | 17 | +    QStringList headers = {"आइत", "सोम", "मङ्गल", "बुध", "बिही", "शुक्र", "शनि"}; | 
|  | 18 | +    table->setHorizontalHeaderLabels(headers); | 
|  | 19 | +    table->horizontalHeader()->setStyleSheet( | 
|  | 20 | +        "QHeaderView::section {background-color: #d3d3d3;color: blue;border: 1px solid gray;}" | 
|  | 21 | +    ); | 
|  | 22 | +    table->setStyleSheet( | 
|  | 23 | +        "background-color: white;QTableWidget::item {border: 1px solid gray;}QTableWidget .dayLabel {font-size: 24px;color: black;}QTableWidget .tithiLabel {font-size: 8px;color: blue;}" | 
|  | 24 | +    ); | 
|  | 25 | +    converter.toGregorian(year, month, 1, gYear, gMonth, gDay); | 
|  | 26 | +    QDate firstDay(gYear, gMonth, gDay); | 
|  | 27 | +    int startDay = firstDay.dayOfWeek(); | 
|  | 28 | +    QDate today = QDate::currentDate(); | 
|  | 29 | +    converter.fromGregorian(today.year(), today.month(), today.day()); | 
|  | 30 | +    int todayBsYear = converter.getYear(); | 
|  | 31 | +    int todayBsMonth = converter.getMonth(); | 
|  | 32 | +    int todayBsDay = converter.getDay(); | 
|  | 33 | +    int saturdayIndex = 6; | 
|  | 34 | +    QIcon purnimaIcon(":/resources/purnima.png"); | 
|  | 35 | +    QIcon amavasyaIcon(":/resources/amawasya.png"); | 
|  | 36 | +    int row = 0; | 
|  | 37 | +    int col = (startDay - saturdayIndex + 6) % 7; | 
|  | 38 | +    for (int day = 1; day <= daysInMonth; ++day) { | 
|  | 39 | +        int cellGYear, cellGMonth, cellGDay; | 
|  | 40 | +        converter.toGregorian(year, month, day, cellGYear, cellGMonth, cellGDay); | 
|  | 41 | +        double julianDate = gregorianToJulian(cellGYear, cellGMonth, cellGDay); | 
|  | 42 | +        Panchang panchang(julianDate); | 
|  | 43 | +        QString tithiName = QString::fromStdString(tithi[(int)panchang.tithi_index]); | 
|  | 44 | +        QString englishDayStr = QString::number(cellGDay); | 
|  | 45 | +        DayTithiWidget *customWidget = new DayTithiWidget(convertToNepaliNumerals(day), tithiName, englishDayStr); | 
|  | 46 | +        customWidget->setProperty("year", cellGYear); | 
|  | 47 | +        customWidget->setProperty("month", cellGMonth); | 
|  | 48 | +        customWidget->setProperty("gDay", cellGDay); // Store actual Gregorian day | 
|  | 49 | +        TithiResult tithiResult = calculateTithi({cellGYear - 1900, cellGMonth - 1, cellGDay}); | 
|  | 50 | +        QString tooltipText = QString("%1 (%2)") | 
|  | 51 | +            .arg(QString::fromStdString(tithiResult.tithiName)) | 
|  | 52 | +            .arg(QString::fromStdString(tithiResult.paksha)); | 
|  | 53 | +        customWidget->setToolTip(tooltipText); | 
|  | 54 | +        QTableWidgetItem *item = new QTableWidgetItem(); | 
|  | 55 | +        table->setItem(row, col, item); | 
|  | 56 | +        bool isToday = (year == todayBsYear && month == todayBsMonth && day == todayBsDay); | 
|  | 57 | +        bool isSaturday = (col == saturdayIndex); | 
|  | 58 | +        if (isToday) customWidget->setTodayStyle(); | 
|  | 59 | +        else if (isSaturday) customWidget->setSaturdayStyle(); | 
|  | 60 | +        if (panchang.tithi_index == 14) customWidget->setIcon(purnimaIcon, 0.9); | 
|  | 61 | +        else if (panchang.tithi_index == 29) customWidget->setIcon(amavasyaIcon, 0.9); | 
|  | 62 | +        else customWidget->setIcon(QIcon(), 0.0); | 
|  | 63 | +        table->setCellWidget(row, col, customWidget); | 
|  | 64 | +        col++; | 
|  | 65 | +        if (col > 6) { col = 0; row++; } | 
|  | 66 | +    } | 
|  | 67 | +    table->resizeRowsToContents(); | 
|  | 68 | +    table->resizeColumnsToContents(); | 
|  | 69 | +    CalendarTableHelper::adjustCellSizes(table); | 
|  | 70 | +    table->verticalHeader()->setVisible(false); | 
|  | 71 | +} | 
|  | 72 | + | 
|  | 73 | +void CalendarTableHelper::adjustCellSizes(QTableWidget* table) { | 
|  | 74 | +    int tableWidth = table->viewport()->width(); | 
|  | 75 | +    int tableHeight = table->viewport()->height(); | 
|  | 76 | +    int numColumns = table->columnCount(); | 
|  | 77 | +    int numRows = table->rowCount(); | 
|  | 78 | +    if (numColumns > 0 && numRows > 0) { | 
|  | 79 | +        int columnWidth = tableWidth / numColumns; | 
|  | 80 | +        int rowHeight = tableHeight / numRows; | 
|  | 81 | +        for (int i = 0; i < numColumns; ++i) table->setColumnWidth(i, columnWidth); | 
|  | 82 | +        for (int i = 0; i < numRows; ++i) table->setRowHeight(i, rowHeight); | 
|  | 83 | +    } | 
|  | 84 | +} | 
|  | 85 | + | 
|  | 86 | +void CalendarTableHelper::populateBsDays(QComboBox* dayCombo, Bikram& converter, int year, int month) { | 
|  | 87 | +    int daysInMonth = converter.daysInMonth(year, month); | 
|  | 88 | +    int currentDay = converter.getDay(); | 
|  | 89 | +    dayCombo->clear(); | 
|  | 90 | +    for (int day = 1; day <= daysInMonth; ++day) dayCombo->addItem(QString::number(day)); | 
|  | 91 | +    dayCombo->setCurrentText(QString::number(currentDay)); | 
|  | 92 | +} | 
0 commit comments