Skip to content

Commit

Permalink
improved for consistent color change and made widget moving easier
Browse files Browse the repository at this point in the history
  • Loading branch information
khumnath committed Sep 17, 2024
1 parent 6e0f4c0 commit 36e9794
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 61 deletions.
116 changes: 58 additions & 58 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ MainWindow::MainWindow(QWidget *parent) :
connect(updateTimer, &QTimer::timeout, this, &MainWindow::updateDateButton);
updateTimer->start(1000);

updateTimer = new QTimer(this);
connect(updateTimer, &QTimer::timeout, this, &MainWindow::adjustTextColorBasedOnBackground);
updateTimer->start(1000);

setWindowFlags(Qt::Tool | Qt::Window | Qt::FramelessWindowHint | Qt::BypassWindowManagerHint | Qt::WindowDoesNotAcceptFocus);
setMouseTracking(true);
setAttribute(Qt::WA_TranslucentBackground);
Expand Down Expand Up @@ -176,22 +180,23 @@ int MainWindow::cnvToNepali(int mm, int dd, int yy) {
bool MainWindow::eventFilter(QObject *watched, QEvent *event) {
if (watched == ui->dateButton) {
if (event->type() == QEvent::MouseMove) {
// Adjust the position above the button
QPoint position = ui->dateButton->mapToGlobal(QPoint(0, 0));
position.setY(position.y() - ui->dateButton->height() - 10); // Position the tooltip above the button
QToolTip::showText(position, ui->dateButton->toolTip(), ui->dateButton);
return true; // Indicate the event has been handled
if (!isDragging) {
QPoint position = ui->dateButton->mapToGlobal(QPoint(0, 0));
position.setY(position.y() - ui->dateButton->height() - 10);
QToolTip::showText(position, ui->dateButton->toolTip(), ui->dateButton);
}
return false; // Pass the event to the base class
} else if (event->type() == QEvent::Leave) {
// Hide the tooltip when leaving the button area
QToolTip::hideText();
return true; // Indicate the event has been handled
return false; // Pass the event to the base class
}
}
// Pass the event on to the base class
return QMainWindow::eventFilter(watched, event);
}




// Helper function to calculate the luminance of a color
double luminance(QColor color) {
double r = color.redF();
Expand Down Expand Up @@ -231,6 +236,9 @@ void MainWindow::adjustTextColorBasedOnBackground() {
double blackContrast = contrastRatio(averageColor, black);
double whiteContrast = contrastRatio(averageColor, white);

// Threshold for significant contrast difference (e.g., 12% difference)
const double contrastThreshold = 1.12;

// Determine the best color based on contrast ratios
if (blackContrast >= whiteContrast && blackContrast >= 4.5) {
bestColor = black;
Expand All @@ -242,12 +250,19 @@ void MainWindow::adjustTextColorBasedOnBackground() {
bestColor = (brightness > 128) ? black : white;
}

// Static variable to store the previous color
// Static variables to store the previous color and previous contrast
static QColor previousColor;
static double previousContrast = 0;

// Calculate the current contrast ratio for the chosen color
double currentContrast = (bestColor == black) ? blackContrast : whiteContrast;

// Only update the color if it differs significantly from the previous color
if (previousColor != bestColor) {
if (previousColor != bestColor &&
(currentContrast > previousContrast * contrastThreshold || currentContrast < previousContrast / contrastThreshold)) {

previousColor = bestColor;
previousContrast = currentContrast;

// Set the text color of the dateButton
QString styleSheet = QString("QPushButton { color: %1; border: none; outline: none; }").arg(bestColor.name());
Expand All @@ -262,10 +277,10 @@ QColor MainWindow::getAverageColor(const QImage &image) {

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();
QColor color(image.pixel(x, y));
red += color.red();
green += color.green();
blue += color.blue();
}
}

Expand Down Expand Up @@ -384,91 +399,76 @@ void MainWindow::contextMenuEvent(QContextMenuEvent *event) {
}

void MainWindow::mousePressEvent(QMouseEvent *event) {
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
dragStartPosition = event->globalPosition().toPoint() - frameGeometry().topLeft();
#else
dragStartPosition = event->globalPos() - frameGeometry().topLeft();
#endif
if (event->button() == Qt::LeftButton) {
dragStartPosition = event->globalPosition().toPoint() - frameGeometry().topLeft();
isDragging = true;
event->accept();
} else {
isDragging = false;
event->ignore();
}
QMainWindow::mousePressEvent(event);
}

void MainWindow::mouseMoveEvent(QMouseEvent *event) {
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
move(event->globalPosition().toPoint() - dragStartPosition);
#else
move(event->globalPos() - dragStartPosition);
#endif
// qDebug() << "Mouse Move Event detected.";
if (isDragging) {
move(event->globalPosition().toPoint() - dragStartPosition);
event->accept();
}
QMainWindow::mouseMoveEvent(event);
adjustTextColorBasedOnBackground();
}



void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
void MainWindow::mouseReleaseEvent(QMouseEvent *event) {
if (event->button() == Qt::LeftButton) {
isDragging = false;
event->accept();
}
QMainWindow::mouseReleaseEvent(event);
adjustTextColorBasedOnBackground();
}
void MainWindow::openCalendarWindow(const QString &link) {
qDebug() << "Clicked on datebutton. Link: " << link;
if (link == "#") {
// Ignore the link since it doesn't represent a valid action
return;
}

// Check if calendarWindow is nullptr or not visible


void MainWindow::openCalendarWindow() {
if (!calendarWindow || !calendarWindow->isVisible()) {
// If calendarWindow is nullptr or not visible, create a new instance
calendarWindow = new CalendarWindow(this);
calendarWindow->setWindowModality(Qt::NonModal);

// Connect the destroyed signal of calendarWindow to a lambda that sets calendarWindow to nullptr when the window is closed
connect(calendarWindow, &CalendarWindow::destroyed, this, [this]() {
calendarWindow = nullptr;
});

// Show the calendarWindow
calendarWindow->show();
} else {
// If calendarWindow is already visible, raise it to the top
calendarWindow->raise();
calendarWindow->activateWindow();

}
}

void MainWindow::on_dateButton_clicked()
{
// Check if calendarWindow is nullptr or not visible
if (!calendarWindow || !calendarWindow->isVisible()) {
// If calendarWindow is nullptr or not visible, create a new instance
calendarWindow = new CalendarWindow(this);

// Connect the destroyed signal of calendarWindow to a lambda that sets calendarWindow to nullptr when the window is closed
connect(calendarWindow, &CalendarWindow::destroyed, this, [this]() {
calendarWindow = nullptr;
});

// Show the calendarWindow
calendarWindow->show();
} else {
// If calendarWindow is already visible, raise it to the top
calendarWindow->raise();
calendarWindow->activateWindow();
}
// Call openCalendarWindow when the dateButton is clicked
openCalendarWindow();
}


void MainWindow::copyButtonText() {
QClipboard *clipboard = QGuiApplication::clipboard();
clipboard->setText(ui->dateButton->text());
}

void MainWindow::updateDateButton() {
QDate today = QDate::currentDate();
if (today != lastUpdatedDate) { // Check if the date has changed
if (today != lastUpdatedDate) {
lastUpdatedDate = today;
cnvToNepali(today.month(), today.day(), today.year());
adjustTextColorBasedOnBackground();
}
}


QString MainWindow::getnepalimonth(int m) {
const QString nepaliMonths[] = {
"बैशाख", "जेष्ठ", "आषाढ", "श्रावण", "भाद्र", "आश्विन",
Expand Down
2 changes: 1 addition & 1 deletion mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class MainWindow : public QMainWindow {
void exitAll();
bool eventFilter(QObject *watched, QEvent *event) override;
public slots:
void openCalendarWindow(const QString &link);
void openCalendarWindow();

private slots:

Expand Down
4 changes: 2 additions & 2 deletions mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>244</width>
<width>278</width>
<height>47</height>
</rect>
</property>
Expand Down Expand Up @@ -71,7 +71,7 @@ border-radius: 10px;</string>
</property>
<property name="geometry">
<rect>
<x>0</x>
<x>40</x>
<y>0</y>
<width>241</width>
<height>41</height>
Expand Down

0 comments on commit 36e9794

Please sign in to comment.