#include "NoteWindow.h"
#include "SettingsDialog.h"
#include "WedgeSecurity.h"
#include <QSizeGrip>
#include <QPalette>
#include <QDateTime>
#include <QResizeEvent>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QScrollBar>
#include <QRegularExpression>
#include <QTextCursor>
#include <QKeyEvent>
#include <QTextBlock>
#include <QCoreApplication>
NoteWindow::NoteWindow(QWidget *parent) : QWidget(parent) {
setWindowFlags(Qt::FramelessWindowHint);
setWindowTitle("wedge");
setupUi();
loadData();
installEventFilter(this);
resize(currentData.width, currentData.height);
move(currentData.x, currentData.y);
updateAppearance();
isInitialized = true;
}
void NoteWindow::setupUi() {
auto *layout = new QVBoxLayout(this);
layout->setContentsMargins(8, 8, 0, 8);
layout->setSpacing(5);
auto *toolbar = new QHBoxLayout();
toolbar->setContentsMargins(0, 0, 8, 0);
settingsBtn = new QPushButton("\u2630");
settingsBtn->setStyleSheet("QPushButton { border: none; font-size: 18px; background: transparent; } "
"QPushButton:hover { color: rgba(0,0,0,0.5); }");
toolbar->addStretch();
toolbar->addWidget(settingsBtn);
layout->addLayout(toolbar);
editor = new QTextEdit();
editor->setFrameStyle(QFrame::NoFrame);
editor->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
editor->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
editor->setLineWrapMode(QTextEdit::WidgetWidth);
editor->installEventFilter(this);
highlighter = new WedgeHighlighter(editor->document(), currentData.focusColor);
editor->verticalScrollBar()->setStyleSheet(
"QScrollBar:vertical { border: none; background: transparent; width: 8px; }"
"QScrollBar::handle:vertical { background: rgba(0, 0, 0, 0.4); min-height: 20px; border-radius: 4px; }"
"QScrollBar::add-line, QScrollBar::sub-line { height: 0px; }"
);
layout->addWidget(editor);
auto *grip = new QSizeGrip(this);
layout->addWidget(grip, 0, Qt::AlignBottom | Qt::AlignRight);
connect(settingsBtn, &QPushButton::clicked, this, &NoteWindow::openSettings);
connect(editor, &QTextEdit::textChanged, [this]() {
currentData.content = editor->toPlainText();
currentData.lastModified = QDateTime::currentSecsSinceEpoch();
ConfigManager::saveNote(currentData);
});
}
bool NoteWindow::eventFilter(QObject *obj, QEvent *event) {
if (event->type() == QEvent::WindowActivate) {
refreshDates();
}
if (obj == editor && event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
int key = keyEvent->key();
if (key == Qt::Key_Return || key == Qt::Key_Enter || key == Qt::Key_Space) {
if (handleCalculation(key)) return true;
}
}
return QWidget::eventFilter(obj, event);
}
QString NoteWindow::getOtcDateResult(int day, QString mon) {
static QMap<QString, int> months = {
{"jan", 1}, {"feb", 2}, {"mar", 3}, {"apr", 4}, {"may", 5}, {"jun", 6},
{"jul", 7}, {"aug", 8}, {"sep", 9}, {"oct", 10}, {"nov", 11}, {"dec", 12}
};
int m = months.value(mon.toLower(), 0);
if (m == 0) return "";
QDate now = QDate::currentDate();
QDate target(now.year(), m, day);
if (target < now) target = target.addYears(1);
QDate otcStart(target.year(), 3, 20);
if (target < otcStart) otcStart = otcStart.addYears(-1);
return QString("(%1)=%2").arg(otcStart.daysTo(target) + 1).arg(now.daysTo(target));
}
void NoteWindow::refreshDates() {
if (!editor) return;
QString content = editor->toPlainText();
QString newContent = content;
static QRegularExpression scanRegex("(?<=\\s|^)(\\d{1,2})([a-zA-Z]{3})\\(\\d*\\)=\\d*");
QRegularExpressionMatchIterator i = scanRegex.globalMatch(content);
int offset = 0;
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
QString res = getOtcDateResult(match.captured(1).toInt(), match.captured(2));
if (!res.isEmpty()) {
QString replacement = match.captured(1) + match.captured(2) + res;
newContent.replace(match.capturedStart() + offset, match.capturedLength(), replacement);
offset += (replacement.length() - match.capturedLength());
}
}
if (newContent != content) {
QTextCursor cursor = editor->textCursor();
int pos = cursor.position();
editor->blockSignals(true);
editor->setPlainText(newContent);
cursor.setPosition(qMin(pos, newContent.length()));
editor->setTextCursor(cursor);
editor->blockSignals(false);
currentData.content = newContent;
ConfigManager::saveNote(currentData);
}
}
bool NoteWindow::handleCalculation(int key) {
QTextCursor cursor = editor->textCursor();
QString lineText = cursor.block().text().left(cursor.positionInBlock());
static QRegularExpression mathRegex("(?<=\\s|^)((\\d+(\\.\\d+)?%?[\\+\\-\\*\\/])+\\d+(\\.\\d+)?%?)=$");
static QRegularExpression dateRegex("(?<=\\s|^)(\\d{1,2})([a-zA-Z]{3})\\(\\)=$");
static QRegularExpression listRegex("^(\\s*)(\\d+)\\.\\s");
static QRegularExpression hrRegex("(?<=\\s|^)hr\\((\\d+)\\)$");
static QRegularExpression dashRegex("(?<=\\s|^)dash\\((\\d+)\\)$");
auto mMatch = mathRegex.match(lineText);
if (mMatch.hasMatch()) {
QString expression = mMatch.captured(1);
static QRegularExpression percentRegex("([\\d\\.]+)\\s*([\\+\\-\\*\\/])\\s*([\\d\\.]+)%");
while (expression.contains('%')) {
QRegularExpressionMatch pMatch = percentRegex.match(expression);
if (!pMatch.hasMatch()) break;
QString base = pMatch.captured(1);
QString op = pMatch.captured(2);
QString percentVal = pMatch.captured(3);
QString replacement = QString("%1 %2 (%1 * (%3 / 100))").arg(base, op, percentVal);
expression.replace(pMatch.capturedStart(), pMatch.capturedLength(), replacement);
}
QJSValue result = mathEngine.evaluate(expression);
if (!result.isError()) {
QString resStr = QString::number(result.toNumber(), 'f', 2);
if (resStr.contains('.')) {
while (resStr.endsWith('0')) resStr.chop(1);
if (resStr.endsWith('.')) resStr.chop(1);
}
editor->blockSignals(true);
cursor.insertText(resStr);
editor->blockSignals(false);
currentData.content = editor->toPlainText();
ConfigManager::saveNote(currentData);
}
return (key == Qt::Key_Space);
}
auto dMatch = dateRegex.match(lineText);
if (dMatch.hasMatch()) {
QString res = getOtcDateResult(dMatch.captured(1).toInt(), dMatch.captured(2));
if (!res.isEmpty()) {
editor->blockSignals(true); cursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor, 3);
cursor.insertText(res); editor->blockSignals(false);
currentData.content = editor->toPlainText(); ConfigManager::saveNote(currentData);
}
return (key == Qt::Key_Space);
}
auto hMatch = hrRegex.match(lineText);
if (hMatch.hasMatch()) {
int count = hMatch.captured(1).toInt();
if (count > 0) {
QString hrline = QString("\u2014").repeated(qMin(count, 100));
editor->blockSignals(true);
cursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor, hMatch.capturedLength());
cursor.insertText(hrline);
editor->blockSignals(false);
currentData.content = editor->toPlainText(); ConfigManager::saveNote(currentData);
}
return (key == Qt::Key_Space);
}
auto emMatch = dashRegex.match(lineText);
if (emMatch.hasMatch()) {
int counts = emMatch.captured(1).toInt();
if (counts > 0) {
QString dashes = QString("\u2014 ").repeated(qMin(counts, 100));
editor->blockSignals(true);
cursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor, emMatch.capturedLength());
cursor.insertText(dashes);
editor->blockSignals(false);
currentData.content = editor->toPlainText(); ConfigManager::saveNote(currentData);
}
return (key == Qt::Key_Space);
}
if (key != Qt::Key_Space) {
auto lMatch = listRegex.match(lineText);
if (lMatch.hasMatch()) {
QString ws = lMatch.captured(1);
int num = lMatch.captured(2).toInt();
QString prefix = "\n" + ws + QString::number(num + 1) + ". ";
editor->blockSignals(true);
cursor.beginEditBlock();
cursor.insertText(prefix);
QTextBlock nextBlock = cursor.block().next();
int nextExpected = num + 1;
int i = 0;
while (nextBlock.isValid()) {
auto m = listRegex.match(nextBlock.text());
if (m.hasMatch() && m.captured(1) == ws && m.captured(2).toInt() == nextExpected) {
int foundNum = m.captured(2).toInt();
QTextCursor nextCursor(nextBlock);
nextCursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, m.capturedLength());
nextCursor.insertText(m.captured(1) + QString::number(foundNum + 1) + ". ");
nextExpected++;
} else if (i > 0) break;
nextBlock = nextBlock.next(); i++;
}
cursor.endEditBlock(); editor->blockSignals(false);
currentData.content = editor->toPlainText(); ConfigManager::saveNote(currentData);
return true;
}
static QRegularExpression indentRegex("^(\\s+)");
auto iMatch = indentRegex.match(lineText);
if (iMatch.hasMatch()) {
QString indent = "\n" + iMatch.captured(1);
editor->blockSignals(true);
cursor.insertText(indent);
editor->blockSignals(false);
currentData.content = editor->toPlainText(); ConfigManager::saveNote(currentData);
return true;
}
}
return false;
}
void NoteWindow::updateAppearance() {
Qt::WindowFlags flags = Qt::FramelessWindowHint;
if (currentData.hideTaskbar) {
flags |= Qt::Tool;
} else {
flags |= Qt::Window;
}
if (windowFlags() != flags) {
setWindowFlags(flags);
setAttribute(Qt::WA_QuitOnClose, true);
show();
}
QPalette pal = palette();
pal.setColor(QPalette::Window, currentData.bgColor);
setPalette(pal);
setAutoFillBackground(true);
highlighter->setFocusColor(currentData.focusColor);
editor->setStyleSheet(QString(
"QTextEdit { background-color: %1; color: %2; font-family: '%3'; font-size: %4pt; border: none; }"
).arg(currentData.bgColor.name())
.arg(currentData.textColor.name())
.arg(currentData.fontFamily)
.arg(currentData.fontSize));
}
void NoteWindow::closeEvent(QCloseEvent *event) {
QCoreApplication::quit();
event->accept();
}
void NoteWindow::resizeEvent(QResizeEvent *event) {
if (isInitialized) {
currentData.width = event->size().width();
currentData.height = event->size().height();
ConfigManager::saveNote(currentData);
}
QWidget::resizeEvent(event);
}
void NoteWindow::loadData() {
currentData = ConfigManager::loadNote();
if (editor) {
editor->blockSignals(true);
editor->setPlainText(currentData.content);
editor->blockSignals(false);
refreshDates();
}
}
void NoteWindow::openSettings() {
SettingsDialog dialog(currentData, this);
connect(&dialog, &SettingsDialog::encryptRequested, this, [this](const QString &phrase) {
QString plain = editor->toPlainText();
QString cipher = WedgeSecurity::encrypt(plain, phrase);
if (!cipher.isEmpty()) {
editor->setPlainText(cipher);
currentData.content = cipher;
ConfigManager::saveNote(currentData);
}
});
connect(&dialog, &SettingsDialog::decryptRequested, this, [this](const QString &phrase) {
QString cipher = editor->toPlainText();
if (WedgeSecurity::isEncrypted(cipher)) {
QString plain = WedgeSecurity::decrypt(cipher, phrase);
if (!plain.isEmpty()) {
editor->setPlainText(plain);
currentData.content = plain;
ConfigManager::saveNote(currentData);
}
}
});
if (dialog.exec() == QDialog::Accepted) {
currentData = dialog.getUpdatedData();
updateAppearance();
ConfigManager::saveNote(currentData);
}
}
void NoteWindow::mousePressEvent(QMouseEvent *event) {
if (event->button() == Qt::LeftButton) {
dragPosition = event->globalPosition().toPoint() - frameGeometry().topLeft();
event->accept();
}
}
void NoteWindow::mouseMoveEvent(QMouseEvent *event) {
if (event->buttons() & Qt::LeftButton) {
QPoint newPos = event->globalPosition().toPoint() - dragPosition;
move(newPos);
if (isInitialized) {
currentData.x = newPos.x();
currentData.y = newPos.y();
ConfigManager::saveNote(currentData);
}
event->accept();
}
}