summaryrefslogtreecommitdiffstats
path: root/examples/dbus/chat
diff options
context:
space:
mode:
Diffstat (limited to 'examples/dbus/chat')
-rw-r--r--examples/dbus/chat/CMakeLists.txt37
-rw-r--r--examples/dbus/chat/chat.cpp120
-rw-r--r--examples/dbus/chat/chat.h20
-rw-r--r--examples/dbus/chat/chat.pro2
-rw-r--r--examples/dbus/chat/chatsetnickname.ui149
5 files changed, 62 insertions, 266 deletions
diff --git a/examples/dbus/chat/CMakeLists.txt b/examples/dbus/chat/CMakeLists.txt
index e7245fd7d6..472d893dee 100644
--- a/examples/dbus/chat/CMakeLists.txt
+++ b/examples/dbus/chat/CMakeLists.txt
@@ -1,22 +1,15 @@
# Copyright (C) 2022 The Qt Company Ltd.
-# SPDX-License-Identifier: BSD-3-Clause
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
cmake_minimum_required(VERSION 3.16)
project(chat LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
-set(CMAKE_AUTOMOC ON)
-set(CMAKE_AUTOUIC ON)
-
-if(NOT DEFINED INSTALL_EXAMPLESDIR)
- set(INSTALL_EXAMPLESDIR "examples")
-endif()
-
-set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/dbus/chat")
-
find_package(Qt6 REQUIRED COMPONENTS Core DBus Gui Widgets)
+qt_standard_project_setup()
+
set(chat_SRCS)
qt_add_dbus_interface(chat_SRCS
org.example.chat.xml
@@ -33,7 +26,6 @@ qt_add_dbus_adaptor(chat_SRCS
qt_add_executable(chat
chat.cpp chat.h
chatmainwindow.ui
- chatsetnickname.ui
${chat_SRCS}
)
@@ -42,15 +34,22 @@ set_target_properties(chat PROPERTIES
MACOSX_BUNDLE TRUE
)
-target_link_libraries(chat PUBLIC
- Qt::Core
- Qt::DBus
- Qt::Gui
- Qt::Widgets
+target_link_libraries(chat PRIVATE
+ Qt6::Core
+ Qt6::DBus
+ Qt6::Gui
+ Qt6::Widgets
)
install(TARGETS chat
- RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
- BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
- LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
+ BUNDLE DESTINATION .
+ RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+)
+
+qt_generate_deploy_app_script(
+ TARGET chat
+ OUTPUT_SCRIPT deploy_script
+ NO_UNSUPPORTED_PLATFORM_ERROR
)
+install(SCRIPT ${deploy_script})
diff --git a/examples/dbus/chat/chat.cpp b/examples/dbus/chat/chat.cpp
index 6e7c670834..cced911955 100644
--- a/examples/dbus/chat/chat.cpp
+++ b/examples/dbus/chat/chat.cpp
@@ -2,6 +2,7 @@
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include <QApplication>
+#include <QInputDialog>
#include <QMessageBox>
#include "chat.h"
@@ -9,110 +10,69 @@
#include "chat_interface.h"
ChatMainWindow::ChatMainWindow()
- : m_nickname(QLatin1String("nickname"))
{
setupUi(this);
- sendButton->setEnabled(false);
- connect(messageLineEdit, &QLineEdit::textChanged,
- this, &ChatMainWindow::textChangedSlot);
- connect(sendButton, &QPushButton::clicked,
- this, &ChatMainWindow::sendClickedSlot);
+ connect(messageLineEdit, &QLineEdit::textChanged, this,
+ [this](const QString &newText) { sendButton->setEnabled(!newText.isEmpty()); });
+ connect(sendButton, &QPushButton::clicked, this, [this]() {
+ emit message(m_nickname, messageLineEdit->text());
+ messageLineEdit->clear();
+ });
connect(actionChangeNickname, &QAction::triggered,
this, &ChatMainWindow::changeNickname);
- connect(actionAboutQt, &QAction::triggered,
- this, &ChatMainWindow::aboutQt);
- connect(qApp, &QApplication::lastWindowClosed,
- this, &ChatMainWindow::exiting);
+ connect(actionAboutQt, &QAction::triggered, this, [this]() { QMessageBox::aboutQt(this); });
+ connect(qApp, &QApplication::lastWindowClosed, this,
+ [this]() { emit action(m_nickname, tr("leaves the chat")); });
// add our D-Bus interface and connect to D-Bus
new ChatAdaptor(this);
- QDBusConnection::sessionBus().registerObject("/", this);
-
- org::example::chat *iface;
- iface = new org::example::chat(QString(), QString(), QDBusConnection::sessionBus(), this);
- //connect(iface, SIGNAL(message(QString,QString)), this, SLOT(messageSlot(QString,QString)));
- QDBusConnection::sessionBus().connect(QString(), QString(), "org.example.chat", "message", this, SLOT(messageSlot(QString,QString)));
- connect(iface, &org::example::chat::action,
- this, &ChatMainWindow::actionSlot);
-
- NicknameDialog dialog;
- dialog.cancelButton->setVisible(false);
- dialog.exec();
- m_nickname = dialog.nickname->text().trimmed();
- emit action(m_nickname, QLatin1String("joins the chat"));
-}
-ChatMainWindow::~ChatMainWindow()
-{
-}
+ auto connection = QDBusConnection::sessionBus();
+ connection.registerObject("/", this);
-void ChatMainWindow::rebuildHistory()
-{
- QString history = m_messages.join( QLatin1String("\n" ) );
- chatHistory->setPlainText(history);
-}
+ using org::example::chat;
-void ChatMainWindow::messageSlot(const QString &nickname, const QString &text)
-{
- QString msg( QLatin1String("<%1> %2") );
- msg = msg.arg(nickname, text);
- m_messages.append(msg);
+ 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 (m_messages.count() > 100)
- m_messages.removeFirst();
- rebuildHistory();
+ if (!changeNickname(true))
+ QMetaObject::invokeMethod(qApp, &QApplication::quit, Qt::QueuedConnection);
}
-void ChatMainWindow::actionSlot(const QString &nickname, const QString &text)
+void ChatMainWindow::displayMessage(const QString &message)
{
- QString msg( QLatin1String("* %1 %2") );
- msg = msg.arg(nickname, text);
- m_messages.append(msg);
+ m_messages.append(message);
if (m_messages.count() > 100)
m_messages.removeFirst();
- rebuildHistory();
-}
-
-void ChatMainWindow::textChangedSlot(const QString &newText)
-{
- sendButton->setEnabled(!newText.isEmpty());
-}
-void ChatMainWindow::sendClickedSlot()
-{
- //emit message(m_nickname, messageLineEdit->text());
- QDBusMessage msg = QDBusMessage::createSignal("/", "org.example.chat", "message");
- msg << m_nickname << messageLineEdit->text();
- QDBusConnection::sessionBus().send(msg);
- messageLineEdit->setText(QString());
+ auto history = m_messages.join(QLatin1String("\n"));
+ chatHistory->setPlainText(history);
}
-void ChatMainWindow::changeNickname()
+bool ChatMainWindow::changeNickname(bool initial)
{
- NicknameDialog dialog(this);
- if (dialog.exec() == QDialog::Accepted) {
- QString old = m_nickname;
- m_nickname = dialog.nickname->text().trimmed();
- emit action(old, QString("is now known as %1").arg(m_nickname));
+ auto newNickname = QInputDialog::getText(this, tr("Set nickname"), tr("New nickname:"));
+ newNickname = newNickname.trimmed();
+
+ if (!newNickname.isEmpty()) {
+ auto old = m_nickname;
+ m_nickname = newNickname;
+
+ if (initial)
+ emit action(m_nickname, tr("joins the chat"));
+ else
+ emit action(old, tr("is now known as %1").arg(m_nickname));
+ return true;
}
-}
-
-void ChatMainWindow::aboutQt()
-{
- QMessageBox::aboutQt(this);
-}
-
-void ChatMainWindow::exiting()
-{
- emit action(m_nickname, QLatin1String("leaves the chat"));
-}
-NicknameDialog::NicknameDialog(QWidget *parent)
- : QDialog(parent)
-{
- setupUi(this);
+ return false;
}
int main(int argc, char **argv)
diff --git a/examples/dbus/chat/chat.h b/examples/dbus/chat/chat.h
index aabafcdc95..f7706eeeba 100644
--- a/examples/dbus/chat/chat.h
+++ b/examples/dbus/chat/chat.h
@@ -7,7 +7,6 @@
#include <QStringList>
#include "ui_chatmainwindow.h"
-#include "ui_chatsetnickname.h"
class ChatMainWindow: public QMainWindow, Ui::ChatMainWindow
{
@@ -16,29 +15,16 @@ class ChatMainWindow: public QMainWindow, Ui::ChatMainWindow
QStringList m_messages;
public:
ChatMainWindow();
- ~ChatMainWindow();
- void rebuildHistory();
+private:
+ void displayMessage(const QString &message);
signals:
void message(const QString &nickname, const QString &text);
void action(const QString &nickname, const QString &text);
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();
- void changeNickname();
- void aboutQt();
- void exiting();
-};
-
-class NicknameDialog: public QDialog, public Ui::NicknameDialog
-{
- Q_OBJECT
-public:
- NicknameDialog(QWidget *parent = nullptr);
+ bool changeNickname(bool initial = false);
};
#endif // CHAT_H
diff --git a/examples/dbus/chat/chat.pro b/examples/dbus/chat/chat.pro
index 93d3178177..2b4630d5f1 100644
--- a/examples/dbus/chat/chat.pro
+++ b/examples/dbus/chat/chat.pro
@@ -2,7 +2,7 @@ QT += dbus widgets
HEADERS += chat.h
SOURCES += chat.cpp
-FORMS += chatmainwindow.ui chatsetnickname.ui
+FORMS += chatmainwindow.ui
DBUS_ADAPTORS += org.example.chat.xml
DBUS_INTERFACES += org.example.chat.xml
diff --git a/examples/dbus/chat/chatsetnickname.ui b/examples/dbus/chat/chatsetnickname.ui
deleted file mode 100644
index fb9894e09f..0000000000
--- a/examples/dbus/chat/chatsetnickname.ui
+++ /dev/null
@@ -1,149 +0,0 @@
-<ui version="4.0" >
- <author></author>
- <comment></comment>
- <exportmacro></exportmacro>
- <class>NicknameDialog</class>
- <widget class="QDialog" name="NicknameDialog" >
- <property name="geometry" >
- <rect>
- <x>0</x>
- <y>0</y>
- <width>396</width>
- <height>105</height>
- </rect>
- </property>
- <property name="sizePolicy" >
- <sizepolicy>
- <hsizetype>1</hsizetype>
- <vsizetype>1</vsizetype>
- <horstretch>0</horstretch>
- <verstretch>0</verstretch>
- </sizepolicy>
- </property>
- <property name="windowTitle" >
- <string>Set nickname</string>
- </property>
- <layout class="QVBoxLayout" >
- <property name="margin" >
- <number>9</number>
- </property>
- <property name="spacing" >
- <number>6</number>
- </property>
- <item>
- <layout class="QVBoxLayout" >
- <property name="margin" >
- <number>0</number>
- </property>
- <property name="spacing" >
- <number>6</number>
- </property>
- <item>
- <widget class="QLabel" name="label" >
- <property name="sizePolicy" >
- <sizepolicy>
- <hsizetype>1</hsizetype>
- <vsizetype>1</vsizetype>
- <horstretch>0</horstretch>
- <verstretch>0</verstretch>
- </sizepolicy>
- </property>
- <property name="text" >
- <string>New nickname:</string>
- </property>
- </widget>
- </item>
- <item>
- <widget class="QLineEdit" name="nickname" />
- </item>
- </layout>
- </item>
- <item>
- <layout class="QHBoxLayout" >
- <property name="margin" >
- <number>0</number>
- </property>
- <property name="spacing" >
- <number>6</number>
- </property>
- <item>
- <spacer>
- <property name="orientation" >
- <enum>Qt::Horizontal</enum>
- </property>
- <property name="sizeHint" >
- <size>
- <width>131</width>
- <height>31</height>
- </size>
- </property>
- </spacer>
- </item>
- <item>
- <widget class="QPushButton" name="okButton" >
- <property name="text" >
- <string>OK</string>
- </property>
- </widget>
- </item>
- <item>
- <widget class="QPushButton" name="cancelButton" >
- <property name="text" >
- <string>Cancel</string>
- </property>
- </widget>
- </item>
- <item>
- <spacer>
- <property name="orientation" >
- <enum>Qt::Horizontal</enum>
- </property>
- <property name="sizeHint" >
- <size>
- <width>40</width>
- <height>20</height>
- </size>
- </property>
- </spacer>
- </item>
- </layout>
- </item>
- </layout>
- </widget>
- <pixmapfunction></pixmapfunction>
- <resources/>
- <connections>
- <connection>
- <sender>okButton</sender>
- <signal>clicked()</signal>
- <receiver>NicknameDialog</receiver>
- <slot>accept()</slot>
- <hints>
- <hint type="sourcelabel" >
- <x>278</x>
- <y>253</y>
- </hint>
- <hint type="destinationlabel" >
- <x>96</x>
- <y>254</y>
- </hint>
- </hints>
- </connection>
- <connection>
- <sender>cancelButton</sender>
- <signal>clicked()</signal>
- <receiver>NicknameDialog</receiver>
- <slot>reject()</slot>
- <hints>
- <hint type="sourcelabel" >
- <x>369</x>
- <y>253</y>
- </hint>
- <hint type="destinationlabel" >
- <x>179</x>
- <y>282</y>
- </hint>
- </hints>
- </connection>
- </connections>
-</ui>