File indexing completed on 2024-05-05 05:54:32

0001 /*
0002     SPDX-FileCopyrightText: 2015 Elvis Angelaccio <elvis.angelaccio@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "sessiondialog.h"
0008 #include "sessionmodel.h"
0009 
0010 #include <KLocalizedString>
0011 #include <KMessageBox>
0012 #include <KMessageWidget>
0013 #include <kwidgetsaddons_version.h>
0014 
0015 #include <QDialogButtonBox>
0016 #include <QKeyEvent>
0017 #include <QPushButton>
0018 #include <QScreen>
0019 #include <QSortFilterProxyModel>
0020 #include <QTableView>
0021 
0022 SessionDialog::SessionDialog(SessionModel *sessionModel, QWidget *parent) : QDialog(parent, Qt::Dialog)
0023 {
0024     setupUi(this);
0025     m_sessionModel = sessionModel;
0026 
0027     m_proxyModel = new QSortFilterProxyModel {this};
0028     m_proxyModel->setSourceModel(m_sessionModel);
0029 
0030     m_sessionView->setModel(m_proxyModel);
0031     m_sessionView->resizeColumnsToContents();
0032     // TODO: the user may want to select/remove more than one session
0033 
0034     m_msgWidget->hide();
0035     m_buttonBox->button(QDialogButtonBox::Ok)->setText(i18nc("@action:button", "Open session"));
0036 
0037     connect(m_sessionView, &QTableView::doubleClicked, this, &SessionDialog::slotDoubleClicked);
0038     connect(m_sessionView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &SessionDialog::slotSelectionChanged);
0039     connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0040     connect(m_sessionModel, &SessionModel::rowsInserted, this, &SessionDialog::slotSessionAdded);
0041     connect(m_sessionModel, &SessionModel::rowsRemoved, this, &SessionDialog::slotEmptyModel);
0042 
0043     slotEmptyModel();
0044     slotSelectionChanged();
0045 
0046     auto screens = QGuiApplication::screens();
0047     // Set a good default size, tested on 1920x1200 and 1366x768 screens.
0048     resize(screens[0]->size() / 2.5);
0049 }
0050 
0051 Session SessionDialog::selectedSession() const
0052 {
0053     return m_selectedSession;
0054 }
0055 
0056 void SessionDialog::keyPressEvent(QKeyEvent *event)
0057 {
0058     if (event->key() == Qt::Key_Delete && !m_sessionModel->isEmpty()) {
0059         removeDialog();
0060     }
0061 
0062     QDialog::keyPressEvent(event);
0063 }
0064 
0065 void SessionDialog::accept()
0066 {
0067     m_selectedSession = m_sessionModel->data(selectedIndex(), static_cast<int>(SessionModel::Roles::SessionRole)).value<Session>();
0068     QDialog::accept();
0069 }
0070 
0071 void SessionDialog::slotDoubleClicked(const QModelIndex& index)
0072 {
0073     if (m_sessionModel->isEditable(index))
0074         return;
0075 
0076     this->accept();
0077 }
0078 
0079 void SessionDialog::slotSelectionChanged()
0080 {
0081     auto openButton = m_buttonBox->button(QDialogButtonBox::Ok);
0082     m_sessionView->selectionModel()->hasSelection() ? openButton->setEnabled(true) : openButton->setEnabled(false);
0083 }
0084 
0085 void SessionDialog::slotSessionAdded()
0086 {
0087     auto openButton = m_buttonBox->button(QDialogButtonBox::Ok);
0088 
0089     if (openButton->isEnabled())
0090         return;
0091 
0092     openButton->setEnabled(true);
0093     openButton->setToolTip(QString());
0094 }
0095 
0096 void SessionDialog::slotEmptyModel()
0097 {
0098     if (!m_sessionModel->isEmpty())
0099         return;
0100 
0101     const auto message = i18nc("@info", "You don't have any saved session yet.");
0102 
0103     auto openButton = m_buttonBox->button(QDialogButtonBox::Ok);
0104     openButton->setEnabled(false);
0105     openButton->setToolTip(message);
0106 
0107     m_msgWidget->setMessageType(KMessageWidget::Information);
0108     m_msgWidget->setText(message);
0109     m_msgWidget->animatedShow();
0110 }
0111 
0112 QModelIndex SessionDialog::selectedIndex()
0113 {
0114     auto sortedIndex = m_sessionView->selectionModel()->currentIndex();
0115 
0116     return m_proxyModel->mapToSource(sortedIndex);
0117 }
0118 
0119 void SessionDialog::removeDialog()
0120 {
0121 #if KWIDGETSADDONS_VERSION < QT_VERSION_CHECK(5, 100, 0)
0122     const auto buttonCode = KMessageBox::warningYesNo(
0123                 this,
0124                 i18nc("@info", "Do you want to remove the selected session?"),
0125                 i18nc("@title:window", "Confirm deletion"),
0126                 KStandardGuiItem::yes(),
0127                 KStandardGuiItem::no(),
0128                 QStringLiteral("delete-session"));
0129 
0130     if (buttonCode != KMessageBox::Yes)
0131 #else
0132     const auto buttonCode = KMessageBox::warningTwoActions(
0133                 this,
0134                 i18nc("@info", "Do you want to remove the selected session?"),
0135                 i18nc("@title:window", "Confirm deletion"),
0136                 KStandardGuiItem::remove(),
0137                 KStandardGuiItem::cancel(),
0138                 QStringLiteral("delete-session"));
0139 
0140     if (buttonCode != KMessageBox::PrimaryAction)
0141 #endif
0142         return;
0143 
0144     m_sessionModel->removeRow(selectedIndex().row());
0145 }
0146 
0147 
0148 #include "moc_sessiondialog.cpp"