fix tricky regression noticed by Vyacheslav Tokarev on Google Reader.
[kdelibs.git] / kfile / kfileplacesselector.cpp
blob5d639a78580971d73b0370fbf5c65279f55bf8bf
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 ***************************************************************************/
21 #include "kfileplacesselector_p.h"
23 #include "kurlnavigator.h"
25 #include <kiconloader.h>
26 #include <kglobalsettings.h>
27 #include <kfileplacesmodel.h>
28 #include <kmenu.h>
29 #include <kmimetype.h>
30 #include <kdebug.h>
32 #include <QtGui/QDragEnterEvent>
33 #include <QtGui/QDragLeaveEvent>
34 #include <QtGui/QDropEvent>
35 #include <QtGui/QPainter>
36 #include <QtGui/QPixmap>
37 #include <kicon.h>
39 KFilePlacesSelector::KFilePlacesSelector(KUrlNavigator* parent, KFilePlacesModel* placesModel) :
40 KUrlButton(parent),
41 m_selectedItem(-1),
42 m_placesModel(placesModel)
44 setFocusPolicy(Qt::NoFocus);
46 m_placesMenu = new KMenu(this);
48 updateMenu();
50 connect(m_placesModel, SIGNAL(rowsInserted(const QModelIndex&, int, int)),
51 this, SLOT(updateMenu()));
52 connect(m_placesModel, SIGNAL(rowsRemoved(const QModelIndex&, int, int)),
53 this, SLOT(updateMenu()));
54 connect(m_placesModel, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
55 this, SLOT(updateMenu()));
56 connect(m_placesMenu, SIGNAL(triggered(QAction*)),
57 this, SLOT(activatePlace(QAction*)));
59 setMenu(m_placesMenu);
61 setAcceptDrops(true);
64 KFilePlacesSelector::~KFilePlacesSelector()
68 void KFilePlacesSelector::updateMenu()
70 m_placesMenu->clear();
72 updateSelection(m_selectedUrl);
73 const int rowCount = m_placesModel->rowCount();
74 for (int i = 0; i < rowCount; ++i) {
75 QModelIndex index = m_placesModel->index(i, 0);
76 QAction* action = new QAction(m_placesModel->icon(index),
77 m_placesModel->text(index),
78 m_placesMenu);
79 m_placesMenu->addAction(action);
80 action->setData(i);
81 if (i == m_selectedItem) {
82 setIcon(m_placesModel->icon(index));
86 updateTeardownAction();
89 void KFilePlacesSelector::updateTeardownAction()
91 const int rowCount = m_placesModel->rowCount();
92 if (m_placesMenu->actions().size()==rowCount+2) {
93 // remove teardown action
94 QAction *action = m_placesMenu->actions().at(rowCount+1);
95 m_placesMenu->removeAction(action);
96 delete action;
98 // remove separator
99 action = m_placesMenu->actions().at(rowCount);
100 m_placesMenu->removeAction(action);
101 delete action;
104 const QModelIndex index = m_placesModel->index(m_selectedItem, 0);
105 QAction *teardown = m_placesModel->teardownActionForIndex(index);
106 if (teardown!=0) {
107 teardown->setParent(m_placesMenu);
108 teardown->setData("teardownAction");
110 m_placesMenu->addSeparator();
111 m_placesMenu->addAction(teardown);
115 void KFilePlacesSelector::updateSelection(const KUrl& url)
117 const QModelIndex index = m_placesModel->closestItem(url);
118 if (index.isValid()) {
119 m_selectedItem = index.row();
120 m_selectedUrl = url;
121 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"));
129 updateTeardownAction();
132 KUrl KFilePlacesSelector::selectedPlaceUrl() const
134 const QModelIndex index = m_placesModel->index(m_selectedItem, 0);
135 return index.isValid() ? m_placesModel->url(index) : KUrl();
138 QString KFilePlacesSelector::selectedPlaceText() const
140 const QModelIndex index = m_placesModel->index(m_selectedItem, 0);
141 return index.isValid() ? m_placesModel->text(index) : QString();
144 QSize KFilePlacesSelector::sizeHint() const
146 const int height = KUrlButton::sizeHint().height();
147 return QSize(height, height);
150 void KFilePlacesSelector::paintEvent(QPaintEvent* event)
152 Q_UNUSED(event);
153 QPainter painter(this);
154 drawHoverBackground(&painter);
156 // draw icon
157 const QPixmap pixmap = icon().pixmap(QSize(22, 22), QIcon::Normal);
158 const int x = (width() - pixmap.width()) / 2;
159 const int y = (height() - pixmap.height()) / 2;
160 painter.drawPixmap(x, y, pixmap);
163 void KFilePlacesSelector::dragEnterEvent(QDragEnterEvent* event)
165 if (event->mimeData()->hasUrls()) {
166 setDisplayHintEnabled(DraggedHint, true);
167 event->acceptProposedAction();
169 update();
173 void KFilePlacesSelector::dragLeaveEvent(QDragLeaveEvent* event)
175 KUrlButton::dragLeaveEvent(event);
177 setDisplayHintEnabled(DraggedHint, false);
178 update();
181 void KFilePlacesSelector::dropEvent(QDropEvent* event)
183 setDisplayHintEnabled(DraggedHint, false);
184 update();
186 const KUrl::List urlList = KUrl::List::fromMimeData(event->mimeData());
187 if (urlList.isEmpty()) {
188 return;
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);
198 void KFilePlacesSelector::activatePlace(QAction* action)
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;
207 QModelIndex index = m_placesModel->index(action->data().toInt(), 0);
209 m_lastClickedIndex = QPersistentModelIndex();
211 if (m_placesModel->setupNeeded(index)) {
212 connect(m_placesModel, SIGNAL(setupDone(const QModelIndex &, bool)),
213 this, SLOT(onStorageSetupDone(const QModelIndex &, bool)));
215 m_lastClickedIndex = index;
216 m_placesModel->requestSetup(index);
217 return;
219 else if (index.isValid()) {
220 m_selectedItem = index.row();
221 setIcon(m_placesModel->icon(index));
222 updateTeardownAction();
223 emit placeActivated(m_placesModel->url(index));
227 void KFilePlacesSelector::onStorageSetupDone(const QModelIndex &index, bool success)
229 if (m_lastClickedIndex==index) {
230 if (success) {
231 m_selectedItem = index.row();
232 setIcon(m_placesModel->icon(index));
233 updateTeardownAction();
234 emit placeActivated(m_placesModel->url(index));
236 m_lastClickedIndex = QPersistentModelIndex();
240 #include "kfileplacesselector_p.moc"