30 #include <QtGui/QDragEnterEvent>
31 #include <QtGui/QDragLeaveEvent>
32 #include <QtGui/QDropEvent>
33 #include <QtGui/QPainter>
34 #include <QtGui/QPixmap>
43 m_placesModel(placesModel)
45 setFocusPolicy(Qt::NoFocus);
47 m_placesMenu =
new KMenu(
this);
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*)));
60 setMenu(m_placesMenu);
69 void KUrlNavigatorPlacesSelector::updateMenu()
71 m_placesMenu->clear();
74 const int rowCount = m_placesModel->
rowCount();
75 for (
int i = 0; i < rowCount; ++i) {
76 QModelIndex index = m_placesModel->
index(i, 0);
78 m_placesModel->
text(index),
80 m_placesMenu->addAction(action);
82 if (i == m_selectedItem) {
83 setIcon(m_placesModel->
icon(index));
87 updateTeardownAction();
90 void KUrlNavigatorPlacesSelector::updateTeardownAction()
92 const int rowCount = m_placesModel->
rowCount();
93 if (m_placesMenu->actions().size()==rowCount+2) {
95 QAction *action = m_placesMenu->actions().at(rowCount+1);
96 m_placesMenu->removeAction(action);
100 action = m_placesMenu->actions().at(rowCount);
101 m_placesMenu->removeAction(action);
105 const QModelIndex index = m_placesModel->
index(m_selectedItem, 0);
108 teardown->setParent(m_placesMenu);
109 teardown->setData(
"teardownAction");
111 m_placesMenu->addSeparator();
112 m_placesMenu->addAction(teardown);
118 const QModelIndex index = m_placesModel->
closestItem(url);
119 if (index.isValid()) {
120 m_selectedItem = index.row();
122 setIcon(m_placesModel->
icon(index));
127 setIcon(
KIcon(
"folder"));
129 updateTeardownAction();
134 const QModelIndex index = m_placesModel->
index(m_selectedItem, 0);
135 return index.isValid() ? m_placesModel->
url(index) :
KUrl();
140 const QModelIndex index = m_placesModel->
index(m_selectedItem, 0);
141 return index.isValid() ? m_placesModel->
text(index) :
QString();
146 const int height = KUrlNavigatorButtonBase::sizeHint().height();
147 return QSize(height, height);
153 QPainter painter(
this);
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);
165 if (event->mimeData()->hasUrls()) {
167 event->acceptProposedAction();
175 KUrlNavigatorButtonBase::dragLeaveEvent(event);
187 if (urlList.isEmpty()) {
190 foreach(
const KUrl &url, urlList) {
191 KMimeType::Ptr
mimetype = KMimeType::findByUrl(url);
192 if (mimetype->is(
"inode/directory")) {
198 void KUrlNavigatorPlacesSelector::activatePlace(
QAction* action)
200 Q_ASSERT(action != 0);
201 if (action->data().toString()==
"teardownAction") {
202 QModelIndex index = m_placesModel->
index(m_selectedItem, 0);
207 QModelIndex index = m_placesModel->
index(action->data().toInt(), 0);
209 m_lastClickedIndex = QPersistentModelIndex();
212 connect(m_placesModel, SIGNAL(setupDone(QModelIndex,
bool)),
213 this, SLOT(onStorageSetupDone(QModelIndex,
bool)));
215 m_lastClickedIndex = index;
218 }
else if (index.isValid()) {
219 m_selectedItem = index.row();
220 setIcon(m_placesModel->
icon(index));
221 updateTeardownAction();
226 void KUrlNavigatorPlacesSelector::onStorageSetupDone(
const QModelIndex &index,
bool success)
228 if (m_lastClickedIndex == index) {
230 m_selectedItem = index.row();
231 setIcon(m_placesModel->
icon(index));
232 updateTeardownAction();
235 m_lastClickedIndex = QPersistentModelIndex();
241 #include "kurlnavigatorplacesselector_p.moc"
KUrl url(const QModelIndex &index) const
virtual void dragEnterEvent(QDragEnterEvent *event)
void requestSetup(const QModelIndex &index)
void updateSelection(const KUrl &url)
Updates the selection dependent from the given URL url.
MimetypeJob * mimetype(const KUrl &url, JobFlags flags=DefaultFlags)
KUrl selectedPlaceUrl() const
Returns the selected bookmark.
void requestTeardown(const QModelIndex &index)
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Get the children model index for the given row and column.
QString text(const QModelIndex &index) const
void addPlace(const QString &text, const KUrl &url, const QString &iconName=QString(), const QString &appName=QString())
QAction * teardownActionForIndex(const QModelIndex &index) const
KUrlNavigatorPlacesSelector(QWidget *parent, KFilePlacesModel *placesModel)
bool setupNeeded(const QModelIndex &index) const
int rowCount(const QModelIndex &parent=QModelIndex()) const
Get the number of rows for a model index.
void placeActivated(const KUrl &url)
Is send when a bookmark has been activated by the user.
KIcon icon(const QModelIndex &index) const
static KUrl::List fromMimeData(const QMimeData *mimeData, KUrl::MetaDataMap *metaData=0)
virtual ~KUrlNavigatorPlacesSelector()
QString selectedPlaceText() const
Returns the selected bookmark.
virtual void dropEvent(QDropEvent *event)
QModelIndex closestItem(const KUrl &url) const
Returns the closest item for the URL url.
virtual QSize sizeHint() const
virtual void paintEvent(QPaintEvent *event)
Draws the icon of the selected Url as content of the Url selector.
virtual void dragLeaveEvent(QDragLeaveEvent *event)
QString fileName(const DirectoryOptions &options=IgnoreTrailingSlash) const
This class is a list view model.