Skip to content

Commit 72c5274

Browse files
committed
refactor and add panchanga components
1 parent c0f42c7 commit 72c5274

14 files changed

+602
-301
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ set(NEPDATE_WIDGET_SOURCES
5555
main.cpp
5656
mainwindow.cpp
5757
calendarwindow.cpp
58+
calendarlogic.cpp
59+
calendartable.cpp
60+
convertor.cpp
61+
panchanga.cpp
5862
)
5963

6064
set(NEPDATE_WIDGET_HEADERS

DayTithiWidget.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
#include <QFont>
1212
#include <QFontDatabase>
1313
#include <QtGlobal>
14+
#include <QMouseEvent>
15+
#include <QMessageBox>
16+
#include "panchanga.h"
17+
#include "bikram.h"
18+
#include <QDate>
19+
#include "calendarlogic.h"
1420

1521
class DayTithiWidget : public QWidget {
1622
Q_OBJECT
@@ -147,6 +153,49 @@ class DayTithiWidget : public QWidget {
147153
tithiLabel->move(6, h - tithiLabel->height() - 6);
148154
adjustIconSize();
149155
}
156+
void mousePressEvent(QMouseEvent *event) override {
157+
if (event->button() == Qt::LeftButton) {
158+
int day = property("gDay").toInt(); // Use actual Gregorian day
159+
int month = property("month").toInt();
160+
int year = property("year").toInt();
161+
// Gregorian date
162+
QDate gdate(year, month, day);
163+
// Bikram date
164+
Bikram bs;
165+
bs.fromGregorian(year, month, day);
166+
int bsYear = bs.getYear();
167+
int bsMonth = bs.getMonth();
168+
int bsDay = bs.getDay();
169+
// Panchanga details
170+
std::tm date = {};
171+
date.tm_year = year - 1900;
172+
date.tm_mon = month - 1;
173+
date.tm_mday = day;
174+
TithiResult tithi = calculateTithi(date);
175+
YogaResult yoga = calculateYoga(date);
176+
KaranResult karan = calculateKaran(date);
177+
NakshatraResult nakshatra = calculateNakshatra(date);
178+
RashiResult rashi = calculateRashi(date);
179+
QString sunrise = QString::fromStdString(calculateSunriseOrSunset(date, true));
180+
QString sunset = QString::fromStdString(calculateSunriseOrSunset(date, false));
181+
// Show details dialog
182+
QString details = QString(
183+
"Gregorian: %1\nBikram: %2-%3-%4\nTithi: %5\nPaksha: %6\nYoga: %7\nKarana: %8\nNakshatra: %9\nRashi: %10\nSunrise: %11\nSunset: %12")
184+
.arg(QString::number(gdate.year()) + "-" + QString::number(gdate.month()).rightJustified(2, '0') + "-" + QString::number(gdate.day()).rightJustified(2, '0'))
185+
.arg(convertToNepaliNumerals(bsYear)).arg(convertToNepaliNumerals(bsMonth)).arg(convertToNepaliNumerals(bsDay))
186+
.arg(QString::fromStdString(tithi.tithiName))
187+
.arg(QString::fromStdString(tithi.paksha))
188+
.arg(QString::fromStdString(yoga.yogaName))
189+
.arg(QString::fromStdString(karan.karanName))
190+
.arg(QString::fromStdString(nakshatra.nakshatraName))
191+
.arg(QString::fromStdString(rashi.rashiName))
192+
.arg(sunrise)
193+
.arg(sunset);
194+
QWidget *topLevel = this->window();
195+
QMessageBox::information(topLevel, tr("Day Details"), details);
196+
}
197+
QWidget::mousePressEvent(event);
198+
}
150199

151200
};
152201

bikram.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef BIKRAM_H
22
#define BIKRAM_H
33

4+
#include "panchanga.h"
5+
46
#include <cmath>
57

68
class Bikram {

calendarlogic.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "calendarlogic.h"
2+
3+
const QStringList bikramMonths = {
4+
"वैशाख", "जेठ", "असार", "श्रावण", "भाद्र",
5+
"आश्विन", "कार्तिक", "मंसिर", "पौष", "माघ",
6+
"फाल्गुन", "चैत्र"
7+
};
8+
const QStringList gregorianMonths = {
9+
"जनवरी", "फेब्रुअरी", "मार्च", "अप्रिल", "मे", "जुन",
10+
"जुलाई", "अगस्ट", "सेप्टेम्बर", "अक्टोबर", "नोभेम्बर", "डिसेम्बर"
11+
};
12+
13+
QString convertToNepaliNumerals(int number) {
14+
QString nepaliNumerals = QString::number(number);
15+
nepaliNumerals.replace("0", "");
16+
nepaliNumerals.replace("1", "");
17+
nepaliNumerals.replace("2", "");
18+
nepaliNumerals.replace("3", "");
19+
nepaliNumerals.replace("4", "");
20+
nepaliNumerals.replace("5", "");
21+
nepaliNumerals.replace("6", "");
22+
nepaliNumerals.replace("7", "");
23+
nepaliNumerals.replace("8", "");
24+
nepaliNumerals.replace("9", "");
25+
return nepaliNumerals;
26+
}
27+
28+
QString getBikramMonthName(int month) {
29+
if (month < 1 || month > 12) return "";
30+
return bikramMonths.at(month - 1);
31+
}
32+
33+
QString getEnglishMonthName(int month) {
34+
if (month < 1 || month > 12) return "";
35+
return gregorianMonths.at(month - 1);
36+
}

calendarlogic.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef CALENDARLOGIC_H
2+
#define CALENDARLOGIC_H
3+
#include <QString>
4+
#include <QStringList>
5+
6+
QString convertToNepaliNumerals(int number);
7+
QString getBikramMonthName(int month);
8+
QString getEnglishMonthName(int month);
9+
extern const QStringList bikramMonths;
10+
extern const QStringList gregorianMonths;
11+
12+
#endif // CALENDARLOGIC_H

calendartable.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
}

calendartable.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef CALENDARTABLE_H
2+
#define CALENDARTABLE_H
3+
4+
#include <QObject>
5+
#include <QTableWidget>
6+
#include <QString>
7+
#include <QDate>
8+
#include "bikram.h"
9+
#include <QComboBox>
10+
11+
class CalendarTableHelper {
12+
public:
13+
static void updateCalendar(QTableWidget* table, Bikram& converter, int year, int month, int& gYear, int& gMonth, int& gDay);
14+
static void adjustCellSizes(QTableWidget* table);
15+
static void populateBsDays(QComboBox* dayCombo, Bikram& converter, int year, int month);
16+
};
17+
18+
#endif // CALENDARTABLE_H

0 commit comments

Comments
 (0)