Commit 9a1c47e7 authored by Gustavo Carneiro's avatar Gustavo Carneiro Committed by Kurt Hindenburg

Move CheckableSessionModel class to a new file.

parent dc81e79e
......@@ -59,6 +59,7 @@ set(konsoleprivate_SRCS ${sessionadaptors_SRCS}
ColorSchemeManager.cpp
ColorSchemeEditor.cpp
CopyInputDialog.cpp
CheckableSessionModel.cpp
EditProfileDialog.cpp
FontDialog.cpp
Emulation.cpp
......
/*
Copyright 2008 by Robert Knight <robertknight@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
*/
#include "CheckableSessionModel.h"
using namespace Konsole;
CheckableSessionModel::CheckableSessionModel(QObject *parent) :
SessionListModel(parent),
_checkedSessions(QSet<Session *>()),
_fixedSessions(QSet<Session *>()),
_checkColumn(0)
{
}
void CheckableSessionModel::setCheckColumn(int column)
{
beginResetModel();
_checkColumn = column;
endResetModel();
}
Qt::ItemFlags CheckableSessionModel::flags(const QModelIndex &index) const
{
auto *session = static_cast<Session *>(index.internalPointer());
if (_fixedSessions.contains(session)) {
return SessionListModel::flags(index) & ~Qt::ItemIsEnabled;
}
return SessionListModel::flags(index) | Qt::ItemIsUserCheckable;
}
QVariant CheckableSessionModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::CheckStateRole && index.column() == _checkColumn) {
auto *session = static_cast<Session *>(index.internalPointer());
return QVariant::fromValue(static_cast<int>(
_checkedSessions.contains(session) ? Qt::Checked : Qt::Unchecked)
);
}
return SessionListModel::data(index, role);
}
bool CheckableSessionModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (role == Qt::CheckStateRole && index.column() == _checkColumn) {
auto *session = static_cast<Session *>(index.internalPointer());
if (_fixedSessions.contains(session)) {
return false;
}
if (value.toInt() == Qt::Checked) {
_checkedSessions.insert(session);
} else {
_checkedSessions.remove(session);
}
emit dataChanged(index, index);
return true;
}
return SessionListModel::setData(index, value, role);
}
void CheckableSessionModel::setCheckedSessions(const QSet<Session *> &sessions)
{
beginResetModel();
_checkedSessions = sessions;
endResetModel();
}
QSet<Session *> CheckableSessionModel::checkedSessions() const
{
return _checkedSessions;
}
void CheckableSessionModel::setCheckable(Session *session, bool checkable)
{
beginResetModel();
if (!checkable) {
_fixedSessions.insert(session);
} else {
_fixedSessions.remove(session);
}
endResetModel();
}
void CheckableSessionModel::sessionRemoved(Session *session)
{
_checkedSessions.remove(session);
_fixedSessions.remove(session);
}
/*
Copyright 2008 by Robert Knight <robertknight@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
*/
#ifndef CHECKABLESESSIONMODEL_H
#define CHECKABLESESSIONMODEL_H
// Qt
#include <QPointer>
#include <QSet>
// Konsole
#include "session/Session.h"
#include "session/SessionListModel.h"
namespace Konsole
{
/**
* A list of sessions with a checkbox next to each one which allows the
* user to select a subset of the available sessions to perform
* some action on them.
*/
class CheckableSessionModel : public SessionListModel
{
Q_OBJECT
public:
explicit CheckableSessionModel(QObject *parent);
void setCheckColumn(int column);
int checkColumn() const;
/**
* Sets whether a session can be checked or un-checked.
* Non-checkable items have the Qt::ItemIsEnabled flag unset.
*/
void setCheckable(Session *session, bool checkable);
/** Sets the list of sessions which are currently checked. */
void setCheckedSessions(const QSet<Session *> &sessions);
/** Returns the set of checked sessions. */
QSet<Session *> checkedSessions() const;
// reimplemented from QAbstractItemModel
Qt::ItemFlags flags(const QModelIndex &index) const override;
QVariant data(const QModelIndex &index, int role) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
protected:
void sessionRemoved(Session *) override;
private:
QSet<Session *> _checkedSessions;
QSet<Session *> _fixedSessions;
int _checkColumn;
};
inline int CheckableSessionModel::checkColumn() const
{
return _checkColumn;
}
}
#endif
......@@ -25,6 +25,7 @@
// Konsole
#include "ui_CopyInputDialog.h"
#include "CheckableSessionModel.h"
#include <KLocalizedString>
#include <QDialogButtonBox>
......@@ -140,91 +141,3 @@ void CopyInputDialog::setRowChecked(int row, bool checked)
QModelIndex index = model->index(row, _model->checkColumn());
model->setData(index, static_cast<int>( checked ? Qt::Checked : Qt::Unchecked), Qt::CheckStateRole);
}
CheckableSessionModel::CheckableSessionModel(QObject *parent) :
SessionListModel(parent),
_checkedSessions(QSet<Session *>()),
_fixedSessions(QSet<Session *>()),
_checkColumn(0)
{
}
void CheckableSessionModel::setCheckColumn(int column)
{
beginResetModel();
_checkColumn = column;
endResetModel();
}
Qt::ItemFlags CheckableSessionModel::flags(const QModelIndex &index) const
{
auto *session = static_cast<Session *>(index.internalPointer());
if (_fixedSessions.contains(session)) {
return SessionListModel::flags(index) & ~Qt::ItemIsEnabled;
}
return SessionListModel::flags(index) | Qt::ItemIsUserCheckable;
}
QVariant CheckableSessionModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::CheckStateRole && index.column() == _checkColumn) {
auto *session = static_cast<Session *>(index.internalPointer());
return QVariant::fromValue(static_cast<int>(
_checkedSessions.contains(session) ? Qt::Checked : Qt::Unchecked)
);
}
return SessionListModel::data(index, role);
}
bool CheckableSessionModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (role == Qt::CheckStateRole && index.column() == _checkColumn) {
auto *session = static_cast<Session *>(index.internalPointer());
if (_fixedSessions.contains(session)) {
return false;
}
if (value.toInt() == Qt::Checked) {
_checkedSessions.insert(session);
} else {
_checkedSessions.remove(session);
}
emit dataChanged(index, index);
return true;
}
return SessionListModel::setData(index, value, role);
}
void CheckableSessionModel::setCheckedSessions(const QSet<Session *> &sessions)
{
beginResetModel();
_checkedSessions = sessions;
endResetModel();
}
QSet<Session *> CheckableSessionModel::checkedSessions() const
{
return _checkedSessions;
}
void CheckableSessionModel::setCheckable(Session *session, bool checkable)
{
beginResetModel();
if (!checkable) {
_fixedSessions.insert(session);
} else {
_fixedSessions.remove(session);
}
endResetModel();
}
void CheckableSessionModel::sessionRemoved(Session *session)
{
_checkedSessions.remove(session);
_fixedSessions.remove(session);
}
......@@ -90,50 +90,6 @@ private:
QPointer<Session> _masterSession;
};
/**
* A list of sessions with a checkbox next to each one which allows the
* user to select a subset of the available sessions to perform
* some action on them.
*/
class CheckableSessionModel : public SessionListModel
{
Q_OBJECT
public:
explicit CheckableSessionModel(QObject *parent);
void setCheckColumn(int column);
int checkColumn() const;
/**
* Sets whether a session can be checked or un-checked.
* Non-checkable items have the Qt::ItemIsEnabled flag unset.
*/
void setCheckable(Session *session, bool checkable);
/** Sets the list of sessions which are currently checked. */
void setCheckedSessions(const QSet<Session *> &sessions);
/** Returns the set of checked sessions. */
QSet<Session *> checkedSessions() const;
// reimplemented from QAbstractItemModel
Qt::ItemFlags flags(const QModelIndex &index) const override;
QVariant data(const QModelIndex &index, int role) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
protected:
void sessionRemoved(Session *) override;
private:
QSet<Session *> _checkedSessions;
QSet<Session *> _fixedSessions;
int _checkColumn;
};
inline int CheckableSessionModel::checkColumn() const
{
return _checkColumn;
}
}
#endif // COPYINPUTDIALOG_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment