Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / konsole / src / CopyInputDialog.cpp
blob7c353156319e26bf3b12b78f16c54132a1a7f9c8
1 /*
2 Copyright 2008 by Robert Knight <robertknight@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301 USA.
20 // Own
21 #include "CopyInputDialog.h"
23 // Qt
24 #include <QtGui/QSortFilterProxyModel>
25 #include <QtGui/QHeaderView>
27 // Konsole
28 #include "ui_CopyInputDialog.h"
30 using namespace Konsole;
32 CopyInputDialog::CopyInputDialog(QWidget* parent)
33 : KDialog(parent)
35 setCaption(i18n("Copy Input"));
36 setButtons( KDialog::Ok | KDialog::Cancel );
38 _ui = new Ui::CopyInputDialog();
39 _ui->setupUi(mainWidget());
41 connect(_ui->selectAllButton,SIGNAL(clicked()),this,SLOT(selectAll()));
42 connect(_ui->deselectAllButton,SIGNAL(clicked()),this,SLOT(deselectAll()));
44 _ui->filterEdit->setClearButtonShown(true);
45 _ui->filterEdit->setFocus();
47 _model = new CheckableSessionModel(parent);
48 _model->setCheckColumn(1);
49 _model->setSessions(SessionManager::instance()->sessions());
51 QSortFilterProxyModel* filterProxyModel = new QSortFilterProxyModel(this);
52 filterProxyModel->setDynamicSortFilter(true);
53 filterProxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
54 filterProxyModel->setSourceModel(_model);
55 filterProxyModel->setFilterKeyColumn(-1);
57 connect(_ui->filterEdit,SIGNAL(textChanged(QString)),filterProxyModel,
58 SLOT(setFilterFixedString(QString)));
60 _ui->sessionList->setModel(filterProxyModel);
61 _ui->sessionList->setColumnHidden(0,true); // Hide number column
62 _ui->sessionList->header()->hide();
64 void CopyInputDialog::setChosenSessions(const QSet<Session*>& sessions)
66 QSet<Session*> checked = sessions;
67 if (_masterSession)
68 checked.insert(_masterSession);
70 _model->setCheckedSessions(checked);
72 QSet<Session*> CopyInputDialog::chosenSessions() const
74 return _model->checkedSessions();
76 void CopyInputDialog::setMasterSession(Session* session)
78 if (_masterSession)
79 _model->setCheckable(_masterSession,true);
81 _model->setCheckable(session,false);
82 QSet<Session*> checked = _model->checkedSessions();
83 checked.insert(session);
84 _model->setCheckedSessions(checked);
86 _masterSession = session;
88 void CopyInputDialog::setSelectionChecked(bool checked)
90 QAbstractItemModel* model = _ui->sessionList->model();
91 int rows = model->rowCount();
93 QModelIndexList selected = _ui->sessionList->selectionModel()->selectedIndexes();
95 if (selected.count() > 1)
97 foreach(const QModelIndex &index,selected)
98 setRowChecked(index.row(),checked);
100 else
102 for (int i=0;i<rows;i++)
103 setRowChecked(i,checked);
106 void CopyInputDialog::setRowChecked(int row, bool checked)
108 QAbstractItemModel* model = _ui->sessionList->model();
109 QModelIndex index = model->index(row,_model->checkColumn());
110 if (checked)
111 model->setData(index,(int)Qt::Checked,Qt::CheckStateRole);
112 else
113 model->setData(index,(int)Qt::Unchecked,Qt::CheckStateRole);
115 CheckableSessionModel::CheckableSessionModel(QObject* parent)
116 : SessionListModel(parent)
117 , _checkColumn(0)
120 void CheckableSessionModel::setCheckColumn(int column)
122 _checkColumn = column;
123 reset();
125 Qt::ItemFlags CheckableSessionModel::flags(const QModelIndex& index) const
127 Session* session = (Session*)index.internalPointer();
129 if (_fixedSessions.contains(session))
130 return SessionListModel::flags(index) & ~Qt::ItemIsEnabled;
131 else
132 return SessionListModel::flags(index) | Qt::ItemIsUserCheckable;
134 QVariant CheckableSessionModel::data(const QModelIndex& index, int role) const
136 if (role == Qt::CheckStateRole && index.column() == _checkColumn)
138 Session* session = (Session*)index.internalPointer();
140 if (_checkedSessions.contains(session))
141 return QVariant::fromValue((int)Qt::Checked);
142 else
143 return QVariant::fromValue((int)Qt::Unchecked);
145 else
146 return SessionListModel::data(index,role);
148 bool CheckableSessionModel::setData(const QModelIndex& index, const QVariant& value, int role)
150 if (role == Qt::CheckStateRole && index.column() == _checkColumn)
152 Session* session = (Session*)index.internalPointer();
154 if (_fixedSessions.contains(session))
155 return false;
157 if (value.value<int>() == Qt::Checked)
158 _checkedSessions.insert(session);
159 else
160 _checkedSessions.remove(session);
162 emit dataChanged(index,index);
163 return true;
165 else
166 return SessionListModel::setData(index,value,role);
168 void CheckableSessionModel::setCheckedSessions(const QSet<Session*> sessions)
170 _checkedSessions = sessions;
171 reset();
173 QSet<Session*> CheckableSessionModel::checkedSessions() const
175 return _checkedSessions;
177 void CheckableSessionModel::setCheckable(Session* session, bool checkable)
179 if (!checkable)
180 _fixedSessions.insert(session);
181 else
182 _fixedSessions.remove(session);
184 reset();
186 void CheckableSessionModel::sessionRemoved(Session* session)
188 _checkedSessions.remove(session);
189 _fixedSessions.remove(session);