浏览代码

UI: Truncate names in advanced audio dialog

This limits the length of the name label in the advanced audio
dialog to 80 characters, so the dialog doesn't get too wide. If the
name is truncated, "..." are added at the end of the label, and the
label's tooltip is set to the full name of the source.
cg2121 3 年之前
父节点
当前提交
0d0b65e8c8
共有 4 个文件被更改,包括 31 次插入6 次删除
  1. 3 5
      UI/adv-audio-control.cpp
  2. 1 1
      UI/adv-audio-control.hpp
  3. 22 0
      UI/qt-wrappers.cpp
  4. 5 0
      UI/qt-wrappers.hpp

+ 3 - 5
UI/adv-audio-control.cpp

@@ -366,8 +366,7 @@ void OBSAdvAudioCtrl::OBSSourceRenamed(void *param, calldata_t *calldata)
 	QString newName = QT_UTF8(calldata_string(calldata, "new_name"));
 	QString newName = QT_UTF8(calldata_string(calldata, "new_name"));
 
 
 	QMetaObject::invokeMethod(reinterpret_cast<OBSAdvAudioCtrl *>(param),
 	QMetaObject::invokeMethod(reinterpret_cast<OBSAdvAudioCtrl *>(param),
-				  "SetSourceName",
-				  Q_ARG(const QString &, newName));
+				  "SetSourceName", Q_ARG(QString &, newName));
 }
 }
 
 
 /* ------------------------------------------------------------------------- */
 /* ------------------------------------------------------------------------- */
@@ -700,8 +699,7 @@ void OBSAdvAudioCtrl::SetIconVisible(bool visible)
 	visible ? iconLabel->show() : iconLabel->hide();
 	visible ? iconLabel->show() : iconLabel->hide();
 }
 }
 
 
-void OBSAdvAudioCtrl::SetSourceName(const QString &newName)
+void OBSAdvAudioCtrl::SetSourceName(QString &newName)
 {
 {
-	if (nameLabel->text() != newName)
-		nameLabel->setText(newName);
+	TruncateLabel(nameLabel, newName);
 }
 }

+ 1 - 1
UI/adv-audio-control.hpp

@@ -87,7 +87,7 @@ public slots:
 	void SourceMonitoringTypeChanged(int type);
 	void SourceMonitoringTypeChanged(int type);
 	void SourceMixersChanged(uint32_t mixers);
 	void SourceMixersChanged(uint32_t mixers);
 	void SourceBalanceChanged(int balance);
 	void SourceBalanceChanged(int balance);
-	void SetSourceName(const QString &newNamw);
+	void SetSourceName(QString &newNamw);
 
 
 	void volumeChanged(double db);
 	void volumeChanged(double db);
 	void percentChanged(int percent);
 	void percentChanged(int percent);

+ 22 - 0
UI/qt-wrappers.cpp

@@ -28,6 +28,7 @@
 #include <QKeyEvent>
 #include <QKeyEvent>
 #include <QFileDialog>
 #include <QFileDialog>
 #include <QStandardItemModel>
 #include <QStandardItemModel>
+#include <QLabel>
 
 
 #if !defined(_WIN32) && !defined(__APPLE__)
 #if !defined(_WIN32) && !defined(__APPLE__)
 #include <obs-nix-platform.h>
 #include <obs-nix-platform.h>
@@ -377,3 +378,24 @@ QStringList OpenFiles(QWidget *parent, QString title, QString path,
 
 
 	return files;
 	return files;
 }
 }
+
+static void SetLabelText(QLabel *label, const QString &newText)
+{
+	if (label->text() != newText)
+		label->setText(newText);
+}
+
+void TruncateLabel(QLabel *label, QString &newText, int length)
+{
+	if (newText.length() < length) {
+		label->setToolTip(QString());
+		SetLabelText(label, newText);
+		return;
+	}
+
+	label->setToolTip(newText);
+	newText.truncate(length);
+	newText += "...";
+
+	SetLabelText(label, newText);
+}

+ 5 - 0
UI/qt-wrappers.hpp

@@ -30,6 +30,7 @@
 
 
 #define QT_UTF8(str) QString::fromUtf8(str, -1)
 #define QT_UTF8(str) QString::fromUtf8(str, -1)
 #define QT_TO_UTF8(str) str.toUtf8().constData()
 #define QT_TO_UTF8(str) str.toUtf8().constData()
+#define MAX_LABEL_LENGTH 80
 
 
 class QDataStream;
 class QDataStream;
 class QComboBox;
 class QComboBox;
@@ -37,6 +38,7 @@ class QWidget;
 class QLayout;
 class QLayout;
 class QString;
 class QString;
 struct gs_window;
 struct gs_window;
+class QLabel;
 
 
 class OBSMessageBox {
 class OBSMessageBox {
 public:
 public:
@@ -117,3 +119,6 @@ QString OpenFile(QWidget *parent, QString title, QString path,
 		 QString extensions);
 		 QString extensions);
 QStringList OpenFiles(QWidget *parent, QString title, QString path,
 QStringList OpenFiles(QWidget *parent, QString title, QString path,
 		      QString extensions);
 		      QString extensions);
+
+void TruncateLabel(QLabel *label, QString &newText,
+		   int length = MAX_LABEL_LENGTH);