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

kleopatra

  • sources
  • kde-4.12
  • kdepim
  • kleopatra
  • utils
dragqueen.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  utils/dragqueen.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2007 Klarälvdalens Datakonsult AB
6 
7  Kleopatra is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  Kleopatra is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  In addition, as a special exception, the copyright holders give
22  permission to link the code of this program with any edition of
23  the Qt library by Trolltech AS, Norway (or with modified versions
24  of Qt that use the same license as Qt), and distribute linked
25  combinations including the two. You must obey the GNU General
26  Public License in all respects for all of the code used other than
27  Qt. If you modify this file, you may extend this exception to
28  your version of the file, but you are not obligated to do so. If
29  you do not wish to do so, delete this exception statement from
30  your version.
31 */
32 
33 
34 #include <config-kleopatra.h>
35 
36 #include "dragqueen.h"
37 
38 
39 #include <QDrag>
40 #include <QMouseEvent>
41 #include <QStringList>
42 #include <QVariant>
43 #include <QApplication>
44 #include <QUrl>
45 #include <QStyle>
46 
47 #include <algorithm>
48 
49 using namespace Kleo;
50 
51 namespace {
52  class MimeDataProxy : public QMimeData {
53  Q_OBJECT
54  public:
55  explicit MimeDataProxy( QMimeData * source )
56  : QMimeData(), m_source( source )
57  {
58 
59  }
60 
61  /* reimp */ QStringList formats() const {
62  if ( m_source )
63  return m_source->formats();
64  else
65  return QStringList();
66  }
67 
68  /* reimp */ bool hasFormat( const QString & format ) const {
69  return m_source && m_source->hasFormat( format );
70  }
71 
72  protected:
73  /* reimp */ QVariant retrieveData( const QString & format, QVariant::Type type ) const {
74  if ( !m_source )
75  return QVariant();
76  // Doesn't work, is protected:
77  // return m_source->retrieveData( format, type );
78 
79  switch ( type ) {
80  case QVariant::String:
81  if ( format == QLatin1String( "text/plain" ) )
82  return m_source->text();
83  if ( format == QLatin1String( "text/html" ) )
84  return m_source->html();
85  break;
86  case QVariant::Color:
87  if ( format == QLatin1String( "application/x-color" ) )
88  return m_source->colorData();
89  break;
90  case QVariant::Image:
91  if ( format == QLatin1String( "application/x-qt-image" ) )
92  return m_source->imageData();
93  break;
94  case QVariant::List:
95  case QVariant::Url:
96  if ( format == QLatin1String( "text/uri-list" ) ) {
97  const QList<QUrl> urls = m_source->urls();
98  if ( urls.size() == 1 )
99  return urls.front();
100  QList<QVariant> result;
101  std::copy( urls.begin(), urls.end(),
102  std::back_inserter( result ) );
103  return result;
104  }
105  break;
106  default:
107  break;
108  }
109 
110  QVariant v = m_source->data( format );
111  v.convert( type );
112  return v;
113  }
114  private:
115  QPointer<QMimeData> m_source;
116  };
117 }
118 
119 
120 DragQueen::DragQueen( QWidget * p, Qt::WindowFlags f )
121  : QLabel( p, f ),
122  m_data(),
123  m_dragStartPosition()
124 {
125 
126 }
127 
128 DragQueen::DragQueen( const QString & t, QWidget * p, Qt::WindowFlags f )
129  : QLabel( t, p, f ),
130  m_data(),
131  m_dragStartPosition()
132 {
133 
134 }
135 
136 DragQueen::~DragQueen() {
137  delete m_data;
138 }
139 
140 void DragQueen::setUrl( const QString & url ) {
141  QMimeData * data = new QMimeData;
142  QList<QUrl> urls;
143  urls.push_back( QUrl( url ) );
144  data->setUrls( urls );
145  setMimeData( data );
146 }
147 
148 QString DragQueen::url() const {
149  if ( !m_data || !m_data->hasUrls() )
150  return QString();
151  const QList<QUrl> urls = m_data->urls();
152  if ( urls.empty() )
153  return QString();
154  return urls.front().toString();
155 }
156 
157 void DragQueen::setMimeData( QMimeData * data ) {
158  if ( data == m_data )
159  return;
160  delete m_data;
161  m_data = data;
162 }
163 
164 QMimeData * DragQueen::mimeData() const {
165  return m_data;
166 }
167 
168 void DragQueen::mousePressEvent( QMouseEvent * e ) {
169 #ifndef QT_NO_DRAGANDDROP
170  if ( m_data && e->button() == Qt::LeftButton )
171  m_dragStartPosition = e->pos();
172 #endif
173  QLabel::mousePressEvent( e );
174 }
175 
176 static QPoint calculate_hot_spot( const QPoint & mouse, const QSize & pix, const QLabel * label ) {
177  const Qt::Alignment align = label->alignment();
178  const int margin = label->margin();
179  const QRect cr = label->contentsRect().adjusted( margin, margin, -margin, -margin );
180  const QRect rect = QStyle::alignedRect( QApplication::layoutDirection(), align, pix, cr );
181  return mouse - rect.topLeft();
182 }
183 
184 void DragQueen::mouseMoveEvent( QMouseEvent * e ) {
185 #ifndef QT_NO_DRAGANDDROP
186  if ( m_data &&
187  (e->buttons() & Qt::LeftButton) &&
188  ( m_dragStartPosition - e->pos() ).manhattanLength() > QApplication::startDragDistance() ) {
189  QDrag * drag = new QDrag( this );
190  if ( const QPixmap * const pix = pixmap() ) {
191  drag->setPixmap( *pix );
192  drag->setHotSpot( calculate_hot_spot( e->pos(), pix->size(), this ) );
193  }
194  drag->setMimeData( new MimeDataProxy( m_data ) );
195  drag->exec();
196  } else {
197 #endif
198  QLabel::mouseMoveEvent( e );
199 #ifndef QT_NO_DRAGANDDROP
200  }
201 #endif
202 }
203 
204 #include "moc_dragqueen.cpp"
205 #include "dragqueen.moc"
Kleo::DragQueen::~DragQueen
~DragQueen()
Definition: dragqueen.cpp:136
calculate_hot_spot
static QPoint calculate_hot_spot(const QPoint &mouse, const QSize &pix, const QLabel *label)
Definition: dragqueen.cpp:176
Kleo::Formatting::type
QString type(const GpgME::Key &key)
Kleo::DragQueen::DragQueen
DragQueen(QWidget *p=0, Qt::WindowFlags f=0)
Definition: dragqueen.cpp:120
Kleo::DragQueen::setMimeData
void setMimeData(QMimeData *md)
Definition: dragqueen.cpp:157
QWidget
Kleo::DragQueen::mimeData
QMimeData * mimeData() const
Definition: dragqueen.cpp:164
Kleo::DragQueen::mouseMoveEvent
void mouseMoveEvent(QMouseEvent *)
Definition: dragqueen.cpp:184
Kleo::DragQueen::mousePressEvent
void mousePressEvent(QMouseEvent *)
Definition: dragqueen.cpp:168
Kleo::DragQueen::url
QString url() const
dragqueen.h
Kleo::DragQueen::setUrl
void setUrl(const QString &url)
Definition: dragqueen.cpp:140
QList
Definition: commands/command.h:46
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:56:41 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kleopatra

Skip menu "kleopatra"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

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