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

KFile

  • sources
  • kde-4.14
  • kdelibs
  • kfile
kurlnavigatorplacesselector.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) *
3  * Copyright (C) 2007 by Kevin Ottens (ervin@kde.org) *
4  * *
5  * This library is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2 of the License, or (at your option) any later version. *
9  * *
10  * This library 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 GNU *
13  * Lesser General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Lesser General Public *
16  * License along with this library; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19  ***************************************************************************/
20 
21 #include "kurlnavigatorplacesselector_p.h"
22 
23 #include <kiconloader.h>
24 #include <kglobalsettings.h>
25 #include <kfileplacesmodel.h>
26 #include <kmenu.h>
27 #include <kmimetype.h>
28 #include <kdebug.h>
29 
30 #include <QtGui/QDragEnterEvent>
31 #include <QtGui/QDragLeaveEvent>
32 #include <QtGui/QDropEvent>
33 #include <QtGui/QPainter>
34 #include <QtGui/QPixmap>
35 #include <kicon.h>
36 
37 namespace KDEPrivate
38 {
39 
40 KUrlNavigatorPlacesSelector::KUrlNavigatorPlacesSelector(QWidget* parent, KFilePlacesModel* placesModel) :
41  KUrlNavigatorButtonBase(parent),
42  m_selectedItem(-1),
43  m_placesModel(placesModel)
44 {
45  setFocusPolicy(Qt::NoFocus);
46 
47  m_placesMenu = new KMenu(this);
48 
49  updateMenu();
50 
51  connect(m_placesModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
52  this, SLOT(updateMenu()));
53  connect(m_placesModel, SIGNAL(rowsRemoved(QModelIndex,int,int)),
54  this, SLOT(updateMenu()));
55  connect(m_placesModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
56  this, SLOT(updateMenu()));
57  connect(m_placesMenu, SIGNAL(triggered(QAction*)),
58  this, SLOT(activatePlace(QAction*)));
59 
60  setMenu(m_placesMenu);
61 
62  setAcceptDrops(true);
63 }
64 
65 KUrlNavigatorPlacesSelector::~KUrlNavigatorPlacesSelector()
66 {
67 }
68 
69 void KUrlNavigatorPlacesSelector::updateMenu()
70 {
71  m_placesMenu->clear();
72 
73  updateSelection(m_selectedUrl);
74  const int rowCount = m_placesModel->rowCount();
75  for (int i = 0; i < rowCount; ++i) {
76  QModelIndex index = m_placesModel->index(i, 0);
77  QAction* action = new QAction(m_placesModel->icon(index),
78  m_placesModel->text(index),
79  m_placesMenu);
80  m_placesMenu->addAction(action);
81  action->setData(i);
82  if (i == m_selectedItem) {
83  setIcon(m_placesModel->icon(index));
84  }
85  }
86 
87  updateTeardownAction();
88 }
89 
90 void KUrlNavigatorPlacesSelector::updateTeardownAction()
91 {
92  const int rowCount = m_placesModel->rowCount();
93  if (m_placesMenu->actions().size()==rowCount+2) {
94  // remove teardown action
95  QAction *action = m_placesMenu->actions().at(rowCount+1);
96  m_placesMenu->removeAction(action);
97  delete action;
98 
99  // remove separator
100  action = m_placesMenu->actions().at(rowCount);
101  m_placesMenu->removeAction(action);
102  delete action;
103  }
104 
105  const QModelIndex index = m_placesModel->index(m_selectedItem, 0);
106  QAction *teardown = m_placesModel->teardownActionForIndex(index);
107  if (teardown!=0) {
108  teardown->setParent(m_placesMenu);
109  teardown->setData("teardownAction");
110 
111  m_placesMenu->addSeparator();
112  m_placesMenu->addAction(teardown);
113  }
114 }
115 
116 void KUrlNavigatorPlacesSelector::updateSelection(const KUrl& url)
117 {
118  const QModelIndex index = m_placesModel->closestItem(url);
119  if (index.isValid()) {
120  m_selectedItem = index.row();
121  m_selectedUrl = url;
122  setIcon(m_placesModel->icon(index));
123  } else {
124  m_selectedItem = -1;
125  // No bookmark has been found which matches to the given Url. Show
126  // a generic folder icon as pixmap for indication:
127  setIcon(KIcon("folder"));
128  }
129  updateTeardownAction();
130 }
131 
132 KUrl KUrlNavigatorPlacesSelector::selectedPlaceUrl() const
133 {
134  const QModelIndex index = m_placesModel->index(m_selectedItem, 0);
135  return index.isValid() ? m_placesModel->url(index) : KUrl();
136 }
137 
138 QString KUrlNavigatorPlacesSelector::selectedPlaceText() const
139 {
140  const QModelIndex index = m_placesModel->index(m_selectedItem, 0);
141  return index.isValid() ? m_placesModel->text(index) : QString();
142 }
143 
144 QSize KUrlNavigatorPlacesSelector::sizeHint() const
145 {
146  const int height = KUrlNavigatorButtonBase::sizeHint().height();
147  return QSize(height, height);
148 }
149 
150 void KUrlNavigatorPlacesSelector::paintEvent(QPaintEvent* event)
151 {
152  Q_UNUSED(event);
153  QPainter painter(this);
154  drawHoverBackground(&painter);
155 
156  // draw icon
157  const QPixmap pixmap = icon().pixmap(QSize(22, 22).expandedTo(iconSize()), QIcon::Normal);
158  const int x = (width() - pixmap.width()) / 2;
159  const int y = (height() - pixmap.height()) / 2;
160  painter.drawPixmap(x, y, pixmap);
161 }
162 
163 void KUrlNavigatorPlacesSelector::dragEnterEvent(QDragEnterEvent* event)
164 {
165  if (event->mimeData()->hasUrls()) {
166  setDisplayHintEnabled(DraggedHint, true);
167  event->acceptProposedAction();
168 
169  update();
170  }
171 }
172 
173 void KUrlNavigatorPlacesSelector::dragLeaveEvent(QDragLeaveEvent* event)
174 {
175  KUrlNavigatorButtonBase::dragLeaveEvent(event);
176 
177  setDisplayHintEnabled(DraggedHint, false);
178  update();
179 }
180 
181 void KUrlNavigatorPlacesSelector::dropEvent(QDropEvent* event)
182 {
183  setDisplayHintEnabled(DraggedHint, false);
184  update();
185 
186  const KUrl::List urlList = KUrl::List::fromMimeData(event->mimeData());
187  if (urlList.isEmpty()) {
188  return;
189  }
190  foreach(const KUrl &url, urlList) {
191  KMimeType::Ptr mimetype = KMimeType::findByUrl(url);
192  if (mimetype->is("inode/directory")) {
193  m_placesModel->addPlace(url.fileName(), url);
194  }
195  }
196 }
197 
198 void KUrlNavigatorPlacesSelector::activatePlace(QAction* action)
199 {
200  Q_ASSERT(action != 0);
201  if (action->data().toString()=="teardownAction") {
202  QModelIndex index = m_placesModel->index(m_selectedItem, 0);
203  m_placesModel->requestTeardown(index);
204  return;
205  }
206 
207  QModelIndex index = m_placesModel->index(action->data().toInt(), 0);
208 
209  m_lastClickedIndex = QPersistentModelIndex();
210 
211  if (m_placesModel->setupNeeded(index)) {
212  connect(m_placesModel, SIGNAL(setupDone(QModelIndex,bool)),
213  this, SLOT(onStorageSetupDone(QModelIndex,bool)));
214 
215  m_lastClickedIndex = index;
216  m_placesModel->requestSetup(index);
217  return;
218  } else if (index.isValid()) {
219  m_selectedItem = index.row();
220  setIcon(m_placesModel->icon(index));
221  updateTeardownAction();
222  emit placeActivated(m_placesModel->url(index));
223  }
224 }
225 
226 void KUrlNavigatorPlacesSelector::onStorageSetupDone(const QModelIndex &index, bool success)
227 {
228  if (m_lastClickedIndex == index) {
229  if (success) {
230  m_selectedItem = index.row();
231  setIcon(m_placesModel->icon(index));
232  updateTeardownAction();
233  emit placeActivated(m_placesModel->url(index));
234  }
235  m_lastClickedIndex = QPersistentModelIndex();
236  }
237 }
238 
239 } // namespace KDEPrivate
240 
241 #include "kurlnavigatorplacesselector_p.moc"
242 
KFilePlacesModel::url
KUrl url(const QModelIndex &index) const
Definition: kfileplacesmodel.cpp:177
QModelIndex
QPushButton::setMenu
void setMenu(QMenu *menu)
QWidget
KDEPrivate::KUrlNavigatorPlacesSelector::dragEnterEvent
virtual void dragEnterEvent(QDragEnterEvent *event)
Definition: kurlnavigatorplacesselector.cpp:163
QPixmap::width
int width() const
kdebug.h
QDropEvent::mimeData
const QMimeData * mimeData() const
KFilePlacesModel::requestSetup
void requestSetup(const QModelIndex &index)
Definition: kfileplacesmodel.cpp:817
QPushButton::sizeHint
virtual QSize sizeHint() const
kmimetype.h
kglobalsettings.h
KDEPrivate::KUrlNavigatorPlacesSelector::updateSelection
void updateSelection(const KUrl &url)
Updates the selection dependent from the given URL url.
Definition: kurlnavigatorplacesselector.cpp:116
QWidget::setFocusPolicy
void setFocusPolicy(Qt::FocusPolicy policy)
QAction::data
QVariant data() const
mimetype
MimetypeJob * mimetype(const KUrl &url, JobFlags flags=DefaultFlags)
KDEPrivate::KUrlNavigatorPlacesSelector::selectedPlaceUrl
KUrl selectedPlaceUrl() const
Returns the selected bookmark.
Definition: kurlnavigatorplacesselector.cpp:132
QWidget::dragLeaveEvent
virtual void dragLeaveEvent(QDragLeaveEvent *event)
KMenu
QList::at
const T & at(int i) const
QMenu::addAction
void addAction(QAction *action)
QWidget::y
int y() const
KFilePlacesModel::requestTeardown
void requestTeardown(const QModelIndex &index)
Definition: kfileplacesmodel.cpp:786
KFilePlacesModel::index
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Get the children model index for the given row and column.
Definition: kfileplacesmodel.cpp:249
KDEPrivate::KUrlNavigatorButtonBase
Base class for buttons of the URL navigator.
Definition: kurlnavigatorbuttonbase_p.h:39
KFilePlacesModel::text
QString text(const QModelIndex &index) const
Definition: kfileplacesmodel.cpp:192
KFilePlacesModel::addPlace
void addPlace(const QString &text, const KUrl &url, const QString &iconName=QString(), const QString &appName=QString())
Definition: kfileplacesmodel.cpp:634
kiconloader.h
KFilePlacesModel::teardownActionForIndex
QAction * teardownActionForIndex(const QModelIndex &index) const
Definition: kfileplacesmodel.cpp:727
KDEPrivate::KUrlNavigatorPlacesSelector::KUrlNavigatorPlacesSelector
KUrlNavigatorPlacesSelector(QWidget *parent, KFilePlacesModel *placesModel)
Definition: kurlnavigatorplacesselector.cpp:40
KFilePlacesModel::setupNeeded
bool setupNeeded(const QModelIndex &index) const
Definition: kfileplacesmodel.cpp:182
QAbstractButton::setIcon
void setIcon(const QIcon &icon)
QWidget::update
void update()
KUrl
KFilePlacesModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const
Get the number of rows for a model index.
Definition: kfileplacesmodel.cpp:266
QIcon::pixmap
QPixmap pixmap(const QSize &size, Mode mode, State state) const
QList::size
int size() const
QWidget::width
int width() const
QMenu::clear
void clear()
QModelIndex::isValid
bool isValid() const
KDEPrivate::KUrlNavigatorPlacesSelector::placeActivated
void placeActivated(const KUrl &url)
Is send when a bookmark has been activated by the user.
KFilePlacesModel::icon
KIcon icon(const QModelIndex &index) const
Definition: kfileplacesmodel.cpp:187
QVariant::toInt
int toInt(bool *ok) const
QWidget::x
int x() const
KUrl::List::fromMimeData
static KUrl::List fromMimeData(const QMimeData *mimeData, KUrl::MetaDataMap *metaData=0)
KDEPrivate::KUrlNavigatorPlacesSelector::~KUrlNavigatorPlacesSelector
virtual ~KUrlNavigatorPlacesSelector()
Definition: kurlnavigatorplacesselector.cpp:65
kmenu.h
KDEPrivate::KUrlNavigatorPlacesSelector::selectedPlaceText
QString selectedPlaceText() const
Returns the selected bookmark.
Definition: kurlnavigatorplacesselector.cpp:138
KDEPrivate::KUrlNavigatorPlacesSelector::dropEvent
virtual void dropEvent(QDropEvent *event)
Definition: kurlnavigatorplacesselector.cpp:181
KFilePlacesModel::closestItem
QModelIndex closestItem(const KUrl &url) const
Returns the closest item for the URL url.
Definition: kfileplacesmodel.cpp:281
QAbstractButton::iconSize
QSize iconSize() const
QDropEvent
QPainter::drawPixmap
void drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
QList::isEmpty
bool isEmpty() const
QPainter
QModelIndex::row
int row() const
kurlnavigatorplacesselector_p.h
KIcon
kfileplacesmodel.h
QMenu::addSeparator
QAction * addSeparator()
QString
QAbstractButton::pixmap
const QPixmap * pixmap() const
KDEPrivate::KUrlNavigatorPlacesSelector::sizeHint
virtual QSize sizeHint() const
Definition: kurlnavigatorplacesselector.cpp:144
KDEPrivate::KUrlNavigatorButtonBase::DraggedHint
Definition: kurlnavigatorbuttonbase_p.h:60
QPixmap
QWidget::setAcceptDrops
void setAcceptDrops(bool on)
QAction::setData
void setData(const QVariant &userData)
QObject::setParent
void setParent(QObject *parent)
KDEPrivate::KUrlNavigatorPlacesSelector::paintEvent
virtual void paintEvent(QPaintEvent *event)
Draws the icon of the selected Url as content of the Url selector.
Definition: kurlnavigatorplacesselector.cpp:150
QSize
QPixmap::height
int height() const
QDragLeaveEvent
KUrl::List
QPersistentModelIndex
KDEPrivate::KUrlNavigatorPlacesSelector::dragLeaveEvent
virtual void dragLeaveEvent(QDragLeaveEvent *event)
Definition: kurlnavigatorplacesselector.cpp:173
QMimeData::hasUrls
bool hasUrls() const
QDragEnterEvent
KUrl::fileName
QString fileName(const DirectoryOptions &options=IgnoreTrailingSlash) const
KDEPrivate::KUrlNavigatorButtonBase::drawHoverBackground
void drawHoverBackground(QPainter *painter)
Definition: kurlnavigatorbuttonbase.cpp:109
QAction
QWidget::removeAction
void removeAction(QAction *action)
QSize::height
int height() const
KDEPrivate::KUrlNavigatorButtonBase::setDisplayHintEnabled
void setDisplayHintEnabled(DisplayHint hint, bool enable)
Definition: kurlnavigatorbuttonbase.cpp:67
QPaintEvent
kicon.h
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QWidget::actions
QList< QAction * > actions() const
QVariant::toString
QString toString() const
QWidget::height
int height() const
KFilePlacesModel
This class is a list view model.
Definition: kfileplacesmodel.h:40
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:27:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KFile

Skip menu "KFile"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

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