scroll through extra window history

This commit is contained in:
Akash Mozumdar 2019-08-12 10:43:34 -04:00
parent e4c74a55fe
commit 4e3c1a93e0

View File

@ -7,6 +7,7 @@
#include <QMenu> #include <QMenu>
#include <QPainter> #include <QPainter>
#include <QMouseEvent> #include <QMouseEvent>
#include <QWheelEvent>
extern const char* EXTRA_WINDOW_INFO; extern const char* EXTRA_WINDOW_INFO;
extern const char* TOPMOST; extern const char* TOPMOST;
@ -55,7 +56,7 @@ public:
} }
connect(ui.display, &QLabel::customContextMenuRequested, [this](QPoint point) { menu.exec(mapToGlobal(point)); }); connect(ui.display, &QLabel::customContextMenuRequested, [this](QPoint point) { menu.exec(mapToGlobal(point)); });
QMetaObject::invokeMethod(this, [this] { ui.display->setText(EXTRA_WINDOW_INFO); }, Qt::QueuedConnection); QMetaObject::invokeMethod(this, [this] { AddSentence(EXTRA_WINDOW_INFO); }, Qt::QueuedConnection);
}, Qt::QueuedConnection); }, Qt::QueuedConnection);
} }
@ -65,6 +66,13 @@ public:
settings.sync(); settings.sync();
} }
void AddSentence(const QString& sentence)
{
sentenceHistory.push_back(sentence);
i = sentenceHistory.size() - 1;
ui.display->setText(sentence);
}
Ui::ExtraWindow ui; Ui::ExtraWindow ui;
QSettings settings{ CONFIG_FILE, QSettings::IniFormat, this }; QSettings settings{ CONFIG_FILE, QSettings::IniFormat, this };
@ -133,10 +141,19 @@ private:
oldPos = event->globalPos(); oldPos = event->globalPos();
} }
void wheelEvent(QWheelEvent* event) override
{
QPoint scroll = event->angleDelta();
if (scroll.y() > 0 && i > 0) ui.display->setText(sentenceHistory[--i]);
if (scroll.y() < 0 && i + 1 < sentenceHistory.size()) ui.display->setText(sentenceHistory[++i]);
}
QMenu menu{ ui.display }; QMenu menu{ ui.display };
bool locked = true; bool locked = true;
QColor bgColor; QColor bgColor;
QPoint oldPos; QPoint oldPos;
std::vector<QString> sentenceHistory;
int i = 0;
} window; } window;
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
@ -146,6 +163,6 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
QString qSentence = S(sentence); QString qSentence = S(sentence);
if (!window.settings.value(SHOW_ORIGINAL, true).toBool()) qSentence = qSentence.section('\n', qSentence.count('\n') / 2 + 1); if (!window.settings.value(SHOW_ORIGINAL, true).toBool()) qSentence = qSentence.section('\n', qSentence.count('\n') / 2 + 1);
QMetaObject::invokeMethod(&window, [=] { window.ui.display->setText(qSentence); }); QMetaObject::invokeMethod(&window, [=] { window.AddSentence(qSentence); });
return false; return false;
} }