Konsole
CopyInputDialog.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "CopyInputDialog.h"
00022
00023
00024 #include <QtGui/QSortFilterProxyModel>
00025 #include <QtGui/QHeaderView>
00026
00027
00028 #include "ui_CopyInputDialog.h"
00029
00030 using namespace Konsole;
00031
00032 CopyInputDialog::CopyInputDialog(QWidget* parent)
00033 : KDialog(parent)
00034 {
00035 setCaption(i18n("Copy Input"));
00036 setButtons( KDialog::Ok | KDialog::Cancel );
00037
00038 _ui = new Ui::CopyInputDialog();
00039 _ui->setupUi(mainWidget());
00040
00041 connect(_ui->selectAllButton,SIGNAL(clicked()),this,SLOT(selectAll()));
00042 connect(_ui->deselectAllButton,SIGNAL(clicked()),this,SLOT(deselectAll()));
00043
00044 _ui->filterEdit->setClearButtonShown(true);
00045 _ui->filterEdit->setFocus();
00046
00047 _model = new CheckableSessionModel(parent);
00048 _model->setCheckColumn(1);
00049 _model->setSessions(SessionManager::instance()->sessions());
00050
00051 QSortFilterProxyModel* filterProxyModel = new QSortFilterProxyModel(this);
00052 filterProxyModel->setDynamicSortFilter(true);
00053 filterProxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
00054 filterProxyModel->setSourceModel(_model);
00055 filterProxyModel->setFilterKeyColumn(-1);
00056
00057 connect(_ui->filterEdit,SIGNAL(textChanged(QString)),filterProxyModel,
00058 SLOT(setFilterFixedString(QString)));
00059
00060 _ui->sessionList->setModel(filterProxyModel);
00061 _ui->sessionList->setColumnHidden(0,true);
00062 _ui->sessionList->header()->hide();
00063 }
00064 void CopyInputDialog::setChosenSessions(const QSet<Session*>& sessions)
00065 {
00066 QSet<Session*> checked = sessions;
00067 if (_masterSession)
00068 checked.insert(_masterSession);
00069
00070 _model->setCheckedSessions(checked);
00071 }
00072 QSet<Session*> CopyInputDialog::chosenSessions() const
00073 {
00074 return _model->checkedSessions();
00075 }
00076 void CopyInputDialog::setMasterSession(Session* session)
00077 {
00078 if (_masterSession)
00079 _model->setCheckable(_masterSession,true);
00080
00081 _model->setCheckable(session,false);
00082 QSet<Session*> checked = _model->checkedSessions();
00083 checked.insert(session);
00084 _model->setCheckedSessions(checked);
00085
00086 _masterSession = session;
00087 }
00088 void CopyInputDialog::setSelectionChecked(bool checked)
00089 {
00090 QAbstractItemModel* model = _ui->sessionList->model();
00091 int rows = model->rowCount();
00092
00093 QModelIndexList selected = _ui->sessionList->selectionModel()->selectedIndexes();
00094
00095 if (selected.count() > 1)
00096 {
00097 foreach(const QModelIndex &index,selected)
00098 setRowChecked(index.row(),checked);
00099 }
00100 else
00101 {
00102 for (int i=0;i<rows;i++)
00103 setRowChecked(i,checked);
00104 }
00105 }
00106 void CopyInputDialog::setRowChecked(int row, bool checked)
00107 {
00108 QAbstractItemModel* model = _ui->sessionList->model();
00109 QModelIndex index = model->index(row,_model->checkColumn());
00110 if (checked)
00111 model->setData(index,(int)Qt::Checked,Qt::CheckStateRole);
00112 else
00113 model->setData(index,(int)Qt::Unchecked,Qt::CheckStateRole);
00114 }
00115 CheckableSessionModel::CheckableSessionModel(QObject* parent)
00116 : SessionListModel(parent)
00117 , _checkColumn(0)
00118 {
00119 }
00120 void CheckableSessionModel::setCheckColumn(int column)
00121 {
00122 _checkColumn = column;
00123 reset();
00124 }
00125 Qt::ItemFlags CheckableSessionModel::flags(const QModelIndex& index) const
00126 {
00127 Session* session = (Session*)index.internalPointer();
00128
00129 if (_fixedSessions.contains(session))
00130 return SessionListModel::flags(index) & ~Qt::ItemIsEnabled;
00131 else
00132 return SessionListModel::flags(index) | Qt::ItemIsUserCheckable;
00133 }
00134 QVariant CheckableSessionModel::data(const QModelIndex& index, int role) const
00135 {
00136 if (role == Qt::CheckStateRole && index.column() == _checkColumn)
00137 {
00138 Session* session = (Session*)index.internalPointer();
00139
00140 if (_checkedSessions.contains(session))
00141 return QVariant::fromValue((int)Qt::Checked);
00142 else
00143 return QVariant::fromValue((int)Qt::Unchecked);
00144 }
00145 else
00146 return SessionListModel::data(index,role);
00147 }
00148 bool CheckableSessionModel::setData(const QModelIndex& index, const QVariant& value, int role)
00149 {
00150 if (role == Qt::CheckStateRole && index.column() == _checkColumn)
00151 {
00152 Session* session = (Session*)index.internalPointer();
00153
00154 if (_fixedSessions.contains(session))
00155 return false;
00156
00157 if (value.value<int>() == Qt::Checked)
00158 _checkedSessions.insert(session);
00159 else
00160 _checkedSessions.remove(session);
00161
00162 emit dataChanged(index,index);
00163 return true;
00164 }
00165 else
00166 return SessionListModel::setData(index,value,role);
00167 }
00168 void CheckableSessionModel::setCheckedSessions(const QSet<Session*> sessions)
00169 {
00170 _checkedSessions = sessions;
00171 reset();
00172 }
00173 QSet<Session*> CheckableSessionModel::checkedSessions() const
00174 {
00175 return _checkedSessions;
00176 }
00177 void CheckableSessionModel::setCheckable(Session* session, bool checkable)
00178 {
00179 if (!checkable)
00180 _fixedSessions.insert(session);
00181 else
00182 _fixedSessions.remove(session);
00183
00184 reset();
00185 }
00186 void CheckableSessionModel::sessionRemoved(Session* session)
00187 {
00188 _checkedSessions.remove(session);
00189 _fixedSessions.remove(session);
00190 }
00191
00192
00193
00194