1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor: another refactoring step

Brief list of changes:
  - contentdialog.* and friendlistwidget.cpp reverted to their previous states;
  - added macros for setting stylesheets for form components;
  - added 'static' qualificator for 'fontToCss' function;
  - added 'needsToHideName' method and several places in code replaced with this
  method's calls;
  - some changes which are related to previous listed;
  - 'resolveToxId' returned to GenericChatForm class.
This commit is contained in:
alexrazinkov 2017-04-20 14:56:59 +03:00 committed by noavarice
parent a83f8d1dfd
commit 972c3f2353
8 changed files with 85 additions and 72 deletions

View File

@ -69,8 +69,11 @@ ContentDialog::ContentDialog(SettingsWidget* settingsWidget, QWidget* parent)
friendLayout->setMargin(0); friendLayout->setMargin(0);
friendLayout->setSpacing(0); friendLayout->setSpacing(0);
layouts = {friendLayout->getLayoutOnline(), groupLayout.getLayout(), layouts = {
friendLayout->getLayoutOffline()}; friendLayout->getLayoutOnline(),
groupLayout.getLayout(),
friendLayout->getLayoutOffline()
};
if (s.getGroupchatPosition()) { if (s.getGroupchatPosition()) {
layouts.swap(0, 1); layouts.swap(0, 1);
@ -138,9 +141,8 @@ ContentDialog::ContentDialog(SettingsWidget* settingsWidget, QWidget* parent)
Translator::registerHandler(std::bind(&ContentDialog::retranslateUi, this), this); Translator::registerHandler(std::bind(&ContentDialog::retranslateUi, this), this);
} }
void ContentDialog::removeCurrent(QHash<int, ContactInfo>& infos) void ContentDialog::removeCurrent(QHash<int, ContactInfo>& infos) {
{ for (auto it = infos.begin(); it != infos.end(); ) {
for (auto it = infos.begin(); it != infos.end();) {
if (std::get<0>(*it) == this) { if (std::get<0>(*it) == this) {
it = infos.erase(it); it = infos.erase(it);
} else { } else {
@ -350,9 +352,9 @@ void ContentDialog::cycleContacts(bool forward, bool inverse)
bool isCurOffline = currentLayout == friendLayout->getLayoutOffline(); bool isCurOffline = currentLayout == friendLayout->getLayoutOffline();
bool isCurOnline = currentLayout == friendLayout->getLayoutOnline(); bool isCurOnline = currentLayout == friendLayout->getLayoutOnline();
bool isCurGroup = currentLayout == groupLayout.getLayout(); bool isCurGroup = currentLayout == groupLayout.getLayout();
bool nextIsEmpty = (isCurOnline && offlineEmpty && (groupsEmpty || groupsOnTop)) bool nextIsEmpty = (isCurOnline && offlineEmpty && (groupsEmpty || groupsOnTop)) ||
|| (isCurGroup && offlineEmpty && (onlineEmpty || !groupsOnTop)) (isCurGroup && offlineEmpty && (onlineEmpty || !groupsOnTop)) ||
|| isCurOffline; isCurOffline;
if (nextIsEmpty) { if (nextIsEmpty) {
forward = !forward; forward = !forward;
@ -505,10 +507,12 @@ void ContentDialog::updateTitleAndStatusIcon()
Status currentStatus = activeChatroomWidget->getFriend()->getStatus(); Status currentStatus = activeChatroomWidget->getFriend()->getStatus();
QMap<Status, QIcon> icons{{Status::Online, QIcon(":/img/status/dot_online.svg")}, QMap<Status, QIcon> icons {
{Status::Away, QIcon(":/img/status/dot_away.svg")}, {Status::Online, QIcon(":/img/status/dot_online.svg")},
{Status::Busy, QIcon(":/img/status/dot_busy.svg")}, {Status::Away, QIcon(":/img/status/dot_away.svg")},
{Status::Offline, QIcon(":/img/status/dot_offline.svg")}}; {Status::Busy, QIcon(":/img/status/dot_busy.svg")},
{Status::Offline, QIcon(":/img/status/dot_offline.svg")}
};
setWindowIcon(icons[currentStatus]); setWindowIcon(icons[currentStatus]);
} }
@ -815,7 +819,8 @@ bool ContentDialog::hasWidget(int id, GenericChatroomWidget* chatroomWidget,
return false; return false;
} }
return std::get<0>(*iter) == this && std::get<1>(*iter) == chatroomWidget; return std::get<0>(*iter) == this &&
std::get<1>(*iter) == chatroomWidget;
} }
/** /**

View File

@ -112,7 +112,8 @@ private:
QLayout* nextLayout(QLayout* layout, bool forward) const; QLayout* nextLayout(QLayout* layout, bool forward) const;
int getCurrentLayout(QLayout*& layout); int getCurrentLayout(QLayout*& layout);
bool hasWidget(int id, GenericChatroomWidget* chatroomWidget, const QHash<int, ContactInfo>& list); bool hasWidget(int id, GenericChatroomWidget* chatroomWidget,
const QHash<int, ContactInfo>& list);
void removeCurrent(QHash<int, ContactInfo>& infos); void removeCurrent(QHash<int, ContactInfo>& infos);
static bool existsWidget(int id, const QHash<int, ContactInfo>& list); static bool existsWidget(int id, const QHash<int, ContactInfo>& list);
static void focusDialog(int id, const QHash<int, ContactInfo>& list); static void focusDialog(int id, const QHash<int, ContactInfo>& list);

View File

@ -559,7 +559,7 @@ void ChatForm::onFriendMessageReceived(quint32 friendId, const QString& message,
} }
QDateTime timestamp = QDateTime::currentDateTime(); QDateTime timestamp = QDateTime::currentDateTime();
addMessage(f->getPublicKey(), message, timestamp, isAction, true); addMessage(f->getPublicKey(), message, timestamp, isAction);
} }
void ChatForm::onStatusMessage(const QString& message) void ChatForm::onStatusMessage(const QString& message)
@ -694,8 +694,8 @@ void ChatForm::loadHistory(const QDateTime& since, bool processUndelivered)
QString pk = f->getPublicKey().toString(); QString pk = f->getPublicKey().toString();
QList<History::HistMessage> msgs = history->getChatHistory(pk, since, now); QList<History::HistMessage> msgs = history->getChatHistory(pk, since, now);
ToxPk storedPrevId = previousId; ToxPk prevIdBackup = previousId;
ToxPk prevId; previousId = ToxPk{};
QList<ChatLine::Ptr> historyMessages; QList<ChatLine::Ptr> historyMessages;
@ -733,13 +733,11 @@ void ChatForm::loadHistory(const QDateTime& since, bool processUndelivered)
ChatMessage::MessageType type = isAction ? ChatMessage::ACTION : ChatMessage::NORMAL; ChatMessage::MessageType type = isAction ? ChatMessage::ACTION : ChatMessage::NORMAL;
QDateTime dateTime = needSending ? QDateTime() : msgDateTime; QDateTime dateTime = needSending ? QDateTime() : msgDateTime;
auto msg = ChatMessage::createChatMessage(authorStr, messageText, type, isSelf, dateTime); auto msg = ChatMessage::createChatMessage(authorStr, messageText, type, isSelf, dateTime);
if (!isAction && needsToHideName(authorPk)) {
uint prev = prevMsgDateTime.secsTo(msgDateTime);
if (!isAction && prevId == authorPk && prev < chatWidget->repNameAfter) {
msg->hideSender(); msg->hideSender();
} }
prevId = authorPk; previousId = authorPk;
prevMsgDateTime = msgDateTime; prevMsgDateTime = msgDateTime;
if (needSending && processUndelivered) { if (needSending && processUndelivered) {
@ -754,7 +752,7 @@ void ChatForm::loadHistory(const QDateTime& since, bool processUndelivered)
historyMessages.append(msg); historyMessages.append(msg);
} }
previousId = storedPrevId; previousId = prevIdBackup;
earliestMessage = since; earliestMessage = since;
QScrollBar* verticalBar = chatWidget->verticalScrollBar(); QScrollBar* verticalBar = chatWidget->verticalScrollBar();

View File

@ -55,6 +55,8 @@
* (excluded) * (excluded)
*/ */
#define SET_STYLESHEET(x) (x)->setStyleSheet(Style::getStylesheet(":/ui/"#x"/"#x".css"))
static const QSize AVATAR_SIZE{40, 40}; static const QSize AVATAR_SIZE{40, 40};
static const QSize CALL_BUTTONS_SIZE{50, 40}; static const QSize CALL_BUTTONS_SIZE{50, 40};
static const QSize VOL_MIC_BUTTONS_SIZE{22, 18}; static const QSize VOL_MIC_BUTTONS_SIZE{22, 18};
@ -73,7 +75,7 @@ static const QString FONT_STYLE[]{"normal", "italic", "oblique"};
* @param name Class name * @param name Class name
* @return Style string * @return Style string
*/ */
QString fontToCss(const QFont& font, const QString& name) static QString fontToCss(const QFont& font, const QString& name)
{ {
QString result{"%1{" QString result{"%1{"
"font-family: \"%2\"; " "font-family: \"%2\"; "
@ -89,7 +91,7 @@ QString fontToCss(const QFont& font, const QString& name)
* @param pk Searched public key * @param pk Searched public key
* @return Name or alias of someone with such public key * @return Name or alias of someone with such public key
*/ */
QString resolveToxId(const ToxPk& pk) QString GenericChatForm::resolveToxId(const ToxPk& pk)
{ {
Friend* f = FriendList::findFriend(pk); Friend* f = FriendList::findFriend(pk);
if (f) { if (f) {
@ -173,25 +175,19 @@ GenericChatForm::GenericChatForm(QWidget* parent)
msgEdit->setFixedHeight(MESSAGE_EDIT_HEIGHT); msgEdit->setFixedHeight(MESSAGE_EDIT_HEIGHT);
msgEdit->setFrameStyle(QFrame::NoFrame); msgEdit->setFrameStyle(QFrame::NoFrame);
sendButton->setStyleSheet(Style::getStylesheet(":/ui/sendButton/sendButton.css")); SET_STYLESHEET(sendButton);
fileButton->setStyleSheet(Style::getStylesheet(":/ui/fileButton/fileButton.css")); SET_STYLESHEET(fileButton);
QString screenshotBtnStyle{Style::getStylesheet(":/ui/screenshotButton/screenshotButton.css")}; SET_STYLESHEET(screenshotButton);
screenshotButton->setStyleSheet(screenshotBtnStyle); SET_STYLESHEET(emoteButton);
emoteButton->setStyleSheet(Style::getStylesheet(":/ui/emoteButton/emoteButton.css")); SET_STYLESHEET(callButton);
SET_STYLESHEET(videoButton);
SET_STYLESHEET(volButton);
SET_STYLESHEET(micButton);
callButton->setObjectName("green"); callButton->setObjectName("green");
callButton->setStyleSheet(Style::getStylesheet(":/ui/callButton/callButton.css"));
videoButton->setObjectName("green"); videoButton->setObjectName("green");
videoButton->setStyleSheet(Style::getStylesheet(":/ui/videoButton/videoButton.css"));
QString volButtonStylesheet = Style::getStylesheet(":/ui/volButton/volButton.css");
volButton->setObjectName("grey"); volButton->setObjectName("grey");
volButton->setStyleSheet(volButtonStylesheet);
QString micButtonStylesheet = Style::getStylesheet(":/ui/micButton/micButton.css");
micButton->setObjectName("grey"); micButton->setObjectName("grey");
micButton->setStyleSheet(micButtonStylesheet);
setLayout(mainLayout); setLayout(mainLayout);
@ -378,6 +374,17 @@ void GenericChatForm::onChatContextMenuRequested(QPoint pos)
menu.exec(pos); menu.exec(pos);
} }
/**
* @brief Show, is it needed to repeat message author name or not
* @param messageAuthor Author of the sent message
* @return True if it's needed to repeat name, false otherwise
*/
bool GenericChatForm::needsToHideName(const ToxPk &messageAuthor) const
{
qint64 messagesTimeDiff = prevMsgDateTime.secsTo(QDateTime::currentDateTime());
return messageAuthor == previousId && messagesTimeDiff < chatWidget->repNameAfter;
}
/** /**
* @brief Creates ChatMessage shared object that later will be inserted into ChatLog * @brief Creates ChatMessage shared object that later will be inserted into ChatLog
* @param author Author of the message * @param author Author of the message
@ -405,8 +412,7 @@ ChatMessage::Ptr GenericChatForm::createMessage(const ToxPk& author, const QStri
previousId = ToxPk{}; previousId = ToxPk{};
} else { } else {
msg = ChatMessage::createChatMessage(authorStr, message, ChatMessage::NORMAL, isSelf); msg = ChatMessage::createChatMessage(authorStr, message, ChatMessage::NORMAL, isSelf);
qint64 messagesTimeDiff = prevMsgDateTime.secsTo(QDateTime::currentDateTime()); if (needsToHideName(author)) {
if (author == previousId && messagesTimeDiff < chatWidget->repNameAfter) {
msg->hideSender(); msg->hideSender();
} }
@ -435,18 +441,18 @@ ChatMessage::Ptr GenericChatForm::createSelfMessage(const QString& message, cons
* @brief Inserts message into ChatLog * @brief Inserts message into ChatLog
*/ */
void GenericChatForm::addMessage(const ToxPk& author, const QString& message, const QDateTime& dt, void GenericChatForm::addMessage(const ToxPk& author, const QString& message, const QDateTime& dt,
bool isAction, bool isSent) bool isAction)
{ {
insertChatMessage(createMessage(author, message, dt, isAction, isSent)); insertChatMessage(createMessage(author, message, dt, isAction, true));
} }
/** /**
* @brief Inserts int ChatLog message that you have sent * @brief Inserts int ChatLog message that you have sent
*/ */
void GenericChatForm::addSelfMessage(const QString& message, const QDateTime& datetime, void GenericChatForm::addSelfMessage(const QString& message, const QDateTime& datetime,
bool isAction, bool isSent) bool isAction)
{ {
insertChatMessage(createSelfMessage(message, datetime, isAction, isSent)); insertChatMessage(createSelfMessage(message, datetime, isAction, true));
} }
void GenericChatForm::addAlertMessage(const ToxPk& author, const QString& msg, const QDateTime& dt) void GenericChatForm::addAlertMessage(const ToxPk& author, const QString& msg, const QDateTime& dt)
@ -454,8 +460,7 @@ void GenericChatForm::addAlertMessage(const ToxPk& author, const QString& msg, c
QString authorStr = resolveToxId(author); QString authorStr = resolveToxId(author);
bool isSelf = author == Core::getInstance()->getSelfId().getPublicKey(); bool isSelf = author == Core::getInstance()->getSelfId().getPublicKey();
auto chatMsg = ChatMessage::createChatMessage(authorStr, msg, ChatMessage::ALERT, isSelf, dt); auto chatMsg = ChatMessage::createChatMessage(authorStr, msg, ChatMessage::ALERT, isSelf, dt);
qint64 messagesTimeDiff = prevMsgDateTime.secsTo(QDateTime::currentDateTime()); if (needsToHideName(author)) {
if (author == previousId && messagesTimeDiff < chatWidget->repNameAfter) {
chatMsg->hideSender(); chatMsg->hideSender();
} }

View File

@ -21,7 +21,7 @@
#define GENERICCHATFORM_H #define GENERICCHATFORM_H
#include "src/chatlog/chatmessage.h" #include "src/chatlog/chatmessage.h"
#include "src/core/toxid.h" #include "src/core/toxpk.h"
#include <QMenu> #include <QMenu>
#include <QWidget> #include <QWidget>
@ -32,8 +32,6 @@
* - Even a different font is not enough TODO #1307 ~~zetok * - Even a different font is not enough TODO #1307 ~~zetok
*/ */
QString resolveToxId(const ToxPk& id);
class ChatLog; class ChatLog;
class ChatTextEdit; class ChatTextEdit;
class ContentLayout; class ContentLayout;
@ -67,11 +65,12 @@ public:
virtual void show(ContentLayout* contentLayout); virtual void show(ContentLayout* contentLayout);
void addMessage(const ToxPk& author, const QString& message, const QDateTime& datetime, void addMessage(const ToxPk& author, const QString& message, const QDateTime& datetime,
bool isAction, bool isSent); bool isAction);
void addSelfMessage(const QString& message, const QDateTime& datetime, bool isAction, bool isSent); void addSelfMessage(const QString& message, const QDateTime& datetime, bool isAction);
void addSystemInfoMessage(const QString& message, ChatMessage::SystemMessageType type, void addSystemInfoMessage(const QString& message, ChatMessage::SystemMessageType type,
const QDateTime& datetime); const QDateTime& datetime);
void addAlertMessage(const ToxPk& author, const QString& message, const QDateTime& datetime); void addAlertMessage(const ToxPk& author, const QString& message, const QDateTime& datetime);
static QString resolveToxId(const ToxPk& id);
QDate getLatestDate() const; QDate getLatestDate() const;
signals: signals:
@ -108,6 +107,7 @@ protected:
const QDateTime& datetime, bool isAction, bool isSent); const QDateTime& datetime, bool isAction, bool isSent);
ChatMessage::Ptr createSelfMessage(const QString& message, const QDateTime& datetime, ChatMessage::Ptr createSelfMessage(const QString& message, const QDateTime& datetime,
bool isAction, bool isSent); bool isAction, bool isSent);
bool needsToHideName(const ToxPk& author) const;
void showNetcam(); void showNetcam();
void hideNetcam(); void hideNetcam();
virtual GenericNetCamView* createNetcam() = 0; virtual GenericNetCamView* createNetcam() = 0;

View File

@ -156,9 +156,9 @@ void GroupChatForm::onSendTriggered()
} else { } else {
if (msg.startsWith(ChatForm::ACTION_PREFIX, Qt::CaseInsensitive)) if (msg.startsWith(ChatForm::ACTION_PREFIX, Qt::CaseInsensitive))
addSelfMessage(msg.mid(ChatForm::ACTION_PREFIX.length()), QDateTime::currentDateTime(), addSelfMessage(msg.mid(ChatForm::ACTION_PREFIX.length()), QDateTime::currentDateTime(),
true, true); true);
else else
addSelfMessage(msg, QDateTime::currentDateTime(), false, true); addSelfMessage(msg, QDateTime::currentDateTime(), false);
} }
} }

View File

@ -57,12 +57,16 @@ Time getTime(const QDate& date)
} }
QDate today = QDate::currentDate(); QDate today = QDate::currentDate();
const QMap<Time, QDate> dates{ const QMap<Time, QDate> dates {
{Time::Today, today.addDays(0)}, {Time::Yesterday, today.addDays(-1)}, { Time::Today, today.addDays(0) },
{Time::ThisWeek, today.addDays(-6)}, {Time::ThisMonth, today.addMonths(-1)}, { Time::Yesterday, today.addDays(-1) },
{Time::Month1Ago, today.addMonths(-2)}, {Time::Month2Ago, today.addMonths(-3)}, { Time::ThisWeek, today.addDays(-6) },
{Time::Month3Ago, today.addMonths(-4)}, {Time::Month4Ago, today.addMonths(-5)}, { Time::ThisMonth, today.addMonths(-1) },
{Time::Month5Ago, today.addMonths(-6)}, { Time::Month1Ago, today.addMonths(-2) },
{ Time::Month2Ago, today.addMonths(-3) },
{ Time::Month3Ago, today.addMonths(-4) },
{ Time::Month4Ago, today.addMonths(-5) },
{ Time::Month5Ago, today.addMonths(-6) },
}; };
for (Time time : dates.keys()) { for (Time time : dates.keys()) {
@ -189,18 +193,18 @@ void FriendListWidget::setMode(Mode mode)
QLocale ql(Settings::getInstance().getTranslation()); QLocale ql(Settings::getInstance().getTranslation());
QDate today = QDate::currentDate(); QDate today = QDate::currentDate();
#define COMMENT "Category for sorting friends by activity" #define COMMENT "Category for sorting friends by activity"
const QMap<Time, QString> names{ const QMap<Time, QString> names {
{Time::Today, tr("Today", COMMENT)}, { Time::Today, tr("Today", COMMENT) },
{Time::Yesterday, tr("Yesterday", COMMENT)}, { Time::Yesterday, tr("Yesterday", COMMENT) },
{Time::ThisWeek, tr("Last 7 days", COMMENT)}, { Time::ThisWeek, tr("Last 7 days", COMMENT) },
{Time::ThisMonth, tr("This month", COMMENT)}, { Time::ThisMonth, tr("This month", COMMENT) },
{Time::LongAgo, tr("Older than 6 Months", COMMENT)}, { Time::LongAgo, tr("Older than 6 Months", COMMENT) },
{Time::Never, tr("Never", COMMENT)}, { Time::Never, tr("Never", COMMENT) },
{Time::Month1Ago, ql.monthName(today.addMonths(-1).month())}, { Time::Month1Ago, ql.monthName(today.addMonths(-1).month()) },
{Time::Month2Ago, ql.monthName(today.addMonths(-2).month())}, { Time::Month2Ago, ql.monthName(today.addMonths(-2).month()) },
{Time::Month3Ago, ql.monthName(today.addMonths(-3).month())}, { Time::Month3Ago, ql.monthName(today.addMonths(-3).month()) },
{Time::Month4Ago, ql.monthName(today.addMonths(-4).month())}, { Time::Month4Ago, ql.monthName(today.addMonths(-4).month()) },
{Time::Month5Ago, ql.monthName(today.addMonths(-5).month())}, { Time::Month5Ago, ql.monthName(today.addMonths(-5).month()) },
}; };
#undef COMMENT #undef COMMENT

View File

@ -1601,7 +1601,7 @@ void Widget::onGroupMessageReceived(int groupnumber, int peernumber, const QStri
if (targeted && !isAction) { if (targeted && !isAction) {
g->getChatForm()->addAlertMessage(author, message, QDateTime::currentDateTime()); g->getChatForm()->addAlertMessage(author, message, QDateTime::currentDateTime());
} else { } else {
g->getChatForm()->addMessage(author, message, QDateTime::currentDateTime(), isAction, true); g->getChatForm()->addMessage(author, message, QDateTime::currentDateTime(), isAction);
} }
newGroupMessageAlert(g->getGroupId(), targeted || Settings::getInstance().getGroupAlwaysNotify()); newGroupMessageAlert(g->getGroupId(), targeted || Settings::getInstance().getGroupAlwaysNotify());