• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Konsole

  • kde-4.14
  • applications
  • konsole
  • src
SessionListModel.cpp
Go to the documentation of this file.
1 /*
2  This source file is part of Konsole, a terminal emulator.
3 
4  Copyright 2006-2008 by Robert Knight <robertknight@gmail.com>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301 USA.
20 */
21 
22 // Own
23 #include "SessionListModel.h"
24 
25 // KDE
26 #include <KIcon>
27 #include <KLocalizedString>
28 
29 // Konsole
30 #include "Session.h"
31 
32 using Konsole::Session;
33 using Konsole::SessionListModel;
34 
35 SessionListModel::SessionListModel(QObject* parent)
36  : QAbstractListModel(parent)
37 {
38 }
39 
40 void SessionListModel::setSessions(const QList<Session*>& sessions)
41 {
42  _sessions = sessions;
43 
44  foreach(Session * session, sessions) {
45  connect(session, SIGNAL(finished()), this, SLOT(sessionFinished()));
46  }
47 
48  reset();
49 }
50 
51 QVariant SessionListModel::data(const QModelIndex& index, int role) const
52 {
53  Q_ASSERT(index.isValid());
54 
55  int row = index.row();
56  int column = index.column();
57 
58  Q_ASSERT(row >= 0 && row < _sessions.count());
59  Q_ASSERT(column >= 0 && column < 2);
60 
61  switch (role) {
62  case Qt::DisplayRole:
63  if (column == 1) {
64  // This code is duplicated from SessionController.cpp
65  QString title = _sessions[row]->title(Session::DisplayedTitleRole);
66 
67  // special handling for the "%w" marker which is replaced with the
68  // window title set by the shell
69  title.replace("%w", _sessions[row]->userTitle());
70  // special handling for the "%#" marker which is replaced with the
71  // number of the shell
72  title.replace("%#", QString::number(_sessions[row]->sessionId()));
73  return title;
74  } else if (column == 0) {
75  return _sessions[row]->sessionId();
76  }
77  break;
78  case Qt::DecorationRole:
79  if (column == 1)
80  return KIcon(_sessions[row]->iconName());
81  else
82  return QVariant();
83  }
84 
85  return QVariant();
86 }
87 
88 QVariant SessionListModel::headerData(int section, Qt::Orientation orientation,
89  int role) const
90 {
91  if (role != Qt::DisplayRole)
92  return QVariant();
93 
94  if (orientation == Qt::Vertical) {
95  return QVariant();
96  } else {
97  switch (section) {
98  case 0:
99  return i18nc("@item:intable The session index", "Number");
100  case 1:
101  return i18nc("@item:intable The session title", "Title");
102  default:
103  return QVariant();
104  }
105  }
106 }
107 
108 int SessionListModel::columnCount(const QModelIndex&) const
109 {
110  return 2;
111 }
112 
113 int SessionListModel::rowCount(const QModelIndex&) const
114 {
115  return _sessions.count();
116 }
117 
118 QModelIndex SessionListModel::parent(const QModelIndex&) const
119 {
120  return QModelIndex();
121 }
122 
123 void SessionListModel::sessionFinished()
124 {
125  Session* session = qobject_cast<Session*>(sender());
126  int row = _sessions.indexOf(session);
127 
128  if (row != -1) {
129  beginRemoveRows(QModelIndex(), row, row);
130  sessionRemoved(session);
131  _sessions.removeAt(row);
132  endRemoveRows();
133  }
134 }
135 
136 QModelIndex SessionListModel::index(int row, int column, const QModelIndex& parent) const
137 {
138  if (hasIndex(row, column, parent))
139  return createIndex(row, column, _sessions[row]);
140  else
141  return QModelIndex();
142 }
143 
144 #include "SessionListModel.moc"
QAbstractItemModel::hasIndex
bool hasIndex(int row, int column, const QModelIndex &parent) const
Session.h
QModelIndex
Konsole::Session
Represents a terminal session consisting of a pseudo-teletype and a terminal emulation.
Definition: Session.h:78
Konsole::SessionListModel::headerData
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const
Definition: SessionListModel.cpp:88
QObject::sender
QObject * sender() const
Konsole::Session::DisplayedTitleRole
The title of the session which is displayed in tabs etc.
Definition: Session.h:255
Konsole::SessionListModel::data
virtual QVariant data(const QModelIndex &index, int role) const
Definition: SessionListModel.cpp:51
Konsole::SessionListModel::index
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
Definition: SessionListModel.cpp:136
Konsole::SessionListModel
Item-view model which contains a flat list of sessions.
Definition: SessionListModel.h:42
QAbstractItemModel::reset
void reset()
QModelIndex::isValid
bool isValid() const
QString::number
QString number(int n, int base)
QObject
QAbstractListModel
QAbstractItemModel::beginRemoveRows
void beginRemoveRows(const QModelIndex &parent, int first, int last)
QModelIndex::row
int row() const
Konsole::SessionListModel::setSessions
void setSessions(const QList< Session * > &sessions)
Sets the list of sessions displayed in the model.
Definition: SessionListModel.cpp:40
QString
QList
Konsole::SessionListModel::rowCount
virtual int rowCount(const QModelIndex &parent) const
Definition: SessionListModel.cpp:113
QAbstractItemModel::createIndex
QModelIndex createIndex(int row, int column, void *ptr) const
SessionListModel.h
QString::replace
QString & replace(int position, int n, QChar after)
QModelIndex::column
int column() const
QAbstractItemModel::endRemoveRows
void endRemoveRows()
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Konsole::SessionListModel::columnCount
virtual int columnCount(const QModelIndex &parent) const
Definition: SessionListModel.cpp:108
QObject::parent
QObject * parent() const
Konsole::SessionListModel::sessionRemoved
virtual void sessionRemoved(Session *)
Definition: SessionListModel.h:66
QVariant
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Konsole

Skip menu "Konsole"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal