From 8c4b1e00a128b739904ed60543132b34817f0ba5 Mon Sep 17 00:00:00 2001 From: TriKriSta Date: Sat, 25 May 2019 22:16:47 +0300 Subject: [PATCH] feat: edit load history in search --- src/chatlog/content/text.cpp | 23 ++++++++++---- src/chatlog/content/text.h | 4 +++ src/persistence/history.cpp | 14 ++++---- src/widget/form/genericchatform.cpp | 44 ++++++++++++++++---------- src/widget/form/genericchatform.h | 4 +-- src/widget/form/loadhistorydialog.cpp | 12 +++---- src/widget/form/loadhistorydialog.h | 3 +- src/widget/form/loadhistorydialog.ui | 2 +- src/widget/form/searchsettingsform.cpp | 13 ++++---- src/widget/form/searchsettingsform.h | 2 +- src/widget/searchtypes.h | 4 +-- 11 files changed, 72 insertions(+), 53 deletions(-) diff --git a/src/chatlog/content/text.cpp b/src/chatlog/content/text.cpp index d32adc4e8..e1877ad23 100644 --- a/src/chatlog/content/text.cpp +++ b/src/chatlog/content/text.cpp @@ -66,9 +66,11 @@ void Text::selectText(const QString& txt, const std::pair& point) return; } - auto cursor = doc->find(txt, point.first); + selectCursor = doc->find(txt, point.first); + selectPoint = point; - selectText(cursor, point); + regenerate(); + update(); } void Text::selectText(const QRegularExpression &exp, const std::pair& point) @@ -79,14 +81,20 @@ void Text::selectText(const QRegularExpression &exp, const std::pair& return; } - auto cursor = doc->find(exp, point.first); + selectCursor = doc->find(exp, point.first); + selectPoint = point; - selectText(cursor, point); + regenerate(); + update(); } void Text::deselectText() { dirty = true; + + selectCursor = QTextCursor(); + selectPoint = {0, 0}; + regenerate(); update(); } @@ -355,6 +363,10 @@ void Text::regenerate() dirty = false; } + if (!selectCursor.isNull()) { + selectText(selectCursor, selectPoint); + } + // if we are not visible -> free mem if (!keepInMemory) freeResources(); @@ -462,9 +474,6 @@ void Text::selectText(QTextCursor& cursor, const std::pair& point) QTextCharFormat format; format.setBackground(QBrush(Style::getColor(Style::SearchHighlighted))); cursor.mergeCharFormat(format); - - regenerate(); - update(); } } diff --git a/src/chatlog/content/text.h b/src/chatlog/content/text.h index e1f69112b..0f58bdb4a 100644 --- a/src/chatlog/content/text.h +++ b/src/chatlog/content/text.h @@ -24,6 +24,7 @@ #include "src/widget/style.h" #include +#include class QTextDocument; @@ -110,6 +111,9 @@ private: TextType textType; QColor color; QColor customColor; + + QTextCursor selectCursor; + std::pair selectPoint{0, 0}; }; #endif // TEXT_H diff --git a/src/persistence/history.cpp b/src/persistence/history.cpp index a309eb10c..41c9323d3 100644 --- a/src/persistence/history.cpp +++ b/src/persistence/history.cpp @@ -681,14 +681,14 @@ QDateTime History::getDateWhereFindPhrase(const QString& friendPk, const QDateTi break; } - QDateTime date = from; + QDateTime time = from; - if (!date.isValid()) { - date = QDateTime::currentDateTime(); + if (!time.isValid()) { + time = QDateTime::currentDateTime(); } if (parameter.period == PeriodSearch::AfterDate || parameter.period == PeriodSearch::BeforeDate) { - date = QDateTime(parameter.date); + time = parameter.time; } QString period; @@ -698,15 +698,15 @@ QDateTime History::getDateWhereFindPhrase(const QString& friendPk, const QDateTi break; case PeriodSearch::AfterDate: period = QStringLiteral("AND timestamp > '%1' ORDER BY timestamp ASC LIMIT 1;") - .arg(date.toMSecsSinceEpoch()); + .arg(time.toMSecsSinceEpoch()); break; case PeriodSearch::BeforeDate: period = QStringLiteral("AND timestamp < '%1' ORDER BY timestamp DESC LIMIT 1;") - .arg(date.toMSecsSinceEpoch()); + .arg(time.toMSecsSinceEpoch()); break; default: period = QStringLiteral("AND timestamp < '%1' ORDER BY timestamp DESC LIMIT 1;") - .arg(date.toMSecsSinceEpoch()); + .arg(time.toMSecsSinceEpoch()); break; } diff --git a/src/widget/form/genericchatform.cpp b/src/widget/form/genericchatform.cpp index a047f1fee..658daef69 100644 --- a/src/widget/form/genericchatform.cpp +++ b/src/widget/form/genericchatform.cpp @@ -37,7 +37,6 @@ #include "src/widget/contentlayout.h" #include "src/widget/emoticonswidget.h" #include "src/widget/form/chatform.h" -#include "src/widget/form/loadhistorydialog.h" #include "src/widget/maskablepixmapwidget.h" #include "src/widget/searchform.h" #include "src/widget/style.h" @@ -646,6 +645,23 @@ QDateTime GenericChatForm::getTime(const ChatLine::Ptr &chatLine) const return QDateTime(); } +void GenericChatForm::loadHistory(const QDateTime &time, const LoadHistoryDialog::LoadType type) +{ + chatWidget->clear(); + messages.clear(); + + auto begin = firstItemAfterDate(time.date(), chatLog); + auto end = ChatLogIdx(begin.get() + 1); + + renderMessages(begin, end); + + if (type == LoadHistoryDialog::from) { + loadHistoryUpper(); + } else { + loadHistoryLower(); + } +} + void GenericChatForm::disableSearchText() { @@ -811,22 +827,10 @@ void GenericChatForm::onLoadHistory() { LoadHistoryDialog dlg(&chatLog); if (dlg.exec()) { - chatWidget->clear(); - messages.clear(); - QDateTime time = dlg.getFromDate(); auto type = dlg.getLoadType(); - auto begin = firstItemAfterDate(dlg.getFromDate().date(), chatLog); - auto end = ChatLogIdx(begin.get() + 1); - - renderMessages(begin, end); - - if (type == LoadHistoryDialog::from) { - loadHistoryUpper(); - } else { - loadHistoryLower(); - } + loadHistory(time, type); } } @@ -873,6 +877,12 @@ void GenericChatForm::searchInBegin(const QString& phrase, const ParameterSearch { disableSearchText(); + if (!parameter.time.isNull()) { + LoadHistoryDialog::LoadType type = (parameter.period == PeriodSearch::BeforeDate) + ? LoadHistoryDialog::to : LoadHistoryDialog::from; + loadHistory(parameter.time, type); + } + bool bForwardSearch = false; switch (parameter.period) { case PeriodSearch::WithTheFirst: { @@ -890,13 +900,13 @@ void GenericChatForm::searchInBegin(const QString& phrase, const ParameterSearch } case PeriodSearch::AfterDate: { bForwardSearch = true; - searchPos.logIdx = firstItemAfterDate(parameter.date, chatLog); + searchPos.logIdx = firstItemAfterDate(parameter.time.date(), chatLog); searchPos.numMatches = 0; break; } case PeriodSearch::BeforeDate: { bForwardSearch = false; - searchPos.logIdx = firstItemAfterDate(parameter.date, chatLog); + searchPos.logIdx = firstItemAfterDate(parameter.time.date(), chatLog); searchPos.numMatches = 0; break; } @@ -1007,7 +1017,7 @@ void GenericChatForm::loadHistoryLower() void GenericChatForm::loadHistoryUpper() { - auto begin = messages.end()->first; + auto begin = messages.rbegin()->first; int add = 100; if (begin.get() + 100 > chatLog.getNextIdx().get()) { diff --git a/src/widget/form/genericchatform.h b/src/widget/form/genericchatform.h index 699aacc9c..13699c224 100644 --- a/src/widget/form/genericchatform.h +++ b/src/widget/form/genericchatform.h @@ -23,6 +23,7 @@ #include "src/chatlog/chatmessage.h" #include "src/core/toxpk.h" #include "src/model/ichatlog.h" +#include "src/widget/form/loadhistorydialog.h" #include "src/widget/searchtypes.h" #include @@ -133,6 +134,7 @@ private: void retranslateUi(); void addSystemDateMessage(const QDate& date); QDateTime getTime(const ChatLine::Ptr& chatLine) const; + void loadHistory(const QDateTime& time, const LoadHistoryDialog::LoadType type); protected: ChatMessage::Ptr createMessage(const ToxPk& author, const QString& message, @@ -166,8 +168,6 @@ protected: ToxPk previousId; - QDateTime earliestMessage; - QMenu menu; QPushButton* emoteButton; diff --git a/src/widget/form/loadhistorydialog.cpp b/src/widget/form/loadhistorydialog.cpp index 2ff15eeb4..12e9d8197 100644 --- a/src/widget/form/loadhistorydialog.cpp +++ b/src/widget/form/loadhistorydialog.cpp @@ -71,14 +71,12 @@ LoadHistoryDialog::LoadType LoadHistoryDialog::getLoadType() return LoadType::to; } -void LoadHistoryDialog::setTitle(const QString& title) +void LoadHistoryDialog::turnSearchMode() { - setWindowTitle(title); -} - -void LoadHistoryDialog::setInfoLabel(const QString& info) -{ - ui->fromLabel->setText(info); + setWindowTitle(tr("Select Date Dialog")); + ui->fromLabel->setText(tr("Select a date")); + ui->loadTypeComboBox->setVisible(false); + ui->infoLabel->setVisible(false); } void LoadHistoryDialog::highlightDates(int year, int month) diff --git a/src/widget/form/loadhistorydialog.h b/src/widget/form/loadhistorydialog.h index dfb046867..59c769573 100644 --- a/src/widget/form/loadhistorydialog.h +++ b/src/widget/form/loadhistorydialog.h @@ -45,8 +45,7 @@ public: QDateTime getFromDate(); LoadType getLoadType(); - void setTitle(const QString& title); - void setInfoLabel(const QString& info); + void turnSearchMode(); public slots: void highlightDates(int year, int month); diff --git a/src/widget/form/loadhistorydialog.ui b/src/widget/form/loadhistorydialog.ui index 5bf3fc553..b382c706d 100644 --- a/src/widget/form/loadhistorydialog.ui +++ b/src/widget/form/loadhistorydialog.ui @@ -41,7 +41,7 @@ - + (about 100 messages are loaded) diff --git a/src/widget/form/searchsettingsform.cpp b/src/widget/form/searchsettingsform.cpp index abdf6b40c..733c1a4e2 100644 --- a/src/widget/form/searchsettingsform.cpp +++ b/src/widget/form/searchsettingsform.cpp @@ -86,7 +86,7 @@ ParameterSearch SearchSettingsForm::getParameterSearch() break; } - ps.date = startDate; + ps.time = startTime; ps.isUpdate = isUpdate; isUpdate = false; @@ -101,7 +101,7 @@ void SearchSettingsForm::reloadTheme() void SearchSettingsForm::updateStartDateLabel() { - ui->startDateLabel->setText(startDate.toString(Settings::getInstance().getDateFormat())); + ui->startDateLabel->setText(startTime.toString(Settings::getInstance().getDateFormat())); } void SearchSettingsForm::setUpdate(const bool isUpdate) @@ -119,8 +119,8 @@ void SearchSettingsForm::onStartSearchSelected(const int index) ui->choiceDateButton->setProperty("state", QStringLiteral("green")); ui->choiceDateButton->setStyleSheet(Style::getStylesheet(QStringLiteral("chatForm/buttons.css"))); - if (startDate.isNull()) { - startDate = QDate::currentDate(); + if (startTime.isNull()) { + startTime = QDateTime::currentDateTime(); updateStartDateLabel(); } @@ -162,10 +162,9 @@ void SearchSettingsForm::onRegularClicked(const bool checked) void SearchSettingsForm::onChoiceDate() { LoadHistoryDialog dlg; - dlg.setTitle(tr("Select Date Dialog")); - dlg.setInfoLabel(tr("Select a date")); + dlg.turnSearchMode(); if (dlg.exec()) { - startDate = dlg.getFromDate().date(); + startTime = dlg.getFromDate(); updateStartDateLabel(); } diff --git a/src/widget/form/searchsettingsform.h b/src/widget/form/searchsettingsform.h index 91d665be5..1814c4e75 100644 --- a/src/widget/form/searchsettingsform.h +++ b/src/widget/form/searchsettingsform.h @@ -40,7 +40,7 @@ public: private: Ui::SearchSettingsForm *ui; - QDate startDate; + QDateTime startTime; bool isUpdate{false}; void updateStartDateLabel(); diff --git a/src/widget/searchtypes.h b/src/widget/searchtypes.h index cbfce5738..7130887b6 100644 --- a/src/widget/searchtypes.h +++ b/src/widget/searchtypes.h @@ -48,13 +48,13 @@ enum class SearchDirection { struct ParameterSearch { FilterSearch filter{FilterSearch::None}; PeriodSearch period{PeriodSearch::None}; - QDate date; + QDateTime time; bool isUpdate{false}; bool operator ==(const ParameterSearch& other) { return filter == other.filter && period == other.period && - date == other.date; + time == other.time; } bool operator !=(const ParameterSearch& other) {