diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2023-11-13 17:57:39 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2023-11-27 10:51:01 +0000 |
commit | ca644f8de0bfc6092daf04265546f4d607515097 (patch) | |
tree | 65fb34c6bb2b6a5df772faeebf209f0ef9d75f16 | |
parent | 4112e9da815e782135bf6d7965e105fe3768cb11 (diff) |
Bluetooth Chat example: update UI
* Add some icons for Light and Dark themes. Implement switching between
themes by listening to QStyleHints::colorSchemeChanged() signal and
manually picking the proper icon.
* Rework the desing to get rid of the unnecessary "Quit" button, and
also change the layout of buttons in the connection dialog.
* Update the screenshot in the documentation.
As a drive-by fix, make sure that the cursor is always at the end of
QTextEdit before showing a new message. The user can change the cursor
position by clicking/selecting the text, and this could previously lead
to some bugs in message visualization.
Fixes: QTBUG-118875
Pick-to: 6.5
Change-Id: Ia3a9dec3dae0d1411643ed851eba0ab4dc96a47f
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
(cherry picked from commit 6eb66322f0933de004baf35a96c5dd06d5d62ff6)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
27 files changed, 160 insertions, 25 deletions
diff --git a/examples/bluetooth/btchat/CMakeLists.txt b/examples/bluetooth/btchat/CMakeLists.txt index 6d2bd0b9..0e4e217c 100644 --- a/examples/bluetooth/btchat/CMakeLists.txt +++ b/examples/bluetooth/btchat/CMakeLists.txt @@ -22,6 +22,18 @@ qt_add_executable(btchat remoteselector.cpp remoteselector.h remoteselector.ui ) +set(icon_files) +foreach(icon IN ITEMS bluetooth bluetooth_dark send send_dark) + foreach(scale IN ITEMS "" "@2" "@3" "@4") + list(APPEND icon_files "icons/btchat/24x24${scale}/${icon}.png") + endforeach() +endforeach() + +qt_add_resources(btchat "theme" FILES + ${icon_files} + icons/btchat/index.theme +) + set_target_properties(btchat PROPERTIES WIN32_EXECUTABLE TRUE MACOSX_BUNDLE TRUE diff --git a/examples/bluetooth/btchat/btchat.pro b/examples/bluetooth/btchat/btchat.pro index 7e0c976f..0d7b5f77 100644 --- a/examples/bluetooth/btchat/btchat.pro +++ b/examples/bluetooth/btchat/btchat.pro @@ -21,6 +21,29 @@ FORMS = \ chat.ui \ remoteselector.ui +theme_resources.files = \ + icons/btchat/24x24/bluetooth.png \ + icons/btchat/24x24/bluetooth_dark.png \ + icons/btchat/24x24/send.png \ + icons/btchat/24x24/send_dark.png \ + icons/btchat/24x24@2/bluetooth.png \ + icons/btchat/24x24@2/bluetooth_dark.png \ + icons/btchat/24x24@2/send.png \ + icons/btchat/24x24@2/send_dark.png \ + icons/btchat/24x24@3/bluetooth.png \ + icons/btchat/24x24@3/bluetooth_dark.png \ + icons/btchat/24x24@3/send.png \ + icons/btchat/24x24@3/send_dark.png \ + icons/btchat/24x24@4/bluetooth.png \ + icons/btchat/24x24@4/bluetooth_dark.png \ + icons/btchat/24x24@4/send.png \ + icons/btchat/24x24@4/send_dark.png \ + icons/btchat/index.theme + +theme_resources.prefix = / + +RESOURCES += theme_resources + ios: QMAKE_INFO_PLIST = ../shared/Info.qmake.ios.plist macos: QMAKE_INFO_PLIST = ../shared/Info.qmake.macos.plist diff --git a/examples/bluetooth/btchat/chat.cpp b/examples/bluetooth/btchat/chat.cpp index 288f4641..b323b2a3 100644 --- a/examples/bluetooth/btchat/chat.cpp +++ b/examples/bluetooth/btchat/chat.cpp @@ -13,6 +13,9 @@ #include <QtBluetooth/qbluetoothlocaldevice.h> #include <QtBluetooth/qbluetoothuuid.h> +#include <QtGui/qguiapplication.h> +#include <QtGui/qstylehints.h> + #if QT_CONFIG(permissions) #include <QtCore/qcoreapplication.h> #include <QtCore/qpermissions.h> @@ -32,12 +35,15 @@ Chat::Chat(QWidget *parent) //! [Construct UI] ui->setupUi(this); - connect(ui->quitButton, &QPushButton::clicked, this, &Chat::accept); connect(ui->connectButton, &QPushButton::clicked, this, &Chat::connectClicked); connect(ui->sendButton, &QPushButton::clicked, this, &Chat::sendClicked); //! [Construct UI] ui->connectButton->setFocus(); + QStyleHints *styleHints = qGuiApp->styleHints(); + updateIcons(styleHints->colorScheme()); + connect(styleHints, &QStyleHints::colorSchemeChanged, this, &Chat::updateIcons); + initBluetooth(); } @@ -106,6 +112,15 @@ void Chat::initBluetooth() //! [Get local device name] } +void Chat::updateIcons(Qt::ColorScheme scheme) +{ + const QString bluetoothIconName = (scheme == Qt::ColorScheme::Dark) ? u"bluetooth_dark"_s + : u"bluetooth"_s; + const QString sendIconName = (scheme == Qt::ColorScheme::Dark) ? u"send_dark"_s : u"send"_s; + ui->sendButton->setIcon(QIcon::fromTheme(sendIconName)); + ui->connectButton->setIcon(QIcon::fromTheme(bluetoothIconName)); +} + //! [clientConnected clientDisconnected] void Chat::clientConnected(const QString &name) { @@ -237,6 +252,7 @@ void Chat::sendClicked() //! [showMessage] void Chat::showMessage(const QString &sender, const QString &message) { + ui->chat->moveCursor(QTextCursor::End); ui->chat->insertPlainText(QString::fromLatin1("%1: %2\n").arg(sender, message)); ui->chat->ensureCursorVisible(); } diff --git a/examples/bluetooth/btchat/chat.h b/examples/bluetooth/btchat/chat.h index d9a22ca7..6230e639 100644 --- a/examples/bluetooth/btchat/chat.h +++ b/examples/bluetooth/btchat/chat.h @@ -42,6 +42,8 @@ private slots: void initBluetooth(); + void updateIcons(Qt::ColorScheme scheme); + private: int adapterFromUserSelection() const; int currentAdapterIndex = 0; diff --git a/examples/bluetooth/btchat/chat.ui b/examples/bluetooth/btchat/chat.ui index 639626f1..bea3d2ee 100644 --- a/examples/bluetooth/btchat/chat.ui +++ b/examples/bluetooth/btchat/chat.ui @@ -50,12 +50,16 @@ <item> <layout class="QHBoxLayout" name="horizontalLayout"> <item> - <widget class="QLineEdit" name="sendText"/> + <widget class="QLineEdit" name="sendText"> + <property name="placeholderText"> + <string>Enter message</string> + </property> + </widget> </item> <item> <widget class="QPushButton" name="sendButton"> <property name="text"> - <string>Send</string> + <string/> </property> <property name="default"> <bool>true</bool> @@ -89,15 +93,21 @@ </property> </widget> </item> + <item> + <spacer name="horizontalSpacer_2"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> </layout> </item> - <item> - <widget class="QPushButton" name="quitButton"> - <property name="text"> - <string>Quit</string> - </property> - </widget> - </item> </layout> </widget> <resources/> diff --git a/examples/bluetooth/btchat/doc/images/btchat-example.png b/examples/bluetooth/btchat/doc/images/btchat-example.png Binary files differindex 2da48731..acfadf5c 100644 --- a/examples/bluetooth/btchat/doc/images/btchat-example.png +++ b/examples/bluetooth/btchat/doc/images/btchat-example.png diff --git a/examples/bluetooth/btchat/icons/btchat/24x24/bluetooth.png b/examples/bluetooth/btchat/icons/btchat/24x24/bluetooth.png Binary files differnew file mode 100644 index 00000000..832dd042 --- /dev/null +++ b/examples/bluetooth/btchat/icons/btchat/24x24/bluetooth.png diff --git a/examples/bluetooth/btchat/icons/btchat/24x24/bluetooth_dark.png b/examples/bluetooth/btchat/icons/btchat/24x24/bluetooth_dark.png Binary files differnew file mode 100644 index 00000000..48b772d0 --- /dev/null +++ b/examples/bluetooth/btchat/icons/btchat/24x24/bluetooth_dark.png diff --git a/examples/bluetooth/btchat/icons/btchat/24x24/send.png b/examples/bluetooth/btchat/icons/btchat/24x24/send.png Binary files differnew file mode 100644 index 00000000..dfce248a --- /dev/null +++ b/examples/bluetooth/btchat/icons/btchat/24x24/send.png diff --git a/examples/bluetooth/btchat/icons/btchat/24x24/send_dark.png b/examples/bluetooth/btchat/icons/btchat/24x24/send_dark.png Binary files differnew file mode 100644 index 00000000..66e78daf --- /dev/null +++ b/examples/bluetooth/btchat/icons/btchat/24x24/send_dark.png diff --git a/examples/bluetooth/btchat/icons/btchat/24x24@2/bluetooth.png b/examples/bluetooth/btchat/icons/btchat/24x24@2/bluetooth.png Binary files differnew file mode 100644 index 00000000..12bbb81d --- /dev/null +++ b/examples/bluetooth/btchat/icons/btchat/24x24@2/bluetooth.png diff --git a/examples/bluetooth/btchat/icons/btchat/24x24@2/bluetooth_dark.png b/examples/bluetooth/btchat/icons/btchat/24x24@2/bluetooth_dark.png Binary files differnew file mode 100644 index 00000000..f3456e6f --- /dev/null +++ b/examples/bluetooth/btchat/icons/btchat/24x24@2/bluetooth_dark.png diff --git a/examples/bluetooth/btchat/icons/btchat/24x24@2/send.png b/examples/bluetooth/btchat/icons/btchat/24x24@2/send.png Binary files differnew file mode 100644 index 00000000..8c4d5d0e --- /dev/null +++ b/examples/bluetooth/btchat/icons/btchat/24x24@2/send.png diff --git a/examples/bluetooth/btchat/icons/btchat/24x24@2/send_dark.png b/examples/bluetooth/btchat/icons/btchat/24x24@2/send_dark.png Binary files differnew file mode 100644 index 00000000..03b597c3 --- /dev/null +++ b/examples/bluetooth/btchat/icons/btchat/24x24@2/send_dark.png diff --git a/examples/bluetooth/btchat/icons/btchat/24x24@3/bluetooth.png b/examples/bluetooth/btchat/icons/btchat/24x24@3/bluetooth.png Binary files differnew file mode 100644 index 00000000..235b91aa --- /dev/null +++ b/examples/bluetooth/btchat/icons/btchat/24x24@3/bluetooth.png diff --git a/examples/bluetooth/btchat/icons/btchat/24x24@3/bluetooth_dark.png b/examples/bluetooth/btchat/icons/btchat/24x24@3/bluetooth_dark.png Binary files differnew file mode 100644 index 00000000..c64aa4a9 --- /dev/null +++ b/examples/bluetooth/btchat/icons/btchat/24x24@3/bluetooth_dark.png diff --git a/examples/bluetooth/btchat/icons/btchat/24x24@3/send.png b/examples/bluetooth/btchat/icons/btchat/24x24@3/send.png Binary files differnew file mode 100644 index 00000000..3d15b197 --- /dev/null +++ b/examples/bluetooth/btchat/icons/btchat/24x24@3/send.png diff --git a/examples/bluetooth/btchat/icons/btchat/24x24@3/send_dark.png b/examples/bluetooth/btchat/icons/btchat/24x24@3/send_dark.png Binary files differnew file mode 100644 index 00000000..7f1a9f4d --- /dev/null +++ b/examples/bluetooth/btchat/icons/btchat/24x24@3/send_dark.png diff --git a/examples/bluetooth/btchat/icons/btchat/24x24@4/bluetooth.png b/examples/bluetooth/btchat/icons/btchat/24x24@4/bluetooth.png Binary files differnew file mode 100644 index 00000000..bad3e2ea --- /dev/null +++ b/examples/bluetooth/btchat/icons/btchat/24x24@4/bluetooth.png diff --git a/examples/bluetooth/btchat/icons/btchat/24x24@4/bluetooth_dark.png b/examples/bluetooth/btchat/icons/btchat/24x24@4/bluetooth_dark.png Binary files differnew file mode 100644 index 00000000..01f420b0 --- /dev/null +++ b/examples/bluetooth/btchat/icons/btchat/24x24@4/bluetooth_dark.png diff --git a/examples/bluetooth/btchat/icons/btchat/24x24@4/send.png b/examples/bluetooth/btchat/icons/btchat/24x24@4/send.png Binary files differnew file mode 100644 index 00000000..c931f7a0 --- /dev/null +++ b/examples/bluetooth/btchat/icons/btchat/24x24@4/send.png diff --git a/examples/bluetooth/btchat/icons/btchat/24x24@4/send_dark.png b/examples/bluetooth/btchat/icons/btchat/24x24@4/send_dark.png Binary files differnew file mode 100644 index 00000000..8b801207 --- /dev/null +++ b/examples/bluetooth/btchat/icons/btchat/24x24@4/send_dark.png diff --git a/examples/bluetooth/btchat/icons/btchat/index.theme b/examples/bluetooth/btchat/icons/btchat/index.theme new file mode 100644 index 00000000..3209f84f --- /dev/null +++ b/examples/bluetooth/btchat/icons/btchat/index.theme @@ -0,0 +1,22 @@ +[Icon Theme] +Name=btchat +Directories=24x24,24x24@2,24x24@3,24x24@4 + +[24x24] +Size=24 +Type=Fixed + +[24x24@2] +Size=24 +Scale=2 +Type=Fixed + +[24x24@3] +Size=24 +Scale=3 +Type=Fixed + +[24x24@4] +Size=24 +Scale=4 +Type=Fixed diff --git a/examples/bluetooth/btchat/main.cpp b/examples/bluetooth/btchat/main.cpp index 6347c914..16c8e272 100644 --- a/examples/bluetooth/btchat/main.cpp +++ b/examples/bluetooth/btchat/main.cpp @@ -6,11 +6,15 @@ #include <QtCore/qloggingcategory.h> #include <QtWidgets/qapplication.h> +using namespace Qt::StringLiterals; + int main(int argc, char *argv[]) { // QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true")); QApplication app(argc, argv); + QIcon::setThemeName(u"btchat"_s); + Chat d; QObject::connect(&d, &Chat::accepted, &app, &QApplication::quit); diff --git a/examples/bluetooth/btchat/remoteselector.cpp b/examples/bluetooth/btchat/remoteselector.cpp index 7ab496bd..2c1f9a00 100644 --- a/examples/bluetooth/btchat/remoteselector.cpp +++ b/examples/bluetooth/btchat/remoteselector.cpp @@ -9,8 +9,13 @@ #include <QtBluetooth/qbluetoothservicediscoveryagent.h> #include <QtBluetooth/qbluetoothuuid.h> +#include <QtGui/qguiapplication.h> +#include <QtGui/qstylehints.h> + #include <QtWidgets/qlistwidget.h> +using namespace Qt::StringLiterals; + RemoteSelector::RemoteSelector(const QBluetoothAddress &localAdapter, QWidget *parent) : QDialog(parent), ui(new Ui::RemoteSelector) { @@ -19,6 +24,10 @@ RemoteSelector::RemoteSelector(const QBluetoothAddress &localAdapter, QWidget *p setWindowState(Qt::WindowMaximized); #endif + QStyleHints *styleHints = qGuiApp->styleHints(); + updateIcon(styleHints->colorScheme()); + connect(styleHints, &QStyleHints::colorSchemeChanged, this, &RemoteSelector::updateIcon); + //! [createDiscoveryAgent] m_discoveryAgent = new QBluetoothServiceDiscoveryAgent(localAdapter); @@ -104,6 +113,14 @@ void RemoteSelector::discoveryFinished() ui->status->setText(tr("Select the chat service to connect to.")); } +void RemoteSelector::updateIcon(Qt::ColorScheme scheme) +{ + const QString bluetoothIconName = (scheme == Qt::ColorScheme::Dark) ? u"bluetooth_dark"_s + : u"bluetooth"_s; + const QIcon bluetoothIcon = QIcon::fromTheme(bluetoothIconName); + ui->iconLabel->setPixmap(bluetoothIcon.pixmap(24, 24, QIcon::Normal, QIcon::On)); +} + void RemoteSelector::on_remoteDevices_itemActivated(QListWidgetItem *item) { m_service = m_discoveredServices.value(item); @@ -116,6 +133,7 @@ void RemoteSelector::on_remoteDevices_itemActivated(QListWidgetItem *item) void RemoteSelector::on_remoteDevices_itemClicked(QListWidgetItem *) { ui->connectButton->setEnabled(true); + ui->connectButton->setFocus(); } void RemoteSelector::on_connectButton_clicked() diff --git a/examples/bluetooth/btchat/remoteselector.h b/examples/bluetooth/btchat/remoteselector.h index e319caa6..6d5ba034 100644 --- a/examples/bluetooth/btchat/remoteselector.h +++ b/examples/bluetooth/btchat/remoteselector.h @@ -42,6 +42,7 @@ private: private slots: void serviceDiscovered(const QBluetoothServiceInfo &serviceInfo); void discoveryFinished(); + void updateIcon(Qt::ColorScheme scheme); void on_remoteDevices_itemActivated(QListWidgetItem *item); void on_remoteDevices_itemClicked(QListWidgetItem *item); void on_cancelButton_clicked(); diff --git a/examples/bluetooth/btchat/remoteselector.ui b/examples/bluetooth/btchat/remoteselector.ui index 108ae885..853f6105 100644 --- a/examples/bluetooth/btchat/remoteselector.ui +++ b/examples/bluetooth/btchat/remoteselector.ui @@ -15,31 +15,58 @@ </property> <layout class="QVBoxLayout" name="verticalLayout"> <item> - <widget class="QLabel" name="status"> + <widget class="QLabel" name="iconLabel"> <property name="text"> - <string>Scanning...</string> + <string/> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> </property> </widget> </item> <item> - <widget class="QListWidget" name="remoteDevices"/> - </item> - <item> - <widget class="QPushButton" name="connectButton"> - <property name="enabled"> - <bool>false</bool> - </property> + <widget class="QLabel" name="status"> <property name="text"> - <string>Connect</string> + <string>Scanning...</string> </property> </widget> </item> <item> - <widget class="QPushButton" name="cancelButton"> - <property name="text"> - <string>Cancel</string> - </property> - </widget> + <widget class="QListWidget" name="remoteDevices"/> + </item> + <item> + <layout class="QHBoxLayout" name="horizontalLayout"> + <item> + <spacer name="horizontalSpacer"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + <item> + <widget class="QPushButton" name="cancelButton"> + <property name="text"> + <string>Cancel</string> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="connectButton"> + <property name="enabled"> + <bool>false</bool> + </property> + <property name="text"> + <string>Connect</string> + </property> + </widget> + </item> + </layout> </item> </layout> </widget> |