24 #include <QStringList>
26 using namespace MailCommon;
28 class MailCommon::SnippetItem
31 SnippetItem(
bool isGroup =
false, SnippetItem *parent = 0 );
36 void setName(
const QString &name );
39 void setText(
const QString &
text );
42 void setKeySequence(
const QString &sequence );
43 QString keySequence()
const;
45 void appendChild( SnippetItem *child );
46 void removeChild( SnippetItem *child );
47 SnippetItem *child(
int row )
const;
48 int childCount()
const;
50 SnippetItem *parent()
const;
54 SnippetItem *mParentItem;
62 SnippetItem::SnippetItem(
bool isGroup, SnippetItem *parent )
63 : mParentItem( parent ), mIsGroup( isGroup )
67 SnippetItem::~SnippetItem()
69 qDeleteAll( mChildItems );
73 bool SnippetItem::isGroup()
const
78 void SnippetItem::setName(
const QString &name )
83 QString SnippetItem::name()
const
88 void SnippetItem::setText(
const QString &
text )
98 void SnippetItem::setKeySequence(
const QString &sequence )
100 mKeySequence = sequence;
103 QString SnippetItem::keySequence()
const
108 void SnippetItem::appendChild( SnippetItem *item )
110 mChildItems.append( item );
113 void SnippetItem::removeChild( SnippetItem *item )
115 mChildItems.removeAll( item );
119 SnippetItem *SnippetItem::child(
int row )
const
121 return mChildItems.value( row );
124 int SnippetItem::childCount()
const
126 return mChildItems.count();
129 SnippetItem *SnippetItem::parent()
const
134 int SnippetItem::row()
const
137 return mParentItem->mChildItems.indexOf( const_cast<SnippetItem*>(
this ) );
146 mRootItem =
new SnippetItem(
true );
148 QHash<int, QByteArray> names = roleNames();
152 setRoleNames( names );
167 if ( !index.isValid() ) {
171 SnippetItem *item =
static_cast<SnippetItem*
>( index.internalPointer() );
176 item->setName( value.toString() );
177 emit dataChanged( index, index );
181 item->setText( value.toString() );
182 emit dataChanged( index, index );
186 item->setKeySequence( value.toString() );
187 emit dataChanged( index, index );
200 if ( !index.isValid() ) {
204 SnippetItem *item =
static_cast<SnippetItem*
>( index.internalPointer() );
207 case Qt::DisplayRole:
211 return item->isGroup();
220 return item->keySequence();
229 Qt::ItemFlags defaultFlags = QAbstractItemModel::flags( index );
231 if ( index.isValid() ) {
232 const SnippetItem *item =
static_cast<SnippetItem*
>( index.internalPointer() );
233 if ( !item->isGroup() ) {
234 return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
238 return Qt::ItemIsDropEnabled | defaultFlags;
243 if ( !hasIndex( row, column, parent ) ) {
244 return QModelIndex();
247 SnippetItem *parentItem;
249 if ( !parent.isValid() ) {
250 parentItem = mRootItem;
252 parentItem =
static_cast<SnippetItem*
>( parent.internalPointer() );
255 SnippetItem *childItem = parentItem->child( row );
257 return createIndex( row, column, childItem );
259 return QModelIndex();
265 if ( !index.isValid() ) {
266 return QModelIndex();
269 SnippetItem *childItem =
static_cast<SnippetItem*
>( index.internalPointer() );
270 SnippetItem *parentItem = childItem->parent();
272 if ( parentItem == mRootItem ) {
273 return QModelIndex();
276 return createIndex( parentItem->row(), 0, parentItem );
281 SnippetItem *parentItem;
282 if ( parent.column() > 0 ) {
286 if ( !parent.isValid() ) {
287 parentItem = mRootItem;
289 parentItem =
static_cast<SnippetItem*
>( parent.internalPointer() );
292 return parentItem->childCount();
297 SnippetItem *parentItem;
299 if ( !parent.isValid() ) {
300 parentItem = mRootItem;
302 parentItem =
static_cast<SnippetItem*
>( parent.internalPointer() );
305 beginInsertRows( parent, row, row + count - 1 );
306 for (
int i = 0; i < count; ++i ) {
307 SnippetItem * snippet =
new SnippetItem( !parent.isValid(), parentItem );
308 parentItem->appendChild( snippet );
317 SnippetItem *parentItem;
319 if ( !parent.isValid() ) {
320 parentItem = mRootItem;
322 parentItem =
static_cast<SnippetItem*
>( parent.internalPointer() );
325 beginRemoveRows( parent, row, row + count - 1 );
326 for (
int i = 0; i < count; ++i ) {
327 parentItem->removeChild( parentItem->child( row ) );
337 QStringList() << QLatin1String(
"text/x-kmail-textsnippet" )
338 << QLatin1String(
"text/plain" );
343 if ( indexes.isEmpty() ) {
347 const QModelIndex
index = indexes.first();
349 SnippetItem *item =
static_cast<SnippetItem*
>( index.internalPointer() );
350 if ( item->isGroup() ) {
354 QMimeData *
mimeData =
new QMimeData();
356 QByteArray encodedData;
357 QDataStream stream( &encodedData, QIODevice::WriteOnly );
358 stream << index.parent().internalId()<< item->name() << item->text() <<item->keySequence();
360 mimeData->setData( QLatin1String(
"text/x-kmail-textsnippet" ), encodedData );
361 mimeData->setText( item->text() );
367 int row,
int column,
const QModelIndex &parent )
371 if ( action == Qt::IgnoreAction ) {
375 if ( !parent.isValid() ) {
379 if ( !data->hasFormat( QLatin1String(
"text/x-kmail-textsnippet" ) ) ) {
387 SnippetItem *item =
static_cast<SnippetItem*
>( parent.internalPointer() );
389 if ( !item->isGroup() ) {
393 QByteArray encodedData = data->data( QLatin1String(
"text/x-kmail-textsnippet" ) );
394 QDataStream stream( &encodedData, QIODevice::ReadOnly );
400 stream >>
id >> name >> text >> keySequence;
402 if ( parent.internalId() ==
id ) {
406 insertRow(
rowCount( parent ), parent );
408 const QModelIndex idx =
index(
rowCount( parent )-1, 0, parent );
419 return Qt::CopyAction|Qt::MoveAction;
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
virtual bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Returns whether the index represents a group.
The key sequence to activate a snippet.
virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
virtual Qt::ItemFlags flags(const QModelIndex &index) const
The name of a snippet or group.
virtual bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex())
virtual QModelIndex parent(const QModelIndex &index) const
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
virtual bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex())
virtual QStringList mimeTypes() const
SnippetsModel(QObject *parent=0)
virtual QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
virtual QVariant data(const QModelIndex &index, int role) const
virtual QMimeData * mimeData(const QModelIndexList &indexes) const
virtual Qt::DropActions supportedDropActions() const