KFile
kfileplacesselector.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "kfileplacesselector_p.h"
00022
00023 #include "kurlnavigator.h"
00024
00025 #include <kiconloader.h>
00026 #include <kglobalsettings.h>
00027 #include <kfileplacesmodel.h>
00028 #include <kmenu.h>
00029 #include <kmimetype.h>
00030 #include <kdebug.h>
00031
00032 #include <QtGui/QDragEnterEvent>
00033 #include <QtGui/QDragLeaveEvent>
00034 #include <QtGui/QDropEvent>
00035 #include <QtGui/QPainter>
00036 #include <QtGui/QPixmap>
00037 #include <kicon.h>
00038
00039 KFilePlacesSelector::KFilePlacesSelector(KUrlNavigator* parent, KFilePlacesModel* placesModel) :
00040 KUrlButton(parent),
00041 m_selectedItem(-1),
00042 m_placesModel(placesModel)
00043 {
00044 setFocusPolicy(Qt::NoFocus);
00045
00046 m_placesMenu = new KMenu(this);
00047
00048 updateMenu();
00049
00050 connect(m_placesModel, SIGNAL(rowsInserted(const QModelIndex&, int, int)),
00051 this, SLOT(updateMenu()));
00052 connect(m_placesModel, SIGNAL(rowsRemoved(const QModelIndex&, int, int)),
00053 this, SLOT(updateMenu()));
00054 connect(m_placesModel, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
00055 this, SLOT(updateMenu()));
00056 connect(m_placesMenu, SIGNAL(triggered(QAction*)),
00057 this, SLOT(activatePlace(QAction*)));
00058
00059 setMenu(m_placesMenu);
00060
00061 setAcceptDrops(true);
00062 }
00063
00064 KFilePlacesSelector::~KFilePlacesSelector()
00065 {
00066 }
00067
00068 void KFilePlacesSelector::updateMenu()
00069 {
00070 m_placesMenu->clear();
00071
00072 updateSelection(m_selectedUrl);
00073 const int rowCount = m_placesModel->rowCount();
00074 for (int i = 0; i < rowCount; ++i) {
00075 QModelIndex index = m_placesModel->index(i, 0);
00076 QAction* action = new QAction(m_placesModel->icon(index),
00077 m_placesModel->text(index),
00078 m_placesMenu);
00079 m_placesMenu->addAction(action);
00080 action->setData(i);
00081 if (i == m_selectedItem) {
00082 setIcon(m_placesModel->icon(index));
00083 }
00084 }
00085
00086 updateTeardownAction();
00087 }
00088
00089 void KFilePlacesSelector::updateTeardownAction()
00090 {
00091 const int rowCount = m_placesModel->rowCount();
00092 if (m_placesMenu->actions().size()==rowCount+2) {
00093
00094 QAction *action = m_placesMenu->actions().at(rowCount+1);
00095 m_placesMenu->removeAction(action);
00096 delete action;
00097
00098
00099 action = m_placesMenu->actions().at(rowCount);
00100 m_placesMenu->removeAction(action);
00101 delete action;
00102 }
00103
00104 const QModelIndex index = m_placesModel->index(m_selectedItem, 0);
00105 QAction *teardown = m_placesModel->teardownActionForIndex(index);
00106 if (teardown!=0) {
00107 teardown->setParent(m_placesMenu);
00108 teardown->setData("teardownAction");
00109
00110 m_placesMenu->addSeparator();
00111 m_placesMenu->addAction(teardown);
00112 }
00113 }
00114
00115 void KFilePlacesSelector::updateSelection(const KUrl& url)
00116 {
00117 const QModelIndex index = m_placesModel->closestItem(url);
00118 if (index.isValid()) {
00119 m_selectedItem = index.row();
00120 m_selectedUrl = url;
00121 setIcon(m_placesModel->icon(index));
00122 }
00123 else {
00124 m_selectedItem = -1;
00125
00126
00127 setIcon(KIcon("folder"));
00128 }
00129 updateTeardownAction();
00130 }
00131
00132 KUrl KFilePlacesSelector::selectedPlaceUrl() const
00133 {
00134 const QModelIndex index = m_placesModel->index(m_selectedItem, 0);
00135 return index.isValid() ? m_placesModel->url(index) : KUrl();
00136 }
00137
00138 QString KFilePlacesSelector::selectedPlaceText() const
00139 {
00140 const QModelIndex index = m_placesModel->index(m_selectedItem, 0);
00141 return index.isValid() ? m_placesModel->text(index) : QString();
00142 }
00143
00144 QSize KFilePlacesSelector::sizeHint() const
00145 {
00146 const int height = KUrlButton::sizeHint().height();
00147 return QSize(height, height);
00148 }
00149
00150 void KFilePlacesSelector::paintEvent(QPaintEvent* event)
00151 {
00152 Q_UNUSED(event);
00153 QPainter painter(this);
00154 drawHoverBackground(&painter);
00155
00156
00157 const QPixmap pixmap = icon().pixmap(QSize(22, 22), QIcon::Normal);
00158 const int x = (width() - pixmap.width()) / 2;
00159 const int y = (height() - pixmap.height()) / 2;
00160 painter.drawPixmap(x, y, pixmap);
00161 }
00162
00163 void KFilePlacesSelector::dragEnterEvent(QDragEnterEvent* event)
00164 {
00165 if (event->mimeData()->hasUrls()) {
00166 setDisplayHintEnabled(DraggedHint, true);
00167 event->acceptProposedAction();
00168
00169 update();
00170 }
00171 }
00172
00173 void KFilePlacesSelector::dragLeaveEvent(QDragLeaveEvent* event)
00174 {
00175 KUrlButton::dragLeaveEvent(event);
00176
00177 setDisplayHintEnabled(DraggedHint, false);
00178 update();
00179 }
00180
00181 void KFilePlacesSelector::dropEvent(QDropEvent* event)
00182 {
00183 setDisplayHintEnabled(DraggedHint, false);
00184 update();
00185
00186 const KUrl::List urlList = KUrl::List::fromMimeData(event->mimeData());
00187 if (urlList.isEmpty()) {
00188 return;
00189 }
00190 foreach(const KUrl &url, urlList) {
00191 KMimeType::Ptr mimetype = KMimeType::findByUrl(url);
00192 if (mimetype->is("inode/directory")) {
00193 m_placesModel->addPlace(url.fileName(), url);
00194 }
00195 }
00196 }
00197
00198 void KFilePlacesSelector::activatePlace(QAction* action)
00199 {
00200 Q_ASSERT(action != 0);
00201 if (action->data().toString()=="teardownAction") {
00202 QModelIndex index = m_placesModel->index(m_selectedItem, 0);
00203 m_placesModel->requestTeardown(index);
00204 return;
00205 }
00206
00207 QModelIndex index = m_placesModel->index(action->data().toInt(), 0);
00208
00209 m_lastClickedIndex = QPersistentModelIndex();
00210
00211 if (m_placesModel->setupNeeded(index)) {
00212 connect(m_placesModel, SIGNAL(setupDone(const QModelIndex &, bool)),
00213 this, SLOT(onStorageSetupDone(const QModelIndex &, bool)));
00214
00215 m_lastClickedIndex = index;
00216 m_placesModel->requestSetup(index);
00217 return;
00218 }
00219 else if (index.isValid()) {
00220 m_selectedItem = index.row();
00221 setIcon(m_placesModel->icon(index));
00222 updateTeardownAction();
00223 emit placeActivated(m_placesModel->url(index));
00224 }
00225 }
00226
00227 void KFilePlacesSelector::onStorageSetupDone(const QModelIndex &index, bool success)
00228 {
00229 if (m_lastClickedIndex==index) {
00230 if (success) {
00231 m_selectedItem = index.row();
00232 setIcon(m_placesModel->icon(index));
00233 updateTeardownAction();
00234 emit placeActivated(m_placesModel->url(index));
00235 }
00236 m_lastClickedIndex = QPersistentModelIndex();
00237 }
00238 }
00239
00240 #include "kfileplacesselector_p.moc"
00241