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

kopete/kopete

  • sources
  • kde-4.12
  • kdenetwork
  • kopete
  • kopete
  • config
  • status
statusmodel.cpp
Go to the documentation of this file.
1 /*
2  kopetestatusmodel.cpp - Kopete Status Model
3 
4  Copyright (c) 2008 by Roman Jarosz <kedgedev@centrum.cz>
5  Kopete (c) 2008 by the Kopete developers <kopete-devel@kde.org>
6 
7  *************************************************************************
8  * *
9  * This library is free software; you can redistribute it and/or *
10  * modify it under the terms of the GNU Lesser General Public *
11  * License as published by the Free Software Foundation; either *
12  * version 2 of the License, or (at your option) any later version. *
13  * *
14  *************************************************************************
15 */
16 #include "statusmodel.h"
17 
18 #include <klocale.h>
19 #include <kicon.h>
20 
21 #include <QtCore/QMimeData>
22 #include <QtXml/QDomDocument>
23 
24 #include "kopetestatusitems.h"
25 #include "kopetestatusmanager.h"
26 
27 KopeteStatusModel::KopeteStatusModel( Kopete::Status::StatusGroup *rootItem, QObject *parent )
28 : QAbstractItemModel( parent ), mRootItem( rootItem )
29 {
30 }
31 
32 KopeteStatusModel::~KopeteStatusModel()
33 {
34 }
35 
36 QVariant KopeteStatusModel::data( const QModelIndex &index, int role ) const
37 {
38  if ( !index.isValid() )
39  return QVariant();
40 
41  QVariant result;
42 
43  switch ( role )
44  {
45  case Qt::DisplayRole:
46  if ( index.column() == 0 )
47  result = getStatusItem( index )->title();
48  break;
49  case Qt::DecorationRole:
50  if ( index.column() == 0 )
51  result = Kopete::OnlineStatusManager::pixmapForCategory( getStatusItem( index )->category() );
52  break;
53  case KopeteStatusModel::Group:
54  result = getStatusItem( index )->isGroup();
55  break;
56  case KopeteStatusModel::Category:
57  result = (int)getStatusItem( index )->category();
58  break;
59  case KopeteStatusModel::Title:
60  result = getStatusItem( index )->title();
61  break;
62  case KopeteStatusModel::Message:
63  {
64  Kopete::Status::Status *s = getStatus( getStatusItem( index ) );
65  if ( s )
66  result = s->message();
67  }
68  break;
69  default:
70  return result;
71  }
72 
73  return result;
74 }
75 
76 bool KopeteStatusModel::setData( const QModelIndex &index, const QVariant &value, int role )
77 {
78  if ( !index.isValid() )
79  return false;
80 
81  switch ( role )
82  {
83  case KopeteStatusModel::Category:
84  getStatusItem( index )->setCategory( (Kopete::OnlineStatusManager::Categories)value.toInt() );
85  break;
86  case KopeteStatusModel::Title:
87  getStatusItem( index )->setTitle( value.toString() );
88  break;
89  case KopeteStatusModel::Message:
90  {
91  Kopete::Status::Status *s = getStatus( getStatusItem( index ) );
92  if ( !s )
93  return false;
94 
95  s->setMessage( value.toString() );
96  }
97  break;
98  default:
99  return false;
100  }
101 
102  emit dataChanged( index, index );
103  emit changed();
104  return true;
105 }
106 
107 
108 Qt::ItemFlags KopeteStatusModel::flags( const QModelIndex &index ) const
109 {
110  if ( !index.isValid() )
111  return Qt::ItemIsDropEnabled;
112 
113  if ( getStatusItem( index )->isGroup() )
114  return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
115  else
116  return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled;
117 }
118 
119 QVariant KopeteStatusModel::headerData( int section, Qt::Orientation orientation, int role ) const
120 {
121  if ( orientation == Qt::Horizontal && role == Qt::DisplayRole && section == 0 )
122  return i18n( "Title" );
123 
124  return QVariant();
125 }
126 
127 QModelIndex KopeteStatusModel::index( int row, int column, const QModelIndex &parent ) const
128 {
129  if ( !hasIndex( row, column, parent ) )
130  return QModelIndex();
131 
132  Kopete::Status::StatusItem *childItem = getStatusItem( parent )->child( row );
133  if ( childItem )
134  return createIndex( row, column, childItem );
135  else
136  return QModelIndex();
137 }
138 
139 QModelIndex KopeteStatusModel::parent( const QModelIndex &index ) const
140 {
141  if ( !index.isValid() )
142  return QModelIndex();
143 
144  Kopete::Status::StatusItem *parentItem = getStatusItem( index )->parentGroup();
145  if ( parentItem == mRootItem )
146  return QModelIndex();
147 
148  return createIndex( parentItem->index(), 0, parentItem );
149 }
150 
151 int KopeteStatusModel::rowCount( const QModelIndex &parent ) const
152 {
153  if ( parent.column() > 0 )
154  return 0;
155 
156  return getStatusItem( parent )->childCount();
157 }
158 
159 int KopeteStatusModel::columnCount( const QModelIndex & ) const
160 {
161  return 1;
162 }
163 
164 Qt::DropActions KopeteStatusModel::supportedDropActions() const
165 {
166  return Qt::MoveAction;
167 }
168 
169 QModelIndex KopeteStatusModel::insertItem( const QModelIndex &index, Kopete::Status::StatusItem *item )
170 {
171  int row = 0;
172  QModelIndex parentIndex;
173  if ( index.isValid() )
174  {
175  // Don't create nasted groups
176  if ( getStatusItem( index )->isGroup() && !item->isGroup() )
177  {
178  parentIndex = index;
179  }
180  else
181  {
182  parentIndex = index.parent();
183  row = index.row() + 1;
184  }
185  }
186 
187  Kopete::Status::StatusGroup *group = getGroup( getStatusItem( parentIndex ) );
188  if ( !group )
189  return QModelIndex();
190 
191  emit layoutAboutToBeChanged();
192  beginInsertRows( parentIndex, row, row );
193 
194  group->insertChild( row, item );
195 
196  endInsertRows();
197  emit layoutChanged();
198 
199  emit changed();
200  return this->index(row, 0, parentIndex);
201 }
202 
203 bool KopeteStatusModel::removeRows( int row, int count, const QModelIndex &parent )
204 {
205  if ( count == 0 )
206  return false;
207 
208  Kopete::Status::StatusGroup *group = getGroup( getStatusItem( parent ) );
209  if ( !group )
210  return false;
211 
212  emit layoutAboutToBeChanged();
213  beginRemoveRows( parent, row, row + count - 1 );
214 
215  while( (count--) > 0 )
216  delete group->child( row );
217 
218  endRemoveRows();
219  emit layoutChanged();
220  emit changed();
221  return true;
222 }
223 
224 QStringList KopeteStatusModel::mimeTypes() const
225 {
226  QStringList types;
227  types << "application/xml-kopete-status";
228  return types;
229 }
230 
231 QMimeData *KopeteStatusModel::mimeData( const QModelIndexList &indexes ) const
232 {
233  using namespace Kopete;
234  QMimeData *mimeData = new QMimeData();
235  QByteArray encodedData;
236 
237  QDataStream stream( &encodedData, QIODevice::WriteOnly );
238 
239  foreach ( const QModelIndex &index, indexes )
240  {
241  if ( index.isValid() && index.column() == 0 )
242  {
243  Status::StatusItem *item = getStatusItem( index );
244  QDomDocument doc( QString::fromLatin1( "kopete-status" ) );
245  doc.appendChild( StatusManager::storeStatusItem( item ) );
246  stream << doc.toString();
247  }
248  }
249 
250  mimeData->setData( "application/xml-kopete-status", encodedData );
251  return mimeData;
252 }
253 
254 
255 bool KopeteStatusModel::dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent )
256 {
257  if ( action == Qt::IgnoreAction )
258  return true;
259 
260  if ( !data->hasFormat( "application/xml-kopete-status" ) )
261  return false;
262 
263  if ( column > 0 )
264  return false;
265 
266  int beginRow;
267 
268  if ( row != -1 )
269  beginRow = row;
270  else if ( parent.isValid() )
271  beginRow = parent.row();
272  else
273  beginRow = rowCount( QModelIndex() );
274 
275  QByteArray encodedData = data->data( "application/xml-kopete-status" );
276  QDataStream stream( &encodedData, QIODevice::ReadOnly );
277 
278  using namespace Kopete;
279  Status::StatusGroup *parentItem = getGroup( getStatusItem( parent ) );
280  if ( !parentItem )
281  return false;
282 
283  QStringList newItems;
284  int rows = 0;
285 
286  while ( !stream.atEnd() ) {
287  QString text;
288  stream >> text;
289  newItems << text;
290  ++rows;
291  }
292 
293  emit layoutAboutToBeChanged();
294  for ( int i = 0; i < newItems.size(); ++i )
295  {
296  QDomDocument doc;
297  doc.setContent( newItems.at(i) );
298  if ( !doc.isNull() )
299  {
300  Status::StatusItem *item = StatusManager::parseStatusItem( doc.documentElement() );
301 
302  QDomDocument doc2( QString::fromLatin1( "kopete-status" ) );
303  doc2.appendChild( StatusManager::storeStatusItem( item ) );
304 
305  // Don't create nasted groups
306  if ( item->isGroup() && parentItem != mRootItem )
307  {
308  int parentRow = parent.row();
309  beginInsertRows( parent.parent(), parentRow, parentRow );
310  parentItem->parentGroup()->insertChild( parentRow, item );
311  endInsertRows();
312  }
313  else
314  {
315  beginInsertRows( parent, beginRow, beginRow );
316  parentItem->insertChild( beginRow++, item );
317  endInsertRows();
318  }
319  }
320  }
321  emit layoutChanged();
322  emit changed();
323  return true;
324 }
325 
326 Kopete::Status::StatusItem *KopeteStatusModel::getStatusItem( const QModelIndex &index ) const
327 {
328  if ( !index.isValid() )
329  return mRootItem;
330 
331  return static_cast<Kopete::Status::StatusItem*>( index.internalPointer() );
332 }
333 
334 Kopete::Status::Status *KopeteStatusModel::getStatus( Kopete::Status::StatusItem *item ) const
335 {
336  if ( !item )
337  return 0;
338 
339  return qobject_cast<Kopete::Status::Status*>( item );
340 }
341 
342 Kopete::Status::StatusGroup *KopeteStatusModel::getGroup( Kopete::Status::StatusItem *item ) const
343 {
344  if ( !item )
345  return 0;
346 
347  return qobject_cast<Kopete::Status::StatusGroup*>( item );
348 }
349 
350 #include "statusmodel.moc"
KopeteStatusModel::parent
virtual QModelIndex parent(const QModelIndex &index) const
Definition: statusmodel.cpp:139
KopeteStatusModel::index
virtual QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Definition: statusmodel.cpp:127
KopeteStatusModel::Message
Definition: statusmodel.h:44
KopeteStatusModel::flags
virtual Qt::ItemFlags flags(const QModelIndex &index) const
Definition: statusmodel.cpp:108
KopeteStatusModel::supportedDropActions
virtual Qt::DropActions supportedDropActions() const
Definition: statusmodel.cpp:164
statusmodel.h
KopeteStatusModel::removeRows
virtual bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex())
Definition: statusmodel.cpp:203
KopeteStatusModel::Category
Definition: statusmodel.h:42
QObject
KopeteStatusModel::headerData
virtual QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: statusmodel.cpp:119
KopeteStatusModel::data
virtual QVariant data(const QModelIndex &index, int role) const
Definition: statusmodel.cpp:36
KopeteStatusModel::Group
Definition: statusmodel.h:45
KopeteStatusModel::mimeTypes
QStringList mimeTypes() const
Definition: statusmodel.cpp:224
QAbstractItemModel
KopeteStatusModel::mimeData
QMimeData * mimeData(const QModelIndexList &indexes) const
Definition: statusmodel.cpp:231
KopeteStatusModel::setData
virtual bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Definition: statusmodel.cpp:76
KopeteStatusModel::~KopeteStatusModel
~KopeteStatusModel()
Definition: statusmodel.cpp:32
KopeteStatusModel::columnCount
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
Definition: statusmodel.cpp:159
KopeteStatusModel::insertItem
QModelIndex insertItem(const QModelIndex &index, Kopete::Status::StatusItem *item)
Definition: statusmodel.cpp:169
KopeteStatusModel::Title
Definition: statusmodel.h:43
KopeteStatusModel::KopeteStatusModel
KopeteStatusModel(Kopete::Status::StatusGroup *rootItem, QObject *parent=0)
Definition: statusmodel.cpp:27
KopeteStatusModel::rowCount
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: statusmodel.cpp:151
KopeteStatusModel::dropMimeData
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
Definition: statusmodel.cpp:255
KopeteStatusModel::changed
void changed()
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:53:41 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kopete/kopete

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

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

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