Warning, file /utilities/kronometer/src/gui/sessiondialog.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     Copyright (C) 2015 by Elvis Angelaccio <elvis.angelaccio@kde.org>
0003 
0004     This file is part of Kronometer.
0005 
0006     Kronometer is free software: you can redistribute it and/or modify
0007     it under the terms of the GNU General Public License as published by
0008     the Free Software Foundation, either version 2 of the License, or
0009     (at your option) any later version.
0010 
0011     Kronometer is distributed in the hope that it will be useful,
0012     but WITHOUT ANY WARRANTY; without even the implied warranty of
0013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014     GNU General Public License for more details.
0015 
0016     You should have received a copy of the GNU General Public License
0017     along with Kronometer.  If not, see <http://www.gnu.org/licenses/>.
0018 */
0019 
0020 #include "sessiondialog.h"
0021 #include "sessionmodel.h"
0022 
0023 #include <KLocalizedString>
0024 #include <KMessageBox>
0025 #include <KMessageWidget>
0026 
0027 #include <QDesktopWidget>
0028 #include <QDialogButtonBox>
0029 #include <QHeaderView>
0030 #include <QKeyEvent>
0031 #include <QPushButton>
0032 #include <QSortFilterProxyModel>
0033 #include <QTableView>
0034 
0035 SessionDialog::SessionDialog(QWidget *parent) : QDialog(parent, Qt::Dialog)
0036 {
0037     setupUi(this);
0038     m_sessionModel = new SessionModel {this};
0039 
0040     m_proxyModel = new QSortFilterProxyModel {this};
0041     m_proxyModel->setSourceModel(m_sessionModel);
0042 
0043     m_sessionView->setModel(m_proxyModel);
0044     m_sessionView->resizeColumnsToContents();
0045     // TODO: the user may want to select/remove more than one session
0046 
0047     m_msgWidget->hide();
0048     m_buttonBox->button(QDialogButtonBox::Ok)->setText(i18nc("@action:button", "Open session"));
0049 
0050     connect(m_sessionView, &QTableView::doubleClicked, this, &SessionDialog::slotDoubleClicked);
0051     connect(m_sessionView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &SessionDialog::slotSelectionChanged);
0052     connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0053     connect(m_sessionModel, &SessionModel::rowsInserted, this, &SessionDialog::slotSessionAdded);
0054     connect(m_sessionModel, &SessionModel::rowsRemoved, this, &SessionDialog::slotEmptyModel);
0055 
0056     slotEmptyModel();
0057     slotSelectionChanged();
0058 
0059     auto desktopWidget = QApplication::desktop();
0060     // Set a good default size, tested on 1920x1200 and 1366x768 screens.
0061     resize(desktopWidget->screenGeometry().size() / 2.5);
0062 }
0063 
0064 Session SessionDialog::selectedSession() const
0065 {
0066     return m_selectedSession;
0067 }
0068 
0069 void SessionDialog::keyPressEvent(QKeyEvent *event)
0070 {
0071     if (event->key() == Qt::Key_Delete and not m_sessionModel->isEmpty()) {
0072         removeDialog();
0073     }
0074 
0075     QDialog::keyPressEvent(event);
0076 }
0077 
0078 void SessionDialog::accept()
0079 {
0080     m_selectedSession = m_sessionModel->data(selectedIndex(), static_cast<int>(SessionModel::Roles::SessionRole)).value<Session>();
0081     QDialog::accept();
0082 }
0083 
0084 void SessionDialog::slotDoubleClicked(const QModelIndex& index)
0085 {
0086     if (m_sessionModel->isEditable(index))
0087         return;
0088 
0089     this->accept();
0090 }
0091 
0092 void SessionDialog::slotSelectionChanged()
0093 {
0094     auto openButton = m_buttonBox->button(QDialogButtonBox::Ok);
0095     m_sessionView->selectionModel()->hasSelection() ? openButton->setEnabled(true) : openButton->setEnabled(false);
0096 }
0097 
0098 void SessionDialog::slotSessionAdded()
0099 {
0100     auto openButton = m_buttonBox->button(QDialogButtonBox::Ok);
0101 
0102     if (openButton->isEnabled())
0103         return;
0104 
0105     openButton->setEnabled(true);
0106     openButton->setToolTip(QString());
0107 }
0108 
0109 void SessionDialog::slotEmptyModel()
0110 {
0111     if (not m_sessionModel->isEmpty())
0112         return;
0113 
0114     const auto message = i18nc("@info", "You don't have any saved session yet.");
0115 
0116     auto openButton = m_buttonBox->button(QDialogButtonBox::Ok);
0117     openButton->setEnabled(false);
0118     openButton->setToolTip(message);
0119 
0120     m_msgWidget->setMessageType(KMessageWidget::Information);
0121     m_msgWidget->setText(message);
0122     m_msgWidget->animatedShow();
0123 }
0124 
0125 QModelIndex SessionDialog::selectedIndex()
0126 {
0127     auto sortedIndex = m_sessionView->selectionModel()->currentIndex();
0128 
0129     return m_proxyModel->mapToSource(sortedIndex);
0130 }
0131 
0132 void SessionDialog::removeDialog()
0133 {
0134     const auto buttonCode = KMessageBox::warningYesNo(
0135                 this,
0136                 i18nc("@info", "Do you want to remove the selected session?"),
0137                 i18nc("@title:window", "Confirm deletion"),
0138                 KStandardGuiItem::yes(),
0139                 KStandardGuiItem::no(),
0140                 QStringLiteral("delete-session"));
0141 
0142     if (buttonCode != KMessageBox::Yes)
0143         return;
0144 
0145     m_sessionModel->removeRow(selectedIndex().row());
0146 }
0147