chat example: Update code style
Use lambdas instead of slots where practical. Consistently use signals to transmit D-Bus messages. Extract a local variable for the used D-Bus connection. Task-number: QTBUG-111366 Change-Id: Icc6667e1392ada1b7d3b33c4e4b32917dd648390 Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io> (cherry picked from commit 742e79312fc98711f68749937d0db433d961f546) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
47b372ae8c
commit
aa5b3da439
@ -12,74 +12,50 @@
|
|||||||
ChatMainWindow::ChatMainWindow()
|
ChatMainWindow::ChatMainWindow()
|
||||||
{
|
{
|
||||||
setupUi(this);
|
setupUi(this);
|
||||||
sendButton->setEnabled(false);
|
|
||||||
|
|
||||||
connect(messageLineEdit, &QLineEdit::textChanged,
|
connect(messageLineEdit, &QLineEdit::textChanged, this,
|
||||||
this, &ChatMainWindow::textChangedSlot);
|
[this](const QString &newText) { sendButton->setEnabled(!newText.isEmpty()); });
|
||||||
connect(sendButton, &QPushButton::clicked,
|
connect(sendButton, &QPushButton::clicked, this, [this]() {
|
||||||
this, &ChatMainWindow::sendClickedSlot);
|
emit message(m_nickname, messageLineEdit->text());
|
||||||
|
messageLineEdit->clear();
|
||||||
|
});
|
||||||
connect(actionChangeNickname, &QAction::triggered,
|
connect(actionChangeNickname, &QAction::triggered,
|
||||||
this, &ChatMainWindow::changeNickname);
|
this, &ChatMainWindow::changeNickname);
|
||||||
connect(actionAboutQt, &QAction::triggered,
|
connect(actionAboutQt, &QAction::triggered, this, [this]() { QMessageBox::aboutQt(this); });
|
||||||
this, &ChatMainWindow::aboutQt);
|
connect(qApp, &QApplication::lastWindowClosed, this,
|
||||||
connect(qApp, &QApplication::lastWindowClosed,
|
[this]() { emit action(m_nickname, tr("leaves the chat")); });
|
||||||
this, &ChatMainWindow::exiting);
|
|
||||||
|
|
||||||
// add our D-Bus interface and connect to D-Bus
|
// add our D-Bus interface and connect to D-Bus
|
||||||
new ChatAdaptor(this);
|
new ChatAdaptor(this);
|
||||||
QDBusConnection::sessionBus().registerObject("/", this);
|
|
||||||
|
|
||||||
org::example::chat *iface;
|
auto connection = QDBusConnection::sessionBus();
|
||||||
iface = new org::example::chat(QString(), QString(), QDBusConnection::sessionBus(), this);
|
connection.registerObject("/", this);
|
||||||
QDBusConnection::sessionBus().connect(QString(), QString(), "org.example.chat", "message", this, SLOT(messageSlot(QString,QString)));
|
|
||||||
connect(iface, &org::example::chat::action,
|
using org::example::chat;
|
||||||
this, &ChatMainWindow::actionSlot);
|
|
||||||
|
auto *iface = new chat({}, {}, connection, this);
|
||||||
|
connect(iface, &chat::message, this, [this](const QString &nickname, const QString &text) {
|
||||||
|
displayMessage(tr("<%1> %2").arg(nickname, text));
|
||||||
|
});
|
||||||
|
connect(iface, &chat::action, this, [this](const QString &nickname, const QString &text) {
|
||||||
|
displayMessage(tr("* %1 %2").arg(nickname, text));
|
||||||
|
});
|
||||||
|
|
||||||
if (!changeNickname(true))
|
if (!changeNickname(true))
|
||||||
QMetaObject::invokeMethod(qApp, &QApplication::quit, Qt::QueuedConnection);
|
QMetaObject::invokeMethod(qApp, &QApplication::quit, Qt::QueuedConnection);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ChatMainWindow::rebuildHistory()
|
void ChatMainWindow::displayMessage(const QString &message)
|
||||||
{
|
{
|
||||||
QString history = m_messages.join( QLatin1String("\n" ) );
|
m_messages.append(message);
|
||||||
|
|
||||||
|
if (m_messages.count() > 100)
|
||||||
|
m_messages.removeFirst();
|
||||||
|
|
||||||
|
auto history = m_messages.join(QLatin1String("\n"));
|
||||||
chatHistory->setPlainText(history);
|
chatHistory->setPlainText(history);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ChatMainWindow::messageSlot(const QString &nickname, const QString &text)
|
|
||||||
{
|
|
||||||
QString msg( QLatin1String("<%1> %2") );
|
|
||||||
msg = msg.arg(nickname, text);
|
|
||||||
m_messages.append(msg);
|
|
||||||
|
|
||||||
if (m_messages.count() > 100)
|
|
||||||
m_messages.removeFirst();
|
|
||||||
rebuildHistory();
|
|
||||||
}
|
|
||||||
|
|
||||||
void ChatMainWindow::actionSlot(const QString &nickname, const QString &text)
|
|
||||||
{
|
|
||||||
QString msg( QLatin1String("* %1 %2") );
|
|
||||||
msg = msg.arg(nickname, text);
|
|
||||||
m_messages.append(msg);
|
|
||||||
|
|
||||||
if (m_messages.count() > 100)
|
|
||||||
m_messages.removeFirst();
|
|
||||||
rebuildHistory();
|
|
||||||
}
|
|
||||||
|
|
||||||
void ChatMainWindow::textChangedSlot(const QString &newText)
|
|
||||||
{
|
|
||||||
sendButton->setEnabled(!newText.isEmpty());
|
|
||||||
}
|
|
||||||
|
|
||||||
void ChatMainWindow::sendClickedSlot()
|
|
||||||
{
|
|
||||||
QDBusMessage msg = QDBusMessage::createSignal("/", "org.example.chat", "message");
|
|
||||||
msg << m_nickname << messageLineEdit->text();
|
|
||||||
QDBusConnection::sessionBus().send(msg);
|
|
||||||
messageLineEdit->setText(QString());
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ChatMainWindow::changeNickname(bool initial)
|
bool ChatMainWindow::changeNickname(bool initial)
|
||||||
{
|
{
|
||||||
auto newNickname = QInputDialog::getText(this, tr("Set nickname"), tr("New nickname:"));
|
auto newNickname = QInputDialog::getText(this, tr("Set nickname"), tr("New nickname:"));
|
||||||
@ -99,16 +75,6 @@ bool ChatMainWindow::changeNickname(bool initial)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ChatMainWindow::aboutQt()
|
|
||||||
{
|
|
||||||
QMessageBox::aboutQt(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ChatMainWindow::exiting()
|
|
||||||
{
|
|
||||||
emit action(m_nickname, QLatin1String("leaves the chat"));
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
QApplication app(argc, argv);
|
QApplication app(argc, argv);
|
||||||
|
@ -16,20 +16,15 @@ class ChatMainWindow: public QMainWindow, Ui::ChatMainWindow
|
|||||||
public:
|
public:
|
||||||
ChatMainWindow();
|
ChatMainWindow();
|
||||||
|
|
||||||
void rebuildHistory();
|
private:
|
||||||
|
void displayMessage(const QString &message);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void message(const QString &nickname, const QString &text);
|
void message(const QString &nickname, const QString &text);
|
||||||
void action(const QString &nickname, const QString &text);
|
void action(const QString &nickname, const QString &text);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void messageSlot(const QString &nickname, const QString &text);
|
|
||||||
void actionSlot(const QString &nickname, const QString &text);
|
|
||||||
void textChangedSlot(const QString &newText);
|
|
||||||
void sendClickedSlot();
|
|
||||||
bool changeNickname(bool initial = false);
|
bool changeNickname(bool initial = false);
|
||||||
void aboutQt();
|
|
||||||
void exiting();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // CHAT_H
|
#endif // CHAT_H
|
||||||
|
Loading…
x
Reference in New Issue
Block a user