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
21 #include "CopyInputDialog.h"
24 #include <QtGui/QSortFilterProxyModel>
25 #include <QtGui/QHeaderView>
28 #include "ui_CopyInputDialog.h"
30 using namespace Konsole
;
32 CopyInputDialog::CopyInputDialog(QWidget
* 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
;
68 checked
.insert(_masterSession
);
70 _model
->setCheckedSessions(checked
);
72 QSet
<Session
*> CopyInputDialog::chosenSessions() const
74 return _model
->checkedSessions();
76 void CopyInputDialog::setMasterSession(Session
* session
)
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
);
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());
111 model
->setData(index
,(int)Qt::Checked
,Qt::CheckStateRole
);
113 model
->setData(index
,(int)Qt::Unchecked
,Qt::CheckStateRole
);
115 CheckableSessionModel::CheckableSessionModel(QObject
* parent
)
116 : SessionListModel(parent
)
120 void CheckableSessionModel::setCheckColumn(int column
)
122 _checkColumn
= column
;
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
;
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
);
143 return QVariant::fromValue((int)Qt::Unchecked
);
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
))
157 if (value
.value
<int>() == Qt::Checked
)
158 _checkedSessions
.insert(session
);
160 _checkedSessions
.remove(session
);
162 emit
dataChanged(index
,index
);
166 return SessionListModel::setData(index
,value
,role
);
168 void CheckableSessionModel::setCheckedSessions(const QSet
<Session
*> sessions
)
170 _checkedSessions
= sessions
;
173 QSet
<Session
*> CheckableSessionModel::checkedSessions() const
175 return _checkedSessions
;
177 void CheckableSessionModel::setCheckable(Session
* session
, bool checkable
)
180 _fixedSessions
.insert(session
);
182 _fixedSessions
.remove(session
);
186 void CheckableSessionModel::sessionRemoved(Session
* session
)
188 _checkedSessions
.remove(session
);
189 _fixedSessions
.remove(session
);