1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
/*
SPDX-FileCopyrightText: 2008 Andreas Pakulat <apaku@gmx.de>
SPDX-FileCopyrightText: 2010 David Nolden <david.nolden.kdevelop@art-master.de>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "sessionchooserdialog.h"
#include "sessioncontroller.h"
#include "core.h"
#include <QDialogButtonBox>
#include <QKeyEvent>
#include <QLineEdit>
#include <QListView>
#include <QPushButton>
#include <QVBoxLayout>
#include <KLocalizedString>
#include <KMessageBox>
#include <KMessageBox_KDevCompat>
using namespace KDevelop;
SessionChooserDialog::SessionChooserDialog(QListView* view, QAbstractItemModel* model, QLineEdit* filter)
: m_view(view), m_model(model), m_filter(filter), m_deleteCandidateRow(-1)
{
m_updateStateTimer.setInterval(5000);
m_updateStateTimer.setSingleShot(false);
m_updateStateTimer.start();
connect(&m_updateStateTimer, &QTimer::timeout, this, &SessionChooserDialog::updateState);
connect(view, &QListView::doubleClicked, this, &SessionChooserDialog::doubleClicked);
connect(view, &QListView::entered, this, &SessionChooserDialog::itemEntered);
m_deleteButton = new QPushButton(view->viewport());
m_deleteButton->setIcon(QIcon::fromTheme(QStringLiteral("edit-delete")));
m_deleteButton->setToolTip(i18nc("@info:tooltip", "Delete session"));
m_deleteButton->hide();
connect(m_deleteButton, &QPushButton::clicked, this, &SessionChooserDialog::deleteButtonPressed);
m_deleteButtonTimer.setInterval(500);
m_deleteButtonTimer.setSingleShot(true);
connect(&m_deleteButtonTimer, &QTimer::timeout, this, &SessionChooserDialog::showDeleteButton);
view->setMouseTracking(true);
view->installEventFilter(this);
filter->installEventFilter(this);
connect(filter, &QLineEdit::textChanged, this, &SessionChooserDialog::filterTextChanged);
setWindowTitle(i18nc("@title:window", "Pick a Session"));
m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Close);
auto mainLayout = new QVBoxLayout(this);
m_mainWidget = new QWidget(this);
mainLayout->addWidget(m_mainWidget);
QPushButton *okButton = m_buttonBox->button(QDialogButtonBox::Ok);
okButton->setDefault(true);
okButton->setShortcut(Qt::Key_Return);
connect(m_buttonBox, &QDialogButtonBox::accepted, this, &SessionChooserDialog::accept);
connect(m_buttonBox, &QDialogButtonBox::rejected, this, &SessionChooserDialog::reject);
mainLayout->addWidget(m_buttonBox);
okButton->setText(i18nc("@action:button", "Run"));
okButton->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
}
void SessionChooserDialog::filterTextChanged()
{
m_view->selectionModel()->setCurrentIndex(m_model->index(0, 0), QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
bool enabled = m_view->model()->rowCount(QModelIndex())>0;
m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(enabled);
m_deleteButton->setVisible(false);
}
void SessionChooserDialog::doubleClicked(const QModelIndex& index)
{
if(m_model->flags(index) & Qt::ItemIsEnabled)
accept();
}
void SessionChooserDialog::updateState() {
// Sometimes locking may take some time, so we stop the timer, to prevent an 'avalanche' of events
m_updateStateTimer.stop();
for(int row = 0; row < m_model->rowCount(); ++row)
{
QString session = m_model->index(row, 0).data().toString();
if(session.isEmpty()) //create new session
continue;
QString state, tooltip;
SessionRunInfo info = SessionController::sessionRunInfo(session);
if(info.isRunning)
{
tooltip = i18nc("@info:tooltip", "Active session.\npid %1, app %2, host %3",
info.holderPid, info.holderApp, info.holderHostname);
state = i18n("Running");
}
m_model->setData(m_model->index(row, 1),
!info.isRunning ? QIcon() : QIcon::fromTheme(QStringLiteral("media-playback-start")),
Qt::DecorationRole);
m_model->setData(m_model->index(row, 1), tooltip, Qt::ToolTipRole);
m_model->setData(m_model->index(row, 2), state, Qt::DisplayRole);
}
m_updateStateTimer.start();
}
void SessionChooserDialog::itemEntered(const QModelIndex& index)
{
// The last row says "Create new session", we don't want to delete that
if(index.row() == m_model->rowCount()-1) {
m_deleteButton->hide();
m_deleteButtonTimer.stop();
return;
}
// align the delete-button to stay on the right border of the item
// we need the right most column's index
QModelIndex in = m_model->index( index.row(), 1 );
const QRect rect = m_view->visualRect(in);
m_deleteButton->resize(rect.height(), rect.height());
QPoint p(rect.right() - m_deleteButton->size().width(), rect.top()+rect.height()/2-m_deleteButton->height()/2);
m_deleteButton->move(p);
m_deleteCandidateRow = index.row();
m_deleteButtonTimer.start();
}
void SessionChooserDialog::showDeleteButton()
{
m_deleteButton->show();
}
bool SessionChooserDialog::eventFilter(QObject* object, QEvent* event)
{
if(object == m_view && event->type() == QEvent::Leave ) {
m_deleteButtonTimer.stop();
m_deleteButton->hide();
// don't eat the event, pass on
} else if (object == m_filter && event->type() == QEvent::KeyPress) {
auto *keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->key() == Qt::Key_Return) {
accept();
// don't eat the event, pass on
} else if (keyEvent->key() == Qt::Key_Up || keyEvent->key() == Qt::Key_Down) {
QModelIndex currentIndex = m_view->selectionModel()->currentIndex();
int selectRow = -1;
if (keyEvent->key() == Qt::Key_Up) {
if(!currentIndex.isValid()) {
selectRow = m_model->rowCount()-1;
} else if(currentIndex.row()-1 >= 0) {
selectRow = currentIndex.row()-1;
}
} else {
if(!currentIndex.isValid()) {
selectRow = 0;
} else if(currentIndex.row()+1 < m_model->rowCount()) {
selectRow = currentIndex.row()+1;
}
}
if (selectRow != -1) {
m_view->selectionModel()->setCurrentIndex(m_model->index(selectRow, 0), QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
}
return true; // eat event
}
}
return QDialog::eventFilter(object, event);
}
QWidget* SessionChooserDialog::mainWidget() const
{
return m_mainWidget;
}
void SessionChooserDialog::deleteButtonPressed()
{
if(m_deleteCandidateRow == -1)
return;
QModelIndex uuidIndex = m_model->index(m_deleteCandidateRow, 0);
QModelIndex sessionNameIndex = m_model->index(m_deleteCandidateRow, 3);
const QString uuid = m_model->data(uuidIndex, Qt::DisplayRole).toString();
const QString sessionName = m_model->data(sessionNameIndex, Qt::DisplayRole).toString();
TryLockSessionResult result = SessionController::tryLockSession( uuid );
if( !result.lock ) {
const QString errCaption = i18nc("@title:window", "Cannot Delete Session");
QString errText = i18nc("@info", "<p>Cannot delete a locked session.");
if( result.runInfo.holderPid != -1 ) {
errText += i18nc("@info", "<p>The session <b>%1</b> is locked by %2 on %3 (PID %4).",
sessionName, result.runInfo.holderApp, result.runInfo.holderHostname, result.runInfo.holderPid);
}
KMessageBox::error( this, errText, errCaption );
return;
}
const QString text = i18nc("@info", "The session <b>%1</b> and all contained settings will be deleted. The projects will stay unaffected. Do you really want to continue?", sessionName);
const QString caption = i18nc("@title:window", "Delete Session");
const KGuiItem deleteItem = KStandardGuiItem::del();
const KGuiItem cancelItem = KStandardGuiItem::cancel();
if (KMessageBox::warningTwoActions(this, text, caption, deleteItem, cancelItem) == KMessageBox::PrimaryAction) {
SessionController::deleteSessionFromDisk(result.lock);
m_model->removeRows( m_deleteCandidateRow, 1 );
m_deleteCandidateRow = -1;
}
}
|