Skip to content

Commit

Permalink
update style and date line in calendar window
Browse files Browse the repository at this point in the history
  • Loading branch information
khumnath committed May 19, 2024
1 parent f276e74 commit d6e7629
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 51 deletions.
10 changes: 5 additions & 5 deletions calendarwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
#include <QScreen>
#include <QUrl>
#include <QDesktopServices>

#include<QMainWindow>
CalendarWindow::CalendarWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::CalendarWindow)
{
: QMainWindow(parent), ui(new Ui::CalendarWindow) {
ui->setupUi(this);

ui->calendarWidget->setStyleSheet("QCalendarWidget QDateEdit { color: red; } "
"QCalendarWidget QWidget#qt_calendar_navigationbar { background-color: lightgreen; }");

// Center the window on the screen
centerOnScreen();
Expand Down Expand Up @@ -47,3 +46,4 @@ void CalendarWindow::on_pushButton_clicked()




147 changes: 144 additions & 3 deletions calendarwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,53 @@
<height>570</height>
</rect>
</property>
<property name="font">
<font>
<family>Noto Sans Devanagari</family>
</font>
</property>
<property name="windowTitle">
<string>CalendarWindow</string>
</property>
<property name="styleSheet">
<string notr="true">background-color: white;
QCalendarWidget {
background-color: #838488; /* Set your desired background color */
border-radius: 10px; /* Adjust the radius for rounded corners */
}

QCalendarWidget QToolButton {
height: 60px; /* Set button height */
width: 150px; /* Set button width */
color: blue; /* Text color for navigation buttons */
font-size: 24px; /* Font size */
icon-size: 56px, 56px; /* Icon size */
background-color: red;
}

QCalendarWidget QMenu {
width: 150px; /* Set menu width */
left: 20px; /* Adjust position */
}

QCalendarWidget QMenu::item:selected {
background-color: rgb(136, 136, 136); /* Selected item background color */
color: rgb(255, 255, 255); /* Selected item text color */
}
</string>
</property>
<property name="locale">
<locale language="Nepali" country="Nepal"/>
</property>
<widget class="QWidget" name="centralwidget">
<property name="font">
<font>
<family>Noto Sans Devanagari</family>
</font>
</property>
<property name="styleSheet">
<string notr="true">
background-color: #ffffff;
background-color: light gray;
</string>
</property>
<widget class="QCalendarWidget" name="calendarWidget">
Expand All @@ -31,6 +68,26 @@
<height>341</height>
</rect>
</property>
<property name="font">
<font>
<family>Noto Sans Devanagari</family>
</font>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<property name="gridVisible">
<bool>true</bool>
</property>
<property name="verticalHeaderFormat">
<enum>QCalendarWidget::NoVerticalHeader</enum>
</property>
<property name="navigationBarVisible">
<bool>true</bool>
</property>
<property name="dateEditEnabled">
<bool>true</bool>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
Expand All @@ -41,6 +98,11 @@
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<family>Noto Sans Devanagari</family>
</font>
</property>
<property name="lineWidth">
<number>2</number>
</property>
Expand All @@ -57,6 +119,11 @@
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<family>Noto Sans Devanagari</family>
</font>
</property>
<property name="text">
<string>यो सञ्झ्याल नेपाली पात्रोका लागि प्रयोग गर्ने उद्देश्यले बनाइएको हो । (तर कार्यान्वयन भएको छैन ।)</string>
</property>
Expand All @@ -65,11 +132,16 @@
<property name="geometry">
<rect>
<x>130</x>
<y>80</y>
<y>50</y>
<width>291</width>
<height>36</height>
</rect>
</property>
<property name="font">
<font>
<family>Noto Sans Devanagari</family>
</font>
</property>
<property name="styleSheet">
<string notr="true">color: blue;</string>
</property>
Expand All @@ -86,6 +158,11 @@
<height>36</height>
</rect>
</property>
<property name="font">
<font>
<family>Noto Sans Devanagari</family>
</font>
</property>
<property name="styleSheet">
<string notr="true">border: none;
background-color: transparent;</string>
Expand All @@ -97,8 +174,72 @@ background-color: transparent;</string>
<iconset theme="dialog-warning"/>
</property>
</widget>
<widget class="QPushButton" name="pushButton_3">
<property name="geometry">
<rect>
<x>70</x>
<y>90</y>
<width>71</width>
<height>36</height>
</rect>
</property>
<property name="font">
<font>
<family>Noto Sans Devanagari</family>
</font>
</property>
<property name="styleSheet">
<string notr="true">background-color: gray;
color: white;</string>
</property>
<property name="text">
<string>Today</string>
</property>
</widget>
<widget class="QPushButton" name="mitiButton">
<property name="geometry">
<rect>
<x>160</x>
<y>90</y>
<width>241</width>
<height>36</height>
</rect>
</property>
<property name="font">
<font>
<family>Noto Sans Devanagari</family>
<pointsize>16</pointsize>
</font>
</property>
<property name="styleSheet">
<string notr="true">color: black;
border: 1px solid;
border-color: black;
border-radius: 5px;</string>
</property>
<property name="text">
<string/>
</property>
</widget>
</widget>
</widget>
<resources/>
<connections/>
<connections>
<connection>
<sender>pushButton_3</sender>
<signal>clicked()</signal>
<receiver>calendarWidget</receiver>
<slot>showToday()</slot>
<hints>
<hint type="sourcelabel">
<x>292</x>
<y>497</y>
</hint>
<hint type="destinationlabel">
<x>294</x>
<y>300</y>
</hint>
</hints>
</connection>
</connections>
</ui>
86 changes: 52 additions & 34 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@
#include "mainwindow.h"
#include "qmenu.h"
#include "ui_mainwindow.h"
#include "QHash"
#include "QDebug"
#include <QMouseEvent>
#include "QMessageBox"
#include <QTimer>
#include <QSettings>
#include <QToolTip>
#include "calendarwindow.h"
#include <QPushButton>
#include <QVBoxLayout>
#include <QClipboard>
#include "bikram.h"
#include <QLocale>
Expand All @@ -39,7 +34,7 @@ MainWindow::MainWindow(QWidget *parent) :
updateTimer->start(1000); // Update every second

// Set the window flags to make it borderless
setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::Tool);

// Get the geometry of the primary screen
QScreen *primaryScreen = QGuiApplication::primaryScreen();
Expand All @@ -58,37 +53,14 @@ MainWindow::MainWindow(QWidget *parent) :
// Set the position of the window
move(x, y);
;


setAttribute(Qt::WA_TranslucentBackground);


setAttribute(Qt::WA_TranslucentBackground);

// Set today's date as default when setting up the UI
setupDefaultDate();

// Make the window rounded
setMask(createRoundRectMask(size(), 10)); // Adjust the radius as needed


}

QBitmap MainWindow::createRoundRectMask(const QSize &size, int radius)
{
// Create a bitmap with the same size as the window
QBitmap mask(size);
mask.fill(Qt::color0); // Fill the mask with transparent pixels

// Create a painter to draw the rounded rectangle on the mask
QPainter painter(&mask);
painter.setRenderHint(QPainter::Antialiasing, true); // Enable antialiasing for smooth edges

// Draw the rounded rectangle on the mask
painter.setBrush(Qt::color1); // Fill color (opaque)
painter.drawRoundedRect(mask.rect(), radius, radius);

return mask;
}


MainWindow::~MainWindow()
Expand Down Expand Up @@ -133,13 +105,13 @@ int MainWindow::cnvToNepali(int mm, int dd, int yy) {

// Adjust the day of the week
QString nepaliMonthName = get_nepali_month(nepaliMonth);
QString nepaliDayOfWeekName = QString::fromStdString(weekdayName); // Convert std::string to QString
QString nepaliDayOfWeekName = QString::fromStdString(weekdayName);

// Construct the Nepali date format string
QString nepaliFormat = QString::number(nepaliYear) + " " +
nepaliMonthName + " " +
QString::number(nepaliDay) + " गते " +
nepaliDayOfWeekName; // Include the day of the week
nepaliDayOfWeekName;

QLocale nepaliLocale(QLocale::Nepali);

Expand All @@ -153,9 +125,49 @@ int MainWindow::cnvToNepali(int mm, int dd, int yy) {
QFont tooltipFont("Noto Sans Devanagari", 12); // Replace "Noto Sans Devnagari" with the name of your desired font
QToolTip::setFont(tooltipFont); // Set the tooltip font globally
ui->dateButton->setToolTip(nepaliFormat);
adjustTextColorBasedOnBackground();
if (calendarWindow) {
QPushButton *mitiButton = calendarWindow->findChild<QPushButton*>("mitiButton");
if (mitiButton) {
mitiButton->setText(nepaliFormat);
}
}

return 0;
}
QColor MainWindow::getAverageColor(const QImage &image) {
qint64 red = 0, green = 0, blue = 0;
int pixelCount = image.width() * image.height();

for (int y = 0; y < image.height(); ++y) {
for (int x = 0; x < image.width(); ++x) {
QColor color(image.pixel(x, y));
red += color.red();
green += color.green();
blue += color.blue();
}
}

return QColor(red / pixelCount, green / pixelCount, blue / pixelCount);
}

void MainWindow::adjustTextColorBasedOnBackground() {
// Get the screen behind the window
QScreen *screen = QGuiApplication::primaryScreen();
QPixmap pixmap = screen->grabWindow(0, x(), y(), width(), height());
QImage image = pixmap.toImage();

// Calculate the average color
QColor averageColor = getAverageColor(image);

// Determine whether to use black or white text based on the brightness of the average color
int brightness = (averageColor.red() + averageColor.green() + averageColor.blue()) / 3;
QColor textColor = (brightness > 127) ? Qt::black : Qt::white;

// Set the text color of the dateButton
QString styleSheet = QString("QPushButton { color: %1; }").arg(textColor.name());
ui->dateButton->setStyleSheet(styleSheet);
}



Expand Down Expand Up @@ -224,10 +236,16 @@ QString MainWindow::get_nepali_month(int m) {
}



void MainWindow::contextMenuEvent(QContextMenuEvent *event)
{
QMenu menu(this);
QMenu menu(tr("Context menu"), this);
QString styleSheet = "QMenu { background-color: #C9F8FA; color: black; border: 1px solid black; border-radius: 5px; }"
"QMenu::item { background-color: transparent; }"
"QMenu::item:selected { background-color: #1AEFF7; color: #184805;}";
menu.setStyleSheet(styleSheet);
menu.setWindowFlags(menu.windowFlags() | Qt::FramelessWindowHint);


QAction *copyAction = menu.addAction("Copy Date");
connect(copyAction, &QAction::triggered, this, &MainWindow::copyButtonText);
QAction *exitAction = menu.addAction("Exit");
Expand Down
Loading

0 comments on commit d6e7629

Please sign in to comment.