1010#include  " panchanga.h" 
1111
1212void  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 ();
13+     //  Delete all existing cell widgets to free memory
14+     for  (int  row = 0 ; row < table->rowCount (); ++row) {
15+         for  (int  col = 0 ; col < table->columnCount (); ++col) {
16+             QWidget *widget = table->cellWidget (row, col);
17+             if  (widget) {
18+                 table->removeCellWidget (row, col);
19+                 delete  widget;
20+             }
21+             //  Also delete the QTableWidgetItem if exists
22+             QTableWidgetItem *item = table->item (row, col);
23+             if  (item) {
24+                 delete  item;
25+                 table->setItem (row, col, nullptr );
26+             }
27+         }
28+     }
29+ 
30+     //  Clear the table contents (headers remain, but items cleared)
31+     table->clearContents ();
32+ 
33+     //  Reconfigure table
1534    table->setRowCount (6 );
1635    table->setColumnCount (7 );
36+ 
1737    QStringList headers = {" आइत" " सोम" " मङ्गल" " बुध" " बिही" " शुक्र" " शनि" 
1838    table->setHorizontalHeaderLabels (headers);
39+ 
1940    table->horizontalHeader ()->setStyleSheet (
20-         " QHeaderView::section {background-color: #d3d3d3;color: blue;border: 1px solid gray;}" 
41+         " QHeaderView::section {background-color: #d3d3d3;  color: blue;  border: 1px solid gray;}" 
2142    );
43+ 
2244    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;}" 
45+         " background-color: white;" 
46+         " QTableWidget::item {border: 1px solid gray;}" 
47+         " QTableWidget .dayLabel {font-size: 24px; color: black;}" 
48+         " QTableWidget .tithiLabel {font-size: 8px; color: blue;}" 
2449    );
50+ 
51+     //  First day Gregorian conversion
2552    converter.toGregorian (year, month, 1 , gYear , gMonth , gDay );
2653    QDate firstDay (gYear , gMonth , gDay );
2754    int  startDay = firstDay.dayOfWeek ();
55+     int  col = (startDay - 6  + 7 ) % 7 ; //  Saturday is last
56+ 
57+     //  Today's BS date for comparison
2858    QDate today = QDate::currentDate ();
2959    converter.fromGregorian (today.year (), today.month (), today.day ());
3060    int  todayBsYear = converter.getYear ();
3161    int  todayBsMonth = converter.getMonth ();
3262    int  todayBsDay = converter.getDay ();
33-     int  saturdayIndex = 6 ;
34-     QIcon purnimaIcon (" :/resources/purnima.png" 
35-     QIcon amavasyaIcon (" :/resources/amawasya.png" 
63+ 
64+     static  QIcon purnimaIcon (" :/resources/purnima.png" 
65+     static  QIcon amavasyaIcon (" :/resources/amawasya.png" 
66+ 
3667    int  row = 0 ;
37-     int  col = (startDay - saturdayIndex + 6 ) % 7 ;
68+     int  daysInMonth = converter.daysInMonth (year, month);
69+ 
3870    for  (int  day = 1 ; day <= daysInMonth; ++day) {
3971        int  cellGYear, cellGMonth, cellGDay;
4072        converter.toGregorian (year, month, day, cellGYear, cellGMonth, cellGDay);
73+ 
4174        double  julianDate = gregorianToJulian (cellGYear, cellGMonth, cellGDay);
4275        Panchang panchang (julianDate);
76+ 
4377        QString tithiName = QString::fromStdString (tithi[(int )panchang.tithi_index ]);
4478        QString englishDayStr = QString::number (cellGDay);
79+ 
4580        DayTithiWidget *customWidget = new  DayTithiWidget (convertToNepaliNumerals (day), tithiName, englishDayStr);
4681        customWidget->setProperty (" year" 
4782        customWidget->setProperty (" month" 
48-         customWidget->setProperty (" gDay" //  Store actual Gregorian day
49-         std::tm date_to_pass = {}; //  This zero-initializes all members of the struct
83+         customWidget->setProperty (" gDay" 
84+ 
85+         std::tm date_to_pass = {};
5086        date_to_pass.tm_year  = cellGYear - 1900 ;
5187        date_to_pass.tm_mon  = cellGMonth - 1 ;
5288        date_to_pass.tm_mday  = cellGDay;
89+ 
5390        TithiResult tithiResult = calculateTithi (date_to_pass);
5491        QString tooltipText = QString (" %1 (%2)" 
5592            .arg (QString::fromStdString (tithiResult.tithiName ))
5693            .arg (QString::fromStdString (tithiResult.paksha ));
5794        customWidget->setToolTip (tooltipText);
58-         QTableWidgetItem *item = new  QTableWidgetItem ();
59-         table->setItem (row, col, item);
95+ 
96+         table->setItem (row, col, new  QTableWidgetItem ());
97+ 
6098        bool  isToday = (year == todayBsYear && month == todayBsMonth && day == todayBsDay);
61-         bool  isSaturday = (col == saturdayIndex);
62-         if  (isToday) customWidget->setTodayStyle ();
63-         else  if  (isSaturday) customWidget->setSaturdayStyle ();
64-         if  (panchang.tithi_index  == 14 ) customWidget->setIcon (purnimaIcon, 0.9 );
65-         else  if  (panchang.tithi_index  == 29 ) customWidget->setIcon (amavasyaIcon, 0.9 );
66-         else  customWidget->setIcon (QIcon (), 0.0 );
99+         bool  isSaturday = (col == 6 );
100+ 
101+         if  (isToday) {
102+             customWidget->setTodayStyle ();
103+         } else  if  (isSaturday) {
104+             customWidget->setSaturdayStyle ();
105+         }
106+ 
107+         if  (panchang.tithi_index  == 14 ) {
108+             customWidget->setIcon (purnimaIcon, 0.9 );
109+         } else  if  (panchang.tithi_index  == 29 ) {
110+             customWidget->setIcon (amavasyaIcon, 0.9 );
111+         } else  {
112+             customWidget->setIcon (QIcon (), 0.0 );
113+         }
114+ 
67115        table->setCellWidget (row, col, customWidget);
116+ 
68117        col++;
69-         if  (col > 6 ) { col = 0 ; row++; }
118+         if  (col > 6 ) {
119+             col = 0 ;
120+             row++;
121+         }
70122    }
123+ 
71124    table->resizeRowsToContents ();
72125    table->resizeColumnsToContents ();
73126    CalendarTableHelper::adjustCellSizes (table);
@@ -79,18 +132,27 @@ void CalendarTableHelper::adjustCellSizes(QTableWidget* table) {
79132    int  tableHeight = table->viewport ()->height ();
80133    int  numColumns = table->columnCount ();
81134    int  numRows = table->rowCount ();
135+ 
82136    if  (numColumns > 0  && numRows > 0 ) {
83137        int  columnWidth = tableWidth / numColumns;
84138        int  rowHeight = tableHeight / numRows;
85-         for  (int  i = 0 ; i < numColumns; ++i) table->setColumnWidth (i, columnWidth);
86-         for  (int  i = 0 ; i < numRows; ++i) table->setRowHeight (i, rowHeight);
139+ 
140+         for  (int  i = 0 ; i < numColumns; ++i)
141+             table->setColumnWidth (i, columnWidth);
142+ 
143+         for  (int  i = 0 ; i < numRows; ++i)
144+             table->setRowHeight (i, rowHeight);
87145    }
88146}
89147
90148void  CalendarTableHelper::populateBsDays (QComboBox* dayCombo, Bikram& converter, int  year, int  month) {
91149    int  daysInMonth = converter.daysInMonth (year, month);
92150    int  currentDay = converter.getDay ();
151+ 
93152    dayCombo->clear ();
94-     for  (int  day = 1 ; day <= daysInMonth; ++day) dayCombo->addItem (QString::number (day));
153+     for  (int  day = 1 ; day <= daysInMonth; ++day) {
154+         dayCombo->addItem (QString::number (day));
155+     }
156+ 
95157    dayCombo->setCurrentText (QString::number (currentDay));
96158}
0 commit comments