diff --git a/src/plugins/core/session/sessiondialog.cpp b/src/plugins/core/session/sessiondialog.cpp index 1c72b437c..1350a68b9 100644 --- a/src/plugins/core/session/sessiondialog.cpp +++ b/src/plugins/core/session/sessiondialog.cpp @@ -174,6 +174,7 @@ SessionNameInputDialog::SessionNameInputDialog(QWidget *parent) : DDialog(parent) { initUI(); + connect(this, &SessionNameInputDialog::buttonClicked, this, &SessionNameInputDialog::handleButtonClicked); } void SessionNameInputDialog::initUI() @@ -181,6 +182,8 @@ void SessionNameInputDialog::initUI() setSpacing(10); setIcon(QIcon::fromTheme("ide")); lineEdit = new DLineEdit(this); + QRegExpValidator *validator = new QRegExpValidator(QRegExp("[^/\?:\\\\*]*"), lineEdit); + lineEdit->lineEdit()->setValidator(validator); lineEdit->setPlaceholderText(tr("Please input session name")); connect(lineEdit, &DLineEdit::textChanged, this, [this](const QString &text) { getButton(1)->setEnabled(!text.isEmpty()); @@ -194,6 +197,25 @@ void SessionNameInputDialog::initUI() getButton(1)->setEnabled(false); getButton(2)->setEnabled(false); setFocusProxy(lineEdit); + setOnButtonClickedClose(false); +} + +void SessionNameInputDialog::handleButtonClicked(int index) +{ + if (index == 0) + return reject(); + + const auto name = sessionName(); + if (SessionManager::instance()->sessionList().contains(name)) { + QString msg = tr("The session already exists, please re-enter."); + lineEdit->showAlertMessage(msg); + return; + } + + if (index == 2) + isSwitchTo = true; + + accept(); } void SessionNameInputDialog::setSessionName(const QString &name) @@ -211,3 +233,8 @@ void SessionNameInputDialog::setActionText(const QString &actText, const QString getButton(1)->setText(actText); getButton(2)->setText(openActText); } + +bool SessionNameInputDialog::isSwitchToRequested() const +{ + return isSwitchTo; +} diff --git a/src/plugins/core/session/sessiondialog.h b/src/plugins/core/session/sessiondialog.h index c138439d6..d32b3e68b 100644 --- a/src/plugins/core/session/sessiondialog.h +++ b/src/plugins/core/session/sessiondialog.h @@ -51,11 +51,14 @@ class SessionNameInputDialog : public DTK_WIDGET_NAMESPACE::DDialog void setSessionName(const QString &name); QString sessionName() const; void setActionText(const QString &actText, const QString &openActText); + bool isSwitchToRequested() const; private: void initUI(); + void handleButtonClicked(int index); DTK_WIDGET_NAMESPACE::DLineEdit *lineEdit { nullptr }; + bool isSwitchTo { false }; }; #endif // SESSIONDIALOG_H diff --git a/src/plugins/core/session/sessionlistview.cpp b/src/plugins/core/session/sessionlistview.cpp index c1bd9f2c1..a8402dc9b 100644 --- a/src/plugins/core/session/sessionlistview.cpp +++ b/src/plugins/core/session/sessionlistview.cpp @@ -195,17 +195,17 @@ QStringList SessionListView::selectedSessions() const void SessionListView::runInputDialog(SessionNameInputDialog *dialog, std::function handler) { - int ret = dialog->exec(); - if (ret < 1) - return; - - const auto name = dialog->sessionName(); - if (name.isEmpty() || SessionManager::instance()->sessionList().contains(name)) - return; + if (dialog->exec() == QDialog::Accepted) { + const auto name = dialog->sessionName(); + if (name.isEmpty()) + return; - handler(name); - model.reset(); - if (ret == 2) - SessionManager::instance()->loadSession(name); - Q_EMIT sessionCreated(name); + handler(name); + model.reset(); + if (dialog->isSwitchToRequested()) { + SessionManager::instance()->loadSession(name); + Q_EMIT sessionSwitched(); + } + Q_EMIT sessionCreated(name); + } } diff --git a/src/plugins/recent/mainframe/sessionitemwidget.cpp b/src/plugins/recent/mainframe/sessionitemwidget.cpp index b084a31dd..e1fc33f41 100644 --- a/src/plugins/recent/mainframe/sessionitemwidget.cpp +++ b/src/plugins/recent/mainframe/sessionitemwidget.cpp @@ -35,6 +35,7 @@ ArrowHeaderLine::ArrowHeaderLine(QWidget *parent) titleLabel = new DLabel(this); titleLabel->installEventFilter(this); titleLabel->setCursor(Qt::PointingHandCursor); + titleLabel->setTextFormat(Qt::PlainText); DFontSizeManager::instance()->bind(titleLabel, DFontSizeManager::T5, QFont::Medium); connect(arrowButton, &DToolButton::clicked, this, &ArrowHeaderLine::expandChanged); @@ -75,7 +76,7 @@ void ArrowHeaderLine::setTitleTip(const QString &tooltip) void ArrowHeaderLine::resizeEvent(QResizeEvent *e) { updateTitle(); - + return QWidget::resizeEvent(e); } @@ -126,7 +127,7 @@ void ArrowHeaderLine::reverseArrowDirection() void ArrowHeaderLine::updateTitle() { QFontMetrics fm = titleLabel->fontMetrics(); - auto displayText = fm.elidedText(titleText, Qt::ElideRight, titleLabel->width()); + auto displayText = fm.elidedText(titleText, Qt::ElideMiddle, titleLabel->width()); titleLabel->setText(displayText); } @@ -321,7 +322,10 @@ void SessionItemWidgetPrivate::runInputDialog(const QString &title, const QStrin dlg.setTitle(title); dlg.setIcon(QIcon::fromTheme("ide")); DLineEdit *lineEdit = new DLineEdit(&dlg); + QRegExpValidator *validator = new QRegExpValidator(QRegExp("[^/\?:\\\\*]*"), lineEdit); + lineEdit->lineEdit()->setValidator(validator); lineEdit->setPlaceholderText(SessionItemWidget::tr("Please input session name")); + lineEdit->setText(editText); connect(lineEdit, &DLineEdit::textChanged, &dlg, [&dlg](const QString &text) { dlg.getButton(1)->setEnabled(!text.isEmpty()); dlg.getButton(2)->setEnabled(!text.isEmpty()); @@ -332,21 +336,26 @@ void SessionItemWidgetPrivate::runInputDialog(const QString &title, const QStrin dlg.addButton(SessionItemWidget::tr("Cancel", "button")); dlg.addButton(actList[0]); dlg.addButton(actList[1], true, DDialog::ButtonRecommend); - dlg.getButton(1)->setEnabled(false); - dlg.getButton(2)->setEnabled(false); - lineEdit->setText(editText); + dlg.setOnButtonClickedClose(false); + connect(&dlg, &DDialog::buttonClicked, this, [&](int index) { + if (index == 0) + return dlg.reject(); + + const auto name = lineEdit->text(); + if (sessionSrv->sessionList().contains(name)) { + QString msg = tr("The session already exists, please re-enter."); + lineEdit->showAlertMessage(msg); + return; + } - int ret = dlg.exec(); - if (ret < 1) - return; + handler(name); + if (index == 2) + sessionSrv->loadSession(name); - const auto name = lineEdit->text(); - if (name.isEmpty() || sessionSrv->sessionList().contains(name)) - return; + dlg.accept(); + }); - handler(name); - if (ret == 2) - sessionSrv->loadSession(name); + dlg.exec(); } SessionItemWidget::SessionItemWidget(QWidget *parent) diff --git a/src/plugins/recent/mainframe/sessionitemwidget.h b/src/plugins/recent/mainframe/sessionitemwidget.h index 52a53a1ec..9fde82215 100644 --- a/src/plugins/recent/mainframe/sessionitemwidget.h +++ b/src/plugins/recent/mainframe/sessionitemwidget.h @@ -45,7 +45,7 @@ class ArrowHeaderLine : public QWidget private: void reverseArrowDirection(); void updateTitle(); - + bool isExpanded { false }; QString titleText; DTK_WIDGET_NAMESPACE::DToolButton *arrowButton { nullptr };