diff --git a/src/chatlog/chatlog.cpp b/src/chatlog/chatlog.cpp index 1d8edd5e3..dd2ca0b82 100644 --- a/src/chatlog/chatlog.cpp +++ b/src/chatlog/chatlog.cpp @@ -168,8 +168,9 @@ void ChatLog::updateSceneRect() void ChatLog::layout(int start, int end, qreal width) { - if (lines.empty()) + if (lines.empty()) { return; + } qreal h = 0.0; @@ -312,8 +313,9 @@ void ChatLog::mouseMoveEvent(QMouseEvent* ev) // Much faster than QGraphicsScene::itemAt()! ChatLineContent* ChatLog::getContentFromPos(QPointF scenePos) const { - if (lines.empty()) + if (lines.empty()) { return nullptr; + } auto itr = std::lower_bound(lines.cbegin(), lines.cend(), scenePos.y(), ChatLine::lessThanBSRectBottom); @@ -454,8 +456,9 @@ void ChatLog::scrollToBottom() void ChatLog::startResizeWorker() { - if (lines.empty()) + if (lines.empty()) { return; + } // (re)start the worker if (!workerTimer->isActive()) { @@ -656,8 +659,9 @@ void ChatLog::scrollToLine(ChatLine::Ptr line) void ChatLog::selectAll() { - if (lines.empty()) + if (lines.empty()) { return; + } clearSelection(); @@ -717,8 +721,9 @@ void ChatLog::forceRelayout() void ChatLog::checkVisibility(bool causedWheelEvent) { - if (lines.empty()) + if (lines.empty()) { return; + } // find first visible line auto lowerBound = std::lower_bound(lines.cbegin(), lines.cend(), getVisibleRect().top(), @@ -813,8 +818,9 @@ void ChatLog::updateTypingNotification() qreal posY = 0.0; - if (!lines.empty()) + if (!lines.empty()) { posY = lines.last()->sceneBoundingRect().bottom() + lineSpacing; + } notification->layout(useableWidth(), QPointF(0.0, posY)); } diff --git a/src/model/chathistory.cpp b/src/model/chathistory.cpp index 24096f710..84bfb5d33 100644 --- a/src/model/chathistory.cpp +++ b/src/model/chathistory.cpp @@ -110,10 +110,9 @@ ChatHistory::ChatHistory(Friend& f_, History* history_, const ICoreIdHandler& co // NOTE: this has to be done _after_ sending all sent messages since initial // state of the message has to be marked according to our dispatch state - constexpr auto defaultNumMessagesToLoad = 100; - auto firstChatLogIdx = sessionChatLog.getFirstIdx().get() < defaultNumMessagesToLoad + auto firstChatLogIdx = sessionChatLog.getFirstIdx().get() < DEF_NUM_MSG_TO_LOAD ? ChatLogIdx(0) - : sessionChatLog.getFirstIdx() - defaultNumMessagesToLoad; + : sessionChatLog.getFirstIdx() - DEF_NUM_MSG_TO_LOAD; if (canUseHistory()) { loadHistoryIntoSessionChatLog(firstChatLogIdx); diff --git a/src/model/ichatlog.h b/src/model/ichatlog.h index 2144c2d0d..1438610d6 100644 --- a/src/model/ichatlog.h +++ b/src/model/ichatlog.h @@ -35,6 +35,8 @@ #include +static const auto DEF_NUM_MSG_TO_LOAD = 100; + using ChatLogIdx = NamedType; Q_DECLARE_METATYPE(ChatLogIdx); diff --git a/src/widget/form/genericchatform.cpp b/src/widget/form/genericchatform.cpp index 2c59c8846..bd66e8bd3 100644 --- a/src/widget/form/genericchatform.cpp +++ b/src/widget/form/genericchatform.cpp @@ -388,7 +388,7 @@ GenericChatForm::GenericChatForm(const Contact* contact, IChatLog& chatLog, connect(contact, &Contact::displayedNameChanged, this, &GenericChatForm::setName); auto chatLogIdxRange = chatLog.getNextIdx() - chatLog.getFirstIdx(); - auto firstChatLogIdx = (chatLogIdxRange < 100) ? chatLog.getFirstIdx() : chatLog.getNextIdx() - 100; + auto firstChatLogIdx = (chatLogIdxRange < DEF_NUM_MSG_TO_LOAD) ? chatLog.getFirstIdx() : chatLog.getNextIdx() - DEF_NUM_MSG_TO_LOAD; renderMessages(firstChatLogIdx, chatLog.getNextIdx()); @@ -670,10 +670,10 @@ void GenericChatForm::loadHistoryTo(const QDateTime &time) { auto end = ChatLogIdx(0); if (time.isNull()) { - if (messages.size() + 100 >= maxMessages) { - end = ChatLogIdx(messages.rbegin()->first.get() - 100); - chatWidget->removeLasts(100); - removeLastsMessages(100); + if (messages.size() + DEF_NUM_MSG_TO_LOAD >= maxMessages) { + end = ChatLogIdx(messages.rbegin()->first.get() - DEF_NUM_MSG_TO_LOAD); + chatWidget->removeLasts(DEF_NUM_MSG_TO_LOAD); + removeLastsMessages(DEF_NUM_MSG_TO_LOAD); } else { end = messages.begin()->first; } @@ -682,8 +682,8 @@ void GenericChatForm::loadHistoryTo(const QDateTime &time) } auto begin = ChatLogIdx(0); - if (end.get() > 100) { - begin = ChatLogIdx(end.get() - 100); + if (end.get() > DEF_NUM_MSG_TO_LOAD) { + begin = ChatLogIdx(end.get() - DEF_NUM_MSG_TO_LOAD); } renderMessages(begin, end); @@ -693,10 +693,10 @@ void GenericChatForm::loadHistoryFrom(const QDateTime &time) { auto begin = ChatLogIdx(0); if (time.isNull()) { - if (messages.size() + 100 >= maxMessages) { - begin = ChatLogIdx(messages.rbegin()->first.get() + 100); - chatWidget->removeFirsts(100); - removeFirstsMessages(100); + if (messages.size() + DEF_NUM_MSG_TO_LOAD >= maxMessages) { + begin = ChatLogIdx(messages.rbegin()->first.get() + DEF_NUM_MSG_TO_LOAD); + chatWidget->removeFirsts(DEF_NUM_MSG_TO_LOAD); + removeFirstsMessages(DEF_NUM_MSG_TO_LOAD); } else { begin = messages.rbegin()->first; } @@ -705,9 +705,9 @@ void GenericChatForm::loadHistoryFrom(const QDateTime &time) begin = firstItemAfterDate(time.date(), chatLog); } - int add = 100; - if (begin.get() + 100 > chatLog.getNextIdx().get()) { - add = chatLog.getNextIdx().get() - (begin.get() + 100); + int add = DEF_NUM_MSG_TO_LOAD; + if (begin.get() + DEF_NUM_MSG_TO_LOAD > chatLog.getNextIdx().get()) { + add = chatLog.getNextIdx().get() - (begin.get() + DEF_NUM_MSG_TO_LOAD); } auto end = ChatLogIdx(begin.get() + add); renderMessages(begin, end); @@ -724,8 +724,8 @@ void GenericChatForm::removeFirstsMessages(const int num) void GenericChatForm::removeLastsMessages(const int num) { - if (messages.size() > 100) { - messages.erase(std::next(messages.end(), -100), messages.end()); + if (messages.size() > num) { + messages.erase(std::next(messages.end(), -num), messages.end()); } else { messages.clear(); } @@ -1082,7 +1082,7 @@ void GenericChatForm::goToCurrentDate() chatWidget->clear(); messages.clear(); auto end = ChatLogIdx(chatLog.size() - 1); - auto begin = end.get() > 100 ? ChatLogIdx(end.get() - 100) : ChatLogIdx(0); + auto begin = end.get() > DEF_NUM_MSG_TO_LOAD ? ChatLogIdx(end.get() - DEF_NUM_MSG_TO_LOAD) : ChatLogIdx(0); renderMessages(begin, end); } diff --git a/src/widget/form/loadhistorydialog.cpp b/src/widget/form/loadhistorydialog.cpp index 12e9d8197..c3246bb8f 100644 --- a/src/widget/form/loadhistorydialog.cpp +++ b/src/widget/form/loadhistorydialog.cpp @@ -38,11 +38,15 @@ LoadHistoryDialog::LoadHistoryDialog(const IChatLog* chatLog, QWidget* parent) &LoadHistoryDialog::highlightDates); } -LoadHistoryDialog::LoadHistoryDialog(QWidget* parent) +LoadHistoryDialog::LoadHistoryDialog(Mode mode, QWidget* parent) : QDialog(parent) , ui(new Ui::LoadHistoryDialog) { ui->setupUi(this); + + if (mode == Mode::search) { + enableSearchMode(); + } } LoadHistoryDialog::~LoadHistoryDialog() @@ -71,7 +75,7 @@ LoadHistoryDialog::LoadType LoadHistoryDialog::getLoadType() return LoadType::to; } -void LoadHistoryDialog::turnSearchMode() +void LoadHistoryDialog::enableSearchMode() { setWindowTitle(tr("Select Date Dialog")); ui->fromLabel->setText(tr("Select a date")); diff --git a/src/widget/form/loadhistorydialog.h b/src/widget/form/loadhistorydialog.h index 59c769573..7fdd178b6 100644 --- a/src/widget/form/loadhistorydialog.h +++ b/src/widget/form/loadhistorydialog.h @@ -39,18 +39,24 @@ public: to }; + enum Mode { + common, + search + }; + explicit LoadHistoryDialog(const IChatLog* chatLog, QWidget* parent = nullptr); - explicit LoadHistoryDialog(QWidget* parent = nullptr); + explicit LoadHistoryDialog(Mode mode, QWidget* parent = nullptr); ~LoadHistoryDialog(); QDateTime getFromDate(); LoadType getLoadType(); - void turnSearchMode(); public slots: void highlightDates(int year, int month); private: + void enableSearchMode(); + Ui::LoadHistoryDialog* ui; const IChatLog* chatLog; }; diff --git a/src/widget/form/searchsettingsform.cpp b/src/widget/form/searchsettingsform.cpp index 733c1a4e2..c55735099 100644 --- a/src/widget/form/searchsettingsform.cpp +++ b/src/widget/form/searchsettingsform.cpp @@ -161,8 +161,7 @@ void SearchSettingsForm::onRegularClicked(const bool checked) void SearchSettingsForm::onChoiceDate() { - LoadHistoryDialog dlg; - dlg.turnSearchMode(); + LoadHistoryDialog dlg(LoadHistoryDialog::search); if (dlg.exec()) { startTime = dlg.getFromDate(); updateStartDateLabel();