~ubuntu-branches/ubuntu/wily/qgis/wily

« back to all changes in this revision

Viewing changes to src/gui/qgsfiledropedit.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
    qgsfiledropedit.cpp - File Dropable LineEdit
 
3
     --------------------------------------
 
4
    Date                 : 31-Jan-2007
 
5
    Copyright            : (C) 2007 by Tom Elwertowski
 
6
    Email                : telwertowski at users dot sourceforge dot net
 
7
 ***************************************************************************
 
8
 *                                                                         *
 
9
 *   This program is free software; you can redistribute it and/or modify  *
 
10
 *   it under the terms of the GNU General Public License as published by  *
 
11
 *   the Free Software Foundation; either version 2 of the License, or     *
 
12
 *   (at your option) any later version.                                   *
 
13
 *                                                                         *
 
14
 ***************************************************************************/
 
15
/* $Id$ */
 
16
 
 
17
#include "qgsfiledropedit.h"
 
18
#include <QDropEvent>
 
19
#include <QFileInfo>
 
20
#include <QPainter>
 
21
#include <QUrl>
 
22
 
 
23
/*!
 
24
  \class QgsFileDropEdit
 
25
 
 
26
  \brief The QgsDropNameEdit class provides a line edit widget which
 
27
  accepts file drops.
 
28
 
 
29
  Dropping can be limited to files only, files with a specific extension
 
30
  or directories only. By default, dropping is limited to files only.
 
31
*/
 
32
 
 
33
QgsFileDropEdit::QgsFileDropEdit( QWidget *parent )
 
34
    : QLineEdit( parent )
 
35
{
 
36
  mDirOnly = false;
 
37
  mFileOnly = true;
 
38
  mDragActive = false;
 
39
  setAcceptDrops( true );
 
40
}
 
41
 
 
42
QgsFileDropEdit::~QgsFileDropEdit()
 
43
{}
 
44
 
 
45
/*!
 
46
  Limit drops to directories.
 
47
*/
 
48
void QgsFileDropEdit::setDirOnly( bool isDirOnly )
 
49
{
 
50
  mDirOnly = isDirOnly;
 
51
  if ( mDirOnly )
 
52
  {
 
53
    mFileOnly = false;
 
54
  }
 
55
}
 
56
 
 
57
/*!
 
58
  Limit drops to files.
 
59
*/
 
60
void QgsFileDropEdit::setFileOnly( bool isFileOnly )
 
61
{
 
62
  mFileOnly = isFileOnly;
 
63
  if ( mFileOnly )
 
64
  {
 
65
    mDirOnly = false;
 
66
  }
 
67
}
 
68
 
 
69
/*!
 
70
  Limit drops to files with specified extension.
 
71
*/
 
72
void QgsFileDropEdit::setSuffixFilter( const QString& suffix )
 
73
{
 
74
  mSuffix = suffix;
 
75
}
 
76
 
 
77
/*!
 
78
  Return file name if object meets drop criteria.
 
79
*/
 
80
QString QgsFileDropEdit::acceptableFilePath( QDropEvent *event ) const
 
81
{
 
82
  QString path;
 
83
  if ( event->mimeData()->hasUrls() )
 
84
  {
 
85
    QFileInfo file( event->mimeData()->urls().first().toLocalFile() );
 
86
    if ( !(( mFileOnly && !file.isFile() ) ||
 
87
           ( mDirOnly && !file.isDir() ) ||
 
88
           ( !mSuffix.isEmpty() && mSuffix.compare( file.suffix(), Qt::CaseInsensitive ) ) ) )
 
89
      path = file.filePath();
 
90
  }
 
91
  return path;
 
92
}
 
93
 
 
94
/*!
 
95
  Check if dragged object is acceptible. Called when a drag is in progress
 
96
  and the mouse enters this widget.
 
97
*/
 
98
void QgsFileDropEdit::dragEnterEvent( QDragEnterEvent *event )
 
99
{
 
100
  QString filePath = acceptableFilePath( event );
 
101
  if ( !filePath.isEmpty() )
 
102
  {
 
103
    event->acceptProposedAction();
 
104
    mDragActive = true;
 
105
    update();
 
106
  }
 
107
  else
 
108
  {
 
109
    QLineEdit::dragEnterEvent( event );
 
110
  }
 
111
}
 
112
 
 
113
/*!
 
114
  Called when a drag is in progress and the mouse leaves this widget.
 
115
*/
 
116
void QgsFileDropEdit::dragLeaveEvent( QDragLeaveEvent *event )
 
117
{
 
118
  QLineEdit::dragLeaveEvent( event );
 
119
  event->accept();
 
120
  mDragActive = false;
 
121
  update();
 
122
}
 
123
 
 
124
/*!
 
125
  Receive the dragged object. Called when the drag is dropped on this widget.
 
126
*/
 
127
void QgsFileDropEdit::dropEvent( QDropEvent *event )
 
128
{
 
129
  QString filePath = acceptableFilePath( event );
 
130
  if ( !filePath.isEmpty() )
 
131
  {
 
132
    setText( filePath );
 
133
    selectAll();
 
134
    setFocus( Qt::MouseFocusReason );
 
135
    event->acceptProposedAction();
 
136
    mDragActive = false;
 
137
    update();
 
138
  }
 
139
  else
 
140
  {
 
141
    QLineEdit::dropEvent( event );
 
142
  }
 
143
}
 
144
 
 
145
/*!
 
146
  Paints line edit with drag highlight in response to a paint event.
 
147
*/
 
148
void QgsFileDropEdit::paintEvent( QPaintEvent *e )
 
149
{
 
150
  QLineEdit::paintEvent( e );
 
151
  if ( mDragActive )
 
152
  {
 
153
    QPainter p( this );
 
154
    int width = 2;  // width of highlight rectangle inside frame
 
155
    p.setPen( QPen( palette().highlight(), width ) );
 
156
    QRect r = rect().adjusted( width, width, -width, -width );
 
157
    p.drawRect( r );
 
158
  }
 
159
}