• 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.14
  • kdenetwork
  • kopete
  • kopete
  • config
  • appearance
  • layout
TokenPool.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2008 Teo Mrnjavac <teo.mrnjavac@gmail.com> *
3  * 2009 Seb Ruiz <ruiz@kde.org> *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17  ******************************************************************************/
18 #include "TokenPool.h"
19 
20 #include <KApplication>
21 
22 #include <QMouseEvent>
23 
24 
25 TokenPool::TokenPool( QWidget *parent )
26  : KListWidget( parent )
27 {
28  setAcceptDrops( true );
29 }
30 
31 void
32 TokenPool::addToken( Token * token )
33 {
34 
35  QListWidgetItem *item = new QListWidgetItem( token->icon().pixmap( 48, 48 ), token->name() );
36  addItem( item );
37 
38  m_itemTokenMap.insert( item, token );
39 }
40 
41 QString
42 TokenPool::mimeType() const
43 {
44  return m_mimeType;
45 }
46 
47 void
48 TokenPool::setMimeType( const QString& mimeType )
49 {
50  m_mimeType = mimeType;
51 }
52 
53 // Executed on doubleclick of the TokenPool, emits signal onDoubleClick( QString )
54 // that connects to TokenLayoutWidget::addToken( QString )
55 void
56 TokenPool::mouseDoubleClickEvent( QMouseEvent *event )
57 {
58  QListWidgetItem *tokenItem = itemAt( event->pos() );
59  if( tokenItem )
60  emit onDoubleClick( m_itemTokenMap.value( tokenItem ) ); //token->name() << token->iconName() << token->value()
61 }
62 
63 //Executed on mouse press, handles start of drag.
64 void
65 TokenPool::mousePressEvent( QMouseEvent *event )
66 {
67  if( event->button() == Qt::LeftButton )
68  m_startPos = event->pos(); //store the start position
69  KListWidget::mousePressEvent( event ); //feed it to parent's event
70 }
71 
72 //Executed on mouse move, handles start of drag.
73 void
74 TokenPool::mouseMoveEvent( QMouseEvent *event )
75 {
76  if( event->buttons() & Qt::LeftButton )
77  {
78  int distance = ( event->pos() - m_startPos ).manhattanLength();
79  if ( distance >= KApplication::startDragDistance() )
80  performDrag( event );
81  }
82  KListWidget::mouseMoveEvent( event );
83 }
84 
85 //This doesn't do much since TokenPool doesn't accept objects.
86 void
87 TokenPool::dragEnterEvent( QDragEnterEvent *event )
88 {
89  QWidget *source = qobject_cast<QWidget *>( event->source() );
90  if ( source && source != this )
91  {
92  event->setDropAction( Qt::MoveAction );
93  event->accept();
94  }
95 }
96 
97 //Same as above.
98 void
99 TokenPool::dragMoveEvent( QDragMoveEvent *event ) //overrides QListWidget's implementation
100 {
101  QWidget *source = qobject_cast<QWidget *>( event->source() );
102  if ( source && source != this )
103  {
104  event->setDropAction( Qt::MoveAction );
105  event->accept();
106  }
107 }
108 
109 //Same as above.
110 void
111 TokenPool::dropEvent( QDropEvent *event )
112 {
113  Q_UNUSED( event )
114  //does nothing, I want the item to be deleted and not dragged here
115 }
116 
117 //Handles the creation of a QDrag object that carries the (text-only) QDataStream from an item in TokenPool
118 void
119 TokenPool::performDrag( QMouseEvent *event )
120 {
121  QListWidgetItem *item = currentItem();
122 
123  if( item )
124  {
125  Token *token = m_itemTokenMap.value( item );
126  QByteArray itemData;
127 
128  QDataStream dataStream( &itemData, QIODevice::WriteOnly );
129  dataStream << token->name() << token->iconName() << token->value() << QPoint( event->pos() - rect().topLeft() );
130 
131  QMimeData *mimeData = new QMimeData;
132  mimeData->setData( m_mimeType, itemData );
133 
134  QDrag *drag = new QDrag( this );
135  drag->setMimeData( mimeData );
136 
137  //TODO: set a pointer for the drag, like this: drag->setPixmap( QPixmap("foo.png" ) );
138  drag->exec( Qt::MoveAction | Qt::CopyAction, Qt::CopyAction );
139  }
140 }
141 
142 #include "TokenPool.moc"
QWidget
TokenPool::dropEvent
void dropEvent(QDropEvent *event)
Definition: TokenPool.cpp:111
QDrag::setMimeData
void setMimeData(QMimeData *data)
QByteArray
QDragMoveEvent
QDataStream
QListWidgetItem
QPoint
QMouseEvent
QMouseEvent::buttons
Qt::MouseButtons buttons() const
Token::value
int value() const
Definition: Token.cpp:83
TokenPool.h
TokenPool::mousePressEvent
void mousePressEvent(QMouseEvent *event)
Definition: TokenPool.cpp:65
TokenPool::addToken
void addToken(Token *token)
Definition: TokenPool.cpp:32
TokenPool::TokenPool
TokenPool(QWidget *parent=0)
Definition: TokenPool.cpp:25
QMimeData
QDrag::exec
Qt::DropAction exec(QFlags< Qt::DropAction > supportedActions)
Token::name
QString name() const
Definition: Token.cpp:77
QMouseEvent::button
Qt::MouseButton button() const
QDropEvent
Token::icon
KIcon icon() const
Definition: Token.cpp:89
QDrag
QString
TokenPool::mimeType
QString mimeType() const
TokenPool::onDoubleClick
void onDoubleClick(Token *token)
QDragEnterEvent
TokenPool::mouseDoubleClickEvent
void mouseDoubleClickEvent(QMouseEvent *event)
Definition: TokenPool.cpp:56
QMap::insert
iterator insert(const Key &key, const T &value)
TokenPool::mouseMoveEvent
void mouseMoveEvent(QMouseEvent *event)
Definition: TokenPool.cpp:74
QMimeData::setData
void setData(const QString &mimeType, const QByteArray &data)
QMouseEvent::pos
const QPoint & pos() const
Token
Definition: Token.h:38
KListWidget
TokenPool::setMimeType
void setMimeType(const QString &mimeType)
Definition: TokenPool.cpp:48
TokenPool::dragMoveEvent
void dragMoveEvent(QDragMoveEvent *event)
Definition: TokenPool.cpp:99
Token::iconName
QString iconName() const
Definition: Token.cpp:94
TokenPool::dragEnterEvent
void dragEnterEvent(QDragEnterEvent *event)
Definition: TokenPool.cpp:87
QMap::value
const T value(const Key &key) const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:29:08 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